Marius Stubberud
Marius Stubberud

Reputation: 29

Calculating position of a third-person camera

I've recently been following the LearnOpenGL tutorials, up to and including their lighting section. Previously, for a graphics-oriented math course, I've made Lunar Lander like physics in 2D, and now I'm trying to translate that into 3D. While the physics themselves are going nicely, I'm struggling with the camera. At first it was simply a camera hovering behind the object and following it around, which was quite simple to do. But now I'm trying to implement rotation based on mouse input, and I just can't get it look good.

What I want is a normal third-person camera like in any modern 3D game. I'm currently only trying to set the X and Z position based on yaw (xoffset supplied by mouse input). My view matrix is calculated like this:

glm::lookAt(cameraPosition, focusTarget, cameraUp);

My focus target is simply the normalized vector between the current cameraPosition and the position of the parent object (the one the camera should follow). The up vector is the normalized cross product of the right vector and the target vector, and the right vector is the normalized cross product of the target vector and the world up vector (0.0f, 1.0f, 0.0f).

I calculate the position of the camera like this:

glm::vec4 cp;
cp.x = hoverRadius * sin(glm::radians(yaw));
cp.z = hoverRadius * cos(glm::radians(yaw));

cameraPosition = glm::vec3(parentPosition->x + cp.x, parentPosition->y + 3.0f, parentPosition->z + cp.z + 20.0f);

It made sense to me to base the x and z values off of sine and cosine, using the yaw as the angle, but the result ends up looking like this when I swipe my mouse left and right:

https://zippy.gfycat.com/TediousElementaryBarnswallow.webm

Obviously, this is not what I want. First off, because the distance from the sphere doesn't stay the same, and secondly because it's orbiting in an ellipsis behind the sphere rather than a circle around it.

So, does anyone know how to fix this? Alternatively, could someone point me towards a resource/tutorial that could help me program a functional third-person camera? I've tried googling this for hours, and haven't managed to find something myself, but I also don't really know what to google.

Also, if you need more detail about my code, I'll gladly supply my camera code in its entirety (which is largely based on the LearnOpenGL camera class).

Upvotes: 1

Views: 1592

Answers (1)

Anselme François
Anselme François

Reputation: 74

It looks like somewhere, the camera matrix is scaled on the X axis, are you sure there isn't a glm::scale somewhere, maybe a multiplication in the vertex shader ?

Also, do you know that you can use glm::rotate and glm::translate on vectors or on positions instead of manually computing cosines and angles ? There are a lots of overloaded operators in glm too that can help you make your code more readable, example :

glm::vec3 offset(0, 3, 20);
glm::vec3 cp = glm::rotate(glm::vec3(0, 0, hoverRadius), yaw, glm::vec3(0, 1, 0));

cameraPosition = parentPosition + cp + offset;

(i'm not sure if this code is completely valid, i did not test it, but from memory, it should do approximatively what you did)

Upvotes: 0

Related Questions