atan2 and X Y issues

Monkey Forums/Monkey Beginners/atan2 and X Y issues

Ashmoor(Posted 2016) [#1]
I have an image I rotate based on it's xPos,yPos and the MouseX(),MouseY() so if I use angle = atan2(xPos-MouseX(),yPos-MouseY())

and draw the image using
DrawImage(img,x,y,angle,scalex,scaley)

it rotates nicely to point at the mouse.

What puzzles me is the fact that the angle value is negative for clockwise rotation, should it not be 0 at 12 o'clock and 90 at 3 o'clock? Currently it is 0 at 12 and -90 at 3 o'clock looks like a 90 degree shifted trigonometric circle.

This is getting to me because if I want to rotate a point along with the image, it spins in the opposite direction therefore I must use offset*Sin/Cos(-angle) formula to get the x/y coordinates.

Can you please explain why it happens? Shouldn't atan return the angle with the x axis in the counter clockwise direction? Is it because the y axis is flipped from the trigonometric one?


Jesse(Posted 2016) [#2]
the formulas is:
ATan2(y,x)
if you want to get return value between 0 and 360 do this:
(ATan2(y,x)+360.0) Mod 360.0

it will still go counter clock wise. To do it clockwise, you can change the sign. You can minus the returning angle sign. it will work otherwise as well. this is a common problem with a lot of beginners, was mine too.


Ashmoor(Posted 2016) [#3]
Why does monkey help say Atan2(x,y)? I know that in other languages is Atan2(y,x), I doubt it's a typo. I got my stuff working, I'm just puzzled because I don't really understand how it works.I tried looking at the definition of some functions and the math is beyond my current level. I can't understand the definition of ATan2 which looks like this:: Function ATan2#( x#,y# )="$atan2" what does it mean? Is it in another external file and is asked to read at "atan2" point?


Gerry Quinn(Posted 2016) [#4]
(1) Yes, it's a perennial issue with computer graphics as hardware tends to have +Y going down the screen, and nearly all APIs/libraries copy this. So you are upside down compared to standard trigonometry, meaning positive angles are clockwise and so forth.

(2) Monkey says Atan2(X/Y) just to be different, I guess. But it is just the names of the parameters that are reversed, so in fact you use it just the same as the standard Atan2(Y/X)! Just pretend it's a typo and says Y/X as normal, because that is easier to understand in terms of trig, even though the Monkey version as defined by Mark is mathematically correct.

ATan2( 50, 100 ) returns 26.565. That is the angle in degrees (Monkey uses degrees instead of radians) of the point Y=50 and X=100. based on the origin.


Ashmoor(Posted 2016) [#5]
Got it, thanks.