2013-08-08 18:16:47 +00:00
|
|
|
/// <reference path="../_definitions.ts" />
|
2013-05-25 03:21:24 +00:00
|
|
|
/**
|
|
|
|
* Phaser - Vec2
|
|
|
|
*
|
2013-06-07 15:27:33 +00:00
|
|
|
* A Vector 2
|
2013-05-25 03:21:24 +00:00
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
var Phaser;
|
|
|
|
(function (Phaser) {
|
|
|
|
var Vec2 = (function () {
|
2013-05-25 03:21:24 +00:00
|
|
|
/**
|
|
|
|
* Creates a new Vec2 object.
|
|
|
|
* @class Vec2
|
|
|
|
* @constructor
|
|
|
|
* @param {Number} x The x position of the vector
|
|
|
|
* @param {Number} y The y position of the vector
|
|
|
|
* @return {Vec2} This object
|
|
|
|
**/
|
2013-08-28 06:02:55 +00:00
|
|
|
function Vec2(x, y) {
|
|
|
|
if (typeof x === "undefined") { x = 0; }
|
|
|
|
if (typeof y === "undefined") { y = 0; }
|
2013-05-25 03:21:24 +00:00
|
|
|
this.x = x;
|
|
|
|
this.y = y;
|
2013-06-26 13:18:48 +00:00
|
|
|
return this;
|
2013-05-25 03:21:24 +00:00
|
|
|
}
|
2013-08-28 06:02:55 +00:00
|
|
|
Vec2.prototype.copyFrom = /**
|
|
|
|
* Copies the x and y properties from any given object to this Vec2.
|
|
|
|
* @method copyFrom
|
|
|
|
* @param {any} source - The object to copy from.
|
|
|
|
* @return {Vec2} This Vec2 object.
|
|
|
|
**/
|
|
|
|
function (source) {
|
2013-05-28 20:38:37 +00:00
|
|
|
return this.setTo(source.x, source.y);
|
2013-08-28 06:02:55 +00:00
|
|
|
};
|
|
|
|
Vec2.prototype.setTo = /**
|
2013-05-25 03:21:24 +00:00
|
|
|
* Sets the x and y properties of the Vector.
|
|
|
|
* @param {Number} x The x position of the vector
|
|
|
|
* @param {Number} y The y position of the vector
|
|
|
|
* @return {Vec2} This object
|
|
|
|
**/
|
2013-08-28 06:02:55 +00:00
|
|
|
function (x, y) {
|
2013-05-25 03:21:24 +00:00
|
|
|
this.x = x;
|
|
|
|
this.y = y;
|
|
|
|
return this;
|
2013-08-28 06:02:55 +00:00
|
|
|
};
|
|
|
|
Vec2.prototype.add = /**
|
2013-05-25 03:21:24 +00:00
|
|
|
* Add another vector to this one.
|
2013-08-28 06:02:55 +00:00
|
|
|
*
|
2013-05-25 03:21:24 +00:00
|
|
|
* @param {Vec2} other The other Vector.
|
|
|
|
* @return {Vec2} This for chaining.
|
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
function (a) {
|
2013-05-25 03:21:24 +00:00
|
|
|
this.x += a.x;
|
|
|
|
this.y += a.y;
|
|
|
|
return this;
|
2013-08-28 06:02:55 +00:00
|
|
|
};
|
|
|
|
Vec2.prototype.subtract = /**
|
2013-05-25 03:21:24 +00:00
|
|
|
* Subtract another vector from this one.
|
2013-08-28 06:02:55 +00:00
|
|
|
*
|
2013-05-25 03:21:24 +00:00
|
|
|
* @param {Vec2} other The other Vector.
|
|
|
|
* @return {Vec2} This for chaining.
|
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
function (v) {
|
2013-05-25 03:21:24 +00:00
|
|
|
this.x -= v.x;
|
|
|
|
this.y -= v.y;
|
|
|
|
return this;
|
2013-08-28 06:02:55 +00:00
|
|
|
};
|
|
|
|
Vec2.prototype.multiply = /**
|
2013-05-25 03:21:24 +00:00
|
|
|
* Multiply another vector with this one.
|
2013-08-28 06:02:55 +00:00
|
|
|
*
|
2013-05-25 03:21:24 +00:00
|
|
|
* @param {Vec2} other The other Vector.
|
|
|
|
* @return {Vec2} This for chaining.
|
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
function (v) {
|
2013-05-25 03:21:24 +00:00
|
|
|
this.x *= v.x;
|
|
|
|
this.y *= v.y;
|
|
|
|
return this;
|
2013-08-28 06:02:55 +00:00
|
|
|
};
|
|
|
|
Vec2.prototype.divide = /**
|
2013-05-25 03:21:24 +00:00
|
|
|
* Divide this vector by another one.
|
2013-08-28 06:02:55 +00:00
|
|
|
*
|
2013-05-25 03:21:24 +00:00
|
|
|
* @param {Vec2} other The other Vector.
|
|
|
|
* @return {Vec2} This for chaining.
|
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
function (v) {
|
2013-05-25 03:21:24 +00:00
|
|
|
this.x /= v.x;
|
|
|
|
this.y /= v.y;
|
|
|
|
return this;
|
2013-08-28 06:02:55 +00:00
|
|
|
};
|
|
|
|
Vec2.prototype.length = /**
|
2013-05-25 03:21:24 +00:00
|
|
|
* Get the length of this vector.
|
2013-08-28 06:02:55 +00:00
|
|
|
*
|
2013-05-25 03:21:24 +00:00
|
|
|
* @return {number} The length of this vector.
|
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
function () {
|
2013-05-25 03:21:24 +00:00
|
|
|
return Math.sqrt((this.x * this.x) + (this.y * this.y));
|
2013-08-28 06:02:55 +00:00
|
|
|
};
|
|
|
|
Vec2.prototype.lengthSq = /**
|
2013-05-25 03:21:24 +00:00
|
|
|
* Get the length squared of this vector.
|
2013-08-28 06:02:55 +00:00
|
|
|
*
|
2013-05-25 03:21:24 +00:00
|
|
|
* @return {number} The length^2 of this vector.
|
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
function () {
|
2013-05-25 03:21:24 +00:00
|
|
|
return (this.x * this.x) + (this.y * this.y);
|
2013-08-28 06:02:55 +00:00
|
|
|
};
|
|
|
|
Vec2.prototype.normalize = /**
|
2013-06-14 01:42:51 +00:00
|
|
|
* Normalize this vector.
|
2013-08-28 06:02:55 +00:00
|
|
|
*
|
2013-06-14 01:42:51 +00:00
|
|
|
* @return {Vec2} This for chaining.
|
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
function () {
|
|
|
|
var inv = (this.x != 0 || this.y != 0) ? 1 / Math.sqrt(this.x * this.x + this.y * this.y) : 0;
|
|
|
|
this.x *= inv;
|
|
|
|
this.y *= inv;
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
Vec2.prototype.dot = /**
|
2013-05-25 03:21:24 +00:00
|
|
|
* The dot product of two 2D vectors.
|
2013-08-28 06:02:55 +00:00
|
|
|
*
|
2013-05-25 03:21:24 +00:00
|
|
|
* @param {Vec2} a Reference to a source Vec2 object.
|
|
|
|
* @return {Number}
|
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
function (a) {
|
2013-05-25 03:21:24 +00:00
|
|
|
return ((this.x * a.x) + (this.y * a.y));
|
2013-08-28 06:02:55 +00:00
|
|
|
};
|
|
|
|
Vec2.prototype.cross = /**
|
2013-05-25 03:21:24 +00:00
|
|
|
* The cross product of two 2D vectors.
|
2013-08-28 06:02:55 +00:00
|
|
|
*
|
2013-05-25 03:21:24 +00:00
|
|
|
* @param {Vec2} a Reference to a source Vec2 object.
|
|
|
|
* @return {Number}
|
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
function (a) {
|
2013-05-25 03:21:24 +00:00
|
|
|
return ((this.x * a.y) - (this.y * a.x));
|
2013-08-28 06:02:55 +00:00
|
|
|
};
|
|
|
|
Vec2.prototype.projectionLength = /**
|
2013-05-25 03:21:24 +00:00
|
|
|
* The projection magnitude of two 2D vectors.
|
2013-08-28 06:02:55 +00:00
|
|
|
*
|
2013-05-25 03:21:24 +00:00
|
|
|
* @param {Vec2} a Reference to a source Vec2 object.
|
|
|
|
* @return {Number}
|
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
function (a) {
|
|
|
|
var den = a.dot(a);
|
|
|
|
if(den == 0) {
|
2013-05-25 03:21:24 +00:00
|
|
|
return 0;
|
2013-08-28 06:02:55 +00:00
|
|
|
} else {
|
2013-05-25 03:21:24 +00:00
|
|
|
return Math.abs(this.dot(a) / den);
|
|
|
|
}
|
2013-08-28 06:02:55 +00:00
|
|
|
};
|
|
|
|
Vec2.prototype.angle = /**
|
2013-05-25 03:21:24 +00:00
|
|
|
* The angle between two 2D vectors.
|
2013-08-28 06:02:55 +00:00
|
|
|
*
|
2013-05-25 03:21:24 +00:00
|
|
|
* @param {Vec2} a Reference to a source Vec2 object.
|
|
|
|
* @return {Number}
|
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
function (a) {
|
2013-05-25 03:21:24 +00:00
|
|
|
return Math.atan2(a.x * this.y - a.y * this.x, a.x * this.x + a.y * this.y);
|
2013-08-28 06:02:55 +00:00
|
|
|
};
|
|
|
|
Vec2.prototype.scale = /**
|
2013-05-25 03:21:24 +00:00
|
|
|
* Scale this vector.
|
2013-08-28 06:02:55 +00:00
|
|
|
*
|
2013-05-25 03:21:24 +00:00
|
|
|
* @param {number} x The scaling factor in the x direction.
|
|
|
|
* @param {?number=} y The scaling factor in the y direction. If this is not specified, the x scaling factor will be used.
|
|
|
|
* @return {Vec2} This for chaining.
|
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
function (x, y) {
|
2013-05-25 03:21:24 +00:00
|
|
|
this.x *= x;
|
|
|
|
this.y *= y || x;
|
|
|
|
return this;
|
2013-08-28 06:02:55 +00:00
|
|
|
};
|
|
|
|
Vec2.prototype.multiplyByScalar = /**
|
2013-05-30 12:25:48 +00:00
|
|
|
* Multiply this vector by the given scalar.
|
2013-08-28 06:02:55 +00:00
|
|
|
*
|
2013-05-30 12:25:48 +00:00
|
|
|
* @param {number} scalar
|
|
|
|
* @return {Vec2} This for chaining.
|
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
function (scalar) {
|
2013-05-30 12:25:48 +00:00
|
|
|
this.x *= scalar;
|
|
|
|
this.y *= scalar;
|
|
|
|
return this;
|
2013-08-28 06:02:55 +00:00
|
|
|
};
|
|
|
|
Vec2.prototype.multiplyAddByScalar = /**
|
2013-06-13 16:15:16 +00:00
|
|
|
* Adds the given vector to this vector then multiplies by the given scalar.
|
2013-08-28 06:02:55 +00:00
|
|
|
*
|
2013-06-13 16:15:16 +00:00
|
|
|
* @param {Vec2} a Reference to a source Vec2 object.
|
|
|
|
* @param {number} scalar
|
|
|
|
* @return {Vec2} This for chaining.
|
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
function (a, scalar) {
|
2013-06-13 16:15:16 +00:00
|
|
|
this.x += a.x * scalar;
|
|
|
|
this.y += a.y * scalar;
|
|
|
|
return this;
|
2013-08-28 06:02:55 +00:00
|
|
|
};
|
|
|
|
Vec2.prototype.divideByScalar = /**
|
2013-05-25 03:21:24 +00:00
|
|
|
* Divide this vector by the given scalar.
|
2013-08-28 06:02:55 +00:00
|
|
|
*
|
2013-05-25 03:21:24 +00:00
|
|
|
* @param {number} scalar
|
|
|
|
* @return {Vec2} This for chaining.
|
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
function (scalar) {
|
2013-05-25 03:21:24 +00:00
|
|
|
this.x /= scalar;
|
|
|
|
this.y /= scalar;
|
|
|
|
return this;
|
2013-08-28 06:02:55 +00:00
|
|
|
};
|
|
|
|
Vec2.prototype.reverse = /**
|
2013-05-25 03:21:24 +00:00
|
|
|
* Reverse this vector.
|
2013-08-28 06:02:55 +00:00
|
|
|
*
|
2013-05-25 03:21:24 +00:00
|
|
|
* @return {Vec2} This for chaining.
|
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
function () {
|
2013-05-25 03:21:24 +00:00
|
|
|
this.x = -this.x;
|
|
|
|
this.y = -this.y;
|
|
|
|
return this;
|
2013-08-28 06:02:55 +00:00
|
|
|
};
|
|
|
|
Vec2.prototype.equals = /**
|
2013-05-29 14:45:34 +00:00
|
|
|
* Check if both the x and y of this vector equal the given value.
|
2013-08-28 06:02:55 +00:00
|
|
|
*
|
2013-08-13 03:22:24 +00:00
|
|
|
* @return {bool}
|
2013-05-29 14:45:34 +00:00
|
|
|
*/
|
2013-08-28 06:02:55 +00:00
|
|
|
function (value) {
|
2013-05-29 14:45:34 +00:00
|
|
|
return (this.x == value && this.y == value);
|
2013-08-28 06:02:55 +00:00
|
|
|
};
|
|
|
|
Vec2.prototype.toString = /**
|
2013-05-25 03:21:24 +00:00
|
|
|
* Returns a string representation of this object.
|
|
|
|
* @method toString
|
|
|
|
* @return {string} a string representation of the object.
|
|
|
|
**/
|
2013-08-28 06:02:55 +00:00
|
|
|
function () {
|
2013-08-08 18:16:47 +00:00
|
|
|
return "[{Vec2 (x=" + this.x + " y=" + this.y + ")}]";
|
2013-08-28 06:02:55 +00:00
|
|
|
};
|
|
|
|
return Vec2;
|
|
|
|
})();
|
|
|
|
Phaser.Vec2 = Vec2;
|
|
|
|
})(Phaser || (Phaser = {}));
|