Class Vector3

The Vector3 implements a fixed-size three-dimensional vector. The class follows the 'copy-on-write' pattern, which means that every operation such as Vector3.normalized or Vector3.add returns a copy of the vector with updates values. It also implies that the values of the vector cannot be directly modified.

The concept of immutability improves sharing of objects as all types in TypeScript are assigned by identity, instead of by-value (e.g. a copy) as in other programming languages like C.

Constructors

  • Parameters

    • Optionalx: number
    • Optionaly: number
    • Optionalz: number

    Returns Vector3

Properties

_x: number
_y: number
_z: number
one: Vector3

A Vector with all components set to one.

xAxis: Vector3

A Vector with the X component set to one and Y and Z set to zero.

yAxis: Vector3

A Vector with the Y component set to one and X and Z set to zero.

zAxis: Vector3

A Vector with the Z component set to one and X and Y set to zero.

zero: Vector3

A Vector with all components set to zero.

Accessors

  • get isZero(): boolean
  • Determines if this instance is zero length.

    Returns boolean

  • get x(): number
  • Returns number

  • set x(value): void
  • Parameters

    • value: number

    Returns void

  • get y(): number
  • Returns number

  • set y(value): void
  • Parameters

    • value: number

    Returns void

  • get z(): number
  • Returns number

  • set z(value): void
  • Parameters

    • value: number

    Returns void

Methods

  • Returns a copy of the current vector added by the given scalar.

    Parameters

    • rhs: number

    Returns Vector3

  • Adds two vectors together, a scale factor is applied to rhs before adding.

    Parameters

    Returns Vector3

  • Copies all properties defined by the prototype of Vector3 from other to this instance.

    Parameters

    Returns Vector3

  • Clones this instance and returns a new Vector3 with identical values.

    Returns Vector3

  • Calculates the cross product between this and the other vector.

    Parameters

    Returns Vector3

    Cross product between two vectors.

  • Calculates the normalized direction vector from this to position.

    Parameters

    Returns Vector3

    Directional vector from this to position.

  • Calculates the distance to position.

    Parameters

    Returns number

    Distance between the two points.

  • Returns a copy of the current vector divided by a factor.

    Parameters

    • factor: number

    Returns Vector3

  • Adds a scalar to this one and returns it.

    Parameters

    • rhs: number

    Returns Vector3

  • Adds another vector with a scale factor to this one and returns it.

    Parameters

    • rhs: Immutable<Vector3>

      The vector to add with scale applied.

    • scale: number

      The scale factor to apply to the rhs vector.

    Returns Vector3

    This operation is commonly called a multiply-add (MAD) operation. However, for clarity's sake, we call it addScaled.

  • Clears all elements of the vector to zero.

    Returns Vector3

  • Divides this vector by a factor.

    Parameters

    • factor: number

    Returns Vector3

  • Performs scalar multiplication of this vector and returns it.

    Parameters

    • factor: number

    Returns Vector3

  • Negates this instance and returns itself.

    Returns Vector3

  • Normalizes the vector.

    Returns number

  • Subtracts a scalar from this one and returns it.

    Parameters

    • rhs: number

    Returns Vector3

  • Calculates the inner product of this and the other vector.

    Parameters

    Returns number

    Inner product between the two vectors.

  • Evaluates the expression and assigns it to the vector.

    Parameters

    • fnExpression: (() => number)

      The expression to evaluate.

        • (): number
        • Returns number

    Returns Vector3

    A reference to this vector

    Make sure to read up on how Vector expressions work in Typescript.

    MathEx.eval for detailed documentation, limitations and considerations.

  • Accesses a specific component of the vector.

    Parameters

    • index: number

      Index of the component.

    Returns number

    Value of the specific component.

  • Gets the maximum element (non absolute).

    Returns number

  • Returns whether the provided vector, given a error margin, has the same vector components values as this one.

    Parameters

    • rhs: Immutable<Vector3>

      The other Vector3 to compare with.

    • Optionalepsilon: number

      Optional. Error margin within which the numbers should be considered equal.

    Returns boolean

    True if equal, false otherwise.

  • Returns whether the given vector contains the same vector components values as this one.

    Parameters

    Returns boolean

  • Computes the length of the vector.

    Returns number

  • Returns the squared length of the vector.

    Returns number

  • Returns a copy of this vector multiplied by the factor.

    Parameters

    • factor: number

    Returns Vector3

  • Gets a negated copy of this instance.

    Returns Vector3

  • Returns a normalized copy of the vector.

    Returns Vector3

  • Assigns the value to the element at the specified index.

    Parameters

    • index: number
    • value: number

    Returns void

  • Assigns the specified values.

    Parameters

    • x: number
    • y: number
    • z: number

    Returns void

  • Returns a copy of the current vector subtracted by the given vector.

    Parameters

    • rhs: number

    Returns Vector3

  • Converts this instance to an array with 3 elements.

    Returns [number, number, number]

  • Returns a string representation of an object.

    Returns string

  • Calculates the cross product between two vectors.

    Parameters

    Returns Vector3

    Cross product between two vectors.

  • Calculates the distance between two points.

    Parameters

    Returns number

    Distance between the two points.

  • Calculates the inner product of the two vectors.

    Parameters

    Returns number

    Inner product between the two vectors.

  • Compares two 3D vectors with an error margin.

    Parameters

    • lhs: Immutable<Vector3>

      First vector to compare.

    • rhs: Immutable<Vector3>

      Second vector to compare.

    • Optionalepsilon: number

      Optional. Error margin within which the numbers should be considered equal.

    Returns boolean

    True if equal, false otherwise.

  • Returns whether all the vectors components contains the same values.

    Parameters

    Returns boolean

  • Calculates the squared magnitude of the provided vector.

    Parameters

    Returns number

    Squared magnitude of the vector.

  • Linearly interpolates between two values.

    Parameters

    • a: Immutable<Vector3>

      Starting value to interpolate from.

    • b: Immutable<Vector3>

      Ending value to interpolate towards.

    • t: number

      Interpolation factor in specified range.

    Returns Vector3

    Interpolated vector.

  • Calculates the magnitude of the provided vector.

    Parameters

    Returns number

    Magnitude of the vector.

  • Returns the maximum of all the vector components as a new vector.

    Parameters

    Returns Vector3

    Vector consisting of maximum components of the first and second vector.

  • Returns the minimum of all the vector components as a new vector.

    Parameters

    Returns Vector3

    Vector consisting of minimum components of the first and second vector.

  • Calculates two vectors orthonormal to the first vector.

    Parameters

    • x: Vector3

      Normalized vector to calculate orthonormal vectors for.

    • y: Vector3

      First orthonormal vector.

    • z: Vector3

      Second orthonormal vector.

    Returns void

  • Performs Gram-Schmidt orthonormalization on the specified basis, making all the vectors normal and orthogonal to each other.

    Parameters

    • x: Vector3

      First vector to orthogonalize.

    • y: Vector3

      Second vector to orthogonalize.

    • z: Vector3

      Third vector to orthogonalize.

    Returns void