# Transforming quaternion between coordinate systems [duplicate]

This question already has an answer here:

Working in DirectX (with DirectXMath objects): If I have the rotation of an object relative to some coordinate space as quaternion Q, and I have a transformation matrix tx from that coordinate space to world space, how do I find the rotation of my object in world space?

My thinking, which has proven inaccurate, was: Use XMMatrixDecompose on tx to get the orientation that is the rotation difference between world space and my local coordinate space (qLocalToWorld), invert it to get the rotation of the local coordinate system relative to the world (qWorldToLocal) and combine it with my local rotation Q.

XMVECTOR scale, qLocalToWorld, translation;
XMMatrixDecompose(&scale, &qLocalToWorld, &translation, tx);
auto qWorldToLocal = XMQuaternionInverse(qLocalToWorld);
auto worldOrientation = q * qWorldToLocal;


I figured for e.g. that if the local coordinate system was rotated 90 degrees with respect to the world, and my object was 5 degrees with respect to local, this would yield the 90 + 5 = 95 in world space. But I was wrong :(

I'd appreciate help in understanding the right way to do this. Thanks in advance!

## marked as duplicate by DMGregory♦, Maximus Minimus, Alexandre Vaillancourt♦, Engineer, GnemlockSep 24 '17 at 5:40

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

• Not a duplicate, the linked question answers only how to transform between coordinate systems with a different axis convention. This question is about transforming to an arbitrary space (same convention) given the transformation between space – Ben Sep 18 '17 at 15:42
• @Ben: I was going to disagree with your statement that it's not a duplicate, because the math is the same either way. However, the answer on that question is very specific to axis convention changes. That is, the general solution would work in both cases, but nobody actually provided the general solution... – Nicol Bolas Sep 18 '17 at 15:57

## 1 Answer

If I have the rotation of an object relative to some coordinate space as quaternion Q, and I have a transformation matrix tx from that coordinate space to world space, how do I find the rotation of my object in world space?

Consider what you're doing.

You have a transformation Q, which transforms from object to "local" space. You have a transformation tm which transforms from "local" to "world" space.

What you want is a transformation from object to world space. The transform from object to world space is tm * Q. Admittedly, this is under OpenGL/GLM conventions and ordering, so the order might need to be reversed for the math library you're using.

You will of course need to extract a quaternion from the 3x3 transformation matrix.