With these two things combined - the two most commonly cited reasons about why to use quaternions (using slerp and avoiding gimbal lock) - what are other reasons to use quaternions? I’m aware that there are moderate compute savings in some cases (a matrix obviously has more degrees of freedom than a rigid orientation). Are there other good reasons to deal in quaternions? There are some reasonable ideas about why not to use them. [3]
[1] https://en.wikipedia.org/wiki/Slerp#Geometric_Slerp
[2] https://www.euclideanspace.com/maths/algebra/realNormedAlgeb...
[3] http://number-none.com/product/Understanding%20Slerp,%20Then...
Certainly, but as you say it's a bit harder.
> does it make up for the general complications with using quats?
I'm sure that's context dependent, and don't have much of an opinion which way it's likely to break in general. I was just throwing something in the "pro" column that hadn't yet been discussed.
But composing, interpolating, exponentiating, etc. is a lot nicer with quaternions. (Easier to reason about, numerically better behaved, computationally cheaper.)
If you need to apply the same rotation to a large number of separate vectors, keep your rotation representation as a quaternion internally and convert to a matrix just before vector rotation.
Also, it is not really interesting what matrices or quaternions "can do". What is interesting is what we can do with them. And quaternions make a lot of things possible that are intractable or at least a huge pain with matrices.
Yes, that was my point, that what you can do with quats is a strict subset of what you can do with 4x4 mats. Not sure what you thought I meant.
> quaternions make a lot of things possible that are intractable
Such as?
How else would you compute quaternion exponentiation? I don't think there's really a dichotomy here. When you compute quaternion exponentiation, one natural way to do it (as with complex numbers) is to think of the quaternions as a real magnitude multiplied by a phase. For complex numbers, the magnitude grows according to exp(x) and the phase evolves according to cos(x)+isin(x). This just falls out of Euler's formula. If you know that the magnitude is 1, you take the exp(x) term out, and you end up with a point that moves in a circle.
The same thing applies to quaternions.
I'm aware that there are other ways to compute quaternion exponentiation, but this is just a natural way to do it, especially for people who aren't experts in numeric programming.
You don't have to stand back that far! They're really quite similar pieces of code.
quat_log() is basically a conversion to axis-angle. quat_exp() is basically a conversion from axis-angle back to quaternions. So, the quat_exp(t quat_log(x)) formula, with different names for the functions, is described as:
1. Figure out the angle between the starting and ending position, and the axis of rotation.
2. Vary the angle of rotation smoothly from t=0..1.
3. Convert back from axis-angle to an orientation.
The only funny thing here is that the axis-angle encoding of quaternions uses a magnitude which a factor of two away from the angle in radians, so you'll see the sample code you linked to (with SLERP) use variables like "halfTheta" and "cosHalfTheta", where the quat_exp() and quat_log() formulas simply won't name them that way.
In the end I think the point of learning more math is so you can see past the differences in naming and recognize when two seemingly different approaches to the same problem are really just two different sets of terminology and names for the same approach.
>To fix gimbal lock, we must avoid modelling this physical gimbal system.
> In 3D, instead of using 3 fixed angles that we multiply together to get the final rotation, we will:
> 1. Construct a quaternion that describes a rotation around whatever axis we want, and the angle to rotate by.
Said another way, are you conflating gimbal lock the physical property, with gimbal lock the common bug of creating irreversible rotations or is the bug still possible?
And just to be 100% sure, is the approach I'm using the right one: storing a quaternion instead of 3 angles, multiplying, overwriting the rotation value?
And the idea is, _that's_ how you avoid gimbal lock. It's that implementation of multiplying, and overwriting, which can be done with any system that describes and applies rotation, including Euler angles, rotation matrices, etc.
http://number-none.com/product/Understanding%20Slerp,%20Then...
And that's funny...I had the exact same experience in a job interview...which gave me the extra motivation I needed to finally write that up!
Would you mind elaborating?
``` rotationAroundX = Quaternion.fromAxisAngle(angle1, Xaxis); rotationAroundY = Quaternion.fromAxisAngle(angle2, Yaxis); rotationAroundZ = Quaternion.fromAxisAngle(angle3, Zaxis);
cubeRotation = rotationAroundX * rotationAroundY * rotationAroundZ; ```
Basically, you store 3 quaternions, representing 3 angles, and combine them to get the final rotation.
You might say "This is just quaternions emulating Euler angles!" and my answer is, sure. You can say the same about rotation matrices. There's nothing inherent about rotation matrices that makes them susceptible to gimbal lock. You can implement them as representing 3 fixed angles, thus gimbal lock, or you can implement them as accumulating rotations, thus no gimbal lock. Same is true of quaternions.
The fact that you can get gimbal lock with quaternions is a feature, not a bug. Quaternions are just one way to describe rotations. Gimbal lock is a natural phenomenon of certain physical rotation systems, and can be described whether you use quaternions, or matrices etc.
This is a common misconception.
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).
I wrote a short article demonstrating and clarifying this, hope it helps: https://omar-shehata.medium.com/how-to-fix-gimbal-lock-in-n-...