In a moment, I’ll point you to a separate website hosting a short sequence of what we’re
calling “explorable videos”.
It was done in collaboration with Ben Eater, who runs an excellent channel about Computer
Engineering which viewers of this channel would definitely enjoy, and all the web development
that made these explorable videos possible is thanks to him.
I don’t want to say too much about it now, it’s really something that you have to experience
for yourself.
Certainly one of the coolest things I’ve had the pleasure of working on.
So to set the stage, last video I described quaternions, a certain 4-dimensional number
system that the 19th-century versions of Wolverine and the old man from Home Alone called “evil”
for how convoluted it seemed at the time.
And perhaps you too are wondering why on earth anyone would bother with such an alien-seeming
number system.
One of the big reasons, especially for programmers, is that they give a really nice way for describing
a 3d orientation which is not susceptible to the bugs and edge-cases of other methods.
I mean, they’re interesting mathematically for a lot of reasons, but this
is probably their biggest practical application.
To take one example, a friend of mine who used to work at Apple, Andy Matuschak, delighted
in telling me about shipping code to hundreds of millions of devices that uses quaternions
to track the phone’s model for how it’s oriented in space.
That’s right, your phone almost certainly has software running inside it that rely on
quaternions.
The thing is, there are other ways to think about and compute rotations, many of which
are way simpler to think about than quaternions.
For example, any of you familiar with linear algebra will know that 3x3 matrices can really
nicely describe 3d orientation, and a common way many programmers like to think about constructing
one of these matrices for a desired orientation is to imagine rotating an object around three
easy-to-think-about axes, where the relevant angles for these rotations are commonly called
“Euler angles”.
This mostly works, but one big problem is that it’s vulnerable to is something called
“gimbal lock”, where two of your axes of rotation get lined up and you lose a degree
of freedom.
It can also cause difficulties when trying to interpolate between two orientations.
There are many great resources online for learning about Euler angles and Gimbal lock,
and I’ve left links in the description to a few.
Not only do quaternions avoid issues like gimbal lock, they give a very seamless way
to interpolate between two three-dimensional orientations, one which lacks ambiguities
of Euler angles, and which avoids the issues of numerical precision and normalization that
arise in trying to interpolate between two rotation matrices.
To warm up to the idea of how multiplication in some higher dimensional number system might
be used to compute rotations, take a moment to remember how complex numbers give a slick
method for computing 2d rotations.
Specifically, let’s say you have some point in 2d space, like (4, 1), and you want to
know the new coordinates you’d get after rotating it 30 degrees.
Complex numbers give sort of a snazzy way to do this: Take the complex number 30 degrees
off the horizontal, a distance 1 from the origin, cos(30o) + sin(30o)i.
Now just multiply this by your point, represented as a complex number.
The only rule you need to know to carry out this computation is that i^2 = -1, and in
what might feel like a bit of magic to those first learning it, carrying out this product
from that one simple rule gives the coordinates of a new point, rotated 30 degrees away from
the original.
Using quaternions to describe 3d rotations is similar, though the look and feel is slightly
different.
Let’s say you want to rotate some angle around some axis.
First, define that axis with a unit vector, which we’ll write as having i, j and k components,
normalized so that the sum of their squares is 1.
Similar to the case of complex numbers, you use the angle to construct a quaternion by
taking cos(that angle) as the real part, plus sin(that angle) times an imaginary part, except
this time the imaginary part is the 3d axis of rotation.
Well, actually you take half that angle, which might feel totally arbitrary, but hopefully
that will make some sense by the end of this whole experience.
Now let’s say you have some 3d point, which we’ll write with ijk, components, and you
want to know the coordinates of what you’ll get by rotating this point by your specified
angle around your specified axis.
What you do is not just a single quaternion product, but a quaternion sandwich, where
you multiply by q from the left and the inverse of q from the right.
If you know how i, j and k multiply amongst themselves, you can carry out these two products
by expanding everything out, or more likely by having a computer do so.
And, in what might feel like a bit of black magic, this big computation will return for
you the rotated version of the point.
Our goal is to break down and visualize what’s happening with these two products.
I’ll review the method for thinking about quaternion multiplication described last video,
explain why half the angle is used, and why you multiply from the right by the inverse.
On the screen, and at the top of the description, is a link to the website Ben Eater setup with
the explorable videos.
It’s...it’s just really cool, Eater did something awesome here.
At the very least, you should go take a quick look, but I’d love it if you went through
the full experience.