FPS Camera

Started by
6 comments, last by clarkm2 14 years, 10 months ago
After writing my first FPS camera view in OpenGl, I notices a possible artifact that has confused me. I'm curious if this is a known issue... When the camera spawns, the camera is viewing in the -z direction with +x to the right and +y is up. Linear motion of the mouse in the +/-y direction generates x axis rotation and linear mouse motion in the +/-x direction generates y axis rotation. Now when I move the mouse in a circular motion on the screen, I am seeing a noticeable rotation in the z-axis. The pseudocode is pretty straight forward:

GetMouseData();
GetKeyboardData();

rotz = 0; // always!

//Calculate Transform matrix based on update params from the mouse and keyboard
UpdateMatrix  = CalculateEulerMatrix(x,y,z,rotx,roty,rotz);

//Update "Current" World Transform
CurrentMatrix  = UpdateCurrentMatrix(UpdateMatrix);

//Transpose Current Matrix for openGl
oMTranspose = TransposeMatrix(CurrentMatrix);

//Convert 2D array to 1D Array for OpenGl
oMReshape = MatrixReshape(oMTranspose);
	
//Load Matix into ModelView
glLoadMatrixf(oMReshape);

I've checked the math and everything seems fine. The camera view is working as expected, but there is just this minor issue.
Advertisement
I'm just going to post my code. Your rotation code is wrong for some reason. Here I have a camera with a left/right angle called side angle and up/down angle called up angle. To create the matrix: translate, rotate on the side angle, rotate on the up angle. Hope it helps.

void Input(){    GetCursorPos(&mouse);	//up	if(GetAsyncKeyState(0x57))	{		gCamera->z-= (cos(gCamera->side_angle*(3.14/180)))*2;		gCamera->x+= (sin(gCamera->side_angle*(3.14/180)))*2;    		}	//down	if(GetAsyncKeyState(0x53))	{		gCamera->z+= (cos(gCamera->side_angle*(3.14/180)))*2;		gCamera->x-= (sin(gCamera->side_angle*(3.14/180)))*2; 	}	//left	if(GetAsyncKeyState(0x41))	{		gCamera->x-= (cos(gCamera->side_angle*(3.14/180)))*2;		gCamera->z-= (sin(gCamera->side_angle*(3.14/180)))*2; 	}	//right	if(GetAsyncKeyState(0x44))	{		gCamera->x+= (cos(gCamera->side_angle*(3.14/180)))*2;		gCamera->z+= (sin(gCamera->side_angle*(3.14/180)))*2; 	}		//mouse	if(GetAsyncKeyState(VK_MBUTTON))	{			if(mouse.x < 400 )		{			gCamera->side_angle-=1.3;		}		if(mouse.x > 400 )		{			gCamera->side_angle+=1.3;		}		if(mouse.y > 300)		{			gCamera->up_angle += .6;		}		if(mouse.y < 300)		{			gCamera->up_angle -= .6;		}		SetCursorPos(400,300);	}}CreateCameraMatrix(){    glMatrixMode(GL_MODELVIEW);    glLoadIdentity();	glRotatef(cam->up_angle, 1, 0, 0);	glRotatef(cam->side_angle, 0, 1, 0);	glTranslatef(-cam->x,-cam->y,-cam->z);		glGetFloatv(GL_MODELVIEW_MATRIX, cam->Camera_Matrix);}

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

And btw, convering a 2d array to a 1d array can be done like such: I'm assuming your using glMultMatrixf( glFloat* [16] ) ?

float M_44[4][4];
float M_16[16];

M_44[0][16] , you can index like this. 0*4 steps + 16 bytes.

And
glMultMatrixf( &(M_44[0][0] ); <-----treated as a 4x4 array.

Its all 16*float_size bytes in memory, its all stored the same. 2d arrays are just built easier for you as a programmer.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

Its a common problem Ive seen pop up, Ive had it myself. You try and rotate the camera in a circular motion, from right, up, left, down, repeat, almost like rolling your head and you start getting roll.
Id be interested in seeing the CalculateEulerMatrix(x,y,z,rotx,roty,rotz) and
UpdateCurrentMatrix(UpdateMatrix) functions to get a better idea of how your going about this.
I dont remember the exact reason why it happens, but I remember fixing it by not updating the orientation each frame, but accumulating the angles and then rebuilding the orientation from these angles, instead of updating or modifying the old orientation with new rotation information.
I just double checked all the math again and it seems fine. My Euler Matrix is defined as:

Rx    1 0 0 0    0 cos(rotx) sin(rotx) 0     0 -sin(rotx) cos(rotx) 0    x 0 0 1Ry      cos(roty) 0 -sin(roty) 0    0 1 0 0    sin(roty) 0 cos(roty) 0    0 y 0 1Rz     cos(rotz) sin(rotz) 0 0    -sin(rotz) cos(rotz) 0 0    0 0 1 0    0 0 z 1R = RxRyRz


Based on the glTranslatef description, (x,y,z) need to be defined down the 4th column. I'm not sure about the rotation params (R), but when I transposed the matrix the camera responded correctly (x,y rotation). I followed the Matrix index definition from the glLoadMatrix description, so I'm sure the Matrix reshape func is correct.

I'm really just trying to understand how to go from a Euler matrix to glLoadMatrix.



EDIT: The UpdateCurrentMatrix function just takes the current ModelView matrix and multiplies it by the updated Euler Matrix based on recent deltas in mouse position and key hits like so:

Current = Euler(Update) * Current;
You are building the matrix fine, but try not using any matrix built from a previous frame, instead build a new matrix each frame straight from the angles.

float rx, ry, rz;
// each frame, get mouse movement
rx += mouseMoveAsAngleAmount; // += signed amount, -/+ mouse moves left/right
ry += mouseMoveAsAngleAmount; // += signed amount, -/+ mouse moves up/down
rz = 0;
// build new matrix from these total amounts each frame
matrix = From(rx, ry, rz)
// use new matrix built each frame, discarding old
Load(matrix)
Thanks for the help, its working as expected now.
After testing out the camera, I noticed a few differences:

When the modelview matrix got "updated" based on my original implementation, the only issue was the z-axis rotation when moving the mouse in a circular pattern. I was able to view my world model inside and outside its boundaries and still have the feel of a camera.

Now that I'm accumulating mouse and keyboard data and calculating a new modelview matrix per frame, the camera has a different feel. Let me try to explain...

When I'm inside my world model (e.g. a simple skycube), I feel that the world model is rotating and not the camera. If I zoom out, well past the boundaries of the world model, I see the world model changing it orientation and not the camera. I'm no longer able to "fly" around the model as I did when I updated the modelview matrix. This approach seems to work while the camera is inside the world model but not outside.

The second approach seems to be an artifact of translating before rotating, but I can't explain these difference based on the implementation.

This topic is closed to new replies.

Advertisement