10 mins GameDev tips - Quaternions

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello and welcome in this 10 minutes game dev tips today we are going to learn how useful quaternions are when making games this video is not going to show you strict mathematical derivations but if you want to know more about that you can take a look at the different links that i've shared in the video description my goal is that at the end of this video you will have a good intuition on how they work and how to use them properly in your game projects the first thing you need to know is that quaternions are a way to represent orientations in 3d space but it's not the only approach indeed there is another very famous and easier way to orient objects called euler angles however quaternions solve issues that we have when using euler angles so let's get started by first understanding euler angles and why they can be tricky to manage rotations in games the idea is that we decompose the orientation of our object using three axes x y and z it means that a rotation around the x axis will rotate the object on the y z plane same for a y rotation that will lead the object to rotate on the x z plane finally our z rotation will make the object rotate on the x y plane you can see the pattern here the object rotates on the plane formed by the two other axes now imagine having a screwdriver aligned with the axis that you are going to rotate around when turning the tool your screw is constrained to turn with it by combining different values on different axes we can orient our object in any direction that we want before continuing let me explain the difference between a rotation and an orientation they are often confused an orientation is the word describing where the object is pointing at and the rotation is the act of changing the object's orientation now i'm going to show you an issue that we have using euler angles because of the decomposition of the rotation using those three axes the order of rotation will affect the final orientation of our object for instance rotating first around y then x could lead to a different orientation than first rotating around x then y with the same values it means that the order of rotation the game engine or blender is using for euler angles matters another issue related to the order of rotation is what we call gimbal luck the idea is that depending on the order of rotation and which axis you rotate around you can put the object in a state where two axes represent the same rotation meaning that you have lost some potential orientations in 3d space let's see again bad luck in action i have an object that is the child of the blue circle that is the child of the red circle and that is the child of the green circle meaning that when i'm rotating the blue circle the object will follow when i'm rotating the red one the blue circle will follow therefore the object will follow and finally when i'm rotating the green one you will see that the red one will follow therefore by inheritance the blue and the object will follow too in order to show gimbal lock we can select the inner circle the red one and we can rotate it by 90 degree and as you can see the blue and the green circle are now representing the same rotation meaning that if i'm rotating the blue axis or the green one they both represent a rotation around the same axis wouldn't it be great if we had a system that giving the same inputs leads always to the same orientation like if every possible orientations in 3d space were mapped to the same entry point now guess what quaternions are there to the rescue quaternions are represented by four numbers usually called x y z w one thing that you should understand is that the x y z part isn't representing an axis all those values have the same importance in the representation of the quaternion so w is as important as x for instance the way we can see a quaternion is by visualizing those four values or components as four independent orientations a value of 1 will give 100 percent weight to the orientation that this value represents and you guessed it a value of 0 will give 0 weight to this orientation and now imagine that you can combine those four distinct orientations together for instance fifty percent of x and fifty percent of w you can see that the result gives us a perfect mix of those two orientations and there is no other way to result in that specific orientation on top of that if we had a minus sign on one of those two components you can see that it represents the opposite orientation one thing to note is that quaternions are often being normalized behind the scenes by your engine or by blender for instance normalizing exists because you want to represent this 4d rotation back to a 3d rotation because in the end we live in a 3d world and we want to see a 3d rotation a normalized quaternion is called a unit quaternion like with vectors we compute a unit quaternion by dividing each components by its length and because of numerical precision it is always recommended to often normalize your quaternions which again you won't have to do in many engines [Music] so now let's look at some useful things that we can do with quaternions in unity slurping for spherical linear interpolation is a way to interpolate between two quaternions think of the following you have two orientations defined by two quaternions and you want to create a new orientation that represents a certain percentage of the two this is really useful in order to create a smooth rotation between two orientations by simply calling the slurp method over time let's look at an example in this case we have two arrows with two distinct orientations the third one is going to orient itself according to the slurp parameters that are the red and blue arrows quaternions and the t value a value of 0 for t aligns the white arrow to the red one and a value of 1 aligns the white arrow to the blue one and in between values for t represents a mix of the two orientations for instance a 0.5 value represents a 50 mix of the blue and the red arrows orientation another useful tool is the look rotation method from unity it creates a quaternion that when assigned to a transform aligns the forward direction of this transform to a specified vector here you can see the white arrow's forward vector matching the red one in unity and in many other engines you can also create a quaternion from a euler angle i'm now going to show you a useful function called from two rotation that creates a quaternion according to two vectors let's see that in action here we raycast in the down direction of the arrow if we hit something we align the up direction of the arrow with the normal using the from2 rotation function you can also take the opposite orientation by taking the inverse of a quaternion [Music] one final powerful thing that you can do with quaternions is multiplying them together and also multiplying them with vectors multiplying a quaternion with another one means visually adding the orientations [Music] multiplying a quaternion by a vector means rotating this vector by this quaternion as you can see here we are rotating the position vector of the transform by a quaternion vector multiplication the cool thing is that you can chain multiplications and it will work as expected that's all for this video i hope it was useful for you and that you now have a better understanding of quaternions there are ton of things that i didn't explain here but be sure to take a look at the different resources that i've put in the video description below finally make sure to subscribe to the channel if you want to see more videos of this kind see you later
Info
Channel: sociamix
Views: 23,207
Rating: 4.9538107 out of 5
Keywords: quaternions, unity, programming, tutorial, math, game programming, game dev, 10 minutes, rotation, lookat, orientations, euler angles, angles, learning, 3D, blender
Id: 1yoFjjJRnLY
Channel Id: undefined
Length: 10min 12sec (612 seconds)
Published: Mon Jul 27 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.