Phaser.Math
Scope: static
Source: src/math/index.js#L10
Static functions
Static functions
Average
<static> Average(values)
Description:
Calculate the mean average of the given values.
Parameters:
| name | type | optional | description |
|---|---|---|---|
| values | Array.<number> | No | The values to average. |
Returns: number - The average value.
Source: src/math/Average.js#L7
Since: 3.0.0
Bernstein
<static> Bernstein(n, i)
Description:
Calculates a Bernstein basis polynomial coefficient, used as a weighting factor in Bezier curve calculations. The result is the binomial coefficient n! / (i! * (n - i)!), which determines how much influence control point i has on a degree-n Bezier curve.
Parameters:
| name | type | optional | description |
|---|---|---|---|
| n | number | No | The degree of the Bernstein polynomial. |
| i | number | No | The index of the basis function. |
Returns: number - The Bernstein basis coefficient: Factorial(n) / Factorial(i) / Factorial(n - i).
Source: src/math/Bernstein.js#L9
Since: 3.0.0
Between
<static> Between(min, max)
Description:
Compute a random integer between the min and max values, inclusive.
Parameters:
| name | type | optional | description |
|---|---|---|---|
| min | number | No | The minimum value. |
| max | number | No | The maximum value. |
Returns: number - The random integer.
Source: src/math/Between.js#L7
Since: 3.0.0
CatmullRom
<static> CatmullRom(t, p0, p1, p2, p3)
Description:
Calculates a Catmull-Rom interpolated value from the given control points, using a centripetal alpha of 0.5. The interpolation occurs between p1 and p2, with p0 and p3 providing tangent context at each end of the segment. At t = 0 the result equals p1; at t = 1 the result equals p2.
This function is used internally by Phaser's spline curve implementations to produce smooth, continuous curves that pass through each control point.
Parameters:
| name | type | optional | description |
|---|---|---|---|
| t | number | No | The interpolation factor, typically in the range 0 to 1. |
| p0 | number | No | The first control point (influences the tangent at p1). |
| p1 | number | No | The second control point (start of the interpolated segment). |
| p2 | number | No | The third control point (end of the interpolated segment). |
| p3 | number | No | The fourth control point (influences the tangent at p2). |
Returns: number - The interpolated Catmull-Rom value between p1 and p2.
Source: src/math/CatmullRom.js#L7
Since: 3.0.0
CeilTo
<static> CeilTo(value, [place], [base])
Description:
Ceils a given value to the nearest multiple of a base raised to the power of place.
The place is represented by the power applied to base to get that place. For example, with the default base of 10, a place of 0 ceils to the nearest integer, a place of 1 ceils to the nearest 0.1, and a place of -1 ceils to the nearest 10.
Parameters:
| name | type | optional | default | description |
|---|---|---|---|---|
| value | number | No | The value to ceil. | |
| place | number | Yes | 0 | The place to ceil to. |
| base | number | Yes | 10 | The base to ceil in. Default is 10 for decimal. |
Returns: number - The ceiled value.
Source: src/math/CeilTo.js#L7
Since: 3.0.0
Clamp
<static> Clamp(value, min, max)
Description:
Force a value within the boundaries by clamping it to the range min, max.
Parameters:
| name | type | optional | description |
|---|---|---|---|
| value | number | No | The value to be clamped. |
| min | number | No | The minimum bounds. |
| max | number | No | The maximum bounds. |
Returns: number - The clamped value.
Source: src/math/Clamp.js#L7
Since: 3.0.0
DegToRad
<static> DegToRad(degrees)
Description:
Convert the given angle from degrees, to the equivalent angle in radians.
Parameters:
| name | type | optional | description |
|---|---|---|---|
| degrees | number | No | The angle (in degrees) to convert to radians. |
Returns: number - The given angle converted to radians.
Source: src/math/DegToRad.js#L9
Since: 3.0.0
Difference
<static> Difference(a, b)
Description:
Calculates the positive difference of two given numbers.
Parameters:
| name | type | optional | description |
|---|---|---|---|
| a | number | No | The first number in the calculation. |
| b | number | No | The second number in the calculation. |
Returns: number - The positive difference of the two given numbers.
Source: src/math/Difference.js#L7
Since: 3.0.0
Factorial
<static> Factorial(value)
Description:
Calculates the factorial of a given number for integer values greater than 0.
Parameters:
| name | type | optional | description |
|---|---|---|---|
| value | number | No | A positive integer to calculate the factorial of. |
Returns: number - The factorial of the given number.
Source: src/math/Factorial.js#L7
Since: 3.0.0
FloatBetween
<static> FloatBetween(min, max)
Description:
Generate a random floating point number between the two given bounds, minimum inclusive, maximum exclusive.
Parameters:
| name | type | optional | description |
|---|---|---|---|
| min | number | No | The lower bound for the float, inclusive. |
| max | number | No | The upper bound for the float, exclusive. |
Returns: number - A random float within the given range.
Source: src/math/FloatBetween.js#L7
Since: 3.0.0
FloorTo
<static> FloorTo(value, [place], [base])
Description:
Floors to some place comparative to a base, default is 10 for decimal place.
The place is represented by the power applied to base to get that place.
Parameters:
| name | type | optional | default | description |
|---|---|---|---|---|
| value | number | No | The value to floor. | |
| place | number | Yes | 0 | The place to floor to. |
| base | number | Yes | 10 | The base to floor in. Default is 10 for decimal. |
Returns: number - The floored value.
Source: src/math/FloorTo.js#L7
Since: 3.0.0
FromPercent
<static> FromPercent(percent, min, [max])
Description:
Returns a value based on the range between min and max and the percentage given.
Parameters:
| name | type | optional | description |
|---|---|---|---|
| percent | number | No | A value representing the percentage, between 0 and 1. Values outside this range are clamped to it. |
| min | number | No | The minimum value. |
| max | number | Yes | The maximum value. |
Returns: number - The value that is percent percent between min and max.
Source: src/math/FromPercent.js#L9
Since: 3.0.0
GetCentroid
<static> GetCentroid(points, [out])
Description:
Get the centroid or geometric center of a plane figure (the arithmetic mean position of all the points in the figure). Informally, it is the point at which a cutout of the shape could be perfectly balanced on the tip of a pin.
Tags:
- generic
Parameters:
| name | type | optional | description |
|---|---|---|---|
| points | Array.<Phaser.Types.Math.Vector2Like> | No | An array of Vector2Like objects to get the geometric center of. |
| out | Phaser.Math.Vector2 | Yes | A Vector2 object to store the output coordinates in. If not given, a new Vector2 instance is created. |
Returns: Phaser.Math.Vector2 - A Vector2 object representing the geometric center of the given points.
Source: src/math/GetCentroid.js#L9
Since: 4.0.0
GetSpeed
<static> GetSpeed(distance, time)
Description:
Calculate the speed required to cover a given distance in a given time.
The distance is assumed to be in pixels and the time is given in seconds. The result is returned as pixels per millisecond.
Parameters:
| name | type | optional | description |
|---|---|---|---|
| distance | number | No | The distance to travel, in pixels. |
| time | number | No | The time allowed to cover the distance, in seconds. |
Returns: number - The speed required, in pixels per millisecond.
Source: src/math/GetSpeed.js#L7
Since: 3.0.0
GetVec2Bounds
<static> GetVec2Bounds(points, [out])
Description:
Calculates the Axis Aligned Bounding Box (or aabb) from an array of points.
Tags:
- generic
Parameters:
| name | type | optional | description |
|---|---|---|---|
| points | Array.<Phaser.Types.Math.Vector2Like> | No | An array of Vector2Like objects to get the AABB from. |
| out | Phaser.Geom.Rectangle | Yes | A Rectangle object to store the results in. If not given, a new Rectangle instance is created. |
Returns: Phaser.Geom.Rectangle - A Rectangle object holding the AABB values for the given points.
Source: src/math/GetVec2Bounds.js#L9
Since: 4.0.0
Hash
<static> Hash(vector, [algorithm])
Description:
Hash a number or list of numbers.
A hash is an unpredictable transformation of an input, which always returns the same output from the same input. It is useful for generating random data in a predictable way.
For example, you could use a hash to place objects around a scene. Given the same inputs, the objects would always appear in the same places, even though those places appear random.
This function takes 1-4 numbers as input, and returns a single number from 0-1.
Disclaimer: This function is intended for efficiently generating visual variety. It is not intended for cryptographic use. Do not use it for security/authentication/encryption purposes. Use a proper tool instead.
Performance note: A 16ms frame has enough time to generate tens or hundreds of thousands of hash values, depending on system and other activity.
You can select from different hashing algorithms.
-
0: TRIG. This uses sine functions and dot products to hash the input.
-
1: PCG. This uses a permuted congruential generator to hash the input,
but is restricted to integer inputs.
-
2: PCG_FLOAT. This variant of PCG accepts float inputs.
TRIG is the same algorithm used in Phaser 4's Noise object and relatives. It produces decent variety, and accepts any number as input. Its precision is 32 bits. Its input values may lose distinction if larger than 32 bits (4294967296). Its output values cannot differ by more than 1/4294967296. This algorithm is designed to work on graphics hardware that lacks bitshifting capabilities, and is not state of the art.
PCG is a Permuted Congruential Generator. See https://www.pcg-random.org/ for more information on the theory. This algorithm is more modern and considered higher quality. It runs slightly faster than TRIG. It only accepts whole numbers (integers) as input. Its precision is 32 bits. Its input values may lose distinction if larger than 32 bits (4294967296). Its output values cannot differ by more than 1/4294967296.
PCG_FLOAT works just like PCG, but accepts floating-point inputs. This conversion process causes it to run slightly slower than TRIG. The same precision concerns apply.
If your hash values start clustering, you may be using values outside the safe range, where bits available for precision are insufficient. You can keep values in a safe range by sampling along circular paths. This is why it's useful to have several dimensions of input.
Parameters:
| name | type | optional | default | description |
|---|---|---|---|---|
| vector | number | Array.<number> | No | The number or number list to hash. 1 to 4 numbers. | |
| algorithm | number | Yes | 0 | The algorithm to use. 0 is TRIG. 1 is PCG. 2 is PCG_FLOAT. |
Returns: number - - A number from 0-1.
Source: src/math/Hash.js#L11
Since: 4.0.0
HashCell
<static> HashCell(vector, [config])
Description:
Hash a number or list of numbers to a cellular noise value.
A hash is an unpredictable transformation of an input, which always returns the same output from the same input. It is useful for generating random data in a predictable way.
Cellular noise uses hashes to distort a grid, creating distinctive 'cells' in a continuous pattern. While the result is still effectively random, it is continuous: very similar inputs produce very similar outputs (although there may be some minor discontinuities between cells). The result has a distinctive bumpy pattern. This is the algorithm used in the NoiseCell2D family of GameObjects. It is sometimes called Worley or Voronoi noise.
For example, you could use cellular noise to create a series of ridges across a procedural terrain. Every time you generate the same region, it will look the same.
This function takes 1-4 numbers as input, and returns a single number. The range of the return number increases with the number of inputs. It is the distance from the input vector to the nearest distorted cell center. In higher dimensions, the possible distance is higher.
Note: If you specify dimensional properties in the config parameter, ensure they are at least as long as the input vector. Missing values can corrupt the result.
Disclaimer: This function is intended for efficiently generating visual variety. It is not intended for cryptographic use. Do not use it for security/authentication/encryption purposes. Use a proper tool instead.
Performance note: A 16ms frame has enough time to generate a few hundred hash values, depending on device and workload.
See Phaser.Math.Hash for more information on the hashing algorithms.
Parameters:
| name | type | optional | description |
|---|---|---|---|
| vector | number | Array.<number> | No | The input vector to hash. 1 to 4 numbers. |
| config | Phaser.Types.Math.HashCellConfig | Yes | The configuration of the noise cell field. |
Returns: number - The hashed cellular noise value. The range increases with the number of input dimensions, as it represents the distance to the nearest distorted cell center.
Source: src/math/HashCell.js#L3
Since: 4.0.0
HashSimplex
<static> HashSimplex(vector, [config])
Description:
Hash a number or list of numbers to a simplex noise value.
A hash is an unpredictable transformation of an input, which always returns the same output from the same input. It is useful for generating random data in a predictable way.
The output is in the range -1 to 1.
You can set the config object to control the output. You can add octaves of detail, add a turbulent warp, adjust gradient flow, and set a randomness seed.
Simplex noise is an evolution of Perlin noise, both invented by Ken Perlin. It is a form of gradient noise, which forms a smooth, continuous field: very similar inputs produce very similar outputs, although the field itself is still random.
Simplex noise works by assigning random gradients to points on a grid, splitting space into grid cells (using a minimal triangular shape called a simplex), identifying which grid cell the input belongs to, and blending between the gradients of that simplex.
This version uses a flow parameter to animate the noise field. The flow value rotates gradients in place, creating a periodic shifting pattern.
This implementation deliberately copies the shaders used in the NoiseSimplex2D and NoiseSimplex3D game object shaders. They behave in similar fashion, and the config objects are very similar. HashSimplex does not support value factor/add/power terms, as you can simply process the output yourself.
HashSimplex also supports 1-dimensional input. This is automatically padded to 2 dimensions.
Simplex performance varies depending on octaves and warp settings. You might compute a thousand or so hashes per millisecond, depending on device and workload.
Note: If you specify dimensional properties in the config parameter, ensure they are at least as long as the input vector. Missing values can corrupt the result.
Disclaimer: This function is intended for efficiently generating visual variety. It is not intended for cryptographic use. Do not use it for security/authentication/encryption purposes. Use a proper tool instead.
Parameters:
| name | type | optional | description |
|---|---|---|---|
| vector | number | Array.<number> | No | The input vector to hash. 1 to 3 numbers. |
| config | Phaser.Types.Math.HashSimplexConfig | Yes | The configuration of the noise field. |
Returns: number - A noise value in the range -1 to 1.
Source: src/math/HashSimplex.js#L1
Since: 4.0.0
IsEven
<static> IsEven(value)
Description:
Check if a given value is an even number.
Parameters:
| name | type | optional | description |
|---|---|---|---|
| value | number | No | The number to perform the check with. |
Returns: boolean - Whether the number is even or not.
Source: src/math/IsEven.js#L7
Since: 3.0.0
IsEvenStrict
<static> IsEvenStrict(value)
Description:
Check if a given value is an even number using a strict type check.
Parameters:
| name | type | optional | description |
|---|---|---|---|
| value | number | No | The number to perform the check with. |
Returns: boolean - Whether the number is even or not.
Source: src/math/IsEvenStrict.js#L7
Since: 3.0.0
Linear
<static> Linear(p0, p1, t)
Description:
Performs a linear interpolation between two values, returning the value that is t percent of the way between p0 and p1.
Parameters:
| name | type | optional | description |
|---|---|---|---|
| p0 | number | No | The first point. |
| p1 | number | No | The second point. |
| t | number | No | The percentage between p0 and p1 to return, represented as a number between 0 and 1. |
Returns: number - The interpolated value between p0 and p1, at position t.
Source: src/math/Linear.js#L7
Since: 3.0.0
LinearXY
<static> LinearXY(vector1, vector2, [t])
Description:
Interpolates two given Vectors and returns a new Vector between them.
Does not modify either of the passed Vectors.
Parameters:
| name | type | optional | default | description |
|---|---|---|---|---|
| vector1 | Phaser.Math.Vector2 | No | The starting Vector2 to interpolate from. | |
| vector2 | Phaser.Math.Vector2 | No | The ending Vector2 to interpolate to. | |
| t | number | Yes | 0 | The percentage between vector1 and vector2 to return, represented as a number between 0 and 1. |
Returns: Phaser.Math.Vector2 - The step t% of the way between vector1 and vector2.
Source: src/math/LinearXY.js#L7
Since: 3.60.0
MaxAdd
<static> MaxAdd(value, amount, max)
Description:
Add an amount to a value, limiting the maximum result to max.
Parameters:
| name | type | optional | description |
|---|---|---|---|
| value | number | No | The value to add to. |
| amount | number | No | The amount to add. |
| max | number | No | The maximum value to return. |
Returns: number - The resulting value.
Source: src/math/MaxAdd.js#L7
Since: 3.0.0
Median
<static> Median(values)
Description:
Calculate the median of the given values. The values are sorted and the middle value is returned. In case of an even number of values, the average of the two middle values is returned.
Parameters:
| name | type | optional | description |
|---|---|---|---|
| values | Array.<number> | No | The values to find the median of. |
Returns: number - The median value.
Source: src/math/Median.js#L7
Since: 3.54.0
MinSub
<static> MinSub(value, amount, min)
Description:
Subtract an amount from value, limiting the minimum result to min.
Parameters:
| name | type | optional | description |
|---|---|---|---|
| value | number | No | The value to subtract from. |
| amount | number | No | The amount to subtract. |
| min | number | No | The minimum value to return. |
Returns: number - The resulting value.
Source: src/math/MinSub.js#L7
Since: 3.0.0
Percent
<static> Percent(value, min, [max], [upperMax])
Description:
Work out what percentage value is of the range between min and max. If max isn't given then it defaults to min + 1, giving a unit range of width 1 starting at min.
You can optionally specify an upperMax value, 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 | optional | description |
|---|---|---|---|
| value | number | No | The value to determine the percentage of. |
| min | number | No | The minimum value. |
| max | number | Yes | The maximum value. |
| upperMax | number | Yes | The mid-way point in the range that represents 100%. |
Returns: number - A value between 0 and 1 representing the percentage.
Source: src/math/Percent.js#L7
Since: 3.0.0
RadToDeg
<static> RadToDeg(radians)
Description:
Convert the given angle in radians, to the equivalent angle in degrees.
Parameters:
| name | type | optional | description |
|---|---|---|---|
| radians | number | No | The angle in radians to convert to degrees. |
Returns: number - The given angle converted to degrees.
Source: src/math/RadToDeg.js#L9
Since: 3.0.0
RandomXY
<static> RandomXY(vector, [scale])
Description:
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 | optional | default | description |
|---|---|---|---|---|
| vector | Phaser.Math.Vector2 | No | The Vector to compute random values for. | |
| scale | number | Yes | 1 | The scale of the random values. |
Returns: Phaser.Math.Vector2 - The given Vector.
Source: src/math/RandomXY.js#L7
Since: 3.0.0
RandomXYZ
<static> RandomXYZ(vec3, [radius])
Description:
Compute a random position vector on the surface of a sphere of the given radius.
Parameters:
| name | type | optional | default | description |
|---|---|---|---|---|
| vec3 | Phaser.Math.Vector3 | No | The Vector to compute random values for. | |
| radius | number | Yes | 1 | The radius of the sphere. |
Returns: Phaser.Math.Vector3 - The given Vector.
Source: src/math/RandomXYZ.js#L7
Since: 3.0.0
RandomXYZW
<static> RandomXYZW(vec4, [scale])
Description:
Compute a random four-dimensional vector.
Parameters:
| name | type | optional | default | description |
|---|---|---|---|---|
| vec4 | Phaser.Math.Vector4 | No | The Vector to compute random values for. | |
| scale | number | Yes | 1 | The scale of the random values. |
Returns: Phaser.Math.Vector4 - The given Vector.
Source: src/math/RandomXYZW.js#L7
Since: 3.0.0
Rotate
<static> Rotate(point, angle)
Description:
Rotate a given point by a given angle around the origin (0, 0), in an anti-clockwise direction.
Tags:
- generic
Parameters:
| name | type | optional | description |
|---|---|---|---|
| point | Phaser.Types.Math.Vector2Like | No | The point to be rotated. |
| angle | number | No | The angle to rotate by, in radians, in an anti-clockwise direction. |
Returns: Phaser.Types.Math.Vector2Like - The given point, rotated by the given angle in an anti-clockwise direction.
Source: src/math/Rotate.js#L7
Since: 3.0.0
RotateAround
<static> RotateAround(point, x, y, angle)
Description:
Rotate a point around x and y by the given angle, at the same distance.
In polar notation, this maps a point from (r, t) to (r, t + angle), vs. the origin (x, y).
Tags:
- generic
Parameters:
| name | type | optional | description |
|---|---|---|---|
| point | Phaser.Types.Math.Vector2Like | No | The point to be rotated. |
| x | number | No | The horizontal coordinate to rotate around. |
| y | number | No | The vertical coordinate to rotate around. |
| angle | number | No | The angle of rotation in radians. |
Returns: Phaser.Types.Math.Vector2Like - The given point.
Source: src/math/RotateAround.js#L7
Since: 3.0.0
RotateAroundDistance
<static> RotateAroundDistance(point, x, y, angle, distance)
Description:
Rotate a point around x and y by the given angle and distance.
In polar notation, this maps a point from (r, t) to (distance, t + angle), vs. the origin (x, y).
Tags:
- generic
Parameters:
| name | type | optional | description |
|---|---|---|---|
| point | Phaser.Types.Math.Vector2Like | No | The point to be rotated. |
| x | number | No | The horizontal coordinate to rotate around. |
| y | number | No | The vertical coordinate to rotate around. |
| angle | number | No | The angle of rotation in radians. |
| distance | number | No | The distance from (x, y) to place the point at. |
Returns: Phaser.Types.Math.Vector2Like - The given point.
Source: src/math/RotateAroundDistance.js#L7
Since: 3.0.0
RotateTo
<static> RotateTo(point, x, y, angle, distance)
Description:
Position a point at the given angle and distance to (x, y).
Tags:
- generic
Parameters:
| name | type | optional | description |
|---|---|---|---|
| point | Phaser.Types.Math.Vector2Like | No | The point to be positioned. |
| x | number | No | The horizontal coordinate to position from. |
| y | number | No | The vertical coordinate to position from. |
| angle | number | No | The angle of rotation in radians. |
| distance | number | No | The distance from (x, y) to place the point at. |
Returns: Phaser.Types.Math.Vector2Like - The given point.
Source: src/math/RotateTo.js#L7
Since: 3.24.0
RotateVec3
<static> RotateVec3(vec, axis, radians)
Description:
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 | optional | description |
|---|---|---|---|
| vec | Phaser.Math.Vector3 | No | The vector to be rotated. |
| axis | Phaser.Math.Vector3 | No | The axis to rotate around. |
| radians | number | No | The angle of rotation in radians. |
Returns: Phaser.Math.Vector3 - The given vector.
Source: src/math/RotateVec3.js#L15
Since: 3.0.0
RoundAwayFromZero
<static> RoundAwayFromZero(value)
Description:
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 | optional | description |
|---|---|---|---|
| value | number | No | The number to round. |
Returns: number - The rounded number, rounded away from zero.
Source: src/math/RoundAwayFromZero.js#L7
Since: 3.0.0
RoundTo
<static> RoundTo(value, [place], [base])
Description:
Round a value to the given precision.
For example:
RoundTo(123.456, 0) = 123
RoundTo(123.456, 1) = 120
RoundTo(123.456, 2) = 100
To 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.457
Parameters:
| name | type | optional | default | description |
|---|---|---|---|---|
| value | number | No | The value to round. | |
| place | number | Yes | 0 | The place to round to. Positive to round the units, negative to round the decimal. |
| base | number | Yes | 10 | The base to round in. Default is 10 for decimal. |
Returns: number - The rounded value.
Source: src/math/RoundTo.js#L7
Since: 3.0.0
SmootherStep
<static> SmootherStep(x, min, max)
Description:
Calculate a smoother interpolation percentage of x between min and max.
The function receives the number x as an argument and returns 0 if x is less than or equal to the left edge, 1 if x is 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 | optional | description |
|---|---|---|---|
| x | number | No | The input value. |
| min | number | No | The minimum value, also known as the 'left edge', assumed smaller than the 'right edge'. |
| max | number | No | The maximum value, also known as the 'right edge', assumed greater than the 'left edge'. |
Returns: number - The percentage of interpolation, between 0 and 1.
Source: src/math/SmootherStep.js#L7
Since: 3.0.0
SmoothStep
<static> SmoothStep(x, min, max)
Description:
Calculate a smooth interpolation percentage of x between min and max.
The function receives the number x as an argument and returns 0 if x is less than or equal to the left edge, 1 if x is greater than or equal to the right edge, and smoothly interpolates, using a Hermite polynomial, between 0 and 1 otherwise.
Parameters:
| name | type | optional | description |
|---|---|---|---|
| x | number | No | The input value. |
| min | number | No | The minimum value, also known as the 'left edge', assumed smaller than the 'right edge'. |
| max | number | No | The maximum value, also known as the 'right edge', assumed greater than the 'left edge'. |
Returns: number - The percentage of interpolation, between 0 and 1.
Source: src/math/SmoothStep.js#L7
Since: 3.0.0
ToXY
<static> ToXY(index, width, height, [out])
Description:
Returns a Vector2 containing the x and y position of the given index in a width x height sized 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, a Vector2 with x and y set to zero is returned.
Parameters:
| name | type | optional | description |
|---|---|---|---|
| index | number | No | The position within the grid to get the x/y value for. |
| width | number | No | The width of the grid. |
| height | number | No | The height of the grid. |
| out | Phaser.Math.Vector2 | Yes | An optional Vector2 to store the result in. If not given, a new Vector2 instance will be created. |
Returns: Phaser.Math.Vector2 - A Vector2 containing the x and y grid coordinates derived from the given index.
Source: src/math/ToXY.js#L9
Since: 3.19.0
TransformXY
<static> TransformXY(x, y, positionX, positionY, rotation, scaleX, scaleY, [output])
Description:
Takes the x and y coordinates and transforms them into the local coordinate space defined by the given position, rotation, and scale values. This performs an inverse transformation, converting a point from world space into the equivalent local-space coordinates of a Game Object or other transformed entity. This is useful for hit-testing or pointer input when you need to determine where a world-space point falls within a transformed object's local coordinate system.
Parameters:
| name | type | optional | description |
|---|---|---|---|
| x | number | No | The x coordinate to be transformed. |
| y | number | No | The y coordinate to be transformed. |
| positionX | number | No | Horizontal position of the transform point. |
| positionY | number | No | Vertical position of the transform point. |
| rotation | number | No | Rotation of the transform point, in radians. |
| scaleX | number | No | Horizontal scale of the transform point. |
| scaleY | number | No | Vertical scale of the transform point. |
| output | Phaser.Types.Math.Vector2Like | Yes | The output vector, point or object for the translated coordinates. |
Returns: Phaser.Types.Math.Vector2Like - The translated point.
Source: src/math/TransformXY.js#L9
Since: 3.0.0
Within
<static> Within(a, b, tolerance)
Description:
Checks if the two values are within the given tolerance of each other.
Parameters:
| name | type | optional | description |
|---|---|---|---|
| a | number | No | The first value to use in the calculation. |
| b | number | No | The second value to use in the calculation. |
| tolerance | number | No | The tolerance. Anything equal to or less than this value is considered as being within range. |
Returns: boolean - Returns true if the absolute difference between a and b is less than or equal to tolerance.
Source: src/math/Within.js#L7
Since: 3.0.0
Wrap
<static> Wrap(value, min, max)
Description:
Wrap the given value between min (inclusive) and max (exclusive).
When the value exceeds max it wraps back around to min, and when it falls below min it wraps around to just below max. This is useful for cycling through a range, such as keeping an angle within 0–360 degrees or looping a tile index within a tileset.
Parameters:
| name | type | optional | description |
|---|---|---|---|
| value | number | No | The value to wrap. |
| min | number | No | The minimum bound of the range (inclusive). |
| max | number | No | The maximum bound of the range (exclusive). |
Returns: number - The wrapped value, guaranteed to be within [min, max).
Source: src/math/Wrap.js#L7
Since: 3.0.0
Static functions
Static functions
DEG_TO_RAD
DEG_TO_RAD: number
Description:
Conversion factor for degrees to radians (PI / 180). Multiply a degree value by this constant to obtain its equivalent in radians.
Source: src/math/const.js#L36
Since: 3.0.0
EPSILON
EPSILON: number
Description:
A small epsilon value (1.0e-6) used for floating-point comparisons and near-zero checks to avoid precision errors.
Source: src/math/const.js#L27
Since: 3.0.0
MAX_SAFE_INTEGER
MAX_SAFE_INTEGER: number
Description:
The maximum safe integer this browser supports. We use a const for backward compatibility with older browsers.
Source: src/math/const.js#L74
Since: 3.21.0
MIN_SAFE_INTEGER
MIN_SAFE_INTEGER: number
Description:
The minimum safe integer this browser supports. We use a const for backward compatibility with older browsers.
Source: src/math/const.js#L64
Since: 3.21.0
PI_OVER_2
PI_OVER_2: number
Description:
The value of PI / 2, or 90 degrees, in radians.
Source: src/math/const.js#L18
Since: 3.0.0
RAD_TO_DEG
RAD_TO_DEG: number
Description:
Conversion factor for radians to degrees (180 / PI). Multiply a radian value by this constant to obtain its equivalent in degrees.
Source: src/math/const.js#L45
Since: 3.0.0
RND
RND: Phaser.Math.RandomDataGenerator
Description:
An instance of the Random Number Generator. This is not set until the Game boots.
Source: src/math/const.js#L54
Since: 3.0.0
TAU
TAU: number
Description:
The value of PI * 2, representing a full circle (360 degrees) in radians.
Source: src/math/const.js#L9
Since: 4.0.0