# Camera math: How to calculate the horizontal and vertical angle from the look at vector

I'm using the PerspectiveCamera class in C#/WPF to implement an FPS style camera.

For that, I would need to offset the camera's horizontal and vertical angles every time the mouse is moved. The problem is that PerspectiveCamera doesn't store those angles and only provides a lookat vector.

So, is there a way to calculate those angles from the lookat vector ?

• Yes, it's just the spherical coordinate representation of the vector, but you shouldn't have to do that. Just store the angles in your own variables. – DMGregory Oct 11 '17 at 16:05

If there is no roll in your FPS camera (typically, there is not in an FPS) then all you need to do to get a yaw angle (-π..π ) from the vector is the atan2() function.

As suggested by DMGregory, the arcsin of the z component of the forward vector gives the tilt angle.

#include <math.h>
...

// calculate yaw
const float yaw_angle = atan2f( cam_fwd.y, cam_fwd.x );

// calculate pitch
const float pitch_angle = asinf( cam_fwd.z );


This assumes you know where the camera is looking (cam_fwd.)

• Remember OP is looking for two angles: a yaw and a pitch. They need two slightly different techniques. Don't forget to describe what you mean by lookat too — in some contexts this is used to mean a worldspace point the camera should look towards, but in your example above it looks like you mean it to be the camera's forward direction vector or the offset from the camera position to a point in the center of the camera's view some distance away. – DMGregory Oct 11 '17 at 16:53
• Thank you @DMGregory, I forgot about tilt. Answer adapted to calculate both yaw and tilt. – Bram Oct 11 '17 at 17:05
• You might want the arcsine of forward.z instead — this handles the sign naturally without an extra dot product checking step. – DMGregory Oct 11 '17 at 17:46
• @Bram Thanks :3 It's exactly what I've been looking for! – MattMatt2000 Oct 11 '17 at 20:44