- Source: src/math/index.js (Line 10)
Classes
Namespaces
Members
-
<static> DEG_TO_RAD :number
-
For converting degrees to radians (PI / 180)
Type:
- number
- Since: 3.0.0
- Source: src/math/const.js (Line 36)
-
<static> EPSILON :number
-
An epsilon value (1.0e-6)
Type:
- number
- Since: 3.0.0
- Source: src/math/const.js (Line 27)
-
<static> MAX_SAFE_INTEGER :number
-
The maximum safe integer this browser supports. We use a const for backward compatibility with Internet Explorer.
Type:
- number
- Since: 3.21.0
- Source: src/math/const.js (Line 74)
-
<static> MIN_SAFE_INTEGER :number
-
The minimum safe integer this browser supports. We use a const for backward compatibility with Internet Explorer.
Type:
- number
- Since: 3.21.0
- Source: src/math/const.js (Line 64)
-
<static> PI2 :number
-
The value of PI * 2.
Type:
- number
- Since: 3.0.0
- Source: src/math/const.js (Line 9)
-
<static> RAD_TO_DEG :number
-
For converting radians to degrees (180 / PI)
Type:
- number
- Since: 3.0.0
- Source: src/math/const.js (Line 45)
-
<static> RND :Phaser.Math.RandomDataGenerator
-
An instance of the Random Number Generator. This is not set until the Game boots.
Type:
- Since: 3.0.0
- Source: src/math/const.js (Line 54)
-
<static> TAU :number
-
The value of PI * 0.5.
Type:
- number
- Since: 3.0.0
- Source: src/math/const.js (Line 18)
Methods
-
<static> Average(values)
-
Calculate the mean average of the given values.
Parameters:
Name Type Description valuesArray.<number> The values to average.
- Since: 3.0.0
- Source: src/math/Average.js (Line 7)
Returns:
The average value.
- Type
- number
-
<static> Bernstein(n, i)
-
Calculates the Bernstein basis from the three factorial coefficients.
Parameters:
Name Type Description nnumber The first value.
inumber The second value.
- Since: 3.0.0
- Source: src/math/Bernstein.js (Line 9)
Returns:
The Bernstein basis of Factorial(n) / Factorial(i) / Factorial(n - i)
- Type
- number
-
<static> Between(min, max)
-
Compute a random integer between the
minandmaxvalues, inclusive.Parameters:
Name Type Description minnumber The minimum value.
maxnumber The maximum value.
- Since: 3.0.0
- Source: src/math/Between.js (Line 7)
Returns:
The random integer.
- Type
- number
-
<static> CatmullRom(t, p0, p1, p2, p3)
-
Calculates a Catmull-Rom value from the given points, based on an alpha of 0.5.
Parameters:
Name Type Description tnumber The amount to interpolate by.
p0number The first control point.
p1number The second control point.
p2number The third control point.
p3number The fourth control point.
- Since: 3.0.0
- Source: src/math/CatmullRom.js (Line 7)
Returns:
The Catmull-Rom value.
- Type
- number
-
<static> CeilTo(value [, place] [, base])
-
Ceils to some place comparative to a
base, default is 10 for decimal place.The
placeis represented by the power applied tobaseto get that place.Parameters:
Name Type Argument Default Description valuenumber The value to round.
placenumber <optional>
0 The place to round to.
basenumber <optional>
10 The base to round in. Default is 10 for decimal.
- Since: 3.0.0
- Source: src/math/CeilTo.js (Line 7)
Returns:
The rounded value.
- Type
- number
-
<static> Clamp(value, min, max)
-
Force a value within the boundaries by clamping it to the range
min,max.Parameters:
Name Type Description valuenumber The value to be clamped.
minnumber The minimum bounds.
maxnumber The maximum bounds.
- Since: 3.0.0
- Source: src/math/Clamp.js (Line 7)
Returns:
The clamped value.
- Type
- number
-
<static> DegToRad(degrees)
-
Convert the given angle from degrees, to the equivalent angle in radians.
Parameters:
Name Type Description degreesnumber The angle (in degrees) to convert to radians.
- Since: 3.0.0
- Source: src/math/DegToRad.js (Line 9)
Returns:
The given angle converted to radians.
- Type
- number
-
<static> Difference(a, b)
-
Calculates the positive difference of two given numbers.
Parameters:
Name Type Description anumber The first number in the calculation.
bnumber The second number in the calculation.
- Since: 3.0.0
- Source: src/math/Difference.js (Line 7)
Returns:
The positive difference of the two given numbers.
- Type
- number
-
<static> Factorial(value)
-
Calculates the factorial of a given number for integer values greater than 0.
Parameters:
Name Type Description valuenumber A positive integer to calculate the factorial of.
- Since: 3.0.0
- Source: src/math/Factorial.js (Line 7)
Returns:
The factorial of the given number.
- Type
- number
-
<static> FloatBetween(min, max)
-
Generate a random floating point number between the two given bounds, minimum inclusive, maximum exclusive.
Parameters:
Name Type Description minnumber The lower bound for the float, inclusive.
maxnumber The upper bound for the float exclusive.
- Since: 3.0.0
- Source: src/math/FloatBetween.js (Line 7)
Returns:
A random float within the given range.
- Type
- number
-
<static> FloorTo(value [, place] [, base])
-
Floors to some place comparative to a
base, default is 10 for decimal place.The
placeis represented by the power applied tobaseto get that place.Parameters:
Name Type Argument Default Description valuenumber The value to round.
placenumber <optional>
0 The place to round to.
basenumber <optional>
10 The base to round in. Default is 10 for decimal.
- Since: 3.0.0
- Source: src/math/FloorTo.js (Line 7)
Returns:
The rounded value.
- Type
- number
-
<static> FromPercent(percent, min [, max])
-
Return a value based on the range between
minandmaxand the percentage given.Parameters:
Name Type Argument Description percentnumber A value between 0 and 1 representing the percentage.
minnumber The minimum value.
maxnumber <optional>
The maximum value.
- Since: 3.0.0
- Source: src/math/FromPercent.js (Line 9)
Returns:
The value that is
percentpercent betweenminandmax.- Type
- number
-
<static> GetSpeed(distance, time)
-
Calculate a per-ms speed from a distance and time (given in seconds).
Parameters:
Name Type Description distancenumber The distance.
timenumber The time, in seconds.
- Since: 3.0.0
- Source: src/math/GetSpeed.js (Line 7)
Returns:
The speed, in distance per ms.
- Type
- number
Example
// 400px over 1 second is 0.4 px/ms Phaser.Math.GetSpeed(400, 1) // -> 0.4
-
<static> IsEven(value)
-
Check if a given value is an even number.
Parameters:
Name Type Description valuenumber The number to perform the check with.
- Since: 3.0.0
- Source: src/math/IsEven.js (Line 7)
Returns:
Whether the number is even or not.
- Type
- boolean
-
<static> IsEvenStrict(value)
-
Check if a given value is an even number using a strict type check.
Parameters:
Name Type Description valuenumber The number to perform the check with.
- Since: 3.0.0
- Source: src/math/IsEvenStrict.js (Line 7)
Returns:
Whether the number is even or not.
- Type
- boolean
-
<static> Linear(p0, p1, t)
-
Calculates a linear (interpolation) value over t.
Parameters:
Name Type Description p0number The first point.
p1number The second point.
tnumber The percentage between p0 and p1 to return, represented as a number between 0 and 1.
- Since: 3.0.0
- Source: src/math/Linear.js (Line 7)
Returns:
The step t% of the way between p0 and p1.
- Type
- number
-
<static> MaxAdd(value, amount, max)
-
Add an
amountto avalue, limiting the maximum result tomax.Parameters:
Name Type Description valuenumber The value to add to.
amountnumber The amount to add.
maxnumber The maximum value to return.
- Since: 3.0.0
- Source: src/math/MaxAdd.js (Line 7)
Returns:
The resulting value.
- Type
- number
-
<static> MinSub(value, amount, min)
-
Subtract an
amountfromvalue, limiting the minimum result tomin.Parameters:
Name Type Description valuenumber The value to subtract from.
amountnumber The amount to subtract.
minnumber The minimum value to return.
- Since: 3.0.0
- Source: src/math/MinSub.js (Line 7)
Returns:
The resulting value.
- Type
- number
-
<static> Percent(value, min [, max] [, upperMax])
-
Work out what percentage
valueis of the range betweenminandmax. Ifmaxisn't given then it will return the percentage ofvaluetomin.You can optionally specify an
upperMaxvalue, which is a mid-way point in the range that represents 100%, after which the % starts to go down to zero again.Parameters:
Name Type Argument Description valuenumber The value to determine the percentage of.
minnumber The minimum value.
maxnumber <optional>
The maximum value.
upperMaxnumber <optional>
The mid-way point in the range that represents 100%.
- Since: 3.0.0
- Source: src/math/Percent.js (Line 7)
Returns:
A value between 0 and 1 representing the percentage.
- Type
- number
-
<static> RadToDeg(radians)
-
Convert the given angle in radians, to the equivalent angle in degrees.
Parameters:
Name Type Description radiansnumber The angle in radians to convert ot degrees.
- Since: 3.0.0
- Source: src/math/RadToDeg.js (Line 9)
Returns:
The given angle converted to degrees.
- Type
- number
-
<static> RandomXY(vector [, scale])
-
Compute a random unit vector.
Computes random values for the given vector between -1 and 1 that can be used to represent a direction.
Optionally accepts a scale value to scale the resulting vector by.
Parameters:
Name Type Argument Default Description vectorPhaser.Math.Vector2 The Vector to compute random values for.
scalenumber <optional>
1 The scale of the random values.
- Since: 3.0.0
- Source: src/math/RandomXY.js (Line 7)
Returns:
The given Vector.
- Type
- Phaser.Math.Vector2
-
<static> RandomXYZ(vec3 [, radius])
-
Compute a random position vector in a spherical area, optionally defined by the given radius.
Parameters:
Name Type Argument Default Description vec3Phaser.Math.Vector3 The Vector to compute random values for.
radiusnumber <optional>
1 The radius.
- Since: 3.0.0
- Source: src/math/RandomXYZ.js (Line 7)
Returns:
The given Vector.
- Type
- Phaser.Math.Vector3
-
<static> RandomXYZW(vec4 [, scale])
-
Compute a random four-dimensional vector.
Parameters:
Name Type Argument Default Description vec4Phaser.Math.Vector4 The Vector to compute random values for.
scalenumber <optional>
1 The scale of the random values.
- Since: 3.0.0
- Source: src/math/RandomXYZW.js (Line 7)
Returns:
The given Vector.
- Type
- Phaser.Math.Vector4
-
<static> Rotate(point, angle)
-
Rotate a given point by a given angle around the origin (0, 0), in an anti-clockwise direction.
Parameters:
Name Type Description pointPhaser.Geom.Point | object The point to be rotated.
anglenumber The angle to be rotated by in an anticlockwise direction.
- Since: 3.0.0
- Source: src/math/Rotate.js (Line 7)
Returns:
The given point, rotated by the given angle in an anticlockwise direction.
- Type
- Phaser.Geom.Point
-
<static> RotateAround(point, x, y, angle)
-
Rotate a
pointaroundxandyto the givenangle, at the same distance.In polar notation, this maps a point from (r, t) to (r, angle), vs. the origin (x, y).
Parameters:
Name Type Description pointPhaser.Geom.Point | object The point to be rotated.
xnumber The horizontal coordinate to rotate around.
ynumber The vertical coordinate to rotate around.
anglenumber The angle of rotation in radians.
- Since: 3.0.0
- Source: src/math/RotateAround.js (Line 7)
Returns:
The given point.
-
<static> RotateAroundDistance(point, x, y, angle, distance)
-
Rotate a
pointaroundxandyby the givenangleanddistance.In polar notation, this maps a point from (r, t) to (distance, t + angle), vs. the origin (x, y).
Parameters:
Name Type Description pointPhaser.Geom.Point | object The point to be rotated.
xnumber The horizontal coordinate to rotate around.
ynumber The vertical coordinate to rotate around.
anglenumber The angle of rotation in radians.
distancenumber The distance from (x, y) to place the point at.
- Since: 3.0.0
- Source: src/math/RotateAroundDistance.js (Line 7)
Returns:
The given point.
-
<static> RotateTo(point, x, y, angle, distance)
-
Position a
pointat the givenangleanddistanceto (x,y).Parameters:
Name Type Description pointPhaser.Types.Math.Vector2Like The point to be positioned.
xnumber The horizontal coordinate to position from.
ynumber The vertical coordinate to position from.
anglenumber The angle of rotation in radians.
distancenumber The distance from (x, y) to place the point at.
- Since: 3.24.0
- Source: src/math/RotateTo.js (Line 7)
Returns:
The given point.
-
<static> RotateVec3(vec, axis, radians)
-
Rotates a vector in place by axis angle.
This is the same as transforming a point by an axis-angle quaternion, but it has higher precision.
Parameters:
Name Type Description vecPhaser.Math.Vector3 The vector to be rotated.
axisPhaser.Math.Vector3 The axis to rotate around.
radiansnumber The angle of rotation in radians.
- Since: 3.0.0
- Source: src/math/RotateVec3.js (Line 15)
Returns:
The given vector.
- Type
- Phaser.Math.Vector3
-
<static> RoundAwayFromZero(value)
-
Round a given number so it is further away from zero. That is, positive numbers are rounded up, and negative numbers are rounded down.
Parameters:
Name Type Description valuenumber The number to round.
- Since: 3.0.0
- Source: src/math/RoundAwayFromZero.js (Line 7)
Returns:
The rounded number, rounded away from zero.
- Type
- number
-
<static> RoundTo(value [, place] [, base])
-
Round a value to the given precision.
For example:
RoundTo(123.456, 0) = 123 RoundTo(123.456, 1) = 120 RoundTo(123.456, 2) = 100To round the decimal, i.e. to round to precision, pass in a negative
place:RoundTo(123.456789, 0) = 123 RoundTo(123.456789, -1) = 123.5 RoundTo(123.456789, -2) = 123.46 RoundTo(123.456789, -3) = 123.457Parameters:
Name Type Argument Default Description valuenumber The value to round.
placenumber <optional>
0 The place to round to. Positive to round the units, negative to round the decimal.
basenumber <optional>
10 The base to round in. Default is 10 for decimal.
- Since: 3.0.0
- Source: src/math/RoundTo.js (Line 7)
Returns:
The rounded value.
- Type
- number
-
<static> SinCosTableGenerator(length [, sinAmp] [, cosAmp] [, frequency])
-
Generate a series of sine and cosine values.
Parameters:
Name Type Argument Default Description lengthnumber The number of values to generate.
sinAmpnumber <optional>
1 The sine value amplitude.
cosAmpnumber <optional>
1 The cosine value amplitude.
frequencynumber <optional>
1 The frequency of the values.
- Since: 3.0.0
- Source: src/math/SinCosTableGenerator.js (Line 7)
Returns:
The generated values.
-
<static> SmootherStep(x, min, max)
-
Calculate a smoother interpolation percentage of
xbetweenminandmax.The function receives the number
xas an argument and returns 0 ifxis less than or equal to the left edge, 1 ifxis greater than or equal to the right edge, and smoothly interpolates, using a Hermite polynomial, between 0 and 1 otherwise.Produces an even smoother interpolation than Phaser.Math.SmoothStep.
Parameters:
Name Type Description xnumber The input value.
minnumber The minimum value, also known as the 'left edge', assumed smaller than the 'right edge'.
maxnumber The maximum value, also known as the 'right edge', assumed greater than the 'left edge'.
- Since: 3.0.0
- Source: src/math/SmootherStep.js (Line 7)
- See:
Returns:
The percentage of interpolation, between 0 and 1.
- Type
- number
-
<static> SmoothStep(x, min, max)
-
Calculate a smooth interpolation percentage of
xbetweenminandmax.The function receives the number
xas an argument and returns 0 ifxis less than or equal to the left edge, 1 ifxis greater than or equal to the right edge, and smoothly interpolates, using a Hermite polynomial, between 0 and 1 otherwise.Parameters:
Name Type Description xnumber The input value.
minnumber The minimum value, also known as the 'left edge', assumed smaller than the 'right edge'.
maxnumber The maximum value, also known as the 'right edge', assumed greater than the 'left edge'.
- Since: 3.0.0
- Source: src/math/SmoothStep.js (Line 7)
- See:
Returns:
The percentage of interpolation, between 0 and 1.
- Type
- number
-
<static> ToXY(index, width, height [, out])
-
Returns a Vector2 containing the x and y position of the given index in a
widthxheightsized grid.For example, in a 6 x 4 grid, index 16 would equal x: 4 y: 2.
If the given index is out of range an empty Vector2 is returned.
Parameters:
Name Type Argument Description indexnumber The position within the grid to get the x/y value for.
widthnumber The width of the grid.
heightnumber The height of the grid.
outPhaser.Math.Vector2 <optional>
An optional Vector2 to store the result in. If not given, a new Vector2 instance will be created.
- Since: 3.19.0
- Source: src/math/ToXY.js (Line 9)
Returns:
A Vector2 where the x and y properties contain the given grid index.
- Type
- Phaser.Math.Vector2
-
<static> TransformXY(x, y, positionX, positionY, rotation, scaleX, scaleY [, output])
-
Takes the
xandycoordinates and transforms them into the same space as defined by the position, rotation and scale values.Parameters:
Name Type Argument Description xnumber The x coordinate to be transformed.
ynumber The y coordinate to be transformed.
positionXnumber Horizontal position of the transform point.
positionYnumber Vertical position of the transform point.
rotationnumber Rotation of the transform point, in radians.
scaleXnumber Horizontal scale of the transform point.
scaleYnumber Vertical scale of the transform point.
outputPhaser.Math.Vector2 | Phaser.Geom.Point | object <optional>
The output vector, point or object for the translated coordinates.
- Since: 3.0.0
- Source: src/math/TransformXY.js (Line 9)
Returns:
The translated point.
- Type
- Phaser.Math.Vector2 | Phaser.Geom.Point | object
-
<static> Within(a, b, tolerance)
-
Checks if the two values are within the given
toleranceof each other.Parameters:
Name Type Description anumber The first value to use in the calculation.
bnumber The second value to use in the calculation.
tolerancenumber The tolerance. Anything equal to or less than this value is considered as being within range.
- Since: 3.0.0
- Source: src/math/Within.js (Line 7)
Returns:
Returns
trueifais less than or equal to the tolerance ofb.- Type
- boolean
-
<static> Wrap(value, min, max)
-
Wrap the given
valuebetweenminand `max.Parameters:
Name Type Description valuenumber The value to wrap.
minnumber The minimum value.
maxnumber The maximum value.
- Since: 3.0.0
- Source: src/math/Wrap.js (Line 7)
Returns:
The wrapped value.
- Type
- number
