Quaternions are used for rotating a geometry and points with along multiple axes of rotation. They exist in four dimensions, and so that have four parts: w, x, y and z . As x, y and z are linked and similar to each other, people sometimes use another letter to represent all three, v is common for this. To represent them you should store them in a 4-dimensional vector like so:
q
=
(
w
x
y
z
)
{\displaystyle {\textbf {q}}={\begin{pmatrix}w\\x\\y\\z\end{pmatrix}}}
. Quaternions are used as an alternative to Euler angles .
Benefits of Quaternions[ 1] :
No gimbal locking
Interpolation is smooth and direct
Simple to do calculations with
So if you chose some unit length 3D-vector that you could like to rotate the geometry around (
n
^
{\displaystyle {\hat {\textbf {n}}}}
) and an amount of degrees that you want to rotate the geometry (
θ
{\displaystyle \theta }
) then create a rotation quaternion (
q
{\displaystyle {\textbf {q}}}
).
There are generally two forms for representing quaternion, short and long. In the short notation it shows it in terms of w and v (a combination of x, y and z ).
q
=
(
cos
(
θ
2
)
,
n
^
sin
(
θ
2
)
)
{\displaystyle {\textbf {q}}={\bigg (}\cos {\bigg (}{\frac {\theta }{2}}{\bigg )},{\hat {\textbf {n}}}\sin {\bigg (}{\frac {\theta }{2}}{\bigg )}{\bigg )}}
This can be represented in the long notation using w, x, y and z :
q
=
(
cos
(
θ
2
)
n
^
x
sin
(
θ
2
)
n
^
y
sin
(
θ
2
)
n
^
z
sin
(
θ
2
)
)
{\displaystyle {\textbf {q}}={\begin{pmatrix}\cos({\frac {\theta }{2}})\\{\hat {\textbf {n}}}_{x}\sin({\frac {\theta }{2}})\\{\hat {\textbf {n}}}_{y}\sin({\frac {\theta }{2}})\\{\hat {\textbf {n}}}_{z}\sin({\frac {\theta }{2}})\end{pmatrix}}}
Where [ 2] :
w
2
+
x
2
+
y
2
+
z
2
=
1
{\displaystyle w^{2}+x^{2}+y^{2}+z^{2}=1}
If a quaternion is not rotated, then it will have the value[ 3] :
q
=
(
1
0
0
0
)
{\displaystyle {\textbf {q}}={\begin{pmatrix}1\\0\\0\\0\end{pmatrix}}}
The 4 components (w, x, y and z ) can be broken down into one real number and three imaginary numbers :
w
+
i
x
+
j
y
+
k
z
{\displaystyle w+ix+jy+kz}
where
i
{\displaystyle i}
,
j
{\displaystyle j}
and
k
{\displaystyle k}
are imaginary numbers such that
i
2
=
j
2
=
k
2
=
i
j
k
=
−
1
{\displaystyle i^{2}=j^{2}=k^{2}=ijk=-1}
. From that, you can create the following statements:
i
j
=
k
=
−
j
i
j
k
=
i
=
−
k
j
k
i
=
j
=
−
i
k
i
k
=
−
j
=
−
k
i
j
i
=
−
k
=
−
i
j
k
j
=
−
i
=
−
j
k
{\displaystyle {\begin{aligned}ij=k=-ji\\jk=i=-kj\\ki=j=-ik\\ik=-j=-ki\\ji=-k=-ij\\kj=-i=-jk\end{aligned}}}
To invert a quaternion, simply multiply the x , y and z components by -1 .
q
=
(
w
x
y
z
)
{\displaystyle {\textbf {q}}={\begin{pmatrix}w\\x\\y\\z\end{pmatrix}}}
q
−
1
=
(
w
−
x
−
y
−
z
)
{\displaystyle {\textbf {q}}^{-1}={\begin{pmatrix}w\\-x\\-y\\-z\end{pmatrix}}}
When multiplying two quaternions together, order matters (
Q
=
RS
≠
SR
{\displaystyle {\textbf {Q}}={\textbf {RS}}\neq {\textbf {SR}}}
).
The multiple is defined by[ 4] :
Q
=
RS
=
[
w
r
+
i
x
r
+
j
y
r
+
k
z
r
]
[
w
s
+
i
x
s
+
j
y
s
+
k
z
s
]
=
[
w
s
w
r
−
n
^
s
⋅
n
^
r
,
w
s
n
^
r
+
w
r
n
^
s
+
n
^
r
×
n
^
s
]
=
(
w
s
w
r
−
n
^
s
⋅
n
^
v
w
s
n
^
r
x
+
w
r
n
^
s
x
+
(
n
^
r
×
n
^
s
)
x
w
s
n
^
r
y
+
w
r
n
^
s
y
+
(
n
^
r
×
n
^
s
)
y
w
s
n
^
r
z
+
w
r
n
^
s
z
+
(
n
^
r
×
n
^
s
)
z
)
{\displaystyle {\textbf {Q}}={\textbf {RS}}=[w_{r}+ix_{r}+jy_{r}+kz_{r}][w_{s}+ix_{s}+jy_{s}+kz_{s}]=[w_{s}w_{r}-{\hat {\textbf {n}}}_{s}\cdot {\hat {\textbf {n}}}_{r},w_{s}{\hat {\textbf {n}}}_{r}+w_{r}{\hat {\textbf {n}}}_{s}+{\hat {\textbf {n}}}_{r}\times {\hat {\textbf {n}}}_{s}]={\begin{pmatrix}w_{s}w_{r}-{\hat {\textbf {n}}}_{s}\cdot {\hat {\textbf {n}}}_{v}\\w_{s}{\hat {\textbf {n}}}_{r_{x}}+w_{r}{\hat {\textbf {n}}}_{s_{x}}+({\hat {\textbf {n}}}_{r}\times {\hat {\textbf {n}}}_{s})_{x}\\w_{s}{\hat {\textbf {n}}}_{r_{y}}+w_{r}{\hat {\textbf {n}}}_{s_{y}}+({\hat {\textbf {n}}}_{r}\times {\hat {\textbf {n}}}_{s})_{y}\\w_{s}{\hat {\textbf {n}}}_{r_{z}}+w_{r}{\hat {\textbf {n}}}_{s_{z}}+({\hat {\textbf {n}}}_{r}\times {\hat {\textbf {n}}}_{s})_{z}\end{pmatrix}}}
To do: Add and understand the statement: (rotationQuaternion*pointQuaternion)*conjugateQuaternion
look into:
p
′
=
q
p
q
−
1
{\displaystyle \mathbf {p'} =\mathbf {q} \mathbf {p} \mathbf {q} ^{-1}}
[Seen here ]
This is how you Interpolate gradually between two quaternions, like getting a mid-point (or some other point) between two quaternions.
Before you can do SLERP, you need to understand the superscript notation for a quaternion.
If you have a quaternion a and you use the superscript notation to raise it to the power t , this means that it will scale the angles inside the quaternion by superscript t. As this only scales all of the angles, this means that the magnitude (
w
2
+
x
2
+
y
2
+
z
2
{\displaystyle w^{2}+x^{2}+y^{2}+z^{2}}
) of the Quaternion is still 1 .
a
=
(
cos
(
θ
2
)
,
n
^
sin
(
θ
2
)
)
{\displaystyle {\textbf {a}}={\bigg (}\cos {\bigg (}{\frac {\theta }{2}}{\bigg )},{\hat {\textbf {n}}}\sin {\bigg (}{\frac {\theta }{2}}{\bigg )}{\bigg )}}
a
t
=
(
cos
(
t
θ
2
)
,
n
^
sin
(
t
θ
2
)
)
{\displaystyle {\textbf {a}}^{t}={\bigg (}\cos {\bigg (}{\frac {t\theta }{2}}{\bigg )},{\hat {\textbf {n}}}\sin {\bigg (}{\frac {t\theta }{2}}{\bigg )}{\bigg )}}
a
=
(
cos
(
θ
2
)
n
^
x
sin
(
θ
2
)
n
^
y
sin
(
θ
2
)
n
^
z
sin
(
θ
2
)
)
{\displaystyle {\textbf {a}}={\begin{pmatrix}\cos({\frac {\theta }{2}})\\{\hat {\textbf {n}}}_{x}\sin({\frac {\theta }{2}})\\{\hat {\textbf {n}}}_{y}\sin({\frac {\theta }{2}})\\{\hat {\textbf {n}}}_{z}\sin({\frac {\theta }{2}})\end{pmatrix}}}
a
t
=
(
cos
(
t
θ
2
)
n
^
x
sin
(
t
θ
2
)
n
^
y
sin
(
t
θ
2
)
n
^
z
sin
(
t
θ
2
)
)
{\displaystyle {\textbf {a}}^{t}={\begin{pmatrix}\cos({\frac {t\theta }{2}})\\{\hat {\textbf {n}}}_{x}\sin({\frac {t\theta }{2}})\\{\hat {\textbf {n}}}_{y}\sin({\frac {t\theta }{2}})\\{\hat {\textbf {n}}}_{z}\sin({\frac {t\theta }{2}})\end{pmatrix}}}
If your first Quaternion was q and the second quaternion was r and you wanted to find a point in-between (p ) which is t percent of the way from q to r , where 0 ≤ t ≤ 1.
The final equation is defined as[ 5] :
p
=
(
r
q
−
1
)
t
q
{\displaystyle {\textbf {p}}=({\textbf {r}}{\textbf {q}}^{-1})^{t}{\textbf {q}}}
Youtube:
Wikipedia pages:
Wikibooks: