Class: Math

Phaser. Math

new Math()

A collection of mathematical methods.

Source:

Methods

angleBetween(x1, y1, x2, y2) → {number}

Find the angle of a segment from (x1, y1) -> (x2, y2).

Parameters:
Name Type Description
x1 number
y1 number
x2 number
y2 number
Source:
Returns:
Type
number

angleBetweenPoints(point1, point2) → {number}

Find the angle of a segment from (point1.x, point1.y) -> (point2.x, point2.y).

Parameters:
Name Type Description
point1 Phaser.Point
point2 Phaser.Point
Source:
Returns:
Type
number

angleLimit(angle, min, max) → {number}

Keeps an angle value between the given min and max values.

Parameters:
Name Type Description
angle number

The angle value to check. Must be between -180 and +180.

min number

The minimum angle that is allowed (must be -180 or greater).

max number

The maximum angle that is allowed (must be 180 or less).

Source:
Returns:

The new angle value, returns the same as the input angle if it was within bounds

Type
number

average() → {number}

Averages all values passed to the function and returns the result. You can pass as many parameters as you like.

Source:
Returns:

The average of all given values.

Type
number

bernstein(n, i) → {number}

Parameters:
Name Type Description
n number
i number
Source:
Returns:
Type
number

bezierInterpolation(v, k) → {number}

A Bezier Interpolation Method, mostly used by Phaser.Tween.

Parameters:
Name Type Description
v number
k number
Source:
Returns:
Type
number

catmullRom(p0, p1, p2, p3, t) → {number}

Description.

Parameters:
Name Type Description
p0 number
p1 number
p2 number
p3 number
t number
Source:
Returns:
Type
number

catmullRomInterpolation(v, k) → {number}

A Catmull Rom Interpolation Method, mostly used by Phaser.Tween.

Parameters:
Name Type Description
v number
k number
Source:
Returns:
Type
number

ceil(value) → {number}

Round up to the next whole number. E.g. ceil(1.3) == 2, and ceil(-2.3) == -3.

Parameters:
Name Type Description
value number

Any number.

Source:
Returns:

The rounded value of that number.

Type
number

ceilTo(value, place, base) → {number}

Parameters:
Name Type Description
value number

The value to round.

place number

The place to round to.

base number

The base to round in... default is 10 for decimal.

Source:
Returns:
Type
number

chanceRoll(chance) → {boolean}

Generate a random bool result based on the chance value.

<p> Returns true or false based on the chance value (default 50%). For example if you wanted a player to have a 30% chance of getting a bonus, call chanceRoll(30) - true means the chance passed, false means it failed. </p>

Parameters:
Name Type Description
chance number

The chance of receiving the value. A number between 0 and 100 (effectively 0% to 100%).

Source:
Returns:

True if the roll passed, or false otherwise.

Type
boolean

clamp(x, a, b) → {number}

Force a value within the boundaries of two values. Clamp value to range <a, b>

Parameters:
Name Type Description
x number
a number
b number
Source:
Returns:
Type
number

clampBottom(x, a) → {number}

Clamp value to range <a, inf).

Parameters:
Name Type Description
x number
a number
Source:
Returns:
Type
number

degToRad() → {function}

Convert degrees to radians.

Source:
Returns:
Type
function

difference(a, b) → {number}

Parameters:
Name Type Description
a number
b number
Source:
Returns:
Type
number

distance(x1, y1, x2, y2) → {number}

Returns the distance between the two given set of coordinates.

Parameters:
Name Type Description
x1 number
y1 number
x2 number
y2 number
Source:
Returns:

The distance between the two sets of coordinates.

Type
number

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
x1 number
y1 number
x2 number
y2 number
pow number <optional>
2
Source:
Returns:

The distance between the two sets of coordinates.

Type
number

distanceRounded(x1, y1, x2, y2) → {number}

Returns the rounded distance between the two given set of coordinates.

Parameters:
Name Type Description
x1 number
y1 number
x2 number
y2 number
Source:
Returns:

The distance between this Point object and the destination Point object.

Type
number

floor(Value) → {number}

Round down to the next whole number. E.g. floor(1.7) == 1, and floor(-2.7) == -2.

Parameters:
Name Type Description
Value number

Any number.

Source:
Returns:

The rounded value of that number.

Type
number

floorTo(value, place, base) → {number}

Parameters:
Name Type Description
value number

The value to round.

place number

The place to round to.

base number

The base to round in... default is 10 for decimal.

Source:
Returns:
Type
number

fuzzyCeil(val, epsilon) → {boolean}

Parameters:
Name Type Description
val number
epsilon number
Source:
Returns:

ceiling(val-ε)

Type
boolean

fuzzyEqual(a, b, epsilon) → {boolean}

Two number are fuzzyEqual if their difference is less than ε.

Parameters:
Name Type Description
a number
b number
epsilon number
Source:
Returns:

True if |a-b|<ε

Type
boolean

fuzzyFloor(val, epsilon) → {boolean}

Parameters:
Name Type Description
val number
epsilon number
Source:
Returns:

floor(val-ε)

Type
boolean

fuzzyGreaterThan(a, b, epsilon) → {boolean}

a is fuzzyGreaterThan b if it is more than b - ε.

Parameters:
Name Type Description
a number
b number
epsilon number
Source:
Returns:

True if a>b+ε

Type
boolean

fuzzyLessThan(a, b, epsilon) → {boolean}

a is fuzzyLessThan b if it is less than b + ε.

Parameters:
Name Type Description
a number
b number
epsilon number
Source:
Returns:

True if a<b+ε

Type
boolean

getRandom(objects, startIndex, length) → {object}

Fetch a random entry from the given array. Will return null if random selection is missing, or array has no entries.

Parameters:
Name Type Description
objects array

An array of objects.

startIndex number

Optional offset off the front of the array. Default value is 0, or the beginning of the array.

length number

Optional restriction on the number of values you want to randomly select from.

Source:
Returns:

The random object that was selected.

Type
object

interpolateAngles(a1, a2, weight, radians, ease) → {number}

Interpolate across the shortest arc between two angles.

Parameters:
Name Type Description
a1 number

Description.

a2 number

Description.

weight number

Description.

radians boolean

True if angle sizes are expressed in radians.

ease Description

Description.

Source:
Returns:
Type
number

interpolateFloat(a, b, weight) → {number}

A one dimensional linear interpolation of a value.

Parameters:
Name Type Description
a number
b number
weight number
Source:
Returns:
Type
number

isEven(n) → {boolean}

Returns true if the number given is even.

Parameters:
Name Type Description
n number

The number to check.

Source:
Returns:

True if the given number is even. False if the given number is odd.

Type
boolean

isOdd(n) → {boolean}

Returns true if the number given is odd.

Parameters:
Name Type Description
n number

The number to check.

Source:
Returns:

True if the given number is odd. False if the given number is even.

Type
boolean

Linear(p0, p1, t) → {number}

Description.

Parameters:
Name Type Description
p0 number
p1 number
t number
Source:
Returns:
Type
number

linearInterpolation(v, k) → {number}

A Linear Interpolation Method, mostly used by Phaser.Tween.

Parameters:
Name Type Description
v number
k number
Source:
Returns:
Type
number

mapLinear(x, a1, a2, b1, b2) → {number}

Linear mapping from range <a1, a2> to range <b1, b2>

Parameters:
Name Type Description
x number

the value to map

a1 number

first endpoint of the range <a1, a2>

a2 number

final endpoint of the range <a1, a2>

b1 number

first endpoint of the range <b1, b2>

b2 number

final endpoint of the range <b1, b2>

Source:
Returns:
Type
number

max() → {number}

Updated version of Math.max that can be passed either an array of numbers or the numbers as parameters.

Source:
Returns:

The largest value from those given.

Type
number

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
value number

The value to add the amount to.

amount number

The amount to add to the value.

max- number

The maximum the value is allowed to be.

Source:
Returns:
Type
number

maxProperty() → {number}

Updated version 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:
Returns:

The largest value from those given.

Type
number

min() → {number}

Updated version of Math.min that can be passed either an array of numbers or the numbers as parameters. See http://jsperf.com/math-s-min-max-vs-homemade/5

Source:
Returns:

The lowest value from those given.

Type
number

minProperty() → {number}

Updated version 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:
Returns:

The lowest value from those given.

Type
number

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
value number

The base value.

amount number

The amount to subtract from the base value.

min number

The minimum the value is allowed to be.

Source:
Returns:

The new value.

Type
number

nearestAngleBetween(a1, a2, radians) → {number}

Closest angle between two angles from a1 to a2 absolute value the return for exact angle

Parameters:
Name Type Description
a1 number
a2 number
radians boolean

True if angle sizes are expressed in radians.

Source:
Returns:
Type
number

normalizeAngle(angleRad) → {number}

Normalizes an angle to the [0,2pi) range.

Parameters:
Name Type Description
angleRad number

The angle to normalize, in radians.

Source:
Returns:

Returns the angle, fit within the [0,2pi] range, in radians.

Type
number

normalizeLatitude(lat) → {number}

Normalizes a latitude to the [-90,90] range. Latitudes above 90 or below -90 are capped, not wrapped.

Parameters:
Name Type Description
lat number

The latitude to normalize, in degrees.

Source:
Returns:

Returns the latitude, fit within the [-90,90] range.

Type
number

normalizeLongitude(lng) → {number}

Normalizes a longitude to the [-180,180] range. Longitudes above 180 or below -180 are wrapped.

Parameters:
Name Type Description
lng number

The longitude to normalize, in degrees.

Source:
Returns:

Returns the longitude, fit within the [-180,180] range.

Type
number

numberArray(min, max) → {array}

Returns an Array containing the numbers from min to max (inclusive).

Parameters:
Name Type Description
min number

The minimum value the array starts with.

max number

The maximum value the array contains.

Source:
Returns:

The array of number values.

Type
array

PI2()

= 2 π

Source:

radToDeg() → {function}

Convert degrees to radians.

Source:
Returns:
Type
function

randomSign() → {number}

Randomly returns either a 1 or -1.

Source:
Returns:

1 or -1

Type
number

removeRandom(objects, startIndex, length) → {object}

Removes a random object from the given array and returns it. Will return null if random selection is missing, or array has no entries.

Parameters:
Name Type Description
objects array

An array of objects.

startIndex number

Optional offset off the front of the array. Default value is 0, or the beginning of the array.

length number

Optional restriction on the number of values you want to randomly select from.

Source:
Returns:

The random object that was removed.

Type
object

reverseAngle(angleRad) → {number}

Reverses an angle.

Parameters:
Name Type Description
angleRad number

The angle to reverse, in radians.

Source:
Returns:

Returns the reverse angle, in radians.

Type
number

roundTo(value, place, base) → {number}

Round to some place comparative to a 'base', default is 10 for decimal place.

'place' is represented by the power applied to 'base' to 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.10111

Note 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 Description
value number

The value to round.

place number

The place to round to.

base number

The base to round in... default is 10 for decimal.

Source:
Returns:
Type
number

shear(n) → {number}

Parameters:
Name Type Description
n number
Source:
Returns:

n mod 1

Type
number

shift(stack) → {any}

Removes the top element from the stack and re-inserts it onto the bottom, then returns it. The original stack is modified in the process. This effectively moves the position of the data from the start to the end of the table.

Parameters:
Name Type Description
stack array

The array to shift.

Source:
Returns:

The shifted value.

Type
any

shuffleArray(array) → {array}

Shuffles the data in the given array into a new order

Parameters:
Name Type Description
array array

The array to shuffle

Source:
Returns:

The array

Type
array

sign(x) → {number}

A value representing the sign of the value. -1 for negative, +1 for positive, 0 if value is 0

Parameters:
Name Type Description
x number
Source:
Returns:
Type
number

sinCosGenerator(length, sinAmplitude, cosAmplitude, frequency) → {Array}

Generate a sine and cosine table simultaneously and extremely quickly. Based on research by Franky of scene.at

<p> The parameters allow you to specify the length, amplitude and frequency of the wave. Once you have called this function you should get the results via getSinTable() and getCosTable(). This generator is fast enough to be used in real-time. </p>

Parameters:
Name Type Description
length number

The length of the wave

sinAmplitude number

The amplitude to apply to the sine table (default 1.0) if you need values between say -+ 125 then give 125 as the value

cosAmplitude number

The amplitude to apply to the cosine table (default 1.0) if you need values between say -+ 125 then give 125 as the value

frequency number

The frequency of the sine and cosine table data

Source:
Returns:

Returns the sine table

Type
Array

smootherstep(x, min, max) → {number}

Smootherstep function as detailed at http://en.wikipedia.org/wiki/Smoothstep

Parameters:
Name Type Description
x number
min number
max number
Source:
Returns:
Type
number

smoothstep(x, min, max) → {number}

Smoothstep function as detailed at http://en.wikipedia.org/wiki/Smoothstep

Parameters:
Name Type Description
x number
min number
max number
Source:
Returns:
Type
number

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 Description
input number

The value to snap.

gap number

The interval gap of the grid.

start number <optional>

Optional starting offset for gap.

Source:
Returns:
Type
number

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 Description
input number

The value to snap.

gap number

The interval gap of the grid.

start number <optional>

Optional starting offset for gap.

Source:
Returns:
Type
number

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 Description
input number

The value to snap.

gap number

The interval gap of the grid.

start number <optional>

Optional starting offset for gap.

Source:
Returns:
Type
number

snapToInArray(input, arr, sort) → {number}

Snaps a value to the nearest value in an array.

Parameters:
Name Type Description
input number
arr array
sort boolean

True if the array needs to be sorted.

Source:
Returns:
Type
number

truncate(n) → {number}

Parameters:
Name Type Description
n number
Source:
Returns:
Type
number

within(a, b, tolerance) → {boolean}

Checks if two values are within the given tolerance of each other.

Parameters:
Name Type Description
a number

The first number to check

b number

The second number to check

tolerance number

The tolerance. Anything equal to or less than this is considered within the range.

Source:
Returns:

True if a is <= tolerance of b.

Type
boolean

wrap(value, min, max) → {number}

Ensures that the value always stays between min and max, by wrapping the value around. max should be larger than min, or the function will return 0.

Parameters:
Name Type Description
value number

The value to wrap.

min number

The minimum the value is allowed to be.

max number

The maximum the value is allowed to be.

Source:
Returns:

The wrapped value.

Type
number

wrapAngle(angle, radians) → {number}

Keeps an angle value between -180 and +180<br> Should be called whenever the angle is updated on the Sprite to stop it from going insane.

Parameters:
Name Type Description
angle number

The angle value to check

radians boolean

True if angle sizes are expressed in radians.

Source:
Returns:

The new angle value, returns the same as the input angle if it was within bounds.

Type
number

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.

Parameters:
Name Type Description
value number

The value to add the amount to.

amount number

The amount to add to the value.

max number

The maximum the value is allowed to be.

Source:
Returns:

The wrapped value.

Type
number
Phaser Copyright © 2012-2014 Photon Storm Ltd.
Documentation generated by JSDoc 3.3.0-dev on Fri Mar 28 2014 01:41:31 GMT-0000 (GMT) using the DocStrap template.