0

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

JPtheK9
  • 2,031
  • 2
  • 14
  • 32

2 Answers2

1

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
Thelo
  • 61
  • 2
  • I need a solution that works for all angles. Will this work for i.e. -170, 170? – JPtheK9 Jan 11 '15 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. – Thelo Jan 11 '15 at 23:11
0

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

Nevertheless can you add your rotation code to question?