Class Quaternion

This class holds a quaternion rotation. It includes routines for using the quaternion to rotate points and vectors. It includes routines for constructing many common rotations and inverting the rotation as well as accessing the underlying x,y,z,w components of the quaterion.

Most of the functions in the class are defined both as member functions that can be called on a specific instance of Quaternion 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 the quaternion itself and return void:

const Q1 = Quaternion.makeRotationX(Math.PI);
const Q2 = Quaternion.makeRotationY(Math.PI);

// save the result in Q3, leaving Q1 and Q2 unchanged
const Q3 = Quaternion.multiply(Q1, Q2);

// each call to multiply overwrites the previous contents of Q4 with the result of the multiplication
const Q4 = Quaternion.makeRotationZ(Math.PI);
Q4.multiply(Q1);
Q4.multiply(Q2);

Hierarchy

  • Quaternion

Constructors

  • Creates a new Quaternion object with zero rotation (i.e., the identity quaternion)

    Parameters

    • x: number = 0

      The x component of the Quaternion

    • y: number = 0

      The y component of the Quaternion

    • z: number = 0

      The z component of the Quaternion

    • w: number = 1

      The w component of the Quaternion

    Returns Quaternion

Properties

w: number
x: number
y: number
z: number
IDENTITY: Quaternion = ...

A static property to provide quick access to the identity quaternion (0, 0, 0, 1). (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 IDENTITY:
const q = Quaternion();
if (q.equals(Quaternion.IDENTITY)) {
console.log("q equals the identity quaternion")
}

// Dangerous use of IDENTITY!!!!
const q = Quaternion.IDENTITY; // makes q a reference to Quaternion.IDENTITY
q.makeRotationZ(Math.PI); // changes Quaternion.IDENTITY!

// Do this instead:
const q = Quaternion.makeIdentity(); // create a new identity matrix M
q.makeRotationZ(Math.PI);

Methods

  • Converts this quaternion into an equivalent 4x4 rotation matrix and returns the result.

    Returns

    A 4x4 rotation matrix representation of this Quaternion

    Returns Matrix4

  • Sets this quaternion to a rotation like that used to orient a camera. The quaternion will rotate the -Z direction (typically, the "forward" direction for objects and the default "look" direction for cameras) to point in the new "look" direction defined by the vector (targetPoint - eyePoint). The up vector is used to further constrain the rotation. The original Y direction will rotate to point, as much as possible, toward the upVector.

    Parameters

    • eyePoint: Vector3

      The position of the camera or other object being oriented

    • targetPoint: Vector3

      The point to look at

    • upVector: Vector3 = Vector3.UP

      The direction that the original +Y direction should rotate into (as closely as possible).

    Returns void

  • Multiply this quaternion with the input quaternion and save the result in this quaternion. i.e., this = this * q Quaternion multiplication is not commutative, so order matters. See premultiply() to do the multiplication in the opposite order.

    Parameters

    Returns void

  • Normalize this quaternion and save the result in this quaternion.

    Returns void

  • Premultiply this quaternion with the input quaternion and save the result in this quaternion. i.e., this = q * this. This operation is not commutative, so order matters; See multiply() to do the multiplication in the opposite order.

    Parameters

    • q: Quaternion

      The quaternion to premultiply with

    Returns void

  • Sets the x,y,z,w components of the quaternion to the given x, y, z, and w values

    Parameters

    • x: number

      The x value

    • y: number

      The y value

    • z: number

      The z value

    • w: number

      The w value

    Returns void

  • Sets the quaternion to a rotation around axis by angle radians. Note, the axis parameter must be a unit vector.

    Parameters

    • axis: Vector3

      A unit vector that describes the axis of rotation

    • angle: number

      The angle of rotation in radians

    Returns void

  • Sets the quaternion to the rotation specified by the provied Euler angles and order of rotation

    Parameters

    • x: number

      The x-axis rotation angle in radians

    • y: number

      The y-axis rotation angle in radians

    • z: number

      The z-axis rotation angle in radians

    • order: string = 'YZX'

      The order in which the rotation angles are applied (defaults to 'YZX')

    Returns void

  • Sets the quaternion to a rotation equal to the one in the provided 4x4 rotation matrix

    Parameters

    • matrix: Matrix4

      A 4x4 rotation matrix

    Returns void

  • Sets the quaternion to a rotation around the x axis

    Parameters

    • angle: number

      The angle of rotation in radians

    Returns void

  • Sets the quaternion to a rotation around the y axis

    Parameters

    • angle: number

      The angle of rotation in radians

    Returns void

  • Sets the quaternion to a rotation around the z axis

    Parameters

    • angle: number

      The angle of rotation in radians

    Returns void

  • Sets this quaternion to an interpolation between two quaternions, q1 and q2, based on the given alpha value

    Parameters

    • q1: Quaternion

      The starting quaternion

    • q2: Quaternion

      The ending quaternion

    • alpha: number

      The interpolation value (0-1)

    Returns void

  • Creates a new quaternion with a rotation like that used to orient a camera. The quaternion will rotate the -Z direction (typically, the "forward" direction for objects and the default "look" direction for cameras) to point in the new "look" direction defined by the vector (targetPoint - eyePoint). The up vector is used to further constrain the rotation. The original Y direction will rotate to point, as much as possible, toward the upVector.

    Returns

    A new Quaternion that performs the specified rotation

    Parameters

    • eyePoint: Vector3

      The position of the camera or other object being oriented

    • targetPoint: Vector3

      The point to look at

    • upVector: Vector3 = Vector3.UP

    Returns Quaternion

  • Creates a new quaternion to represent a rotation of angle radians around axis.

    Returns

    A new Quaternion

    Parameters

    • axis: Vector3

      The axis to rotate around

    • angle: number

      The angle of rotation

    Returns Quaternion

  • Creates a new quaternion from given Euler angles

    Returns

    A new Quaternion

    Parameters

    • x: number

      The x-axis rotation angle

    • y: number

      The y-axis rotation angle

    • z: number

      The z-axis rotation angle

    • order: string = 'YZX'

      The order of the rotations (defaults to 'YZX')

    Returns Quaternion

  • Creates a new quaternion with the same rotation as in the provided 4x4 transformation matrix.

    Returns

    A new Quaternion that specifies the same rotation

    Parameters

    • matrix: Matrix4

      A 4x4 transformation matrix that includes a rotation.

    Returns Quaternion

  • Creates a new Quaternion object representing a rotation around the x-axis

    Returns

    A new Quaternion object representing a rotation around the x-axis

    Parameters

    • angle: number

      The angle to rotate by around the x-axis (in radians)

    Returns Quaternion

  • Creates a new Quaternion object representing a rotation around the y-axis

    Returns

    A new Quaternion object representing a rotation around the y-axis

    Parameters

    • angle: number

      The angle to rotate by around the y-axis (in radians)

    Returns Quaternion

  • Creates a new Quaternion object representing a rotation around the z-axis

    Returns

    A new Quaternion object representing a rotation around the z-axis

    Parameters

    • angle: number

      The angle to rotate by around the z-axis (in radians)

    Returns Quaternion

  • Multiplies two Quaternion objects together and returns a new quaternion = q1 * q2 Note: multiplication of quaternions is not commutative, so order matters. See premultiply() to do the opposite order, or just switch the order of the arguments.

    Returns

    A new Quaternion object representing the product of q1 and q2

    Parameters

    Returns Quaternion

  • Premultiplies two Quaternion objects and returns a new quaternion = q2 * q1 Note: multiplication of quaternions is not commutative, so order matters. See multiply() to do the opposite order, or just switch the order of the arguments.

    Returns

    A new Quaternion object which is the result of the premultiplication of the two input Quaternion objects

    Parameters

    Returns Quaternion

Generated using TypeDoc