3 Attachment(s)
Motion at an Angle 2D (For the Games FAQ)
Well seen as though this has started to come up quite a lot I thought I'd get round to writing a quick tutorial to show you how it all works :).
Well to start off here is an image of the basic setup:
http://www.vbforums.com/attachment.p...chmentid=34450
This is based on the origin being in the bottom left corner. ie. As Y increases you go up and as X increases you go right.
So now we need to find the values of dX and dY. This is quite simple actually, its just a right handed triangle and using Sin() & Cos(). Only tricky part is working out where we use Sin() and where we use Cos().
So lets have a little Trig first:
http://www.vbforums.com/attachment.p...chmentid=34451
Right so on this diagram I can say:
lX = Cos(B) * l
lY = Sin(B) * l
Now hold up a minute, beore you say you have the answer already just take notice of where the angle is. Yep the angle is against the bottom and on our first pic its on the Side. This causes the major differance in where we use the Sin()'s & Cos()'s so to fit this onto our problem we just rotate/flip the triangle to fit the first pic.
http://www.vbforums.com/attachment.p...chmentid=34452
Right so now we have the image rotated to fit the right angle you might notcie that if A == B then lY == dX and lX == dY. Yes I know maybe it wasn't a good idea to label them lX & lY but you see that now this means the Sin() & Cos() have swaped which is on X and Y.
So now we can say:
dX = Sin(A) * d
dY = Cos(A) * d
Before I end this just want to add because VB and quite a few other things in computing take the origin to be the Top Left that to make this chnage is simple. If you keep the Angle measured from the same place you just use the negative for the Y, giving:
dX = Sin(A) * d
dY = -Cos(A) * d
Given all this you should be able to figure out how to work out which way round these go depending on your axis setup but I've shown the two most popular there.
Now for the actual motion:
This is really easy, seen as though we added the d value anyway. This is simply how fast you want the overal speed to be. So each frame we would do:
Code:
dX = Sin(A) * d
dY = -Cos(A) * d
XPos = XPos + dX
YPos = YPos + dY
This will cause your object to move with Speed d in direction A.
Re: Motion at an Angle 2D (For the Games FAQ)
Very nice tutorial.
I'd add the name of that type of 2D graph into the FAQ -- that really helped my understanding learning that Cartesian and I think Polar (???) are the same yet the way Polar works more closely related a game (usually). As you have a point and a distance or an angle and a distance on a polar plane.
Re: Motion at an Angle 2D (For the Games FAQ)
Don't forget to convert the A and B angles to radians ;)
Cos(A * Pi/180)
Sin(A * Pi/180)
Re: Motion at an Angle 2D (For the Games FAQ)
Quote:
Originally Posted by Jacob Roman
Don't forget to convert the A and B angles to radians ;)
Cos(A * Pi/180)
Sin(A * Pi/180)
Good point yea, but mind I'd expect you'd work in radians from the start anyway if its a game cos having loads of * Pi/180 is gonna slow things down ;).
Re: Motion at an Angle 2D (For the Games FAQ)
Note that your trigs are going to slow down much more, and generally in a game you try to avoid polar cordinates as much as possible for that reason, as last resort you use a table of precalculated values on the trigs. For objects in motion you store the cartesian velocity vector, and add the velocity to the position each frame. Polar cordinates also make motion unrealistic. Otherwise if you insist on rotation, especially repeated with the same angle values, then you can save the calculation of trigs by using the same matrix for every rotation, and usually you can do with that unless you have angular accelartion.
Re: Motion at an Angle 2D (For the Games FAQ)
Quick question: So how do you find out the angle from dX and dY? Last thing I knew there wasn't a sin/cos/tan inverse function...
Re: Motion at an Angle 2D (For the Games FAQ)
There is an ArcTangent function. To avoid losing sign I use this
Code:
Option Explicit
Public Const PI As Double = 3.14159265358979
Public Const PI½ As Double = 1.5707963267949
Public Function ATan2(ByVal Dy As Double, ByVal Dx As Double) As Double
If Dx = 0 Then
ATan2 = PI½ * Sgn(Dy)
Else
ATan2 = Atn(Dy / Dx)
If Dx < 0 Then ATan2 = ATan2 + PI
End If
End Function
Works fine as Single too in most cases
Re: Motion at an Angle 2D (For the Games FAQ)
I think Pi can be calculated near enough with Abs(4) or something like that in VB6.
Small thing bugging me is the use of dx and dy. The d means small change in but the change could be up to infinity. Just me being picky though (I see it too often in calculus to see it anywhere else :cry:)
Re: Motion at an Angle 2D (For the Games FAQ)
Aye, but of course ordinary keyboards don't come with triangles for big changes :P
Re: Motion at an Angle 2D (For the Games FAQ)
Too true but "small change in" is used for differentiation and by small change it means small - too small for trig in movement.