Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding roll, yaw and pitch of a camera, having it's position, target and up vectors

Tags:

math

graphics

3d

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 :)

like image 939
mjekov Avatar asked Dec 21 '25 00:12

mjekov


1 Answers

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);
}
like image 137
ChrisF Avatar answered Dec 24 '25 01:12

ChrisF



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!