mirror of
https://github.com/photonstorm/phaser
synced 2024-11-27 15:12:18 +00:00
added getMagnitude, setMagnitude, normalize and isZero methods to Point
This commit is contained in:
parent
01f64c6694
commit
903c2e73fa
1 changed files with 45 additions and 2 deletions
|
@ -215,9 +215,7 @@ Phaser.Point.prototype = {
|
|||
* @return {number} The distance between this Point object and the destination Point object.
|
||||
*/
|
||||
distance: function (dest, round) {
|
||||
|
||||
return Phaser.Point.distance(this, dest, round);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
|
@ -244,6 +242,51 @@ Phaser.Point.prototype = {
|
|||
return Phaser.Point.rotate(this, x, y, angle, asDegrees, distance);
|
||||
},
|
||||
|
||||
/**
|
||||
* Calculates the length of the vector
|
||||
* @method Phaser.Point#getMagnitude
|
||||
* @return {number} the length of the vector
|
||||
*/
|
||||
getMagnitude: function() {
|
||||
return Math.sqrt((this.x * this.x) + (this.y * this.y));
|
||||
},
|
||||
|
||||
/**
|
||||
* Alters the length of the vector without changing the direction
|
||||
* @method Phaser.Point#getMagnitude
|
||||
* @param {number} magnitude the desired magnitude of the resulting vector
|
||||
* @return {Phaser.Point} the modified original vector
|
||||
*/
|
||||
setMagnitude: function(magnitude) {
|
||||
return this.normalize().multiply(magnitude, magnitude);
|
||||
},
|
||||
|
||||
/**
|
||||
* Alters the vector so that its length is 1, but it retains the same direction
|
||||
* @method Phaser.Point#normalize
|
||||
* @return {Phaser.Point} the modified original vector
|
||||
*/
|
||||
normalize: function() {
|
||||
|
||||
if(!this.isZero()) {
|
||||
var m = this.getMagnitude();
|
||||
this.x /= m;
|
||||
this.y /= m;
|
||||
}
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Determine if this point is at 0,0
|
||||
* @method Phaser.Point#isZero
|
||||
* @return {boolean} True if this Point is 0,0, otherwise false
|
||||
*/
|
||||
isZero: function() {
|
||||
return (this.x === 0 && this.y === 0);
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns a string representation of this object.
|
||||
* @method Phaser.Point#toString
|
||||
|
|
Loading…
Reference in a new issue