Archived

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

-Fruz-

Move to cursor

Recommended Posts

-Fruz-    122
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
krez    443
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
krez    443
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
-Fruz-    122
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
-Fruz-    122
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
krez    443
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
TerranFury    142
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 vector
velocity.x = cursor.x - ship.x
velocity.y = cursor.y - ship.y

' Normalize velocity vector
magnitude = sqr(velocity.x^2 + velocity.y^2)
velocity.x = velocity.x / magnitude
velocity.y = velocity.y / magnitude

' Multiply velocity vector by speed
velocity.x = velocity.x * speed
velocity.y = velocity.y * speed

' Initialize bullet position
bullet.x = ship.x
bullet.y = ship.y




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


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

Share this post


Link to post
Share on other sites
-Fruz-    122
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 this post


Link to post
Share on other sites
Dracoliche    122
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 this post


Link to post
Share on other sites
Cedric    158
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 this post


Link to post
Share on other sites
-Fruz-    122
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, mY
Dim Running As Boolean '' Bol to stop and run

Function MoveToCursor() '' Loop
Do While Running
DoEvents
Obj1.Move Obj1.Left + mX, Obj1.Top + mY
Loop
End Function

Private Sub Form_DblClick()
Running = Not Running
End Sub

Private 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)
MoveToCursor
End Sub

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


Link to post
Share on other sites
glJunkie    122
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 this post


Link to post
Share on other sites
grhodes_at_work    1385
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.

Share this post


Link to post
Share on other sites