Move to cursor

Started by
16 comments, last by -Fruz- 22 years ago
I''m making a game in VB n have a little prob. I have a ship that is ment to shoot a bullet towards a place I choose by clicking my mouse... From the ship to the point where I clicked... Can anybody help me? Plz! JFK (jfk-post@online.no)
Advertisement
you have to get the two points P1 & P2 (P1 = the ship''s coordinates, P2 = where you clicked). get the angle of the shot from there:
Dim Theta As SingledX = X2 - X1dY = Y2 - Y1If dX <> 0  Theta = Atn(dY/dX)Else  Theta = Pi / 2End If 

Theta will now hold the angle (in radians). you''ll have to define Pi (3.1415....) also. i haven''t done this in VB for a while, so i don''t recall exactly, but under certain conditions Theta will be off by 90 degrees (Pi/2 radians), so you''ll have to add it back in:
If dY < 0 Then Theta = Theta + Pi/2 

only i''m not sure if the condition is "dY < 0"... but you''ll figure it out i''m sure...
so, the "bullet" should travel @ angle Theta @ some speed.
BulletXSpeed = Cos(Theta) * SpeedBulletYSpeed = Sin(Theta) * Speed 

then, set the initial position of the bullet to where the ship is, and just add those two numbers to the (X,Y) of the bullet each frame.
HTH.

--- krez (krezisback@aol.com)
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
Thanx
Hmmm... It doesn''t work :\
really?
what exactly is wrong?

--- krez (krezisback@aol.com)
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
Not much... Just the fact that it goes a completly wrong way... :\
quote:Original post by krez
i haven''t done this in VB for a while, so i don''t recall exactly, but under certain conditions Theta will be off by 90 degrees (Pi/2 radians), so you''ll have to add it back in:
If dY < 0 Then Theta = Theta + Pi/2  

only i''m not sure if the condition is "dY < 0"... but you''ll figure it out i''m sure...

well, i tried this out real quick, and i did make a mistake there... that line should be:
If dX < 0 Then THETA = THETA + PI 

i was close though, dontcha think?
if you didn''t see what the problem was (other than "going completely the wrong way") then you should read up on your trig...
HTH.

--- krez (krezisback@aol.com)
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
This is what I did:
----------------------------------------------------------------
Dim Theta As Single
X2 = mousepos.X * Screen.TwipsPerPixelX
Y2 = mousepos.Y * Screen.TwipsPerPixelY
X1 = Picture1.Left
Y1 = Picture1.Top
dX = X2 - X1
dY = Y2 - Y1
If dX <> 0 Then
Theta = Atn(dY / dX)
Else
Theta = Pi / 2
End If
If dY < 0 Then
Theta = Theta + Pi
Else
Theta = Pi / 2
End If
BXs = Cos(Theta) * 10
BYs = Sin(Theta) * 10
Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()
Picture1.Move Picture1.Left + BXs, Picture1.Top + BYs
End Sub
----------------------------------------------------------------
This works... Allmost. The only thing wrong, is that it doesn''t move down or left. But thanx VERY much for your help! It has been really usefull!
This is a modified code of the above...
----------------------------------------------------------------
Dim X1, X2, Y1, Y2, Pi, dY, dX, BXs, BYs
Option Explicit
Dim mousepos As POINTAPI

Private Sub Form_Click()
GetCursorPos mousepos

Dim Theta As Single
X2 = mousepos.X * Screen.TwipsPerPixelX
Y2 = mousepos.Y * Screen.TwipsPerPixelY
X1 = Picture1.Left
Y1 = Picture1.Top
dX = X2 - X1
dY = Y2 - Y1
If dX > 0 Then
Theta = Atn(dY / dX)
Else
Theta = Atn(dY / dX) ''???
End If
BXs = Cos(Theta) * 10
BYs = Sin(Theta) * 10
Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()
Picture1.Move Picture1.Left + BXs, Picture1.Top + BYs
End Sub
----------------------------------------------------------------
This allmost works''s to, but when i click on the left side of the picture, it goes the opposite direction of where it''s suposed to go... :\ I tried makin the number negative like this:
------------------------
BXs = -(Cos(Theta) * 10)
BYs = -(Sin(Theta) * 10)
------------------------
That didn''t work so I tried this:
-------------------------------------------------
BXs = (Cos(Theta) * 10) - ((Cos(Theta) * 10) * 2)

BYs = (Sin(Theta) * 10) - ((Sin(Theta) * 10) * 2)
-------------------------------------------------
This didn''t work either, so I thought I should ask one of u!

JFK

your first one looked ok, but you put in a bit extra:
quote:Original post by -Fruz-
If dY < 0 Then
Theta = Theta + Pi
Else
Theta = Pi / 2
End If

should be "If dY < 0 Then Theta = Theta + Pi"... that ELSE is messing you up.

--- krez (krezisback@aol.com)
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])

This topic is closed to new replies.

Advertisement