new Math()
A collection of useful mathematical functions.
These are normally accessed through game.math.
- Source - math/Math.js, line 17
- See:
Members
-
<static> DEG_TO_RAD
-
Degrees to Radians factor.
- Source - math/Math.js, line 30
Properties:
Name Type Description Phaser.Math#DEG_TO_RADnumber -
<static> PI2
-
Twice PI.
- Default Value:
- ~6.283
- Source - math/Math.js, line 24
Properties:
Name Type Description Phaser.Math#PI2number -
<static> RAD_TO_DEG
-
Degrees to Radians factor.
- Source - math/Math.js, line 36
Properties:
Name Type Description Phaser.Math#RAD_TO_DEGnumber
Methods
-
angleBetween(x1, y1, x2, y2) → {number}
-
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.
Returns:
number -The angle, in radians.
- Source - math/Math.js, line 534
-
angleBetweenPoints(point1, point2) → {number}
-
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.
Returns:
number -The angle between the two points, in radians.
- Source - math/Math.js, line 569
-
angleBetweenPointsY(point1, point2) → {number}
-
Find the angle of a segment from (point1.x, point1.y) -> (point2.x, point2.y).
Parameters:
Name Type Description point1Phaser.Point point2Phaser.Point Returns:
number -The angle, in radians.
- Source - math/Math.js, line 583
-
angleBetweenY(x1, y1, x2, y2) → {number}
-
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.
Returns:
number -The angle, in radians.
- Source - math/Math.js, line 550
-
average() → {number}
-
Averages all values passed to the function and returns the result.
Returns:
number -The average of all given values.
- Source - math/Math.js, line 253
-
<internal> bernstein(n, i) → {number}
-
Parameters:
Name Type Description nnumber inumber Returns:
number -- Internal:
- This member is internal (protected) and may be modified or removed in the future.
- Source - math/Math.js, line 976
-
between(min, max) → {number}
-
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.
Returns:
number -An integer between min (inclusive) and max (inclusive).
- Source - math/Math.js, line 137
-
bezierInterpolation(v, k) → {number}
-
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.
Returns:
number -The interpolated value
- Source - math/Math.js, line 899
-
<internal> catmullRom(p0, p1, p2, p3, t) → {number}
-
Calculates a catmum rom value.
Parameters:
Name Type Description p0number p1number p2number p3number tnumber Returns:
number -- Internal:
- This member is internal (protected) and may be modified or removed in the future.
- Source - math/Math.js, line 1012
-
catmullRomInterpolation(v, k) → {number}
-
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.
Returns:
number -The interpolated value
- Source - math/Math.js, line 921
-
ceilTo(value, place, base) → {number}
-
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.
Returns:
number -The rounded value.
- Source - math/Math.js, line 432
-
clamp(v, min, max) → {number}
-
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.
Returns:
number -The clamped value.
- Source - math/Math.js, line 1172
-
clampBottom(x, a) → {number}
-
Clamp
xto the range[a, Infinity).
Roughly the same asMath.max(x, a), except for NaN handling.Parameters:
Name Type Description xnumber anumber Returns:
number -- Source - math/Math.js, line 1198
-
degToRad(degrees) → {number}
-
Convert degrees to radians.
Parameters:
Name Type Description degreesnumber Angle in degrees.
Returns:
number -Angle in radians.
- Source - math/Math.js, line 38
-
difference(a, b) → {number}
-
The absolute difference between two values.
Parameters:
Name Type Description anumber The first value to check.
bnumber The second value to check.
Returns:
number -The absolute difference between the two values.
- Source - math/Math.js, line 1032
-
distance(x1, y1, x2, y2) → {number}
-
Returns the euclidian distance between the two given set of coordinates.
Parameters:
Name Type Description x1number y1number x2number y2number Returns:
number -The distance between the two sets of coordinates.
- Source - math/Math.js, line 1114
-
distancePow(x1, y1, x2, y2, pow) → {number}
-
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 Returns:
number -The distance between the two sets of coordinates.
- Source - math/Math.js, line 1153
-
distanceSq(x1, y1, x2, y2) → {number}
-
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 Returns:
number -The distance squared between the two sets of coordinates.
- Source - math/Math.js, line 1133
-
factorial(value) → {number}
-
Parameters:
Name Type Description valuenumber the number you want to evaluate
Returns:
number -- Source - math/Math.js, line 989
-
floorTo(value, place, base) → {number}
-
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.
Returns:
number -The rounded value.
- Source - math/Math.js, line 411
-
fuzzyCeil(val, epsilon) → {number}
-
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)
Returns:
number -ceiling(val-epsilon)
- Source - math/Math.js, line 221
-
fuzzyEqual(a, b, epsilon) → {boolean}
-
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)
Returns:
boolean -True if |a-b|<epsilon
- Source - math/Math.js, line 170
-
fuzzyFloor(val, epsilon) → {number}
-
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)
Returns:
number -floor(val+epsilon)
- Source - math/Math.js, line 237
-
fuzzyGreaterThan(a, b, epsilon) → {boolean}
-
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)
Returns:
boolean -True if a>b+epsilon
- Source - math/Math.js, line 204
-
fuzzyLessThan(a, b, epsilon) → {boolean}
-
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)
Returns:
boolean -True if a<b+epsilon
- Source - math/Math.js, line 187
-
getNextPowerOfTwo(value) → {number}
-
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.
Returns:
number -The closest number that is a power of two.
- Source - math/Math.js, line 64
-
getShortestAngle(angle1, angle2) → {number}
-
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.
Returns:
number -The shortest angle, in degrees. If greater than zero it's a counter-clockwise rotation.
- Source - math/Math.js, line 504
-
hypot(a, b) → {number}
-
Returns the length of the hypotenuse connecting two segments of given lengths.
Parameters:
Name Type Description anumber bnumber Returns:
number -The length of the hypotenuse connecting the given lengths.
- Source - math/Math.js, line 1100
-
isEven(n) → {boolean}
-
Returns true if the number given is even.
Parameters:
Name Type Description ninteger The number to check.
Returns:
boolean -True if the given number is even. False if the given number is odd.
- Source - math/Math.js, line 719
-
isOdd(n) → {boolean}
-
Returns true if the number given is odd.
Parameters:
Name Type Description ninteger The number to check.
Returns:
boolean -True if the given number is odd. False if the given number is even.
- Source - math/Math.js, line 705
-
isPowerOfTwo(width, height) → {boolean}
-
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.
Returns:
boolean -True if the width and height are a power of two.
- Source - math/Math.js, line 93
-
linear(p0, p1, t) → {number}
-
Calculates a linear (interpolation) value over t.
Parameters:
Name Type Description p0number p1number tnumber A value between 0 and 1.
Returns:
number -- Source - math/Math.js, line 961
-
linearInterpolation(v, k) → {number}
-
A Linear 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.
Returns:
number -The interpolated value
- Source - math/Math.js, line 871
-
mapLinear(x, a1, a2, b1, b2) → {number}
-
Linear mapping from range
to range Parameters:
Name Type Description xnumber The value to map
a1number First endpoint of the range
a2number Final endpoint of the range
b1number First endpoint of the range
b2number Final endpoint of the range
Returns:
number -- Source - math/Math.js, line 1229
-
max() → {number}
-
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:
number -The largest value from those given.
- Source - math/Math.js, line 765
- See:
-
maxAdd(value, amount, max) → {number}
-
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.
Returns:
number -The new value.
- Source - math/Math.js, line 621
-
maxProperty() → {number}
-
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.Returns:
number -The largest value from those given.
- Source - math/Math.js, line 827
-
min() → {number}
-
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:
number -The lowest value from those given.
- Source - math/Math.js, line 733
- See:
-
minProperty() → {number}
-
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.Returns:
number -The lowest value from those given.
- Source - math/Math.js, line 797
-
minSub(value, amount, min) → {number}
-
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.
Returns:
number -The new value.
- Source - math/Math.js, line 636
-
normalizeAngle(angleRad) → {number}
-
Normalizes an angle to the [0,2pi) range.
Parameters:
Name Type Description angleRadnumber The angle to normalize, in radians.
Returns:
number -The angle, fit within the [0,2pi] range, in radians.
- Source - math/Math.js, line 608
-
percent(a, b, base) → {number}
-
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.
Returns:
number -The percentage a is of b, between 0 and 1.
- Source - math/Math.js, line 1297
-
radToDeg(radians) → {number}
-
Convert radians to degrees.
Parameters:
Name Type Description radiansnumber Angle in radians.
Returns:
number -Angle in degrees
- Source - math/Math.js, line 51
-
random(min, max) → {number}
-
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.
Returns:
number -A floating point number between min (inclusive) and max (exclusive).
- Source - math/Math.js, line 107
-
reverseAngle(angleRad) → {number}
-
Reverses an angle.
Parameters:
Name Type Description angleRadnumber The angle to reverse, in radians.
Returns:
number -The reverse angle, in radians.
- Source - math/Math.js, line 596
-
rotateToAngle(currentAngle, targetAngle, lerp) → {number}
-
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.
Returns:
number -The adjusted angle.
- Source - math/Math.js, line 453
-
roundAwayFromZero(value) → {integer}
-
Round to the next whole number away from zero.
Parameters:
Name Type Description valuenumber Any number.
Returns:
integer -The rounded value of that number.
- Source - math/Math.js, line 1046
-
roundTo(value, place, base) → {number}
-
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.
Returns:
number -The rounded value.
- Source - math/Math.js, line 365
-
shear(n) → {number}
-
Parameters:
Name Type Description nnumber Returns:
number -n mod 1
- Source - math/Math.js, line 274
-
sign(x) → {integer}
-
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 Returns:
integer -An integer in {-1, 0, 1}
- Source - math/Math.js, line 1282
-
sinCosGenerator(length, sinAmplitude, cosAmplitude, frequency) → {Object}
-
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.atParameters:
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
Returns:
Object -Returns the table data.
- Source - math/Math.js, line 1060
-
smootherstep(x, min, max) → {float}
-
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.
Returns:
float -A value between 0 and 1.
- Source - math/Math.js, line 1265
-
smoothstep(x, min, max) → {float}
-
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.
Returns:
float -A value between 0 and 1.
- Source - math/Math.js, line 1246
-
snapTo(input, gap, start) → {number}
-
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.
Returns:
number -The snapped value.
- Source - math/Math.js, line 285
-
snapToCeil(input, gap, start) → {number}
-
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.
Returns:
number -The snapped value.
- Source - math/Math.js, line 338
-
snapToFloor(input, gap, start) → {number}
-
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.
Returns:
number -The snapped value.
- Source - math/Math.js, line 311
-
within(a, b, tolerance) → {boolean}
-
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.
Returns:
boolean -True if a is <= tolerance of b.
- Source - math/Math.js, line 1213
- See:
-
- Phaser.Math.fuzzyEqual
-
wrap(value, min, max) → {number}
-
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.Returns:
number -The wrapped value.
- Source - math/Math.js, line 651
-
wrapAngle(angle, radians) → {number}
-
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.Returns:
number -The new angle value; will be the same as the input angle if it was within bounds.
- Source - math/Math.js, line 857
-
wrapValue(value, amount, max) → {number}
-
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.
Returns:
number -The wrapped value.
- Source - math/Math.js, line 682
