rhodonite
    Preparing search index...

    Class Quaternion_<T>

    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);

    Type Parameters

    Hierarchy (View Summary)

    Implements

    Index
    _v: TypedArray = ...

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

    _type: FloatTypedArrayConstructor = Float32Array

    The typed array constructor used for newly created quaternions.

    • get className(): string

      Gets the class name for debugging and reflection purposes.

      Returns string

      The string "Quaternion"

    • get w(): number

      Gets the w component (scalar part) of the quaternion.

      Returns number

      The w component value

    • get x(): number

      Gets the x component (i coefficient) of the quaternion.

      Returns number

      The x component value

    • get y(): number

      Gets the y component (j coefficient) of the quaternion.

      Returns number

      The y component value

    • get z(): number

      Gets the z component (k coefficient) of the quaternion.

      Returns number

      The z component value

    • get compositionType(): CompositionTypeClass<"VEC4">

      Gets the composition type for this quaternion class.

      Returns CompositionTypeClass<"VEC4">

      CompositionType.Vec4 indicating this is a 4-component vector

    • Gets the component at the specified index.

      Parameters

      • i: number

        The index (0=x, 1=y, 2=z, 3=w)

      Returns number

      The component value at the given index

    • Creates a deep copy of this quaternion.

      Returns this

      A new quaternion with the same component values

    • 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 any

      The normalized quaternion representing the rotation

    • Checks if this quaternion is a dummy (uninitialized) quaternion.

      Returns boolean

      True if the internal array has zero length, false otherwise

    • 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

    • Calculates the magnitude (length) of the quaternion.

      Returns number

      The Euclidean norm of the quaternion

    • Calculates the squared magnitude of the quaternion. This is more efficient than length() when you only need to compare magnitudes.

      Returns number

      The squared Euclidean norm of the quaternion

    • Converts the quaternion to a string representation.

      Returns string

      A string in the format "(x, y, z, w)"

    • Converts the quaternion to an approximately formatted string using financial precision.

      Returns string

      A formatted string with components separated by spaces and ending with newline

    • Creates a quaternion from an axis-angle representation.

      If the axis has zero length, an identity quaternion is returned instead of a quaternion filled with Infinity/NaN. Use Quaternion.tryAxisAngle when the caller needs to detect the failure.

      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, or the identity quaternion if the axis is degenerate

    • 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

    • Divides a quaternion by a scalar value. If the divisor is zero, all components are set to 0 and an error is logged instead of producing Infinity/NaN.

      Parameters

      • quat: IQuaternion

        The quaternion to divide

      • value: number

        The scalar value to divide by

      Returns Quaternion_<FloatTypedArrayConstructor>

      A new quaternion with all components divided by the scalar

    • Divides a quaternion by a scalar and stores the result in the output parameter. If the divisor is zero, all components are set to 0 and an error is logged instead of producing Infinity/NaN.

      Parameters

      • quat: IQuaternion

        The quaternion to divide

      • value: number

        The scalar value to divide by

      • out: IMutableQuaternion

        The output quaternion to store the result

      Returns IMutableQuaternion

      The output quaternion containing the divided result

    • Creates a quaternion from an axis-angle representation and stores it in the output parameter.

      Parameters

      • axis: IVector3

        The rotation axis vector

      • rad: number

        The rotation angle in radians

      • out: IMutableQuaternion

        The output quaternion to store the result

      Returns IMutableQuaternion

      The output quaternion containing the rotation

    • 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_<FloatTypedArrayConstructor>

      A new quaternion with the specified components

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

      If the rotation cannot be extracted, an identity quaternion is returned instead of a quaternion filled with Infinity/NaN. Use Quaternion.tryFromMatrix when the caller needs to detect the failure.

      Parameters

      • mat: IMatrix44

        The 4x4 matrix containing rotation information

      Returns Quaternion

      A quaternion representing the same rotation as the matrix, or the identity quaternion if the rotation cannot be extracted

    • Creates a quaternion from a 4x4 rotation matrix and stores it in the output parameter.

      If the rotation cannot be extracted, the output quaternion is set to the identity quaternion instead of being filled with Infinity/NaN. Use Quaternion.tryFromMatrixTo when the caller needs to detect the failure.

      Parameters

      Returns IMutableQuaternion

      The output quaternion containing the extracted rotation, or the identity quaternion if the rotation cannot be extracted

    • Creates a quaternion representing the rotation from one vector to another (static version).

      Parameters

      • from: IVector3

        The starting direction vector

      • to: IVector3

        The target direction vector

      Returns Quaternion

      A normalized quaternion representing the rotation

    • Creates a quaternion representing the rotation from one vector to another and stores it in the output parameter.

      Parameters

      Returns IMutableQuaternion

      The output quaternion containing the normalized rotation

    • 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) and stores the result in the output parameter.

      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)

      • out: IMutableQuaternion

        The output quaternion to store the result

      Returns IMutableQuaternion

      The output quaternion containing the interpolated result

    • Creates a quaternion that looks in the specified forward direction using default up vector (0, 1, 0).

      If the direction is degenerate (zero length), an identity quaternion is returned instead of a quaternion filled with Infinity/NaN. Use Quaternion.tryLookForward when the caller needs to detect the failure.

      Parameters

      • forward: IVector3

        The forward direction vector

      Returns Quaternion

      A quaternion representing the look rotation, or the identity quaternion if the direction is degenerate

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

      If the directions are degenerate (zero length, or forward parallel to up), an identity quaternion is returned instead of a quaternion filled with Infinity/NaN. Use Quaternion.tryLookForwardAccordingToThisUp when the caller needs to detect the failure.

      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, or the identity quaternion if the directions are degenerate

    • 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 a quaternion by a scalar and stores the result in the output parameter.

      Parameters

      • quat: IQuaternion

        The quaternion to multiply

      • value: number

        The scalar value to multiply by

      • out: IMutableQuaternion

        The output quaternion to store the result

      Returns IMutableQuaternion

      The output quaternion containing the scaled result

    • Normalizes a quaternion to unit length. A zero-length quaternion is normalized to the identity quaternion (an error is logged) instead of becoming Infinity/NaN.

      Parameters

      Returns Quaternion

      A new normalized quaternion

    • 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

    • Performs spherical linear interpolation (SLERP) and stores the result in the output parameter.

      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)

      • out: IMutableQuaternion

        The output quaternion to store the result

      Returns IMutableQuaternion

      The output quaternion containing the interpolated result

    • Attempts to create a quaternion from an axis-angle representation.

      Returns null when the axis has zero length (or contains non-finite values), because such a rotation is undefined.

      Parameters

      • vec: IVector3

        The rotation axis (will be normalized internally)

      • radian: number

        The rotation angle in radians

      Returns Quaternion | null

      A quaternion representing the rotation around the given axis, or null

    • Attempts to create 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 | null

      A quaternion representing the same rotation as the matrix, or null if the rotation cannot be extracted (zero-length basis vector, numerically singular linear part, or non-finite values)

    • Attempts to create a quaternion from a 4x4 rotation matrix and stores it in the output parameter. The output quaternion is left untouched when the rotation cannot be extracted.

      Parameters

      Returns boolean

      True if the quaternion could be extracted, false otherwise

    • Attempts to create a quaternion that looks in the specified forward direction using default up vector (0, 1, 0).

      Parameters

      • forward: IVector3

        The forward direction vector

      Returns Quaternion | null

      A quaternion representing the look rotation, or null if the direction is degenerate

    • Attempts to create a quaternion that looks in the specified forward direction with a custom up vector.

      Returns null when either direction has zero length or when the forward and up directions are parallel, because the rotation basis cannot be built.

      Parameters

      • forward: IVector3

        The forward direction vector

      • up: IVector3

        The up direction vector

      Returns Quaternion | null

      A quaternion representing the look rotation, or null