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


  • 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.





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

The Main Question

So how would I go about calculating the yaw?


This is the orientation of my axis.

(source: wikispaces.com)

Answer - Thanks to Omnomnomnom




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


  • $\begingroup$ I suggest looking into spherical coordinates. $\endgroup$ – Omnomnomnom Aug 18 '13 at 0:54
  • $\begingroup$ 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. $\endgroup$ – chubakueno Aug 18 '13 at 1:04
  • $\begingroup$ Also, you should clarify your orientation system. However takaing in the three axis when calculating the pitch says that you are doing something wrong. $\endgroup$ – chubakueno Aug 18 '13 at 1:14
  • $\begingroup$ @Omnomnomnom could you provide an example? because I just tried that and got a better result, though overall fail. $\endgroup$ – 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˚ $$

  • $\begingroup$ The x, y, z, what are they? are they the camera x, y, z or the target x, y, z or what? $\endgroup$ – Vallentin Aug 18 '13 at 1:41
  • $\begingroup$ 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. $\endgroup$ – Omnomnomnom Aug 18 '13 at 1:43
  • $\begingroup$ 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)$$ $\endgroup$ – Omnomnomnom Aug 18 '13 at 1:49
  • $\begingroup$ 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. $\endgroup$ – Vallentin Aug 18 '13 at 1:50
  • 1
    $\begingroup$ 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. $\endgroup$ – Omnomnomnom Aug 18 '13 at 2:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.