Class Vector3

This class holds the x,y,z components of a 3D point or vector. It includes linear algebra routines for working with vectors (e.g., dot product, cross product).

Most of the functions in the class are defined both as member functions that can be called on a specific instance of Vector3 and as static functions. The static functions return a new result, leaving the original inputs unchanged, whereas, in general, member functions save the result in this vector itself and return void:

const v = new Vector3(1, 2, 3);
const w = new Vector3(4, 5, 6);

// saves the result in n, v and w are unchanged.
const n = Vector3.cross(v, w);

// saves the result in v and returns null. v now becomes v crossed-with w.
v.cross(w);

Hierarchy

  • Vector3

Constructors

  • Constructs a Vector3 object from x, y, and z values

    Parameters

    • x: number = 0

      The x value for the Vector3

    • y: number = 0

      The y value for the Vector3

    • z: number = 0

      The z value for the Vector3

    Returns Vector3

Properties

x: number
y: number
z: number
BACK: Vector3 = ...

A Vector3 object that points backward. (See note in the Vector3.ZERO docs for important usage info.)

DOWN: Vector3 = ...

A Vector3 object that points down. (See note in the Vector3.ZERO docs for important usage info.)

FORWARD: Vector3 = ...

A Vector3 object that points forward. (See note in the Vector3.ZERO docs for important usage info.)

LEFT: Vector3 = ...

A Vector3 object that points left. (See note in the Vector3.ZERO docs for important usage info.)

ONE: Vector3 = ...

A Vector3 object with all one values. (See note in the Vector3.ZERO docs for important usage info.)

RIGHT: Vector3 = ...

A Vector3 object that points right. (See note in the Vector3.ZERO docs for important usage info.)

UP: Vector3 = ...

A Vector3 object that points up. (See note in the Vector3.ZERO docs for important usage info.)

X_AXIS: Vector3 = Vector3.RIGHT

A Vector3 object that points along the x-axis. (See note in the Vector3.ZERO docs for important usage info.)

Y_AXIS: Vector3 = Vector3.UP

A Vector3 object that points along the y-axis. (See note in the Vector3.ZERO docs for important usage info.)

ZERO: Vector3 = ...

A static property to provide quick access to a Vector3 with all of its x,y,z components equal to zero. (Note: Be careful not to change the value of this field! It is marked readonly, but typescipt do not completely enforce this!)

// Good use of ZERO:
const p = new Vector3();
if (p.equals(Vector3.ZERO)) {
console.log("p is (0,0,0)")
}

// Dangerous use of ZERO!!!!
const p = Vector3.ZERO; // makes p a reference to Vector3.ZERO
p.add(new Vector3(1, 0, 0)); // changes Vector3.ZERO!

// Do this instead:
const p = Vector3.copy(Vector3.ZERO); // or "new Vector3();" or another function that returns a NEW Vector3 object
p.add(new Vector3(1, 0, 0));
Z_AXIS: Vector3 = Vector3.FORWARD

A Vector3 object that points along the z-axis. (See note in the Vector3.ZERO docs for important usage info.)

Methods

  • Computes the angle between this Vector3 and another Vector3

    Returns

    The angle between the two Vectors

    Parameters

    Returns number

  • Computes the cross product of two Vector3s and stores the result in the original Vector3.

    Parameters

    • v: Vector3

      The Vector3 to compute the cross product with.

    Returns void

  • Computes the distance between this Vector3 and another Vector3

    Returns

    The distance between the two Vectors

    Parameters

    Returns number

  • Divides each component of the vector with a scalar value

    Parameters

    • n: number

      Scalar value to divide with

    Returns void

  • Compute the dot product of two Vector3 objects

    Returns

    The dot product of the two Vector3 objects

    Parameters

    • v: Vector3

      The other Vector3 object

    Returns number

  • Checks if the x,y,z components of this Vector3 are exactly equal to those of the given Vector3

    Returns

    A boolean value indicating if the Vector3 objects are equal

    Parameters

    • v: Vector3

      The Vector3 object to compare to

    Returns boolean

  • Checks if the x,y,z components of this Vector3 are equal (within a small value of epsilon) to those of the given Vector3.

    Returns

    A boolean value indicating if the Vector3 objects are equal

    Parameters

    • v: Vector3

      The Vector3 object to compare to

    • epsilon: number = MathUtils.EPSILON

      A small value of acceptable variance to account for numerical instability

    Returns boolean

  • Calculates the length of a Vector3 object

    Returns

    The length of the Vector3 object

    Returns number

  • Linearly interpolates between two Vector3s

    Parameters

    • v1: Vector3

      The starting Vector3

    • v2: Vector3

      The ending Vector3

    • alpha: number

      The interpolation amount (should be in the range [0, 1])

    Returns void

  • Multiplies each component of the vector with a scalar value

    Parameters

    • n: number

      Scalar value to multiply with

    Returns void

  • Sets the x, y, and z values of a Vector3 object

    Parameters

    • x: number

      The x value to set

    • y: number

      The y value to set

    • z: number

      The z value to set

    Returns void

  • Sets this Vector3's position to the provided Matrix4's position

    Parameters

    • m: Matrix4

      The Matrix4 to get the position from

    Returns void

  • Sets this Vector3's scale to the provided Matrix4's scale

    Parameters

    • m: Matrix4

      The Matrix4 to get the scale from

    Returns void

  • Transforms this Vector3 instance as a point with a given Matrix4

    Parameters

    • m: Matrix4

      The Matrix4 to transform this Vector3

    Returns void

  • Transforms this Vector3 instance as a direction with a given Matrix4, ignoring the translation component

    Parameters

    • m: Matrix4

      The Matrix4 to transform this Vector3

    Returns void

  • Adds two Vector3 objects together

    Returns

    A new Vector3 object with the x, y, and z values equal to the sum of the corresponding values of the input vectors

    Parameters

    • v1: Vector3

      The first Vector3 object

    • v2: Vector3

      The second Vector3 object

    Returns Vector3

  • Computes the angle in radians between two Vector3 objects

    Returns

    The angle in radians between the two input vectors

    Parameters

    • v1: Vector3

      The first Vector3 object

    • v2: Vector3

      The second Vector3 object

    Returns number

  • Computes the distance between two Vector3 objects

    Returns

    The distance between the two input vectors

    Parameters

    • v1: Vector3

      The first Vector3 object

    • v2: Vector3

      The second Vector3 object

    Returns number

  • Divide two Vector3 objects

    Returns

    A new Vector3 object with the x, y, and z values equal to the quotient of the corresponding values of the input vectors

    Parameters

    • v1: Vector3

      The Vector3 object to divide

    • v2: Vector3

      The Vector3 object to divide by

    Returns Vector3

  • Divides a Vector3 object by a scalar value

    Returns

    A new Vector3 object with the x, y, and z values divided by the scalar value

    Parameters

    • v: Vector3

      The Vector3 object

    • n: number

      The scalar value

    Returns Vector3

  • Computes the dot product of two Vector3 objects

    Returns

    The dot product of the two input vectors

    Parameters

    • v1: Vector3

      The first Vector3 object

    • v2: Vector3

      The second Vector3 object

    Returns number

  • Checks if the x,y,z components of two Vector3 objects are exactly equal.

    Returns

    A boolean value indicating if the Vector3 objects are equal

    Parameters

    • v1: Vector3

      The first Vector3 object

    • v2: Vector3

      The second Vector3 object

    Returns boolean

  • Checks if the x,y,z components of two Vector3 objects are equal within a small value of epsilon.

    Returns

    A boolean value indicating if the Vector3 objects are equal

    Parameters

    • v1: Vector3

      The first Vector3 object

    • v2: Vector3

      The second Vector3 object

    • epsilon: number = MathUtils.EPSILON

      A small value of acceptable variance to account for numerical instability

    Returns boolean

  • Linearly interpolates between two Vector3 objects

    Returns

    A new Vector3 object that represents the result of interpolating between v1 and v2

    Parameters

    • v1: Vector3

      The first Vector3 object

    • v2: Vector3

      The second Vector3 object

    • alpha: number

      The interpolation value between 0 and 1

    Returns Vector3

  • Multiply two Vector3 objects

    Returns

    A new Vector3 object with the x, y, and z values equal to the product of the corresponding values of the input vectors

    Parameters

    • v1: Vector3

      The first Vector3 object

    • v2: Vector3

      The second Vector3 object

    Returns Vector3

  • Multiplies a Vector3 object with a scalar value

    Returns

    A new Vector3 object with the x, y, and z values multiplied by the scalar value

    Parameters

    • v: Vector3

      The Vector3 object

    • n: number

      The scalar value

    Returns Vector3

  • Normalizes a Vector3 object

    Returns

    A new Vector3 object with a magnitude of 1, pointing in the same direction as the original vector If the original vector is a zero vector, returns a new zero vector

    Parameters

    Returns Vector3

  • Subtracts two Vector3 objects

    Returns

    A new Vector3 object with the x, y, and z values equal to the difference of the corresponding values of the input vectors

    Parameters

    • v1: Vector3

      The Vector3 object to subtract from

    • v2: Vector3

      The Vector3 object to subtract

    Returns Vector3

  • Transforms a Vector3 object representing a point by a Matrix4

    Returns

    A new Vector3 object that represents the result of transforming point v by m

    Parameters

    • v: Vector3

      The Vector3 object to transform

    • m: Matrix4

      The Matrix4 object to transform the Vector3 by

    Returns Vector3

  • Transforms a Vector3 object representing a direction by a Matrix4, ignoring the translation component

    Returns

    A new Vector3 object that represents the result of transforming direction v by m

    Parameters

    • v: Vector3

      The Vector3 object to transform

    • m: Matrix4

      The Matrix4 object to transform the Vector3 by

    Returns Vector3

Generated using TypeDoc