I’m not very good at math( and i never was ) and I’ve a problem: I’m trying to make sprite always rotated to mouse cursor. That’s what i have:
// mx is mouse X and my is mouse y
double rotate = (atan2(my, mx) * PI);
// Set rotation takes rotation in degrees
ship.SetRotation( rad2deg(rotate) );
Where deg2rad function is:
double rad2deg(double rad)
{
double deg = 0;
deg = rad * (180/M_PI);
return deg;
}
Unfortunately it is not working. The ship is rotating very weird ( really hard to define that ). And I don’t have any idea to solve this problem.
I’m working on SFML and SetRotation takes degrees.
Thanks in advance.
You need to define the rotation relative to some point. Currently you’re doing it relative to the corner of the screen, which will only give you 90 degrees and is probably not what you want. You probably want rotation relative to the sprite’s location or perhaps relative to the centre of the screen, e.g.
[As others have noted, e.g. @duffymo, you may also have the arguments to atan2 transposed, so I’ve made this change also.]