Vector2

Vector2

Vector with 2 components

Constructor

new Vector2(…args)

Parameters:
Name Type Attributes Description
args number | Array.<Number> | Vector2 <repeatable>

Initial values for the vector components.

Members

copy

Copies the values of another vector to this vector; an alias for .setVector

mul

Deprecated:
  • Deprecated as of 0.23.x Multiplies the current vector by another vector; an alias for .mult

(static) ONE :Vector2

One-vector (1, 1)

(static) UNIT_X :Vector2

Unit-X (1, 0)

(static) UNIT_Y :Vector2

Unit-Y (0, 1)

(static) ZERO :Vector2

Zero-vector (0, 0)

Methods

add(rhs) → {Vector2}

Adds a vector to the current vector

Parameters:
Name Type Description
rhs Vector2
Example
var v1 = new Vector2(1, 2);
var v2 = new Vector2(4, 5);
v1.add(v2); // v1 == (5, 7)

addDirect(x, y) → {Vector2}

Adds numbers 'x', 'y' to the current Vector2 values

Parameters:
Name Type Description
x number
y number
Example
var v = new Vector2(1, 2);
v.addDirect(2, 4); // v == (3, 6)

applyPost(matrix) → {Vector2}

Multiplies a Matrix2 with this vector

Parameters:
Name Type Description
matrix Matrix2

applyPre(matrix) → {Vector2}

Multiplies this vector with a Matrix2

Parameters:
Name Type Description
matrix Matrix2

clone() → {Vector2}

Clones the vector

copyTo(target) → {Vector2}

Copies this vector over another. Not equivalent to target.copy(this) when
the target vector has more components than the source vector

Parameters:
Name Type Description
target Vector

distance(rhs) → {number}

Computes the distance between the current Vector2 and another Vector2.
Note: When comparing the relative distances between two points it is usually sufficient
to compare the squared distances, thus avoiding an expensive square root operation.

Parameters:
Name Type Description
rhs Vector2

Vector2

Example
var v1 = new Vector2(); // v1 == (0, 0)
var v2 = new Vector2(0, 9);
v1.distance(v2); // 9

distanceSquared(rhs) → {number}

Computes the squared distance between the current Vector2 and another Vector2.
Note: When comparing the relative distances between two points it is usually sufficient
to compare the squared distances, thus avoiding an expensive square root operation.

Parameters:
Name Type Description
rhs Vector2

Vector2

Example
var v1 = new Vector2(); // v1 == (0, 0)
var v2 = new Vector2(0, 9);
v1.distanceSquared(v2); // 81

div(rhs) → {Vector2}

Divides the current Vector2 by another vector

Parameters:
Name Type Description
rhs Vector2
Example
var v = new Vector2(4, 16);
v.div(2, 4); // v == (2, 16)

divDirect(x, y) → {Vector2}

Divides the current Vector2 by numbers 'x', 'y' as inputs

Parameters:
Name Type Description
x number
y number
Example
var v = new Vector2(4, 9);
v.divDirect(2, 3); // v == (2, 3)

dot(rhs) → {number}

Computes the dot product between the current vector and another vector

Parameters:
Name Type Description
rhs Vector2

dotDirect(x, y) → {number}

Computes the dot product between the current vector and another vector given as 2 numeric values

Parameters:
Name Type Description
x number
y number

equals(rhs) → {boolean}

Returns whether this vector is approximately equal to a given vector

Parameters:
Name Type Description
rhs

equalsDirect(x, y) → {boolean}

Returns whether this vector is approximately equal to a given vector given as 2 numeric values

Parameters:
Name Type Description
x number
y number

getComponent(index) → {number}

Returns the vector component associated with the given index.
Vector components are numbered from 0 to 1 in this order: x, y.

Parameters:
Name Type Description
index number

length() → {number}

Calculates length of vector

lengthSquared() → {number}

Calculates the squared length/magnitude of the current Vector2.
Note: When comparing the relative distances between two points it is usually sufficient
to compare the squared distances, thus avoiding an expensive square root operation.

Example
var v = new Vector2(0, 9);
v.lengthSquared(); // 81

lerp(end, factor) → {Vector2}

Linearly interpolates between the current Vector2 and an 'end' Vector2

Parameters:
Name Type Description
end Vector2

End Vector2

factor number

Interpolation factor between 0.0 and 1.0

Example
var from = new Vector2(1, 2);
var to = new Vector2(3, 4);
var midway = from.clone().lerp(to, 0.5); // midway == (2, 3)

mulDirect(x, y) → {Vector2}

Multiplies the numbers 'x', 'y' to the current Vector2 values

Parameters:
Name Type Description
x number
y number
Example
var v = new Vector2(1, 2);
v.mulDirect(2, 4); // v == (2, 8)

mult(rhs) → {Vector2}

Multiplies the current vector by another vector

Parameters:
Name Type Description
rhs Vector2
Example
var v1 = new Vector2(4, 5);
var v2 = new Vector2(1, 2);
v1.mult(v2); // v1 == (4, 10)

negate() → {Vector2}

Performs component-wise negation of the vector

normalize() → {Vector2}

Normalizes the current vector

reflect(normal) → {Vector2}

Reflects a vector relative to the plane obtained from the normal parameter.

Parameters:
Name Type Description
normal Vector2

Defines the plane that reflects the vector. Assumed to be of unit length.

scale(factor) → {Vector2}

Scales the vector by a factor

Parameters:
Name Type Description
factor number

set(rhs) → {Vector2}

Sets the vector's values from another vector's values

Parameters:
Name Type Description
rhs Vector2
Example
var v = new Vector2(); // v == (0, 0)
v.set(new Vector2(2, 4)); // v == (2, 4)

setAny(…args) → {Vector2}

Sets a Vector2 given 2 numbers, an array, an {x, y} object or another Vector2
If no arguments are provided, the vector is set to Vector2.ZERO.

Parameters:
Name Type Attributes Description
args number | Array.<Number> | Vector2 <repeatable>

Values for the vector components.

setArray(array)

Sets the vector content from an array (length 2) of numbers.

Parameters:
Name Type Description
array Array.<number>

setComponent(index, value) → {Vector2}

Sets the vector component associated with the given index to a given value.
Vector components are numbered from 0 to 1 in this order: x, y.

Parameters:
Name Type Description
index number
value number

setDirect(x, y) → {Vector2}

Sets the vector's values from 2 numeric arguments

Parameters:
Name Type Description
x number
y number
Example
var v = new Vector2(); // v == (0, 0)
v.setDirect(2, 4); // v == (2, 4)

sub(rhs) → {Vector2}

Adds a vector from the current vector

Parameters:
Name Type Description
rhs Vector2
Example
var v1 = new Vector2(4, 5);
var v2 = new Vector2(1, 2);
v1.sub(v2); // v1 == (3, 3)

subDirect(x, y) → {Vector2}

Subtracts numbers 'x', 'y' from the current Vector2

Parameters:
Name Type Description
x number
y number
Example
var v = new Vector2(); // v == (0, 0)
v.subDirect(1, 2); // v == (-1, -2)

toArray() → {Array.<number>}

Returns the components of the vector in array form (length 2)

toString()

returns a human-readable string '{x:this.x, y:this.y}' of the vector

unsafeNormalize() → {Vector2}

Normalizes the current vector; this method does not perform special checks for zero length vectors

(static) fromAny(…args) → {Vector2}

Creates a Vector2 given 3 numbers, an array, an {x, y} object or another Vector2

Parameters:
Name Type Attributes Description
args number | Array.<Number> | Vector2 <repeatable>

Values for the vector components.

(static) fromArray(array) → {Vector2}

Creates a Vector2 given an array

Parameters:
Name Type Description
array Array.<number>