A class containing a set of math-related functions.
Static variables
staticinlineread onlyMIN_VALUE_FLOAT:Float = 0.0000000000000001
Minimum value of a floating point number.
Static methods
staticinlinebound(Value:Float, ?Min:Float, ?Max:Float):Float
Bound a number by a minimum and maximum. Ensures that this number is
no smaller than the minimum, and no larger than the maximum.
Leaving a bound null
means that side is unbounded.
Parameters:
Value | Any number. |
---|---|
Min | Any number. |
Max | Any number. |
Returns:
The bounded value of the number.
staticinlinedistanceBetween(SpriteA:FlxSprite, SpriteB:FlxSprite):Int
Find the distance (in pixels, rounded) between two FlxSprites, taking their origin into account
Parameters:
SpriteA | The first FlxSprite |
---|---|
SpriteB | The second FlxSprite |
Returns:
Distance between the sprites in pixels
staticinlinedistanceToMouse(Sprite:FlxSprite):Int
Find the distance (in pixels, rounded) from the object x/y and the mouse x/y
Parameters:
Sprite | The FlxSprite to test against |
---|
Returns:
The distance between the given sprite and the mouse coordinates
staticinlinedistanceToPoint(Sprite:FlxSprite, Target:FlxPoint):Int
Find the distance (in pixels, rounded) from an FlxSprite to the given FlxPoint, taking the source origin into account.
Parameters:
Sprite | The FlxSprite |
---|---|
Target | The FlxPoint |
Returns:
Distance in pixels
staticinlinedistanceToTouch(Sprite:FlxSprite, Touch:FlxTouch):Int
Find the distance (in pixels, rounded) from the object x/y and the FlxPoint screen x/y
Parameters:
Sprite | The FlxSprite to test against |
---|---|
Touch | The FlxTouch to test against |
Returns:
The distance between the given sprite and the mouse coordinates
staticinlinedotProduct(ax:Float, ay:Float, bx:Float, by:Float):Float
Finds the dot product value of two vectors
Parameters:
ax | Vector X |
---|---|
ay | Vector Y |
bx | Vector X |
by | Vector Y |
Returns:
Result of the dot product
staticinlinefastCos(n:Float):Float
A faster, but less accurate version of Math.cos()
.
About 2-6 times faster with < 0.05% average error.
Parameters:
n | The angle in radians. |
---|
Returns:
An approximated cosine of n
.
staticinlinefastSin(n:Float):Float
A faster but slightly less accurate version of Math.sin()
.
About 2-6 times faster with < 0.05% average error.
Parameters:
n | The angle in radians. |
---|
Returns:
An approximated sine of n
.
staticinlineinBounds(Value:Float, Min:Null<Float>, Max:Null<Float>):Bool
Checks if number is in defined range. A null bound means that side is unbounded.
Parameters:
Value | Number to check. |
---|---|
Min | Lower bound of range. |
Max | Higher bound of range. |
Returns:
Returns true if Value is in range.
staticinlineisDistanceToMouseWithin(Sprite:FlxSprite, Distance:Float, IncludeEqual:Bool = false):Bool
Check if the distance from the object x/y and the mouse x/y is within a specified number. A faster algorithm than distanceToMouse because the Math.sqrt() is avoided.
Parameters:
Sprite | The FlxSprite to test against |
---|---|
Distance | The distance to check |
IncludeEqual | If set to true, the function will return true if the calculated distance is equal to the given Distance |
Returns:
True if the distance between the sprites is less than the given Distance
staticinlineisDistanceToPointWithin(Sprite:FlxSprite, Target:FlxPoint, Distance:Float, IncludeEqual:Bool = false):Bool
Check if the distance from an FlxSprite to the given FlxPoint is within a specified number. A faster algorithm than distanceToPoint because the Math.sqrt() is avoided.
Parameters:
Sprite | The FlxSprite |
---|---|
Target | The FlxPoint |
Distance | The distance to check |
IncludeEqual | If set to true, the function will return true if the calculated distance is equal to the given Distance |
Returns:
True if the distance between the sprites is less than the given Distance
staticinlineisDistanceToTouchWithin(Sprite:FlxSprite, Touch:FlxTouch, Distance:Float, IncludeEqual:Bool = false):Bool
Check if the distance from the object x/y and the FlxPoint screen x/y is within a specified number. A faster algorithm than distanceToTouch because the Math.sqrt() is avoided.
Parameters:
Sprite | The FlxSprite to test against |
---|---|
Distance | The distance to check |
IncludeEqual | If set to true, the function will return true if the calculated distance is equal to the given Distance |
Returns:
True if the distance between the sprites is less than the given Distance
staticinlineisDistanceWithin(SpriteA:FlxSprite, SpriteB:FlxSprite, Distance:Float, IncludeEqual:Bool = false):Bool
Check if the distance between two FlxSprites is within a specified number. A faster algorithm than distanceBetween because the Math.sqrt() is avoided.
Parameters:
SpriteA | The first FlxSprite |
---|---|
SpriteB | The second FlxSprite |
Distance | The distance to check |
IncludeEqual | If set to true, the function will return true if the calculated distance is equal to the given Distance |
Returns:
True if the distance between the sprites is less than the given Distance
staticinlinelerp(a:Float, b:Float, ratio:Float):Float
Returns the linear interpolation of two numbers if ratio
is between 0 and 1, and the linear extrapolation otherwise.
Examples:
lerp(a, b, 0) = a
lerp(a, b, 1) = b
lerp(5, 15, 0.5) = 10
lerp(5, 15, -1) = -5
staticmaxAdd(value:Int, amount:Int, max:Int, min:Int = 0):Int
Adds the given amount to the value, but never lets the value go over the specified maximum or under the specified minimum.
Parameters:
value | The value to add the amount to |
---|---|
amount | The amount to add to the value |
max | The maximum the value is allowed to be |
min | The minimum the value is allowed to be |
Returns:
The new value
staticmouseInFlxRect(useWorldCoords:Bool, rect:FlxRect):Bool
Returns true if the mouse world x/y coordinate are within the given rectangular block
Parameters:
useWorldCoords | If true the world x/y coordinates of the mouse will be used, otherwise screen x/y |
---|---|
rect | The FlxRect to test within. If this is null for any reason this function always returns true. |
Returns:
true if mouse is within the FlxRect, otherwise false
staticnumericComparison(a:Float, b:Float):Int
Returns -1
if a
is smaller, 1
if b
is bigger and 0
if both numbers are equal.
staticpointInCoordinates(pointX:Float, pointY:Float, rectX:Float, rectY:Float, rectWidth:Float, rectHeight:Float):Bool
Returns true if the given x/y coordinate is within the given rectangular block
Parameters:
pointX | The X value to test |
---|---|
pointY | The Y value to test |
rectX | The X value of the region to test within |
rectY | The Y value of the region to test within |
rectWidth | The width of the region to test within |
rectHeight | The height of the region to test within |
Returns:
true if pointX/pointY is within the region, otherwise false
staticpointInFlxRect(pointX:Float, pointY:Float, rect:FlxRect):Bool
Returns true if the given x/y coordinate is within the given rectangular block
Parameters:
pointX | The X value to test |
---|---|
pointY | The Y value to test |
rect | The FlxRect to test within |
Returns:
true if pointX/pointY is within the FlxRect, otherwise false
staticpointInRectangle(pointX:Float, pointY:Float, rect:Rectangle):Bool
Returns true if the given x/y coordinate is within the Rectangle
Parameters:
pointX | The X value to test |
---|---|
pointY | The Y value to test |
rect | The Rectangle to test within |
Returns:
true if pointX/pointY is within the Rectangle, otherwise false
staticremapToRange(value:Float, start1:Float, stop1:Float, start2:Float, stop2:Float):Float
Remaps a number from one range to another.
Parameters:
value | The incoming value to be converted |
---|---|
start1 | Lower bound of the value's current range |
stop1 | Upper bound of the value's current range |
start2 | Lower bound of the value's target range |
stop2 | Upper bound of the value's target range |
Returns:
The remapped value
staticroundDecimal(Value:Float, Precision:Int):Float
Round a decimal number to have reduced precision (less decimal numbers).
roundDecimal(1.2485, 2) = 1.25
Parameters:
Value | Any number. |
---|---|
Precision | Number of decimals the result should have. |
Returns:
The rounded value of that number.
staticinlinesameSign(a:Float, b:Float):Bool
Checks if two numbers have the same sign (using FlxMath.signOf()
).