PointFinder Class AS3.0

Here’s a class that I wrote. It’s just a bunch of static functions to help find common values like the angle of an object, the point to tween to after that angle has been found etc etc…


package  {

 import flash.geom.Point;

 /**
 * @author Dan Sanderson
 * @email dan@pldm.co.uk
 * @url dev.pldm.co.uk
 */

 public class PointFinder {

 public static function findPoint(angle : Number, distance : Number) : Point {

 var radian : Number = angle * (Math.PI/180);
 var point : Point = new Point();

 point.x = distance * Math.cos(radian);
 point.y = distance * Math.sin(radian);

 return point;
 }

 public static function findAngle(startPosition : Point, targetPosition : Point) : Number {

 var newAngle : Number;

 if(targetPosition) {
 var newX : Number = startPosition.x - targetPosition.x;
 var newY : Number = startPosition.y - targetPosition.y;
 newAngle = -Math.atan2(newX, newY)/(Math.PI/180);
 } else {
 newAngle = 0;
 }

 return newAngle;
 }

 public static function findDistance(startPosition : Point, targetPosition : Point) : Number
 {
 var distance : Number = Math.round(Math.sqrt(((startPosition.x - targetPosition.x) * (startPosition.x - targetPosition.x)) + (startPosition.y - targetPosition.y) * (startPosition.y - targetPosition.y)));
 return distance;
 }

 // This function is for converting wierd rotation values (like -58) to degrees.

 public static function convertRotationValueToDegress(rotation : Number) : Number
 {
 var degrees : Number;

 if(rotation < 0) {
 degrees = 180 + (180 - Math.abs(rotation));
 } else {
 degrees = rotation;
 }

 return degrees;
 }
 }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s