Quaternion Day - Spring 2021 Week 6
Transcript
now the people watching will never have your credentials is a perfect way to start the presentation for anyone watching at home quaternions suck you already knew this um yeah so i feel like the typical introduction as we go over like the origin of the idea um and then we talk about like gimbal lock and 2d 2d examples and then we talk about 3d math and then we finally get to like the good stuff that we want to learn about which is how to use quaternions um and from my own experience i kind of like decided um by the you learning most part that this was not something worth putting effort into like you get to the 3d vector math and you're like this sucks i hate this i don't want to continue this lesson um and so you once you get to the actual good stuff you're already in that mindset of quaternions suck and so this is a perfectly valid thing to feel considering like mathematical scope of them but just for like 10 minutes bear with me as i give you some cool examples of what we can do with quaternions uh yeah so instead we're just gonna look at the applications first um it's good to know your history of maths but who really cares right now we're just gonna go over how to use them you can look that up later uh 2d rotation examples are not super useful and you should probably already know what euler angles are and if you don't just google them for the next like two minutes and you'll probably understand um but yeah it's important to know what problem we're solving so we're still going to talk about gimbal lock and the 3d vector math is still kind of useful to understand the concept yeah so first the big take-home message i hope you get from this is you don't need to fully understand quaternions to use them effectively um i know you're maybe thinking dane i want to implement quaternions in my custom game engine with new physics and all that jazz that's fine good excuse but even then there's probably libraries or someone's covered it better than you can so better just leave this one to the people that study maths uh yeah and also disclaimer everything i'm about to tell you about has a lot of math going on behind the scenes that we're probably not going to get into the first big thing that you can do with quaternions is rotate things you're probably familiar with this concept already given that quaternion rotation is a thing and that's generally what they're associated with more specifically though they're rotations that are applied to vectors and even more specifically than that the rotations that maintain the magnitude of a vector and so you can sort of think about the vector coming from an origin which is that red dot there and then it has a distance and you're rotating a vector sort of within a sphere and i want you to imagine that sphere whenever we talk about rotating a vector because it is kind of rotating around that origin and because quaternions maintain the size of a vector it is always rotating within that sphere and so you can either think of it from the perspective of the vectors rotating within the sphere or there's a point on the sphere that is rotating around the what do you call it circumference maybe that's not the word for a sphere but it's rotating around the outside of the sphere yeah cool um yeah so the other big thing that they're known for is slurping um which is like the cousin of the lerp i wish these looped but they don't um and so you can kind of see here they kind of do the same thing where they're interpolating along a line but then there's a little bit of a difference between slurp and lerp when it comes to like covering the same distance yeah and these are from there if you want to check them out and so if you imagine this is the outside of the sphere you're sort of interpolating along that and we're going to see that it's kind of difficult to do that with yellow rotations and other kinds of ways of doing this you can still get the same end result from doing those but they don't really lend themselves to doing this interpolation along a sphere next yeah yeah and then another thing to keep in mind um when we're talking about quaternions is that they always take the shortest path um so we're rotating from the top of the sphere to sort of the right axis of it uh quaternions if you say move from here to here they're going to take the shortest path to get there they're not going to do some weird spirally thing around the globe they're not going to go all the way around the back they're not going to take like a route down and to the right they're always going to take the shortest path and that's all the crazy math that we're not going to get into um and you can kind of think of this like you're planning a flight around the globe you don't want to just draw a line when it's all flattened out you want to find the path along the curvature that gets you there the quickest yeah so that's another takeaways quaternions always take the shortest path um yeah and this is one of the examples i did for a project for gdd2 i think it was where we just had this menu and there was different like menu things around the scene and we just did quaternion.slurp to move around the scene when different buttons were pressed and it was super fast super easy you just move from one position to another with time um and yeah you could do this within each individual angle and say so we know at this one the rotation is facing this way we know this one the i think what is it pitch or no one of the rotations is facing that way so we can just interpolate between those but that's like yeah but each of those is like its own angle and so if you had like a more complex rotation um it'd be a lot more difficult to program that in it would also not follow the the slurp rule it would probably be a little bit more linear which as we've seen is slightly different um yeah another thing you can do with criterions is angle axis or axis angle uh people use them interchangeably pretty much um so this is you define an axis that you want to rotate around and then you move that angle around that axis so in a really simple case this would just be rotating something like around the y axis and so you would spin in place like if you were in a spinny chair you would super easy to understand um but you can also do more complex things where if it's like pointed up by like 45 degrees and then to the left by 30 you could still spin around that axis with the same amount of ease as if it was the up vector um yeah and then there's the the similar rotation thing on the on transforms and unity that's two angle axis where you can find the angle and the axis from the existing rotation of the quaternion i don't know exactly what this is useful for but i'm sure there are some uses for it um another big thing quaternions are really good for is pointing at stuff um because quaternions always take the shortest path you can fairly easily have like a camera or something that's moving with the player in a it's not just snapping to the position it needs to be in it's sort of lurping and doing a smooth movement to point at your player or something um so yeah they always take the shortest path you do slurping on them and they're continuously pointing where you want them to point which is very useful and yeah sort of a unity i don't know if unreal has this or any other game engine but there's this look rotation method that's pretty useful where if you define a forward vector and an up vector you can just say quaternion.look rotation and it'll do all that interpolation for you um let's see so yeah in this case this was the spline example that i was talking about earlier um that i did for my position um it turns out it's really easy to find the tangent on a spline and so that's sort of our forward vector at any given point and then the up vector um isn't always going to be facing directly upwards because you can see this ramp sort of points in a different direction i can't see comments but i can see them coming in so sorry about i'm not answering questions um but it sort of tries to figure out where the best up vector will be and where the best right vector will be which has been super useful for aligning things to that track and aligning things to a spline yeah and if you didn't do this there's a chance that the track would like spiral to get to where it needs to be um so yeah quaternions are always unit quaternions um as i don't think i've actually mentioned that but i think it's been at the bottom of my couple of the slides so far which means when you're applying quaternions two vectors you're always doing it through multiplication and so you're applying i think the order is you have a vector and you multiply it by a quaternion just the way the vector math works out but these also can chain together so you can have multiple quaternions multiplied by each other and it's multiple rotations that are happening and the thing to keep in mind here is that it's associative but not cumulative which hopefully some uh semblance of what that means which is just you can apply it in the same order with different uh parentheses versus like a reversed order doesn't have the same effect so if i were to like rotate my arm like maybe up and then over it'd be a little different than if i rotated it over and then up uh so this is where it gets a little bit more complicated and mathy um so if you're familiar with what are these called imaginary numbers and the concept of moving into the imaginary plane by multiplying by i um so yeah if you're in the real plane and you multiply by i and you multiply by i again you're back at negative one uh quaternions do follow a similar sort of logic in their four four numbers and so i think as you can see here there's still sort of that idea of a circle in their multiplications and there's also like much larger circles going on and everything sort of connects and rotates back onto itself which i think will become a little more clear later on um i guess this is a 2d example don't sue me and then something to think about is summation and why doesn't it work um after all you can add vectors and you can can add scalars together um but remember that all quaternions are unit quaternions and so if we add them together that would no longer be the case and they lose a bit of their meaning there i don't really have a great explanation without diving deeper into like the mathematical concepts but think of it like um like adding two vectors that are direction vectors you don't really preserve the the final direction if you do that because there is like a magnitude that is associated with that direction and so if you were to add them together it's usually not a good idea hopefully that made a little bit more sense but just don't add quaternions most game engines don't let you do it uh this is another thing quaternions are great for is converting between local and global space um so this is just a really simple thing i have in one of my projects because i had to do like a it was kind of similar to a transform but not really and so to define a forward vector you just multiply the standard forward vector by the rotation of the object super simple um and then to convert between local space and world space you would multiply by the rotation again so any position that is relative to the object if you multiply it by its position and then add if you multiply by its rotation and then add the position you can get its its local coordinates or its world coordinates um and you can sort of think about what this would look like with uh euler angles where if you would had if you had to convert between local and global space what would that even like look like like you would have to multiply by the pitch multiplied by the yaw and the roll and then you get into ordering effects and it's just a mess whereas quaternion's super simple multiplied by rotation can i actually see comments i can cool does if you try hard enough cool um i have notes here and i forgot what i was supposed to talk about here okay so yeah as i mentioned earlier i was doing this thing with um simulations and euler angle output into this uh flight tracker um and the problem was gimbal locking which i'll mention a second um so occasionally they would do really weird rotations and sort of like when the pilot would go up for like a go up and then down for a nosedive um weird things would happen when they were going down where they would start spinning even though they're not spinning in the simulation um and to solve this what we would do is we convert the uh the angles that are coming in to quaternions and then convert the current position and the current rotation that was in euler angles to a quaternion as well and that way we could just simply slurp between those two angles and it would give a nice uh smooth transition between them and so even if the simulation read out that we had rotated a full 350 degrees quaternions take the shortest path and so it would only rotate the 10 degrees and that was much cleaner than doing the full spinning thing um so yeah just taking advantage of the fact that quaternions always take the shortest path um even if the euler angles are doing crazy things yeah and this kind of brings me to a next point of why don't we use quaternions all the time i'm talking about them like there's some great mathematical genius thing um but in fact there are some limitations to them the primarily of which is that there's no winding up of the axes and that they always take the shortest path which may not be a good thing for animation and so this guy uh paul neely summarized it pretty well i thought so i just posted his quote there so if you have two key frames and you set one at let's say i think he does it one on zero and one on 360. what you would want to happen between those two frames is it to like come out and then do a full 360 degree rotation um but because quaternions are always taking the shortest path they're like 360. that's just zero what are you talking about i'm just going to stay where i am nothing's going to happen um and so yeah that wouldn't be super great if you were animating um and the other reason we don't use them all the time is that they're super easy to understand like pity on roll you can google that in two minutes and sort of like okay i understand what this means um you could probably explain it to a four-year-old and they'd be like cool i get it for the most part um unfortunately you are not four years old and so you have to learn complicated math i'm sorry um there's also the issue of representing them so i know in the unity editor you do like the position as x y z and then you do the rotation as x y z which is the euler angles and then scale as x y z so just to maintain some consistency but in fact behind the scenes it is working with quaternions any questions so far before we get into the the super duper mathy stuff i will actually open up the chat again if i can find it it's supposed to just be complaining about how you pronounced euler i know i know i said it how many times is it oiler or oiler euler you said it you said it twice the right way yeah back to back euler euler who cares quaternions are better i like euler because you can make puns with it oh that's my hot take i like quaternion because it's not the name of a mathematician wow so yeah just wanted to review real quick all quaternions unicotronians uh quaternions always take the shortest path big takeaways you don't need to understand quaternions to use them effectively like this is a pretty simple concept rotate around an axis point at a thing interpolate between two rotations like these are not super complicated things to mentally get your head around it's just the math behind them sucks for some reason and yeah always multiply and if you want to understand the math um wait no we'll go back all right i have that later cool picture on roll we're going to talk about euler angles now so if you weren't already familiar um euler angles are pitch yaw and roll and so that's three rotation matrices that you multiply together just like you multiply uh quaternion matrices um but the issue this that this um creates is a thing called gimbal lock and if you don't know what gimbal lock is you'll run into the obadiah stain i think his name is you run it the same problem but i stay in where you'll be 50 feet in the 500 feet in the air and you'll be like gimbal lock what's gimbal lock and your whole program is going to crash that bad thing to happen so it's important to understand why why we're doing quaternions um so this is a kind of nice image representation of it but essentially what's happening is that because you're multiplying them in a certain order um one of the axes of rotation will get locked in with another so the center rotation here the one in red the y gimbal is happening relative to the first and so if you were to rotate the first gimbal and so now it's aligned with another one um those would be rotating on the same axes and so if i do if i do this first rotation and now the gimbals are locked this variable here and this variable here are doing exactly the same thing and i've lost a degree of freedom and so now you can think about what would happen if i wanted to rotate around what was the y y axis like how would i point to anything that say can you see my mouse yes okay so if i have like an object here that i want to point to or look at like i can do with a quaternion how would i get the object to rotate in that direction i have to rotate the the red axis 90 degrees and then i have to rotate the green axis 90 degrees when really i just want to rotate one of the axes 90 degrees and there's also some ambiguity about which way to do that and what's the most effective way to get there um and yeah so this is kind of similar to the plain example i was describing so you get up to a um to an orientation where you're pointing straight up or straight down and all of a sudden rotating around the y axis causes you to spin even though you're not really spinning and yeah you can see this in the in the matrices as well the middle rotation is happening relative to the first and then the last rotation is happening relative to the middle um and right now you might be thinking just reorder them easy well it's the same problem you just it's the order that you apply them in always has an effect on on gimbal lock i guess so you can be you can try and be clever and avoid it but eventually it will happen um some real world exam real world examples of this are this spinning death contraption oh my god i don't know how to pause it but at a certain point all three gimbals are locked and so rotation of any single one of them um causes the same action which is why it kind of like snaps and like slows down and speeds up all of a sudden so you can see it it kind of looks like it's slowing down for a little bit and then it sort of speeds back up and then it slows down again and that's just um that's caused mostly by gibberlock even though they're moving even though each axis is moving at the same speed constantly um and then another i think this is apollo 11 or 13 one of them um whichever one had had problems with the fuel explosion while in space um when they were doing that well we're in trouble houston we have a problem situation um they were a little bit off control and so their gimbals were actually very close to locking which would have left them kind of dead in space without an orient without a way to orient themselves um yeah so this is a real world thing that also happens and you can't um as i did a little bit of research into you can't just add another gimbal because you're gonna run into the same issue of one of the axes in the middle can rotate and become the same as the outer two and yeah here's another 2d analogy that i think is a little bit more intuitive in this example you kind of have two degrees of freedom so you can either go north or south or you can go west and east but when you get to the north pole west and east have no meaning and you're only able to move north but if we were to find two new poles um say in like rochester and sydney australia we would now be able to move relative to those two new poles which is kind of what quaternions are doing when you're orienting yourself in 3d space they're kind of adding a fourth axis that you can move along so you can orient yourself if you get into this situation west and east have no meaning it is true um how much time do i have oh i have so much time cool um next we're going to talk about the math everyone's favorite part um i'm going to preface this again by saying you don't need to fully understand quaternions in order to use them effectively andrew come back you arrived at the worst time um hello cool uh if you think i'm full of and that quaternions are actually um something you want to dive into check out three blue one brown's videos he's much more qualified than i am and i spent much more time dedicated to making these concepts uh a little easier to understand so yeah check him out for sure that being said um one of the reasons i looked into was this book game engine architecture which i'm sure we've plugged at some point before um and they sort of talk about the the reasoning behind quaternions um from a game engine perspective um so if you were to do euler angles uh you need nine floating point values to represent a rotation which is excessive considering there's only three degrees of freedom which are pitchy on roll and then rotating a vector requires a vector matrix multiplication which involves three dot products or a total of nine multiplications and six additions um and ideally you'd like to do that in a little bit less expensive way um and yeah again and then point three there is to do with the slurping attributes of quaternions so ideally you'd like to be able to interpolate between two rotations and euler angles don't really let you do that in a in a neat way and yeah and so he says thankfully there's a rotation presentation representation that overcomes these three problems um it kind of looks like a 4d vector viewed this way and it behaves a lot differently as we've seen and usually write them as q x q y q z and q w um i forget if i've shown examples of them before and then yeah here's that thing about all quetonians are unit quaternions they follow this property of if you square all them up and add them together uh it equals one okay there it is that all seems super complicated and mathematically deceiving but if you were to actually look at the x y z and w values of a quaternion it it's not super complicated looking so if you have a quaternion identity it would just be 0 0 0 1 because if you could think about x y and z as being no rotation and then one is that fourth variable that has to be one because it's a it's a unit quaternion um and then if you're rotating around the x axis um it'd be negative one there and then zero zero zero because you're not rotating around any of the other axes and then the w gets set to zero because it doesn't need a value um if you're rotating 180 around y it's the same thing but y 100 around z same thing with z and if you're rotating 90 degrees around one of the axes um you sort of do that thing where it's like root two over two and so this isn't super complicated looking i think when you express them in this way and so again that's the x y z and w and they're all adding up to one when squared and yeah there's also ways to convert them between euler angles and quaternions and so sort of the notation that gets used is x y and z are a vector in their own right because they are x y and z coordinates and so you can often express quaternions in terms of a vector and a scalar with the scalar always being w and the vector being just the x y and z components and you don't need to know this thing at all so i'm sort of getting to the end of my presentation there's this very famous formula that was carved into a bridge somewhere for some reason but i say who cares we're not math students we are game developers and it's much easier to just use quaternions in a cool way than to understand all the math behind it um and yeah so what i wanted you to take away from this is you don't need to fully understand quaternions to use them effectively quaternions always take the shortest path and all quaternions are unit quaternions and hopefully by repeating that multiple times you remember it any questions why is math evil math is an evil it was just invented that way so a quaternion is like a transformation you apply to a like an is it both a transformation and an orientation like it's like the state and the thing you do to that state yeah i don't think i have an example in here but they are there is like a matrix that you apply sort of like um it is multiple you just multiply a quaternion that represents the state with a quaternion that represents like the transformation to like get the end state that's like a four by four yeah so you have you have the what is it like um you have the position vector that you would maybe multiply to get a rotation and so it would have to be at least um three tall but then quaternions i think it's actually like a four by four matrix that you use um i i wish i had put an example in here i didn't think about that but yeah essentially it's a a 4x4 matrix and i think somewhere in here it looks more like this 4x4 matrix here but there's like way more math behind it ferris boilers day off oh my god i remember uh this is just a me thing but like um a lot of physics professors like when you do uh cop physics they're just like yeah you should kind of know this uh when i had calc uh calculus physical one in my old school they're just like yeah uh first week we had to do stuff like this it was awful they they really do want you like know your your signs and coincides and quantums we have libraries for that like i i honestly don't think you need to know how quaternions work like if you can just know that you can slurp them you can do angle access stuff um you can make them point at stuff like using those concepts it's very googleable um we have to find a solution we have the technology yeah like it's not unimportant to like understand the history behind them and the actual math but we are not math professors like why no don't don't convert them please i beg of you really that all made sense i'm shocked no i can end the presentation um but yeah if you have any questions feel free to post and like miscellaneous help or something look rotation yeah i showed off um the sword game we made andrew i don't know why i'm using text chat yeah uh yeah meeting adjourned um but if you want to stick around you can also if you have a topic you want to talk about uh please hit us up and let us know because we're always looking for new topics and quaternion day only happened because we ran out of topics so got them or if there's a topic you want to hear about yeah we had other topics i pitched i pitched doing one on like jobs and then dave was like oh no you you just did a talk don't do it so we just talked about we had presentations we had presentations but no no no i don't have any good presentations i mean have you guys done one on the pixel art yet or is that like something that happened last week yeah i mean we did we did a general did we do an art one i'm just thinking about pico i definitely did one last semester i think i gave it you drew something okay i did it in conjunction with you yeah we got the tomato icon from it was that no that was the first one that was the first one that was oh geez that was something fake yet again yeah i understand that this could be less important but have you ever considered a like a story development i could do that narrative development i'm not trained in that uh i like to think i am but i'm not i mean me kaylee i think eddie could put together like a fine one too fair enough i made a a twine for my roommates for christmas that was pretty fun wine yeah i was i was being ambitious about it too i'm like i'm gonna make this twine and then i'm gonna draw all these pictures for it and like you stop recording now this is important content