# What is the equivalent of rotation matrix in Euler angle space for Quaternions?

I have to load a mesh in Blender (which uses Z and Y as up and forward axes), modify it and export it (with setting up and forward axes to Y and -Z) and import it into another software which uses a coordinate system with Y up and -Z forward. Later on I export the mesh from this software and import it in Blender for rendering. Since I wanted to get the same rotation in the other software that I get in Blender, I changed the rotation matrix of the other software from Identity to the following:

[1.0, 0.0, 0.0;
0.0, 0.0, -1.0;
0.0, 1.0, 0.0]


Now I get the same exact rotation in that software that I get in Blender when using Euler angles. However, I have to actually use Quaternions and I realized that Quaternions do not have a rotation matrix. I wonder, what is the equivalent version of rotation matrix for Quaternions? Also, how can I change that equivalent-rotation-matrix of Quaternions so that I get the same rotation that I get in Blender?

• "I realized that Quaternions do not have a rotation matrix" ... what? A quaternion and a rotation matrix are ways to define an orientation (relative to another orientation). You can interconvert between them, with a 2:1 mapping (a quaternion and its negation yield the same rotation matrix). So where are you getting this from? – Nicol Bolas Nov 7 '18 at 23:36
• The answer by russ is correct. Unfortunately, if you are unfamiliar with quaternions you will not be able to understand it. I suggest 3Blue1Brown videos as a visual introduction to the topic. – vgs Nov 14 '18 at 1:12
• Plus one for 3blue1brown, it's a great resource :) Also check out acko.net/blog/animate-your-way-to-glory-pt2 for a good interactive explanation (the section called 'Blowing Up The Death Star') – russ Nov 22 '18 at 9:46

It's not true to say quaternions don't have a rotation matrix. They are both ways of completely describing a rotation in 3D, so they are freely convertible. Each quaternion corresponds to exactly one rotation matrix, and each rotation matrix corresponds to one quaternion and its negative. Quaternions are just more compact and easier to interpolate.

To work out the rotation matrix for a quaternion, you need to take the effect of the quaternion, $$q = (w, x, y, z) = (w, \textbf{v})$$ on some point $$\textbf{p}$$, that is, $$(w, \textbf{v}) * (0, \textbf{p}) * (w, \textbf{-v})$$, and express this formula as a matrix operating on $$\textbf{p}$$.

Multiplying out using the rules of quaternions gives us $$(w^2 - \textbf{v.v})\textbf{p} + 2( w\textbf{v}\times \textbf{p} + \textbf{(v.p)v})$$

This can be rearranged into 3 matrix terms - the first part is just a constant value down the diagonal, the second part with the cross product has values everywhere except the diagonal, and the third one is simply $$\textbf{v}_m * \textbf{v}_n$$ for rows m and columns n.

Adding these 3 matrices together gives you

$$\begin{bmatrix} 1-2(y^2+z^2) &2(xy-zw) & 2(xz+yw)\\2(xy + zw)&1-2(x^2+z^2) & 2(yz-xw)\\2(xz-yw) & 2(yz + xw) & 1 - 2(x^2+y^2)\end{bmatrix}$$

which is the conversion you need.

In your particular use case, though, you are just doing a simple rotate-x through 90 degrees, which simplifies things a lot. Remember a quaternion is $$( cos(\theta/ 2) , sin(\theta/ 2)\textbf{a})$$ where $$\textbf{a}$$ is your normalized axis of rotation. So the quaternion representing ±90 degrees rotation around x will be $$( \frac{1}{\sqrt{2}},±\frac{1}{\sqrt{2}},0,0)$$

• "Quaternions are just more compact and easier to interpolate." And easier to re-orthonormalize after applying successive rotations. – Nicol Bolas Nov 8 '18 at 0:44
• @NicolBolas or more accurately they are never not orthogonal. They only need renormalization which you can often delay for quite a bit. Or you can embrace the length as a uniform scaling factor, though you would need to adjust the matrix a bit replacing the 1 terms with the squared length of the quaternion. – ratchet freak Nov 8 '18 at 11:01