 So, what I am going to do is I am going to use something called unit quaternions as a better or perhaps the best representation of 3D rotation. So, I will use Q for quaternion it will look like a 4 dimensional vector I will give it 4 very simple coordinates A, B, C, D not going to use x, y, z just to avoid confusion with the ordinary Euclidean space that we are in, but nevertheless you can consider this as an element of R4. So, there is nothing strange going on with each of the components they are just for 4 real numbers, 4 real parameters and I am going to impose a constraint they are called unit quaternions which is going to mean that A squared plus B squared plus C squared plus D squared equals 1. Hey that is not bad that gives us how many degrees of freedom? 3. So, that is good. So, I just want to make sure that is a sanity check right just make sure we are always keeping the degrees of freedom right. What is this geometrically? What kind of shape is this? It is the equation of a yeah hypersphere or let us just say a sphere hypersphere that is right. So, it is 1 dimension higher than the 3D sphere it is a 3 dimensional surface right the ordinary sphere is a 2 dimensional surface that sits in R3, this is a 3 dimensional surface that sits in R4 right. So, you know you do not have to completely visualize that. So, the set of all Q that we are allowed to have here the ones that are unit let me even say it I will say unit quaternions is a hypersphere. In mathematics it is called S3 if you are curious. So, yes you can do it in higher dimensions you can have S n in n dimensions. It is 3 because it is a 3 dimensional surface. So, the standard sphere that you know is S2 and a circle is S1. I should point out that I am going to use this A, B, C, D representation and in unity 3D and in game engines they tend to use a different representation which is x, y, z and w. So, even without being sorted alphabetically correctly and in terms of my representation which I am going to use the one that is consistent with mathematics and most of engineering as well which is B, C, D, A. So, if you want to go back and forth between what I am doing in class and what you might see in unity and other game engines then you have to take the first element and put it at the end. I do not want to tell you how many bugs I had in my code because of this this extra change. So, it is very easy to make mistakes with this. So, it is be very careful. Very commonly you will see quaternions especially in math written like this instead of writing it as a 4D vector you have A plus B I plus C J plus D K perhaps with little hats on these, but not necessarily. So, so maybe with or without the hats. In fact, I am going to take the hats off let me sorry. So, very often you will see it written like that if I look at it with just this part A plus B I that reminds you of what complex numbers right maybe you put a J there if you are an electrical engineer because somebody used I for current right. So, and so very often people say that there is a real part of the quaternion which is the A and then three imaginary parts which are I J and K and then this leads to lots of fear and confusion among students right. So, when you deal with complex numbers and you have the real and imaginary parts those were invented because people wanted to find roots of polynomials right. The polynomials have real coefficients and if you want to find all their roots you need to invent complex numbers to have a kind of algebraic closure. This does not correspond to the algebraic closure of some kind of polynomials. So, there are some reasons for referring to these as imaginary parts because algebraically they behave similar to complex numbers and become a kind of generalization, but they do not correspond to roots of polynomials and we are not going to do things like that. So, in this class I prefer not to make a big deal out of real and imaginary I do not think that is important here. It is important for you to just think of it as four parameters it is a 4-D vector and we are going to normalize them so that they are on a sphere. So, I would say avoid this kind of you know let us say fear of something perhaps nightmarish and algebraic does happening here. If you find these things interesting go and read about it and learn about it it is quite fine I find it interesting, but we do not need to go down that path and so it is important here just to keep in mind that there are four parameters ABCD and if you treat it as a 4-D vector the length is 1. The next thing I want to tell you is how to encode a 3-D rotation using our ABCD parameters. So, here I gave you an example of how to encode rotation using exponential coordinates I just took the theta and multiplied it by the v. I am going to do some other transformation or operation to do the encoding as a quaternion. So, I am going to have v and theta and convert it to a quaternion. So, here is the thing again suppose I get v equals v 1, v 2, v 3 and I have theta right. So, I have four parameters that I am using. So, I want to go between v and theta and I am going to have this representation cosine theta over 2 that is the A part I just going to be a 4-D vector here that is the A part the B part is going to be v 1 times sine of theta over 2. So, that is the B v 2 sine theta over 2 that is the C v 3 sine theta over 2 that is the D alright. So, all I did was write cosine theta over 2 here and then for the remaining 3 components I just took the v vector and did a scalar multiplication times sine theta over 2 correct that is all I did. If I square all of these and add them up what do I get right. So, I get cosine squared theta over 2 and then these 3 well I get a bunch of sine squares to add up, but I know the length is going to be 1 because the v's when I square those and add them up they all add up to 1. So, the entire thing is going to when I square all of these is going to add up to be sine squared theta over 2 by the simple trig identity I get 1. So, this is a parameterization that puts points on this unit sphere in 4 dimensional space right. So, what I want you to get comfortable with is just being able to move back and forth between these 2. So, that when you see this a b c d vector somewhere in the code maybe you are looking at unity or you are reading the code except remember that it is x, y, z, w there, but whenever you whenever you see it you should be able to look at it directly and understand how to extract the angle theta and the axis v. So, it should be very natural for you do not have fear about imaginary parts and other kinds of strange things going on and visualization of 4 dimensional spheres and spheres in R 4 and things like that. So, 3 dimensional sphere in R 4 do not worry about that just worry about understanding that there is an axis and an angle that is encoded in here and it is provided by this simple formula. So, if you understand that let us try to go over some simple examples. So, these are some useful examples useful examples. So, what does that quaternion do if I apply this formula what is it what does it correspond to what is that? Yes. So, this is this should do this should be the identity rotation right it should not do anything. If theta equals 0 then all of these components becomes 0 b c and d and this component becomes 1 correct. So, this one means the identity rotation it is very interesting that in that case I cannot even tell what the axis was anymore right because they all became 0 does that matter? No it is the perfect case it did not rotate anyway. So, there is no axis. So, it seems like the the axis just vanishes exactly in the place where there was no rotation occurring. So, very nice feels good does not it? Let us see well let us try some other very obvious patterns here what does that correspond to? So, that is right 180 degree rotation about the x axis right because the axis here for this case must be v equals 1 0 0 right for this case. So, this is pitch am I still being consistent with my definitions pitch is rotation about x correct ok. So, pitch by pi 180 degrees well if you know that case should be able to fill in these this one is yaw by pi right because it is rotation about the y axis. See how you can look at the last 3 components and if it is simple enough you know the axis of rotation. In fact, this is just the axis of rotation, but not correctly normalized right. If you just take the last 3 components and renormalize them you will get the axis you cannot renormalize it in this case, but that is ok because there is no rotation anyway. So, the axis does not matter, but the other cases if you test for being non-zero then you can figure out the axis very easily with your eyes not even writing code all right. The last one is roll by pi right. So, these are very simple examples. So, there really should not be fear of quaternions I see a lot of fear of quaternions these it is quite simple once you understand what to look for. The first coordinate is the amount of rotation which is inverted right the smaller the number the bigger the rotation and then the last 3 coordinates are giving information about the axis of rotation. Let us try another one 1 over square root 2 1 over square root 2 0 0. So, suppose I see that what is the axis of rotation. So, again that is a perfect case where now I have to renormalize it, but it looks like it is about the x axis how far have I rotated yeah very good. So, that is good. So, you are doing you are doing great here. So, 45 or 90 90 because in the formula here we divide by 2. So, that is how some of you got fooled right. I make the mistake a lot myself. So, it is no problem. So, pitch by pi over 2. So, I should be able to very quickly go with the other cases 1 over square root 2 0 1 over square root 2 0 1 over square root 2 0 0 1 over square root 2. So, this is very nice if you are debugging your code you just want to make some simple transformations and try them out. It is very nice to be able to just immediately specify some simple transformations like this or if you are debugging and you are not sure you got the code correct. So, it is very nice to be able to to read these quickly. So, this one is yaw by pi over 2 and roll by pi over 2. Questions about that? I am going to give a little more information as we as we go along here, but I just want to make sure that is clear right. So, no questions see there means it is very clear or very confusing. So, I know hmm. So, here is something else to think about inverses and multiple representations in other words non uniqueness multiple multiple representations. So, if I have if I have a quaternion ABCD now Uniquaternion that is being used to represent a rotation I should probably write back the conversion formula. Let me remind you of this cosine theta over 2 v 1 sine theta over 2 v 2 sine theta over 2 v 3 sine theta over 2 right. So, remember that right that is how we got the ABCD components. So, what if I have minus a minus b minus c minus d can anyone tell me how the two of these are related. So, if ABCD corresponds to some 3D rotation what is minus a minus b minus c minus d going to correspond to? Is that the inverse rotation? It is the same rotation is not amazing it is the same rotation because if I take the v and I twist it in the opposite direction right I just flip it around I will negate the BCD components and then if I negate the theta as well that will negate the a, but it will not force another negation here. So, that will negate all four components. So, that means that these two are equivalent. So, that is something very interesting to pay attention to. So, that means that when we travel on this sphere right. So, 3 dimensional sphere in 4 dimensions if we go to the opposite side of it right it is called the antipodal point right the opposite point what anybody know where the opposite point on the earth is from here maybe it is a good guess I do not know it has to be in the southern hemisphere right. So, you have to change hemispheres in all cases right. So, anyway you go to the opposite point on the earth and that corresponds to the antipodal point. So, on this quaternion sphere this unit sphere of quaternions unit hypersphere of quaternions the opposite point is always equivalent is that interesting just to add an extra layer of confusion all right. Let us think about what this one does what if I have a, but I decide to negate the BC and D parts. So, I keep a the same by negate these three what does that correspond to yeah I decided not to now yeah. If I take what do I get if I do cosine minus theta it negates right or it stays the same oh it should stay the same ok I see. So, if I move the axis backwards let us see ah. So, these should stay the same right. So, let me let me think about this if I let us go back here let us go say I have v I do rotation by theta I want to now do minus v and the question is let us see I want to make this correspond to the exact same rotation see if I am going in the opposite direction yep yeah it is a very it is a difficult problem of orientation you have to visualize it we have to remain going counter clockwise right. So, if I want to continue going counter clockwise and apply the exact same rotation is it going to be theta or minus theta was that 2 pi minus theta thank you. So, it is 2 pi minus theta. So, we go in the opposite direction it is 2 pi minus theta alright. So, if I go 2 pi minus theta what sign do I get over here negative a alright good. So, let us keep we keep the trig going ok alright. So, we are fine now what if I decided to do it this way instead and I did not negate the a how are these two related. So, if I say by Euler's 3D representation theorem that every rotation corresponds to a single rotation about some axis then if I want to undo that rotation I just twist it back the other way right. So, that corresponds to the inverse does that make sense. So, if I have some axis v and I rotate it by some amount theta if I now rotate by minus theta it will just undo that rotation. So, in this case this should change the sign and I leave I yeah this should change the sign. So, I end up getting the opposite right. So, if I keep the axis the same, but I just negate the theta then I will negate these three components, but the a will end up being the same. So, here if I relate these two it is inverses is that fine. So, if you are looking at a quaternion ABCD and you want to figure out what its inverse is what do you do just negate the last three components is that interesting it is that easy to compute the inverse rotation. If you want to do it with a matrix what do you do you have to compute a full matrix inverse maybe except if it is a rotation matrix it is just a transpose turns out. So, it is easier it is also reasonably easy in that case. If this is the case here then we also can have another case where I just negate the first coordinate I negate a and I leave B, C and D alone and these two should be equivalent right does that make sense the same rule is up here. If I negate all four components I should end up with these being equivalent and these two being inverses that seem all right. So, again just to summarize if I negate all four components it is the same rotation. If I decide to negate only the first component or I decide to negate the second third and fourth components together, but not the first one either one of those corresponds to an inverse is not nice. So, that is one very quick way many times in programming in these kinds of systems it is easy to make a mistake where you should have the inverse rotation and you have instead calculated the forward rotation. In that case you just perform some simple sign changes you should of course go back and check all of your math carefully and make sure you understand what you are trying to do, but for debugging purposes it is very helpful to know you can very quickly spot that you are off by an inverse if the signs are wrong. So, that is why I am telling you this trying to give you this extra information all right. Any questions about that? There is a direct formula for multiplication of quaternions that do not that does not require you to convert back to rotation matrices. I am not going to make a big deal out of it, but I at least want to quickly give it to you. So, just so you know multiplication, so let us suppose that again we have a b c d for a quaternions and I will say let p equal just a b c d part. So, I can treat it as a 3 d vector let us just suppose that is a 3 d vector called p and now if I want to calculate some quaternion q 1 multiplied by some quaternion q 2 to get some third quaternion q 3 there is a very simple formula just using 3 d standard vector calculus. I say q 1 multiplied by q 2 is equal to a 1 a 2 that is the scalar part the the first components of each one minus p 1 dot product p 2. So, that gives me the first component now this is actually a 3 that we are calculating and then the remaining 3 components are given by p 1 standard cross product p 2 plus scalar a 1 multiplied by p 2 plus scalar a 2 multiplied by vector p 1. So, this gives me the b 3 c 3 and d 3 components I am not deriving this at all there is a whole algebra of quaternions there is a reason for it coming out this way, but what I want to say that is very interesting about it couple of things one of them is that if you go through this you will find out that it is not commutative this product is that a good thing it is a good thing because if you are using this to represent 3 d rotations and 3 d rotations are not commutative it would make sense that this algebra of multiplying quaternions also ends up being non commutative as does matrix multiplication of rotations encoded that way. The reason why I put this and you usually you will find this inside of a quaternion library you know that you do not have to really worry about the details of it, but one of the reasons why I put this is so that you know that you can do the following you could have rotation matrices R 1 and R 2 and you might want to multiply them to get some third matrix R 3. The thing you could do is you could represent the first rotation as a quaternion q 1 you can represent the second rotation as a quaternion q 2 and if you do this and you apply this product that I have given here and you get a result q 3 the interesting thing is that these two results here end up being the same if I go and convert back. So, the arrows here correspond to the standard conversion formula to convert rotations. So, if I go and I convert from rotation matrix to quaternion another rotation matrix to quaternion I do the quaternion multiplication and then I convert that back to rotation matrix I will get the same result and you will actually get it with fewer algebraic operations. So, people tend to prefer to just stay in quaternion land the whole time let us say right. So, you do not ever have to convert back to matrices if you do not want to if you are using a library that forces you to in your code then so be it, but I just wanted to point out that because of this nice algebra you can stay entirely inside of quaternion representation. So, it is another reason why people like using them in computer graphics and in virtual reality in addition to this property that I mentioned, but I did not prove formally in any way which is that small changes in your in your quaternion parameters correspond to the same small changes in rotation of the rigid body regardless of where that rotation is occurring at in the space of rotations and that is very important. Another way to say it is that if I were to pick a an orientation uniformly at random what would that mean? If it is a 2D orientation you just pick a number between 0 and 2 pi at random and you would be fine what would I pick at random for 3D rotation? If you pick Euler angles you all pitch and roll at random you will not end up with a uniform covering of orientations. If you pick a unit quaternion at random a random point on the sphere you will in fact have perfect uniform coverage. So, if you do probabilities over transformations as well it is very important to have this. If you are interested in a lot further detail on this subject it is called Har measure. If you want to study the mathematics of these things.