Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate FlxPoint.angleBetween and make angles consistent #2482

Merged
merged 11 commits into from
Apr 25, 2022
15 changes: 14 additions & 1 deletion flixel/input/FlxSwipe.hx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ class FlxSwipe
public var endPosition(default, null):FlxPoint;

public var distance(get, never):Float;
@:deprecated("FlxSwipe.angle is deprecated, use degrees")
public var angle(get, never):Float;
public var degrees(get, never):Float;
public var radians(get, never):Float;
public var duration(get, never):Float;

var _startTimeInTicks:Int;
Expand Down Expand Up @@ -53,7 +56,17 @@ class FlxSwipe

inline function get_angle():Float
{
return startPosition.angleBetween(endPosition);
return startPosition.degreesTo(endPosition);
}

inline function get_degrees():Float
{
return startPosition.degreesTo(endPosition);
}

inline function get_radians():Float
{
return startPosition.radiansTo(endPosition);
}

inline function get_duration():Float
Expand Down
194 changes: 155 additions & 39 deletions flixel/math/FlxAngle.hx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import flixel.input.touch.FlxTouch;

/**
* A set of functions related to angle calculations.
* In degrees: (down = 90, right = 0, up = -90)
*/
class FlxAngle
{
Expand Down Expand Up @@ -53,6 +54,43 @@ class FlxAngle
*/
public static var TO_RAD(get, never):Float;

/**
* Calculates the angle from (0, 0) to (x, y), in radians
* @param x The x distance from the origin
* @param y The y distance from the origin
* @return The angle in radians between -PI to PI
*/
public static inline function radiansFromOrigin(x:Float, y:Float)
{
return angleFromOrigin(x, y, false);
}

/**
* Calculates the angle from (0, 0) to (x, y), in degrees
* @param x The x distance from the origin
* @param y The y distance from the origin
* @return The angle in degrees between -180 to 180
*/
public static inline function degreesFromOrigin(x:Float, y:Float)
{
return angleFromOrigin(x, y, true);
}

/**
* Calculates the angle from (0, 0) to (x, y)
* @param x The x distance from the origin
* @param y The y distance from the origin
* @param asDegrees If true, it gives the value in degrees
* @return The angle, either in degrees, between -180 and 180 or in radians, between -PI and PI
*/
public static inline function angleFromOrigin(x:Float, y:Float, asDegrees:Bool = false)
{
return if (asDegrees)
Math.atan2(y, x) * TO_DEG;
else
Math.atan2(y, x);
}

/**
* Keeps an angle value between -180 and +180 by wrapping it
* e.g an angle of +270 will be converted to -90
Expand Down Expand Up @@ -101,8 +139,7 @@ class FlxAngle
}

/**
* Find the angle (in radians) between the two FlxSprite, taking their x/y and origin into account.
* The angle is calculated in clockwise positive direction (down = 90 degrees positive, right = 0 degrees positive, up = 90 degrees negative)
* Find the angle between the two FlxSprite, taking their x/y and origin into account.
*
* @param SpriteA The FlxSprite to test from
* @param SpriteB The FlxSprite to test to
Expand All @@ -114,15 +151,36 @@ class FlxAngle
var dx:Float = (SpriteB.x + SpriteB.origin.x) - (SpriteA.x + SpriteA.origin.x);
var dy:Float = (SpriteB.y + SpriteB.origin.y) - (SpriteA.y + SpriteA.origin.y);

if (AsDegrees)
return asDegrees(Math.atan2(dy, dx));
else
return Math.atan2(dy, dx);
return angleFromOrigin(dx, dy, AsDegrees);
}

/**
* Find the angle (in radians) between an FlxSprite and an FlxPoint. The source sprite takes its x/y and origin into account.
* The angle is calculated in clockwise positive direction (down = 90 degrees positive, right = 0 degrees positive, up = 90 degrees negative)
* Find the angle (in degrees) between the two FlxSprite, taking their x/y and origin into account.
*
* @param SpriteA The FlxSprite to test from
* @param SpriteB The FlxSprite to test to
* @return The angle in degrees
*/
public static inline function degreesBetween(SpriteA:FlxSprite, SpriteB:FlxSprite):Float
{
return angleBetween(SpriteA, SpriteB, true);
}

/**
* Find the angle (in radians) between the two FlxSprite, taking their x/y and origin into account.
*
* @param SpriteA The FlxSprite to test from
* @param SpriteB The FlxSprite to test to
* @return The angle in radians
*/
public static inline function radiansBetween(SpriteA:FlxSprite, SpriteB:FlxSprite):Float
{
return angleBetween(SpriteA, SpriteB, false);
}

/**
* Find the angle between an FlxSprite and an FlxPoint.
* The source sprite takes its x/y and origin into account.
*
* @param Sprite The FlxSprite to test from
* @param Target The FlxPoint to angle the FlxSprite towards
Expand All @@ -136,24 +194,46 @@ class FlxAngle

Target.putWeak();

if (AsDegrees)
return asDegrees(Math.atan2(dy, dx));
else
return Math.atan2(dy, dx);
return angleFromOrigin(dx, dy, AsDegrees);
}

/**
* Find the angle (in degrees) between an FlxSprite and an FlxPoint.
* The source sprite takes its x/y and origin into account.
*
* @param Sprite The FlxSprite to test from
* @param Target The FlxPoint to angle the FlxSprite towards
* @return The angle in degrees
*/
public static inline function degreesBetweenPoint(Sprite:FlxSprite, Target:FlxPoint):Float
{
return angleBetweenPoint(Sprite, Target, true);
}

/**
* Find the angle (in radians) between an FlxSprite and an FlxPoint.
* The source sprite takes its x/y and origin into account.
*
* @param Sprite The FlxSprite to test from
* @param Target The FlxPoint to angle the FlxSprite towards
* @return The angle in radians
*/
public static inline function radiansBetweenPoint(Sprite:FlxSprite, Target:FlxPoint):Float
{
return angleBetweenPoint(Sprite, Target, false);
}

#if FLX_MOUSE
/**
* Find the angle (in radians) between an FlxSprite and the mouse, taking their x/y and origin into account.
* The angle is calculated in clockwise positive direction (down = 90 degrees positive, right = 0 degrees positive, up = 90 degrees negative)
* Find the angle between an FlxSprite and the mouse,
* taking their **screen** x/y and origin into account.
*
* @param Object The FlxObject to test from
* @param AsDegrees If you need the value in degrees instead of radians, set to true
* @return The angle (in radians unless AsDegrees is true)
*/
public static function angleBetweenMouse(Object:FlxObject, AsDegrees:Bool = false):Float
{
// In order to get the angle between the object and mouse, we need the objects screen coordinates (rather than world coordinates)
if (Object == null)
return 0;

Expand All @@ -164,17 +244,38 @@ class FlxAngle

p.put();

if (AsDegrees)
return asDegrees(Math.atan2(dy, dx));
else
return Math.atan2(dy, dx);
return angleFromOrigin(dx, dy, AsDegrees);
}

/**
* Find the angle (in degrees) between an FlxSprite and the mouse,
* taking their **screen** x/y and origin into account.
*
* @param Object The FlxObject to test from
* @return The angle in degrees
*/
public static inline function degreesBetweenMouse(Object:FlxObject):Float
{
return angleBetweenMouse(Object, true);
}

/**
* Find the angle (in radians) between an FlxSprite and the mouse,
* taking their **screen** x/y and origin into account.
*
* @param Object The FlxObject to test from
* @return The angle in radians
*/
public static inline function radiansBetweenMouse(Object:FlxObject):Float
{
return angleBetweenMouse(Object, false);
}
#end

#if FLX_TOUCH
/**
* Find the angle (in radians) between an FlxSprite and a FlxTouch, taking their x/y and origin into account.
* The angle is calculated in clockwise positive direction (down = 90 degrees positive, right = 0 degrees positive, up = 90 degrees negative)
* Find the angle between an FlxSprite and a FlxTouch,
taking their **screen** x/y and origin into account.
*
* @param Object The FlxObject to test from
* @param Touch The FlxTouch to test to
Expand All @@ -191,10 +292,33 @@ class FlxAngle

p.put();

if (AsDegrees)
return asDegrees(Math.atan2(dy, dx));
else
return Math.atan2(dy, dx);
return angleFromOrigin(dx, dy, AsDegrees);
}

/**
* Find the angle (in degrees) between an FlxSprite and a FlxTouch,
taking their **screen** x/y and origin into account.
*
* @param Object The FlxObject to test from
* @param Touch The FlxTouch to test to
* @return The angle in degrees
*/
public static inline function degreesBetweenTouch(Object:FlxObject, Touch:FlxTouch):Float
{
return angleBetweenTouch(Object, Touch, true);
}

/**
* Find the angle (in radians) between an FlxSprite and a FlxTouch,
taking their **screen** x/y and origin into account.
*
* @param Object The FlxObject to test from
* @param Touch The FlxTouch to test to
* @return The angle in radians
*/
public static inline function radiansBetweenTouch(Object:FlxObject, Touch:FlxTouch):Float
{
return angleBetweenTouch(Object, Touch, false);
}
#end

Expand All @@ -205,20 +329,10 @@ class FlxAngle
* @param AsDegrees If you need the value in degrees instead of radians, set to true
* @return The angle (in radians unless AsDegrees is true)
*/
@:deprecated("FlxAngle.angleFromFacing is deprecated, use flags.degrees.")
public static function angleFromFacing(Facing:FlxDirectionFlags, AsDegrees:Bool = false):Float
{
var degrees = switch (Facing)
{
case LEFT: 180;
case RIGHT: 0;
case UP: -90;
case DOWN: 90;
case f if (f == UP | LEFT): -135;
case f if (f == UP | RIGHT): -45;
case f if (f == DOWN | LEFT): 135;
case f if (f == DOWN | RIGHT): 45;
default: 0;
}
var degrees = Facing.degrees;
return AsDegrees ? degrees : asRadians(degrees);
}

Expand All @@ -230,6 +344,7 @@ class FlxAngle
* @param point Optional FlxPoint if you don't want a new one created
* @return The point in cartesian coords
*/
@:deprecated("FlxAngle.getCartesianCoords is deprecated, use FlxVector.setPolarDegrees")
public static function getCartesianCoords(Radius:Float, Angle:Float, ?point:FlxPoint):FlxPoint
{
var p = point;
Expand All @@ -247,16 +362,17 @@ class FlxAngle
* @param X x position
* @param Y y position
* @param point Optional FlxPoint if you don't want a new one created
* @return The point in polar coords (x = Radius (degrees), y = Angle)
* @return The point in polar coords (x = Radius, y = Angle (degrees))
*/
@:deprecated("FlxAngle.getCartesianCoords is deprecated, use FlxVector")
public static function getPolarCoords(X:Float, Y:Float, ?point:FlxPoint):FlxPoint
{
var p = point;
if (p == null)
p = FlxPoint.get();

p.x = Math.sqrt((X * X) + (Y * Y));
p.y = Math.atan2(Y, X) * TO_DEG;
p.y = degreesFromOrigin(X, Y);
return p;
}

Expand Down
54 changes: 54 additions & 0 deletions flixel/math/FlxPoint.hx
Original file line number Diff line number Diff line change
Expand Up @@ -374,11 +374,65 @@ class FlxPoint implements IFlxPooled
}

/**
* Calculates the angle from this to another point.
* If the point is straight right of this, 0 is returned.
*
* @param point The other point.
* @return The angle, in radians, between -PI and PI
*/
public inline function radiansTo(point:FlxPoint):Float
{
return FlxAngle.radiansFromOrigin(point.x - x, point.y - y);
}

/**
* Calculates the angle from another point to this.
* If this is straight right of the point, 0 is returned.
*
* @param point The other point.
* @return The angle, in radians, between -PI and PI
*/
public inline function radiansFrom(point:FlxPoint):Float
{
return point.radiansTo(this);
}

/**
* Calculates the angle from this to another point.
* If the point is straight right of this, 0 is returned.
*
* @param point The other point.
* @return The angle, in degrees, between -180 and 180
*/
public inline function degreesTo(point:FlxPoint):Float
{
return FlxAngle.degreesFromOrigin(point.x - x, point.y - y);
}

/**
* Calculates the angle from another point to this.
* If this is straight right of the point, 0 is returned.
*
* @param point The other point.
* @return The angle, in degrees, between -180 and 180
*/
public inline function degreesFrom(point:FlxPoint):Float
{
return point.degreesTo(this);
}

/** DEPRECATED
*
* Calculates the angle between this and another point. 0 degrees points straight up.
*
* Note: Every other flixel function treats straight right as 0 degrees.
*
* Also Note: The result is very innacurate.
*
* @param point The other point.
* @return The angle in degrees, between -180 and 180.
*/
@:deprecated("Use FlxPoint.angleTo instead")
public function angleBetween(point:FlxPoint):Float
{
var x:Float = point.x - x;
Expand Down
Loading