Represents an immutable quaternion that extends AbstractQuaternion. Quaternions are used to represent rotations in 3D space and offer advantages over Euler angles such as avoiding gimbal lock and providing smooth interpolation.

A quaternion consists of four components: x, y, z (vector part) and w (scalar part). For unit quaternions representing rotations: q = w + xi + yj + zk where i² = j² = k² = ijk = -1

// Create identity quaternion (no rotation)
const identity = Quaternion.identity();

// Create quaternion from axis-angle representation
const axis = Vector3.fromCopy3(0, 1, 0); // Y-axis
const angle = Math.PI / 4; // 45 degrees
const rotation = Quaternion.fromAxisAngle(axis, angle);

// Multiply quaternions to combine rotations
const combined = Quaternion.multiply(rotation1, rotation2);

Hierarchy (view full)

Implements

Constructors

Properties

_v: Float32Array = ...

Internal typed array storage for quaternion components [x, y, z, w].

Accessors

  • get compositionType(): CompositionTypeClass<"VEC4">
  • Gets the composition type for this quaternion class.

    Returns CompositionTypeClass<"VEC4">

    CompositionType.Vec4 indicating this is a 4-component vector

Methods

  • Calculates the dot product between this quaternion and another quaternion. The dot product of two quaternions gives a scalar value that represents the cosine of half the angle between them when both quaternions are unit quaternions.

    Parameters

    • quat: IQuaternion

      The quaternion to compute the dot product with

    Returns number

    The dot product result

  • Creates a quaternion representing the rotation from one vector to another. This is an instance method version of the static fromToRotation.

    Parameters

    • from: IVector3

      The starting direction vector

    • to: IVector3

      The target direction vector

    Returns Quaternion

    The normalized quaternion representing the rotation

  • Checks if this quaternion is approximately equal to another quaternion within a tolerance.

    Parameters

    • quat: IQuaternion

      The quaternion to compare with

    • delta: number = Number.EPSILON

      The tolerance value (default: Number.EPSILON)

    Returns boolean

    True if all components are within the tolerance, false otherwise

  • Creates a quaternion from an axis-angle representation.

    Parameters

    • vec: IVector3

      The rotation axis (will be normalized internally)

    • radian: number

      The rotation angle in radians

    Returns Quaternion

    A quaternion representing the rotation around the given axis

  • Clamps the rotation angle of a quaternion to a maximum value. If the quaternion's rotation angle exceeds thetaMax, it scales the rotation down.

    Parameters

    • quat: IQuaternion

      The quaternion to clamp

    • thetaMax: number

      The maximum allowed rotation angle in radians

    Returns IQuaternion

    A quaternion with rotation angle clamped to thetaMax

  • Creates a quaternion from individual component values.

    Parameters

    • x: number

      The x component

    • y: number

      The y component

    • z: number

      The z component

    • w: number

      The w component

    Returns Quaternion

    A new quaternion with the specified components

  • Creates a quaternion from a variable-length array (taking first 4 elements).

    Parameters

    • array: number[]

      The array containing quaternion components

    Returns Quaternion

    A new quaternion with the first 4 components copied

  • Creates a quaternion from a 4x4 rotation matrix. Extracts the rotation component from the matrix and converts it to quaternion form.

    Parameters

    • mat: IMatrix44

      The 4x4 matrix containing rotation information

    Returns Quaternion

    A quaternion representing the same rotation as the matrix

  • Creates a quaternion from a position vector, with w component set to 0. This is useful for representing pure translations in quaternion form.

    Parameters

    Returns Quaternion

    A quaternion with xyz from the vector and w=0

  • Gets the rotation angle (0 to π) represented by a quaternion.

    Parameters

    • q: IQuaternion

      The quaternion to analyze (assumed to be normalized)

    Returns number

    The rotation angle in radians

  • Computes the inverse of a quaternion. For a unit quaternion, the inverse is the conjugate divided by the squared magnitude.

    Parameters

    Returns IQuaternion

    The inverted quaternion, or zero quaternion if input has zero length

  • Performs linear interpolation (LERP) between two quaternions. Note: LERP does not maintain constant angular velocity like SLERP.

    Parameters

    • l_quat: IQuaternion

      The starting quaternion

    • r_quat: IQuaternion

      The ending quaternion

    • ratio: number

      The interpolation parameter (0.0 = l_quat, 1.0 = r_quat)

    Returns Quaternion

    The linearly interpolated quaternion

  • Creates a quaternion that looks in the specified forward direction with a custom up vector.

    Parameters

    • forward: IVector3

      The forward direction vector

    • up: IVector3

      The up direction vector

    Returns IQuaternion

    A quaternion representing the look rotation with the specified up vector

  • Creates a quaternion that rotates from one direction to another.

    Parameters

    • fromDirection: IVector3

      The initial direction vector

    • toDirection: IVector3

      The target direction vector

    Returns IQuaternion

    A quaternion representing the rotation from fromDirection to toDirection

  • Multiplies two quaternions using Hamilton's quaternion multiplication. This combines the rotations represented by both quaternions. Note: Quaternion multiplication is not commutative (order matters).

    Parameters

    Returns Quaternion

    The product of the two quaternions

  • Performs spherical linear interpolation (SLERP) between two quaternions. SLERP provides smooth rotation interpolation that maintains constant angular velocity.

    Parameters

    • l_quat: IQuaternion

      The starting quaternion

    • r_quat: IQuaternion

      The ending quaternion

    • ratio: number

      The interpolation parameter (0.0 = l_quat, 1.0 = r_quat)

    Returns IQuaternion

    The interpolated quaternion