First Person Games - Camera help!

Started by
11 comments, last by YodaTheCoder 22 years, 6 months ago
Please! Could someone (even pseudo code) tell me the basic idea behind a first person program? Something simple; One that you can walk around on the Z plane. Do you move the camera? The whole world? And how? If someone could explain gluLookAt, that''d be great! ~Jesse
Advertisement
Dont bother with gluLookAt, just use glTranslatef and glRotatef to move and rotate the "camera". I say "camera" because there is no real camera in OpenGL, the viewpoint is always at 0,0,0 facing in the same direction, you just move the world around the camera instead. It's really quite simple

For moving around, poll the keys you wish to use for moving/turning... if turning, increment/decrement an angle variable, and if moving forward/backward, use sine/cosine to work out how much to move the camera along the x & y axis, and increment a location vector with the values.

For instance:
if (Key_state[DI_UPARROW]) {
camera.origin[0] += (float)sin(camera.angle / 180 * PI) * MOVE_SPEED;
camera.origin[2] -= (float)cos(camera.angle / 180 * PI) * MOVE_SPEED;
}

In that code, Key_state is an array that holds the state of each key, whether its pressed (value of 1) or not (0).

Edited by - Maximus on October 20, 2001 3:43:36 AM
-----------------------"When I have a problem on an Nvidia, I assume that it is my fault. With anyone else's drivers, I assume it is their fault" - John Carmack
I think I understand what you said (partly). Where would the glRotatef and Translate go?

it looks like it would be something like

glRotatef(camera.angle, camera.x, camera.y, camera.z);

(If that''s even the right syntax)... But, where do I put it? A quick example is all I need... Thank you sir!

~Jesse
Opps... should have included a bit of that in my first post

When you start rendering your scene, before you draw the world but after setting up OGL to render, first rotate and then translate, for example:

glRotatef(camera.angles,0,1,0);
glTranslatef(-camera.origin[0],-camera.origin[1],-camera.origin[2]);

Im using the negative of the cameras origin (location) because we are moving the world backward instead of the camera, so the world moves backwards when the camera "moves" forward.

After doing that, you can proceed to draw your game worlds static geometry.

When moving onto the non-static items in the world, you make use of the matrix stack. Drawing non-static items can be done like so:

glPushMatrix();
glRotatef(); // set the rotating of the item
glTranslatef(); // set the location in the world
DrawNonStaticItem(); // draw item
glPopMatrix();


Hope this post has a bit more info
-----------------------"When I have a problem on an Nvidia, I assume that it is my fault. With anyone else's drivers, I assume it is their fault" - John Carmack
Just to let you know... If you plan on coding a first person shooter, and are having trouble with the rotation and translation, there is very little hope that you will be able to code a BSP/PVS. Although BSPs arent very hard to code, the PVS is very difficult to code and even harder to understand. But that is from my own experiences. I still think you should go ahead with your program though. Even if you dont write the best FPS in the world, or even finish, you will learn alot.

easyBob
RRkk... I know enough about where to put the functions like rotate and translate, but do youy think it would be wiser to move the whole world or the camera? Lets say I want something like Doom, UT, quake, you know... (im not going to get that good, but I''ll try =)

wouldnt I just do something like

if(user presses the up arrow){
camera position on z axis += 1;
}

or something?

bah, and I know I use gluLookAt, but I dont get the parameters in that either... bah!!!

~Jesse
Also, could someone tell me (newbie question?) the smartest way to make a floor? I was thinking of texture mapping, but I don''t know how yet... That smart? No?

I think I am just an anxious newbie..
Could someone help? =)

~Jesse
Why dont you just move the camera?
texture mapping polygons is fine
There is no camera. Your only choice is to move the world. This is often abstracted into a "camera" though, much like maximus described above.

This topic is closed to new replies.

Advertisement