🔄 Quaternion Rotation
About this simulation
A quaternion is a four-component number q = w + xi + yj + zk that encodes a 3D rotation as an axis and an angle in a single compact package. Unlike Euler angles (pitch, yaw, roll), quaternions never suffer from gimbal lock — the loss of a degree of freedom that happens when two rotation axes line up. Quaternions also interpolate smoothly between orientations, which is why they are the standard rotation representation in 3D graphics, robotics, aerospace and game engines.
How it works
- The gizmo (coloured cube with axis arrows) is rotated by directly setting its orientation quaternion.
- The Axis X/Y/Z sliders define a rotation axis, which is automatically normalised to unit length.
- The Angle slider sets how far to rotate around that axis, from 0° to 360°.
- “Set as Orientation A/B” store the current quaternion, and “Play Slerp” smoothly blends between them using spherical linear interpolation — the shortest path across the surface of the 4D unit sphere of rotations.
- The Gimbal-lock demo mode drives the gizmo with Euler angles instead, so you can see the coupling that happens when pitch approaches ±90°.
Key equations
q = w + xi + yj + zk, a unit quaternion with w²+x²+y²+z² = 1.
Axis-angle construction: w = cos(θ/2), (x, y, z) = sin(θ/2) · axis, where axis is a unit vector and θ is the rotation angle.
Spherical linear interpolation: slerp(q₁, q₂, t) = q₁·sin((1−t)Ω)/sin(Ω) + q₂·sin(tΩ)/sin(Ω), where Ω is the angle between q₁ and q₂ and t runs from 0 to 1.
Controls
- Mode — switch between Quaternion (axis-angle) control and the Gimbal-lock demo (Euler angles).
- Axis X/Y/Z, Angle — define the rotation axis and how far to turn around it.
- Pitch/Yaw/Roll — only active in Gimbal-lock demo mode; drive the gizmo via Euler angles.
- Set as Orientation A/B, Play Slerp — capture two orientations and animate a smooth blend between them.
- Autorotate, Reset — toggle the camera’s auto-spin or restore all sliders to their defaults.
Did you know?
Gimbal lock happens with Euler angles when the pitch axis rotates 90°, which swings the yaw and roll axes into alignment so one degree of rotational freedom is lost. This exact problem grounded Apollo 11’s guidance computer gimbals in 1969 — astronauts were warned to avoid certain attitudes. Quaternions sidestep the issue entirely because they represent rotation as a single point on a 4D sphere, with no axes that can ever become degenerate.