1

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?

jamylak
  • 128,818
  • 30
  • 231
  • 230

3 Answers3

0

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

Fuhrmanator
  • 11,459
  • 6
  • 62
  • 111
0

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)))
0

Yes. Use min() and max() (although you haven't said what language you're working in...)

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680