Skip to main content

Quaternions and Change of Basis

Expressing the same rotation in a different coordinate frame using quaternions.

#quaternions

Introduction

In this post, I want to talk about expressing the same rotation in a different coordinate frame. To get to that I first want to talk about linear algebra and how we express vectors in different coordinate frames first.

By the end of this article we will see that

qJ=cqOc−1q_{J} = c q_{O} c^{-1}

where qJq_{J} and qOq_{O} represent the same rotation expressed in to different coordinate frames, and cc is a quaternion describing the rotational relationship between those frames.

Which looks awfully similar to the change of basis formula for a linear transformation. So let’s take a quick look at that first.

Changing a vector’s basis

Let’s say we have a vector, vv, to keep things simple, let’s assume it is expressed in the standard basis, [e1,e2,…,en][e_{1}, e_{2}, \ldots, e_{n}].

Suppose we have two other bases, AA and BB, which are [a1,a2,…,an][a_{1}, a_{2}, \ldots, a_{n}] and [b1,b2,…,bn][b_{1}, b_{2}, \ldots, b_{n}] respectively. Notice that:

vstandard=A[v]A=B[v]Bv_{standard} = A [v]_{A} = B [v]_{B}

Where [v]A[v]_{A} and [v]B[v]_{B} are the coordinates of the vector in the respective bases. If we want to express the vector in basis BB given [v]A[v]_{A}, we can do the following:

A[v]A=B[v]BA [v]_{A} = B [v]_{B}

Multiplying both sides by B−1B^{-1} from the left we get:

[v]B=B−1A[v]A[v]_{B} = B^{-1} A [v]_{A}

which is the change of basis formula. The matrix B−1AB^{-1} A is called the change of basis matrix from basis AA to basis BB.

Changing a rotation’s basis

We can actually generalize the same idea to linear transformations. Let’s say we have a linear transformation TT that is represented by matrix TAT_{A} in basis AA and by matrix TBT_{B} in basis BB. Let’s track back a bit first, we know:

vstd=A[v]A=>[v]A=A−1vstdv_{std} = A [v]_{A} => [v]_{A} = A^{-1} v_{std}

If we have T(v)stdT(v)_{std}, which is a vector expressed in the standard basis, we can express it in basis AA as follows:

[T(v)]A=A−1T(v)std[T(v)]_{A} = A^{-1} T(v)_{std}

and since

T(v)std=TA[v]AT(v)_{std} = T A [v]_{A}

We now have:

[T(v)]A=A−1TA[v]A[T(v)]_{A} = A^{-1} T A [v]_{A}

This is true for any vector vv, so we can say that the matrix representing the linear transformation in basis AA is:

TA=A−1TAT_{A} = A^{-1} T A

Similarly:

TB=B−1TBT_{B} = B^{-1} T B

Now if we substitute TT from the first equation into the second we get:

TB=B−1ATAA−1BT_{B} = B^{-1} A T_{A} A^{-1} B

Let C=B−1AC = B^{-1} A, which is the change of basis matrix from basis AA to basis BB. Then we can rewrite the above equation as:

TB=CTAC−1T_{B} = C T_{A} C^{-1}

So there we go, we now have the change of basis formula for a linear transformation. We now that a rotation in R3\mathbb{R}^{3} is a linear transformation and its matrix follows the same rule.

A 3D rotation can also be represented by a unit quaternions, so one expects that changing the coordinate frame of a rotation should have an equivalent operation with quaternions. We will now see the matrix similarity transformation corresponds to conjugation by the quaternion describing the relationship between the two coordinate frames.

Expressing a rotation in a different coordinate frame using quaternions

Alright, let’s say we again have a vector vv, and let RR be a rotation matrix and let qq be the unit quaternion representing that rotation. We know that:

Rv=qvq−1R v = q v q^{-1}

On the right side of the equality, we treat v as a pure quaternion. Since q is a unit quaternion, let’s say R(q)R(q) is the rotation matrix corresponding to the quaternion qq. Then we have:

R(q)v=qvq−1R(q) v = q v q^{-1}

Now let’s consider the change of basis formula for the rotation matrix again

RJ=CROC−1R_{J} = C R_{O} C^{-1}

If we assume frames OO and JJ are both right handed and also orthonormal, then CC is a rotation matrix and can be represented by a unit quaternion cc. Then we have:

RJv=CROC−1vR_{J} v = C R_{O} C^{-1} v

If we have quaternions q1q_{1} and q2q_{2} then q1q2q_{1} q_{2} means first rotate by q2q_{2} and then rotate by q1q_{1}. If R(q1)R(q_{1}) and R(q2)R(q_{2}) are the rotation matrices corresponding to the quaternions, then we have:

R(q1)R(q2)=R(q1q2)R(q_{1}) R(q_{2}) = R(q_{1} q_{2})

Since rotating by q2q_{2} and then by q1q_{1} is the same as rotating by q1q2q_{1} q_{2}. So we can rewrite the change of basis formula as:

R(qJ)v=R(c)R(qO)R(c−1)vR(q_{J}) v = R(c) R(q_{O}) R(c^{-1}) v

And then finally:

qJ=cqOc−1q_{J} = c q_{O} c^{-1}

Final thoughts

I was trying to make a configurable joint follow a bone transformation in unity and realized to use the configurable joint’s target rotation set method properly i had to express the same rotation in a different coordinate frame and I thought it could be nice to have this in writing both for myself and for anyone who might want to do the same in the future for whatever reason.

There definitely are some “trust me bro” parts in this article like where i stated each unit quaternion corresponds to a rotation matrix, but I think it is a good enough reference for anyone who wants to understand how to express the same rotation in a different coordinate frame using quaternions.

You can reach out to me on my mail or linkedin if you have any questions or comments. Thanks for reading!