View Full Version : Pitch, Yaw, and Moving a Camera

11-09-2012, 01:14 PM

I'm having some trouble with making my camera move while maintaining its ability to pitch and yaw. I have it working at the origin, but I can't seem to figure out how to make it work when I start moving around. When I rotate the camera with my mouse, it works no matter where I am in the world. I guess my issue is that when I translate my camera with the WASD keys, there's always a fixed point that my camera automatically rotates to see. In my case, the starting position is (1,0,-1). So no matter which way I translate the camera, it always stares at this point. However, once I stop moving and rotate my camera with the mouse, two problems arise: 1) the camera doesn't smooth rotate at the start--it jerks to view the position where the camera was staring BEFORE the translate; 2) like the first problem, when I translate after a rotation, the camera automatically rotates to view the last position the camera was located. I understand why 2) is happening, but don't know how to fix it! Here's the code:

//camera globals
float g_angle = 0;
float g_trans = 0;
float strafe;
glm::vec3 up = glm::vec3(0,1,0);
glm::vec3 eye;
glm::vec3 look = glm::vec3(1,0,-1);
glm:: vec3 saveLook = glm::vec3(1,0,-1);
float startX = 0;
float startY = 0;
float endX = 0;
float endY = 0;
float alpha = 0;
float beta = 0;

//setting view matrix
void SetView() {
glm::mat4 view = glm::lookAt(eye,look,up);
safe_glUniformMatrix4fv(h_uViewMatrix, glm::value_ptr(view));

//main display function
void Draw (void)
//...set models and draw

//update camera
eye = glm::vec3(strafe,0,g_trans);
look = saveLook;
//other stuff

//the mouse button callback
void mouse(int button, int state, int x, int y) {
if (button == GLUT_LEFT_BUTTON) {
if (state == GLUT_DOWN) {

//the mouse move callback
void mouseMove(int x, int y) {

float deltaX = endX - startX;
float deltaY = endY - startY;

alpha += (M_PI/2 * deltaY)/2;
beta += (M_PI * deltaX)/2;

look = glm::vec3(cos(alpha)*cos(beta), sin(alpha), cos(alpha)*cos(90-beta));
look += eye;
saveLook = look;

startX = endX;
startY = endY;

The mouse functions are registered in main properly. And the program has the same behavior with out without saveLook the way I have it. It's just a recent addition I made in the debugging process which had no effect. I've also tried adding the value of the translate to the look vector (forward/backward add to look.z, left/right add to look.x), but that gives me some pretty strange behavior as well.

Any help related to how to make my translates not rotate the camera to where it was previously staring, but rather to keep staring the way it was pointing at the end of a rotate would be greatly appreciated :)