Skip to content

Quaternion

Represents a rotation as a unit quaternion.

Quaternion stores four components (x, y, z, w), where (x, y, z) is the vector part and w is the scalar part. Unit quaternions encode 3D rotations without the gimbal lock and order ambiguity of Euler angles, compose through multiplication, and interpolate smoothly.

Rotations assume the quaternion is normalized. Construct one from an axis and angle with FromAxisAngle, extract a Matrix4 with GetMatrix, or recover a quaternion from a rotation matrix using the matrix constructor.

Construction

Constructors


Quaternion() constexpr

Constructs a quaternion from a rotation matrix.

Recovers the rotation encoded in the upper-left 3×3 block of the matrix using the branch that maximizes numerical stability. The matrix is assumed to be a pure rotation (orthonormal, no scale or shear).

cpp
Quaternion::Quaternion(const Matrix4& m);
ParameterDescription
mInput rotation matrix.

Quaternion() constexpr

Constructs a quaternion from individual components.

cpp
Quaternion::Quaternion(float x, float y, float z, float w);
ParameterDescription
xX component of the vector part.
yY component of the vector part.
zZ component of the vector part.
wScalar component.

Properties

w float

Scalar component.

cpp
float w {1.0f};

x float

X component of the vector part.

cpp
float x {0.0f};

y float

Y component of the vector part.

cpp
float y {0.0f};

z float

Z component of the vector part.

cpp
float z {0.0f};

Functions

GetMatrix() Matrix4

Converts the quaternion into a 4×4 rotation matrix.

Assumes the quaternion is normalized.

cpp
Matrix4 Quaternion::GetMatrix() const;

Length() float

Returns the quaternion length.

cpp
float Quaternion::Length() const;

LengthSquared() float

Returns the squared quaternion length.

Useful when comparing lengths without paying the cost of a square root.

cpp
float Quaternion::LengthSquared() const;

Normalize() Quaternion&

Normalizes the quaternion in-place.

If the length is zero, the quaternion is left unchanged.

cpp
Quaternion& Quaternion::Normalize();

Quaternion::FromAxisAngle() Quaternion

Builds a quaternion from an axis and angle.

cpp
static constexpr Quaternion Quaternion::FromAxisAngle(const Vector3& axis, float angle);
ParameterDescription
axisRotation axis; normalized internally.
angleRotation angle in radians.

Quaternion::Identity() Quaternion

Returns the identity quaternion (0, 0, 0, 1).

cpp
static constexpr Quaternion Quaternion::Identity();

Conjugate() Quaternion

Returns the conjugate of a quaternion.

Negates the vector part, leaving the scalar part unchanged. For a unit quaternion the conjugate is also its inverse.

cpp
Quaternion Conjugate(const Quaternion& q);
ParameterDescription
qInput quaternion.

Dot() float

Computes the dot product of two quaternions.

cpp
float Dot(const Quaternion& a, const Quaternion& b);
ParameterDescription
aFirst quaternion.
bSecond quaternion.

Inverse() Quaternion

Returns the inverse of a quaternion.

If the quaternion has zero length, the identity quaternion is returned.

cpp
Quaternion Inverse(const Quaternion& q);
ParameterDescription
qInput quaternion.

Normalize() Quaternion

Returns a normalized copy of a quaternion.

If the input has zero length, the identity quaternion is returned.

cpp
Quaternion Normalize(const Quaternion& q);
ParameterDescription
qInput quaternion.

operator*() Quaternion

Composes two rotations using the Hamilton product.

The result applies b first, then a, mirroring matrix multiplication. Quaternion multiplication is not commutative.

cpp
Quaternion operator* (const Quaternion& a, const Quaternion& b);
ParameterDescription
aSecond rotation to apply.
bFirst rotation to apply.

operator*() Vector3

Rotates a 3D vector by a quaternion.

Assumes the quaternion is normalized.

cpp
Vector3 operator* (const Quaternion& q, const Vector3& v);
ParameterDescription
qRotation quaternion.
vVector to rotate.

operator*() Quaternion

Multiplies a quaternion by a scalar.

cpp
Quaternion operator* (const Quaternion& q, float n);
ParameterDescription
qInput quaternion.
nScalar value.

operator*() Quaternion

Multiplies a scalar by a quaternion.

cpp
Quaternion operator* (float n, const Quaternion& q);
ParameterDescription
nScalar value.
qInput quaternion.

operator+() Quaternion

Adds two quaternions component-wise.

cpp
Quaternion operator+(const Quaternion& a, const Quaternion& b);
ParameterDescription
aFirst quaternion.
bSecond quaternion.

operator-() Quaternion

Subtracts one quaternion from another component-wise.

cpp
Quaternion operator-(const Quaternion& a, const Quaternion& b);
ParameterDescription
aFirst quaternion.
bSecond quaternion.

operator-() Quaternion

Negates a quaternion.

The result represents the same rotation as the input.

cpp
Quaternion operator-(const Quaternion& q);
ParameterDescription
qInput quaternion.

Slerp() Quaternion

Spherically interpolates between two quaternions.

Interpolates along the shortest arc and falls back to normalized linear interpolation when the inputs are nearly parallel.

cpp
Quaternion Slerp(const Quaternion& a, const Quaternion& b, float f);
ParameterDescription
aStart rotation.
bEnd rotation.
fInterpolation factor in .

Released under the MIT License.