Update 2016-10-29-An-Easy-Way-to-Understand-Quaternion-and-Rotation-Part-1-Theory.adoc

= An Easy Way to Understand Quaternion and Rotation: Part 1. Theory
Eric Zhang

:stem: latexmath
:figure-caption!:

Quaternion is widely used in game engines to represent 3D rotation. As a game engineer you might be using quaternion explicitly or implicitly in your daily work, but do you really understand what is going on under the hood when you are calling “rotate a vector” or “combine two rotations”? Why rotating a vector stem:[v] by quaternion stem:[q] is calculated by a “sandwich” multiplication: stem:[qvq^{-1}] ? Why rotating by quaternion stem:[q_1] then stem:[q_2] is in the reversed order: stem:[{q_2}{q_1}], and can you visualize the result rotation axis and angle?

Understanding quaternions also leads to more efficient use of quaternion. For example, one common situation in game development is that we need an object to face its opposite direction. What we usually would do is to get the normal or forward vector, negate it, build a rotation out of it, and assign the rotation to the object. Later in this article we will see how much calculation we need to do in this process. However with the understanding of quaternion, we only need to do stem:[q=(q.y,-q.x,q.w,-q.z)], and I will show you why.

In this article, I will try to avoid touching the algebra structure of quaternion, or having to imagine a 4 dimensional hyper sphere. I will start with a special rotation operation: flip, and use that to visualize quaternion in a more accessible and geometrical way. This article will be split into 2 parts. In Part 1 we will talk about the idea of quaternion, understand and visualize how it rotates a vector and how to compose rotations. In Part 2 we will talk about how to make use of our understanding in Part 1, and how it is used in game engine versus rotation matrix and Euler angle.

I would assume you are comfortable with 3D math (vector dot product and cross product) and basic trigonometry.

=== Quaternion Definition

Quaternion is a 4-tuple denoted as stem:[q=(x,y,z,w)]. The length of a quaternion is defined as stem:[\left|q\right| =\sqrt{x^{2}+y^{2}+z^{2}+w^{2}}], just as you would expected from a 4D vector.

In order to represent 3D rotation, we have a constraint on the quaternions we use. But before that I want to introduce Euler’s rotation theorem:

*_Any rotation in 3D space is equivalent to a single rotation of angle stem:[θ] along some axis stem:[\vec{v}]._*

We can use quaternion to describe this angle-axis rotation : stem:[q=(sin\frac{θ}{2}\vec{v}.x,sin\frac{θ}{2}\vec{v}.y,sin\frac{θ}{2}\vec{v}.z,cos\frac{θ}{2})], or in a more compact form stem:[q=(sin\frac{θ}{2}\vec{v},cos\frac{θ}{2})]. We call this form the vector form of a quaternion, and we will use this form throughout this article. You might be thinking why we are using stem:[\frac{θ}{2}] other than using stem:[θ] directly. I will explain that in a later section.

It is easy to see the length of this quaternion stem:[\left|q\right|=\sqrt{sin^{2}\frac{θ}{2}\left|\vec{v}\right|^{2}+cos^{2}\frac{θ}{2}}=1]. (Remember the axis stem:[\vec{v}] is a unit vector that stem:[\left|\vec{v}\right|=1]). We call it a unit quaternion if the length stem:[\left|q\right|=1]. So we can rewrite Euler’s rotation theorem in quaternion term:

*_Any 3D rotation is equivalent a unit quaternion stem:[q] that stem:[\left|q\right|=1]._*

---
.Side Note
****
This claim actually has 2 sides. Let me go a little be more in details in math term:
(1). For any 3D rotation equivalent to a rotation angle stem:[θ] along axis stem:[\vec{v}], there exists a unit quaternion stem:[q=(sin\frac{θ}{2}\vec{v},cos\frac{θ}{2})] to describe this rotation.
(2). For any unit quaternion stem:[q=(x,y,z,w)], it describes a rotation of angle stem:[θ=2cos^{-1}w] along axis stem:[\vec{v}=\frac{(x,y,z)}{\sqrt{1-w^{2}}}].

****
---

From now on, any quaternion stem:[q] used in this article is by default a unit quaternion, and we will use stem:[q] to describe rotations.

=== Rotation and Flip

Now let’s forget quaternion for a minute, and focus on the nature of rotations. This part is the key to understand quaternion calculation in an easier way.

*_Any 3D rotation can be composed by 2 flips along some axes._*

The reason we want to break down a rotation into flips, is that flips are much easier to think and calculate than general 3D rotation. We will start from flip and build our way to understand rotation.
Here is a loose proof of this idea. We define counter-clockwise as the positive direction of rotation. First consider a special case. We have a rotation stem:[q], which rotates  stem:[+90^{\circ}] along axis stem:[\vec{Z}]. Now I can say this rotation is the same as 2 flips along axis stem:[\vec{a}] and stem:[\vec{b}], both of them are on XY plane, and the angle from stem:[\vec{a}] to stem:[\vec{b}] is stem:[+45^{\circ}].

.Figure 1 (a)
image::https://github.com/lxjk/lxjk.github.io/raw/master/images/quaternions/fig1.png[, 300,float="right",align="center"]
.Figure 1 (b)
image::https://github.com/lxjk/lxjk.github.io/raw/master/images/quaternions/fig1_b.png[, 300,float="right",align="center"]

We demonstrate this through Figure 1. For any vector stem:[\vec{v}], the result of this rotation is stem:[\vec{v''}] , which is the same as flip stem:[\vec{v}] along axis stem:[\vec{a}] and get stem:[\vec{v'}], and then flip stem:[\vec{v'}] along axis stem:[\vec{b}] and get stem:[\vec{v''}].

It doesn’t matter where stem:[\vec{a}] and stem:[\vec{b}] are on the XY plane, but the order must be kept. If we choose stem:[\vec{b}] by rotating stem:[\vec{a}] along axis stem:[\vec{Z}] by stem:[+45^{\circ}] with the positive direction we defined above, then we must flip along stem:[\vec{a}] first then along stem:[\vec{b}] to get our target rotation. The order and the sign of angle is important, as you can easily see flip along stem:[\vec{b}] first then along stem:[\vec{a}] will give a different result.

It’s not hard to generalize to a rotation of any angle stem:[θ] along stem:[\vec{Z}] axis. And in this case, the angle from stem:[\vec{a}] to stem:[\vec{b}] is stem:[\frac{θ}{2}].

What if the axis is not stem:[\vec{Z}] axis but any unit vector stem:[\vec{u}] ? It turns out to be very straight forward. stem:[\vec{a}] and stem:[\vec{b}] are no longer on XY plane but on a plane cross the origin and perpendicular to stem:[\vec{u}], as in Figure 2.

.Figure 2
image::https://github.com/lxjk/lxjk.github.io/raw/master/images/quaternions/fig2.png[, 400,align="center"]

Now we can rewrite our flip composition rule in a more specific form:

*_Any 3D rotation equivalent to rotating angle stem:[θ] along axis stem:[\vec{v}] can be represented as a sequence of 2 flips along axis stem:[\vec{a}] and stem:[\vec{b}], such that stem:[\vec{a}·\vec{v}=0], stem:[\vec{b}·\vec{v}=0] and the angle from stem:[\vec{a}] to stem:[\vec{b}]: stem:[<\vec{a},\vec{b}>=\frac{θ}{2}]._*

This representation means if we fully understand flip, which is easier to visualize, we can fully understand rotation and quaternions, since any quaternion can be broken down to flips.

=== Quaternion and Flip

Now let’s recall the quaternion vector form stem:[q=(sin\frac{θ}{2}\vec{v},cos\frac{θ}{2})]. With the discussion of flips above, you can almost immediately see why we are using stem:[\frac{θ}{2}] here.

Think about flips again. A flip along axis stem:[\vec{a}] is also a stem:[180^{\circ}] rotation along axis stem:[\vec{a}]. So this flip can be represented in quaternion term

[stem]
++++
q_a=(sin\frac{180^{\circ}}{2}\vec{a},cos\frac{180^{\circ}}{2})=(\vec{a},0)
++++

From now on we will use quaternion to represent flip. Actually any unit quaternion with stem:[q.w=0] is a flip along axis stem:[(q.x,q.y,q.z)].

=== Flip Composition

Here we need to introduce the multiplication of general quaternion. Let stem:[q_1=(\vec{v_1},w_1)], stem:[q_2=(\vec{v_2},w_2)] then

[stem]
++++
{q_1}{q_2}=(\vec{v_1},w_1)(\vec{v_2},w_2)=(w_1\vec{v_1} + w_2\vec{v_2} + \vec{v_1}×\vec{v_2}, {w_1}{w_2}-\vec{v_1}·\vec{v_2})
++++

Note here stem:[q_1] and stem:[q_2] are not necessarily unit quaternion, so even I’m using vector form, there’s no need to put stem:[sin\frac{θ}{2}] and stem:[cos\frac{θ}{2}] as we did for unit quaternions. It’s hard to explain this definition without introducing the algebra structure of quaternions, so I will skip that. If you are interesting to know how this is derived, quaternion https://en.wikipedia.org/wiki/Quaternion#Definition[Wiki page] has a very straight forward introduction.

We are not going to use this general quaternion multiplication in Part 1. Here we only need to know a simpler form, the multiplication of flips. Let stem:[q_a=(\vec{a},0)], stem:[q_b=(\vec{b},0)] then

[stem]
++++
{q_a}{q_b}=(\vec{a},0)(\vec{b},0)=(\vec{a}×\vec{b},-\vec{a}·\vec{b})
++++

It is naturally derived from the general form, and we will be only using this multiplication in Part 1.

With flip multiplication defined, we can rewrite our flip composition rule again:

*_Any 3D rotation stem:[q=(sin\frac{θ}{2}\vec{v},cos\frac{θ}{2})] can be represented as a sequence of 2 flips stem:[q_a=(\vec{a},0)] and stem:[q_b=(\vec{b},0)], such that_*
[stem]
++++
q=-{q_b}{q_a}
++++
*_where stem:[\vec{a}·\vec{v}=0], stem:[\vec{b}·\vec{v}=0] and the angle from stem:[\vec{a}] to stem:[\vec{b}]: stem:[<\vec{a},\vec{b}>=\frac{θ}{2}]._*

You might be thinking why it is not stem:[q= {q_a}{q_b}] instead. We will show where the order and the negative sign coming from in the proof.

stem:[\vec{a}·\vec{b}=cos<\vec{a},\vec{b}>\left|\vec{a}\right|\left|\vec{b}\right|=cos\frac{θ}{2}]. Since stem:[\vec{a}·\vec{v}=0], stem:[\vec{b}·\vec{v}=0] and stem:[\left|\vec{v}\right|=1], we have stem:[\vec{a}×\vec{b}=sin<\vec{a},\vec{b}>\left|\vec{a}\right|\left|\vec{b}\right|\vec{v}=sin\frac{θ}{2}\vec{v}].

If you are not sure about the direction of the cross product, see Figure 2.

[stem]
++++
\begin{align*}
q&=(sin\frac{θ}{2}\vec{v},cos\frac{θ}{2})\\
&=(\vec{a}×\vec{b},\vec{a}·\vec{b})\\
&=-(-\vec{a}×\vec{b},-\vec{a}·\vec{b})\\
&=(\vec{b}×\vec{a},-\vec{a}·\vec{b})\\
&=-{q_b}{q_a}
\end{align*}
++++

Here you can also clearly see why we are using stem:[sin\frac{θ}{2}] and stem:[cos\frac{θ}{2}] in quaternions.

One thing I need to mention here is the negation of a quaternion. stem:[q=(sin\frac{θ}{2}\vec{v},cos\frac{θ}{2})], then

[stem]
++++
\begin{align*}
{-q}&=(-sin\frac{θ}{2}\vec{v},-cos\frac{θ}{2})\\
&=(-sin\frac{2π-θ}{2}\vec{v},cos\frac{2π-θ}{2})\\
&=(sin\frac{-(2π-θ)}{2}\vec{v},cos\frac{-(2π-θ)}{2})\\
\end{align*}
++++

Recall that stem:[sinθ=sin(π-θ)] and stem:[-cosθ=cos(π-θ)], then stem:[-sinθ=sin(-θ)] and stem:[cosθ=cos(-θ)].

It shows that stem:[-q] is a rotation along axis stem:[\vec{v}] of angle stem:[-(2π-θ)], which is exactly the same rotation as stem:[q]. For example if stem:[θ=90^{\circ}] then stem:[-(2π-θ)=-270^{\circ}], rotate stem:[90^{\circ}] along axis stem:[\vec{v}] is the same as rotate stem:[270^{\circ}] degree but in the opposite direction along the same axis stem:[\vec{v}].

The fact that stem:[q] and stem:[–q] represents the same rotation is usually called double-cover. However in our calculation I don’t want you to simply think stem:[q] and stem:[–q] are the same. They are different in quaternion space, even though they map to the same 3D rotation. The negative sign of the flip composition needs to be there.

The order of stem:[q=-{q_b}{q_a}] on the right hand side is important. It means flip along stem:[\vec{a}] first and then stem:[\vec{b}]. Actually all unit quaternion multiplication needs to be “read” from right to left when we are thinking about the order of applying those rotations.

---
.Side Note
****
We can however get rid of the negative sign by choosing stem:[\vec{a}] and stem:[\vec{b}] differently.

_Any 3D rotation stem:[q=(sin\frac{θ}{2}\vec{v},cos\frac{θ}{2})] can be represented as a sequence of 2 flips stem:[q_a=(\vec{a},0)] and stem:[q_b=(\vec{b},0)], such that
stem:[q={q_b}{q_a}]
where stem:[\vec{a}·\vec{v}=0], stem:[\vec{b}·\vec{v}=0] and the angle from stem:[\vec{a}] to stem:[\vec{b}]: stem:[<\vec{a},\vec{b}>=\frac{θ}{2}-π]._

It becomes harder to visualize stem:[\vec{a}] and stem:[\vec{b}] if we go this way, and the negative sign does not really introduce a lot of difficulties, so we will stick with that negative sign in this article.

****
---

=== Flip Vector

Given a flip stem:[q_a=(\vec{a},0)] and vector stem:[\vec{v}], we are ready to calculate the result of the flip stem:[\vec{v'}].

.Figure 3
image::https://github.com/lxjk/lxjk.github.io/raw/master/images/quaternions/fig3.png[, 400,align="center"]

According to flip definition, stem:[\vec{v}], stem:[\vec{a}] and stem:[\vec{v'}] are on the same plane, and the angle stem:[<\vec{v},\vec{a}>=<\vec{a},\vec{v'}>].

If we treat stem:[\vec{v}] and stem:[\vec{v'}] as the axis of flip stem:[q_v=(\vec{v},0)] and stem:[q_v'=(\vec{v'},0)]. From our flip composition rule, flipping along axis stem:[\vec{v}] then stem:[\vec{a}] should give us the same rotation as flipping along axis stem:[\vec{a}] then stem:[\vec{v'}].

We can actually calculate the result rotation. Let stem:[<\vec{v},\vec{a}>=<\vec{a},\vec{v'}>=\frac{θ}{2}], stem:[\vec{u}=\frac{\vec{v}×\vec{a}}{\left|\vec{v}×\vec{a}\right|}=\frac{\vec{a}×\vec{v'}}{\left|\vec{a}×\vec{v'}\right|}]. Then the result rotation is of angle stem:[θ] along axis stem:[\vec{u}].

[stem]
++++
\begin{align*}
q&=(sin\frac{θ}{2}\vec{v},cos\frac{θ}{2})\\
&=-{q_a}{q_v}\\
&=-{q_v'}{q_a}
\end{align*}
++++

This gives stem:[{q_v'}{q_a}={q_a}{q_v}].

(Here stem:[\left|\vec{v}×\vec{a}\right|=\left|\vec{a}×\vec{v'}\right|=sin\frac{θ}{2}].If you are not sure what’s going on here, go back “Flip Composition” and read the proof)

Now we need to introduce the inverse of a quaternion. The inverse of stem:[q] is denoted as stem:[q^{-1}], such that stem:[qq^{-1}=q^{-1}q=(\vec{0},1)].

stem:[I=(\vec{0},1)] is called identity quaternion, means no rotation at all. You can think of stem:[I=(sin0\vec{v},cos0)], which means rotating stem:[0^{\circ}] along any axis stem:[\vec{v}]. We haven’t gone into quaternion multiplication or rotation composition, but it’s not hard to see for any quaternion stem:[q], stem:[qI=Iq=q].

In the case of unit quaternion, the idea of inversed quaternion is if you apply a rotation, then apply its inverse, the result should be no rotation at all. And it is the same if you apply an inversed rotation then apply the original one.

For any unit quaternion stem:[q=(sin\frac{θ}{2}\vec{v},cos\frac{θ}{2})], then stem:[q^{-1}=(-sin\frac{θ}{2}\vec{v},cos\frac{θ}{2})]. You can understand this in two ways, either stem:[q^{-1}=(sin\frac{θ}{2}(-\vec{v}),cos\frac{θ}{2})] or stem:[q^{-1}=(sin\frac{-θ}{2}\vec{v},cos\frac{-θ}{2})]. stem:[q^{-1}] is either a rotation of angle stem:[θ] along axis stem:[-\vec{v}], or a rotation of angle stem:[–θ] along axis stem:[\vec{v}]. Either way it will cancel out the original rotation.

I will give a quick proof in the case of flip. You can try extend this proof to general unit quaternion. If stem:[q_a=(\vec{a},0)], stem:[q_a^{-1}=(-\vec{a},0)], we have

[stem]
++++
{q_a}{q_a^{-1}}=(\vec{a}×-\vec{a},-(\vec{a}·-\vec{a}))=(\vec{0},1)
++++

(Make sure you understand the difference between stem:[q^{-1}] and stem:[–q]. Read “Flip Composition” about quaternion negation if you are not sure.)

We can go back to previous result of flipping vector stem:[{q_v'}{q_a}={q_a}{q_v}]. Apply inverse flip of q_a on both side, the equation becomes

[stem]
++++
\begin{align*}
{q_v'}{q_a}{q_a^{-1}}&={q_a}{q_v}{q_a^{-1}}\\
q_v'&={q_a}{q_v}{q_a^{-1}}
\end{align*}
++++

This provides us a way to calculate the result of flip. Since we only need the vector part of the result, we can denote this as

[stem]
++++
\vec{v'}={q_a}\vec{v}{q_a^{-1}}
++++

When we put a vector stem:[\vec{v}] in quaternion multiplication, we are implicitly making that vector the axis of a flip to stuff it into a quaternion stem:[(\vec{v},0)]. This is how the “sandwich” multiplication form comes from, but only in the form of flip. We will prove that our result holds the same for any rotation in the next section.

=== Rotate Vector

We know any 3D rotation stem:[q] can be broken down into 2 flips stem:[q= -{q_b}{q_a}], which means flipping along stem:[\vec{a}] first and then stem:[\vec{b}]. So for a vector stem:[\vec{v}], we apply the first flip and get
[stem]
++++
\vec{v'}={q_a}\vec{v}{q_a^{-1}}
++++
Then we apply the second flip stem:[\vec{v'}] and get
[stem]
++++
\vec{v''}={q_b}\vec{v'}{q_b^{-1}}
++++
So the final result is
[stem]
++++
\begin{align*}
\vec{v''}&={q_b}{q_a}\vec{v}{q_a^{-1}}{q_b^{-1}}\\
&=({q_b}{q_a})\vec{v}({q_b}{q_a})^{-1}\\
&=(-q)\vec{v}(-q^{-1})\\
&=q\vec{v}q^{-1}\\
\end{align*}
++++
Here you can see why stem:[q= -{q_b}{q_a}] needs to be in this order.

One thing we need to prove
[stem]
++++
\begin{align*}
{q_a^{-1}}{q_b^{-1}}&=(-\vec{a},0)(-\vec{b},0)\\
&=(-\vec{a}×-\vec{b},-(-\vec{a})·(-\vec{b}))\\
&=(\vec{a}×\vec{b},-\vec{a}·\vec{b})\\
&=(-\vec{b}×\vec{a},-\vec{b}·\vec{a})\\
&=({q_b}{q_a})^{-1}
\end{align*}
++++
At this point, we fully explained how to rotate a vector using quaternion.

=== Rotation Composition

Given rotation stem:[q_1] and stem:[q_2], from the formula in the previous section, if we rotate vector stem:[\vec{v}] by stem:[q_1] first then by stem:[q_2], we have
[stem]
++++
\begin{align*}
\vec{v'}&={q_1}\vec{v}{q_1^{-1}}\\
\vec{v''}&={q_2}\vec{v'}{q_2^{-1}}\\
&={q_2}{q_1}\vec{v}{q_1^{-1}}{q_2^{-1}}\\
&=({q_2}{q_1})\vec{v}({q_2}{q_1})^{-1}\\
\end{align*}
++++
It is the same as apply the combined rotation stem:[q={q_2}{q_1}]. Be careful about the multiplication order.

Again we need to prove stem:[{q_1^{-1}}{q_2^{-1}}=({q_2}{q_1})^{-1}], but we will do this later. This equation is actually very easy to understand in geometric term. We have a combined rotation stem:[q={q_2}{q_1}] that rotates stem:[q_1] first then rotates stem:[q_2]. If we want to undo this rotation, which means apply the inverse stem:[q^{-1}={q_2}{q_1^{-1}}], we need to undo stem:[q_2] first then undo stem:[q_1], that is effectively stem:[q_1^{-1}q_2^{-1}].

What does it really mean to combine 2 rotations, can we visualize the rotation axis and angle of the result? By converting rotations to flips we actually do that.

Let stem:[q_1=(sin\frac{θ_1}{2}\vec{v_1},cos\frac{θ_1}{2})], stem:[q_2=(sin\frac{θ_2}{2}\vec{v_2},cos\frac{θ_2}{2})], we need to choose a special flip break down, such that they share one flip: stem:[q_1=-{q_c}{q_a}], stem:[q_2=-{q_b}{q_c}].

Can we find such a break down? Remember the rule of flip composition requires the flip axis to be perpendicular to the rotation axis, that is stem:[\vec{c}·\vec{v_1}=0], stem:[\vec{c}·\vec{v_2}=0], we can choose stem:[\vec{c}=\frac{\vec{v_1}×\vec{v_2}}{\left|\vec{v_1}×\vec{v_2}\right|}].

Based on stem:[\vec{c}] we can find out the other two axes: rotate stem:[\vec{c}] along axis stem:[\vec{v_1}] by angle stem:[-\frac{θ_1}{2}] results in stem:[\vec{a}]; rotate stem:[\vec{c}] along axis stem:[\vec{v_2}] by angle stem:[\frac{θ_2}{2}] results in stem:[\vec{b}]. This process is demonstrated in Figure 4.

Now we have stem:[\vec{a}·\vec{v_1}=0], stem:[\vec{c}·\vec{v_1}=0], stem:[<\vec{a},\vec{c}>=\frac{θ_1}{2}] and stem:[\vec{c}·\vec{v_2}=0], stem:[\vec{b}·\vec{v_2}=0], stem:[<\vec{c},\vec{b}>=\frac{θ_2}{2}]. Our break down stem:[q_1=-{q_c}{q_a}], stem:[q_2=-{q_b}{q_c}] is valid. The combined rotation can be written as
[stem]
++++
\begin{align*}
q&={q_2}{q_1}\\
&=(-{q_b}{q_c})(-{q_c}{q_a})\\
&={q_b}({q_c}{q_c}){q_a}\\
&=-{q_b}{q_a}\\
\end{align*}
++++
Here we need to prove this
[stem]
++++
{q_c}{q_c}=(\vec{c},0)(\vec{c},0)=(\vec{c}×\vec{c},-(\vec{c}·\vec{c}))=(\vec{0},-1)=-I
++++
It shows that the combined rotation can be composed by flip stem:[q_a] and stem:[q_b], which tells the combined rotation is a rotation of angle stem:[2<\vec{a},\vec{b}>] along axis stem:[\vec{u}=\frac{\vec{a}×\vec{b}}{\left|\vec{a}×\vec{b}\right|}].

.Figure 4
image::https://github.com/lxjk/lxjk.github.io/raw/master/images/quaternions/fig4.png[, 400,align="center"]
In Figure 4, Blue plane is based on stem:[\vec{v_1}] and stem:[\vec{v_1}], stem:[\vec{c}] is perpendicular to that plane.
Orange plane is based on stem:[\vec{a}] and stem:[\vec{b}], the result rotation axis stem:[\vec{u}] is perpendicular to that plane.

With the same method, let’s prove the thing we left out:
[stem]
++++
\begin{align*}
{q_1^{-1}}{q_2^{-1}}&=(-{q_c}{q_a})^{-1}(-{q_b}{q_c})^{-1}\\
&={q_a^{-1}}{q_c^{-1}}{q_c^{-1}}{q_b^{-1}}\\
&=-{q_a^{-1}}{q_b^{-1}}\\
&=(-{q_b}{q_a})^{-1}\\
&=({q_b}{q_c}{q_c}{q_a})^{-1}\\
&=({q_2}{q_1})^{-1}\\
\end{align*}
++++

=== Summary of Part 1

In Part 1, we covered the definition of quaternion stem:[q=(x,y,z,w)], the vector form of quaternion stem:[q=(\vec{v},w)], unit quaternion stem:[q=(sin\frac{θ}{2}\vec{v},cos\frac{θ}{2})] and how it is used to represent a rotation.

We also talked about negation of quaternion stem:[–q], and its double cover property; the inverse of quaternion stem:[q^{-1}] and identity quaternion stem:[I=(\vec{0},1)].

We use quaternion to represent flip stem:[q_a=(\vec{a},0)], and derive the rule of flip composition stem:[q=-{q_b}{q_a}]. Based on this rule, we visualized and proved how quaternion rotates a vector by stem:[\vec{v'}=q\vec{v}q^{-1}] and how rotation gets composed by stem:[q={q_2}{q_1}].

We slightly touched quaternion multiplication, and we proved an important equation stem:[{q_1^{-1}}{q_2^{-1}}=({q_2}{q_1})^{-1}].

=== Appendix: Derive Quaternion Multiplication

This part is not very important for understanding quaternion. It is a bit calculation heavy and is more for fun. Feel free to skip.

We can actually derive the general quaternion multiplication from this special flip break down. That is if we define flip multiplication stem:[{q_a}{q_b}=(\vec{a},0)(\vec{b},0)=(\vec{a}×\vec{b},-\vec{a}·\vec{b})] directly, we can proof what general quaternion multiplication stem:[{q_1}{q_2}=(sin\frac{θ_1}{2}\vec{v_1},cos\frac{θ_1}{2})(sin\frac{θ_2}{2}\vec{v_2},cos\frac{θ_2}{2})] would look like.

Here are some equations we will be using:
[stem]
++++
\begin{align*}
\vec{a}×(\vec{b}×\vec{c})&=(\vec{a}·\vec{c})\vec{b}-(\vec{a}·\vec{b})\vec{c}\\
(\vec{a}×\vec{b})·(\vec{c}×\vec{d})&=(\vec{a}·\vec{c})(\vec{b}·\vec{d})-(\vec{a}·\vec{d})(\vec{b}·\vec{c})\\
(\vec{a}×\vec{b})×(\vec{a}×\vec{c})&=(\vec{a}·(\vec{b}×\vec{c}))\vec{a}
\end{align*}
++++

Recall how we choose the flip break down stem:[\vec{c}=\frac{\vec{v_1}×\vec{v_2}}{\left|\vec{v_1}×\vec{v_2}\right|}].

Rotate stem:[\vec{c}] along axis stem:[\vec{v_1}] by angle stem:[-\frac{θ_1}{2}] we get
[stem]
++++
\vec{a}=cos\frac{-θ_1}{2}\vec{c} + sin\frac{-θ_1}{2}(\vec{v_1}×\vec{c})=\frac{1}{\left|\vec{v_1}×\vec{v_2}\right|}(cos\frac{θ_1}{2}(\vec{v_1}×\vec{v_2}) - sin\frac{θ_1}{2}(\vec{v_1}×(\vec{v_1}×\vec{v_2})))
++++
Rotate stem:[\vec{c}] along axis stem:[\vec{v_2}] by angle stem:[\frac{θ_2}{2}] we get
[stem]
++++
\vec{b}=cos\frac{θ_2}{2}\vec{c} + sin\frac{θ_2}{2}(\vec{v_2}×\vec{c})=\frac{1}{\left|\vec{v_1}×\vec{v_2}\right|}(cos\frac{θ_2}{2}(\vec{v_1}×\vec{v_2}) + sin\frac{θ_2}{2}(\vec{v_2}×(\vec{v_1}×\vec{v_2})))
++++
And we will have
[stem]
++++
\begin{align*}
\vec{a}·\vec{b}&=\frac{1}{{\left|\vec{v_1}×\vec{v_2}\right|}^{2}}(cos\frac{θ_1}{2}cos\frac{θ_2}{2}{\left|\vec{v_1}×\vec{v_2}\right|}^{2} - sin\frac{θ_1}{2}sin\frac{θ_2}{2}((\vec{v_1}×(\vec{v_1}×\vec{v_2}))·(\vec{v_2}×(\vec{v_1}×\vec{v_2}))))\\
&=\frac{1}{{\left|\vec{v_1}×\vec{v_2}\right|}^{2}}(cos\frac{θ_1}{2}cos\frac{θ_2}{2}{\left|\vec{v_1}×\vec{v_2}\right|}^{2} - sin\frac{θ_1}{2}sin\frac{θ_2}{2}(\vec{v_1}·\vec{v_2}){\left|\vec{v_1}×\vec{v_2}\right|}^{2})\\
&=cos\frac{θ_1}{2}cos\frac{θ_2}{2} - sin\frac{θ_1}{2}sin\frac{θ_2}{2}(\vec{v_1}·\vec{v_2})
\end{align*}
++++
[stem]
++++
\begin{align*}
\vec{a}×\vec{b}&=\frac{1}{{\left|\vec{v_1}×\vec{v_2}\right|}^{2}}(cos\frac{θ_1}{2}sin\frac{θ_2}{2}((\vec{v_1}×\vec{v_2})×(\vec{v_2}×(\vec{v_1}×\vec{v_2})))\\
&- sin\frac{θ_1}{2}cos\frac{θ_2}{2}((\vec{v_1}×(\vec{v_1}×\vec{v_2}))×(\vec{v_1}×\vec{v_2})\\
&- sin\frac{θ_1}{2}sin\frac{θ_2}{2}((\vec{v_1}×(\vec{v_1}×\vec{v_2}))×(\vec{v_2}×(\vec{v_1}×\vec{v_2}))))\\
&=\frac{1}{{\left|\vec{v_1}×\vec{v_2}\right|}^{2}}(cos\frac{θ_1}{2}sin\frac{θ_2}{2}{\left|\vec{v_1}×\vec{v_2}\right|}^{2}\vec{v_2} + sin\frac{θ_1}{2}cos\frac{θ_2}{2}{\left|\vec{v_1}×\vec{v_2}\right|}^{2}\vec{v_1} - sin\frac{θ_1}{2}sin\frac{θ_2}{2}{\left|\vec{v_1}×\vec{v_2}\right|}^{2}(\vec{v_1}×\vec{v_2}))\\
&=cos\frac{θ_1}{2}sin\frac{θ_2}{2}\vec{v_2} + sin\frac{θ_1}{2}cos\frac{θ_2}{2}\vec{v_1} - sin\frac{θ_1}{2}sin\frac{θ_2}{2}(\vec{v_1}×\vec{v_2})
\end{align*}
++++
From the previous proof of rotation composition we know stem:[q={q_2}{q_1}=-{q_b}{q_a}], that is
[stem]
++++
\begin{align*}
q&=(\vec{a}×\vec{b},\vec{a}·\vec{b})\\
&=(cos\frac{θ_1}{2}(sin\frac{θ_2}{2}\vec{v_2}) + cos\frac{θ_2}{2}(sin\frac{θ_1}{2}\vec{v_1}) - (sin\frac{θ_1}{2}\vec{v_1})×(sin\frac{θ_2}{2}\vec{v_2}), cos\frac{θ_1}{2}cos\frac{θ_2}{2} - (sin\frac{θ_1}{2}\vec{v_1})·(sin\frac{θ_2}{2}vec{v_2}))
\end{align*}
++++
which is the definition of quaternion multiplication of stem:[{q_1}{q_2}=(sin\frac{θ_1}{2}\vec{v_1},cos\frac{θ_1}{2})(sin\frac{θ_2}{2}\vec{v_2},cos\frac{θ_2}{2})].

= An Easy Way to Understand Quaternion and Rotation: Part 1. Theory
Eric Zhang

:stem: latexmath
:figure-caption!:

Quaternion is widely used in game engines to represent 3D rotation. As a game engineer you might be using quaternion explicitly or implicitly in your daily work, but do you really understand what is going on under the hood when you are calling “rotate a vector” or “combine two rotations”? Why rotating a vector stem:[v] by quaternion stem:[q] is calculated by a “sandwich” multiplication: stem:[qvq^{-1}] ? Why rotating by quaternion stem:[q_1] then stem:[q_2] is in the reversed order: stem:[{q_2}{q_1}], and can you visualize the result rotation axis and angle?

Understanding quaternions also leads to more efficient use of quaternion. For example, one common situation in game development is that we need an object to face its opposite direction. What we usually would do is to get the normal or forward vector, negate it, build a rotation out of it, and assign the rotation to the object. Later in this article we will see how much calculation we need to do in this process. However with the understanding of quaternion, we only need to do stem:[q=(q.y,-q.x,q.w,-q.z)], and I will show you why.

In this article, I will try to avoid touching the algebra structure of quaternion, or having to imagine a 4 dimensional hyper sphere. I will start with a special rotation operation: flip, and use that to visualize quaternion in a more accessible and geometrical way. This article will be split into 2 parts. In Part 1 we will talk about the idea of quaternion, understand and visualize how it rotates a vector and how to compose rotations. In Part 2 we will talk about how to make use of our understanding in Part 1, and how it is used in game engine versus rotation matrix and Euler angle.

I would assume you are comfortable with 3D math (vector dot product and cross product) and basic trigonometry.

=== Quaternion Definition

Quaternion is a 4-tuple denoted as stem:[q=(x,y,z,w)]. The length of a quaternion is defined as stem:[\left|q\right| =\sqrt{x^{2}+y^{2}+z^{2}+w^{2}}], just as you would expected from a 4D vector.

In order to represent 3D rotation, we have a constraint on the quaternions we use. But before that I want to introduce Euler’s rotation theorem:

*_Any rotation in 3D space is equivalent to a single rotation of angle stem:[θ] along some axis stem:[\vec{v}]._*

We can use quaternion to describe this angle-axis rotation : stem:[q=(sin\frac{θ}{2}\vec{v}.x,sin\frac{θ}{2}\vec{v}.y,sin\frac{θ}{2}\vec{v}.z,cos\frac{θ}{2})], or in a more compact form stem:[q=(sin\frac{θ}{2}\vec{v},cos\frac{θ}{2})]. We call this form the vector form of a quaternion, and we will use this form throughout this article. You might be thinking why we are using stem:[\frac{θ}{2}] other than using stem:[θ] directly. I will explain that in a later section.

It is easy to see the length of this quaternion stem:[\left|q\right|=\sqrt{sin^{2}\frac{θ}{2}\left|\vec{v}\right|^{2}+cos^{2}\frac{θ}{2}}=1]. (Remember the axis stem:[\vec{v}] is a unit vector that stem:[\left|\vec{v}\right|=1]). We call it a unit quaternion if the length stem:[\left|q\right|=1]. So we can rewrite Euler’s rotation theorem in quaternion term:

*_Any 3D rotation is equivalent a unit quaternion stem:[q] that stem:[\left|q\right|=1]._*

---
.Side Note
****
This claim actually has 2 sides. Let me go a little be more in details in math term:
(1). For any 3D rotation equivalent to a rotation angle stem:[θ] along axis stem:[\vec{v}], there exists a unit quaternion stem:[q=(sin\frac{θ}{2}\vec{v},cos\frac{θ}{2})] to describe this rotation.
(2). For any unit quaternion stem:[q=(x,y,z,w)], it describes a rotation of angle stem:[θ=2cos^{-1}w] along axis stem:[\vec{v}=\frac{(x,y,z)}{\sqrt{1-w^{2}}}].

****
---

From now on, any quaternion stem:[q] used in this article is by default a unit quaternion, and we will use stem:[q] to describe rotations.

=== Rotation and Flip

Now let’s forget quaternion for a minute, and focus on the nature of rotations. This part is the key to understand quaternion calculation in an easier way.

*_Any 3D rotation can be composed by 2 flips along some axes._*

The reason we want to break down a rotation into flips, is that flips are much easier to think and calculate than general 3D rotation. We will start from flip and build our way to understand rotation.
Here is a loose proof of this idea. We define counter-clockwise as the positive direction of rotation. First consider a special case. We have a rotation stem:[q], which rotates  stem:[+90^{\circ}] along axis stem:[\vec{Z}]. Now I can say this rotation is the same as 2 flips along axis stem:[\vec{a}] and stem:[\vec{b}], both of them are on XY plane, and the angle from stem:[\vec{a}] to stem:[\vec{b}] is stem:[+45^{\circ}].

.Figure 1 (a)
image::https://github.com/lxjk/lxjk.github.io/raw/master/images/quaternions/fig1.png[, 300,float="right",align="center"]
.Figure 1 (b)
image::https://github.com/lxjk/lxjk.github.io/raw/master/images/quaternions/fig1_b.png[, 300,float="right",align="center"]

We demonstrate this through Figure 1. For any vector stem:[\vec{v}], the result of this rotation is stem:[\vec{v''}] , which is the same as flip stem:[\vec{v}] along axis stem:[\vec{a}] and get stem:[\vec{v'}], and then flip stem:[\vec{v'}] along axis stem:[\vec{b}] and get stem:[\vec{v''}].

It doesn’t matter where stem:[\vec{a}] and stem:[\vec{b}] are on the XY plane, but the order must be kept. If we choose stem:[\vec{b}] by rotating stem:[\vec{a}] along axis stem:[\vec{Z}] by stem:[+45^{\circ}] with the positive direction we defined above, then we must flip along stem:[\vec{a}] first then along stem:[\vec{b}] to get our target rotation. The order and the sign of angle is important, as you can easily see flip along stem:[\vec{b}] first then along stem:[\vec{a}] will give a different result.

It’s not hard to generalize to a rotation of any angle stem:[θ] along stem:[\vec{Z}] axis. And in this case, the angle from stem:[\vec{a}] to stem:[\vec{b}] is stem:[\frac{θ}{2}].

What if the axis is not stem:[\vec{Z}] axis but any unit vector stem:[\vec{u}] ? It turns out to be very straight forward. stem:[\vec{a}] and stem:[\vec{b}] are no longer on XY plane but on a plane cross the origin and perpendicular to stem:[\vec{u}], as in Figure 2.

.Figure 2
image::https://github.com/lxjk/lxjk.github.io/raw/master/images/quaternions/fig2.png[, 400,align="center"]

Now we can rewrite our flip composition rule in a more specific form:

*_Any 3D rotation equivalent to rotating angle stem:[θ] along axis stem:[\vec{v}] can be represented as a sequence of 2 flips along axis stem:[\vec{a}] and stem:[\vec{b}], such that stem:[\vec{a}·\vec{v}=0], stem:[\vec{b}·\vec{v}=0] and the angle from stem:[\vec{a}] to stem:[\vec{b}]: stem:[<\vec{a},\vec{b}>=\frac{θ}{2}]._*

This representation means if we fully understand flip, which is easier to visualize, we can fully understand rotation and quaternions, since any quaternion can be broken down to flips.

=== Quaternion and Flip

Now let’s recall the quaternion vector form stem:[q=(sin\frac{θ}{2}\vec{v},cos\frac{θ}{2})]. With the discussion of flips above, you can almost immediately see why we are using stem:[\frac{θ}{2}] here.

Think about flips again. A flip along axis stem:[\vec{a}] is also a stem:[180^{\circ}] rotation along axis stem:[\vec{a}]. So this flip can be represented in quaternion term

[stem]
++++
q_a=(sin\frac{180^{\circ}}{2}\vec{a},cos\frac{180^{\circ}}{2})=(\vec{a},0)
++++

From now on we will use quaternion to represent flip. Actually any unit quaternion with stem:[q.w=0] is a flip along axis stem:[(q.x,q.y,q.z)].

=== Flip Composition

Here we need to introduce the multiplication of general quaternion. Let stem:[q_1=(\vec{v_1},w_1)], stem:[q_2=(\vec{v_2},w_2)] then

[stem]
++++
{q_1}{q_2}=(\vec{v_1},w_1)(\vec{v_2},w_2)=(w_1\vec{v_1} + w_2\vec{v_2} + \vec{v_1}×\vec{v_2}, {w_1}{w_2}-\vec{v_1}·\vec{v_2})
++++

Note here stem:[q_1] and stem:[q_2] are not necessarily unit quaternion, so even I’m using vector form, there’s no need to put stem:[sin\frac{θ}{2}] and stem:[cos\frac{θ}{2}] as we did for unit quaternions. It’s hard to explain this definition without introducing the algebra structure of quaternions, so I will skip that. If you are interesting to know how this is derived, quaternion https://en.wikipedia.org/wiki/Quaternion#Definition[Wiki page] has a very straight forward introduction.

We are not going to use this general quaternion multiplication in Part 1. Here we only need to know a simpler form, the multiplication of flips. Let stem:[q_a=(\vec{a},0)], stem:[q_b=(\vec{b},0)] then

[stem]
++++
{q_a}{q_b}=(\vec{a},0)(\vec{b},0)=(\vec{a}×\vec{b},-\vec{a}·\vec{b})
++++

It is naturally derived from the general form, and we will be only using this multiplication in Part 1.

With flip multiplication defined, we can rewrite our flip composition rule again:

*_Any 3D rotation stem:[q=(sin\frac{θ}{2}\vec{v},cos\frac{θ}{2})] can be represented as a sequence of 2 flips stem:[q_a=(\vec{a},0)] and stem:[q_b=(\vec{b},0)], such that_*
[stem]
++++
q=-{q_b}{q_a}
++++
*_where stem:[\vec{a}·\vec{v}=0], stem:[\vec{b}·\vec{v}=0] and the angle from stem:[\vec{a}] to stem:[\vec{b}]: stem:[<\vec{a},\vec{b}>=\frac{θ}{2}]._*

You might be thinking why it is not stem:[q= {q_a}{q_b}] instead. We will show where the order and the negative sign coming from in the proof.

stem:[\vec{a}·\vec{b}=cos<\vec{a},\vec{b}>\left|\vec{a}\right|\left|\vec{b}\right|=cos\frac{θ}{2}]. Since stem:[\vec{a}·\vec{v}=0], stem:[\vec{b}·\vec{v}=0] and stem:[\left|\vec{v}\right|=1], we have stem:[\vec{a}×\vec{b}=sin<\vec{a},\vec{b}>\left|\vec{a}\right|\left|\vec{b}\right|\vec{v}=sin\frac{θ}{2}\vec{v}].

If you are not sure about the direction of the cross product, see Figure 2.

[stem]
++++
\begin{align*}
q&=(sin\frac{θ}{2}\vec{v},cos\frac{θ}{2})\\
&=(\vec{a}×\vec{b},\vec{a}·\vec{b})\\
&=-(-\vec{a}×\vec{b},-\vec{a}·\vec{b})\\
&=(\vec{b}×\vec{a},-\vec{a}·\vec{b})\\
&=-{q_b}{q_a}
\end{align*}
++++

Here you can also clearly see why we are using stem:[sin\frac{θ}{2}] and stem:[cos\frac{θ}{2}] in quaternions.

One thing I need to mention here is the negation of a quaternion. stem:[q=(sin\frac{θ}{2}\vec{v},cos\frac{θ}{2})], then

[stem]
++++
\begin{align*}
{-q}&=(-sin\frac{θ}{2}\vec{v},-cos\frac{θ}{2})\\
&=(-sin\frac{2π-θ}{2}\vec{v},cos\frac{2π-θ}{2})\\
&=(sin\frac{-(2π-θ)}{2}\vec{v},cos\frac{-(2π-θ)}{2})\\
\end{align*}
++++

Recall that stem:[sinθ=sin(π-θ)] and stem:[-cosθ=cos(π-θ)], then stem:[-sinθ=sin(-θ)] and stem:[cosθ=cos(-θ)].

It shows that stem:[-q] is a rotation along axis stem:[\vec{v}] of angle stem:[-(2π-θ)], which is exactly the same rotation as stem:[q]. For example if stem:[θ=90^{\circ}] then stem:[-(2π-θ)=-270^{\circ}], rotate stem:[90^{\circ}] along axis stem:[\vec{v}] is the same as rotate stem:[270^{\circ}] degree but in the opposite direction along the same axis stem:[\vec{v}].

The fact that stem:[q] and stem:[–q] represents the same rotation is usually called double-cover. However in our calculation I don’t want you to simply think stem:[q] and stem:[–q] are the same. They are different in quaternion space, even though they map to the same 3D rotation. The negative sign of the flip composition needs to be there.

The order of stem:[q=-{q_b}{q_a}] on the right hand side is important. It means flip along stem:[\vec{a}] first and then stem:[\vec{b}]. Actually all unit quaternion multiplication needs to be “read” from right to left when we are thinking about the order of applying those rotations.

---
.Side Note
****
We can however get rid of the negative sign by choosing stem:[\vec{a}] and stem:[\vec{b}] differently.

_Any 3D rotation stem:[q=(sin\frac{θ}{2}\vec{v},cos\frac{θ}{2})] can be represented as a sequence of 2 flips stem:[q_a=(\vec{a},0)] and stem:[q_b=(\vec{b},0)], such that
stem:[q={q_b}{q_a}]
where stem:[\vec{a}·\vec{v}=0], stem:[\vec{b}·\vec{v}=0] and the angle from stem:[\vec{a}] to stem:[\vec{b}]: stem:[<\vec{a},\vec{b}>=\frac{θ}{2}-π]._

It becomes harder to visualize stem:[\vec{a}] and stem:[\vec{b}] if we go this way, and the negative sign does not really introduce a lot of difficulties, so we will stick with that negative sign in this article.

****
---

=== Flip Vector

Given a flip stem:[q_a=(\vec{a},0)] and vector stem:[\vec{v}], we are ready to calculate the result of the flip stem:[\vec{v'}].

.Figure 3
image::https://github.com/lxjk/lxjk.github.io/raw/master/images/quaternions/fig3.png[, 400,align="center"]

According to flip definition, stem:[\vec{v}], stem:[\vec{a}] and stem:[\vec{v'}] are on the same plane, and the angle stem:[<\vec{v},\vec{a}>=<\vec{a},\vec{v'}>].

If we treat stem:[\vec{v}] and stem:[\vec{v'}] as the axis of flip stem:[q_v=(\vec{v},0)] and stem:[q_v'=(\vec{v'},0)]. From our flip composition rule, flipping along axis stem:[\vec{v}] then stem:[\vec{a}] should give us the same rotation as flipping along axis stem:[\vec{a}] then stem:[\vec{v'}].

We can actually calculate the result rotation. Let stem:[<\vec{v},\vec{a}>=<\vec{a},\vec{v'}>=\frac{θ}{2}], stem:[\vec{u}=\frac{\vec{v}×\vec{a}}{\left|\vec{v}×\vec{a}\right|}=\frac{\vec{a}×\vec{v'}}{\left|\vec{a}×\vec{v'}\right|}]. Then the result rotation is of angle stem:[θ] along axis stem:[\vec{u}].

[stem]
++++
\begin{align*}
q&=(sin\frac{θ}{2}\vec{v},cos\frac{θ}{2})\\
&=-{q_a}{q_v}\\
&=-{q_v'}{q_a}
\end{align*}
++++

This gives stem:[{q_v'}{q_a}={q_a}{q_v}].

(Here stem:[\left|\vec{v}×\vec{a}\right|=\left|\vec{a}×\vec{v'}\right|=sin\frac{θ}{2}].If you are not sure what’s going on here, go back “Flip Composition” and read the proof)

Now we need to introduce the inverse of a quaternion. The inverse of stem:[q] is denoted as stem:[q^{-1}], such that stem:[qq^{-1}=q^{-1}q=(\vec{0},1)].

stem:[I=(\vec{0},1)] is called identity quaternion, means no rotation at all. You can think of stem:[I=(sin0\vec{v},cos0)], which means rotating stem:[0^{\circ}] along any axis stem:[\vec{v}]. We haven’t gone into quaternion multiplication or rotation composition, but it’s not hard to see for any quaternion stem:[q], stem:[qI=Iq=q].

In the case of unit quaternion, the idea of inversed quaternion is if you apply a rotation, then apply its inverse, the result should be no rotation at all. And it is the same if you apply an inversed rotation then apply the original one.

For any unit quaternion stem:[q=(sin\frac{θ}{2}\vec{v},cos\frac{θ}{2})], then stem:[q^{-1}=(-sin\frac{θ}{2}\vec{v},cos\frac{θ}{2})]. You can understand this in two ways, either stem:[q^{-1}=(sin\frac{θ}{2}(-\vec{v}),cos\frac{θ}{2})] or stem:[q^{-1}=(sin\frac{-θ}{2}\vec{v},cos\frac{-θ}{2})]. stem:[q^{-1}] is either a rotation of angle stem:[θ] along axis stem:[-\vec{v}], or a rotation of angle stem:[–θ] along axis stem:[\vec{v}]. Either way it will cancel out the original rotation.

I will give a quick proof in the case of flip. You can try extend this proof to general unit quaternion. If stem:[q_a=(\vec{a},0)], stem:[q_a^{-1}=(-\vec{a},0)], we have

[stem]
++++
{q_a}{q_a^{-1}}=(\vec{a}×-\vec{a},-(\vec{a}·-\vec{a}))=(\vec{0},1)
++++

(Make sure you understand the difference between stem:[q^{-1}] and stem:[–q]. Read “Flip Composition” about quaternion negation if you are not sure.)

We can go back to previous result of flipping vector stem:[{q_v'}{q_a}={q_a}{q_v}]. Apply inverse flip of q_a on both side, the equation becomes

[stem]
++++
\begin{align*}
{q_v'}{q_a}{q_a^{-1}}&={q_a}{q_v}{q_a^{-1}}\\
q_v'&={q_a}{q_v}{q_a^{-1}}
\end{align*}
++++

This provides us a way to calculate the result of flip. Since we only need the vector part of the result, we can denote this as

[stem]
++++
\vec{v'}={q_a}\vec{v}{q_a^{-1}}
++++

When we put a vector stem:[\vec{v}] in quaternion multiplication, we are implicitly making that vector the axis of a flip to stuff it into a quaternion stem:[(\vec{v},0)]. This is how the “sandwich” multiplication form comes from, but only in the form of flip. We will prove that our result holds the same for any rotation in the next section.

=== Rotate Vector

We know any 3D rotation stem:[q] can be broken down into 2 flips stem:[q= -{q_b}{q_a}], which means flipping along stem:[\vec{a}] first and then stem:[\vec{b}]. So for a vector stem:[\vec{v}], we apply the first flip and get
[stem]
++++
\vec{v'}={q_a}\vec{v}{q_a^{-1}}
++++
Then we apply the second flip stem:[\vec{v'}] and get
[stem]
++++
\vec{v''}={q_b}\vec{v'}{q_b^{-1}}
++++
So the final result is
[stem]
++++
\begin{align*}
\vec{v''}&={q_b}{q_a}\vec{v}{q_a^{-1}}{q_b^{-1}}\\
&=({q_b}{q_a})\vec{v}({q_b}{q_a})^{-1}\\
&=(-q)\vec{v}(-q^{-1})\\
&=q\vec{v}q^{-1}\\
\end{align*}
++++
Here you can see why stem:[q= -{q_b}{q_a}] needs to be in this order.

One thing we need to prove
[stem]
++++
\begin{align*}
{q_a^{-1}}{q_b^{-1}}&=(-\vec{a},0)(-\vec{b},0)\\
&=(-\vec{a}×-\vec{b},-(-\vec{a})·(-\vec{b}))\\
&=(\vec{a}×\vec{b},-\vec{a}·\vec{b})\\
&=(-\vec{b}×\vec{a},-\vec{b}·\vec{a})\\
&=({q_b}{q_a})^{-1}
\end{align*}
++++
At this point, we fully explained how to rotate a vector using quaternion.

=== Rotation Composition

Given rotation stem:[q_1] and stem:[q_2], from the formula in the previous section, if we rotate vector stem:[\vec{v}] by stem:[q_1] first then by stem:[q_2], we have
[stem]
++++
\begin{align*}
\vec{v'}&={q_1}\vec{v}{q_1^{-1}}\\
\vec{v''}&={q_2}\vec{v'}{q_2^{-1}}\\
&={q_2}{q_1}\vec{v}{q_1^{-1}}{q_2^{-1}}\\
&=({q_2}{q_1})\vec{v}({q_2}{q_1})^{-1}\\
\end{align*}
++++
It is the same as apply the combined rotation stem:[q={q_2}{q_1}]. Be careful about the multiplication order.

Again we need to prove stem:[{q_1^{-1}}{q_2^{-1}}=({q_2}{q_1})^{-1}], but we will do this later.

What does it really mean to combine 2 rotations, can we visualize the rotation axis and angle of the result? By converting rotations to flips we actually do that.

Let stem:[q_1=(sin\frac{θ_1}{2}\vec{v_1},cos\frac{θ_1}{2})], stem:[q_2=(sin\frac{θ_2}{2}\vec{v_2},cos\frac{θ_2}{2})], we need to choose a special flip break down, such that they share one flip: stem:[q_1=-{q_c}{q_a}], stem:[q_2=-{q_b}{q_c}].

Can we find such a break down? Remember the rule of flip composition requires the flip axis to be perpendicular to the rotation axis, that is stem:[\vec{c}·\vec{v_1}=0], stem:[\vec{c}·\vec{v_2}=0], we can choose stem:[\vec{c}=\frac{\vec{v_1}×\vec{v_2}}{\left|\vec{v_1}×\vec{v_2}\right|}].

Based on stem:[\vec{c}] we can find out the other two axes: rotate stem:[\vec{c}] along axis stem:[\vec{v_1}] by angle stem:[-\frac{θ_1}{2}] results in stem:[\vec{a}]; rotate stem:[\vec{c}] along axis stem:[\vec{v_2}] by angle stem:[\frac{θ_2}{2}] results in stem:[\vec{b}]. This process is demonstrated in Figure 4.

Now we have stem:[\vec{a}·\vec{v_1}=0], stem:[\vec{c}·\vec{v_1}=0], stem:[<\vec{a},\vec{c}>=\frac{θ_1}{2}] and stem:[\vec{c}·\vec{v_2}=0], stem:[\vec{b}·\vec{v_2}=0], stem:[<\vec{c},\vec{b}>=\frac{θ_2}{2}]. Our break down stem:[q_1=-{q_c}{q_a}], stem:[q_2=-{q_b}{q_c}] is valid. The combined rotation can be written as
[stem]
++++
\begin{align*}
q&={q_2}{q_1}\\
&=(-{q_b}{q_c})(-{q_c}{q_a})\\
&={q_b}({q_c}{q_c}){q_a}\\
&=-{q_b}{q_a}\\
\end{align*}
++++
Here we need to prove this
[stem]
++++
{q_c}{q_c}=(\vec{c},0)(\vec{c},0)=(\vec{c}×\vec{c},-(\vec{c}·\vec{c}))=(\vec{0},-1)=-I
++++
It shows that the combined rotation can be composed by flip stem:[q_a] and stem:[q_b], which tells the combined rotation is a rotation of angle stem:[2<\vec{a},\vec{b}>] along axis stem:[\vec{u}=\frac{\vec{a}×\vec{b}}{\left|\vec{a}×\vec{b}\right|}].

.Figure 4
image::https://github.com/lxjk/lxjk.github.io/raw/master/images/quaternions/fig4.png[, 400,align="center"]
In Figure 4, Blue plane is based on stem:[\vec{v_1}] and stem:[\vec{v_1}], stem:[\vec{c}] is perpendicular to that plane.
Orange plane is based on stem:[\vec{a}] and stem:[\vec{b}], the result rotation axis stem:[\vec{u}] is perpendicular to that plane.

With the same method, let’s prove the thing we left out:
[stem]
++++
\begin{align*}
{q_1^{-1}}{q_2^{-1}}&=(-{q_c}{q_a})^{-1}(-{q_b}{q_c})^{-1}\\
&={q_a^{-1}}{q_c^{-1}}{q_c^{-1}}{q_b^{-1}}\\
&=-{q_a^{-1}}{q_b^{-1}}\\
&=(-{q_b}{q_a})^{-1}\\
&=({q_b}{q_c}{q_c}{q_a})^{-1}\\
&=({q_2}{q_1})^{-1}\\
\end{align*}
++++

=== Summary of Part 1

In Part 1, we covered the definition of quaternion stem:[q=(x,y,z,w)], the vector form of quaternion stem:[q=(\vec{v},w)], unit quaternion stem:[q=(sin\frac{θ}{2}\vec{v},cos\frac{θ}{2})] and how it is used to represent a rotation.

We also talked about negation of quaternion stem:[–q], and its double cover property; the inverse of quaternion stem:[q^{-1}] and identity quaternion stem:[I=(\vec{0},1)].

We use quaternion to represent flip stem:[q_a=(\vec{a},0)], and derive the rule of flip composition stem:[q=-{q_b}{q_a}]. Based on this rule, we visualized and proved how quaternion rotates a vector by stem:[\vec{v'}=q\vec{v}q^{-1}] and how rotation gets composed by stem:[q={q_2}{q_1}].

We slightly touched quaternion multiplication, and we proved an important equation stem:[{q_1^{-1}}{q_2^{-1}}=({q_2}{q_1})^{-1}].

=== Appendix: Derive Quaternion Multiplication

This part is not very important for understanding quaternion. It is a bit calculation heavy and is more for fun. Feel free to skip.

We can actually derive the general quaternion multiplication from this special flip break down. That is if we define flip multiplication stem:[{q_a}{q_b}=(\vec{a},0)(\vec{b},0)=(\vec{a}×\vec{b},-\vec{a}·\vec{b})] directly, we can proof what general quaternion multiplication stem:[{q_1}{q_2}=(sin\frac{θ_1}{2}\vec{v_1},cos\frac{θ_1}{2})(sin\frac{θ_2}{2}\vec{v_2},cos\frac{θ_2}{2})] would look like.

Here are some equations we will be using:
[stem]
++++
\begin{align*}
\vec{a}×(\vec{b}×\vec{c})&=(\vec{a}·\vec{c})\vec{b}-(\vec{a}·\vec{b})\vec{c}\\
(\vec{a}×\vec{b})·(\vec{c}×\vec{d})&=(\vec{a}·\vec{c})(\vec{b}·\vec{d})-(\vec{a}·\vec{d})(\vec{b}·\vec{c})\\
(\vec{a}×\vec{b})×(\vec{a}×\vec{c})&=(\vec{a}·(\vec{b}×\vec{c}))\vec{a}
\end{align*}
++++

Recall how we choose the flip break down stem:[\vec{c}=\frac{\vec{v_1}×\vec{v_2}}{\left|\vec{v_1}×\vec{v_2}\right|}].

Rotate stem:[\vec{c}] along axis stem:[\vec{v_1}] by angle stem:[-\frac{θ_1}{2}] we get
[stem]
++++
\vec{a}=cos\frac{-θ_1}{2}\vec{c} + sin\frac{-θ_1}{2}(\vec{v_1}×\vec{c})=\frac{1}{\left|\vec{v_1}×\vec{v_2}\right|}(cos\frac{θ_1}{2}(\vec{v_1}×\vec{v_2}) - sin\frac{θ_1}{2}(\vec{v_1}×(\vec{v_1}×\vec{v_2})))
++++
Rotate stem:[\vec{c}] along axis stem:[\vec{v_2}] by angle stem:[\frac{θ_2}{2}] we get
[stem]
++++
\vec{b}=cos\frac{θ_2}{2}\vec{c} + sin\frac{θ_2}{2}(\vec{v_2}×\vec{c})=\frac{1}{\left|\vec{v_1}×\vec{v_2}\right|}(cos\frac{θ_2}{2}(\vec{v_1}×\vec{v_2}) + sin\frac{θ_2}{2}(\vec{v_2}×(\vec{v_1}×\vec{v_2})))
++++
And we will have
[stem]
++++
\begin{align*}
\vec{a}·\vec{b}&=\frac{1}{{\left|\vec{v_1}×\vec{v_2}\right|}^{2}}(cos\frac{θ_1}{2}cos\frac{θ_2}{2}{\left|\vec{v_1}×\vec{v_2}\right|}^{2} - sin\frac{θ_1}{2}sin\frac{θ_2}{2}((\vec{v_1}×(\vec{v_1}×\vec{v_2}))·(\vec{v_2}×(\vec{v_1}×\vec{v_2}))))\\
&=\frac{1}{{\left|\vec{v_1}×\vec{v_2}\right|}^{2}}(cos\frac{θ_1}{2}cos\frac{θ_2}{2}{\left|\vec{v_1}×\vec{v_2}\right|}^{2} - sin\frac{θ_1}{2}sin\frac{θ_2}{2}(\vec{v_1}·\vec{v_2}){\left|\vec{v_1}×\vec{v_2}\right|}^{2})\\
&=cos\frac{θ_1}{2}cos\frac{θ_2}{2} - sin\frac{θ_1}{2}sin\frac{θ_2}{2}(\vec{v_1}·\vec{v_2})
\end{align*}
++++
[stem]
++++
\begin{align*}
\vec{a}×\vec{b}&=\frac{1}{{\left|\vec{v_1}×\vec{v_2}\right|}^{2}}(cos\frac{θ_1}{2}sin\frac{θ_2}{2}((\vec{v_1}×\vec{v_2})×(\vec{v_2}×(\vec{v_1}×\vec{v_2})))\\
&- sin\frac{θ_1}{2}cos\frac{θ_2}{2}((\vec{v_1}×(\vec{v_1}×\vec{v_2}))×(\vec{v_1}×\vec{v_2})\\
&- sin\frac{θ_1}{2}sin\frac{θ_2}{2}((\vec{v_1}×(\vec{v_1}×\vec{v_2}))×(\vec{v_2}×(\vec{v_1}×\vec{v_2}))))\\
&=\frac{1}{{\left|\vec{v_1}×\vec{v_2}\right|}^{2}}(cos\frac{θ_1}{2}sin\frac{θ_2}{2}{\left|\vec{v_1}×\vec{v_2}\right|}^{2}\vec{v_2} + sin\frac{θ_1}{2}cos\frac{θ_2}{2}{\left|\vec{v_1}×\vec{v_2}\right|}^{2}\vec{v_1} - sin\frac{θ_1}{2}sin\frac{θ_2}{2}{\left|\vec{v_1}×\vec{v_2}\right|}^{2}(\vec{v_1}×\vec{v_2}))\\
&=cos\frac{θ_1}{2}sin\frac{θ_2}{2}\vec{v_2} + sin\frac{θ_1}{2}cos\frac{θ_2}{2}\vec{v_1} - sin\frac{θ_1}{2}sin\frac{θ_2}{2}(\vec{v_1}×\vec{v_2})
\end{align*}
++++
From the previous proof of rotation composition we know stem:[q={q_2}{q_1}=-{q_b}{q_a}], that is
[stem]
++++
\begin{align*}
q&=(\vec{a}×\vec{b},\vec{a}·\vec{b})\\
&=(cos\frac{θ_1}{2}(sin\frac{θ_2}{2}\vec{v_2}) + cos\frac{θ_2}{2}(sin\frac{θ_1}{2}\vec{v_1}) - (sin\frac{θ_1}{2}\vec{v_1})×(sin\frac{θ_2}{2}\vec{v_2}), cos\frac{θ_1}{2}cos\frac{θ_2}{2} - (sin\frac{θ_1}{2}\vec{v_1})·(sin\frac{θ_2}{2}vec{v_2}))
\end{align*}
++++
which is the definition of quaternion multiplication of stem:[{q_1}{q_2}=(sin\frac{θ_1}{2}\vec{v_1},cos\frac{θ_1}{2})(sin\frac{θ_2}{2}\vec{v_2},cos\frac{θ_2}{2})].