[Math] Euler angle to direction vector which is right

3dangle

I tried to implement a first person shooter camera using Euler angles with the order pitch–>yaw rotation.(pitch is rotate round X axis, yaw is rotate round Y axis)

Many tutorial gave the formula to do it, however they are vary from each other and not being clearly explained. I care about the math part and need to tell right from wrong.

Before roation, the camera's front vector(direction) in world space is:
$front = (0,0,-1, 0)$

And after rotate the camera, I need to calculate the direction in world space again.

My result(Formula1)
When rotate the camera, we can compute the direction vector with matrix of $(R_{yaw}* R_{pitch})$, use a point $(0,0,-1,1)$ instead to calculate as follow:
\begin{align}
front' &= R_{yaw}R_{pitch}*\begin{bmatrix}
0 \\
0 \\
-1 \\
1
\end{bmatrix} \\
& = \begin{bmatrix}
cos_{yaw} & 0 & sin_{yaw} & 0 \\
0 & 1 & 0 & 0 \\
-sin_{yaw} & 0 & cos_{yaw} & 0 \\
0 & 0 & 0 & 1
\end{bmatrix} *
\begin{bmatrix}
1 & 0 & 0 & 0 \\
0 & cos_{pitch} & -sin_{pitch} & 0 \\
0 & sin_{pitch} & cos_{pitch} & 0 \\
0 & 0 & 0 & 1
\end{bmatrix}*\begin{bmatrix}
0 \\
0 \\
-1 \\
1
\end{bmatrix} \\
&= \begin{bmatrix}
cos_{yaw} & sin_{yaw}sin_{pitch} & sin_{yaw}cos_{pitch} & 0 \\
0 & cos_{pitch} & -sin_{pitch} & 0 \\
-sin_{yaw} & cos_{yaw}sin_{pitch} & cos_{yaw}cos_{pitch} & 0 \\
0 & 0 & 0 & 1
\end{bmatrix} * \begin{bmatrix}
0 \\
0 \\
-1 \\
1
\end{bmatrix} \\
&= \begin{bmatrix}
-sin_{yaw}cos_{pitch} \\
sin_{pitch} \\
-cos_{yaw}cos_{pitch} \\
1
\end{bmatrix}
\end{align}
Then I get the direciton in world space as :

front.x = -cos(glm::radians(pitch)) * sin(glm::radians(yaw));
front.y = sin(glm::radians(pitch));
front.z = -cos(glm::radians(pitch)) * cos(glm::radians(yaw));

My approach is support by Computing camera front direction and this post.And my result is support by this code.

Other result(Formula2)
While reading the excellent tutorial, with the same rotate order (pitch–>yaw), however the author get direction vector as

front.x = cos(glm::radians(pitch)) * cos(glm::radians(yaw));
front.y = sin(glm::radians(pitch));
front.z = cos(glm::radians(pitch)) * sin(glm::radians(yaw));

you can check here for the code.
And there are other versions, I omit here.


My question ?
1.Why my result is differ from the Formula2 when used the same rotation order ? Am I wrong ? (when compared to Formula2 in program, my formula is working well except the initial camera state)

2.The tutorial's Formula2 is computed using pure trigonemetry without matrix, Could you give a explanation or a specific reference link to the derivation of Formula2 (I can not find any resource of derivation but only result given)?

Best Answer

Why my result is differ from the Formula2 when used the same rotation order?

You used the rotation matrices $$\mathbf{R}_\text{pitch} = \left[\begin{matrix} 1 & 0 & 0 & 0 \\ 0 & \cos p & -\sin p & 0 \\ 0 & \sin p & \cos p & 0 \\ 0 & 0 & 0 & 1 \end{matrix}\right], \, \mathbf{R}_\text{yaw} = \left[\begin{matrix} \cos y & 0 & \sin y & 0 \\ 0 & 1 & 0 & 0 \\ -\sin y & 0 & \cos y & 0 \\ 0 & 0 & 0 & 1 \end{matrix}\right]$$ which do result in the combined rotation matrix $$\mathbf{R} = \mathbf{R}_\text{yaw} \mathbf{R}_\text{pitch} = \left[\begin{matrix} \cos y & \sin y \sin p & \sin y \cos p & 0 \\ 0 & \cos p & -\sin p & 0 \\ -\sin y & \cos y \sin p & \cos y \cos p & 0 \\ 0 & 0 & 0 & 1 \end{matrix}\right]$$ and rotating your front vector, $$\mathbf{R} \left [ \begin{matrix} 0 \\ 0 \\ -1 \\ 1 \end{matrix} \right ] = \left [ \begin{matrix} -\cos p \sin y \\ \sin p \\ -\cos p \cos y \\ 1 \end{matrix}\right ]$$ does yield the result you calculated. So, you haven't made any errors I can see.

Now consider what would happen if you had defined yaw ($y$) in a different way, say $y' = -y-\pi/2$. (That is, you'd consider zero yaw to be rotated ninety degrees clockwise from your current consideration, and counted positive clockwise, instead of positive counterclockwise rotation.)

If you substitute $y'$ for $y$ above, and simplify the expressions (noting that $\cos(-x-\pi/2) = -\sin(x)$ and $\sin(-x-\pi/2) = -\cos(x)$), you'll find that $$\mathbf{R}' \left [ \begin{matrix} 0 \\ 0 \\ -1 \\ 1 \end{matrix} \right ] = \left [ \begin{matrix} \cos p \cos y \\ \sin p \\ \cos p \sin y \\ 1 \end{matrix}\right ]$$

Thus, the answer to your question is, you used different direction of rotation and zero point for yaw $y$.

The tutorial's Formula2 is computed using pure trigonometry without matrix. Could you give a explanation or a specific reference link to the derivation of Formula2?

It is basically just introducing and defining a spherical coordinate system and the conversions between spherical and cartesian coordinates, except that it completely ignores the radial coordinate $r$, limiting to the surface of an unit sphere ($r = 1$).

The key point is that that tutorial is not really rotating anything at all; it is just defining a single unit vector, using two angular parameters -- pitch and yaw.

If you are not yet familiar with spherical coordinate systems, or wish to find introductions and geometric derivations for them, I suggest you do a web search for introduction to spherical coordinates, and pick the link(s) that suit you best.

Related Question