Limiting atan2 to specific angle ranges

百般思念 提交于 2021-01-28 07:01:20

问题


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. enter image description here




回答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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!