Titangale

05-20-2016, 03:05 PM

Hey!

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); // Disregard the hard-coded numbers, they just add some distance and height for testing purposes

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:

https://gfycat.com/TediousElementaryBarnswallow

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).

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); // Disregard the hard-coded numbers, they just add some distance and height for testing purposes

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:

https://gfycat.com/TediousElementaryBarnswallow

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).