The reason this works is often skipped in computationally oriented writeups:<p>Rotations of 3-dimensional real space form the topological group SO(3). Naive parameterizations of that group do not form a cover [1], but the group of norm-1 quaterinons, Spin(3), does.<p>The failure of naive parameterizations, like the Euler angles, to be a cover manifests itself as gimbal lock.<p>[1] <a href="https://en.wikipedia.org/wiki/Covering_space" rel="nofollow">https://en.wikipedia.org/wiki/Covering_space</a>
This article incorrectly states that gimbal lock is a property of Euler angles, and that using quaternions prevents it.<p>This is a common misconception.<p>Euler angles can be used to rotate an object exactly the same way as quaternions do with no gimbal lock. Similarly, you can apply quaternions in such a way that gimbal lock will happen (if you wanted to represent a physical system of gimbals with quaternions, where that is a physical property).<p>I wrote a short article demonstrating and clarifying this, hope it helps: <a href="https://omar-shehata.medium.com/how-to-fix-gimbal-lock-in-n-dimensions-f2f7baec2b5e" rel="nofollow">https://omar-shehata.medium.com/how-to-fix-gimbal-lock-in-n-...</a>
Since this will get posted here anyway I’ll just get it done now. <a href="https://marctenbosch.com/quaternions/" rel="nofollow">https://marctenbosch.com/quaternions/</a><p>I don’t entirely agree with the article’s viewpoint that people do not perfectly understand quaternions and therefore they should not be used, as I get the feeling there are many parts of 3D graphics that are not perfectly understood by developers, and that’s okay.
Be aware, quaternions are not always the right solution. There's a reason Unity, Unreal, 3DSMax, Maya, Blender, etc all support Euler interpolation in animation. A simple example is an artist might want to show a clock hand spinning fast to show the progress of time. To do that they set a start angle of 0 and an end angle of say 20000. Sure, there may be ways to represent that with specialized quaternions but in general the 3D tools all seems to default to using Eulers.<p>This is an issue with the GLTF format. They chose quaternions to represent rotations in animation and as such can't easily represent what the artist's intent was.<p>You might claim you can sample the Euler animation and split it into multiple quaternion slerps but that brings up another issue which is you need support for discontinuous animations in order to handle other situations (another thing the GLTF format apparently didn't consider).
For anyone who is interested in an accessible introduction to representing rotations, I highly recommend this site: <a href="https://rotations.berkeley.edu" rel="nofollow">https://rotations.berkeley.edu</a>. One of my professors provided it for one of his courses, and it's been a really helpful reference several times since then.
I made this guide on how to implement quaternions yourself and use them to rotate objects in a 3D engine. The implementation is probably not the most efficient, but I tried to make it simple enough to understand how quaternions work.
Most of the time you don't want to use his SLERP function. You can even see what is wrong in his illustration video: the cube does 3/4s of a full rotation, while only 1/4 of a full rotation would have been sufficient. In other words, it's not always taking the shortest path between 2 rotations.<p>If you are not careful, this is what you may end up with: <a href="https://www.reddit.com/r/FIFA/comments/9gms3n/most_realistic_graphics_ever_in_fifa_history/" rel="nofollow">https://www.reddit.com/r/FIFA/comments/9gms3n/most_realistic...</a>
A few remarks:<p>0) Very nice practical introduction to quaternions and their application to rotation.<p>1) Neat didactic "textbook" implementation, but note that it is not production quality (eg potential overflow in the norm function unnecessarily). That was not the aim, either, but just something to bear in mind.<p>2) As a supplement, a useful practical reference for rotations in 3D (with good clarifications and basically all formulae you'll ever need, but no implementation) is<p><i>Representing Attitude: Euler Angles, Unit Quaternions, and Rotation Vectors</i> by James Diebel<p><a href="https://www.astro.rug.nl/software/kapteyn-beta/_downloads/attitude.pdf" rel="nofollow">https://www.astro.rug.nl/software/kapteyn-beta/_downloads/at...</a>
As a non-math person, I've thought a lot about quaternions and why they need 4 dimensions, and why there aren't 3d complex numbers. It's because if you think about it, on the complex plane, the imaginary number i just represents a rotation of 90 degrees. Now if you think about a 3d space, i represents a rotation across one dimension, and j represents a rotation across another dimension. But how do you rotate from i to j? You can't without another number k.
If you want to intuitively understand <i>why</i> this particular 4D construction is the right representation of a 3D rotation, then I highly recommend 3blue1brown's explorable interactive video series on the topic:<p><a href="https://eater.net/quaternions" rel="nofollow">https://eater.net/quaternions</a><p>The interactive videos alone are quite the technical feat, but after going through it, it's honestly hard to imagine fully understanding this topic with less technology (or with a less incredible teacher!)
As a much more intuitive version of quaternions, there's Geometric Algebra (aka Clifford Algebra). In 4D, the calculations end up being the same, but there's much more intuition and generalizability behind the Geometric version.
This is good, thanks. But a much more interesting problem I haven't seen a good writeup for is how to interpolate smoothly between quaternions at different times. Quaternion slerp has jerks (C_0 but not C_1 or C_2) at the keyframes.
Small trivia:
Existence of two unit quaternions corresponding to the same rotation is the same thing as the fact that an electron must be fully rotated twice before it has the same configuration as when it started.
Quaternion is great for dealing with 3D rotation. Another great approach is using the rotor in geometric algebra. It's pretty simple and it works on rotation in dimensions higher than 3D as well.
These implementations of difference and slerp aren't accounting for geometric double-cover. You want to do a dot-product check first to make sure you're in the same "hemisphere"
I love this. Quaternions were my nemesis while learning 3D math. I think it was the way I was taught it but quaternions always confused me as I mixed them up with euler angles. Having resources like this that explain them in detail really helps grok what quaternions are, can do, and how to incorporate them in your project. Great job! I’m over the hump now. I use dual quaternions for skinning and single q’s for rotation storage (why store 3x3m when a 4f quaternion will do?).
For a simpler discussion, see [1].<p>[1] <a href="http://wiki.secondlife.com/wiki/Rotation" rel="nofollow">http://wiki.secondlife.com/wiki/Rotation</a>
> A quaternion is basically a 4 dimensional vector, so it has a magnitude (or norm, or length)<p>Is it really a <i>vector</i> in the physical sense? People often say vector when they mean N-tuple -- for example we learned in high school that vectors are just N numbers taken together.<p>For physicists, a vector must satisfy certain transformation laws - it must transform in the correct way if a rotation is applied, and the scalar product must be invariant of the coordinate system, IIRC. I don't have enough intuition of quaternions to say how they behave under transformations, though. I would be surprized if you could have "proper" vectors with four components in three-dimensional space.
tldr: Simply explained without demonstrations:
Quaternions are hypercomplex numbers of the form<p>w + x<i>i + y</i>j + z<i>k<p>Where w, x, y, and z are real and i^2 = j^2 = k^2 = -1 and i</i>j = k, j<i>i = -k, j</i>k = i, k<i>j = -i, k</i>i = j, i<i>k = -j.<p>Being u = (x, y, z) = x</i>i + y<i>j + z</i>k a unitary vector parallel to a rotation axis, it is possible rotate any vector q with a theta arc around u by doing:<p>p<i>q</i>p'<p>where p = cos(theta/2) + sin(theta/2)<i>u and p' = cos(theta/2) - sin(theta/2)</i>u .
Guys I work at a company that uses Quaternions for rotations of physical objects. PTUs we call them (Pan Tilt Units).<p>I am telling you Quaternions have HUGE issues. These issues become much more apparent when you deal with physical objects.<p>Here's the thing Quaternions don't exist in reality. It represents an orientation of rotation but it completely masks the path took to achieve that orientation.<p>For every gimbal in reality there is an actual YawPitchRoll (YPR) that was executed to achieve that orientation. AS soon as you convert that real YPR into a Quaternion you lose the YPR that was needed to achieve that orienation.<p>So let's say I need to have one gimbal imitate the position of another gimbal. I take the YPR given to me by gimbal "A" convert the YPR to a Quat, send that Quat over the wire to Gimbal "B" and convert that Quat back to YPR to feed to the gimbal so it can rotate itself to imitate the orientation of gimbal A.<p>The quat is a higher entropy form of information. Now when converting back to YPR there are MULTIPLE YPRs that yield the same orientation. You can derive a YPR that is out of bounds of the physical gimbal.<p>Literally you can get a YPR that tells your gimbal to Yaw 190 and pitch all the way back past 90 to 170 degrees and roll 180 degrees until it's right side up. This YPR is identical to a yaw of 10, a pitch of 20 and 0 roll. Quaternions hide the original YPR, you lose information so when you receive a Quaternion it's hard to translate it into a physical realization of the orientation.<p>The company I work for doesn't realize this. They used Quaternions from day one and we have all kinds of headaches like this when we try to extract the YPR and use these orientations in the real world. Actually I should say only I have these headaches. A lot of people haven't figured out this problem yet.<p>The only time you should use Quats are if you need to transform an orientation or you're dealing with virtual objects that have no rotational limits. Everybody thinks quats are magic and better. They are not. They have huge downsides. Huge.