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:
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:
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.
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:
This will cause your object to move with Speed d in direction A.Code:dX = Sin(A) * d dY = -Cos(A) * d XPos = XPos + dX YPos = YPos + dY




.
)
Reply With Quote