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

• 11-27-2017, 08:27 AM
Fabiii
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
• 11-27-2017, 07:28 PM
GClements
If the camera were an object, the sequence of transformations to place and orient it would be:

1. translate by target position
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);```
• 11-28-2017, 09:12 AM
Fabiii
Hello

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
• 11-29-2017, 03:03 AM
john_connor
Quote:

Originally Posted by Fabiii
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.

```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)); } };```