Skip to content

Commit

Permalink
Move some FlxU methods to FlxPoint
Browse files Browse the repository at this point in the history
FlxU.rotatePoint() -> FlxPoint.rotate()
FlxU.getAngle()    -> FlxPoint.angleBetwee()
FlxU.getDistance() -> FlxPoint.distance()
  • Loading branch information
IQAndreas committed May 12, 2014
1 parent d0fe830 commit aa649e7
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 118 deletions.
4 changes: 2 additions & 2 deletions src/flixel/FlxObject.as
Original file line number Diff line number Diff line change
Expand Up @@ -660,8 +660,8 @@ package flixel
}
else
{
pathAngle = FlxU.getAngle(_point,node);
FlxU.rotatePoint(0,pathSpeed,0,0,pathAngle,velocity);
pathAngle = FlxPoint.angleBetween(_point,node);
FlxPoint.rotate(0,pathSpeed,0,0,pathAngle,velocity);
}

//then set object rotation if necessary
Expand Down
2 changes: 1 addition & 1 deletion src/flixel/system/FlxSound.as
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ package flixel.system
//Distance-based volume control
if(_target != null)
{
radialMultiplier = FlxU.getDistance(new FlxPoint(_target.x,_target.y),new FlxPoint(x,y))/_radius;
radialMultiplier = FlxPoint.distance(new FlxPoint(_target.x,_target.y),new FlxPoint(x,y))/_radius;
if(radialMultiplier < 0) radialMultiplier = 0;
if(radialMultiplier > 1) radialMultiplier = 1;

Expand Down
119 changes: 119 additions & 0 deletions src/flixel/util/FlxPoint.as
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,125 @@ package flixel.util
*/
public class FlxPoint
{

/* --- Static helper methods --- */

/**
* Calculates the angle between two points. 0 degrees points straight up.
*
* @param point1 The X coordinate of the point.
* @param point2 The Y coordinate of the point.
*
* @return The angle in degrees, between -180 and 180.
*/
static public function angleBetween(point1:FlxPoint, point2:FlxPoint):Number
{
var x:Number = point2.x - point1.x;
var y:Number = point2.y - point1.y;
if((x == 0) && (y == 0))
return 0;
var c1:Number = 3.14159265 * 0.25;
var c2:Number = 3 * c1;
var ay:Number = (y < 0)?-y:y;
var angle:Number = 0;
if (x >= 0)
angle = c1 - c1 * ((x - ay) / (x + ay));
else
angle = c2 - c1 * ((x + ay) / (ay - x));
angle = ((y < 0)?-angle:angle)*57.2957796;
if(angle > 90)
angle = angle - 270;
else
angle += 90;
return angle;
}

/**
* Calculate the distance between two points.
*
* @param point1 A <code>FlxPoint</code> object referring to the first location.
* @param point2 A <code>FlxPoint</code> object referring to the second location.
*
* @return The distance between the two points as a floating point <code>Number</code> object.
*/
static public function distance(point1:FlxPoint, point2:FlxPoint):Number
{
var dx:Number = point1.x - point2.x;
var dy:Number = point1.y - point2.y;
return Math.sqrt(dx * dx + dy * dy);
}

/**
* Rotates a point in 2D space around another point by the given angle.
*
* @param X The X coordinate of the point you want to rotate.
* @param Y The Y coordinate of the point you want to rotate.
* @param pivotX The X coordinate of the point you want to rotate around.
* @param pivotY The Y coordinate of the point you want to rotate around.
* @param angle Rotate the point by this many degrees.
* @param point Optional <code>FlxPoint</code> to store the results in.
*
* @return A <code>FlxPoint</code> containing the coordinates of the rotated point.
*/
static public function rotate(X:Number, Y:Number, pivotX:Number, pivotY:Number, angle:Number, point:FlxPoint=null):FlxPoint
{
var sin:Number = 0;
var cos:Number = 0;
var radians:Number = angle * -0.017453293;
while (radians < -3.14159265)
radians += 6.28318531;
while (radians > 3.14159265)
radians = radians - 6.28318531;

if (radians < 0)
{
sin = 1.27323954 * radians + .405284735 * radians * radians;
if (sin < 0)
sin = .225 * (sin *-sin - sin) + sin;
else
sin = .225 * (sin * sin - sin) + sin;
}
else
{
sin = 1.27323954 * radians - 0.405284735 * radians * radians;
if (sin < 0)
sin = .225 * (sin *-sin - sin) + sin;
else
sin = .225 * (sin * sin - sin) + sin;
}

radians += 1.57079632;
if (radians > 3.14159265)
radians = radians - 6.28318531;
if (radians < 0)
{
cos = 1.27323954 * radians + 0.405284735 * radians * radians;
if (cos < 0)
cos = .225 * (cos *-cos - cos) + cos;
else
cos = .225 * (cos * cos - cos) + cos;
}
else
{
cos = 1.27323954 * radians - 0.405284735 * radians * radians;
if (cos < 0)
cos = .225 * (cos *-cos - cos) + cos;
else
cos = .225 * (cos * cos - cos) + cos;
}

var dx:Number = X-pivotX;
var dy:Number = pivotY+Y; //Y axis is inverted in flash, normally this would be a subtract operation
if(point == null)
point = new FlxPoint();
point.x = pivotX + cos*dx - sin*dy;
point.y = pivotY - sin*dx - cos*dy;
return point;
}


/* --- Actual instance --- */

/**
* @default 0
*/
Expand Down
168 changes: 53 additions & 115 deletions src/flixel/util/FlxU.as
Original file line number Diff line number Diff line change
Expand Up @@ -440,121 +440,6 @@ package flixel.util
return Velocity;
}

//*** NOTE: THESE LAST THREE FUNCTIONS REQUIRE FLXPOINT ***//

/**
* Rotates a point in 2D space around another point by the given angle.
*
* @param X The X coordinate of the point you want to rotate.
* @param Y The Y coordinate of the point you want to rotate.
* @param PivotX The X coordinate of the point you want to rotate around.
* @param PivotY The Y coordinate of the point you want to rotate around.
* @param Angle Rotate the point by this many degrees.
* @param Point Optional <code>FlxPoint</code> to store the results in.
*
* @return A <code>FlxPoint</code> containing the coordinates of the rotated point.
*/
static public function rotatePoint(X:Number, Y:Number, PivotX:Number, PivotY:Number, Angle:Number,Point:FlxPoint=null):FlxPoint
{
var sin:Number = 0;
var cos:Number = 0;
var radians:Number = Angle * -0.017453293;
while (radians < -3.14159265)
radians += 6.28318531;
while (radians > 3.14159265)
radians = radians - 6.28318531;

if (radians < 0)
{
sin = 1.27323954 * radians + .405284735 * radians * radians;
if (sin < 0)
sin = .225 * (sin *-sin - sin) + sin;
else
sin = .225 * (sin * sin - sin) + sin;
}
else
{
sin = 1.27323954 * radians - 0.405284735 * radians * radians;
if (sin < 0)
sin = .225 * (sin *-sin - sin) + sin;
else
sin = .225 * (sin * sin - sin) + sin;
}

radians += 1.57079632;
if (radians > 3.14159265)
radians = radians - 6.28318531;
if (radians < 0)
{
cos = 1.27323954 * radians + 0.405284735 * radians * radians;
if (cos < 0)
cos = .225 * (cos *-cos - cos) + cos;
else
cos = .225 * (cos * cos - cos) + cos;
}
else
{
cos = 1.27323954 * radians - 0.405284735 * radians * radians;
if (cos < 0)
cos = .225 * (cos *-cos - cos) + cos;
else
cos = .225 * (cos * cos - cos) + cos;
}

var dx:Number = X-PivotX;
var dy:Number = PivotY+Y; //Y axis is inverted in flash, normally this would be a subtract operation
if(Point == null)
Point = new FlxPoint();
Point.x = PivotX + cos*dx - sin*dy;
Point.y = PivotY - sin*dx - cos*dy;
return Point;
};

/**
* Calculates the angle between two points. 0 degrees points straight up.
*
* @param Point1 The X coordinate of the point.
* @param Point2 The Y coordinate of the point.
*
* @return The angle in degrees, between -180 and 180.
*/
static public function getAngle(Point1:FlxPoint, Point2:FlxPoint):Number
{
var x:Number = Point2.x - Point1.x;
var y:Number = Point2.y - Point1.y;
if((x == 0) && (y == 0))
return 0;
var c1:Number = 3.14159265 * 0.25;
var c2:Number = 3 * c1;
var ay:Number = (y < 0)?-y:y;
var angle:Number = 0;
if (x >= 0)
angle = c1 - c1 * ((x - ay) / (x + ay));
else
angle = c2 - c1 * ((x + ay) / (ay - x));
angle = ((y < 0)?-angle:angle)*57.2957796;
if(angle > 90)
angle = angle - 270;
else
angle += 90;
return angle;
};

/**
* Calculate the distance between two points.
*
* @param Point1 A <code>FlxPoint</code> object referring to the first location.
* @param Point2 A <code>FlxPoint</code> object referring to the second location.
*
* @return The distance between the two points as a floating point <code>Number</code> object.
*/
static public function getDistance(Point1:FlxPoint,Point2:FlxPoint):Number
{
var dx:Number = Point1.x - Point2.x;
var dy:Number = Point1.y - Point2.y;
return Math.sqrt(dx * dx + dy * dy);
}


/* --- Deprecated members in Flixel v2.57 --- */
/* To be removed after developers have had time to adjust to the new changes. */
Expand Down Expand Up @@ -632,5 +517,58 @@ package flixel.util
return null;
}


/**
* Rotates a point in 2D space around another point by the given angle.
*
* @param X The X coordinate of the point you want to rotate.
* @param Y The Y coordinate of the point you want to rotate.
* @param PivotX The X coordinate of the point you want to rotate around.
* @param PivotY The Y coordinate of the point you want to rotate around.
* @param Angle Rotate the point by this many degrees.
* @param Point Optional <code>FlxPoint</code> to store the results in.
*
* @return A <code>FlxPoint</code> containing the coordinates of the rotated point.
*
* @deprecated This property is deprecated. Use <code>FlxPoint.rotate()</code> instead.
*/
static public function rotatePoint(X:Number, Y:Number, pivotX:Number, pivotY:Number, angle:Number, point:FlxPoint=null):FlxPoint
{
FlxG.warnDeprecated('FlxU.rotatePoint()', 'FlxPoint.rotate()');
return FlxPoint.rotate(X, Y, pivotX, pivotY, angle, point);
}

/**
* Calculates the angle between two points. 0 degrees points straight up.
*
* @param Point1 The X coordinate of the point.
* @param Point2 The Y coordinate of the point.
*
* @return The angle in degrees, between -180 and 180.
*
* @deprecated This property is deprecated. Use <code>FlxPoint.angleBetween()</code> instead.
*/
static public function getAngle(point1:FlxPoint, point2:FlxPoint):Number
{
FlxG.warnDeprecated('FlxU.getAngle()', 'FlxPoint.angleBetween()');
return FlxPoint.angleBetween(point1, point2);
}

/**
* Calculate the distance between two points.
*
* @param Point1 A <code>FlxPoint</code> object referring to the first location.
* @param Point2 A <code>FlxPoint</code> object referring to the second location.
*
* @return The distance between the two points as a floating point <code>Number</code> object.
*
* @deprecated This property is deprecated. Use <code>FlxPoint.distance()</code> instead.
*/
static public function getDistance(point1:FlxPoint, point2:FlxPoint):Number
{
FlxG.warnDeprecated('FlxU.getDistance()', 'FlxPoint.distance()');
return FlxPoint.distance(point1, point2);
}

}
}

0 comments on commit aa649e7

Please sign in to comment.