Posted By

MartinMller on 07/22/12


Tagged

math rotation angle


Versions (?)

AS3 Shortest angle to angle (CW vs. CCW)


 / Published in: ActionScript 3
 

URL: http://board.flashkit.com/board/showthread.php?t=766397

Calculate the shortest angle between two angles. Useful for deciding whether an object should be rotated ClockWise or Counter ClockWise. Given arguments should be angels in degrees. Follow the link if you wish to use Radians instead.

  1. //from and to should be degrees. Returning int as int was what I needed for my project. Could easily be changed to Number if more precision is needed
  2. public static function getShortestRotationAngle(from : int, to : int) : int {
  3. var currRotation : Number = from;
  4. var newCourse : Number = to;
  5. var shortestAngle : int;
  6. shortestAngle = (180/Math.PI) * Math.atan2((Math.cos(currRotation * Math.PI/180) * Math.sin(newCourse * Math.PI/180) - Math.sin(currRotation * Math.PI/180) * Math.cos(newCourse * Math.PI/180)), (Math.sin(currRotation * Math.PI/180) * Math.sin(newCourse * Math.PI/180) + Math.cos(currRotation * Math.PI/180) * Math.cos(newCourse * Math.PI/180)));
  7. return shortestAngle;
  8. }

Report this snippet  

You need to login to post a comment.