# Calculate Euler yaw pitch and roll for a 3d point to face another

This may be a really basic question but the typical atan2 method isn't working when I write it in a program.

I need a camera object to face a certain target in 3D space.
I need to calculate the 3 Euler angles (yaw, pitch, roll) for the camera.

How can I do this?

-

1
The solution will not be unique since you can always rotate the camera on its axis and still have it pointing at a target. Like choosing portrait vs. landscape in a real camera. – Jyotirmoy Bhattacharya Oct 13 '10 at 23:36
You're right, but at least the X and Y can be calculated to face the point, if not a proper Z (roll). – Jarvis Oct 14 '10 at 5:06
Can you explain exactly how your use of two-argument arctangent is failing? It seems to me that any correction to your algorithm would involve the addition or subtraction of some multiple of $\pi/2$. – J. M. Oct 20 '10 at 11:07

## 1 Answer

From looking at your diagram, it appears to me that the transformation takes the unit vector $(i_x,i_y,i_z)$ and rotates it to point to $(f_x,f_y,f_z)$:
$$\begin{pmatrix}c_\Psi&s_\Psi&0\\-s_\Psi&c_\Psi&0\\0&0&1\end{pmatrix} \begin{pmatrix}1&0&0\\0&c_\theta&-s_\theta\\0&s_\theta&c_\theta\end{pmatrix} \begin{pmatrix}c_\phi&-s_\phi&0\\s_\phi&c_\phi&0\\0&0&1\end{pmatrix} \begin{pmatrix}i_x\\i_y\\i_z\end{pmatrix} = \begin{pmatrix}f_x\\f_y\\f_z\end{pmatrix}$$
where $c_\phi = \cos(\phi), s_\phi = \sin(\phi)$, etc.

It isn't hard to solve for $\phi,\theta$ and $\Psi$ if you assume that your camera begins by pointing in a convenient direction such as $(i_x,i_y,i_z) = (0,0,1)$.

-

Uh, I should add that when writing these things out, I normally have (a) wrong signs and (b) wrong orders of matrix multiplication. – Carl Brannen Mar 23 '11 at 0:27
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 7, "mathjax_display_tex": 1, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "github"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "english_confidence": 0.9288419485092163, "perplexity_flag": "middle"}