1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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
if(diff<0)diff+=2*Math.Pi;
//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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.