• FEATURED

View more

View more

View more

Image of the Day Submit

IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

First Person Games - Camera help!

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

12 replies to this topic

#1YodaTheCoder  Members

Posted 19 October 2001 - 08:15 PM

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

#2Maximus  Members

Posted 19 October 2001 - 08:41 PM

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

#3YodaTheCoder  Members

Posted 19 October 2001 - 08:50 PM

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

#4Maximus  Members

Posted 19 October 2001 - 10:59 PM

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();

#5easyBob  Members

Posted 20 October 2001 - 04:15 AM

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

#6YodaTheCoder  Members

Posted 20 October 2001 - 07:26 AM

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

#7YodaTheCoder  Members

Posted 20 October 2001 - 07:32 AM

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

#8Anonymous Poster_Anonymous Poster_*  Guests

Posted 20 October 2001 - 12:52 PM

Why dont you just move the camera?

#9masonium  Members

Posted 20 October 2001 - 01:37 PM

texture mapping polygons is fine

#10Dactylos  Members

Posted 20 October 2001 - 01:46 PM

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.

#11Maximus  Members

Posted 20 October 2001 - 01:56 PM

gluLookAt isnt an OpenGL function, rather its a utility function that generally makes it easier to move the world and make it look directly at a specific location.

For a 1st person shooter, you dont want to have to calculate the specific location that the camera is to look at when you move around. gluLookAt internally calls glTranslatef and glRotatef, and needs to be in the same area of the code anyway.

#12YodaTheCoder  Members

Posted 21 October 2001 - 05:35 AM

Okay, want to bore yourselves in my pathetic code? Here it is (compiled perfectly with MSVC6.0) NOTE: There are some includes and variables that are declared and not used yet; theyre for later.

#include
#include
#include
#include
#include
#include "main.h"

bool forward; //Movement bool''s
bool backward;
bool turnleft;
bool turnright;
bool strafeleft;
bool straferight;

float FBpos=0.0; //front back position
float LRpos=0.0; //left right position

//Function Declarations
void keysDown(unsigned char key, int x, int y);
void keysUp(unsigned char key, int x, int y);
void renderScene(void);
int moveFB(int direction);
int moveLR(float angle);

int main(int argc, char **argv){
glutInit(&argc, argv);
glutInitWindowPosition(100,100);
glutInitWindowSize(550,550);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
glutCreateWindow("<-Hello world!->");
glutFullScreen();
glutReshapeFunc(changeSize);
glutDisplayFunc(renderScene);
glutKeyboardUpFunc(keysUp);
glutKeyboardFunc(keysDown);
glutIdleFunc(renderScene);

glutMainLoop();
return 0;
}

//Keyboard - Keydown Function
void keysDown(unsigned char key, int x, int y){
switch(key){
case ''w'': forward=1; break;
case ''s'': backward=1; break;
case ''a'': turnleft=1; break;
case ''d'': turnright=1; break;

case 27: exit(1); break;
default: break;
}
cout<}

//Keyboard - Keyup Function
void keysUp(unsigned char key, int x, int y){
switch(key){
case ''w'': forward=0; break;
case ''s'': backward=0; break;
case ''a'': turnleft=0; break;
case ''d'': turnright=0; break;

default: break;
}
cout<}

//Terrain Function
void renderScene(void){
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);

if(forward){
FBpos+=0.1;
moveFB(1);
}
if(backward){
FBpos-=0.1;
moveFB(-1);
}
if(turnleft){
LRpos-=0.01;
moveLR(LRpos);
}
if(turnright){
LRpos+=0.01;
moveLR(LRpos);
}

//DRAW THE FLOOR
glPushMatrix();
glBegin(GL_POLYGON);
glColor3f(GREEN);
glVertex3f( 2.5f,-0.5f,-2.5f);
glColor3f(RED);
glVertex3f( 2.5f,-0.5f, 7.5f);
glColor3f(BLUE);
glVertex3f(-2.5f,-0.5f, 7.5f);
glColor3f(WHITE);
glVertex3f(-2.5f,-0.5f,-2.5f);
glEnd();
glPopMatrix();

//DRAW THE CEILING
glPushMatrix();
glBegin(GL_POLYGON);
glColor3f(BLUE);
glVertex3f( 20.5f, 5.5f,-20.5f);
glColor3f(BLUE);
glVertex3f( 20.5f, 5.5f, 70.5f);
glColor3f(TEAL);
glVertex3f(-20.5f, 5.5f, 70.5f);
glColor3f(TEAL);
glVertex3f(-20.5f, 5.5f,-20.5f);
glEnd();
glPopMatrix();

glutSwapBuffers();
}

NOW IN another file, main.h, I have the following:

#include
#include

#define WHITE 1.0f, 1.0f, 1.0f
#define TEAL 0.0f, 1.0f, 1.0f
#define BLUE 0.0f, 0.0f, 1.0f
#define RED 1.0f, 0.0f, 0.0f
#define GREEN 0.0f, 1.0f, 0.0f
#define BLACK 0.0f, 0.0f, 0.0f

static float angle=0.0,ratio;
static float x=0.0f,y=0.0f,z=0.0f;
static float lx=0.0f,ly=0.0f,lz=-1.0f;

int moveFB(int direction){ //''1'' if forward ''-1'' if backwards
x += direction*(lx)*0.1;
z += direction*(lz)*0.1;
gluLookAt(x, y, z,
x + lx, y + ly, z + lz,
0,1,0);
return 0;
}
int moveLR(float angle){
lx = sin(angle);
lz = -cos(angle);
gluLookAt(x, y, z,
x + lx, y + ly, z + lz,
0,1,0);
return 0;
}

void changeSize(int w, int h) {

if(h == 0)
h = 1;

float ratio = 1.0* w / h;

glMatrixMode(GL_PROJECTION);

glViewport(0, 0, w, h);

gluPerspective(45,ratio,1,1000);
glMatrixMode(GL_MODELVIEW);
gluLookAt(0.0,0.0,5.0,
0.0,0.0,-1.0,
0.0f,1.0f,0.0f);
}

Look newbie-ish? Ya, thought so... Compile/run it, it looks okay! (now all I need to learn is texture mapping).. which im on right now =)

~Jesse

#13YodaTheCoder  Members

Posted 21 October 2001 - 05:37 AM

By the way, the movement controls are

w = forward
s = bakcwards
a = turn left
d = turn right

Ill eventually use the arrow keys for strafing..

~Jesse

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.