0
\$\begingroup\$

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

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

2 Answers 2

1
\$\begingroup\$

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
\$\endgroup\$
2
  • \$\begingroup\$ I need a solution that works for all angles. Will this work for i.e. -170, 170? \$\endgroup\$
    – JPtheK9
    Commented Jan 11, 2015 at 20:07
  • \$\begingroup\$ 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. \$\endgroup\$
    – Thelo
    Commented Jan 11, 2015 at 23:11
0
\$\begingroup\$

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?

\$\endgroup\$
2
  • \$\begingroup\$ Thanks, I'll look into that! Um, what do horseshoes mean in slerp? \$\endgroup\$
    – JPtheK9
    Commented Jan 11, 2015 at 20:08
  • \$\begingroup\$ My rotation code is the clamped distance plus the current angle (the clamping caps the increment by the rotation speed). \$\endgroup\$
    – JPtheK9
    Commented Jan 11, 2015 at 20:21

You must log in to answer this question.

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