# 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.

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).