How do you rotate an object in three
dimensions? So, you take an object like that, and rotate it. That would be something that's useful, you can imagine, in computer graphics. But how are we gonna do this? Now, one of the neatest ways of doing this mathematically uses a new type of number, beyond the regular numbers you know. So this is beyond the real numbers, as we call them. They're even beyond the complex numbers—if you've heard of complex numbers before—it is even beyond that. They are called quaternions. They are fantastic! So imagine that I want to move along in one dimension. So you are living somewhere here and if I said move +5. +5 means that you move 5 units to the right along this line, so I'd end up over here. If I said instead move −3, you might understand that means move 3 units, but that time move 3 units to the left. So that's how you move in one dimension. That's fairly easy, isn't it? Let's talk about moving around in two dimensions and say I want to end up at a point over here. What that might be... 3 units this way—let's call that a 3—and I move 4 units up like that. One way you could write that as a set of instructions is to write it as 3+4i. Some of you may know what's coming here, some of you may have seen this 'i' before. But for now, let's just say 'i' means turn 90° and go up and down. For now, that's what it means. So that's one way I can travel to that point. Alternatively I could look at it this way—I could travel 3 across and 4 up, or I could rotate myself to this angle here and move along this diagonal line here— and that would be 3+4i as well. I could use the same instructions to make that point. That's how you travel in two dimensions. I mean, if I want to then go further, and travel maybe a bit further on... So let's say I wanted to go maybe 1 more across and then 2 up like that. What I can do then by adding a further 1+2i, I end up at this point, and I can say that this point is the combined result, which means I have actually traveled—what?—3 across and then a further 1, that's 4 across and... 6 up. Or, if you like, it's the combined result of going from here up to here. So we can add these numbers together, and we should think of them as numbers, right? You can add them together and you can move around in two dimensions. So this time let's rotate this line. Let's take my combined result here. What have I got? That's 4+6i. Let's rotate this 90°. If I rotate it 90°, I think it's gonna end up something like that, and it will end up here. It will end up on a point here, so this is going to be a 90° rotation. This point is actually −6+4i, and that's where I end up if I rotate. These look similar to each other. Why do they look similar? How can I get from this number to this number? What I've done actually is multiplied. Instead of adding the numbers together, I've actually multiplied this time by the number—we call it a number—the number 'i'. So there is a special rule with this. The special rule is 'i' times itself, i×i, is equal to −1. Just to show you what I did, I took where I was, 4+6i, I multiplied it by 'i', and what did I get? I got 4i+6... well, let's call it i². Oh, what is that? It's 4i−6, or in other words, where I ended up here, −6+4i. So multiplying by that number 'i' means you rotate 90°. If you do it twice, you get −1, which does make sense. So if you rotate 90°, and then rotate 90° again, you are 180° now—you're facing the opposite direction—so you're going in the negative direction, so it does make sense. And it turns out that if you multiply these numbers together, you get rotations. When you multiply the two numbers together, you add their angles together. So if I want to rotate 45°, I would get my triangle for 45°. 1/√2 there, 1/√2 here. So that's a nice nice 1 now. That is a 45° angle. Written as a number, that is
(1/√2)+(1/√2)i. So you are going that distance across and then that distance up. If I multiplied by that number, I would rotate by 45°. So adding them together makes you travel around in two dimensions, multiplying them together makes you rotate in two dimensions. Now if you're ahead of me and have seen this sort of thing before, you may recognize these as
complex numbers—which, by the way, is a terrible name, "complex" numbers, which makes them sound like "difficult" numbers, which I don't think is the intention. I think the idea is that they are "compound" numbers. They are actually two-dimensional numbers. That's a way of thinking of complex numbers. Real numbers are one-dimensional numbers, complex numbers are two-dimensional numbers, and you can use them to travel in 2-D and rotate in 2-D. So the question is, how do we rotate in 3-D? We must need three-dimensional numbers, right? So we must have to introduce an 'i' and a 'j' as well. So we have three dimensional numbers, and that's what people thought. It turns out, we don't need three-dimensional numbers, we need four-dimensional numbers. To rotate in three dimensions, bizarrely, you need a four-dimensional number. Let me show you how this works. (STAMMERS) So there was an Irish mathematician called William Hamilton, and he was thinking about this problem, and he couldn't solve it. He said, "it doesn't work in
three dimensions! I can't get it to work!" And the story goes that he was having a walk with his wife along the canal in Dublin, and then suddenly inspiration struck. He realized, "I don't need three dimensions, I need four-dimensional numbers! I need 'i', 'j', and 'k'!" So flushed with this, he immediately walked over to a nearby bridge over the canal, and he carved in the rules that he needed to make this work. These are the rules that he carved into that bridge: i² = j² = k² = i⋅j⋅k = −1. It's very similar to the complex number idea. Complex numbers, i² = −1—that's what we needed for that. It's very similar, but no, we need 'i', 'j' and 'k'. He carved this into the canal bridge,
which unfortunately—graffiti, what a vandal!—unfortunately it's not there anymore.
But now there is a plaque celebrating the rules, his discovery of quaternions. The rules are there, and every year, mathematicians around Dublin have a
pilgrimage to the spot where he had this idea for quaternions for the first time. So by multiplying these four-dimensional numbers, you can now rotate—like the 2-D version—you
can rotate things in three dimensions, and this is how things are done using computer graphics. Not only that, it's how things are worked out for the rotation or the orientation of a space shuttle going up, or it's how they work out the orientation or the rotation of your smartphone. So let's say I want to rotate an object in three dimensions. Well, I pick a line—like this pen here—I stab the object with
this pen, and then I can rotate around this pen here. So I might stab it this way, and then I can rotate the object like that. Or I might stab this way, so it rotates the object this way, or this way, so we can rotate like that. So, how do we write this? I take a line like that, and then I wanna rotate around that line. Let's do an angle, 'θ', like that. Let's call it 'v'—this line, we're calling 'v'. So it's a three-dimensional thing. It has three coordinates. Let's call them v₁, v₂, and v₃. Let's take a point that's getting rotated around. Let's call that 'P'. That's just x, y, and z, for that point, in three dimensions, this is how things work. But now we're gonna rotate it using a quaternion. What does a quaternion look like? It looks like this: A quaternion, let's call it 'h', would be something like a+bi+cj+dk, and actually, for this I do need another little thing, something called h*, and h*=a−bi−cj−dk. Right, if I want to rotate by an angle of θ, then a, b, and c are defined as: a=cos(θ/2), b is going to be written as... v₁—that's come from my vector here, that's my stabbing pen—times sin(θ/2), c=v₂⋅sin(θ/2), and d=v₃⋅sin(θ/2). So it turns out we need four components for our number that we're gonna use. We need four components to have the freedom to do the stabbing and then the rotating around that vector. And how do you work out the rotation then, finally? If P, which is the point I'm rotating, you write P as a quaternion, its coordinates are x, y, and z, so it's actually xi+yj+zk, and finally, the rotation is h⋅p⋅h*. Right, that's the main maths of it, but we need four components to our number to have the freedom to be able to rotate around a vector and then move the angle around it. So the complex numbers contain the real numbers as well, and it turns out that the quaternions contain the complex
numbers, and in turn they contain the the real numbers. When you don't need the j and the k, now you got a complex number, and if you don't need the i, you've got a real number. So you might be thinking, "what's next? Is there a next step up?" And there is, the next step up is octonions—8 dimensional numbers. Octonions, which have their own set of rules as well, are very similar, a very similar idea, but every time we go a step further, they get a bit more abstract. They lose a bit of structure, and there's a property that you lose each time you do it. So the more you do this, the less useful they become. Octonions have some uses in very abstract maths, but they're not
as useful as quaternions are. There is then a step above that, the 16
dimensional numbers—the sedenions—but again you lose another property when you go further, and each time you do they get perhaps less useful. So what are these properties that you're losing each time you go further up? Unfortunately, what you lose is when you go from the complex numbers to the quaternions, the property you lose is this...
i've watched several videos explaining quaternions, and while i feel like i understand everything they say, putting it into practice always ends up being significantly harder than understanding the concept. i need like a long series of progressively harder sample problems to work out in order to really grasp quarternions. as it stands i just dick around in unity, "what happens if i ... ok, didn't expect that".
this guy's exuberance is adorable!
This isn't nearly enough to grasp how they work. It's good to know where they come from and basically how they work, but I feel like you need a maths degree to actually start delving into them.
The only thing that this video convinces me of is that it's best to look at them as a black box, and only use an API to work with them. In Unity that means a lot of AngleAxis stuff to shuttle them to and from comprehensible representations and a few other representations that prevent me from having to know how they work at all.
Quaternions are magic, actually, and as long as I follow the euler rituals I can invoke them safely.
i remember finding the perfect video about quaternions. all the rest fell short until i saw this one. haven't found it since, to share with friends
the video was narrated over like a whiteboard sketch session. it's maybe 4 or 7 minutes long. he explains the maths by giving us a reason to why quaternions were created and how they prevent gimbal lock. he also uses very clean animations to show the correlation between a euler angle and the quaternion value, in real time. if i remember correctly he is an animator so his perspective on it was unique to all the programming examples
anyone have a clue?
I've found that the easiest way to understand quaternions is by visualizing them as normalized "look" or "orientation" vectors.
For example, if a plane is flying in the direction (x,y,z) at a rotation θ around that axis, then its rotation in quaternion form is cos(θ/2)+x sin(θ/2) i + y sin(θ/2) j + z sin(θ/2) k
Notice that the multiplications involving x, y, and z are just linearly scaling the vector.
The video did cover this approach but I felt like there was insufficient emphasis on the look vector visualization.
0 minuets found!
I didn't understand a thing. I feel like I'd need a math degree to get what he's saying or maybe I'm just stupid. I always looked at 3D as 2D inside another 2D but rotated 90°. Like bending a sheet of paper 90°. That probably makes even less sense but it makes sense to my little mind.
You don't need to know quaternion math to know how to use them
If you wanna delve into the math and gain a deep understanding of why and how they represent rotations, I recommend just start with trying to understand how complex numbers can represent 2D rotations. It's a lot easier to visualize and do the math for. Then it's not much of a step to go from that to Quaternions, which are sorta basically just higher dimensional complex numbers.
I think quaternions are like quatum physics: if you claim to understand them you don't really understand them. All you can do is learn how to use them.