Quaternion
A quaternion is a four-dimensional number of the form a + bi + cj + dk, where a, b, c and d are real numbers and i, j and k are three distinct imaginary units. The quaternions form an associative, non-commutative normed division algebra over the real numbers, conventionally denoted by the symbol H in honour of their discoverer, the Irish mathematician William Rowan Hamilton.[1][2]
The set of unit quaternions, those whose four components give a magnitude of exactly 1, can represent any rotation in three-dimensional space. This property makes quaternions a standard tool in computer graphics, robotics, navigation and aerospace, and in virtual reality (VR) and augmented reality (AR), where they encode the orientation of head-mounted displays and controllers. Compared with Euler angles, unit quaternions avoid the singularity known as gimbal lock, are compact, and interpolate smoothly, which is why VR and AR runtimes such as OpenXR and the game engines Unity and Unreal Engine store orientation internally as quaternions.[1][3][4]
History
Hamilton spent years trying to extend complex numbers, which describe rotations and scaling in a two-dimensional plane, into three dimensions. The breakthrough came on Monday 16 October 1843 while he was walking with his wife along the towpath of the Royal Canal in Dublin. He realised that a consistent algebra required four dimensions rather than three, and that the three imaginary units had to satisfy the relations now called the fundamental formula of quaternion algebra. In his excitement he carved the formula into the stone of Brougham Bridge (also called Broom Bridge) as he passed.[5][1]
The formula Hamilton carved was:
i2 = j2 = k2 = ijk = -1
These relations imply that multiplication of the imaginary units is non-commutative, for example ij = k but ji = -k. Abandoning the commutative law was a radical step for the mathematics of the time.[1][2] In a letter written years later to his son Archibald, Hamilton described the moment: "An electric circuit seemed to close; and a spark flashed forth."[5] He requested permission to present a paper on the new numbers to the Royal Irish Academy on the same day, and read that paper on 13 November 1843.[5][1]
Quaternions were studied intensively in the 19th century but were later overshadowed for engineering use by the vector and matrix notation of Gibbs and Heaviside. Interest revived in the late 20th century, driven in large part by computer graphics and the need for a numerically efficient, singularity-free way to represent and interpolate rotations.[1]
Mathematical description
A quaternion is usually written with a scalar (real) part and a vector (imaginary) part. For q = a + bi + cj + dk, the value a is the scalar part and (b, c, d) is the vector part. In the conventions used by VR runtimes and game engines the four components are often ordered (x, y, z, w), where x, y and z are the vector part and w is the scalar part.[4][6]
A rotation by an angle θ about a unit axis u = (ux, uy, uz) is represented by the unit quaternion
q = cos(θ/2) + sin(θ/2)(uxi + uyj + uzk)
so that the scalar part encodes half the rotation angle and the vector part encodes the rotation axis.[2][7] Composing two rotations corresponds to multiplying their quaternions, an operation that combines orientations in the same way as multiplying two 3-by-3 rotation matrices but with fewer numbers to store and renormalise. A unit quaternion uses four numbers to describe an orientation, against the nine of a rotation matrix and the three of a set of Euler angles.[2][8]
The following table summarises the three common ways of representing a 3D rotation.
| Representation | Numbers stored | Gimbal lock | Smooth interpolation | Typical use |
|---|---|---|---|---|
| Euler angles (yaw, pitch, roll) | 3 | Yes, at pitch of plus or minus 90 degrees | Difficult | Human-readable input, authoring |
| Rotation matrix | 9 | No | Awkward (must re-orthonormalise) | Transforming vertices and vectors |
| Unit quaternion | 4 | No | Yes, via slerp | Storing and updating orientation |
Comparison with Euler angles and gimbal lock
Euler angles describe an orientation as three successive rotations about named axes (commonly yaw, pitch and roll). They are intuitive for humans because each value has an obvious meaning, but they suffer from gimbal lock: when the middle rotation reaches plus or minus 90 degrees, two of the three rotation axes line up and one degree of rotational freedom is lost. Near that configuration small changes in orientation can demand large, discontinuous changes in the angle values.[9][10]
Unit quaternions have no such singularity: every orientation maps to a point on the unit hypersphere in four dimensions, and the mapping is smooth everywhere. For an application that must let a user look or turn in any direction, including straight up or straight down, this absence of a special degenerate case is the main practical reason orientation is stored as a quaternion rather than as Euler angles.[9][3] Game engines commonly keep the live orientation as a quaternion and convert to Euler angles or to a rotation matrix only when needed: Euler angles for a human-readable inspector field, and a matrix when vertices are finally transformed for rendering.[3][6]
Interpolation and slerp
Animating between two orientations by linearly interpolating Euler angles produces uneven, sometimes wild motion, and interpolating the entries of two rotation matrices does not in general yield a valid rotation. The standard solution is spherical linear interpolation, or slerp, which moves along the shortest great-circle arc between two unit quaternions on the unit hypersphere at a constant angular rate.[11][12]
Slerp was introduced by Ken Shoemake in the paper "Animating Rotation with Quaternion Curves", presented at SIGGRAPH in 1985 and published in Computer Graphics, volume 19, pages 245 to 254.[13] The technique is used directly in VR and AR: a controller-attached object, a camera transition, or a remote avatar whose pose arrives only intermittently over a network is moved between known orientations with slerp so that the result is a smooth, constant-speed rotation rather than a jerk. Unreal Engine exposes slerp on its quaternion type FQuat for exactly these purposes, and Unity provides Quaternion.Slerp.[6][3]
Role in VR and AR
Tracking a headset or controller means continuously estimating its orientation, and often its position, and reporting that pose to the application. The orientation part of that pose is almost universally a unit quaternion.[8][4]
Rotational tracking and sensor fusion
The orientation of an HMD is derived from an inertial measurement unit (IMU): a tri-axis gyroscope, a tri-axis accelerometer and, in many devices, a tri-axis magnetometer. The gyroscope measures angular velocity, and integrating that angular velocity over time gives a change in orientation, which is accumulated as a quaternion. Integration alone drifts, because gyroscope noise and bias build up, so the slowly drifting estimate is corrected using the accelerometer, which senses the direction of gravity and therefore fixes pitch and roll, and the magnetometer, which senses the Earth's magnetic field and helps fix heading (yaw).[8][14] The process of combining these sensors into one orientation estimate is called sensor fusion.
Quaternions are the working representation throughout this pipeline because they make it easy to compose successive small rotations from the gyroscope and to convert to and from axis-angle form, and because they avoid the singularities that would otherwise appear when the user looks straight up or down.[8] The orientation tracker in the early Oculus Rift development kits ran its sensor-fusion update at roughly 1,000 sensor readings per second and reported the result as a unit quaternion, while still allowing the application to extract yaw, pitch and roll when convenient.[8][15]
A widely used open algorithm for this fusion is the filter published by Sebastian Madgwick in 2010, "An efficient orientation filter for inertial and inertial/magnetic sensor arrays." It uses a quaternion representation and an analytically derived gradient-descent step to combine accelerometer and magnetometer data with the integrated gyroscope, and it includes magnetic-distortion and gyroscope-bias-drift compensation. Madgwick reported that one update of the filter costs about 109 scalar arithmetic operations for the IMU case (gyroscope and accelerometer) and about 277 for the MARG case (with a magnetometer), low enough to run well at modest sampling rates.[16][17]
3DoF and 6DoF
A device that can sense only rotation provides three degrees of freedom (3DoF): yaw, pitch and roll. The orientation is held as a quaternion driven by the IMU. Inexpensive mobile headsets such as Google Cardboard, the Samsung Gear VR, the Oculus Go and Google Daydream View were 3DoF devices that tracked head rotation in this way but could not sense the user walking or leaning.[18][19] A device that also senses translation provides six degrees of freedom (6DoF). In a 6DoF system the rotational part is still a quaternion, supplied by the IMU, while the positional part (x, y, z, measured in metres) comes from optical tracking such as inside-out tracking or outside-in tracking. Drift in the rotational estimate is what makes the virtual world appear to slowly slide away from where it should be when the user holds still, and correcting it is one of the main jobs of the fusion filter.[18][14]
Runtime and engine interfaces
The cross-vendor OpenXR standard from the Khronos Group defines a pose, XrPosef, as an orientation plus a position. The orientation is an XrQuaternionf, a structure of four floats x, y, z and w that must be a unit quaternion; an OpenXR runtime is required to reject a pose whose quaternion norm deviates from unit length by more than 1 percent, returning the error XR_ERROR_POSE_INVALID. In XrPosef the rotation is always applied before the translation.[4][20] The two dominant game engines expose the same representation to developers: Unity stores every object's rotation as a Quaternion and converts to Euler angles only for display, and Unreal Engine uses the type FQuat, with components X, Y, Z (vector part) and W (scalar part), throughout its transform system.[3][6]
Dual quaternions
A dual quaternion extends the idea to encode both rotation and translation in a single algebraic object, which is convenient for rigid-body motion and for blending skeletal animation. In AR research, dual-quaternion formulations have been used to fuse IMU and camera data into a single pose estimate for mobile augmented reality, keeping rotation and translation consistent within one mathematical framework.[21]
Current status
Quaternions remain the default internal representation for 3D orientation in VR and AR as of 2026. They are written into the OpenXR specification maintained by Khronos, into the Oculus and Meta Horizon developer documentation, and into the public APIs of Unity and Unreal Engine, and they are the state variable estimated by the IMU sensor-fusion filters that run on every modern headset and tracked controller. Euler angles continue to be used at the edges of these systems for human-facing input and display, and rotation matrices are used inside the rendering pipeline, but the orientation that is stored, integrated, networked and interpolated is overwhelmingly a unit quaternion.[4][3][6][8]
References
- ↑ 1.0 1.1 1.2 1.3 1.4 1.5 "Quaternion". https://en.wikipedia.org/wiki/Quaternion.
- ↑ 2.0 2.1 2.2 2.3 Horn, Berthold K. P. (2001). "Some Notes on Unit Quaternions and Rotation". https://people.csail.mit.edu/bkph/articles/Quaternions.pdf.
- ↑ 3.0 3.1 3.2 3.3 3.4 3.5 "Quaternion and euler rotations in Unity". https://docs.unity3d.com/6000.4/Documentation/Manual/QuaternionAndEulerRotationsInUnity.html.
- ↑ 4.0 4.1 4.2 4.3 4.4 "XrQuaternionf". https://registry.khronos.org/OpenXR/specs/1.1/man/html/XrQuaternionf.html.
- ↑ 5.0 5.1 5.2 "Letters describing the Discovery of Quaternions". https://www.maths.tcd.ie/pub/HistMath/People/Hamilton/Letters/BroomeBridge.html.
- ↑ 6.0 6.1 6.2 6.3 6.4 "Implementing and Optimizing FQuat for Rotations in Unreal Engine 5". https://unrealstack.com/implementing-and-optimizing-fquat-for-rotations-in-unreal-engine-5/.
- ↑ "Quaternions and spatial rotation". https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation.
- ↑ 8.0 8.1 8.2 8.3 8.4 8.5 "Sensor Fusion: Keeping It Simple". https://developers.meta.com/horizon/blog/sensor-fusion-keeping-it-simple/.
- ↑ 9.0 9.1 "Euler and Quaternion Angles: Differences and Why it Matters". https://inertiallabs.com/euler-and-quaternion-angles-differences-and-why-it-matters/.
- ↑ "The Mathematical Cause of Gimbal Lock and How to Avoid It". 2026-03-07. https://www.compu-tools.com/blog/2026-03-07-gimbal-lock/.
- ↑ "Animating rotation with quaternion curves by Shoemake". https://history.siggraph.org/learning/animating-rotation-with-quaternion-curves-by-shoemake/.
- ↑ "slerp - Spherical linear interpolation". https://www.mathworks.com/help/nav/ref/quaternion.slerp.html.
- ↑ Shoemake, Ken (1985). "Animating rotation with quaternion curves". Proceedings of the 12th annual conference on Computer graphics and interactive techniques (SIGGRAPH '85). pp. 245-254. https://dl.acm.org/doi/10.1145/325334.325242.
- ↑ 14.0 14.1 "Quaternion Orientation Through IMU Sensor Fusion Algorithms". https://qsense-motion.com/quaternion-orientation-imu-sensor-fusion/.
- ↑ "Initialization and Sensor Enumeration". https://developer.oculus.com/documentation/native/pc/dg-sensor/.
- ↑ Madgwick, Sebastian O. H. (2010). "An efficient orientation filter for inertial and inertial/magnetic sensor arrays". https://x-io.co.uk/downloads/madgwick_internal_report.pdf.
- ↑
- Dryanovski, Ivan(2015). "Keeping a Good Attitude
- A Quaternion-Based Orientation Filter for IMUs and MARGs".{Template:Journal. 15(8)
- 19302-19330. https://www.mdpi.com/1424-8220/15/8/19302.
- ↑ 18.0 18.1 "How VR Positional Tracking Systems Work". https://www.uploadvr.com/how-vr-tracking-works/.
- ↑ "The Differences between 3DoF and 6DoF, and Why". https://digitalreality.ieee.org/publications/the-differences-between-3dof-and-6dof-and-why/.
- ↑ "XrPosef". https://registry.khronos.org/OpenXR/specs/1.1/man/html/XrPosef.html.
- ↑ "Dual quaternion based IMU and vision fusion framework for mobile augmented reality". IEEE International Conference on Multimedia and Expo (ICME). 2015. https://ieeexplore.ieee.org/document/7139178/.