We are building an AS3 Helper class to speed up development on Actionscript 3 projects. Feel free to use these methods in your own Helper class, or to share your own methods that you find useful.

**FindAngle()**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public class Helper { /** * This method accepts two * Point objects and returns * the angle between them * in radians. */ public static function FindAngle(point1:Point, point2:Point):Number { var dx:Number = point2.x - point1.x; var dy:Number = point2.y - point1.y; return -Math.atan2(dx,dy); } } |

**Usage**

1 2 3 4 | // Find the angle between the mouse cursor and someClip: var mousePoint:Point = new Point(stage.mouseX, stage.mouseY); var somePoint:Point = new Point(someClip.x, someClip.y); var angle:Number = Helper.FindAngle(mousePoint, somePoint); |

**Comments**

This is a great method that will help you program many different games in Flash. Applications are wide-reaching. Picture a billiards game where you have to find the angle between two balls. Or a space shooter where you need to find the angle between a laser turret and some invading UFOs.

The return value is in radians, which you may not be familiar with if (like me) you stopped taking math courses in the eleventh grade. If you want to convert the result to degrees, refer to our Helper.RadiansToDegrees method.

**IMPORTANT:** The order in which you pass the points makes a difference. You’ll get a completely different angle by reversing the order of the parameters.

For more AS3 Helper Classses and a pile of other useful stuff, check out our Flash and Actionscript 911 feature.

Pingback: untoldentertainment.com » AS3 Helper Class - RadiansToDegrees and DegreesToRadians

HanoxWorked for me when I changed it to this:

public static function findAngle(point1:Point, point2:Point):Number

{

var dx:Number = point2.x – point1.x;

var dy:Number = point2.y – point1.y;

return Math.atan2(dy,dx);

}

In livedocs I found:

Note that the first parameter to atan2 is always the y coordinate.

Anyway thx for the Helper classes!

Hanox

RyanThanks, Hanox! More to come …

Pingback: Angles in Flash: Rotation, Degrees and Radians — Michael James Williams