# What is a quaternion?

What is a quaternion, and do how they work? Also, what advantages do you gain using three points on a 2D plane? Finally, when is it considered good practice to use quaternions?

• Also see this related question Computer Graphics SE. – Martin Ender Nov 10 '15 at 9:05
• Historically, I think quaternions came first, and later the dot and cross products were derived from quaternions. – user64554 Nov 10 '15 at 18:09
• I found this animated article very informative: acko.net/blog/animate-your-way-to-glory-pt2/#quaternions – AShelly Nov 10 '15 at 19:32
• In pure maths, I believe quaternions are 3 complex numbers such as i² = j² = k² = ijk – Vinz243 Nov 12 '15 at 18:56
• Quaternions are the best way to smoothly interpolate rotations. Just interpolating rotation matrices doesn't work, because you won't always get a rotation matrix as result. Interpolating Euler angles doesn't result in a smooth rotation. So for animating rotations, like it is needed in computer graphics or robotics, quaternions are the way to go. And there is a useful, but somehow not that often used extension, called dual quaternions that allows you to represent transformation and rotation – Tobias B Nov 13 '15 at 8:53

Mathematically, a quaternion is a complex number with 4 dimension. But in game development, Quaternions are often used to describe a rotation in 3d space by encoding:

1. a rotation axis (in form of a 3-dimensional vector)
2. how far to turn around that axis

Note that this information is encoded with sines and cosines inside the quaternion, so in general you shouldn't try to explicitly set or read the quaternion's internal components (x y z w) individually. It's easy to make a mistake that way and get a non-meaningful result. A quaternion math library will usually provide functions to operate on quaternions (eg. converting them to & from Euler angles or axis-angle), which ensures the math is correct and has the side benefit of making your code easier to read and understand.

An alternative way to describe rotations is by describing how far to turn around the 3 fixed axis' x, y, and z (aka Euler angles) which only requires 3 numbers instead of 4 and is usually more intuitive to use. However, euler-angles are subject to a problem called gimbal-lock: When you rotate 90° around one axis, the other two axis become equivalent. With quaternions, this problem does not occur.

Another way to express rotations in 3d space is with a 4x4 transformation matrix. But with a transformation matrix you can not just rotate, but also scale, translate and skew. When you want only rotation, a matrix would be overkill and a quaternion a much quicker and simpler solution.

This problem is only relevant in 3d space. In 2d space, you only have one rotation axis. Any rotation can be expressed with a single floating-point number or a single complex number, so you don't have this problem. While you can theoretically express a rotation on a 2d plane with a quaternion where the axis points into (or out of) the plane, it is usually overkill.

• gimbal lock is not a problem in quaternions if you start from quaternions and end with quaternions, the gimbal lock sets in when you have a step that converts to euler angles or back. – ratchet freak Nov 9 '15 at 17:09
• Quaternions are not axis+angle, they are 3 complex numbers and a scale. – transistor09 Nov 9 '15 at 21:37
• @transistor09 would you believe you're both right? The 3-component imaginary part of a unit quaternion can be interpreted as a unit vector along the axis of rotation, scaled by the sine of half the angle of rotation. The real part of the unit quaternion is the cosine of half the angle of rotation. So you're right that it's not exactly an angle-axis format, but it is true that the components of a quaternion can be interpreted as an axis and a (non-linear) measure of how far to turn around that axis. – DMGregory Nov 10 '15 at 0:46
• You could also mention what advantage quaternions have over a rotation matrix: they're faster to combine. When combining rotations, multiplying two quaternions requires fewer operations than multiplying matrices. – Angew is no longer proud of SO Nov 10 '15 at 7:56
• Actually, in 2D space, complex numbers are the exact analogue. Multiply a 2D point with a complex number, and you've rotated it - in fact, it's exactly the same as the usual sin/cos rotation (which should be obvious if you understand complex numbers well enough). This can be exploited a bit, but in the end, 2D graphics are not all that performance intensive today, so it doesn't give you much of an improvement unless you're really comfortable using complex numbers (which most people decidedly aren't - as evidenced by the incredibly poor quaternion-based code out there :D). – Luaan Nov 10 '15 at 9:19

Also, what advantages do you gain using three points on a 2D plane?

You don't really need quaternions if all you're interested is in rotating on the plane, i.e. about the z axis. In this case, all you need is the yaw angle, and you can exploit the fact that successive rotations about the z axis commute. So you can apply your rotations in any order you wish.

The situation is different if you're rotating on a plane that's not the XY plane. This rotation is equivalent to rotating about an arbitrary 3D axis. Now, you have two choices:

• rotate your plane in 3D so that it coincides with the XY plane and then yaw, and transform back , or

• think of your rotation as being in 3D to begin with.

The second choice is easier to code. As @Philipp said, quaternions avoid gimbal lock (if you avoid intermediate RPY or axis/angle conversions).

Finally, when is it considered good practice to use quaternions?

Whenever there are 3D rotations, it's good practice to use quaternions.

E.g.:

• In Qt. Quats make it easy to interpolate between rotations, as in the slerp function.

• ROS uses them for transforming robot poses.

• In the Bullet dynamics engine

• For a very sophisticated application, see here for their use in classical 3D mechanics.

• "Whenever there are 3D rotations, it's good practice to use quaternions." is just slightly too strong. Almost always is better; there are situations where alternatives are appropriate. (As an example of an imperfection, the nth root of a Quaternion is multi-valued) – Yakk Nov 11 '15 at 15:41
• Quaternions are a commodity to use and a pain to implement. You can get along without them if you are aware of the gimbal-lock. – Hatoru Hansou Nov 14 '15 at 23:28