# Turning a sprite such that it rotates in the direction that's most efficient

I have a sprite that moves from waypoint to waypoint. It turns to face its velocity vector, but not instantly (there's a rotation speed).

The problem I'm having is, from one direction to another, the sprite will often turn in wild, unnecessary ways instead of appearing like you might expect a car to appear (turn such that the minimum delta in angle occurs).

Right now I'm using the following formula to calculate the rotation float for the current velocity:

MathHelper.PiOver2 + -Math.Atan2(anotherVector.Y - vector.Y, -(anotherVector.X - vector.X));


And then I lerp the current rotation by the new rotation by the rotation speed (10f):

rotation = MathHelper.Lerp(rotation, targetRotation, 10f * (float)gameTime.ElapsedGameTime.TotalSeconds);


But unfortunately, it's causing the sprite to spin in wild directions before reaching the correct angle. Any suggestions?

• Shouldn't the 3rd parameter for lerp, the time, be between 0 and 1? Commented Jul 16, 2011 at 18:41
• possible duplicate of Calculating angle between 2 vectors
– user744
Commented Jul 18, 2011 at 15:37

if (targetRotation > rotation+Pi)

• The function MathHelper.WrapAngle is useful for this. Commented Jul 16, 2011 at 9:58