Advertisement
suremarc

quaternions

Aug 9th, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 13.79 KB | None | 0 0
  1. <div class="row">
  2.     <div class="inline-diff">
  3. <p>Quaternions are numbers which provide a convenient method of describing rotations in 3D space. However, they are not easy to grasp with the mind's eye because they are 4-dimensional, and unfortunately we don't have 4-dimensional eyes to see them with. In this guide I will show several ways to look at quaternions from a simpler perspective. </p>
  4. <h2 class="diff-ins"><span class="bbcode-b">Why use quaternions?</span></h2>
  5. <p>There are advantages offered by quaternions which other parameterizations of 3D rotations do not have. The most attractive feature of quaternions is that gimbal lock never occurs with them, unlike Euler angles. They are also an excellent analytical tool for the mathematically inclined, as they are an (albeit noncommutative) algebra.
  6. Keep in mind though that these benefits are not always necessary when doing rotations in 3D space. </p>
  7. <span class="bbcode-b">What NOT to use quaternions for</span><ul>
  8.   <li>Spherical linear interpolation (AKA Slerp). There are several ways to do Slerp while avoiding quaternions altogether. Stravant has an implementation <a href="http://www.roblox.com/CFrameInterpolator-Module-item?id=161739700" rel="nofollow">here</a>. </li>
  9.   <li>Low-DOF systems--if you're rotating through only two axes, then the formula CFrame.fromAxisAngle(v1, alpha)<em>(cf-cf.p)</em>CFrame.fromAxisAngle(v2, beta)+cf.p, where v1 and v2 are axes of rotation, avoids gimbal lock. For Euler angles you can use the CFrame.Angles method instead of fromAxisAngle.</li></ul>
  10. <span class="bbcode-b">What to use quaternions for</span><ul>
  11.   <li>When 3 or more independent axes of rotation are involved, matrices will always lead to gimbal lock unless one intentionally veers away from singularities in the configuration space. Quaternions, however, may traverse the entire rotation space smoothly with no discontinuities (i.e. gimbal lock).</li>
  12.   <li>Anything involving continuous, nonlinear rotary interpolation will probably involve quaternions in some form. This is because quaternions provide the only continuous parameterization of 3-dimensional rotation space. </li></ul>
  13. <p><span class="bbcode-b">Some articles to read</span>
  14. Quaternions are a difficult topic, and I recommend you have some background knowledge before taking them on. <a href="http://acko.net/blog/how-to-fold-a-julia-fractal/" rel="nofollow">This article</a> has some very nice interactive visuals describing complex numbers in a straightforward, understandable manner (plus some extra information about Julia fractals!).
  15. <a href="http://www.anticz.com/eularqua.htm" rel="nofollow">This</a> is a nice, low-level resource for Euler angles and quaternions discussing gimbal lock and how to avoid it. On the other hand, if you are brave, then <a href="https://sundaram.wordpress.com/2013/03/08/mathematical-reason-behind-gimbal-lock-in-euler-angles/" rel="nofollow">this text</a>, while slightly more technical in language, is much more comprehensive than the former.
  16. You should have read <a href="http://developer.roblox.com/forum/tutorials/11937-how-to-think-about-vectors" rel="nofollow">Trey's guide on vectors</a>. If you haven't, then please do--we're going to be talking about them a lot.</p><h2 class="diff-ins"><span class="bbcode-b">Humble beginnings</span></h2>
  17. <p>In order to look at quaternions, we'll first examine <span class="bbcode-i">complex numbers</span>, also known as <span class="bbcode-i">imaginary</span> numbers. Let's say we have a complex number <img src="http://puu.sh/g7dd5/7816cd93ab.png" width="9" height="8">. We can write it in one of two ways:<ul>
  18.     <li>Rectangular form: <img src="http://puu.sh/g7dm1/f92e672ca1.png" width="67" height="12"></li>
  19.     <li>Polar form: <img src="http://puu.sh/g7dLp/3db757d0dc.png" width="194" height="19"></li>
  20. </ul>Rectangular form is nice, because addition and subtraction behave like the usual vector operations. It is also a convenient form for multiplication, where the product is easily computed as <img src="http://puu.sh/ht96U/366c991f99.png" width="286" height="18">. But polar form gives a glimpse at the more fundamental nature of complex numbers. To illustrate -- multiplying two complex numbers adds their angles and multiplies their magnitudes:</p>
  21. <div class="lightbox-wrapper"><a href="http://upload.wikimedia.org/wikipedia/commons/0/05/ComplexMultiplication.png" class="lightbox" title="ComplexMultiplication.png" rel="nofollow"><img src="http://upload.wikimedia.org/wikipedia/commons/0/05/ComplexMultiplication.png" width="544" height="500"><div class="meta">
  22. </div></a></div>
  23. <p>It seems far-fetched to think of them as <span class="bbcode-i">numbers</span> because they don't seem to represent any sort of real-life quantity. Nevertheless, they have all the arithmetic properties of real numbers with the added benefit of being able to perform geometric transformations on the 2-dimensional plane.
  24. You can think of complex numbers as an upgrade to real numbers. And while we lose the ability to plot all numbers on a single line (one refers to this as a <span class="bbcode-i">linear ordering</span>), we gain extra functionality with the complex numbers.
  25.  
  26. For those of you who've taken a course in linear algebra, it's worth noting that complex numbers are superior to 2x2 matrices when combining rotations. While it takes 8 multiplications and 4 additions to multiply 2 rotation matrices, it only takes 4 multiplications and 2 additions via complex numbers. And while using a single parameter to represent the angle avoids these two procedures altogether, quaternions provide an alternative where no linear parameterization exists. Furthermore, the advantage in efficiency is greatly exaggerated in 3 dimensions! </p>
  27. <h2 class="diff-ins"><span class="bbcode-b">So what are quaternions?</span></h2>
  28.  
  29. <p>Quaternions can be thought of as the mutant offspring of complex numbers. They also happen to have a bit of an interesting history behind them.
  30. They were discovered by a man named William Hamilton. He wanted to extend the complex numbers into three dimensions--that is, find a "larger", number system, with one real component and <span class="bbcode-i">two</span> "imaginary" components, containing the real &amp; complex numbers. As the complex number system itself is an extension of the real numbers, it is quite natural to wonder if there exist different kinds of numbers beyond the complex horizon. </p>
  31.  
  32. <p>Hamilton wasn't able to do it in three dimensions, but surprisingly he was able to do it in <span class="bbcode-i">four</span>. A famous story has it that Hamilton was walking along a bridge with his wife when the solution suddenly occurred to him. He carved the defining equation of the quaternions into the bridge:
  33. <img src="http://puu.sh/g7qhL/f44499dbbb.png" width="165" height="16">
  34. <p>i, j, and k are referred to as the <span class="bbcode-b">basis elements</span>. You can think of them as the x, y, and z axes, respectively. Amazingly, these are the only relations needed to define the quaternions -- if one performs a series of hand-waving manipulations, it is possible to derive some additional identities:</p>
  35. <img src="http://puu.sh/g7rFI/70ddaff238.png" width="219" height="111"><br>
  36. <p>Normally we'd assume that the last statement is a contradiction, since it violates the commutative law of multiplication -- but no such restriction applies to the quaternions. An abstract argument explains why quaternions don't commute: because rotations in 3 dimensions are sensitive to the order in which they are applied, a number system that contains 3D rotations must behave the same way.
  37. Thankfully, all the other usual laws of arithmetic hold, so you just have to be a little careful when doing stuff with quaternions. Here's a multiplication table: </p>
  38. <p> <img src="http://puu.sh/g817O/31f2742cb7.png" width="131" height="220"> </p>
  39. <p>So we can see that 1 and -1 commute, while i, j, and k anticommute. Therefore a sum of these numbers will be a mixed bag of commuting elements and anticommuting elements.</p>
  40.  
  41. <p>Finally, if we use the distributive property, then we can "solve" the equation for quaternion multiplication. Let <img src="http://puu.sh/g8rj9/78343c558b.png" width="125" height="16">, and <img src="http://puu.sh/hs5SM/0aaeb160e2.png" width="11" height="12"> defined in the same fashion. Then we have
  42. <img src="http://puu.sh/hs7NQ/773442bd26.png" width="242" height="121">
  43.  
  44. In particular, this means that
  45.  
  46. <img src="http://puu.sh/g8swn/f8bef9d8ee.png" width="140" height="55">
  47. <p>The dot denotes not multiplication, but the vector dot product. If you're not familiar with the dot product, read <a href="http://developer.roblox.com/forum/tutorials/11937-how-to-think-about-vectors" rel="nofollow">How to think about vectors</a>.
  48. The significance of this is that if I take any vector of unit length and "square" it, I get -1. In other words, <span class="bbcode-i">our imaginary unit lives in 3-dimensional space:</span></p>
  49. <p> </p><div class="lightbox-wrapper"><a href="http://i.gyazo.com/361e995c52cb423d1d91fc96bf095fdf.gif" class="lightbox" title="361e995c52cb423d1d91fc96bf095fdf.gif" rel="nofollow"><img src="http://i.gyazo.com/361e995c52cb423d1d91fc96bf095fdf.gif" width="493" height="500"><div class="meta"></div></a></div>
  50. <p>If you paid careful attention, you may have noticed that the real and imaginary parts of the product from earlier are precisely the (negative) dot and cross products of the two vectors from earlier. If we rearrange the equation a little bit, then quaternions can be illuminated as <span class="bbcode-b">a transformation between two vectors:</span></p>
  51. <p> <img src="http://puu.sh/g8EuL/008551607b.png" width="209" height="111"> </p>
  52. <p> <img src="http://puu.sh/g9VMs/af55ff70ec.jpg" width="613" height="462"> </p>
  53. <p> <img src="http://puu.sh/g9VJl/dcd0e4cacd.png" width="472" height="361"> </p><h2 class="diff-ins"><span class="bbcode-b">Representing rotations with quaternions</span></h2>
  54. <p>Unfortunately for us, quaternions <span class="bbcode-i">aren't</span> 3D rotations; they rotate through 4-space. The quaternion from earlier is indeed a rotation from <img src="http://puu.sh/hs8en/a8e7662206.png" width="10" height="12"> to <img src="http://puu.sh/hs8fb/66debfb7e5.png" width="9" height="16"> , but it traverses through 4-dimensional space as it rotates. So actually, the <a href="http://puu.sh/g9VMs/af55ff70ec.jpg" rel="nofollow">image</a> I provided earlier is not a faithful representation of how quaternions truly rotate.
  55. At this point one must give up their hopes of visualizing quaternions in their entirety (your head just might explode), as humans sadly do not possess 4-dimensional eyes to see with. But as we will find out soon enough, there are in fact ways to examine quaternions in 3 dimensions. </p>
  56. <p>As I said, multiplication by a quaternion will usually will take a 3D vector into the realm of 4D space. Obviously this is not desirable. If we want to rotate in 3 dimensions, we have to use the following formula:
  57. <p> <img src="http://puu.sh/g9Yfv/8e4bae85bb.png" width="278" height="35"> </p>
  58. <p>The reasoning behind this formula involves some hardcore abstract algebra which I'd love to talk about--alas, I think my dear readers would be terrified at the prospect. We can instead look at quaternions as <span class="bbcode-i">rotors</span>, or as double reflections.
  59.  
  60. Algebraically, the equation for reflecting a vector across another vector is this:
  61.  
  62. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="http://puu.sh/g9QpG/6ea667e843.png" width="226" height="16">
  63.  
  64. Normally quaternion multiplication gets us tangled in 4-dimensional space, but the above equation always lands back in 3D space. The astute reader will know that two reflections makes a rotation -- so, combining two reflections, we arrive at the following equation:
  65.  
  66. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="http://puu.sh/g8HHA/80f5ece21f.png" width="101" height="15">
  67.  
  68. One more thing. This equation doesn't yet quite look how I want it to. Since reflection is its own inverse, swapping 'a' with its inverse doesn't change anything:
  69.  
  70. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="http://puu.sh/g8IdY/2ad8551e78.png" width="341" height="37">
  71.  
  72. The last step is not necessary, but it looks nicer and is equivalent anyhow. We say 'q' is a rotor.
  73.  
  74. There's a catch to this approach. Rotors rotate by <span class="bbcode-i">twice</span> the angle between 'a' and 'b':
  75. <div class="lightbox-wrapper"><a href="http://puu.sh/g9WPn/5fe78c955c.jpg" class="lightbox" title="5fe78c955c.jpg" rel="nofollow"><img src="http://puu.sh/g9WPn/5fe78c955c.jpg" width="515" height="500"><div class="meta">
  76. </div></a></div>
  77. So, we arrive once again at the much-anticipated rotation formula:
  78.  
  79. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="http://puu.sh/g9Yfv/8e4bae85bb.png" width="278" height="35">
  80.  
  81. The great thing about this formula is that we can combine rotations by multiplying quaternions. It takes 27 multiplications and 18 additions to multiply two rotation matrices, but with quaternions it only takes 9 multiplications and 5 additions. This can be summarized by the following tradeoff: rotation matrices are faster at rotating vectors, but slow in composition; and quaternions are slow at rotating vectors, but very fast in composition.
  82.  
  83. <h2 class="diff-ins"><span class="bbcode-b">To be continued...</span></h2>
  84. <p>More on quaternions &amp; converting to axis-angle rotations will be here, sometime later.
  85.  
  86. I know that this is a complicated subject and that not everything may make sense. I'm afraid that this will be the case for some amount of time--I've presented a lot of content here. It will take awhile to absorb all of the information.
  87. However if there is anything I can do to help you better understand, i.e. some fuzzy details I left out, please let me know in the replies. I'd be happy to answer any questions you might have
  88. <img src="https://cdn-business.discourse.org/images/emoji/twitter/slight_smile.png?v=3"> </p></div>
  89.     </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement