Click here to Skip to main content
15,892,809 members
Home / Discussions / Visual Basic
   

Visual Basic

 
Questionfunction in vb Pin
asha_s30-Mar-08 5:03
asha_s30-Mar-08 5:03 
GeneralRe: function in vb Pin
Dave Kreskowiak31-Mar-08 4:30
mveDave Kreskowiak31-Mar-08 4:30 
GeneralEZNamespaceExtensions.Net Help Pin
Fahad Sadah30-Mar-08 0:22
Fahad Sadah30-Mar-08 0:22 
GeneralRe: EZNamespaceExtensions.Net Help Pin
Dave Kreskowiak1-Apr-08 2:54
mveDave Kreskowiak1-Apr-08 2:54 
GeneralRe: EZNamespaceExtensions.Net Help Pin
Fahad Sadah1-Apr-08 5:59
Fahad Sadah1-Apr-08 5:59 
Generalusing VB.NET as mdiParent for another application Pin
Member 408185329-Mar-08 16:19
Member 408185329-Mar-08 16:19 
GeneralRe: using VB.NET as mdiParent for another application Pin
jzonthemtn30-Mar-08 4:05
jzonthemtn30-Mar-08 4:05 
GeneralHelp with a Pool style game Pin
cmikaiti29-Mar-08 16:15
cmikaiti29-Mar-08 16:15 
I did a significant amount of programming in QBasic, though that was when I was much younger. I'm trying to move on up, and have 'began' developing many games in visual basic... though most never got beyond the setup phase.

I've been working for some time on a pool game, but am having several problems. I'm not looking for anyone to parse through my code, but included it all in case it is needed. I specifically need help with my trig functions.

The last function in my code (where I need help) is for a timer, with which I reference its interval for my acceleration and velocity formulas. They seem to be working ok. As I understand it, my ATN() function should be returning an angle based on change in y value divided by change in x value updated each interval of time.

I have 6 separate displays for each ball on my screen to show:
x position
y position
distance between it and all other balls (while dragging)
velocity as I drag it around
acceleration as I drag it around
and angle as I drag it around

The problem is that the display for angle only ever shows a -1, 0, 1 (and the very rare 2 for some reason). After I let go of the ball, it flies off (as desired) in the direction it was launched, however the angle seems to be constrained to (guessing) 0, 30, 45, 60 and 90 for each quadrant. I'm afraid that this is based on my using the Sin or Cosine of the velocity and angle it was traveling when I let go of the ball, but I'm really at a loss as to why... it all looks correct to me.

(I also can't get any balls that are hit by my ball dragging across them to move, but that is really a problem I can likely figure out once I get this first issue resolved).

Also, any tips on how to modularize my code better would be appreciated, but I would need pretty specific tips... I've tried creating my own functions before, but they become as bloated as the rest of my code as I am forced to add new things to it periodically.

my e-mail address is cmikaiti@yahoo.com if anyone wishes to contact me personally, where I would be happy to send you the actual files if it helps.

Thanks for any and all helps, tips, and constructive criticism.

-Chris Mikaitis


My current code (messy and uncommented, unfortunately) is as follows:

Option Explicit
Dim x1, y1 As Integer
Dim i, j As Integer
Dim Drag As Boolean
Dim BallNum, GrabbedBall As Integer
Dim VGrabbedBall(1 To 10), AGrabbedBall(1 To 10), AngleGrabbedBall(1 To 10) As Long
Dim VHitBall(1 To 10), AHitBall(1 To 10), AngleHitBall(1 To 10) As Long
Dim BallDirx(1 To 10), balldiry(1 To 10) As Integer
Dim OldX, OldY, OldVelocity As Long


Private Sub Command1_Click()

For _
i = 1 To BallNum
Unload Ball(i)
Unload BallXVal(i)
Unload BallYVal(i)
Unload Proximity(i)
Unload Velocity(i)
Unload Acceleration(i)
Unload Angle(i)
Next i

Unload Form1

End Sub

Private Sub Form_Load()

Randomize

x1 = 0
y1 = 0
i = 0
j = 0
OldX = 0
OldY = 0
OldVelocity = 0
BallNum = 5
GrabbedBall = 1
Drag = False

For _
i = 1 To 10
VGrabbedBall(i) = 0
AGrabbedBall(i) = 0
Next i

For _
i = 1 To BallNum
Load Ball(i)

Do
Ball(i).Left = Int(Rnd(1) * 15000)
Ball(i).Top = Int(Rnd(1) * 11000)
Ball(i).Visible = True
Loop Until _
Ball(i).Left > Table.Left And _
Ball(i).Left + Ball(i).Width < Table.Left + Table.Width And _
Ball(i).Top > Table.Top And _
Ball(i).Top + Ball(i).Height < Table.Top + Table.Height



Load BallXVal(i)
BallXVal(i).Text = Ball(i).Left + 0.5 * Ball(i).Width
BallXVal(i).Visible = True
BallXVal(i).Top = BallXVal(i - 1).Top + BallXVal(i - 1).Height

Load BallYVal(i)
BallYVal(i).Text = Ball(i).Top + 0.5 * Ball(i).Height
BallYVal(i).Visible = True
BallYVal(i).Top = BallYVal(i - 1).Top + BallYVal(i - 1).Height

Load Proximity(i)
Proximity(i).Visible = True
Proximity(i).Top = Proximity(i - 1).Top + Proximity(i - 1).Height
Proximity(i).Text = 0

Load Velocity(i)
Velocity(i).Visible = True
Velocity(i).Top = Velocity(i - 1).Top + Velocity(i - 1).Height
Velocity(i).Text = 0

Load Acceleration(i)
Acceleration(i).Visible = True
Acceleration(i).Top = Acceleration(i - 1).Top + Acceleration(i - 1).Height
Acceleration(i).Text = 0

Load Angle(i)
Angle(i).Visible = True
Angle(i).Top = Angle(i - 1).Top + Angle(i - 1).Height
Angle(i).Text = 0

Next i

End Sub

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

For i = 1 To BallNum

If _
X > Ball(i).Left And _
X < Ball(i).Left + Ball(i).Width And _
Y > Ball(i).Top And _
Y < Ball(i).Top + Ball(i).Height Then

If _
Button = 1 Then
x1 = X - Ball(i).Left
y1 = Y - Ball(i).Top

Drag = True
GrabbedBall = i
Ball(i).BorderColor = &HFF&
End If

End If

Next i

End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)

If _
Drag = True Then

For _
i = 1 To BallNum
Proximity(i).Text = Sqr(((Ball(GrabbedBall).Left - Ball(i).Left) ^ 2) + (Ball(GrabbedBall).Top - Ball(i).Top) ^ 2)

If _
0.5 * Ball(GrabbedBall).Width + 0.5 * Ball(i).Width > _
Sqr(((Ball(GrabbedBall).Left - Ball(i).Left) ^ 2) + (Ball(GrabbedBall).Top - Ball(i).Top) ^ 2) Then
Ball(GrabbedBall).Top = Y - y1
Ball(GrabbedBall).Left = X - x1
BallXVal(GrabbedBall).Text = Ball(GrabbedBall).Left + 0.5 * Ball(GrabbedBall).Width
BallYVal(GrabbedBall).Text = Ball(GrabbedBall).Top + 0.5 * Ball(GrabbedBall).Height

Command1.Caption = "hit"
VHitBall(i) = VGrabbedBall(GrabbedBall)
AHitBall(i) = AGrabbedBall(GrabbedBall)
AngleHitBall(i) = AngleGrabbedBall(GrabbedBall)

End If

Next i

End If


End Sub

Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Drag = False
Ball(GrabbedBall).BorderColor = &H0&

X = 0
Y = 0
x1 = 0
y1 = 0
End Sub

Private Sub Timer1_Timer()

If Drag = True Then
If Ball(GrabbedBall).Left <> OldX Then
AngleGrabbedBall(GrabbedBall) = Atn((Ball(GrabbedBall).Top - OldY) / (Ball(GrabbedBall).Left - OldX))
End If
Angle(GrabbedBall).Text = AngleGrabbedBall(GrabbedBall)

VGrabbedBall(GrabbedBall) = Sqr((Ball(GrabbedBall).Left - OldX) ^ 2 + (Ball(GrabbedBall).Top - OldY) ^ 2 / Timer1.Interval)
Velocity(GrabbedBall).Text = VGrabbedBall(GrabbedBall)

If OldX > Ball(GrabbedBall).Left Then
BallDirx(GrabbedBall) = -1
Else: BallDirx(GrabbedBall) = 1
End If

OldX = Ball(GrabbedBall).Left
OldY = Ball(GrabbedBall).Top

AGrabbedBall(GrabbedBall) = (VGrabbedBall(GrabbedBall) - OldVelocity) / Timer1.Interval
Acceleration(GrabbedBall).Text = AGrabbedBall(GrabbedBall)
OldVelocity = VGrabbedBall(GrabbedBall)
End If

For i = 1 To BallNum
Ball(i).Left = Ball(i).Left + BallDirx(i) * Cos(AngleGrabbedBall(i)) * VGrabbedBall(i)
Ball(i).Top = Ball(i).Top + BallDirx(i) * Sin(AngleGrabbedBall(i)) * VGrabbedBall(i)
If VGrabbedBall(i) < 10 Then
VGrabbedBall(i) = 0
Else
VGrabbedBall(i) = 0.9 * VGrabbedBall(i)
End If

If Ball(i) <> Ball(GrabbedBall) Then
Ball(i).Left = Ball(i).Left + BallDirx(i) * Cos(AngleHitBall(i)) * VHitBall(i)
Ball(i).Top = Ball(i).Top + BallDirx(i) * Sin(AngleHitBall(i)) * VHitBall(i)
VHitBall(i) = 0.9 * VHitBall(i)
End If

Next i

End Sub
GeneralRe: Help with a Pool style game Pin
Luc Pattyn29-Mar-08 16:35
sitebuilderLuc Pattyn29-Mar-08 16:35 
GeneralRe: Help with a Pool style game Pin
cmikaiti29-Mar-08 17:37
cmikaiti29-Mar-08 17:37 
QuestionWhere are applications installed when using ClickOnce for deployments ? Pin
David Mujica29-Mar-08 10:22
David Mujica29-Mar-08 10:22 
GeneralRe: Where are applications installed when using ClickOnce for deployments ? Pin
jzonthemtn29-Mar-08 11:06
jzonthemtn29-Mar-08 11:06 
Generalam new Pin
solkeens29-Mar-08 10:04
solkeens29-Mar-08 10:04 
GeneralRe: am new Pin
Luc Pattyn29-Mar-08 10:35
sitebuilderLuc Pattyn29-Mar-08 10:35 
QuestionMasked textbox Pin
bapu288929-Mar-08 9:58
bapu288929-Mar-08 9:58 
GeneralRe: Masked textbox Pin
Luc Pattyn29-Mar-08 10:39
sitebuilderLuc Pattyn29-Mar-08 10:39 
GeneralFormat of Crystal Reports ?. Pin
r_mohd29-Mar-08 0:00
r_mohd29-Mar-08 0:00 
GeneralRe: Format of Crystal Reports ?. Pin
ChandraRam31-Mar-08 1:55
ChandraRam31-Mar-08 1:55 
GeneralRe: Format of Crystal Reports ?. Pin
r_mohd31-Mar-08 21:20
r_mohd31-Mar-08 21:20 
GeneralRe: Format of Crystal Reports ?. Pin
ChandraRam31-Mar-08 22:45
ChandraRam31-Mar-08 22:45 
QuestionHow to make trial version software in vb.net Pin
Krishnraj28-Mar-08 20:46
Krishnraj28-Mar-08 20:46 
GeneralRe: How to make trial version software in vb.net Pin
Rupesh Kumar Swami28-Mar-08 21:34
Rupesh Kumar Swami28-Mar-08 21:34 
GeneralRe: How to make trial version software in vb.net Pin
Krishnraj28-Mar-08 23:30
Krishnraj28-Mar-08 23:30 
GeneralRe: How to make trial version software in vb.net Pin
Rupesh Kumar Swami29-Mar-08 2:00
Rupesh Kumar Swami29-Mar-08 2:00 
GeneralRe: How to make trial version software in vb.net Pin
Fahad Sadah30-Mar-08 0:25
Fahad Sadah30-Mar-08 0:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.