Anything wrong with ATan ?

Monkey Forums/Monkey Beginners/Anything wrong with ATan ?

Duke87(Posted 2015) [#1]
Hi Guys, I'm just coding kind of a Path-System for the Monkey-X Jam for the Vertical game.
I just hardcoded 6 Path-Point and stored them within Path[_PathNr][koordinate].
_PathNr is the number of the Path, and koorinate can be 0 for the X and 1 for the Y Value.

X and Y are the koordinates of the Ship that shall move.
Then i made something like this within the Update:
[Code]
Angle = ATan((Y-Path[currPath][1]) / ( X-Path[currPath][0]))

X += EqToFPS(Speed) * Cos(Angle)
Y += EqToFPS(Speed) * Sin(Angle)

If ll_Distance(X,Y,Path[currPath][0],Path[currPath][1]) <= 15
currPath +=1
Endif
[/Code]

My Ships always find Path 1 correctly but then when they should turn into Path 2, they turn exactly into the oppisite.

I also made a DrawPath() Function, and the Points are correctly coded.

Whats the mistake?

bye Duke


ImmutableOctet(SKNG)(Posted 2015) [#2]
For the life of me, I can't remember why this works, but try flipping around your X and Y parameters (Which is divided by what). Or you could flip your uses of 'Cos' and 'Sin', but that tends to be problematic. Also, 'ATan2' is a thing, it does exactly the same thing as 'ATan' for this particular case. It just makes things less cluttered. By the way, this thread might help, regarding way-points.


Duke87(Posted 2015) [#3]
Thank You. yeah i changed to ATan2. and i forgot to declare Angle as a float, so it was just an Int *my fault* i dont know why, but now it Works fine. (just because Angle was no Float? why should it then turn into the opposite direction, makes no sense to me) but anyway, it works fine for now :D.


JaviCervera(Posted 2015) [#4]
Haven't checked your code, but there's something to bear in mind when using ATan: It only gets correct results for quadrants 1 and 4, while ATan2 will return the correct value (with proper sign) in all 4 quadrants.


Gerry Quinn(Posted 2015) [#5]
Yers, use ATan2 for all general geometric purposes, ATan is just for pure trigonometry really.

Note that Mark has defined Atan2( x, y ) as ATan( x / y ). For computational purposes this is identical to the usual Atan2( y, x ) = ATan( y / x ), as only the parameter names change. But it contradicts the usual interpretation of x and y in the Cartesian plane coordinate system[*]. Just pretend you are using the standard form, and reverse x and y when using it.

[Of course, Cartesian coordinates still have the Y-axis pointing the opposite way to screen coordinates...]


Samah(Posted 2015) [#6]
@Gerry Quinn: Note that Mark has defined Atan2( x, y ) as ATan( x / y ).

I never even noticed that. I guess it's another non-standard quirk of Monkey.
The one that annoys me the most is Shr being arithmetic. There is no non-arithmetic shift right. Mark got it right in BlitzMax with Shr and Sar, so I don't know why he didn't bring it through into Monkey too. Same goes with ATan2 in BlitzMax.


Duke87(Posted 2015) [#7]
Ah, the fact that ATan only works for quadrant 1 & 4 would explain a lot. Thank you. Didnt know that yet. Although i fixed it, im glad to know the reason why it had such strange behaviour in some cases.


Gerry Quinn(Posted 2015) [#8]
It's not that ATan is wrong - it's correct in trig. Tan = Sin/Cos has a period of 180 degrees, even though Sin and Cos each have a period of 360 degrees. (ASin and ACos also cannot distinguish every angle of a circle, but in a different way.)

The value y/x doesn't distinguish between opposite quadrants. ATan2 takes the signs of y and x into account to distinguish four quadrants, and also deals with any troublesome infinities.

From the point of view of game physics and geometry, you can probably forget about the existence of anything other than Sin, Cos, and ATan2.