For some game i'm creating, im trying to work out the angle the object is moving towards. Using the current X and Y and the old X an Y values, how could i get this?
thanks
Printable View
For some game i'm creating, im trying to work out the angle the object is moving towards. Using the current X and Y and the old X an Y values, how could i get this?
thanks
angle = arctan (deltaY/deltaX)
where the deltas are the change in x & y.
(At least, if I understand the question correctly);)
.. but don't forget to cover for situation when deltax=0 !
I know, I'm smartassing....
but I had to work around such a thing just yesterday, the error was clearly found, but nothing could be done to the executable. So I had to make sure the situation didn't occur from the user side. Two hours of work, just because those authors were so st....!
Here is the function I use, I wrote in QB and it can probably be done better.
VB Code:
FUNCTION Target (SourceX, SourceY, DestX, DestY) DIM Vertical AS SINGLE DIM Horizontal AS SINGLE DIM Diagonal AS SINGLE DIM Vector AS SINGLE Horizontal = ABS(SourceX - DestX) Vertical = ABS(SourceY - DestY) IF Horizontal AND Vertical <> 0 THEN Diagonal = SQR((Horizontal ^ 2) + (Vertical ^ 2)) Vector = (Arcsin(Vertical / Diagonal)) IF DestX > SourceX AND DestY > SourceY THEN Vector = 0 - Vector: Vector = Vector + 90 IF DestX > SourceX AND DestY < SourceY THEN Vector = Vector + 90 IF DestX < SourceX AND DestY > SourceY THEN Vector = Vector + 270 IF DestX < SourceX AND DestY < SourceY THEN Vector = 180 - Vector: Vector = Vector + 90 ELSE IF Horizontal = 0 AND DestY > SourceY THEN Vector = 0 IF Horizontal = 0 AND DestY < SourceY THEN Vector = 180 IF Vertical = 0 AND DestX > SourceX THEN Vector = 90 IF Vertical = 0 AND DestX < SourceX THEN Vector = 270 IF Vertical = 0 AND Horizontal = 0 THEN Vector = 0: Target = 0: EXIT FUNCTION END IF DO UNTIL Vector <= 360 AND Vector > 0 IF Vector <= 0 THEN Vector = Vector + 360 IF Vector > 360 THEN Vector = Vector - 360 LOOP Target = Vector END FUNCTION
Izt won't work since any Trig formula in VB will use/give radians NOT degrees!
Also keep in mind not everybody uses angels from 0 to 359, with 0 is North and 90 is East.
some are using a 0 that goes to the East and count than towards North !!
Believe me, I have to work with some of them, WEIRD!!!
I'm using this:
VB Code:
Public Sub Bearing(X1, Y1, X2, Y2, Bearing) 'Input X1, Y1, X2, Y2 'Output Bearing (0-360) 'Calculates Bearing from X1,Y1 to X2,Y2 using 360 degree-system Const PI = 3.141592654 Dim dx As Single Dim dy As Single dx = X2 - X1 dy = Y2 - Y1 If dy <> 0 Then bearing = (Atn(dx / dy) * 180 / PI) If X2 > X1 Then If Y2 > Y1 Then bearing = bearing Else bearing = 180 +bearing End If Else If Y2 > Y1 Then bearing = 360 + bearing Else bearing = 180 + bearing End If End If Else If X1 > X2 Then bearing = 270 Else bearing = 90 End If End If End Sub
Actually I put Rad() and Deg() functions in all my programs, using them everytime I call a sin/cos.. although I suppose it would make more sense to only call once ;)
You are using
So Vector is in radiants, and the following line uses Vector as it would be in Degrees!!!!VB Code:
Vector = (Arcsin(Vertical / Diagonal)) IF DestX > SourceX AND DestY > SourceY THEN Vector = 0 - Vector: Vector = Vector + 90
My arcsin function (since Basics don't include them :mad: ) has the deg() built into it.
Good idea, and yes I did miss the point that you were using a not built-in Function (ARCSIN).
Shouldn't you have been telling that?
Note:
After doing the testing on a simulation software for three years now, I've probably seen all kinds of false angle display's because of a mixed use of degrees and radians.
I hate those.........