the simple approach of picking examples and examining predicted output is what got me to a working implementation of Quaternion rotations. Thank you so, so much! Edit: Also, normalization lol
Dude. This video is kahn academy status. Your pragmatic explanation clearly imparted an intuitive knowledge of quaternions. I would consider myself an experienced 3d game developer. Keep up the good work. Matrix math will forever only make me mildly flaccid.
Nice video on the quaternions. I dabble in quaternion and hypercomplex number stuff from the pure mathematical point of view and it is indeed difficult to understand why multiplying quaternions together in the right way gives a rotation. The best I've been able to do, and this only fairly recently, is start at the Rodrigues rotation formula and show how one can algebraically get to the formula using quaternion multiplication.
I'm currently reading about geometric algebra and it seems like a fascinating topic from the little I've read. I'm a big fan of the bivector and wedge product concepts.
This is the best explanation of quaternions I have seen yet. Also, I kept thinking that you sound like Owen Wilson, which cracked me up. Like you said though, I'm still having trouble fully visualizing the concept of a "four-dimensional hypersphere". Yikes!
Yeah that's a tough one. A circle is a two dimensional shape where every point is the same distance from the center. A sphere is a three dimensional shape where every point is the same distance from the center. A hypersphere is a four dimensional shape where every point is the same distance from the center. They're all the same thing but different number of dimensions. Obviously you can't think in four dimensions so you just have to use your imagination for the 4D version.
There are 360 degrees in a circle, and 2pi radians in a circle. So I divide by 360 to get a 0-1 normalized value and then multiply by 2pi to get radians. I did that to make it more clear, but if you simplify 2pi/360 (cancel the 2) you get pi/180. Same thing! :)
Thank you ! I was just a little bit troubled about the (angleInDegree / 360 * PI * 2) , I always use the angleInDegree/ 180 * PI version. I understand that you used it to explain the Degree-> Radian conversion
Curious as to how to turn a rotation encoded in a quaternion to a rotation encoding that OpenGL understands (matrix) for use in the ModelToWorld matrix. Also curious what would happen to the scale values since they're also stored in the 3x3 matrix (first diagonal) that represents the rotation... confused.
+vexe The code to convert a quat to a matrix is kinda boring, actually. You rotate the unit x, y, z vectors by the quaternion and then use that as each column of the matrix. You can simplify the math a bit from that and you get something like this: github.com/BSVino/Grotto/blob/master/common/math/matrix.cpp#L266 Converting the other way, you need to calculate the axis of rotation and degree of rotation, then you construct the quaternion accordingly. For the axis you can probably just get away with a cross product of the rotated vector with the basis vector. For the angle, it's just a little trig. That should do you, but there are faster ways to do it, see: github.com/BSVino/Grotto/blob/master/common/math/quaternion.cpp#L47 If any of the basis vectors of the matrix are length other than 1 then you'll get an incorrect result in the quaternion. I'm unsure if you can use quats to store scaled values - I imagine yes, but in practice scaling only very rarely happens in games, so quats are always normalized. When you do need scaling, you can apply it separately since both operations are linear.
+Jorge “Vino” Rodriguez Thanks for the reply. I don't use scaling that often, but it's handy for situations where you have a crate and you want different variations of it, instead of having the artist do different versions you could just scale it. Although I do have to admit this is not the common case and scaling does seem to make other calculations more complicated. How about uniform scaling though? does that make things any easier? If we build each transformation matrix separately i.e. translate, rotation (via quaternions or w/e) and scale, and then multiply those and give that to opengl, what is the problem in doing that?
+vexe I don't think uniform would make it any easier to build the right quat. Rotation and scaling are linear so it doesn't matter what order you do those two in, but when you add translation into the mix it becomes noncommutative, so you have to be careful. Usually I find it easier to think about it conceptually if you do the translation last. I have some other videos where I do this.
Would there be any point in using quaternions if I almost never want to roll the camera (or maybe a little, like leaning in fps?). I implemented a camera using them with a lot of headache and i figured in the end my angle was always 90 degrees meaning they never really rolled anywhere. So basically it was pointless...
I always encourage people to consider using euler angles in situations where they model the underlying system well. Cameras are well modeled by pitch/yaw/roll, so I tend to use euler regularly in camera systems.
My processor is a Intel dual core x64. I'm using matrices for the rotations of my XNA game. I'm using mesh rotations for animations . When I play for a while(~30s) , The body parts of the human model goes bigger and spin of the world. I could 100% confirm that the translation of matrix is 0. People are saying this is because inaccuracy of the processor. So, Will using Quaternions help to solve the matter? (I'm calling this as "matrix mutation bug")
When you do many operations on floating point numbers they tend to drift and get slightly larger or smaller. You may be able to fix it by just re-normalizing the matrix columns. Quaternions will also help as they re-normalize themselves automatically.
I applied your quaternion multiplication with another vector, but when I see it in actual rotation in the graphics, when trying to rotate the Z axis, everything only goes diagonally..instead of going in circle about the Z axis...any idea?
Jorge Rodriguez after I make the quaternion unit length 1, as you specify...I use this quaternion matrix: public CMatrix3f getRotationMatrix(){ CMatrix3f m = new CMatrix3f(); m.setVertex(0, new CVector3D(1f - 2f*(v.y*v.y) - 2f*(v.z*v.z), 2f*(v.x*v.y) - 2f*(v.z*w), 2f*(v.x*v.z) + 2f*(v.y*w))); m.setVertex(1, new CVector3D(2f*(v.x*v.y) + 2f*(v.z*w), 1f - 2f*(v.x*v.x) - 2f*(v.z*v.z), 2f*(v.y*v.z) - 2f*(v.x*w))); m.setVertex(2, new CVector3D(2f*(v.x*v.z) - 2f*(v.y*w), 2f*(v.y*v.z) + 2f*(v.x*w), 1f - 2f*(v.x*v.x) - 2f*(v.y*v.y))); return m; } to multiply it with a vector point, but still, the only axis that seems to work is the Z-Axis everything else pivots the wrong the way. You probably are going to say, that I am missing something....any idea?
Elvis Machuca this matrix comes out of the quaternion class object, converting from quaternion to matrix, so I can do matrix multiplication with a vector point
If your goal is just to rotate a vector then you shouldn't have to convert it to a matrix first. I have another video on how to rotate a vector with a quat, have you seen that?
Shizumaru18 You want a rotation of theta around the axis n hat, but you have to add the cos and sin in order to make the quaternion unit length. If it's not unit length it will change the length of the vector that you're rotating, which is no bueno. So you add cos and sin to keep it unit length.
Jorge Rodriguez Hmm..shouldn't a unit quaternion be (cos(theta), sin(theta) * n hat) = 1 instead of (cos(theta/2), sin(theta/2) * n hat) .. I didn't watch the next video though...
veggiet2009 6.1232 etc is zero. Notice it says e-17 at the end, that means *10^-17 which makes the number very small. The calculations that produced those numbers were slightly inaccurate, which is normal for computers.
Whereas x y z are an axis of rotation, w is the amount of rotation. However, it scales by cos of the angle, so if there's no rotation it's 1 and as you rotate it progresses towards zero.
Very intuitive explanation! I can finally slerp myself into bed without gimbal locking my neck.
I know this is an old comment but this has to be the best comment on youtube
@@lilcatfriend4575 Don't ask me how I got back here, but thanks
@@dbuezas maybe u can find your way back again
im dead 🤣🤣🤣
@@fishasses long term review: still no gimbal locked neck. Much superior than the euclidean brand
I spent too long trying to wrap my head around this concept and got it instantly the moment you explained it. Thank you so much!
the simple approach of picking examples and examining predicted output is what got me to a working implementation of Quaternion rotations. Thank you so, so much!
Edit: Also, normalization lol
oh screw this, i'm going to be a stripper.
Omg I almost spit out my coffee - LOLOL!
axaxxaxaaxxa
you almost got me killed with this i can't laugh because every one is a sleep
But you'll still need to calculate your rotations around the pole. :-)
I wonder what are you doing for now ? Stripper or game developer
Dude. This video is kahn academy status. Your pragmatic explanation clearly imparted an intuitive knowledge of quaternions. I would consider myself an experienced 3d game developer. Keep up the good work. Matrix math will forever only make me mildly flaccid.
Thank you, thank you, thank you, you are a legend, I wasted three hours fixing my code, and at the end found that video, thank you again!
Nice video on the quaternions. I dabble in quaternion and hypercomplex number stuff from the pure mathematical point of view and it is indeed difficult to understand why multiplying quaternions together in the right way gives a rotation. The best I've been able to do, and this only fairly recently, is start at the Rodrigues rotation formula and show how one can algebraically get to the formula using quaternion multiplication.
Lately I've been subscribing to the geometric algebra way of doing things, with pseudoscalars.
I'm currently reading about geometric algebra and it seems like a fascinating topic from the little I've read. I'm a big fan of the bivector and wedge product concepts.
@@Math_oma
Damn, this conversation is some expert foreshadowing...
This is the best explanation of quaternions I have seen yet. Also, I kept thinking that you sound like Owen Wilson, which cracked me up. Like you said though, I'm still having trouble fully visualizing the concept of a "four-dimensional hypersphere". Yikes!
Yeah that's a tough one. A circle is a two dimensional shape where every point is the same distance from the center. A sphere is a three dimensional shape where every point is the same distance from the center. A hypersphere is a four dimensional shape where every point is the same distance from the center. They're all the same thing but different number of dimensions. Obviously you can't think in four dimensions so you just have to use your imagination for the 4D version.
There are 360 degrees in a circle, and 2pi radians in a circle. So I divide by 360 to get a 0-1 normalized value and then multiply by 2pi to get radians. I did that to make it more clear, but if you simplify 2pi/360 (cancel the 2) you get pi/180. Same thing! :)
Thank you ! I was just a little bit troubled about the (angleInDegree / 360 * PI * 2) , I always use the angleInDegree/ 180 * PI version. I understand that you used it to explain the Degree-> Radian conversion
a super nice video!!
Better optimization would be a*0.5 instead of a/2, division requires 24 cpu cycles while multiplication only uses 2.
did you test that? i would expect compilers/cpus to optimize this for you. also, both are floating point operations....
@@HoD999x Nope, just saying it as a technicality, but it doesnt matter anyways. Won't be a difference with todays CPUs anyways, doesnt really matter.
10:20 - The angle you are rotating by is actually Tangent( Length(n) / a ) - this angle can't be calculated when a = 0.
Awesome Vids! I totally love them and finally am motivated to start playing again.
Thank you! I am trying to learn to use dual quaternions for rigid bodies.
so what do i actually do with the quaternion to get the final x,y,z positions of the point that im rotating?
Try the next video, I think it will answer your question :) ua-cam.com/video/Ne3RNhEVSIE/v-deo.html
Curious as to how to turn a rotation encoded in a quaternion to a rotation encoding that OpenGL understands (matrix) for use in the ModelToWorld matrix. Also curious what would happen to the scale values since they're also stored in the 3x3 matrix (first diagonal) that represents the rotation... confused.
+vexe The code to convert a quat to a matrix is kinda boring, actually. You rotate the unit x, y, z vectors by the quaternion and then use that as each column of the matrix. You can simplify the math a bit from that and you get something like this: github.com/BSVino/Grotto/blob/master/common/math/matrix.cpp#L266
Converting the other way, you need to calculate the axis of rotation and degree of rotation, then you construct the quaternion accordingly. For the axis you can probably just get away with a cross product of the rotated vector with the basis vector. For the angle, it's just a little trig. That should do you, but there are faster ways to do it, see: github.com/BSVino/Grotto/blob/master/common/math/quaternion.cpp#L47
If any of the basis vectors of the matrix are length other than 1 then you'll get an incorrect result in the quaternion. I'm unsure if you can use quats to store scaled values - I imagine yes, but in practice scaling only very rarely happens in games, so quats are always normalized. When you do need scaling, you can apply it separately since both operations are linear.
+Jorge “Vino” Rodriguez Thanks for the reply. I don't use scaling that often, but it's handy for situations where you have a crate and you want different variations of it, instead of having the artist do different versions you could just scale it. Although I do have to admit this is not the common case and scaling does seem to make other calculations more complicated.
How about uniform scaling though? does that make things any easier?
If we build each transformation matrix separately i.e. translate, rotation (via quaternions or w/e) and scale, and then multiply those and give that to opengl, what is the problem in doing that?
+vexe I don't think uniform would make it any easier to build the right quat.
Rotation and scaling are linear so it doesn't matter what order you do those two in, but when you add translation into the mix it becomes noncommutative, so you have to be careful. Usually I find it easier to think about it conceptually if you do the translation last. I have some other videos where I do this.
Would there be any point in using quaternions if I almost never want to roll the camera (or maybe a little, like leaning in fps?). I implemented a camera using them with a lot of headache and i figured in the end my angle was always 90 degrees meaning they never really rolled anywhere. So basically it was pointless...
I always encourage people to consider using euler angles in situations where they model the underlying system well. Cameras are well modeled by pitch/yaw/roll, so I tend to use euler regularly in camera systems.
My processor is a Intel dual core x64. I'm using matrices for the rotations of my XNA game. I'm using mesh rotations for animations . When I play for a while(~30s) , The body parts of the human model goes bigger and spin of the world. I could 100% confirm that the translation of matrix is 0. People are saying this is because inaccuracy of the processor. So, Will using Quaternions help to solve the matter? (I'm calling this as "matrix mutation bug")
When you do many operations on floating point numbers they tend to drift and get slightly larger or smaller. You may be able to fix it by just re-normalizing the matrix columns. Quaternions will also help as they re-normalize themselves automatically.
Thanks for the reply. But I just fixed it by reconstructing matrices at the edges of animations.
Great!
Whoa... how did you do the "copy, paste paste" without selecting the text you wanted to copy and copying it into your clipboard? (around 7:12)
Thank you so much
Thank you very much for the videos sir, they're great!
I applied your quaternion multiplication with another vector, but when I see it in actual rotation in the graphics, when trying to rotate the Z axis, everything only goes diagonally..instead of going in circle about the Z axis...any idea?
Elvis Machuca Tough to say, are you using an existing quaternion library or did you build your own?
Jorge Rodriguez I tried to convert library into Java.
Jorge Rodriguez after I make the quaternion unit length 1, as you specify...I use this quaternion matrix:
public CMatrix3f getRotationMatrix(){
CMatrix3f m = new CMatrix3f();
m.setVertex(0, new CVector3D(1f - 2f*(v.y*v.y) - 2f*(v.z*v.z), 2f*(v.x*v.y) - 2f*(v.z*w), 2f*(v.x*v.z) + 2f*(v.y*w)));
m.setVertex(1, new CVector3D(2f*(v.x*v.y) + 2f*(v.z*w), 1f - 2f*(v.x*v.x) - 2f*(v.z*v.z), 2f*(v.y*v.z) - 2f*(v.x*w)));
m.setVertex(2, new CVector3D(2f*(v.x*v.z) - 2f*(v.y*w), 2f*(v.y*v.z) + 2f*(v.x*w), 1f - 2f*(v.x*v.x) - 2f*(v.y*v.y)));
return m;
}
to multiply it with a vector point, but still, the only axis that seems to work is the Z-Axis everything else pivots the wrong the way. You probably are going to say, that I am missing something....any idea?
Elvis Machuca this matrix comes out of the quaternion class object, converting from quaternion to matrix, so I can do matrix multiplication with a vector point
If your goal is just to rotate a vector then you shouldn't have to convert it to a matrix first. I have another video on how to rotate a vector with a quat, have you seen that?
What is W? I couldn't get it. In which video I can check this out?
krisitak w is just the fourth component in the four dimensional vector that is the quaternion. It's (x, y, z, w).
Thanks Jorge. I wasn't thinking clearly when I asked the question. I watched it again and understood it. Thanks for the cool tutorials.
Can someone please tell me how the hell does the unit quaternion have HALF THETA in the formula? I cannot find any simple explanation for this :/
Watch this video ua-cam.com/video/eo2HNCTV78s/v-deo.html
nice video...but i still couldn't understand the part where you represent (theta, n hat) as the quaternion form (cos(theta/2), n hat * sin(theta /2)..
Shizumaru18 You want a rotation of theta around the axis n hat, but you have to add the cos and sin in order to make the quaternion unit length. If it's not unit length it will change the length of the vector that you're rotating, which is no bueno. So you add cos and sin to keep it unit length.
Jorge Rodriguez Hmm..shouldn't a unit quaternion be (cos(theta), sin(theta) * n hat) = 1 instead of (cos(theta/2), sin(theta/2) * n hat) .. I didn't watch the next video though...
Shizumaru18 nope it is correct as is. Watch the next videos for the explanation.
Wow this really helped!
what application for quaternion?
Great idea, these videos!
Please give code to study it
It's interesting how python represents these numbers:
Quaternion
Quaternion
Quaternion
Quaternion
Quaternion
veggiet2009 6.1232 etc is zero. Notice it says e-17 at the end, that means *10^-17 which makes the number very small. The calculations that produced those numbers were slightly inaccurate, which is normal for computers.
veggiet2009 oh and .707106... is sqrt (2) / 2.
I dont get what w is??
Sir you rock! :D
Is it sad that I immediately recognised 0.707 as root two over two? :P
Not sad and actually it's awesome and super useful :)
+Jorge “Vino” Rodriguez
Well, I recognised it as sin(45) or cos(45), so I got it completely wrong!
Same thing though, really! :)
what a missed opportunity not calling it s hat
Hahaha I'll never run out of math.
Hyperspheres wtf ?!
this video + this video: ua-cam.com/video/jlskQDR8-bY/v-deo.html = Finally, I understand! 😭
"we" lol
WTF IS W THAT WAS NOT EXPLAINED
Whereas x y z are an axis of rotation, w is the amount of rotation. However, it scales by cos of the angle, so if there's no rotation it's 1 and as you rotate it progresses towards zero.
Too much ehhh mmm errr. Quite annoying