- Home /
Quaternion Rotation Order and Axis
I have a question regarding Quaternions and rotation.
I want to rotate an object 45 degrees on the y axis, then 45 degrees on the x axis. I wanted to achieve this via quaternions so I did this:
Quaternion rotateY = Quaternion.AngleAxis(90, Vector3.up);
Quaternion rotateX = Quaternion.AngleAxis(45, Vector3.right);
transform.rotation = rotateY * rotateX;
What I expected to see is this, basically a rotation of 90 degrees about the global y axis, followed by a rotation of 45 degrees around the global x axis:
However, what I do see is this:
It seems like when I composed the rotations with quaternions, its applied to the local coordinate instead of the global. so the rotation around the X axis use the "rotated" y axis after the first rotation. What I'm confused about is, How does all this work? From what I have read, I should be able to think of quaternions as rotational matrices, so the first rotation should be a rotation about the global Y axis and the second rotation should be about the global X axis. I am puzzled to why the second rotation use the "rotated" coordinate system instead since the quaternion does not "know" about the rotated coordinate system.
Any explanation to how this all works will be greatly appreciated
EDIT: In response to the first answer by Jesse, I wrote a simple test case of decomposing a rotation into 2 matrices verses using a combined matrix via the quaternion but they give different results, see below for what I did.
Quaternion rotateY = Quaternion.AngleAxis(90, Vector3.up); Quaternion rotateX = Quaternion.AngleAxis(90, Vector3.right);
Vector3 scaleUnity = new Vector3(1,1,1); Matrix4x4 matRotY = Matrix4x4.TRS(Vector3.zero, rotateY, scaleUnity); Matrix4x4 matRotX = Matrix4x4.TRS(Vector3.zero, rotateX, scaleUnity); // matrix multiplication order, matA*MatB --> first apply MatB (rotateY) then matA (rotateX) Vector3 resSeparate = (matRotX*matRotY).MultiplyPoint(Vector3.forward);
// quaternion multiplication Rotating a point first with lhs(rotateY) and then with rhs(rotateX) Matrix4x4 matCombined = Matrix4x4.TRS(Vector3.zero, rotateY*rotateX, scaleUnity); Vector3 resCombined = matCombined.MultiplyPoint(Vector3.forward);
Debug.Log("separate is " + resSeparate + " combined" + resCombined); // prints separate is (1.0, 0.0, 0.0) combined(0.0, -1.0, 0.0)
Must be something I'm missing here.
Answer by Jesse Anders · Feb 13, 2011 at 07:56 AM
Firstly, what you describe doesn't have anything specifically to do with quaternions; it's just how rotations behave, and will be the same whether you use quaternions or (e.g.) matrices.
What you're seeing is how Euler-angle rotations work. Each rotation in the sequence modifies the coordinate basis (in general), meaning, for example, that the first rotation in the sequence will generally appear to occur about one of the object's local axes rather than a global axis.
If this isn't the desired behavior, you can instead modify the orientation incrementally from update to update (rather than building it 'from scratch' as in your example).
Thanks for the response, I did some further exploring in code and updated my question.
The quaternion multiplication order should be the same as the matrix multiplication order in this case, so ins$$anonymous$$d of 'rotateY*rotateX', you'll want 'rotateX*rotateY'. (The Unity math library uses column vectors and 'standard' quaternion multiplication order, which means that the multiplication order should be the same for both quaternions and matrices.)
Yes, switching it around gives the behavior which I expect. However, the unity documentation here: http://unity3d.com/support/documentation/ScriptReference/Quaternion-operator_multiply.html
implies that a quaternion operation of: lhs*rhs is equivalent to rotating by lhs first and then rhs rather than the other way round.
The documentation, alas, is not always correct :) Some math libraries do swap quaternion multiplication order around in order to match the vector notation convention used, but I'm 99% sure (I'd have to check to be 100% sure) that Unity uses standard quaternion multiplication order, which means that the quaternion product p*q rotates by q and then by p.
Your answer
Follow this Question
Related Questions
Detecting Opposite Rotations of two Pipes 1 Answer
Extrapolating Quaternion Rotation 2 Answers
Make Quaternion affected by float 1 Answer