- Home /
Multiplying quaternions and Multiplying quaternion with Vector3
Hey guys.
I'm having trouble understanding what multiplying quaternions with other quaternions and multiplying quaternions with Vector3 does. Could someone explain how it is different than adding the x, y, z Euler angles of one quaternion with another? Also a simple example of where in a game you would use this.
Thanks ,
A good example of its usefulness is when trying to replicate Transform.TransformDirection or InverseTransformDirection with rotations ins$$anonymous$$d of vectors, by using Transform.rotation.
transform.TransformDirection(Vector3.forward);
// same as
transform.rotation * Vector3.forward;
transform.InverseTransformDirection(Vector3.forward);
// same as
Quaternion.Inverse(transform.rotation) * Vector3.forward;
// get world-space rotation of local-space euler angles (0, 45, 0)
Quaternion newRot = transform.rotation * Quaternion.Euler(0, 45, 0);
The code above is untested.
Answer by Firas4d · Apr 22, 2018 at 09:53 AM
Hi, Multiplying a quaternion with a vector is not problematic -unlike the quaternions multiplication- it simply results in a vector that is rotated by the given quaternion.
Now multiplying quaternions is the real deal, according to Unity's Scripting API multiplying quaternions will combine the rotations in-sequence. Take a look at the example below :
Quaternion q1 = Quaternion.Euler(45, 0, 0);
Quaternion q2 = Quaternion.Euler(0, 45, 0);
Quaternion q3 = Quaternion.Euler(0, 0, 45);
Quaternion q4 = Quaternion.Euler(45, 45, 45);
Quaternion c1 = q1 * q2 * q3;
Do you think q4 and c1 has the same rotation? the answer is absolutely not! However, if you just change the order of the operands by a little bit
Quaternion c1 = q2 * q1 * q3;
then q4 and c1 rotations will be equal!
The whole point of quaternions is to solve a well known problem called Gimbal Lock which happens when dealing with 3 separate rotational axis values, the math behind it is complicated and isn't necessary to be fully understood to use quaternions in Unity. I found this answer to be a good starter to understanding quaternions.
How did you know the correct order of the operands to get the same value as q4?
Euler angles are 3 consecutive / seperate rotations. There are many different conventions in which order you may apply the rotations. Unity uses the local axis order: Y - X - Z. So if you imagine the object is aligned with the world axes ( no rotation at all ), you would first rotate around the local up axis, then around the local x axis and finally around the local z axis.
If you rotate around the world space axis ins$$anonymous$$d of the local axis you have to reverse the order. So you get the same result when you first rotate around the world Z axis, then around the world X axis and finally around the world Y axis.
Answer by ImpOfThePerverse · Apr 22, 2018 at 03:46 PM
As for examples, one might be if you wanted to make something match the rotation that the player is facing without parenting the item. If it's a first person controller, the player will probably have one rotation about the Y axis that makes them look left or right, then a second child gameobject with a rotation about the X axis that makes them look up or down. Multiply the first quaternion by the second (order matters - the Y axis rotation should be first) and you get the overall rotation of the camera.
You could just get the rotation directly from the camera rotation, but say you had a more complicated controller where the X axis rotation happened about multiple nodes, like a neck node and an eye node, to make the viewpoint crane forward when you look down. If you wanted the final rotation of the eye node relative to the body, you'd multiply the neck node rotation by the eye node rotation.
Your answer
Follow this Question
Related Questions
Make Quaternion affected by float 1 Answer
Vector3 and Quaternion issues. 2 Answers
Use Lerp to rotate object 2 Answers
rotation = Quaternion.identity doesn't change x rotation 1 Answer
How to rotate Quaternion around axis for a certain degree 2 Answers