Vector3

Vector3

Vector with 3 components.

Constructor

new Vector3(…args)

Parameters:
Name Type Attributes Description
args number | Array.<Number> | Vector3 <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) DOWN :Vector3

Negative Unit-Y (0, -1, 0)

(static) ONE :Vector3

One-vector (1, 1, 1)

(static) UNIT_X :Vector3

Unit-X (1, 0, 0)

(static) UNIT_Y :Vector3

Unit-Y (0, 1, 0)

(static) UNIT_Z :Vector3

Unit-Z (0, 0, 1)

(static) UP :Vector3

Unit-Y (0, 1, 0)

(static) ZERO :Vector3

Zero-vector (0, 0, 0)

Methods

add(rhs) → {Vector3}

Adds a vector to the current vector

Parameters:
Name Type Description
rhs Vector3
Example
var a = new Vector3(1, 2, 3);
var b = new Vector3(4, 5, 6);
a.add(b); // a == (5, 7, 9)

addDirect(x, y, z) → {Vector3}

Adds numbers 'x', 'y', 'z' to the current Vector3 values

Parameters:
Name Type Description
x number

component of the vector to add

y number

component of the vector to add

z number

component of the vector to add

Example
var v = new Vector3(1, 2, 3);
v.addDirect(2, 4, 6); // v == (3, 6, 9)

applyPost(matrix) → {Vector3}

Multiplies a Matrix3 with this vector

Parameters:
Name Type Description
matrix Matrix3

applyPostPoint(matrix) → {Vector3}

Applies a Matrix4 (rotation, scale, translation) to this vector

Parameters:
Name Type Description
matrix Matrix4

applyPostVector(matrix) → {Vector3}

Applies a Matrix4 (rotation, scale) to this vector

Parameters:
Name Type Description
matrix Matrix4

applyPre(matrix) → {Vector3}

Multiplies this vector with a Matrix3

Parameters:
Name Type Description
matrix Matrix3

applyRotation(quaternion) → {Vector3}

Multiplies a Vector3 with a Quaternion to apply rotation to a Vector3.

Parameters:
Name Type Description
quaternion Quaternion

The Quaternion to rotate the Vector3 with.

calcAngleBetween(anotherVector) → {number}

Calculates angle between two vectors in radians.

Parameters:
Name Type Description
anotherVector Vector3

The second vector

calcAngleDegreesBetween(anotherVector) → {number}

Calculates angle between two vectors in degrees.

Parameters:
Name Type Description
anotherVector Vector3

The second vector

clone() → {Vector3}

Clones the vector

copyTo(target) → {Vector3}

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 Vector3

cross(rhs) → {Vector3}

Replaces this vector with the cross product between the current Vector3 and another vector

Parameters:
Name Type Description
rhs Vector3

vector to cross product with

Example
var a = new Vector3(0, 1, 0);
var b = new Vector3(0, 0, -1);
a.cross(b); // a == (-1, 0, 0)

crossDirect(x, y, z) → {Vector3}

Replaces this vector with the cross product between the current Vector3 and another vector given as 3 values

Parameters:
Name Type Description
x number

component of vector to cross product with

y number

component of vector to cross product with

z number

component of vector to cross product with

Example
var a = new Vector3(0, 1, 0);
var b = new Vector3(0, 0, -1);
a.cross(b); // a == (-1, 0, 0)

distance(rhs) → {number}

Computes the distance between the current Vector3 and another Vector3.
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 Vector3

Vector3

Example
var a = new Vector3(); // a == (0, 0, 0)
var b = new Vector3(0, 9, 0);
a.distance(b); // 9

distanceSquared(rhs) → {number}

Computes the squared distance between the current Vector3 and another Vector3.
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 Vector3

Vector3

Example
var a = new Vector3(); // a == (0, 0, 0)
var b = new Vector3(0, 9, 0);
a.distanceSquared(b); // 81

div(rhs) → {Vector3}

Divides the current Vector3 by another vector

Parameters:
Name Type Description
rhs Vector3

vector to divide by

Example
var v = new Vector3(2, 4, 6);
v.div(1, 2, 3); // v == (2, 2, 2)

divDirect(x, y, z) → {Vector3}

Divides the current Vector3 by numbers 'x', 'y', 'z' as inputs

Parameters:
Name Type Description
x number

component of the vector to divide by

y number

component of the vector to divide by

z number

component of the vector to divide by

Example
var v = new Vector3(2, 4, 6);
v.divDirect(1, 2, 3); // v == (2, 2, 2)

dot(rhs) → {number}

Computes the dot product between the current vector and another vector

Parameters:
Name Type Description
rhs Vector3

vector to dot product with

Example
var a = new Vector3(1, 0, 0);
var b = new Vector3(0, 1, 0);
a.dot(b) === 0; // true

dotDirect(x, y, z) → {number}

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

Parameters:
Name Type Description
x number

component of vector to dot product with

y number

component of vector to dot product with

z number

component of vector to dot product with

equals(rhs) → {boolean}

Returns whether this vector is approximately equal to a given vector

Parameters:
Name Type Description
rhs

vector to compare

equalsDirect(x, y, z) → {boolean}

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

Parameters:
Name Type Description
x number

component of vector to compare with

y number

component of vector to compare with

z number

component of vector to compare with

getComponent(index) → {number}

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

Parameters:
Name Type Description
index number

getDesOrder() → {Array.<number>}

sort the value in the vector in descent order and return the index of sorted x, y, z

length() → {number}

Calculates length of vector

lengthSquared() → {number}

Calculates the squared length/magnitude of the current Vector3.
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 Vector3(0, 9, 0);
v.lengthSquared(); // 81

lerp(end, factor) → {Vector3}

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

Parameters:
Name Type Description
end Vector3

End Vector3

factor number

Interpolation factor between 0.0 and 1.0

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

mulDirect(x, y, z) → {Vector3}

Multiplies the current Vector3 by numbers 'x', 'y', 'z' as inputs

Parameters:
Name Type Description
x number

component of the vector to multiply

y number

component of the vector to multiply

z number

component of the vector to multiply

Example
var v = new Vector3(1, 2, 3);
v.mulDirect(2, 4, 6); // v == (2, 8, 18)

mult(rhs) → {Vector3}

Multiplies the current vector by another vector

Parameters:
Name Type Description
rhs Vector3

vector to add

Example
var a = new Vector3(4, 5, 6);
var b = new Vector3(1, 2, 3);
a.mult(b); // a == (4, 10, 18)

negate() → {Vector3}

Performs component-wise negation of the vector

normalize() → {Vector3}

Normalizes the current vector

permutate(order) → {Vector3}

Permutate the component in the vector based on the order provided
For ex, order = [2, 1, 0], the vector will be permutate as bellow:
[x, y, z] => [z, y, x]

Parameters:
Name Type Description
order Array.<number>

reflect(normal) → {Vector3}

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

Parameters:
Name Type Description
normal Vector3

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

scale(factor) → {Vector3}

Scales the vector by a factor

Parameters:
Name Type Description
factor number
Example
var v = new Vector3(1, 2, 3);
v.scale(2); // v == (2, 4, 6)

set(rhs) → {Vector3}

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

Parameters:
Name Type Description
rhs Vector3 | Vector4

vector to copy from

Example
var v = new Vector3(); // v == (0, 0, 0)
v.set(new Vector3(2, 4, 6)); // v == (2, 4, 6)

setAny(…args) → {Vector3}

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

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

Values for the vector components.

setArray(array)

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

Parameters:
Name Type Description
array Array.<number>

setComponent(index, value) → {Vector3}

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

Parameters:
Name Type Description
index number
value number

setDirect(x, y, z) → {Vector3}

Sets the vector's values from 3 numeric arguments

Parameters:
Name Type Description
x number

component of vector to copy from

y number
z number
Example
var v = new Vector3(); // v == (0, 0, 0)
v.setDirect(2, 4, 6); // v == (2, 4, 6)

smoothDamp(target, currentVelocity, deltaTime, smoothTimeopt, maxSpeedopt) → {Vector3}

Gradually changes the vector value value towards a desired goal over time. See MathUtils#smoothDamp.

Parameters:
Name Type Attributes Default Description
target Vector3

The position we are trying to reach.

currentVelocity Vector3

An object to store the current position and velocity in.

deltaTime number

The time since the last call to this function.

smoothTime number <optional>
0.3

Approximately the time it will take to reach the target. A smaller value will reach the target faster.

maxSpeed number <optional>
1e7

Optionally allows you to clamp the maximum speed.

sub(rhs) → {Vector3}

Subtracts a vector from the current vector

Parameters:
Name Type Description
rhs Vector3

vector to subtract

Example
var a = new Vector3(4, 5, 6);
var b = new Vector3(1, 2, 3);
a.sub(b); // a == (3, 3, 3)

subDirect(x, y, z) → {Vector3}

Subtracts numbers 'x', 'y', 'z' from the current Vector3

Parameters:
Name Type Description
x number

component of vector to subtract

y number

component of vector to subtract

z number

component of vector to subtract

Example
var v = new Vector3(); // v == (0, 0, 0)
v.subDirect(1, 2, 3); // v == (-1, -2, -3)

toArray() → {Array.<number>}

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

toString()

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

unsafeNormalize() → {Vector3}

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

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

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

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

Values for the vector components.

(static) fromArray(array) → {Vector3}

Creates a Vector3 given an array (length 3)

Parameters:
Name Type Description
array Array.<number>