Find the shortest rotation angle between two angles

0 Flares Twitter 0 Facebook 0 Google+ 0 Email -- Filament.io 0 Flares ×

That’s it, the following code finds for you the shortest rotation angle from a given angle to another:

[sourcecode language='as3']
var diffAngle:Number = Math.atan2(Math.sin(angleTo – currentAngle), Math.cos(angleTo – currentAngle));
[/sourcecode]

The code was given on this forum. You’ll also find a way to do it with vectors.

0 Flares Twitter 0 Facebook 0 Google+ 0 Email -- Filament.io 0 Flares ×

2 Comments

  1. Tyranus

    Hi there,

    Thanks for that snippet. It needs a Math.Abs():)

    var diffAngle:Number = Math.abs(Math.atan2(Math.sin(angleTo – currentAngle), Math.cos(angleTo – currentAngle)));

    Greetings!

  2. Matthew

    I found one that doesn’t use trig functions; It might be more efficient. :

    var diffAngle:Number = Math.min(Math.abs(Math.abs(angleTo – currentAngle) – 2*Math.PI), Math.abs(angleTo – currentAngle))

    This one is for javaScript but the math behind it should apply everywhere.

Leave a Comment