This class is for math utility functions previously stored in HXP.
Available since:
4.0.0
Static variables
static read only DEG : Float
static read only NUMBER_MAX_VALUE : Float
static read only RAD : Float
Static functions
static anchorTo (object : Position, anchor : Position, distance : Float = 0) : Void
Anchors the object to a position.
Parameters:
object | The object to anchor. | |
anchor | The anchor object. | |
distance | The max distance object can be anchored to the anchor. |
static angle (x1 : Float, y1 : Float, x2 : Float, y2 : Float) : Float
Finds the angle (in degrees) from point 1 to point 2.
Parameters:
x1 | The first x-position. | |
y1 | The first y-position. | |
x2 | The second x-position. | |
y2 | The second y-position. |
Returns:
The angle from (x1, y1) to (x2, y2).
static angleDifference (angle1 : Float, angle2 : Float) : Float
Get difference between two angles. Result will be between -180 and 180.
Parameters:
angle1 | First angle, in degrees. | |
angle2 | Second angle, in degrees. |
Returns:
The angle difference, in degrees.
static angleXY (object : Position, angle : Float, length : Float = 1, x : Float = 0, y : Float = 0) : Void
Sets the x/y values of the provided object to a vector of the specified angle and length.
Parameters:
object | The object whose x/y properties should be set. | |
angle | The angle of the vector, in degrees. | |
length | The distance to the vector from (0, 0). | |
x | X offset. | |
y | Y offset. |
static approach (value : Float, target : Float, amount : Float) : Float
Approaches the value towards the target, by the specified amount, without overshooting the target.
Parameters:
value | The starting value. | |
target | The target that you want value to approach. | |
amount | How much you want the value to approach target by. |
Returns:
The new value.
static clamp (value : Float, min : Float, max : Float) : Float
Clamps the value within the minimum and maximum values.
Parameters:
value | The Float to evaluate. | |
min | The minimum range. | |
max | The maximum range. |
Returns:
The clamped value.
static clampInRect (object : Position, x : Float, y : Float, width : Float, height : Float, padding : Float = 0) : Void
Clamps the object inside the rectangle.
Parameters:
object | The object to clamp (must have an x and y property). | |
x | Rectangle's x. | |
y | Rectangle's y. | |
width | Rectangle's width. | |
height | Rectangle's height. | |
padding | Rectangle's padding. |
static distance (x1 : Float, y1 : Float, x2 : Float = 0, y2 : Float = 0) : Float
Find the distance between two points.
Parameters:
x1 | The first x-position. | |
y1 | The first y-position. | |
x2 | The second x-position. | |
y2 | The second y-position. |
Returns:
The distance.
static distanceRectPoint (px : Float, py : Float, rx : Float, ry : Float, rw : Float, rh : Float) : Float
Find the distance between a point and a rectangle. Returns 0 if the point is within the rectangle.
Parameters:
px | The x-position of the point. | |
py | The y-position of the point. | |
rx | The x-position of the rect. | |
ry | The y-position of the rect. | |
rw | The width of the rect. | |
rh | The height of the rect. |
Returns:
The distance.
static distanceRects (x1 : Float, y1 : Float, w1 : Float, h1 : Float, x2 : Float, y2 : Float, w2 : Float, h2 : Float) : Float
Find the distance between two rectangles. Will return 0 if the rectangles overlap.
Parameters:
x1 | The x-position of the first rect. | |
y1 | The y-position of the first rect. | |
w1 | The width of the first rect. | |
h1 | The height of the first rect. | |
x2 | The x-position of the second rect. | |
y2 | The y-position of the second rect. | |
w2 | The width of the second rect. | |
h2 | The height of the second rect. |
Returns:
The distance.
static distanceSquared (x1 : Float, y1 : Float, x2 : Float = 0, y2 : Float = 0) : Float
Find the squared distance between two points.
Parameters:
x1 | The first x-position. | |
y1 | The first y-position. | |
x2 | The second x-position. | |
y2 | The second y-position. |
Returns:
The squared distance.
static ilerp (a : Int, b : Int, t : Float = 1) : Int
Linear interpolation between two values. Result rounded to an integer.
Parameters:
a | First value. | |
b | Second value. | |
t | Interpolation factor. |
Returns:
When t=0, returns a. When t=1, returns b. When t=0.5, will return halfway between a and b. Etc.
static lerp (a : Float, b : Float, t : Float = 1) : Float
Linear interpolation between two values.
Parameters:
a | First value. | |
b | Second value. | |
t | Interpolation factor. |
Returns:
When t=0, returns a. When t=1, returns b. When t=0.5, will return halfway between a and b. Etc.
static rotateAround (object : Position, anchor : Position, angle : Float = 0, relative : Bool = true) : Void
Rotates the object around the anchor by the specified amount.
Parameters:
object | Object to rotate around the anchor. | |
anchor | Anchor to rotate around. | |
angle | The amount of degrees to rotate by. | |
relative | If the angle is relative to the angle between the object and the anchor. |
static roundTo (num : Float, precision : Int = 0) : Float
Round a float to the nearest decimal
Parameters:
num | The number to round, | |
precision | The decimal place to round to. |
Returns:
The rounded float.
static scale (value : Float, min : Float, max : Float, min2 : Float, max2 : Float) : Float
Transfers a value from one scale to another scale. For example, scale(.5, 0, 1, 10, 20) == 15, and scale(3, 0, 5, 100, 0) == 40.
Parameters:
value | The value on the first scale. | |
min | The minimum range of the first scale. | |
max | The maximum range of the first scale. | |
min2 | The minimum range of the second scale. | |
max2 | The maximum range of the second scale. |
Returns:
The scaled value.
static scaleClamp (value : Float, min : Float, max : Float, min2 : Float, max2 : Float) : Float
Transfers a value from one scale to another scale, but clamps the return value within the second scale.
Parameters:
value | The value on the first scale. | |
min | The minimum range of the first scale. | |
max | The maximum range of the first scale. | |
min2 | The minimum range of the second scale. | |
max2 | The maximum range of the second scale. |
Returns:
The scaled and clamped value.
static sign (value : Float) : Int
Finds the sign of the provided value.
Parameters:
value | The Float to evaluate. |
Returns:
1 if value > 0, -1 if value < 0, and 0 when value == 0.
static stepTowards (object : Position, x : Float, y : Float, distance : Float = 1) : Void
Steps the object towards a point.
Parameters:
object | Object to move (must have an x and y property). | |
x | X position to step towards. | |
y | Y position to step towards. | |
distance | The distance to step (will not overshoot target). |