# Libgdx RotateToAction does not directly rotate between 179 and -179

It took me a bit of debugging to realize why I'm having trouble with my rotations and it seems that rather than just moving the 2 degrees between 179 and -179, it prefers to rotate all the way in the other direction, the full 358 degrees.

Is there any way to make it go the short way in this case?

Code I have so far:

public void rotateTo(float targetRotation)
{
RotateToAction rotateToAction = new RotateToAction();
rotateToAction.setRotation(targetRotation);
rotateToAction.setDuration(2f);
if(Math.abs(this.getRotation()-targetRotation) > 180)
{
/*failed attempt to force rotation*/
SequenceAction sequenceAction = new SequenceAction();
RotateToAction rotateToAction2 = new RotateToAction();
rotateToAction2.setRotation(180);
rotateToAction2.setDuration(1f);
}
else
{
}
}


I'm not sure exactly how the code works and this obviously won't work if any methods require an angle between -180 and 180 degrees, but rather than setting the target rotation to -179, could you instead set it to 181 degrees? To my (very limited) understanding that is the same angle as -179 degrees.

• To be pedantic, -180 and 180 are the same position. A circle ranges from -180 to 179, or 0 to 359. The value 0 is counted also. – Fuzzy Logic Sep 25 '15 at 4:06
• @FuzzyLogic Thanks for clarifying. My thinking was that the rotate action must increment through rotation values in a linear manner meaning that the difference between -180 and 180 would be 360, like it is on a number line, rather than 0, like it is on a radian circle, which is what is causing the object to rotate all the way around. – Leo Baldock Sep 25 '15 at 4:42
• That does accomplish the task. However, with future rotations it's easy to end up with a "winding up" scenario where you then try to set it to 10, for example, and it spins multiple times coming back down to 10, after having wound up a bit. I'll be posting my solution shortly. – DoubleDouble Sep 25 '15 at 5:05

I have never used this library but a google search later I came up with following:

RotateToAction rotateToAction2 = new RotateToAction();
rotateToAction2.setReverse(true);
rotateToAction2.setRotation(180);
rotateToAction2.setDuration(1f);


I hope this works :)

• setting reverse "When true, the action's progress will go from 100% to 0%." - this causes it to jump to the end (rotated to -179), and then run backwards until it gets to the start (179), but still the long way. – DoubleDouble Sep 25 '15 at 2:00

I ended up creating my own action using the RotateToAction source code as a template.

Essentially just added any of the code involving the number 360 ;)

public class DirectRotateAction extends TemporalAction
{
private float start, end;

protected void begin()
{
start = (this.getActor().getRotation()+360)%360;
if (start - end > 180) end += 360;
if (end - start > 180) start += 360;
}

protected void update(float percent)
{
this.getActor().setRotation((start+(end-start)*percent)%360);
}

public float getRotation() {
return end;
}

public void setRotation(float rotation) {
this.end = (rotation+360)%360;
}
}