Jump to content
  • Advertisement
Sign in to follow this  
clarkm2

OpenGL FPS Camera

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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);

}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 1

Ry
cos(roty) 0 -sin(roty) 0
0 1 0 0
sin(roty) 0 cos(roty) 0
0 y 0 1

Rz

cos(rotz) sin(rotz) 0 0
-sin(rotz) cos(rotz) 0 0
0 0 1 0
0 0 z 1


R = 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;

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!