Explain the concept of "quaternions" in 3D graphics. Please what problem do quaternions solve when representing rotations? What is "gimbal lock", how are quaternions used to represent rotations, what are the advantages of using quaternions over Euler angles, and how do you interpolate between two rotations using quaternions? (SLERP).
Assistant
We are going to explain quaternions in the context of 3D graphics, focusing on the problems they solve (gimbal lock), how they represent rotations, advantages over Euler angles, and spherical linear interpolation (SLERP).
The Problem of Rotations in 3D: Gimbal Lock
In 3D graphics, we often need to represent rotations. Euler angles (pitch, yaw, roll) are intuitive but suffer from a problem called "gimbal lock". Gimbal lock occurs when two of the three rotational axes become aligned, causing a loss of one degree of freedom. For example, when the pitch is 90 degrees (pointing straight up or down), the yaw and roll axes become the same, so rotating around one of them has the same effect as rotating around the other. This makes it impossible to represent certain rotations uniquely and leads to discontinuities in interpolation.
What are Quaternions?
Quaternions are a number system extending complex numbers. A quaternion is written as:
code
q = w + xi + yj + zk
where w, x, y, z are real numbers, and i, j, k are the fundamental quaternion units with the properties:
code
i^2 = j^2 = k^2 = ijk = -1
For 3D rotations, we use unit quaternions (quaternions of norm 1). A rotation by an angle θ around a unit axis (u_x, u_y, u_z) is represented by:
q = [w, v] where w = cos(θ/2) and v = sin(θ/2)*u (with u being the unit axis vector).
How Quaternions Represent Rotations
To rotate a vector p (as a quaternion with w=0: [0, p]) by a unit quaternion q, we compute:
code
p' = q * p * q^(-1)
Since q is a unit quaternion, its inverse is its conjugate: q^{-1} = [w, -x, -y, -z].
This operation rotates the vector p by the rotation represented by q.
Advantages of Quaternions over Euler Angles
No Gimbal Lock: Quaternions represent rotations in a way that does not suffer from gimbal lock because they use four parameters (with a unit norm constraint) to represent three degrees of freedom, so there is no alignment of axes that causes loss of a degree of freedom.
Smooth Interpolation: Quaternions allow for smooth interpolation between rotations (using SLERP) without the issues of Euler angles (like non-constant angular velocity and gimbal lock).
Compact: Quaternions use 4 numbers, while rotation matrices use 9. However, note that rotation matrices are orthogonal and have constraints, so quaternions are more compact.
Concatenation: Rotations can be concatenated by multiplying quaternions, which is computationally cheaper than matrix multiplication (4x4 vs 3x3, but note that quaternion multiplication is still more expensive than component-wise operations, but overall for multiple rotations, quaternions can be more efficient due to fewer numbers).
Numerical Stability: Quaternions can be normalized easily and are less prone to drift due to floating-point errors (though they still need to be renormalized occasionally).
Interpolating Rotations: SLERP
Spherical Linear Interpolation (SLERP) is a method to interpolate between two unit quaternions q0 and q1. It provides constant angular velocity and follows the shortest arc on the 4D unit sphere.
where Ω = arccos(q0 · q1) (the dot product of the two quaternions) and t in [0,1].
However, note that if the two quaternions are almost identical (Ω ≈ 0), then we can use a simpler linear interpolation (nlerp) to avoid division by zero and because for small angles, the difference is negligible.
Steps for SLERP:
a. Compute the dot product: dot = q0.xq1.x + q0.yq1.y + q0.zq1.z + q0.wq1.w.
b. If dot < 0, then negate one of the quaternions (to take the shortest path). Because q and -q represent the same rotation, but we want the shortest interpolation.
c. If dot > 0.9995 (or some threshold), then we can use normalized linear interpolation (nlerp) because the quaternions are very close.
d. Otherwise, compute Ω = arccos(dot) and then the slerp formula.
But note that this formula assumes we have already taken care of the sign (so that the dot product is positive and the shortest path is taken).
Implementation:
code
function slerp(q0, q1, t):
dot = q0.w*q1.w + q0.x*q1.x + q0.y*q1.y + q0.z*q1.z
if dot < 0.0:
q1 = -q1 # because q and -q are the same rotation, so we flip to take the short path
dot = -dot
if dot > 0.9995:
# If the quaternions are very close, use nlerp for better performance and stability.
result = q0 * (1.0 - t) + q1 * t
return normalize(result)
theta0 = arccos(dot) # theta0 = Ω
sin_theta0 = sin(theta0)
theta = theta0 * t
sin_theta = sin(theta)
s0 = cos(theta) - dot * sin_theta / sin_theta0 # Alternatively: s0 = sin((1-t)*Ω)/sin(Ω)
s1 = sin_theta / sin_theta0
return q0 * s0 + q1 * s1
Summary
Quaternions are a powerful tool for representing rotations in 3D graphics. They avoid gimbal lock, allow smooth interpolation, and are more compact and efficient for concatenating multiple rotations. SLERP is used for interpolating between two quaternion rotations with constant angular velocity.
However, note that quaternions are not as intuitive as Euler angles for humans, and converting between Euler angles and quaternions is common in applications (like when a user inputs Euler angles).
Also, note that for some applications (like skinning in animation), quaternions are often converted to matrices for transforming vertices.
I hope this explanation is clear.
Excellent question! Quaternions are a fundamental concept in 3D graphics that solve several critical problems with rotation representations. Let me break this down systematically:
The Core Problem: Gimbal Lock
Gimbal lock occurs when using Euler angles (rotation around X, Y, Z axes) and two rotational axes become aligned, causing a loss of one degree of freedom. For example:
When an aircraft pitches up 90° (looking straight up), the yaw (left/right) and roll (bank) axes align.
At this orientation, you can't yaw left/right without also rolling, making certain rotations impossible to represent uniquely.
This creates discontinuities in interpolation and can break animations or camera controls.
Euler angles are intuitive for humans but suffer from:
Order dependency (XYZ vs. ZYX gives different results)
Non-linear interpolation (causing jerky motion)
Gimbal lock (singularities)
What Are Quaternions?
A quaternion is a 4-dimensional number system:
[
q = w + xi + yj + zk
]
For 3D rotations, we use unit quaternions (norm = 1). A rotation by angle θ around axis v (normalized) is:
[
q = \left[ \cos\frac{\theta}{2},\ \sin\frac{\theta}{2} \cdot v_x,\ \sin\frac{\theta}{2} \cdot v_y,\ \sin\frac{\theta}{2} \cdot v_z \right]
]
Often written as ((w, x, y, z)) or ([w, \mathbf{v}]) where (\mathbf{v} = (x,y,z)).
How Quaternions Represent Rotations
To rotate a vector (\mathbf{p}):
Represent (\mathbf{p}) as a "pure" quaternion: (p = [0, \mathbf{p}])
Apply rotation:
[
\mathbf{p}' = q \cdot p \cdot q^{-1}
]
For unit quaternions, (q^{-1} = q^*) (conjugate: ([w, -\mathbf{v}])).
Key insight: Quaternions encode rotations as points on a 4D unit sphere (hypersphere), avoiding the coordinate singularities of Euler angles.
Advantages Over Euler Angles
Aspect
Euler Angles
Quaternions
Gimbal lock
Yes (singularities)
No (smooth 4D representation)
Interpolation
Non-linear, discontinuous
Smooth (SLERP)
Storage
3 floats
4 floats (but 1 constraint)
Concatenation
Matrix multiplication (9 ops)
Quaternion multiply (16 ops)
Derivatives
Complex
Analytically differentiable
Inverse
Trivial (negate angles)
Conjugate (cheap)
** SLERP: Spherical Linear Interpolation**
SLERP interpolates between two unit quaternions (q_0) and (q_1) along the shortest arc of the 4D hypersphere, maintaining constant angular velocity.
Trade-offs: Less intuitive than Euler angles; slightly more expensive than matrices for single rotations but superior for chaining rotations and interpolation.
Quaternions are the industry standard for character animation, camera systems, VR/AR, and any application requiring smooth rotational transitions.
However, note that the formula for s0 and s1 above is derived from:
where the multiplication by a scalar is component-wise and then we add the two quaternions. This is valid because we are multiplying each quaternion by a scalar and then adding. However, note that this formula is for the case when the quaternions are already adjusted (by possibly negating one) so that the dot product is positive and we are taking the short path.
This is incorrect because it does not account for the non-commutativity in the sense that we are interpolating between two rotations. Actually, the correct formula is:
and then we must normalize the result? Actually, if q0 and q1 are unit quaternions, then the result is also a unit quaternion (provided we use the exact formula). But due to floating point, we might need to normalize.
is not the same as the one above because here we are multiplying q0 and q1 by scalars and then adding, but the two scalars are different. Actually, the standard SLERP formula is:
So it is a convex combination when t in [0,1] and Ω in (0, π]. But note that the quaternions are not being multiplied in the sense of quaternion multiplication, but rather we are doing a linear combination in the 4D vector space. This is correct.
However, note that q0 and q1 are unit quaternions, and the linear combination of two unit vectors (in 4D) with weights that sum to 1 does not necessarily yield a unit vector. But in the case of SLERP, it does because we are on the sphere and the weights are chosen so that the result lies on the great circle between q0 and q1. Actually, the formula above does produce a unit quaternion if the weights are exactly as above? Let me check:
code
Let u = q0 and v = q1, and let w = (1-cos(Ω))/Ω? Actually, I recall that the formula:
q(t) = (sin(Ω*(1-t)) * u + sin(Ω*t) * v) / sin(Ω)
is a unit quaternion because it is the result of moving along the great circle. So no need to normalize if we use exact arithmetic.
But in practice, due to floating point, we might renormalize.
So the implementation:
code
function slerp(q0, q1, t):
# Ensure we take the shortest path
dot = dot(q0, q1)
if dot < 0.0:
q1 = -q1
dot = -dot
if dot > 0.9995:
# Very close, so use nlerp
result = q0 * (1.0 - t) + q1 * t
return normalize(result)
theta = acos(dot)
sin_theta = sin(theta)
factor0 = sin((1.0 - t) * theta) / sin_theta
factor1 = sin(t * theta) / sin_theta
return q0 * factor0 + q1 * factor1
But note: the multiplication by a scalar is component-wise. Also, note that we are not multiplying quaternions in the usual sense, but rather we are doing a linear combination of the two quaternion vectors.
but that would be quaternion multiplication by a scalar? Actually, in the above code, we are doing:
code
q0 * factor0 -> means each component of q0 multiplied by factor0.
So it's a scalar multiplication and then vector addition.
But wait, there's another formulation that uses quaternion multiplication:
code
q(t) = q0 * (q0^{-1} * q1)^t
but that is only for constant angular velocity? Actually, that is a different approach that uses the logarithm and exponent of quaternions, which is more complex.