   Simple rotation problem
  How does one adjust a target heading (-pi/pi) to rotate from a current heading so as to rotate the shortest way.I'm using a simple lerp(current, target, 0.1*time) to ease-animate my compass needle rotation toward a new heading and this is not the problem.The problem is obviously when I need to do a rotation of say South West (> half pi) to North West (< -half pi), over the pi/-pi boundary since the lerp will simply snap anti-clockwise right around, not clockwise since its just lerping a number) which I already expected.I would have thought this logic would work but it still ends up "flicking" sometimes (its almost random to be honest)/* Set new target heading in radians */void setTargetHeading (float heading){/* clamp current heading */float current = fmod(currentAngle, 2 * M_PI);targetAngle = heading;float theta = heading - current;/* if theta is more than pi radians (SW to NW),   rotate clockwise by increasing target angle by two pi */if (theta > M_PI)targetAngle += 2 * M_PI;/* if theta is less than -pi radians (NW to SW),   rotate anti-clockwise by decreasing target by two pi */else if (theta < -M_PI)targetAngle -= 2 * M_PI;}Animating the compass needle:/* update rotation then apply new angle to needle image */currentAngle = lerp(currentAngle, targetAngle, 0.1 * timeDelta);/* set the image rotation */image.setRotation(currentAngle);Its such a simple thing but nothing I try seems to work. Thanks.   
  I've hit this problem from time to time too and i have a solution but i know there has to be a cleaner way (its anoying cause i havent been able to think it up yet and i feel dumb)Anyhow, here's my solution pseudocode (done in degrees for readability!)AngleA = source angleAngleB = destination angleDirection = +1 or -1 depending on whether we should increase our angle or decrease over timeif(AngleB>AngleA){  if(AngleB-AngleA<180)    Direction=1;  else    Direction=-1;}else{  if(AngleA-AngleB<180)    Direction=-1;  else    Direction=1;}then your delta becomes...Delta=Time*Direction*MovementSpeed;Hope that helps!   ;  Thanks but the problem is that I'm interpolating my drawn angle using the current angle (updated each frame) and the desired angle.I know the desired angle must be adjusted by either 2pi or -2pi but any logic I try still causes the needle to "flip" the wrong way right around sometimes.;Read my post in this thread. Links to another thread where I made a post.;  Figured it out! Hasn't broken yet (lots of clicks later).Here's pseudocode:current = current % 2pitheta = target - currentif theta < -pi then target = target + 2piif theta > pi then target = target - 2piHere's the C# versionusing System;class Compass{// Current drawn anglepublic double CurrentAngle;// Angle to animate topublic double TargetAngle;// Constructorpublic Compass(){CurrentAngle = 0;TargetAngle = 0;}// Helper linear-interp functionpublic double Lerp(double a, double b, double d){return a + ((b - a) * d);}// Adjusts the TargetAngle to allow for smallest-angle rotationpublic void AdjustTarget(){// Clamp current heading to -PI/PI rangeCurrentAngle = Math.IEEERemainder(CurrentAngle, 2.0 * Math.PI);double theta = TargetAngle - CurrentAngle;// Adjust if rotating clockwise over boundaryif (theta < -Math.PI)TargetAngle += 2.0 * Math.PI;// Adjust if rotating counter-clockwise over boundaryif (theta > Math.PI)TargetAngle -= 2.0 * Math.PI;}// Update rotationpublic void Update(double time){CurrentAngle = Lerp(CurrentAngle, TargetAngle, time);}// Gets the angle in degreespublic double DrawAngle(){return CurrentAngle * (180.0 / Math.PI);}};  Euler Angle representation is not suitable for interpolation.  It is just not.  Please use quaternion representation or axis-angle representation instead which have obvious way to be interpolated.   ;Quote:Original post by ma_htyEuler Angle representation is not suitable for interpolation.  It is just not.  Please use quaternion representation or axis-angle representation instead which have obvious way to be interpolated.This is in 2d, where all these different representations reduce to pretty much the same thing...   ;Quote:Original post by EmergentThis is in 2d, where all these different representations reduce to pretty much the same thing...Opps... you are right.  I'm being careless.  When I saw "heading" in the first post, I just assume it is about Euler Angle.  ^^"