View Full Version : Problem rotating camera

05-26-2010, 04:40 PM
I am new to opengl and trying to do an assignment for a class based on graphics and programming opengl in c++

I am having trouble trying to rotate the camera:
LastMove, lookAt and camPos are structs that have an x, y and z value. camPitch and camYaw are floats that use the mouse movement as input. Here is my function:

void Camera::Rotate(){
//keep up vector
up = Vector(0,0,1);

//use the mouse movement to rotate cam up/down
camPitch = lastMove.x/3;
camYaw = lastMove.y/3;

//check boundaries of yaw and pitch
while(camYaw > 360.0) camYaw -= 360.0;
while(camYaw < 0.0) camYaw += 360.0;
while(camPitch > 360.0) camPitch -= 360.0;
while(camPitch < 0.0) camPitch += 360.0;


//while w is pressed, movement.x = 1
//while s is pressed, movement.x = -1
//while neither is pressed, movement.x = 0
camPos.x += movement.x;
camPos.y += movement.y;
camPos.z += movement.z;

//rotate side to side
lookAt.y = sin(camPitch) + camPos.y;
lookAt.x = -cos(camPitch) + camPos.x;
lookAt.z = camPos.z + sin(camYaw);

ckuLookAt(camPos, lookAt, up);

My first question is that, although the side to side rotation is fine, i cant seem to rotate up and down properly? (note that x is back and forward, y is side to side and Z is up and down). When you look up and down, it gets to a point and reverses. Like the sin(camYaw) begins to go negative.

I noticed that with (above):

lookAt.y = sin(camPitch) + camPos.y;
lookAt.x = -cos(camPitch) + camPos.x;
lookAt.z = camPos.z + sin(camYaw);

i perform an operation on camPitch for both .y and .z
BUT with camYaw, i only use it on .z
Am i supposed to add to another direction?

My second question is, how to i update the W,S,A,D to strafe with the camera direction?