{"id":115,"date":"2011-05-20T06:23:18","date_gmt":"2011-05-20T04:23:18","guid":{"rendered":"http:\/\/marctenbosch.com\/news\/?p=115"},"modified":"2012-12-19T01:47:36","modified_gmt":"2012-12-18T23:47:36","slug":"4d-rotations-and-the-4d-equivalent-of-quaternions","status":"publish","type":"post","link":"https:\/\/marctenbosch.com\/news\/2011\/05\/4d-rotations-and-the-4d-equivalent-of-quaternions\/","title":{"rendered":"4D Rotations and the 4D equivalent of Quaternions"},"content":{"rendered":"<p>In a game, we often want to represent rotations: the main character\u2019s head tilts left and right, the wheels of a vehicle rotate, characters follow curved paths through space. <\/p>\n<p>There are many ways to represent a rotation mathematically, and each might have different benefits when we actually transform that math into code. <strong>Quaternions <\/strong> are one such way, and they are often favored because they are easy to interpolate; this means that given two rotations, we can easily transform one into the other in a smooth way without running into any issues.<\/p>\n<p><center><img decoding=\"async\" src=\"http:\/\/marctenbosch.com\/miegakure\/data\/RotateCube.gif\" alt=\"Rotating Cube\" \/><\/center><\/p>\n<p>For example, if the main character\u2019s face is pointing down and to the right and his head is tilted sideways, it is possible to bring him back to facing straight ahead smoothly, using only the two quaternions corresponding to each orientation of the head.<\/p>\n<p><strong>This is why Quaternions are used in pretty much any 3D game engine out there.<\/strong><\/p>\n<p>You need four numbers to represent a quaternion. That might feel arbitrary, but it turns out that it&#8217;s related to the number of &#8220;different&#8221; 2D planes that can exist in 3D space.<\/p>\n<p>It\u2019s important to realize that rotations happen on 2D planes. In 3D, we usually think of rotations happening around an axis, like a wheel turning around its axle, but instead of thinking about the axle, we should think about the plane that the wheel lies on, perpendicular to the axle. Allow this old lady to demonstrate:<\/p>\n<p><center><img decoding=\"async\" src=\"http:\/\/marctenbosch.com\/miegakure\/data\/oldlady.png\" alt=\"Old Lady shows off rotations\" \/><\/center><\/p>\n<p>She is spinning wheel in the XZ plane, perpendicular to the Y axis. In addition, this is more like the 2D case where there is only one plane to rotate in. Considering rotations to happen around a third axis (perpendicular to the 2D plane) is technically incorrect, since we shouldn&#8217;t need to introduce another dimension to perform rotations.<\/p>\n<p>In any case, this is what the code for a quaternion class might look like:<\/p>\n<p><a href=\"http:\/\/marctenbosch.com\/miegakure\/data\/QuaternionCode.png\"><img decoding=\"async\" src=\"http:\/\/marctenbosch.com\/miegakure\/data\/QuaternionCode.png\" alt=\"Quaternion Source code\" \/><\/a><\/p>\n<p>You can see that, aside from the scalar part, there are three numbers (x,y, and z), one per axis, or more properly one per plane perpendicular to each axis.<\/p>\n<p><strong>That&#8217;s all well and good, but we&#8217;re making a 4D game.<\/strong><\/p>\n<p>And we need to rotate things. So what about 4D rotations? It turns out there is a mathematical entity called a Rotor which can represent a rotation in any number of dimensions. As long as we think of rotations as happening on 2D planes instead of around 1D axes, everything works out fine. We just need to count the &#8220;different&#8221; 2D planes that can exist in 4D space. <a href=\"http:\/\/en.wikipedia.org\/wiki\/Planes_of_rotation\" title=\"Wikipedia: Planes of rotation\">There are 6.<\/a> <\/p>\n<p>This is what the class definition for a 4D Rotor looks like:<\/p>\n<p><a href=\"http:\/\/marctenbosch.com\/miegakure\/data\/RotorCode.png\"><img decoding=\"async\" src=\"http:\/\/marctenbosch.com\/miegakure\/data\/RotorCode.png\" alt=\"Rotor Source code\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In a game, we often want to represent rotations: the main character\u2019s head tilts left and right, the wheels of a vehicle rotate, characters follow curved paths through space. There are many ways to represent a rotation mathematically, and each might have different benefits when we actually transform that math into code. Quaternions are one [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,6],"tags":[],"class_list":["post-115","post","type-post","status-publish","format-standard","hentry","category-miegakure","category-tech"],"_links":{"self":[{"href":"https:\/\/marctenbosch.com\/news\/wp-json\/wp\/v2\/posts\/115","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/marctenbosch.com\/news\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/marctenbosch.com\/news\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/marctenbosch.com\/news\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/marctenbosch.com\/news\/wp-json\/wp\/v2\/comments?post=115"}],"version-history":[{"count":67,"href":"https:\/\/marctenbosch.com\/news\/wp-json\/wp\/v2\/posts\/115\/revisions"}],"predecessor-version":[{"id":453,"href":"https:\/\/marctenbosch.com\/news\/wp-json\/wp\/v2\/posts\/115\/revisions\/453"}],"wp:attachment":[{"href":"https:\/\/marctenbosch.com\/news\/wp-json\/wp\/v2\/media?parent=115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/marctenbosch.com\/news\/wp-json\/wp\/v2\/categories?post=115"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/marctenbosch.com\/news\/wp-json\/wp\/v2\/tags?post=115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}