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 ?

  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – 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.)

| improve this answer | |
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – DMGregory Oct 11 '17 at 16:53
  • \$\begingroup\$ Thank you @DMGregory, I forgot about tilt. Answer adapted to calculate both yaw and tilt. \$\endgroup\$ – Bram Oct 11 '17 at 17:05
  • 1
    \$\begingroup\$ You might want the arcsine of forward.z instead — this handles the sign naturally without an extra dot product checking step. \$\endgroup\$ – DMGregory Oct 11 '17 at 17:46
  • \$\begingroup\$ @Bram Thanks :3 It's exactly what I've been looking for! \$\endgroup\$ – MattMatt2000 Oct 11 '17 at 20:44

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.