mirror of
https://github.com/photonstorm/phaser
synced 2024-11-28 07:31:11 +00:00
Docs updates.
This commit is contained in:
parent
dfd9203e0a
commit
c34c7bae4e
1 changed files with 87 additions and 41 deletions
128
src/math/Math.js
128
src/math/Math.js
|
@ -147,7 +147,9 @@ Phaser.Math = {
|
||||||
* @return {number} n mod 1
|
* @return {number} n mod 1
|
||||||
*/
|
*/
|
||||||
shear: function (n) {
|
shear: function (n) {
|
||||||
|
|
||||||
return n % 1;
|
return n % 1;
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -158,8 +160,8 @@ Phaser.Math = {
|
||||||
* @method Phaser.Math#snapTo
|
* @method Phaser.Math#snapTo
|
||||||
* @param {number} input - The value to snap.
|
* @param {number} input - The value to snap.
|
||||||
* @param {number} gap - The interval gap of the grid.
|
* @param {number} gap - The interval gap of the grid.
|
||||||
* @param {number} [start] - Optional starting offset for gap.
|
* @param {number} [start=0] - Optional starting offset for gap.
|
||||||
* @return {number}
|
* @return {number} The snapped value.
|
||||||
*/
|
*/
|
||||||
snapTo: function (input, gap, start) {
|
snapTo: function (input, gap, start) {
|
||||||
|
|
||||||
|
@ -185,8 +187,8 @@ Phaser.Math = {
|
||||||
* @method Phaser.Math#snapToFloor
|
* @method Phaser.Math#snapToFloor
|
||||||
* @param {number} input - The value to snap.
|
* @param {number} input - The value to snap.
|
||||||
* @param {number} gap - The interval gap of the grid.
|
* @param {number} gap - The interval gap of the grid.
|
||||||
* @param {number} [start] - Optional starting offset for gap.
|
* @param {number} [start=0] - Optional starting offset for gap.
|
||||||
* @return {number}
|
* @return {number} The snapped value.
|
||||||
*/
|
*/
|
||||||
snapToFloor: function (input, gap, start) {
|
snapToFloor: function (input, gap, start) {
|
||||||
|
|
||||||
|
@ -212,8 +214,8 @@ Phaser.Math = {
|
||||||
* @method Phaser.Math#snapToCeil
|
* @method Phaser.Math#snapToCeil
|
||||||
* @param {number} input - The value to snap.
|
* @param {number} input - The value to snap.
|
||||||
* @param {number} gap - The interval gap of the grid.
|
* @param {number} gap - The interval gap of the grid.
|
||||||
* @param {number} [start] - Optional starting offset for gap.
|
* @param {number} [start=0] - Optional starting offset for gap.
|
||||||
* @return {number}
|
* @return {number} The snapped value.
|
||||||
*/
|
*/
|
||||||
snapToCeil: function (input, gap, start) {
|
snapToCeil: function (input, gap, start) {
|
||||||
|
|
||||||
|
@ -261,9 +263,9 @@ Phaser.Math = {
|
||||||
*
|
*
|
||||||
* @method Phaser.Math#roundTo
|
* @method Phaser.Math#roundTo
|
||||||
* @param {number} value - The value to round.
|
* @param {number} value - The value to round.
|
||||||
* @param {number} place - The place to round to.
|
* @param {number} [place=0] - The place to round to.
|
||||||
* @param {number} base - The base to round in... default is 10 for decimal.
|
* @param {number} [base=10] - The base to round in. Default is 10 for decimal.
|
||||||
* @return {number}
|
* @return {number} The rounded value.
|
||||||
*/
|
*/
|
||||||
roundTo: function (value, place, base) {
|
roundTo: function (value, place, base) {
|
||||||
|
|
||||||
|
@ -277,11 +279,14 @@ Phaser.Math = {
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
* @method Phaser.Math#floorTo
|
* @method Phaser.Math#floorTo
|
||||||
* @param {number} value - The value to round.
|
* @param {number} value - The value to round.
|
||||||
* @param {number} place - The place to round to.
|
* @param {number} [place=0] - The place to round to.
|
||||||
* @param {number} base - The base to round in... default is 10 for decimal.
|
* @param {number} [base=10] - The base to round in. Default is 10 for decimal.
|
||||||
* @return {number}
|
* @return {number} The rounded value.
|
||||||
*/
|
*/
|
||||||
floorTo: function (value, place, base) {
|
floorTo: function (value, place, base) {
|
||||||
|
|
||||||
|
@ -295,11 +300,14 @@ Phaser.Math = {
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Ceils 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.
|
||||||
|
*
|
||||||
* @method Phaser.Math#ceilTo
|
* @method Phaser.Math#ceilTo
|
||||||
* @param {number} value - The value to round.
|
* @param {number} value - The value to round.
|
||||||
* @param {number} place - The place to round to.
|
* @param {number} [place=0] - The place to round to.
|
||||||
* @param {number} base - The base to round in... default is 10 for decimal.
|
* @param {number} [base=10] - The base to round in. Default is 10 for decimal.
|
||||||
* @return {number}
|
* @return {number} The rounded value.
|
||||||
*/
|
*/
|
||||||
ceilTo: function (value, place, base) {
|
ceilTo: function (value, place, base) {
|
||||||
|
|
||||||
|
@ -314,42 +322,51 @@ Phaser.Math = {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find the angle of a segment from (x1, y1) -> (x2, y2).
|
* Find the angle of a segment from (x1, y1) -> (x2, y2).
|
||||||
|
*
|
||||||
* @method Phaser.Math#angleBetween
|
* @method Phaser.Math#angleBetween
|
||||||
* @param {number} x1
|
* @param {number} x1 - The x coordinate of the first value.
|
||||||
* @param {number} y1
|
* @param {number} y1 - The y coordinate of the first value.
|
||||||
* @param {number} x2
|
* @param {number} x2 - The x coordinate of the second value.
|
||||||
* @param {number} y2
|
* @param {number} y2 - The y coordinate of the second value.
|
||||||
* @return {number} The angle, in radians.
|
* @return {number} The angle, in radians.
|
||||||
*/
|
*/
|
||||||
angleBetween: function (x1, y1, x2, y2) {
|
angleBetween: function (x1, y1, x2, y2) {
|
||||||
|
|
||||||
return Math.atan2(y2 - y1, x2 - x1);
|
return Math.atan2(y2 - y1, x2 - x1);
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find the angle of a segment from (x1, y1) -> (x2, y2).
|
* Find the angle of a segment from (x1, y1) -> (x2, y2).
|
||||||
* Note that the difference between this method and Math.angleBetween is that this assumes the y coordinate travels
|
*
|
||||||
|
* The difference between this method and Math.angleBetween is that this assumes the y coordinate travels
|
||||||
* down the screen.
|
* down the screen.
|
||||||
*
|
*
|
||||||
* @method Phaser.Math#angleBetweenY
|
* @method Phaser.Math#angleBetweenY
|
||||||
* @param {number} x1
|
* @param {number} x1 - The x coordinate of the first value.
|
||||||
* @param {number} y1
|
* @param {number} y1 - The y coordinate of the first value.
|
||||||
* @param {number} x2
|
* @param {number} x2 - The x coordinate of the second value.
|
||||||
* @param {number} y2
|
* @param {number} y2 - The y coordinate of the second value.
|
||||||
* @return {number} The angle, in radians.
|
* @return {number} The angle, in radians.
|
||||||
*/
|
*/
|
||||||
angleBetweenY: function (x1, y1, x2, y2) {
|
angleBetweenY: function (x1, y1, x2, y2) {
|
||||||
|
|
||||||
return Math.atan2(x2 - x1, y2 - y1);
|
return Math.atan2(x2 - x1, y2 - y1);
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find the angle of a segment from (point1.x, point1.y) -> (point2.x, point2.y).
|
* Find the angle of a segment from (point1.x, point1.y) -> (point2.x, point2.y).
|
||||||
|
*
|
||||||
* @method Phaser.Math#angleBetweenPoints
|
* @method Phaser.Math#angleBetweenPoints
|
||||||
* @param {Phaser.Point} point1
|
* @param {Phaser.Point} point1 - The first point.
|
||||||
* @param {Phaser.Point} point2
|
* @param {Phaser.Point} point2 - The second point.
|
||||||
* @return {number} The angle, in radians.
|
* @return {number} The angle between the two points, in radians.
|
||||||
*/
|
*/
|
||||||
angleBetweenPoints: function (point1, point2) {
|
angleBetweenPoints: function (point1, point2) {
|
||||||
|
|
||||||
return Math.atan2(point2.y - point1.y, point2.x - point1.x);
|
return Math.atan2(point2.y - point1.y, point2.x - point1.x);
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -360,24 +377,28 @@ Phaser.Math = {
|
||||||
* @return {number} The angle, in radians.
|
* @return {number} The angle, in radians.
|
||||||
*/
|
*/
|
||||||
angleBetweenPointsY: function (point1, point2) {
|
angleBetweenPointsY: function (point1, point2) {
|
||||||
|
|
||||||
return Math.atan2(point2.x - point1.x, point2.y - point1.y);
|
return Math.atan2(point2.x - point1.x, point2.y - point1.y);
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reverses an angle.
|
* Reverses an angle.
|
||||||
* @method Phaser.Math#reverseAngle
|
* @method Phaser.Math#reverseAngle
|
||||||
* @param {number} angleRad - The angle to reverse, in radians.
|
* @param {number} angleRad - The angle to reverse, in radians.
|
||||||
* @return {number} Returns the reverse angle, in radians.
|
* @return {number} The reverse angle, in radians.
|
||||||
*/
|
*/
|
||||||
reverseAngle: function (angleRad) {
|
reverseAngle: function (angleRad) {
|
||||||
|
|
||||||
return this.normalizeAngle(angleRad + Math.PI, true);
|
return this.normalizeAngle(angleRad + Math.PI, true);
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Normalizes an angle to the [0,2pi) range.
|
* Normalizes an angle to the [0,2pi) range.
|
||||||
* @method Phaser.Math#normalizeAngle
|
* @method Phaser.Math#normalizeAngle
|
||||||
* @param {number} angleRad - The angle to normalize, in radians.
|
* @param {number} angleRad - The angle to normalize, in radians.
|
||||||
* @return {number} Returns the angle, fit within the [0,2pi] range, in radians.
|
* @return {number} The angle, fit within the [0,2pi] range, in radians.
|
||||||
*/
|
*/
|
||||||
normalizeAngle: function (angleRad) {
|
normalizeAngle: function (angleRad) {
|
||||||
|
|
||||||
|
@ -393,10 +414,12 @@ Phaser.Math = {
|
||||||
* @param {number} value - The value to add the amount to.
|
* @param {number} value - The value to add the amount to.
|
||||||
* @param {number} amount - The amount to add to the value.
|
* @param {number} amount - The amount to add to the value.
|
||||||
* @param {number} max - The maximum the value is allowed to be.
|
* @param {number} max - The maximum the value is allowed to be.
|
||||||
* @return {number}
|
* @return {number} The new value.
|
||||||
*/
|
*/
|
||||||
maxAdd: function (value, amount, max) {
|
maxAdd: function (value, amount, max) {
|
||||||
|
|
||||||
return Math.min(value + amount, max);
|
return Math.min(value + amount, max);
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -409,7 +432,9 @@ Phaser.Math = {
|
||||||
* @return {number} The new value.
|
* @return {number} The new value.
|
||||||
*/
|
*/
|
||||||
minSub: function (value, amount, min) {
|
minSub: function (value, amount, min) {
|
||||||
|
|
||||||
return Math.max(value - amount, min);
|
return Math.max(value - amount, min);
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -474,8 +499,10 @@ Phaser.Math = {
|
||||||
* @return {boolean} True if the given number is odd. False if the given number is even.
|
* @return {boolean} True if the given number is odd. False if the given number is even.
|
||||||
*/
|
*/
|
||||||
isOdd: function (n) {
|
isOdd: function (n) {
|
||||||
|
|
||||||
// Does not work with extremely large values
|
// Does not work with extremely large values
|
||||||
return !!(n & 1);
|
return !!(n & 1);
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -486,8 +513,10 @@ Phaser.Math = {
|
||||||
* @return {boolean} True if the given number is even. False if the given number is odd.
|
* @return {boolean} True if the given number is even. False if the given number is odd.
|
||||||
*/
|
*/
|
||||||
isEven: function (n) {
|
isEven: function (n) {
|
||||||
|
|
||||||
// Does not work with extremely large values
|
// Does not work with extremely large values
|
||||||
return !(n & 1);
|
return !(n & 1);
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -724,11 +753,13 @@ Phaser.Math = {
|
||||||
* @method Phaser.Math#linear
|
* @method Phaser.Math#linear
|
||||||
* @param {number} p0
|
* @param {number} p0
|
||||||
* @param {number} p1
|
* @param {number} p1
|
||||||
* @param {number} t
|
* @param {number} t - A value between 0 and 1.
|
||||||
* @return {number}
|
* @return {number}
|
||||||
*/
|
*/
|
||||||
linear: function (p0, p1, t) {
|
linear: function (p0, p1, t) {
|
||||||
|
|
||||||
return (p1 - p0) * t + p0;
|
return (p1 - p0) * t + p0;
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -739,7 +770,9 @@ Phaser.Math = {
|
||||||
* @return {number}
|
* @return {number}
|
||||||
*/
|
*/
|
||||||
bernstein: function (n, i) {
|
bernstein: function (n, i) {
|
||||||
|
|
||||||
return this.factorial(n) / this.factorial(i) / this.factorial(n - i);
|
return this.factorial(n) / this.factorial(i) / this.factorial(n - i);
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -747,7 +780,7 @@ Phaser.Math = {
|
||||||
* @param {number} value - the number you want to evaluate
|
* @param {number} value - the number you want to evaluate
|
||||||
* @return {number}
|
* @return {number}
|
||||||
*/
|
*/
|
||||||
factorial : function( value ){
|
factorial: function (value) {
|
||||||
|
|
||||||
if (value === 0)
|
if (value === 0)
|
||||||
{
|
{
|
||||||
|
@ -786,15 +819,17 @@ Phaser.Math = {
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The (absolute) difference between two values.
|
* The absolute difference between two values.
|
||||||
*
|
*
|
||||||
* @method Phaser.Math#difference
|
* @method Phaser.Math#difference
|
||||||
* @param {number} a
|
* @param {number} a - The first value to check.
|
||||||
* @param {number} b
|
* @param {number} b - The second value to check.
|
||||||
* @return {number}
|
* @return {number} The absolute difference between the two values.
|
||||||
*/
|
*/
|
||||||
difference: function (a, b) {
|
difference: function (a, b) {
|
||||||
|
|
||||||
return Math.abs(a - b);
|
return Math.abs(a - b);
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -945,7 +980,9 @@ Phaser.Math = {
|
||||||
* @return {number}
|
* @return {number}
|
||||||
*/
|
*/
|
||||||
clampBottom: function (x, a) {
|
clampBottom: function (x, a) {
|
||||||
|
|
||||||
return x < a ? a : x;
|
return x < a ? a : x;
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -959,22 +996,26 @@ Phaser.Math = {
|
||||||
* @see {@link Phaser.Math.fuzzyEqual}
|
* @see {@link Phaser.Math.fuzzyEqual}
|
||||||
*/
|
*/
|
||||||
within: function (a, b, tolerance) {
|
within: function (a, b, tolerance) {
|
||||||
|
|
||||||
return (Math.abs(a - b) <= tolerance);
|
return (Math.abs(a - b) <= tolerance);
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Linear mapping from range <a1, a2> to range <b1, b2>
|
* Linear mapping from range <a1, a2> to range <b1, b2>
|
||||||
*
|
*
|
||||||
* @method Phaser.Math#mapLinear
|
* @method Phaser.Math#mapLinear
|
||||||
* @param {number} x the value to map
|
* @param {number} x - The value to map
|
||||||
* @param {number} a1 first endpoint of the range <a1, a2>
|
* @param {number} a1 - First endpoint of the range <a1, a2>
|
||||||
* @param {number} a2 final endpoint of the range <a1, a2>
|
* @param {number} a2 - Final endpoint of the range <a1, a2>
|
||||||
* @param {number} b1 first endpoint of the range <b1, b2>
|
* @param {number} b1 - First endpoint of the range <b1, b2>
|
||||||
* @param {number} b2 final endpoint of the range <b1, b2>
|
* @param {number} b2 - Final endpoint of the range <b1, b2>
|
||||||
* @return {number}
|
* @return {number}
|
||||||
*/
|
*/
|
||||||
mapLinear: function (x, a1, a2, b1, b2) {
|
mapLinear: function (x, a1, a2, b1, b2) {
|
||||||
|
|
||||||
return b1 + ( x - a1 ) * ( b2 - b1 ) / ( a2 - a1 );
|
return b1 + ( x - a1 ) * ( b2 - b1 ) / ( a2 - a1 );
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1006,8 +1047,11 @@ Phaser.Math = {
|
||||||
* @return {float} A value between 0 and 1.
|
* @return {float} A value between 0 and 1.
|
||||||
*/
|
*/
|
||||||
smootherstep: function (x, min, max) {
|
smootherstep: function (x, min, max) {
|
||||||
|
|
||||||
x = Math.max(0, Math.min(1, (x - min) / (max - min)));
|
x = Math.max(0, Math.min(1, (x - min) / (max - min)));
|
||||||
|
|
||||||
return x * x * x * (x * (x * 6 - 15) + 10);
|
return x * x * x * (x * (x * 6 - 15) + 10);
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1020,7 +1064,9 @@ Phaser.Math = {
|
||||||
* @return {integer} An integer in {-1, 0, 1}
|
* @return {integer} An integer in {-1, 0, 1}
|
||||||
*/
|
*/
|
||||||
sign: function (x) {
|
sign: function (x) {
|
||||||
|
|
||||||
return ( x < 0 ) ? -1 : ( ( x > 0 ) ? 1 : 0 );
|
return ( x < 0 ) ? -1 : ( ( x > 0 ) ? 1 : 0 );
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue