How to get camera up vector from roll, pitch, and yaw?
Asked Answered
L

3

5

I need to get an up vector for a camera (to get the right look) from a roll, pitch, and yaw angles (in degrees). I've been trying different things for a couple hours and have had no luck :(. Any help here would be appreciated!

Luxembourg answered 15/10, 2009 at 21:32 Comment(2)
Since you tagged it with all of the major graphics libraries, I take it it's safe to assume that you mean how to do it in general.Nematic
Yep Blaenk that is correct... I just needed some help with the maths :DLuxembourg
D
6

Roll, Pitch and Yaw define a rotation in 3 axis. from these angles you can construct a 3x3 transformation matrix which express this rotation (see here how)
After you have this matrix you take your regular up vector, say (0,1,0) if 'up' is the Y axis and multiply it with the matrix. What you'll get is the transformed up vector.

Edit-
Applying the transformation to (0,1,0) is the same thing as taking the middle row. The 3 rows of the matrix make up an orthogonal base of the rotated system. Mind you that a 3D graphic API uses 4x4 matrices. So to make a 4x4 matrix out of the 3x3 rotation matrix you need to add a '1' at M[3][3] (the corner) and zeros at the rest like so:

r r r 0
r r r 0 
r r r 0
0 0 0 1
Dolhenty answered 15/10, 2009 at 21:58 Comment(2)
Shoosh, what if I don't apply the transformation to the (0,1,0)? Can I just pull out one of the rows/columns from the rotation matrix as my up? ThanksLuxembourg
Yes, instead of doing the vector-matrix transformation, you can simplify the equation by leaving out the zero-sums/terms. Also, vectors can be column-major or row-major. Make sure you extract the axis correctly.Solleret
F
1

This may not directly answer your question, although it may still help. I have a free open-source project for XNA that creates a debug terminal that overlays your game while it is running. You can use this for looking up values, invoking methods, or whatever. So if you have a transformation matrix and you wanted to extract various parts of it while the game is running, you can do that. The project can be found here: http://www.protohacks.net/xna_debug_terminal

I don't have much expertise in the kind of math you are using, but hopefully Shoosh's post helps on that. Maybe the debug terminal can help you when trying out his idea or in any other future problems you encounter.

Flycatcher answered 5/11, 2009 at 16:24 Comment(1)
hah i can't wait to play with this later. It looks like a tool ive wanted for a while ... ive been having to write a console in my projects and output data there ... very very clunky.Nakitanalani
F
1

12 years later...

In case anyone is still interested in the answer to this question, here is the solution (even tough its in Java it should be pretty easy to translate it in other languages):

    private Vector3f getRayFromCamera() {
        float rx = (float)Math.sin((double)getYaw() * (double)(Math.PI / 180)) * -1 * (1-Math.abs((float)Math.cos((double)getPitch() * (double)(Math.PI / 180) - 90 * (Math.PI / 180)) * -1));
        float ry = (float)Math.cos((double)getPitch() * (double)(Math.PI / 180) - 90 * (Math.PI / 180)) * -1;
        float rz = (float)Math.cos((double)getYaw() * (double)(Math.PI / 180)) * -1 * (1- Math.abs((float)Math.cos((double)getPitch() * (double)(Math.PI / 180) - 90 * (Math.PI / 180)) * -1));
        return new Vector3f(rx, ry, rz);
    }

Note: This calculates the Front Vector but when multiplying with the vector (0,1,0) you can change that!

Freely answered 30/1, 2022 at 17:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.