# Calculate Camera Pitch & Yaw To Face Point

How do you calculate pitch & yaw for a camera so that it faces a certain 3D point?

Variables

• Camera X, Y, Z
• Point X, Y, Z

### Current Half Solution

Currently I know how to calculate the pitch, and I do that using the following.

$dx:=camera_x-point_x$

$dy:=camera_y-point_y$

$dz:=camera_z-point_z$

$pitch:=atan2(\sqrt{dz*dz+dx*dx},dy))$

Then if $(dy>0)$ pitch gets negated. ($pitch:=-pitch$)

### The Main Question

So how would I go about calculating the yaw?

### Edit

This is the orientation of my axis.

### Answer - Thanks to Omnomnomnom

$dx:=camera_x-point_x$

$dy:=camera_y-point_y$

$dz:=camera_z-point_z$

$pitch:=-atan2(dy, \sqrt{dx*dx+dz*dz})$

$yaw:=atan2(dz,dx)-90^\circ$

-
I suggest looking into spherical coordinates. –  Omnomnomnom Aug 18 '13 at 0:54
The yaw and the pitch should be practically the same thing when calculating this, just that the pitch is vertical and the yaw is horizontal. –  chubakueno Aug 18 '13 at 1:04
Also, you should clarify your orientation system. However takaing in the three axis when calculating the pitch says that you are doing something wrong. –  chubakueno Aug 18 '13 at 1:14
@Omnomnomnom could you provide an example? because I just tried that and got a better result, though overall fail. –  Vallentin Aug 18 '13 at 1:17

So, we're going to describe camera direction needed to direct the camera positioned at $(0,0,0)$ at an $(x,y,z)$ point using the angles from spherical coordinates. The two angles are $\theta$ and $\phi$.

$\theta$, the azimuthal angle, normally taken from $0˚$ to $360˚$, is the angle made in the $xy$ plane between the $x$-axis and the line connecting $(0,0,0)$ to $(x,y,0)$. Simply put, this gives us our "yaw".

$\phi$, the polar angle, normally taken from $0˚$ to $180˚$, is the angle made between the $z$-axis and the line connecting $(0,0,0)$ to $(x,y,z)$. This gives us something like the pitch. That is, $\phi=90˚$ means that you're looking horizontally, whereas $\phi=0˚$ means that you're looking vertically upward.

Now, for a given point $(x,y,z)$, the calculations are as follows: $$\theta = \arctan\left(\frac yx\right)\\ \phi = \arctan\left(\frac {\sqrt{x^2+y^2}}z\right)$$ So for example: to point your camera at the point $(1,1,1)$, you would need the angles $$\theta = \arctan\left(\frac 11\right)=45˚\\ \phi = \arctan\left(\frac {\sqrt{2}}1\right) \approx 55˚$$

-
The x, y, z, what are they? are they the camera x, y, z or the target x, y, z or what? –  Vallentin Aug 18 '13 at 1:41
Sorry, the camera's coordinates are taken to be $(0,0,0)$, and the target's coordinates are $(x,y,z)$. If you're given a camera point and a target point, you could subtract the camera point from both to make this work. –  Omnomnomnom Aug 18 '13 at 1:43
To put this in the language of your question: given a (dx,dy,dz), we calculate $$\theta=\operatorname{atan2}(dy,dx)\\ \phi = \tan(\sqrt{dx^2+dy^2},z)$$ –  Omnomnomnom Aug 18 '13 at 1:49
Well that didn't work, the camera is completely facing away and rotating weirdly no matter how I mode it around. When I move the camera up and down [Y] then camera faces down and up correctly though it also rotates left and right. When I move the camera in either X and/or Z then camera makes all kinds of weird turns. –  Vallentin Aug 18 '13 at 1:50
Okay, in this case, your clockwise yaw ($0˚$ in direction of $x$-axis, increasing the angle moves you clockwise from above in the $xz$ plane) is given by $$a_1=\operatorname{atan2}(dz,dx)$$ you pitch, as in the angle that your direction makes with the $xz$ horizontal, is given by $$a_2 = \operatorname{atan}(dy,\sqrt{dx^2+dz^2})$$ This gives you an angle of $0˚$ when looking horizontally, and $90˚$ when looking along the $y$-axis. –  Omnomnomnom Aug 18 '13 at 2:01