AS3 Helper Class – RadiansToDegrees and DegreesToRadians

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
16
public class Helper
{
	/**
 	* These companion methods will convert radians to degrees
 	* and degrees to radians.
 	*/		
	public static function RadiansToDegrees(radians:Number):Number
	{
		return radians * 180 / Math.PI;
	}
 
	public static function DegreesToRadians(degrees:Number):Number
	{
		return degrees * Math.PI / 180;
	}		
}

Usage

1
2
3
4
5
6
7
8
9
// Find the angle between the mouse cursor and someClip in degrees.
// Refer to our Helper.FindAngle method for that method:
var mousePoint:Point = new Point(stage.mouseX, stage.mouseY);
var somePoint:Point = new Point(someClip.x, someClip.y);
 
var angleInRadians:Number = Helper.FindAngle(mousePoint, somePoint);
 
// What the Hell is a radian?  Let's convert that nonsense:
var angleInDegrees:Number = Helper.RadiansToDegrees(angleInRadians);

Comments

So here are some more trigonometry-related methods. If you use the Helper.FindAngle method, you’ll get the result in radians. i for one don’t really know what a radian is, but i do know it’s one of the beasties that scared me away from taking trig in high school.

Let’s say you have a top-down tank in your game. You used Helper.FindAngle to find the angle between the tank turret and the target. Then you want to rotate the tank turret towards the target. But turret.rotation needs an angle in degrees, not radians. Use the RadiansToDegrees method to make everything right with the world.

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

One thought on “AS3 Helper Class – RadiansToDegrees and DegreesToRadians

  1. Pingback: untoldentertainment.com » AS3 Helper Class - FindAngle

Leave a Reply

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