Vector4

Vector4

Vector with 4 components

Constructor

new Vector4(…args)

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

Initial values for the vector components.

Members

copy

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

mul

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

(static) ONE :Vector4

One-vector (1, 1, 1, 1)

(static) UNIT_W :Vector4

Unit-W (0, 0, 0, 1)

(static) UNIT_X :Vector4

Unit-X (1, 0, 0, 0)

(static) UNIT_Y :Vector4

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

(static) UNIT_Z :Vector4

Unit-Z (0, 0, 1, 0)

(static) ZERO :Vector4

Zero-vector (0, 0, 0, 0)

Methods

add(rhs) → {Vector4}

Adds a vector to the current vector

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

addDirect(x, y, z, w) → {Vector4}

Adds numbers 'x', 'y', 'z', 'w' to the current Vector4 values

Parameters:
Name Type Description
x number
y number
z number
w number
Example
var v = new Vector4(1, 2, 3, 4);
v.addDirect(2, 4, 6, 8); // v == (3, 6, 9, 12)

applyPost(matrix) → {Vector4}

Multiplies a Matrix4 with this vector

Parameters:
Name Type Description
matrix Matrix4

applyPre(matrix) → {Vector4}

Multiplies this vector with a Matrix4

Parameters:
Name Type Description
matrix Matrix4

clone() → {Vector4}

Clones the vector

copyTo(target) → {Vector4}

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 Vector4 and another Vector4.
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 Vector4

Vector4

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

distanceSquared(rhs) → {number}

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

Vector4

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

div(rhs) → {Vector4}

Divides the current Vector4 by another vector

Parameters:
Name Type Description
rhs Vector4
Example
var v = new Vector4(2, 4, 6, 8);
v.divDirect(1, 2, 3, 4); // v == (2, 2, 2, 2)

divDirect(x, y, z, w) → {Vector4}

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

Parameters:
Name Type Description
x number
y number
z number
w number
Example
var v = new Vector4(4, 9, 16, 25);
v.divDirect(2, 3, 4, 5); // v == (2, 3, 4, 5)

dot(rhs) → {number}

Computes the dot product between the current vector and another vector

Parameters:
Name Type Description
rhs Vector4

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

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

Parameters:
Name Type Description
x number
y number
z number
w number

equals(rhs) → {boolean}

Returns whether this vector is approximately equal to a given vector

Parameters:
Name Type Description
rhs

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

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

Parameters:
Name Type Description
x number
y number
z number
w number

getComponent(index) → {number}

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

Parameters:
Name Type Description
index number

length() → {number}

Calculates length of vector

lengthSquared() → {number}

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

lerp(end, factor) → {Vector4}

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

Parameters:
Name Type Description
end Vector4

End Vector4

factor number

Interpolation factor between 0.0 and 1.0

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

mulDirect(x, y, z, w) → {Vector4}

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

Parameters:
Name Type Description
x number
y number
z number
w number
Example
var v = new Vector4(1, 2, 3, 4);
v.mulDirect(2, 4, 6, 8); // v == (2, 8, 18, 32)

mult(rhs) → {Vector4}

Multiplies the current vector by another vector

Parameters:
Name Type Description
rhs Vector4
Example
var v1 = new Vector4(4, 5, 6, 7);
var v2 = new Vector4(1, 2, 3, 4);
v1.mult(v2); // v1 == (4, 10, 18, 28)

negate() → {Vector4}

Performs component-wise negation of the vector

normalize() → {Vector4}

Normalizes the current vector

reflect(normal) → {Vector4}

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

Parameters:
Name Type Description
normal Vector4

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

scale(factor) → {Vector4}

Scales the vector by a factor

Parameters:
Name Type Description
factor number

set(rhs) → {Vector4}

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

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

setAny(…args) → {Vector4}

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

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

Values for the vector components.

setArray(array)

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

Parameters:
Name Type Description
array Array.<number>

setComponent(index, value) → {Vector4}

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

Parameters:
Name Type Description
index number
value number

setDirect(x, y, z, w) → {Vector4}

Sets the vector's values from 4 numeric arguments

Parameters:
Name Type Description
x number
y number
z number
w number
Example
var v = new Vector4(); // v == (0, 0, 0, 0)
v.setDirect(2, 4, 6, 8); // v == (2, 4, 6, 8)

sub(rhs) → {Vector4}

Adds a vector from the current vector

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

subDirect(x, y, z, w) → {Vector4}

Subtracts numbers 'x', 'y', 'z', 'w' from the current Vector4

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

toArray() → {Array.<number>}

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

toString()

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

unsafeNormalize() → {Vector4}

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

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

Creates a Vector4 given 4 numbers, an array, an {x, y, z, w} object or another Vector4

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

Values for the vector components.

(static) fromArray(array) → {Vector4}

Creates a Vector4 given an array (length 4)

Parameters:
Name Type Description
array Array.<number>