3
votes

I'm trying to find the yaw, pitch and roll angles of a camera, assuming that I have the position of the camera, it's look_at point (target point) and it's up vector. My best try was by using the following code

zaxis = lookat-position
xaxis = cross(up, xaxis)
yaxos = cross(zxis, xaxis)

Then I find the angles between each axis and the normal vectors (1,0,0) (0,1,0) and (0,0,1) and assign them to roll, yaw and pitch, but it doesn't seem to work

Any ideas, what I'm doing wrong? Thanks in advance :)

1

1 Answers

3
votes

You won't be able to get the roll angle - as that could be anything, but you can get the elevation and azimuth (pitch and yaw). I've found some old C code which I'll translate to pseudo code, so assuming that your vector isn't zero length:

Vector3 v = lookat - position;
double length = v.Length();

double elevation = asin(v.y / length);
double azimuth;

if (abs(v.z) < 0.00001)
{
    // Special case
    if (v.x > 0)
    {
        azimuth = pi/2.0;
    }
    else if (v.x < 0)
    {
        azimuth = -pi/2.0;
    }
    else
    {
        azimuth = 0.0;
    }
}
else
{
    azimuth = atan2(v.x, v.z);
}