# OpenGL FPS Camera

This topic is 2106 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I recently went back to look at some old OpenGL(3.x+) programs I did and checked my camera class. In my camera class I have a function which calculates the view matrix. The calculations are based on OpenGL's gluLookAt function

Here's what the class looks like (I've omitted several identical and irrelevant functions for less code):

#include "camera.h"

//Create view matrix
glm::mat4 Camera::createViewMatrix()
{
glm::vec3 lookDirection = glm::normalize(cameraTargetPos); //Look direction of camera
static glm::vec3 upDirection = glm::normalize(glm::vec3(0.0f, 1.0f, 0.0f)); //Up direction of camera in the world, aligned with world's y-axis.
glm::vec3 rightDirection = glm::normalize(glm::cross(lookDirection, upDirection)); //Right direction of camera
glm::vec3 perpUpDirection = glm::cross(rightDirection, lookDirection); //Re-calculate up direction, basis vectors may not be orthonormal

static glm::mat4 viewMatrixAxes;

//Create view matrix, [0] is first column, [1] is second etc.
viewMatrixAxes[0] = glm::vec4(rightDirection, 0.0f);
viewMatrixAxes[1] = glm::vec4(perpUpDirection, 0.0f);
viewMatrixAxes[2] = glm::vec4(-lookDirection, 0.0f);

viewMatrixAxes = glm::transpose(viewMatrixAxes); //Transpose for inverse

static glm::mat4 camPosTranslation; //Translate to position of camera

camPosTranslation[3] = glm::vec4(-cameraPosition, 1.0f);

viewMatrix = viewMatrixAxes*camPosTranslation;

return viewMatrix;
}

{
glm::vec3 spherCameraTarget(cartesianToSpherical(cameraTargetPos)); //Convert camera target to spherical coordinates
cameraTargetPos = sphericalToCartesian(spherCameraTarget); //Convert camera target to cartesian coordinates
}

{
glm::vec3 spherCameraTarget(cartesianToSpherical(cameraTargetPos)); //Convert camera target to spherical coordinates

spherCameraTarget.z = glm::clamp(spherCameraTarget.z, 0.0001f, PI); //Clamp the pitch rotation between [0 PI] radians

cameraTargetPos = sphericalToCartesian(spherCameraTarget); //Convert camera target to cartesian coordinates
}

void Camera::moveBackward(GLfloat moveSpeed)
{
cameraPosition.x += (viewMatrix[0][2]*moveSpeed);
cameraPosition.y += (viewMatrix[1][2]*moveSpeed);
cameraPosition.z += (viewMatrix[2][2]*moveSpeed);
}

//Note: Change from standard convention. Z-axis is Y-axis, Y-axis is Z-axis and Z-axis should be negative
//Change this in formula below
glm::vec3 Camera::cartesianToSpherical(glm::vec3 cartesianCoordinate)
{
GLfloat r = (sqrt(pow(cartesianCoordinate.x, 2) + pow(cartesianCoordinate.y, 2) + pow(cartesianCoordinate.z, 2)));

GLfloat theta = atan2(-cartesianCoordinate.z, cartesianCoordinate.x);
GLfloat phi = acos(cartesianCoordinate.y/r);

glm::vec3 sphericalCoordinate(r, theta, phi);

return sphericalCoordinate;
}

//Note: See notes for cartesianToSpherical() function
glm::vec3 Camera::sphericalToCartesian(glm::vec3 sphericalCoordinate)
{
GLfloat theta = sphericalCoordinate.y;
GLfloat phi = sphericalCoordinate.z;

glm::vec3 cartesianCoordinate(cos(theta)*sin(phi), cos(phi), -sin(theta)*sin(phi));

return cartesianCoordinate * sphericalCoordinate.x;
}

This worked as expected. I could strafe left/right, up/down, forward/backwards and I could rotate around my up vector 360 degrees and rotate around my right vector +-90 degrees. However I realized that according to the OpenGL documentation I'm calculating my view matrix wrong. If you look the first line of code in the createViewMatrix() function then you can see that my look direction is only a normalized camera look-at point. According to the documentation it should be (cameraTargetPos-cameraPosition). When I corrected it the entire camera system broke down, both the strafing and rotation. I fixed the strafing so it works now as it should, here's the new code (note that the view matrix is calculated correctly now and that all strafe methods update the camera look-at point):

//Create view matrix
glm::mat4 Camera::createViewMatrix()
{
glm::vec3 lookDirection = glm::normalize(cameraTargetPos-cameraPosition); //Look direction of camera
static glm::vec3 upDirection = glm::normalize(glm::vec3(0.0f, 1.0f, 0.0f)); //Up direction of camera in the world, aligned with world's y-axis.
glm::vec3 rightDirection = glm::normalize(glm::cross(lookDirection, upDirection)); //Right direction of camera
glm::vec3 perpUpDirection = glm::cross(rightDirection, lookDirection); //Re-calculate up direction, basis vectors may not be orthonormal

static glm::mat4 viewMatrixAxes;

//Create view matrix, [0] is first column, [1] is second etc.
viewMatrixAxes[0] = glm::vec4(rightDirection, 0.0f);
viewMatrixAxes[1] = glm::vec4(perpUpDirection, 0.0f);
viewMatrixAxes[2] = glm::vec4(-lookDirection, 0.0f);

viewMatrixAxes = glm::transpose(viewMatrixAxes); //Transpose for inverse

static glm::mat4 camPosTranslation; //Translate to position of camera

camPosTranslation[3] = glm::vec4(-cameraPosition, 1.0f);

viewMatrix = viewMatrixAxes*camPosTranslation;

return viewMatrix;
}

void Camera::moveBackward(GLfloat moveSpeed)
{
cameraTargetPos.x += (viewMatrix[0][2]*moveSpeed);
cameraTargetPos.y += (viewMatrix[1][2]*moveSpeed);
cameraTargetPos.z += (viewMatrix[2][2]*moveSpeed);

cameraPosition.x += (viewMatrix[0][2]*moveSpeed);
cameraPosition.y += (viewMatrix[1][2]*moveSpeed);
cameraPosition.z += (viewMatrix[2][2]*moveSpeed);
}



The rest of the functions in the old code (cartesianToSpherical(), yawRotation etc.) remain unchanged.

The rotation still remains broken. If I'm close enough to my object it works as it should. It's hard to describe how it's broken. If, for example I move my camera far back enough that the camera look-at point  has a positive value (it starts with a negative value a first) one of the spherical coordinates (theta) ends up being negative so when I rotate around the up vector to the left I end up rotating right instead, not only that but I never complete a whole revolution. It's as if I rotate CW 45 degrees and then CCW 45 degrees, it just keeps going on like that. There's some other weird behaviour that goes on as well.

I'm quite certain it has to do with how I go back and forth between cartesian/spherical coordinates and the formulas I use but I'm lost in how to solve it. That I got it to work properly with wrong code is a miracle in itself already.

If you want to try and compile the code and run it to see the effect yourself let me know and I'll attach the source code here.

Any help is appreciated, thanks!

Edited by Suen

##### Share on other sites

Suen,

I'm just going to throw a few things at you.  I think you may be confusing yourself.  I'd rename some of your variables like 'cameraTarget' should be 'cameraTargetPos' so you clearly identity a position VS a direction vector.  It's important not to get those confused.

For rotation, you can just easily make a rotation matrix and multiply that by your viewMatrix.

I'm not familiar with the glm library, but I saw a glm::rotate function that I believe makes a rotation matrix.  Then you can just do...

viewMatrix = rotationMatrix*viewMatrixAxes*camPosTranslation;

(or put rotationMatrix at the end) -- I can't remember what way OpenGL multiplies it's matrices.

Good luck!

Jeff.

##### Share on other sites

Suen,

I'm just going to throw a few things at you.  I think you may be confusing yourself.  I'd rename some of your variables like 'cameraTarget' should be 'cameraTargetPos' so you clearly identity a position VS a direction vector.  It's important not to get those confused.

For rotation, you can just easily make a rotation matrix and multiply that by your viewMatrix.

I'm not familiar with the glm library, but I saw a glm::rotate function that I believe makes a rotation matrix.  Then you can just do...

viewMatrix = rotationMatrix*viewMatrixAxes*camPosTranslation;

(or put rotationMatrix at the end) -- I can't remember what way OpenGL multiplies it's matrices.

Good luck!

Jeff.

Well changing to this actually solved the problem, thanks. Thanks for the name-change suggestion, you're right that I shouldn't confuse a position with a vector, my bad.

But honestly I'm still wondering what exactly I'm doing wrong in the original code. I'm merely changing the position of my camera's reference(target) point. To rotate it I switch to a spherical coordinate system, alter the correct element of the coordinate and switch back to the cartesian coordinate system, and then calculate the look/forward vector. The camera position remain the same but since the camera's reference point now has changed (cameraTargetPos-cameraPos) should result in a new vector which should be rotated by some amount. Am I thinking this wrong?

edit: changed the name of cameraTarget to cameraTargetPos in my first post as suggested.

Edited by Suen

##### Share on other sites

1. Put a Projection/Perspective FOV Matrix on the camera class;
2. Try do no not expand the class for simple jobs;
3. Always look for a simple solution for control (something like: camera->Control(Input* input, float dt) );
4. Don't use Quaternions if you don't know what they do;

• 21
• 11
• 9
• 17
• 13