Skip to main content
Quaternion represents a 3D rotation as four components (x, y, z, w). Bone orientations, hitbox transforms, and any rotation that needs to compose or interpolate cleanly use this type instead of QAngle.
Identity is (0, 0, 0, 1). A unit quaternion has x² + y² + z² + w² = 1. After repeated multiplication, call normalize() to correct drift.

Overview

Creating quaternions

Identity, explicit components, array, or object forms.

Arithmetic

Hamilton product, scalar scaling, conjugate, inverse.

Rotation

Rotate vectors, interpolate between orientations.

Conversion

To and from Euler angles and axis-angle form.

Components

All four are read/write properties:

Creating quaternions

Constructor


Creates a new Quaternion. The no-argument form returns identity (0, 0, 0, 1).

Quaternion.identity


Returns a new identity quaternion (0, 0, 0, 1). Equivalent to new Quaternion() but reads better as a named factory.

Quaternion.fromAxisAngle


Builds a rotation of degrees around axis. The axis does not need to be pre-normalized.

Quaternion.fromEulerAngles


Converts a QAngle (pitch / yaw / roll in degrees) into a quaternion.

Arithmetic

All arithmetic methods return a new Quaternion - the original is never modified. Exceptions are noted inline.

add


Component-wise addition. Rarely useful on its own - quaternions compose via multiplication, not addition - but exposed for completeness.

sub


Component-wise subtraction.

mul


Quaternion × Quaternion - Hamilton product. Composes rotations: a.mul(b) applies b first, then a. Quaternion × number - scales each component by the scalar.
Quaternion multiplication is not commutative. a.mul(b) is generally different from b.mul(a).

div


Quaternion division (a * b⁻¹) or component-wise scalar division.

dot


Dot product of the four components. 1 means identical orientation, -1 means opposite hemisphere (but same rotation - quaternions are double-covers).

conjugate


Returns (-x, -y, -z, w). For a unit quaternion this equals inverse() and is faster.

inverse


Full inverse - conjugate / lengthSqr. Equals conjugate() when q is already unit-length.

normalize


Scales the quaternion to unit length. Mutates the original and returns this for chaining.

normalized


Returns a new unit-length quaternion. The original is untouched.

length


Euclidean length - √(x² + y² + z² + w²). Unit quaternions return 1.

lengthSqr


Squared length. Faster than length() when comparing magnitudes.

Rotation

rotate


Rotates vec by this quaternion. Returns a new Vector.

Quaternion.slerp


Spherical linear interpolation between a and b. Constant angular velocity - use this for smooth camera or bone interpolation.

Quaternion.lerp


Component-wise linear interpolation, then normalize. Cheaper than slerp and visually indistinguishable for small t - prefer it for per-frame smoothing. Result is normalized for you.

Conversion

toEulerAngles


Converts this quaternion to a QAngle (pitch / yaw / roll in degrees).

toAxisAngle


Returns the rotation as an axis + angle pair.

Utility

equals


Exact component-wise equality. Returns false if other is not a Quaternion.
This is exact float comparison. Two quaternions representing the same rotation may differ by floating-point noise, or by sign (both q and -q are the same rotation).

clone


Returns an independent copy.

set


Overwrites all components in place and returns this.

setIdentity


Resets this quaternion to (0, 0, 0, 1). Returns this for chaining.

toArray


Returns [x, y, z, w] as a plain array.

toString


Human-readable representation.

Common patterns