# Rotate can I rotate an object to a target angle using Euler Angles?

How do I rotate an object from its current angle to the desired angle in increments on 1 axis? The problem is the wraparound at 360 degrees What I have so far (Note: pseudocode):

double MaxSpeed = 5;

double CurrentAngle = -170;
double DesiredAngle = 40;

double Distance = DesiredAngle - CurrentAngle; //How do I calculate this value considering angle wraparound?

CurrentAngle += Mathd.clamp(Distance, -MaxSpeed, MaxSpeed); //Caps the rotation speed

• can you explain more, what movement you want to do? Jan 11, 2015 at 14:48
• A simple incremental rotation: Rotation += Increment; where Increment.Y is capped at MaxSpeed. Jan 11, 2015 at 20:51

If your problem is simply to deal with the fact that -10, 350, and 710 are all equivalent angles, that you may potentially get any desired angle, and that you never want to make a total rotation over 180 degrees, then:

double Distance = DesiredAngle - CurrentAngle;

// Let's bring Distance to its closest equivalent in the [-180..180] degrees range
while (Distance < -180) Distance += 360;
while (Distance > 180) Distance -= 360;

CurrentAngle += Mathd.clamp(Distance, -MaxSpeed, MaxSpeed); //Caps the rotation speed

• I need a solution that works for all angles. Will this work for i.e. -170, 170? Jan 11, 2015 at 20:07
• Yes, that's the point. Assuming you mean that CurrentAngle = -170 and DesiredAngle is 170, Distance will start as 170 - (-170) = 340, then will be correctly brought to -20 by the second of the two added lines. Then you will correctly go towards the negative-degrees direction. Jan 11, 2015 at 23:11

If you want rotation with lineal speed from one vector to another in shortest path I can suggest about using quanterions with SLERP http://en.wikipedia.org/wiki/Slerp

If you want just rotate around x,y,z axis thats could be done with rotation matrix http://en.wikipedia.org/wiki/Rotation_matrix