问题
I want my Player sprite to rotate following the position of the mouse cursor.
I'm using atan2 to set up a 360 degree rotation action script for my Player sprite.
atan2(XMouse-XPlayer,YMouse-YPlayer) - 90
Using this, 90 degrees is straight up, 0 is direct right, 270 degrees is straight down. I need the Player sprite to sit on the Left side of the screen and only face the Right side of the screen when it rotates (meaning it can't look backwards).
I need to limit my angles to both conditions; less than 90 degrees OR greater than 270 degrees.
Ideally, I'd like to set 0 degrees to straight up, then I only have to set my conditions to Angles less than 180 degrees (one conditional rather than two).
Can that be done?
回答1:
Yes. Use min()
and max()
(although you haven't said what language you're working in...)
回答2:
If you use radians, then the condition to check for is that abs(theta) < PI/2
is always true. Here's an image from Wikipedia, just to show you what I mean.
回答3:
In order to have 0 degrees straight up and clockwise movement from 0 to 360 degrees
use the formula:
f(x,y)=180-90*(1+sign(y))* (1-sign(x^2))-45*(2+sign(y))*sign(x)
-180/pi()*sign(x*y)*atan((abs(y)-abs(x))/(abs(y)+abs(x)))
来源:https://stackoverflow.com/questions/9956292/limiting-atan2-to-specific-angle-ranges