How to Use Quaternions
Transcript
quaternion are bizarre but very useful mathematical objects if you need to describe the orientation of a three-dimensional body in space you really need to use quaternion they are widely used for 3D Graphics Robotics and physical simulations the only problem with quion is that it's hard to get started with them in this video I will give you the full set of knowledge required to use them or Implement them in code from Scrat it might be tempting to use Oiler angles instead but I will try to convince you that curns are simpler and better in any aspect let's get started by looking at Oiler angles we start with an object that is not rotated and make a sequence of rotations I picked the zyz scheme as an example first we rotate the object around the z-axis by Alpha then we use the object's local y AIS to rotate by Beta And finally we use the local z-axis to rotate by GMA so Aller angles are successive rotations around different axes looks simple not really that may be not very intuitive but we can obtain any possible orientation of the object with only one rotation defined by one axis and one angle if we rotate around this Vector we can revert all three rotations in one step and this is what quter do but quion are not only the way to represent rotations it's also a special mathematical entity that makes all rotation calculations very simple Quon could be represented as a combination of scalar and threedimensional Vector so basically Quon is four numbers where R is scalar and XYZ are components of the vector part and to work with quion we need to know how to do mathematical operations with them I will give you a quick summary now and explain these operations in the last part of the video two cters could be added on multip L it note that when you multiply two quern there order in this operation matters pay attention to that we can also multiply Quon by a scaler Quon has a length usually called norm and quern used for rotations should be normalized or have a norm of one and the last operation is conjugation usually denoted with a star I will explain conjugation later with an example Le this is the whole set of mathematical operations with qu we need to know to work with rotations so let's get started on that by taking a look at two reference frames one is our world's reference frame I will call it Global the other one is a local reference frame of the object the object always stays stationary and not rotated in its local reference frame so when we say that the orientation of the object could be described with quaternion we actually mean that this Quon describes the orientation of the object's local reference frame relative to the global reference frame let's say we have a vector with a length of one and rotat the reference frame around it at angle Theta if we take a look from the vector's origin towards its end clockwise rotation is positive and counterclockwise is negative you can also use the right hand rule to find what direction of rotation is positive take your right hand and give a thumbs up to the vector and also to this video If you point with your sum in the same direction as the vector goes your folded fingers will show you the direction of positive rotation this is a general agreement that it's used everywhere in mass and science so we have normalized vector v an angle Theta the rotation Quon will have a scalar part equal to the cosine of half Theta the vector part will be the sign of half Theta multiplied by V if V was normalized The Quon will be normalized as well this small sign and cosine complication is required to keep any further Mass very simple let's play with that entity a bit first it's easy to see that minus Q is the same rotation as Q because if we take a vector minus V an angle minus Theta that will be the same rotation if we want to reverse the rotation we need to take the opposite Vector but keep the same scalar part and this is exactly what conjugation of Q does by definition so Q conjugated is a rotation opposite to Q okay great we have The Quon of our rotation what's next what we're really interested in is that how to transform local coordinates into Global let's say we have a rotated Cube and we know the coordinates of all Corners in the local reference frame to transform the cube into the global reference frame we need to transform the coordinates of each Corner one by one let's focus on one corner for example so to transform the point we need to start with a quiton that has zero scalar part and coordinates of the point as the vector part and then we multiply it by Q on the left and by Q conjugate on the right this expression will be equan with a zero scalar part and global coordinates of the point as a vector part and now look we can transform it back with the same formula but using Q conjugate instead of Q and since Q conjugate is a reverted rotation of Q this makes perfect sense but if you have have a real application like 3D Graphics you probably also have a translation that means that your reference frames zero points are not aligned if it's true then to transform coordinates you need first do the rotation and then add a vector of translation this Vector has global coordinates of the object zero point what also makes Quon very convenient is that several rotations could be combined together by simply multiplying their quarians look let's say we have the object in some rotation state q if we want to add a rotation q1 then we need to multiply Q by q1 but there are two ways to do that we can put q1 on the left or on the right the choice defines a reference frame that we use for the rotation vector for example we want to rotate 90° around the Y AIS if we want it to be the global yaxis then we place q1 on the left and if we want to rotate around the local ya AIS then we place q1 on the right very simple let's play some more remember the oiler angles example let's build a quaternion that does the same rotation let's start with identity Quon which describes no Rotation by the way the identity Quon is equal to its conjugate because no rotation reversed is no rotation as well I use zyz Oiler angles and angle sequence alpha beta and gamma so we start with identity Quon and multiply it by Quon that rotates around Z by Alpha left left or right doesn't matter here it's the same SC for identity Quon guess why because local and Global reference frames are aligned if you don't have any rotation so now our rotation is equal to Q Alpha the next step of the oiler zyz scheme is rotation around local y by Beta so we multiply our Rotation by Q beta on the right and the last one is a rotation by gamma around local Z once again multiply by Q gamma on the right great there are many other schemes for Oiler angles but now you know how to convert all of them to queron without even Googling the formula and of course I will not give you a way to convert quion back to Oiler angles because if you have an awesome Quon you should never go back back to lay merer angles okay the last formula I want to talk about is related to angular velocities it will be especially useful if you are working with any device that is equipped with a gyroscope the gyroscope is a device that measures current angular velocities in the local reference frame if we read these values frequently we can use this formula to update orientation Quon and keep track of the device orientation here Q Omega is angular velocities from the gyro combined with zero scalar part Qi + 1 is updated Quon Qi is Quon from the previous iteration and DT is the time interval of one iteration but remember two things about this Formula First this formula is a first order approximation so it's not absolutely accurate and Quon change on every step should be small otherwise you will have significant errors the second problem is that the result is not normalized so you probably need to normalize it on each iteration or once in a while otherwise you must can go out of the window eventually just like that I hope you like this video subscribe for more engineering and check out the video about my model rocket which uses quern extensively now as I promised I will explain quance operations to make this guide complete for most operations quance work similar to four-dimensional vectors to add to subtract two quion you just add or subtract corresponding components very straight forward nothing special here the same goes with multiplications by scaler you you just take each component and multiply it by the given scalar Norm is also similar to a four-dimensional vector to find it you need to add squares of all components and take a square root of that to normalize Quon we calculate the norm and then multiply our Quon by one divided by the norm of course zero Quon is the one that could not be Norm normalized conjugation is quite similar to complex numbers to find quaternion conjugate we need to take the same scalar part but the negative Vector part nothing special so far the last operation is a multiplication of two quion the origin of all coolness if we have two quion q1 and Q2 they are product looks like this the scalar part of the product will be R1 R2 minus dot product of V1 and V2 and Vector part will be R1 V2 plus R2 V1 plus cross product of V1 and vs2 here are the definitions of dot in cross products note that the dot product is a scalar and the cross product is a vector using these definitions we can come up with this formula for multiplication that looks like a lot but the mass is very simple it's just a bunch of multiplications and additions in the correct order and that's why querns are used in 3D Graphics these computations are very fast compared to Oiler angles which require trigonometry all over the place and trigonometric operations are computationally expensive I think that all you should know about quaternion if you want to use them if you are curious about how exactly and why quaternion work there are other great videos on YouTube about it but be ready to get your brain melted I'm warning you have a nice day and please don't use Oiler angles