2013-08-08 18:16:47 +00:00
|
|
|
/// <reference path="../_definitions.ts" />
|
2013-05-25 03:21:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Phaser - Vec2Utils
|
|
|
|
*
|
|
|
|
* A collection of methods useful for manipulating and performing operations on 2D vectors.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
module Phaser {
|
|
|
|
|
|
|
|
export class Vec2Utils {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds two 2D vectors.
|
|
|
|
*
|
|
|
|
* @param {Vec2} a Reference to a source Vec2 object.
|
|
|
|
* @param {Vec2} b Reference to a source Vec2 object.
|
|
|
|
* @param {Vec2} out The output Vec2 that is the result of the operation.
|
|
|
|
* @return {Vec2} A Vec2 that is the sum of the two vectors.
|
|
|
|
*/
|
2013-08-08 10:34:33 +00:00
|
|
|
static add(a: Vec2, b: Vec2, out: Vec2 = new Vec2): Vec2 {
|
2013-05-25 03:21:24 +00:00
|
|
|
return out.setTo(a.x + b.x, a.y + b.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Subtracts two 2D vectors.
|
|
|
|
*
|
|
|
|
* @param {Vec2} a Reference to a source Vec2 object.
|
|
|
|
* @param {Vec2} b Reference to a source Vec2 object.
|
|
|
|
* @param {Vec2} out The output Vec2 that is the result of the operation.
|
|
|
|
* @return {Vec2} A Vec2 that is the difference of the two vectors.
|
|
|
|
*/
|
2013-08-08 10:34:33 +00:00
|
|
|
static subtract(a: Vec2, b: Vec2, out: Vec2 = new Vec2): Vec2 {
|
2013-05-25 03:21:24 +00:00
|
|
|
return out.setTo(a.x - b.x, a.y - b.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Multiplies two 2D vectors.
|
|
|
|
*
|
|
|
|
* @param {Vec2} a Reference to a source Vec2 object.
|
|
|
|
* @param {Vec2} b Reference to a source Vec2 object.
|
|
|
|
* @param {Vec2} out The output Vec2 that is the result of the operation.
|
|
|
|
* @return {Vec2} A Vec2 that is the sum of the two vectors multiplied.
|
|
|
|
*/
|
2013-08-08 10:34:33 +00:00
|
|
|
static multiply(a: Vec2, b: Vec2, out: Vec2 = new Vec2): Vec2 {
|
2013-05-25 03:21:24 +00:00
|
|
|
return out.setTo(a.x * b.x, a.y * b.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Divides two 2D vectors.
|
|
|
|
*
|
|
|
|
* @param {Vec2} a Reference to a source Vec2 object.
|
|
|
|
* @param {Vec2} b Reference to a source Vec2 object.
|
|
|
|
* @param {Vec2} out The output Vec2 that is the result of the operation.
|
|
|
|
* @return {Vec2} A Vec2 that is the sum of the two vectors divided.
|
|
|
|
*/
|
2013-08-08 10:34:33 +00:00
|
|
|
static divide(a: Vec2, b: Vec2, out: Vec2 = new Vec2): Vec2 {
|
2013-05-25 03:21:24 +00:00
|
|
|
return out.setTo(a.x / b.x, a.y / b.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scales a 2D vector.
|
|
|
|
*
|
|
|
|
* @param {Vec2} a Reference to a source Vec2 object.
|
|
|
|
* @param {number} s Scaling value.
|
|
|
|
* @param {Vec2} out The output Vec2 that is the result of the operation.
|
|
|
|
* @return {Vec2} A Vec2 that is the scaled vector.
|
|
|
|
*/
|
2013-08-08 10:34:33 +00:00
|
|
|
static scale(a: Phaser.Vec2, s: number, out: Phaser.Vec2 = new Phaser.Vec2): Phaser.Vec2 {
|
2013-05-25 03:21:24 +00:00
|
|
|
return out.setTo(a.x * s, a.y * s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-06-13 16:15:16 +00:00
|
|
|
* Adds two 2D vectors together and multiplies the result by the given scalar.
|
|
|
|
*
|
|
|
|
* @param {Vec2} a Reference to a source Vec2 object.
|
|
|
|
* @param {Vec2} b Reference to a source Vec2 object.
|
|
|
|
* @param {number} s Scaling value.
|
|
|
|
* @param {Vec2} out The output Vec2 that is the result of the operation.
|
|
|
|
* @return {Vec2} A Vec2 that is the sum of the two vectors added and multiplied.
|
|
|
|
*/
|
2013-08-08 10:34:33 +00:00
|
|
|
static multiplyAdd(a: Vec2, b: Vec2, s: number, out: Vec2 = new Vec2): Vec2 {
|
2013-06-13 16:15:16 +00:00
|
|
|
return out.setTo(a.x + b.x * s, a.y + b.y * s);
|
|
|
|
}
|
|
|
|
|
2013-06-14 01:42:51 +00:00
|
|
|
/**
|
|
|
|
* Return a negative vector.
|
|
|
|
*
|
|
|
|
* @param {Vec2} a Reference to a source Vec2 object.
|
|
|
|
* @param {Vec2} out The output Vec2 that is the result of the operation.
|
|
|
|
* @return {Vec2} A Vec2 that is the negative vector.
|
|
|
|
*/
|
2013-08-08 10:34:33 +00:00
|
|
|
static negative(a: Vec2, out: Vec2 = new Vec2): Vec2 {
|
2013-06-14 01:42:51 +00:00
|
|
|
return out.setTo(-a.x, -a.y);
|
|
|
|
}
|
|
|
|
|
2013-06-13 16:15:16 +00:00
|
|
|
/**
|
|
|
|
* Return a perpendicular vector (90 degrees rotation)
|
2013-05-25 03:21:24 +00:00
|
|
|
*
|
|
|
|
* @param {Vec2} a Reference to a source Vec2 object.
|
|
|
|
* @param {Vec2} out The output Vec2 that is the result of the operation.
|
|
|
|
* @return {Vec2} A Vec2 that is the scaled vector.
|
|
|
|
*/
|
2013-08-08 10:34:33 +00:00
|
|
|
static perp(a: Vec2, out: Vec2 = new Vec2): Vec2 {
|
2013-06-13 16:15:16 +00:00
|
|
|
return out.setTo(-a.y, a.x);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a perpendicular vector (-90 degrees rotation)
|
|
|
|
*
|
|
|
|
* @param {Vec2} a Reference to a source Vec2 object.
|
|
|
|
* @param {Vec2} out The output Vec2 that is the result of the operation.
|
|
|
|
* @return {Vec2} A Vec2 that is the scaled vector.
|
|
|
|
*/
|
2013-08-08 10:34:33 +00:00
|
|
|
static rperp(a: Vec2, out: Vec2 = new Vec2): Vec2 {
|
2013-05-25 03:21:24 +00:00
|
|
|
return out.setTo(a.y, -a.x);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if two 2D vectors are equal.
|
|
|
|
*
|
|
|
|
* @param {Vec2} a Reference to a source Vec2 object.
|
|
|
|
* @param {Vec2} b Reference to a source Vec2 object.
|
|
|
|
* @return {Boolean}
|
|
|
|
*/
|
2013-08-08 10:34:33 +00:00
|
|
|
static equals(a: Vec2, b: Vec2): boolean {
|
2013-05-25 03:21:24 +00:00
|
|
|
return a.x == b.x && a.y == b.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param {Vec2} a Reference to a source Vec2 object.
|
|
|
|
* @param {Vec2} b Reference to a source Vec2 object.
|
|
|
|
* @param {Vec2} epsilon
|
|
|
|
* @return {Boolean}
|
|
|
|
*/
|
2013-08-08 10:34:33 +00:00
|
|
|
static epsilonEquals(a: Vec2, b: Vec2, epsilon: number): boolean {
|
2013-05-25 03:21:24 +00:00
|
|
|
return Math.abs(a.x - b.x) <= epsilon && Math.abs(a.y - b.y) <= epsilon;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the distance between two 2D vectors.
|
|
|
|
*
|
|
|
|
* @param {Vec2} a Reference to a source Vec2 object.
|
|
|
|
* @param {Vec2} b Reference to a source Vec2 object.
|
|
|
|
* @return {Number}
|
|
|
|
*/
|
|
|
|
static distance(a: Vec2, b: Vec2): number {
|
|
|
|
return Math.sqrt(Vec2Utils.distanceSq(a, b));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the distance squared between two 2D vectors.
|
|
|
|
*
|
|
|
|
* @param {Vec2} a Reference to a source Vec2 object.
|
|
|
|
* @param {Vec2} b Reference to a source Vec2 object.
|
|
|
|
* @return {Number}
|
|
|
|
*/
|
|
|
|
static distanceSq(a: Vec2, b: Vec2): number {
|
|
|
|
return ((a.x - b.x) * (a.x - b.x)) + ((a.y - b.y) * (a.y - b.y));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Project two 2D vectors onto another vector.
|
|
|
|
*
|
|
|
|
* @param {Vec2} a Reference to a source Vec2 object.
|
|
|
|
* @param {Vec2} b Reference to a source Vec2 object.
|
|
|
|
* @param {Vec2} out The output Vec2 that is the result of the operation.
|
|
|
|
* @return {Vec2} A Vec2.
|
|
|
|
*/
|
2013-08-08 10:34:33 +00:00
|
|
|
static project(a: Vec2, b: Vec2, out: Vec2 = new Vec2): Vec2 {
|
2013-05-25 03:21:24 +00:00
|
|
|
|
|
|
|
var amt = a.dot(b) / b.lengthSq();
|
|
|
|
|
|
|
|
if (amt != 0)
|
|
|
|
{
|
|
|
|
out.setTo(amt * b.x, amt * b.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Project this vector onto a vector of unit length.
|
|
|
|
*
|
|
|
|
* @param {Vec2} a Reference to a source Vec2 object.
|
|
|
|
* @param {Vec2} b Reference to a source Vec2 object.
|
|
|
|
* @param {Vec2} out The output Vec2 that is the result of the operation.
|
|
|
|
* @return {Vec2} A Vec2.
|
|
|
|
*/
|
2013-08-08 10:34:33 +00:00
|
|
|
static projectUnit(a: Vec2, b: Vec2, out: Vec2 = new Vec2): Vec2 {
|
2013-05-25 03:21:24 +00:00
|
|
|
|
|
|
|
var amt = a.dot(b);
|
|
|
|
|
|
|
|
if (amt != 0)
|
|
|
|
{
|
|
|
|
out.setTo(amt * b.x, amt * b.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Right-hand normalize (make unit length) a 2D vector.
|
|
|
|
*
|
|
|
|
* @param {Vec2} a Reference to a source Vec2 object.
|
|
|
|
* @param {Vec2} out The output Vec2 that is the result of the operation.
|
|
|
|
* @return {Vec2} A Vec2.
|
|
|
|
*/
|
2013-08-08 10:34:33 +00:00
|
|
|
static normalRightHand(a: Vec2, out: Vec2 = new Vec2): Vec2 {
|
2013-05-25 03:21:24 +00:00
|
|
|
return out.setTo(a.y * -1, a.x);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Normalize (make unit length) a 2D vector.
|
|
|
|
*
|
|
|
|
* @param {Vec2} a Reference to a source Vec2 object.
|
|
|
|
* @param {Vec2} out The output Vec2 that is the result of the operation.
|
|
|
|
* @return {Vec2} A Vec2.
|
|
|
|
*/
|
2013-08-08 10:34:33 +00:00
|
|
|
static normalize(a: Vec2, out: Vec2 = new Vec2): Vec2 {
|
2013-05-25 03:21:24 +00:00
|
|
|
|
|
|
|
var m = a.length();
|
|
|
|
|
|
|
|
if (m != 0)
|
|
|
|
{
|
|
|
|
out.setTo(a.x / m, a.y / m);
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The dot product of two 2D vectors.
|
|
|
|
*
|
|
|
|
* @param {Vec2} a Reference to a source Vec2 object.
|
|
|
|
* @param {Vec2} b Reference to a source Vec2 object.
|
|
|
|
* @return {Number}
|
|
|
|
*/
|
|
|
|
static dot(a: Vec2, b: Vec2): number {
|
|
|
|
return ((a.x * b.x) + (a.y * b.y));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The cross product of two 2D vectors.
|
|
|
|
*
|
|
|
|
* @param {Vec2} a Reference to a source Vec2 object.
|
|
|
|
* @param {Vec2} b Reference to a source Vec2 object.
|
|
|
|
* @return {Number}
|
|
|
|
*/
|
|
|
|
static cross(a: Vec2, b: Vec2): number {
|
|
|
|
return ((a.x * b.y) - (a.y * b.x));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The angle between two 2D vectors.
|
|
|
|
*
|
|
|
|
* @param {Vec2} a Reference to a source Vec2 object.
|
|
|
|
* @param {Vec2} b Reference to a source Vec2 object.
|
|
|
|
* @return {Number}
|
|
|
|
*/
|
|
|
|
static angle(a: Vec2, b: Vec2): number {
|
|
|
|
return Math.atan2(a.x * b.y - a.y * b.x, a.x * b.x + a.y * b.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The angle squared between two 2D vectors.
|
|
|
|
*
|
|
|
|
* @param {Vec2} a Reference to a source Vec2 object.
|
|
|
|
* @param {Vec2} b Reference to a source Vec2 object.
|
|
|
|
* @return {Number}
|
|
|
|
*/
|
|
|
|
static angleSq(a: Vec2, b: Vec2): number {
|
|
|
|
return a.subtract(b).angle(b.subtract(a));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rotate a 2D vector around the origin to the given angle (theta).
|
|
|
|
*
|
|
|
|
* @param {Vec2} a Reference to a source Vec2 object.
|
|
|
|
* @param {Vec2} b Reference to a source Vec2 object.
|
|
|
|
* @param {Number} theta The angle of rotation in radians.
|
|
|
|
* @param {Vec2} out The output Vec2 that is the result of the operation.
|
|
|
|
* @return {Vec2} A Vec2.
|
|
|
|
*/
|
2013-08-08 10:34:33 +00:00
|
|
|
static rotateAroundOrigin(a: Vec2, b: Vec2, theta: number, out: Vec2 = new Vec2): Vec2 {
|
2013-05-25 03:21:24 +00:00
|
|
|
var x = a.x - b.x;
|
|
|
|
var y = a.y - b.y;
|
|
|
|
return out.setTo(x * Math.cos(theta) - y * Math.sin(theta) + b.x, x * Math.sin(theta) + y * Math.cos(theta) + b.y);
|
|
|
|
}
|
|
|
|
|
2013-06-14 01:42:51 +00:00
|
|
|
/**
|
|
|
|
* Rotate a 2D vector to the given angle (theta).
|
|
|
|
*
|
|
|
|
* @param {Vec2} a Reference to a source Vec2 object.
|
|
|
|
* @param {Vec2} b Reference to a source Vec2 object.
|
|
|
|
* @param {Number} theta The angle of rotation in radians.
|
|
|
|
* @param {Vec2} out The output Vec2 that is the result of the operation.
|
|
|
|
* @return {Vec2} A Vec2.
|
|
|
|
*/
|
2013-08-08 10:34:33 +00:00
|
|
|
static rotate(a: Vec2, theta: number, out: Vec2 = new Vec2): Vec2 {
|
2013-06-14 01:42:51 +00:00
|
|
|
|
|
|
|
var c = Math.cos(theta);
|
|
|
|
var s = Math.sin(theta);
|
|
|
|
|
|
|
|
return out.setTo(a.x * c - a.y * s, a.x * s + a.y * c);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-25 03:21:24 +00:00
|
|
|
/**
|
|
|
|
* Clone a 2D vector.
|
|
|
|
*
|
|
|
|
* @param {Vec2} a Reference to a source Vec2 object.
|
|
|
|
* @param {Vec2} out The output Vec2 that is the result of the operation.
|
|
|
|
* @return {Vec2} A Vec2 that is a copy of the source Vec2.
|
|
|
|
*/
|
2013-08-08 10:34:33 +00:00
|
|
|
static clone(a: Vec2, out: Vec2 = new Vec2): Vec2 {
|
2013-05-25 03:21:24 +00:00
|
|
|
return out.setTo(a.x, a.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reflect this vector on an arbitrary axis.
|
|
|
|
*
|
|
|
|
* @param {Vec2} axis The vector representing the axis.
|
|
|
|
* @return {Vec2} This for chaining.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
static reflect(axis): Vec2 {
|
|
|
|
|
|
|
|
var x = this.x;
|
|
|
|
var y = this.y;
|
|
|
|
this.project(axis).scale(2);
|
|
|
|
this.x -= x;
|
|
|
|
this.y -= y;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reflect this vector on an arbitrary axis (represented by a unit vector)
|
|
|
|
*
|
|
|
|
* @param {Vec2} axis The unit vector representing the axis.
|
|
|
|
* @return {Vec2} This for chaining.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
static reflectN(axis): Vec2 {
|
|
|
|
|
|
|
|
var x = this.x;
|
|
|
|
var y = this.y;
|
|
|
|
this.projectN(axis).scale(2);
|
|
|
|
this.x -= x;
|
|
|
|
this.y -= y;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static getMagnitude(): number {
|
|
|
|
return Math.sqrt(Math.pow(this.x, 2) + Math.pow(this.y, 2));
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|