How quaternions produce 3D rotation

Channel: PenguinMaths Published: 2020-07-30 1,717 words Source: auto_caption
Advanced Mathematics & Geometric Physics

Transcript

[Music] if you like math you may have heard of quaternions you might even note that they're often used as a way of representing rotation in 3d graphics like video games but why quaternions instead of something simpler like euler angles and how quaternions how does something like that produce rotation quaternions you know i once met someone who knew someone that said they're super confusing and to stay away from them they can be confusing if you're unfamiliar with them but that's true of everything hmm all right but before you start talking like a nerd can you explain what quaternions even are sure quaternions are a four dimensional extension of the complex numbers complex numbers are of the form a plus b i quaternions are of the form a plus b i plus c j plus d k where a b c d are real numbers and i j and k are the fundamental quaternion units so complex numbers can be thought of quaternions which have c and d equal to zero just like real numbers can be thought of complex numbers with b equal to zero remember how multiplying two unit complex numbers results in a pure rotation in other words multiplying two complex numbers with magnitude one results in a new complex number with magnitude one ah i see so multiplying two unit quaternions results in another unit quaternion then it's a rotation that's easy well you're partly right it does result in another unit quaternion but we are interested in making a 3d rotation quaternions are four dimensional so it's not as simple as that let's take a look at the multiplication rules of quaternions these rules don't make it immediately obvious what happens rotation wise when two quaternions are multiplied note though that any two fundamental quaternion units being multiplied together do not commute one is the negation of the other but any fundamental quaternion unit multiplying itself or a real number does commute keep this in the back of your mind it will be important later consider this diagram look familiar it's the same stereographic projection that three blue one brown used in his video on quaternions if you haven't seen it i highly recommend watching it and playing with quaternions on his and bin eaters interactive website as it will help you understand how to interpret this projection i'll place a link to those in the description now imagine left multiplying 1 minus 1 and each of the fundamental quaternion units by i it rotates the circle passing through one in i as well as rotating the perpendicular circle that passes through j and k such that one gets mapped to i and j gets matched to k let's watch that again this time only focusing on what happens to 1 and j first left multiply both by i i times 1 is just equal to i and i times j according to the definition of quaternion multiplication equals k wait a minute how do you know these multiplication rules are true you basically pulled them out of a hat it's because this is the definition of quaternion multiplication it's not that we know it's true is that we're defining it to be true then we can see what logical consequences follow from this definition william hamilton the guy who invented quaternions shows this definition because it has some nice algebraic properties now let's see what happens when we left multiply by minus i that's not too surprising it just rotates the same two circles in the opposite direction than positive i did again focusing on 1 and j we see that 1 gets mapped to minus i and j gets mapped to minus k but now let's try something a little crazy what if we write multiply by minus i well remember right multiplying only rotates the opposite direction than left multiplying when you're multiplying two different fundamental quaternion units so the circle that passes through 1 and i will be unaffected by switching to right multiplication but the circle passing through j and k will now rotate the opposite direction so now 1 gets mapped to minus i still and j gets mapped to positive k but wait what will happen if we first write multiply by minus i then left multiply the result of that by i so that our equation looks like i times v times minus i where v is some unit quaternion from the diagram gosh i don't know wait i think i get it the two circles spinning in different directions will cancel out yes the rotation of the circle passing through 1 and i canceled out while the rotation of the circle passing through j and k has doubled we have effectively created a rotation only about the y-axis ray i did it do i get a prize you earned it we can generalize this to create a pure rotation about any unit vector that is a combination of i j and k first some terminology the vector part of the quaternion is the component that lies in the i j k space whereas the real part is the component lying on the real axis a quaternion whose real part is zero is called a pure quaternion the conjugate of a quaternion q is denoted q bar and it's simply q with the vector part negated so to describe the general form of what we just did a moment ago we can write q v q conjugate where v is a pure quaternion his vector part is the 3d vector that we want to rotate and q somehow describes the angle and axis to rotate about and v does not need to be a unit platonian only a appear quaternion so don't mix those two up in the example we just did v equals j and q equals i performing the multiplication we saw that v rotates 180 degrees about the i axis so the obvious question now is how do we pick q to perform our desired rotation about any axis in any angle i think it will be helpful to find a way to visualize the 4d unit hypersphere since that's where the unit quaternions live any suggestions i think you're one dimension too low let's try this it looks like a normal sphere except the red region in the middle is actually a 3d space the ijk space which is squished down into a flat circle the axis perpendicular to that is the real axis just don't forget the red region is actually 3d and the flat appearance is just an artifact of the schematic so this is the unit quaternion with a vector part and a real part let's think back to complex numbers for a moment to specify a complex number z on the unit circle that makes some angle theta with the horizontal you can write z equals cosine theta plus i sine theta increasing theta will rotate a point around the circle passing through 1 and i but instead of multiplying sine theta by i we can multiply it by any unit vector so long as it's orthogonal to the real axis and we would still get a unit circle so let's do exactly that q equals cosine theta plus sine theta times the vector x times i plus y times j plus z times k we are multiplying sine theta by the vector part of the quaternion and in fact the whole equation describes a single quaternion whose real part is cosine theta and vector part is sine theta x i plus yj plus dk now instead of making a circle that passes through 1 and i like complex numbers we have made a circle that passes through 1 in any arbitrary point on the 3d unit sphere 3d since there are only three components in the vector part of the quaternion that is multiplying sine theta and since this point in the ijk space is orthogonal to the real axis we're still forming a unit circle assuming that x squared plus y squared plus z squared equals one also this guarantees that the whole quasternian will still have length one since cosine squared theta plus sine squared theta equals one is always true now think back to quaternion multiplication multiplying by quaternion and q will rotate two circles the one passing through one and the vector x i plus y j plus c k and the perpendicular circle to that multiplying by q conjugate will rotate both circles in the opposite direction but like before if we right multiply by q conjugate instead of left multiplying we will only negate the rotation direction of the one circle passing through one in the vector part so if we multiply qv q conjugate both opposite rotations passing through one in the vector part will cancel out while the other rotation will be doubled so the vector which we are rotating about is given by the vector part of q and the vector which we want to rotate is given by v much like complex number multiplication the angle that we want to rotate is given by theta in this expression but there's a catch since we sandwich v between q and q conjugate and end up doubling the rotation about the vector part of q we actually end up rotating v by two theta degrees a side effect of this doubling of the angles is that there are two ways to specify any orientation in 3d space using quaternions if q rotates v to a particular orientation then minus q will also rotate v to that orientation this property is especially useful for animations when you want to interpolate between two orientations it allows the programmer to choose whether to take the long path or short path to the new orientation the fact that quaternions behave so nicely when interpolating is a major reason that they're often preferred over euler angles and 3d graphics which are prone to gimbal lock when two axes align during interpolation hey are you listening inspired by quatonian multiplication i present the super double four-dimensional flip of the century try negating the quaternion a front flip might be easier