• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# Move to cursor

## Recommended Posts

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)

##### Share on other sites
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)

Thanx

##### Share on other sites
Hmmm... It doesn''t work :\

##### Share on other sites
really?
what exactly is wrong?

--- krez (krezisback@aol.com)

##### Share on other sites
Not much... Just the fact that it goes a completly wrong way... :\

##### Share on other sites
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)

##### Share on other sites
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!

##### Share on other sites
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

##### Share on other sites
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)

##### Share on other sites
You don't need to find the angle (theta). All you need to do is some simple vector arithmatic:

When you click:
  ' Get un-normalized velocity vectorvelocity.x = cursor.x - ship.xvelocity.y = cursor.y - ship.y' Normalize velocity vectormagnitude = sqr(velocity.x^2 + velocity.y^2)velocity.x = velocity.x / magnitudevelocity.y = velocity.y / magnitude' Multiply velocity vector by speedvelocity.x = velocity.x * speedvelocity.y = velocity.y * speed' Initialize bullet positionbullet.x = ship.xbullet.y = ship.y

Each frame:
  bullet.x = bullet.x + velocity.xbullet.y = bullet.y + velocity.y

Edited by - TerranFury on January 16, 2002 4:43:52 PM

##### Share on other sites
U sure?
It didn''t work for me... I wrote this:
----------------------------------------------------------------
Private Sub Form_Click()
VelocityX = mousepos.X - Ship.Left
VelocityY = mousepos.Y - Ship.Top
magnitude = Sqr(VelocityX ^ 2 + VelocityY ^ 2)
VelocityX = VelocityX / magnitude
VelocityY = VelocityY / magnitude
VelocityX = VelocityX * 10
VelocityY = VelocityY * 10
Bullet.top = Ship.top
Bullet.left = Ship.Left
TimerShoot.Enabled = True
End Sub

Private Sub TimerShoot_Timer()
Bullet.Move Bullet.Left + VelocityX, Bullet.Top + VelocityY
End Sub
----------------------------------------------------------------
Don''t worry, I have called the API needed...

##### Share on other sites
I''m guessing that Bullet is a PictureBox or ImageBox, and has its Visible property set to False, since you wouldn''t want it to show up before the player shoots. You need to make it visible at the appropriate time.

(That''s just an educated guess based on your code. No offence if you''ve already done that.)

##### Share on other sites
What is atn in c? I don''t know what it stands for or what it does, so I can''t figure it out.

##### Share on other sites
Yeah, TerranFury''s method is the best, and your code looks fine. What happens? It''s hard to debug you if you don''t tell us. I''m going to guess: are VelocityX and VelocityY declared in the general portion of your form so that their value is not 0 when you get to TimerShoot_Timer()?

Cédric

##### Share on other sites
Allthough I got it to work after a while with some long and stupid code I decided to trie doing it as easy as possible, but there is something wrong here! This is what i''we done...

  Dim mX, mYDim Running As Boolean '' Bol to stop and runFunction MoveToCursor() '' Loop Do While Running DoEvents Obj1.Move Obj1.Left + mX, Obj1.Top + mY LoopEnd FunctionPrivate Sub Form_DblClick() Running = Not RunningEnd SubPrivate Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) XSpace = Obj1.Left - X '' Just here incase I need it YSpace = Obj1.Top - Y ZSpace = Sqr(YSpace ^ 2 + XSpace ^ 2) angle = Atn(YSpace / ZSpace) ''ArcCos(YSpace / ZSpace) mX = 1 * Cos(angle) mY = 1 * Sin(angle) MoveToCursorEnd SubFunction ArcCos(Number) '' I used this somewhere in a little billiard prog.. thought i might need it ArcCos = Atn(-X / Sqr(-X * X + 1)) + 2 * Atn(1)End Function

##### Share on other sites
quote:
Original post by Muzlack
What is atn in c? I don''t know what it stands for or what it does, so I can''t figure it out.

Its arc-tangent. Thats atan() in C++. Dunno about c.
Tim.

##### Share on other sites
quote:
Original post by glJunkie
[quote]Original post by Muzlack
What is atn in c? I don''t know what it stands for or what it does, so I can''t figure it out.

Its arc-tangent. Thats atan() in C++. Dunno about c.
Tim.

In C it would be....."atan()"!

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627644
• Total Posts
2978373

• 10
• 12
• 22
• 13
• 33