PDA

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



Fabiii
11-27-2017, 08:27 AM
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

GClements
11-27-2017, 07:28 PM
If the camera were an object, the sequence of transformations to place and orient it would be:


translate by target position
rotate about Y by heading
rotate about X by elevation
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.:



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

Fabiii
11-28-2017, 09:12 AM
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

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


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