Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 4 of 4

Thread: How can i calculate the Pith/Yaw of a camera to set up a 3rd Person View?

  1. #1
    Junior Member Newbie
    Join Date
    Jan 2017
    Posts
    3

    How can i calculate the Pith/Yaw of a camera to set up a 3rd Person View?

    Hello

    Im programming OpenGL in python language.

    Im actual stuck at the calculation of the 3rd Person View.

    Im currently moving the Camera around its y-axis (UP vector -> vec3(0.0, 1.0, 0.0)) when holding left mouse and moving it and saving it into a viewDirection variable.
    It means, the cam is looking at the viewDirection (glm::lookAt calculates the world to view matrix for me)

    I can also move the cam with "WASD" keys which crosses the view direction with the up vector and the movement-speed i have set.

    Now, i would like to attach my cam to an object, which im moving then with "WASD" and rotating around / up and down it with the mouse by holding left mouse key.

    The calculations tutorials are using a pitch and a yaw value, to make those calcs.

    How can i determine this angles with my given vectors/other values?


    Thanks for reading and also for helping me


    Greetings

    Fabiii

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,519
    If the camera were an object, the sequence of transformations to place and orient it would be:

    1. translate by target position
    2. rotate about Y by heading
    3. rotate about X by elevation
    4. translate along Z by distance

    I.e.:
    translate(target) * rotate_y(heading) * rotate_x(elevation) * translate(0,0,distance)

    But the view matrix is the inverse of the camera's transformation, so you need to invert that. Typically, you use the fact that
    (A.B)-1=B-1.A-1
    I.e. to invert a sequence of transformations, you invert the individual transformations and apply them in the reverse order, so:

    translate(0,0,-distance) * rotate_x(-elevation) * rotate_y(-heading) * translate(-target)

    E.g.:

    Code :
    glTranslatef(0, 0, -distance);
    glRotatef(-elevation, 1, 0, 0);
    glRotatef(-heading, 0, 1, 0);
    glTranslatef(-target.x, -target.y, -target.z);

  3. #3
    Junior Member Newbie
    Join Date
    Jan 2017
    Posts
    3
    Hello

    Thanks for your answer.

    So i have to remove my implementation with the view Direction and the up vector (world to view matrix would be the look at of the position, position + view direction and the up vector) to a translation matrix?

    greetings

  4. #4
    Member Regular Contributor
    Join Date
    May 2016
    Posts
    443
    Quote Originally Posted by Fabiii View Post
    So i have to remove my implementation with the view Direction and the up vector (...) to a translation matrix?
    no, you can do it however you want.

    the broader question is:
    --> how can i represent "orientations" in 3D space?

    and then:
    --> how do i generate transformation (to place objects) or view matrices (for camera) from orientations?

    you need to store a 3D position and a kind of rotation, for example by using quaternion.
    Code :
    using namespace glm;
     
    struct Orientation {
        vec3 Position;
        quat Rotation;
     
        mat4 Transformation() const { return translate(Position) * toMat4(Rotation); };
        mat4 View() const { return inverse(Transformation()); }
     
        void Pitch(float angle) { Rotation = rotate(Rotation, angle, vec3(1, 0, 0)); }
        void Yaw(float angle) { Rotation = rotate(Rotation, angle, vec3(0, 1, 0)); }
        void Roll(float angle) { Rotation = rotate(Rotation, angle, vec3(0, 0, 1)); }
    };

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •