JoyY() and JoyX() to Angle?

Monkey Forums/Monkey Programming/JoyY() and JoyX() to Angle?

Neuro(Posted 2012) [#1]
Having some trouble with this bit of code here. Trying to properly convert the Joystick values into a 360 degrees angle value. Can't quite seem to get it right :
if JoyX(1)=0 And JoyY(1)=0  'none
	rstickangle = 0;
elseif JoyX(1)=0 And JoyY(1)>0  'up
	rstickangle = 0;
elseif JoyX(1)=0 And JoyY(1)<0  'down
	rstickangle = 180;
elseif JoyY(1)=0 And JoyX(1)>0  'right
	rstickangle = 90; 
elseif JoyY(1)=0 And JoyX(1)<0  'left
	rstickangle = 270; 
elseif JoyY(1)>0 And JoyX(1)>0 
	rstickangle = ATan(JoyY(1)/JoyX(1))  'something wacky here...
elseif JoyX(1)<0 And JoyX(1)<0 
	rstickangle = 180+ATan(JoyY(1)/JoyX(1))  'and here too.
else
	rstickangle = 360+ATan(JoyY(1)/JoyX(1))
end
			
sprite.SetAngle(rstickangle);


Any ideas would be much appreciated!


MikeHart(Posted 2012) [#2]
Try Atan2.

This is how I do it in fantomEngine:

ang = ATan2( ydiff, xdiff )+90.0



Raz(Posted 2012) [#3]
Yeah I use the atan2 function as well but I have it within a directionbetweenpoints(x1,y1,x2,y2) function and do...

Angle = directionbetweenpoints(0,0,joyx(),joyy())


Neuro(Posted 2012) [#4]
Great thanks guys, will try it out later today :)!


Neuro(Posted 2012) [#5]
Awesome, i was able to get exactly what i needed with this :
rstickangle = ATan2(JoyX(1),JoyY(1));


Thanks for setting me in the right direction (no pun intended)!