Class: Vector2

Vector2(x, y)

new Vector2(x, y)

Class representing a 2D vector. A 2D vector is an ordered pair of numbers (labeled x and y), which can be used to represent points in space, directions, etc.
Parameters:
Name Type Description
x number X value.
y number Y value.
Source:

Methods

add(v)

Add the content of another vector to this one.
Parameters:
Name Type Description
v Vector2 The other vector.
Source:

addScalar(s)

Add a scalar value to booth vector components.
Parameters:
Name Type Description
s number Scalar value.
Source:

addScaledVector(v, s)

Scale a vector components and add the result to this vector.
Parameters:
Name Type Description
v Vector2 The other vector.
s number Scalar value.
Source:

addVectors(a, b)

Add two vectors and store the result in this vector.
Parameters:
Name Type Description
a Vector2 The first vector.
b Vector2 The second vector.
Source:

angle(forcePositive) → {number}

Computes the angle in radians with respect to the positive x-axis.
Parameters:
Name Type Description
forcePositive boolean If true, the angle will be forced to be positive.
Source:
Returns:
Angle in radians.
Type
number

ceil()

Round the vector coordinates to integer by ceiling to the bigger integer.
Source:

clamp(min, max)

Clamp the vector coordinates to the range defined by two vectors. Applied to x and y independently.
Parameters:
Name Type Description
min Vector2 Minimum value.
max Vector2 Maximum value.
Source:

clampScalar(minVal, maxVal)

Clamp the vector coordinates to the range defined by two scalars.
Parameters:
Name Type Description
minVal number Minimum value.
maxVal number Maximum value.
Source:

clone() → {Vector2}

Create a clone of this vector object.
Source:
Returns:
A new vector with the same values as this one.
Type
Vector2

copy(v)

Copy the content of another vector into this one.
Parameters:
Name Type Description
v Vector2
Source:

cross(vector) → {number}

Cross multiplication between this vector and another vector.
Parameters:
Name Type Description
vector Vector2
Source:
Returns:
Result of the cross multiplication.
Type
number

distanceTo(v) → {number}

Distance between two vector positions.
Parameters:
Name Type Description
v Vector2 Vector to compute the distance to.
Source:
Returns:
Distance between the two vectors.
Type
number

distanceToSquared(v) → {number}

Distance between two vector positions squared. Faster for comparisons.
Parameters:
Name Type Description
v Vector2 Vector to compute the distance to.
Source:
Returns:
Distance between the two vectors squared.
Type
number

divide(v)

Divide the content of another vector from this one.
Parameters:
Name Type Description
v Vector2
Source:

divideScalar(s)

Divide a scalar value by booth vector components.
Parameters:
Name Type Description
s number
Source:

dot(vector) → {number}

Dot multiplication between this vector and another vector.
Parameters:
Name Type Description
vector Vector2
Source:
Returns:
Result of the dot multiplication.
Type
number

equals(v1, v2, alpha, v) → {Vector2}

Lerp between this vector and another vector.
Parameters:
Name Type Description
v1 Vector2 Vector to lerp from.
v2 Vector2 Vector to lerp to.
alpha number Lerp factor.
v Vector2 Vector to compare with.
Source:
Returns:
This vector. Vector2.prototype.lerpVectors = function(v1, v2, alpha) { return this.subVectors(v2, v1).multiplyScalar(alpha).add(v1); }; /** Check if two vectors are equal.
Type
Vector2

floor()

Round the vector coordinates to integer by flooring to the smaller integer.
Source:

fromArray(array)

Set vector value from array [x, y]. The vector can be converted to array using the toArray() method.
Parameters:
Name Type Description
array Array.<number> Array to set the vector value from.
Source:

length() → {number}

Length of the vector.
Source:
Returns:
Length of the vector.
Type
number

lengthSq() → {number}

Squared length of the vector. Faster for comparions.
Source:
Returns:
Squared length of the vector.
Type
number

lerp(v, alpha)

Lerp this vector to another vector.
Parameters:
Name Type Description
v Vector2 Vector to lerp to.
alpha number Lerp factor.
Source:

manhattanDistanceTo(v) → {number}

Manhattan distance between two vector positions.
Parameters:
Name Type Description
v Vector2 Vector to compute the distance to.
Source:
Returns:
Manhattan distance between the two vectors.
Type
number

manhattanLength() → {number}

Manhattan length of the vector.
Source:
Returns:
Manhattan length of the vector.
Type
number

max(v)

Set the maximum of x and y coordinates between two vectors. X is set as the max between this vector and the other vector.
Parameters:
Name Type Description
v Vector2
Source:

min(v)

Set the minimum of x and y coordinates between two vectors. X is set as the min between this vector and the other vector.
Parameters:
Name Type Description
v Vector2
Source:

multiply(v)

Multiply the content of another vector to this one.
Parameters:
Name Type Description
v Vector2 The other vector.
Source:

multiplyScalar(scalar)

Multiply a scalar value by booth vector components.
Parameters:
Name Type Description
scalar number Scalar value.
Source:

negate()

Negate the coordinates of this vector.
Source:

normalize() → {Vector2}

Normalize the vector (make it length one).
Source:
Returns:
This vector.
Type
Vector2

rotateAround(center, angle)

Rotate the vector around a central point.
Parameters:
Name Type Description
center Vector2 Point to rotate around.
angle number Angle in radians.
Source:

round()

Round the vector coordinates to their closest integer.
Source:

set(x, y)

Set vector x and y values.
Parameters:
Name Type Description
x number X value.
y number Y value.
Source:

setLength(length) → {Vector2}

Scale the vector to have a defined length value.
Parameters:
Name Type Description
length number Length to scale the vector to.
Source:
Returns:
This vector.
Type
Vector2

setScalar(scalar)

Set a scalar value into the x and y values.
Parameters:
Name Type Description
scalar number Scalar value.
Source:

sub(v)

Subtract the content of another vector to this one.
Parameters:
Name Type Description
v Vector2 The other vector.
Source:

subScalar(s)

Subtract a scalar value to booth vector components.
Parameters:
Name Type Description
s number Scalar value.
Source:

subVectors(a, b)

Subtract two vectors and store the result in this vector.
Parameters:
Name Type Description
a Vector2 The first vector.
b Vector2 The second vector.
Source:

toArray() → {Array.<number>}

Convert this vector to an array. Useful for serialization and storage. Values stored as [x, y].
Source:
Returns:
Array containing the values of the vector.
Type
Array.<number>