My character has an angle rotation, when the player touches the screen it automatically face to the place that the player touched, using atan2(y,x) and Math.radiansToDegrees methods, but I want it to do that smoothly, is to say, save the required angle in a variable, and the current one in another, and depending of the 2 values, make it to rotate to left(if the required angle is to the right side of the current) or to the right. I've been almost an hour thinking about this, but I couldn't solve it.


1 Answer 1


Have a currentRotation and a desiredRotation you will also want some helper function to give you the shortest angle distance, so let's just call that angleDist or whatever. (You want that because sometimes going clockwise is shorter or vice versa).

Then all you need is to interpolate between them somehow.

currentRotation += expFactor * angleDist(currentRotation, desiredRotation);

You can do any interpolation or tweening or whatever that you want there.

As a sidenote, in almost all cases converting away from radians seems like a bad idea.

If your question was specifically about how to figure out which way to rotate.

//Assuming both angles are between 0 and 2Pi
double diff = desiredAngle-currentAngle;
//If diff is smaller than 0 we add 2Pi so that it ends up in the 0 to 2Pi range
//You can imagine this as making the current angle zero, but preserving the relative difference
//Now if the angle we want to get to is bigger than half a rotation we go the other way
return diff<=Math.Pi ? diff : diff-2*Math.Pi;

Hope this helps.

  • \$\begingroup\$ Thank you very much! After sleeping I found out the solution, but your method seems better, thanks! \$\endgroup\$ Feb 1, 2016 at 15:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .