AS3 Helper Class – FindAngle

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()

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

// 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.

4 thoughts on “AS3 Helper Class – FindAngle

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

  2. Hanox

    Worked 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

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

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>