From 557160c3c2645a6edba2a557d81840beca6067b1 Mon Sep 17 00:00:00 2001 From: TheJare Date: Sun, 22 Sep 2013 03:43:10 +0200 Subject: [PATCH] Fixes to math library --- src/math/Math.js | 48 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/src/math/Math.js b/src/math/Math.js index 1709e317..d9f46edc 100644 --- a/src/math/Math.js +++ b/src/math/Math.js @@ -244,8 +244,8 @@ Phaser.Math = { if (typeof radians === "undefined") { radians = true; } - var rd = (radians) ? GameMath.PI : 180; - return this.wrap(angle, rd, -rd); + var rd = (radians) ? Math.PI : 180; + return this.wrap(angle, -rd, rd); }, @@ -257,7 +257,7 @@ Phaser.Math = { if (typeof radians === "undefined") { radians = true; } - var rd = (radians) ? GameMath.PI : 180; + var rd = (radians) ? Math.PI : 180; a1 = this.normalizeAngle(a1, radians); a2 = this.normalizeAngle(a2, radians); @@ -387,15 +387,39 @@ Phaser.Math = { }, - /** - * 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

- * - * @param value The value to add the amount to - * @param amount The amount to add to the value - * @param max The maximum the value is allowed to be - * @return The wrapped value - */ + /** + * 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

+ * + * @param value The value to wrap + * @param min The minimum the value is allowed to be + * @param max The maximum the value is allowed to be + * @return The wrapped value + */ + wrap: function (value, min, max) { + + var range = max - min; + if (range <= 0) + { + return 0; + } + var result = (value - min) % range; + if (result < 0) + { + result += range; + } + return result + min; + }, + + /** + * 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

+ * + * @param value The value to add the amount to + * @param amount The amount to add to the value + * @param max The maximum the value is allowed to be + * @return The wrapped value + */ wrapValue: function (value, amount, max) { var diff;