Jump to content
  • Advertisement

Archived

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

-Fruz-

Move to cursor

This topic is 6032 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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 Single
dX = X2 - X1
dY = Y2 - Y1
If dX <> 0
Theta = Atn(dY/dX)
Else
Theta = Pi / 2
End 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) * Speed
BulletYSpeed = 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)

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!