new Math()
A collection of useful mathematical functions.
These are normally accessed through game.math.
- Source:
- src/math/Math.js line 17
- See:
Members
-
<static> DEG_TO_RAD
-
Degrees to Radians factor.
- Source:
- src/math/Math.js line 37
Properties:
Name Type Description Phaser.Math#DEG_TO_RADnumber -
<static> HALF_PI
-
Half PI.
- Default Value:
-
- ~1.570
- Source:
- src/math/Math.js line 31
Properties:
Name Type Description Phaser.Math#HALF_PInumber -
<static> PI2
-
Twice PI.
- Default Value:
-
- ~6.283
- Source:
- src/math/Math.js line 24
Properties:
Name Type Description Phaser.Math#PI2number -
<static> RAD_TO_DEG
-
Degrees to Radians factor.
- Source:
- src/math/Math.js line 43
Properties:
Name Type Description Phaser.Math#RAD_TO_DEGnumber
Methods
-
angleBetween(x1, y1, x2, y2)
-
Find the angle of a segment from (x1, y1) -> (x2, y2).
Parameters:
Name Type Description x1number The x coordinate of the first value.
y1number The y coordinate of the first value.
x2number The x coordinate of the second value.
y2number The y coordinate of the second value.
- Source:
- src/math/Math.js line 541
Returns:
The angle, in radians.
- Type
- number
-
angleBetweenPoints(point1, point2)
-
Find the angle of a segment from (point1.x, point1.y) -> (point2.x, point2.y).
Parameters:
Name Type Description point1Phaser.Point The first point.
point2Phaser.Point The second point.
- Source:
- src/math/Math.js line 574
Returns:
The angle between the two points, in radians.
- Type
- number
-
angleBetweenPointsY(point1, point2)
-
Find the angle of a segment from (point1.x, point1.y) -> (point2.x, point2.y).
Parameters:
Name Type Description point1Phaser.Point point2Phaser.Point - Source:
- src/math/Math.js line 587
Returns:
The angle, in radians.
- Type
- number
-
angleBetweenY(x1, y1, x2, y2)
-
Find the angle of a segment from (x1, y1) -> (x2, y2).
The difference between this method and Math.angleBetween is that this assumes the y coordinate travels down the screen.
Parameters:
Name Type Description x1number The x coordinate of the first value.
y1number The y coordinate of the first value.
x2number The x coordinate of the second value.
y2number The y coordinate of the second value.
- Source:
- src/math/Math.js line 556
Returns:
The angle, in radians.
- Type
- number
-
average()
-
Averages all values passed to the function and returns the result.
- Source:
- src/math/Math.js line 249
Returns:
The average of all given values.
- Type
- number
-
<protected> bernstein(n, i)
-
Parameters:
Name Type Description nnumber inumber - Source:
- src/math/Math.js line 962
Returns:
- Type
- number
-
between(min, max)
-
Returns a random integer in the range
[min, max]. If these parameters are not in order than they will be put in order. Default is 0 forminand 1 formax.Parameters:
Name Type Description minnumber The minimum value. Must be a Number.
maxnumber The maximum value. Must be a Number.
- Source:
- src/math/Math.js line 139
Returns:
An integer between min (inclusive) and max (inclusive).
- Type
- number
-
bezierInterpolation(v, k)
-
A Bezier Interpolation Method, mostly used by Phaser.Tween.
Parameters:
Name Type Description vArray The input array of values to interpolate between.
knumber The percentage of interpolation, between 0 and 1.
- Source:
- src/math/Math.js line 888
Returns:
The interpolated value
- Type
- number
-
<protected> catmullRom(p0, p1, p2, p3, t)
-
Calculates a catmum rom value.
Parameters:
Name Type Description p0number p1number p2number p3number tnumber - Source:
- src/math/Math.js line 996
Returns:
- Type
- number
-
catmullRomInterpolation(v, k)
-
A Catmull Rom Interpolation Method, mostly used by Phaser.Tween.
Parameters:
Name Type Description vArray The input array of values to interpolate between.
knumber The percentage of interpolation, between 0 and 1.
- Source:
- src/math/Math.js line 909
Returns:
The interpolated value
- Type
- number
-
ceilTo(value [, place] [, base])
-
Ceils to some place comparative to a
base, default is 10 for decimal place. Theplaceis 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.
- Source:
- src/math/Math.js line 424
Returns:
The rounded value.
- Type
- number
-
clamp(v, min, max)
-
Force a value within the boundaries by clamping it to the range
min,max.Parameters:
Name Type Description vfloat The value to be clamped.
minfloat The minimum bounds.
maxfloat The maximum bounds.
- Source:
- src/math/Math.js line 1150
Returns:
The clamped value.
- Type
- number
-
clampBottom(x, a)
-
Clamp
xto the range[a, Infinity). Roughly the same asMath.max(x, a), except for NaN handling.Parameters:
Name Type Description xnumber anumber - Source:
- src/math/Math.js line 1175
Returns:
- Type
- number
-
degToRad(degrees)
-
Convert degrees to radians.
Parameters:
Name Type Description degreesnumber Angle in degrees.
- Source:
- src/math/Math.js line 45
Returns:
Angle in radians.
- Type
- number
-
difference(a, b)
-
The absolute difference between two values.
Parameters:
Name Type Description anumber The first value to check.
bnumber The second value to check.
- Source:
- src/math/Math.js line 1018
Returns:
The absolute difference between the two values.
- Type
- number
-
distance(x1, y1, x2, y2)
-
Returns the euclidian distance between the two given set of coordinates.
Parameters:
Name Type Description x1number y1number x2number y2number - Source:
- src/math/Math.js line 1095
Returns:
The distance between the two sets of coordinates.
- Type
- number
-
distancePow(x1, y1, x2, y2 [, pow])
-
Returns the distance between the two given set of coordinates at the power given.
Parameters:
Name Type Argument Default Description x1number y1number x2number y2number pownumber <optional>
2 - Source:
- src/math/Math.js line 1132
Returns:
The distance between the two sets of coordinates.
- Type
- number
-
distanceSq(x1, y1, x2, y2)
-
Returns the euclidean distance squared between the two given set of coordinates (cuts out a square root operation before returning).
Parameters:
Name Type Description x1number y1number x2number y2number - Source:
- src/math/Math.js line 1113
Returns:
The distance squared between the two sets of coordinates.
- Type
- number
-
factorial(value)
-
Parameters:
Name Type Description valuenumber the number you want to evaluate
- Source:
- src/math/Math.js line 974
Returns:
- Type
- number
-
floorTo(value [, place] [, base])
-
Floors to some place comparative to a
base, default is 10 for decimal place. Theplaceis 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.
- Source:
- src/math/Math.js line 404
Returns:
The rounded value.
- Type
- number
-
fuzzyCeil(val [, epsilon])
-
Applies a fuzzy ceil to the given value.
Parameters:
Name Type Argument Default Description valnumber The value to ceil.
epsilonnumber <optional>
0.0001 The epsilon (a small value used in the calculation)
- Source:
- src/math/Math.js line 219
Returns:
ceiling(val-epsilon)
- Type
- number
-
fuzzyEqual(a, b [, epsilon])
-
Two number are fuzzyEqual if their difference is less than epsilon.
Parameters:
Name Type Argument Default Description anumber The first number to compare.
bnumber The second number to compare.
epsilonnumber <optional>
0.0001 The epsilon (a small value used in the calculation)
- Source:
- src/math/Math.js line 171
Returns:
True if |a-b|<epsilon
- Type
- boolean
-
fuzzyFloor(val [, epsilon])
-
Applies a fuzzy floor to the given value.
Parameters:
Name Type Argument Default Description valnumber The value to floor.
epsilonnumber <optional>
0.0001 The epsilon (a small value used in the calculation)
- Source:
- src/math/Math.js line 234
Returns:
floor(val+epsilon)
- Type
- number
-
fuzzyGreaterThan(a, b [, epsilon])
-
ais fuzzyGreaterThanbif it is more than b - epsilon.Parameters:
Name Type Argument Default Description anumber The first number to compare.
bnumber The second number to compare.
epsilonnumber <optional>
0.0001 The epsilon (a small value used in the calculation)
- Source:
- src/math/Math.js line 203
Returns:
True if a>b+epsilon
- Type
- boolean
-
fuzzyLessThan(a, b [, epsilon])
-
ais fuzzyLessThanbif it is less than b + epsilon.Parameters:
Name Type Argument Default Description anumber The first number to compare.
bnumber The second number to compare.
epsilonnumber <optional>
0.0001 The epsilon (a small value used in the calculation)
- Source:
- src/math/Math.js line 187
Returns:
True if a<b+epsilon
- Type
- boolean
-
getNextPowerOfTwo(value)
-
Given a number, this function returns the closest number that is a power of two. This function is from the Starling Framework.
Parameters:
Name Type Description valuenumber The value to get the closest power of two from.
- Source:
- src/math/Math.js line 69
Returns:
The closest number that is a power of two.
- Type
- number
-
getShortestAngle(angle1, angle2)
-
Gets the shortest angle between
angle1andangle2. Both angles must be in the range -180 to 180, which is the same clamped range thatsprite.angleuses, so you can pass in two sprite angles to this method, and get the shortest angle back between the two of them.The angle returned will be in the same range. If the returned angle is greater than 0 then it's a counter-clockwise rotation, if < 0 then it's a clockwise rotation.
Parameters:
Name Type Description angle1number The first angle. In the range -180 to 180.
angle2number The second angle. In the range -180 to 180.
- Source:
- src/math/Math.js line 512
Returns:
The shortest angle, in degrees. If greater than zero it's a counter-clockwise rotation.
- Type
- number
-
hypot(a, b)
-
Returns the length of the hypotenuse connecting two segments of given lengths.
Parameters:
Name Type Description anumber bnumber - Source:
- src/math/Math.js line 1082
Returns:
The length of the hypotenuse connecting the given lengths.
- Type
- number
-
isEven(n)
-
Returns true if the number given is even.
Parameters:
Name Type Description ninteger The number to check.
- Source:
- src/math/Math.js line 715
Returns:
True if the given number is even. False if the given number is odd.
- Type
- boolean
-
isOdd(n)
-
Returns true if the number given is odd.
Parameters:
Name Type Description ninteger The number to check.
- Source:
- src/math/Math.js line 702
Returns:
True if the given number is odd. False if the given number is even.
- Type
- boolean
-
isPowerOfTwo(width, height)
-
Checks if the given dimensions make a power of two texture.
Parameters:
Name Type Description widthnumber The width to check.
heightnumber The height to check.
- Source:
- src/math/Math.js line 97
Returns:
True if the width and height are a power of two.
- Type
- boolean
-
linear(p0, p1, t)
-
Calculates a linear (interpolation) value over t.
Parameters:
Name Type Description p0number p1number tnumber A value between 0 and 1.
- Source:
- src/math/Math.js line 948
Returns:
- Type
- number
-
linearInterpolation(v, k)
-
A Linear Interpolation Method, mostly used by Phaser.Tween.
Parameters:
Name Type Description vArray The input array of values to interpolate between.
knumber The amount of interpolation, between 0 (start) and 1 (end).
- Source:
- src/math/Math.js line 861
Returns:
The interpolated value
- Type
- number
-
mapLinear(x, a1, a2, b1, b2)
-
Linear mapping from range <a1, a2> to range <b1, b2>
Parameters:
Name Type Description xnumber The value to map
a1number First endpoint of the range <a1, a2>
a2number Final endpoint of the range <a1, a2>
b1number First endpoint of the range <b1, b2>
b2number Final endpoint of the range <b1, b2>
- Source:
- src/math/Math.js line 1204
Returns:
- Type
- number
-
max()
-
Variation of Math.max that can be passed either an array of numbers or the numbers as parameters.
Prefer the standard
Math.maxfunction when appropriate.Returns:
The largest value from those given.
- Type
- number
-
maxAdd(value, amount, max)
-
Adds the given amount to the value, but never lets the value go over the specified maximum.
Parameters:
Name Type Description valuenumber The value to add the amount to.
amountnumber The amount to add to the value.
maxnumber The maximum the value is allowed to be.
- Source:
- src/math/Math.js line 622
Returns:
The new value.
- Type
- number
-
maxProperty()
-
Variation of Math.max that can be passed a property and either an array of objects or the objects as parameters. It will find the largest matching property value from the given objects.
- Source:
- src/math/Math.js line 819
Returns:
The largest value from those given.
- Type
- number
-
min()
-
Variation of Math.min that can be passed either an array of numbers or the numbers as parameters.
Prefer the standard
Math.minfunction when appropriate.Returns:
The lowest value from those given.
- Type
- number
-
minProperty()
-
Variation of Math.min that can be passed a property and either an array of objects or the objects as parameters. It will find the lowest matching property value from the given objects.
- Source:
- src/math/Math.js line 790
Returns:
The lowest value from those given.
- Type
- number
-
minSub(value, amount, min)
-
Subtracts the given amount from the value, but never lets the value go below the specified minimum.
Parameters:
Name Type Description valuenumber The base value.
amountnumber The amount to subtract from the base value.
minnumber The minimum the value is allowed to be.
- Source:
- src/math/Math.js line 636
Returns:
The new value.
- Type
- number
-
normalizeAngle(angleRad)
-
Normalizes an angle to the [0,2pi) range.
Parameters:
Name Type Description angleRadnumber The angle to normalize, in radians.
- Source:
- src/math/Math.js line 610
Returns:
The angle, fit within the [0,2pi] range, in radians.
- Type
- number
-
percent(a, b [, base])
-
Work out what percentage value
ais of valuebusing the given base.Parameters:
Name Type Argument Default Description anumber The value to work out the percentage for.
bnumber The value you wish to get the percentage of.
basenumber <optional>
0 The base value.
- Source:
- src/math/Math.js line 1268
Returns:
The percentage a is of b, between 0 and 1.
- Type
- number
-
radToDeg(radians)
-
Convert radians to degrees.
Parameters:
Name Type Description radiansnumber Angle in radians.
- Source:
- src/math/Math.js line 57
Returns:
Angle in degrees
- Type
- number
-
random(min, max)
-
Returns a random float in the range
[min, max). If these parameters are not in order than they will be put in order. Default is 0 forminand 1 formax.Parameters:
Name Type Description minnumber The minimum value. Must be a Number.
maxnumber The maximum value. Must be a Number.
- Source:
- src/math/Math.js line 110
Returns:
A floating point number between min (inclusive) and max (exclusive).
- Type
- number
-
reverseAngle(angleRad)
-
Reverses an angle.
Parameters:
Name Type Description angleRadnumber The angle to reverse, in radians.
- Source:
- src/math/Math.js line 599
Returns:
The reverse angle, in radians.
- Type
- number
-
rotateToAngle(currentAngle, targetAngle [, lerp])
-
Rotates currentAngle towards targetAngle, taking the shortest rotation distance. The lerp argument is the amount to rotate by in this call.
Parameters:
Name Type Argument Default Description currentAnglenumber The current angle, in radians.
targetAnglenumber The target angle to rotate to, in radians.
lerpnumber <optional>
0.05 The lerp value to add to the current angle.
- Source:
- src/math/Math.js line 462
Returns:
The adjusted angle.
- Type
- number
-
roundAwayFromZero(value)
-
Round to the next whole number away from zero.
Parameters:
Name Type Description valuenumber Any number.
- Source:
- src/math/Math.js line 1031
Returns:
The rounded value of that number.
- Type
- integer
-
roundTo(value [, place] [, base])
-
Round to some place comparative to a
base, default is 10 for decimal place. Theplaceis represented by the power applied tobaseto get that place.e.g. 2000/7 ~= 285.714285714285714285714 ~= (bin)100011101.1011011011011011 roundTo(2000/7,3) === 0 roundTo(2000/7,2) == 300 roundTo(2000/7,1) == 290 roundTo(2000/7,0) == 286 roundTo(2000/7,-1) == 285.7 roundTo(2000/7,-2) == 285.71 roundTo(2000/7,-3) == 285.714 roundTo(2000/7,-4) == 285.7143 roundTo(2000/7,-5) == 285.71429 roundTo(2000/7,3,2) == 288 -- 100100000 roundTo(2000/7,2,2) == 284 -- 100011100 roundTo(2000/7,1,2) == 286 -- 100011110 roundTo(2000/7,0,2) == 286 -- 100011110 roundTo(2000/7,-1,2) == 285.5 -- 100011101.1 roundTo(2000/7,-2,2) == 285.75 -- 100011101.11 roundTo(2000/7,-3,2) == 285.75 -- 100011101.11 roundTo(2000/7,-4,2) == 285.6875 -- 100011101.1011 roundTo(2000/7,-5,2) == 285.71875 -- 100011101.10111Note what occurs when we round to the 3rd space (8ths place), 100100000, this is to be assumed because we are rounding 100011.1011011011011011 which rounds up.
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.
- Source:
- src/math/Math.js line 359
Returns:
The rounded value.
- Type
- number
-
shear(n)
-
Parameters:
Name Type Description nnumber - Source:
- src/math/Math.js line 269
Returns:
n mod 1
- Type
- number
-
sign(x)
-
A value representing the sign of the value: -1 for negative, +1 for positive, 0 if value is 0.
This works differently from
Math.signfor values of NaN and -0, etc.Parameters:
Name Type Description xnumber - Source:
- src/math/Math.js line 1254
Returns:
An integer in {-1, 0, 1}
- Type
- integer
-
sinCosGenerator(length, sinAmplitude, cosAmplitude, frequency)
-
Generate a sine and cosine table simultaneously and extremely quickly. The parameters allow you to specify the length, amplitude and frequency of the wave. This generator is fast enough to be used in real-time. Code based on research by Franky of scene.at
Parameters:
Name Type Description lengthnumber The length of the wave
sinAmplitudenumber The amplitude to apply to the sine table (default 1.0) if you need values between say -+ 125 then give 125 as the value
cosAmplitudenumber The amplitude to apply to the cosine table (default 1.0) if you need values between say -+ 125 then give 125 as the value
frequencynumber The frequency of the sine and cosine table data
- Source:
- src/math/Math.js line 1044
Returns:
Returns the table data.
- Type
- Object
-
smootherstep(x, min, max)
-
Smootherstep function as detailed at http://en.wikipedia.org/wiki/Smoothstep
Parameters:
Name Type Description xfloat The input value.
minfloat The left edge. Should be smaller than the right edge.
maxfloat The right edge.
- Source:
- src/math/Math.js line 1238
Returns:
A value between 0 and 1.
- Type
- float
-
smoothstep(x, min, max)
-
Smoothstep function as detailed at http://en.wikipedia.org/wiki/Smoothstep
Parameters:
Name Type Description xfloat The input value.
minfloat The left edge. Should be smaller than the right edge.
maxfloat The right edge.
- Source:
- src/math/Math.js line 1220
Returns:
A value between 0 and 1.
- Type
- float
-
snapTo(input, gap [, start])
-
Snap a value to nearest grid slice, using rounding.
Example: if you have an interval gap of 5 and a position of 12... you will snap to 10 whereas 14 will snap to 15.
Parameters:
Name Type Argument Default Description inputnumber The value to snap.
gapnumber The interval gap of the grid.
startnumber <optional>
0 Optional starting offset for gap.
- Source:
- src/math/Math.js line 279
Returns:
The snapped value.
- Type
- number
-
snapToCeil(input, gap [, start])
-
Snap a value to nearest grid slice, using ceil.
Example: if you have an interval gap of 5 and a position of 12... you will snap to 15. As will 14 will snap to 15... but 16 will snap to 20.
Parameters:
Name Type Argument Default Description inputnumber The value to snap.
gapnumber The interval gap of the grid.
startnumber <optional>
0 Optional starting offset for gap.
- Source:
- src/math/Math.js line 332
Returns:
The snapped value.
- Type
- number
-
snapToFloor(input, gap [, start])
-
Snap a value to nearest grid slice, using floor.
Example: if you have an interval gap of 5 and a position of 12... you will snap to 10. As will 14 snap to 10... but 16 will snap to 15.
Parameters:
Name Type Argument Default Description inputnumber The value to snap.
gapnumber The interval gap of the grid.
startnumber <optional>
0 Optional starting offset for gap.
- Source:
- src/math/Math.js line 305
Returns:
The snapped value.
- Type
- number
-
trunc(value)
-
Truncates a number, removing any fractional part. Same as round-towards-zero.
Parameters:
Name Type Description valuenumber The value to truncate.
- Source:
- src/math/Math.js line 444
Returns:
The truncated value.
- Type
- number
-
within(a, b, tolerance)
-
Checks if two values are within the given tolerance of each other.
Parameters:
Name Type Description anumber The first number to check
bnumber The second number to check
tolerancenumber The tolerance. Anything equal to or less than this is considered within the range.
- Source:
- src/math/Math.js line 1189
- See:
-
- Phaser.Math.fuzzyEqual
Returns:
True if a is <= tolerance of b.
- Type
- boolean
-
wrap(value, min, max)
-
Ensures that the value always stays between min and max, by wrapping the value around.
If
maxis not larger thanminthe result is 0.Parameters:
Name Type Description valuenumber The value to wrap.
minnumber The minimum the value is allowed to be.
maxnumber The maximum the value is allowed to be, should be larger than
min.- Source:
- src/math/Math.js line 650
Returns:
The wrapped value.
- Type
- number
-
wrapAngle(angle [, radians])
-
Keeps an angle value between -180 and +180; or -PI and PI if radians.
Parameters:
Name Type Argument Default Description anglenumber The angle value to wrap
radiansboolean <optional>
false Set to
trueif the angle is given in radians, otherwise degrees is expected.- Source:
- src/math/Math.js line 848
Returns:
The new angle value; will be the same as the input angle if it was within bounds.
- Type
- number
-
wrapValue(value, amount, max)
-
Adds value to amount and ensures that the result always stays between 0 and max, by wrapping the value around.
Values must be positive integers, and are passed through Math.abs. See Phaser.Math#wrap for an alternative.
Parameters:
Name Type Description valuenumber The value to add the amount to.
amountnumber The amount to add to the value.
maxnumber The maximum the value is allowed to be.
- Source:
- src/math/Math.js line 680
Returns:
The wrapped value.
- Type
- number