First Person Camera

Started by
14 comments, last by Drakon 22 years, 5 months ago
I''m playing around with OpenGL now, and I thought I''d try to make a simple (VERY simple) first person camera. As far as I can tell, it works to a small extent. (You can only move backwards.) I doubt this is how most of them work, but am I headed in the right direction? NOTE: I put all this in a seperate .cpp and have it connected to the main OpenGL .cpp. #include #include #include #include int Rendering() // also handles moving { static GLfloat ccx = 0.0f; static GLfloat ccy = 0.0f; static GLfloat ccz = 0.0f; static GLfloat cx; static GLfloat cy; static GLfloat cz; WINGDIAPI void APIENTRY glTranslatef (GLfloat x, GLfloat y, GLfloat z); { } if(GetAsyncKeyState( VK_DOWN )) { cz = ccz - 0.5f; ccz = cz; } glTranslatef(cx, cy, cz); glBegin(GL_TRIANGLES); glVertex3f(0.0f,0.0f,0.0f); glVertex3f(1.0f,0.0f,0.0f); glVertex3f(1.0f,1.0f,0.0f); glEnd(); return 0; }
"Go for the eyes, Boo, go for the eyes!"
Advertisement
Remember that opengl''s camera is really the eye coordinates, so if there is no camera to move around, you are forced to move the entire world. Here is a snippet of my code from a 3d game I wrote:

int moveLeftandRight(float angle){
lx = sin(angle);
lz = -cos(angle);
glLoadIdentity();
gluLookAt(x, y, z,
x + lx, y + ly, z + lz,
0,1,0);


return 0;
}

int moveForwardandBackward(int direction){ //''1'' if forward ''-1'' if backwards
x += direction*(lx)*SPEED;
z += direction*(lz)*SPEED;
glLoadIdentity();
gluLookAt(x, y, z,
x + lx, y + ly, z + lz,
0,1,0);
return 0;
}

Then, you can use BOOLs to detect keypresses, like

if(keyLeft){
LRpos-=0.01;
moveLeftandRight(LRpos);

Anyway, its the math that is hard for most people. Hope that helps.
moveLeftandRight(
Forgot to mention:

the camera coordinates are x,y,z and the view coordinates (the coordinates that you look at) are lx,ly,and lz.


So basically I need to change my translate stuff to the lookat stuff?
"Go for the eyes, Boo, go for the eyes!"
Ok I figured that stuff out... but how do you turn left instead of move left? Do you need to use glRotatef to do that?

Also: How do you use that key stuff? I tried it and it doesn't seem to work...

Edited by - Drakon on November 26, 2001 6:50:31 PM
"Go for the eyes, Boo, go for the eyes!"
Change the xcenter variable.
centerx just moves it left and right, not turn it.
"Go for the eyes, Boo, go for the eyes!"
Did you even TRY my code up there? That moveLeftandRight function DOES turn you... sheesh!

try it out, all you need is a few geometrical calls to gluLookAt
I tried it out but it gave me a few errors so I went back to trying it on my own...

What are cos and sin? They have anything to do with it?
"Go for the eyes, Boo, go for the eyes!"
Woah, if you don't know what cos and sin are then you probably are in Algebra I, right?

That's not bad though, you'll get there, but I'll try to give you the basic stuff.

GO HERE to learn about Trigonometry

Read those tutorials they should help you out. The math isn't terribly difficult.

Just read the first three tutorials, and they'll get you in the right direction. The rest is more advanced stuff, but nothing too difficult. When I took trig, I thought it was pretty easy.

After you read those, you should be able to infer what that snippet of code is actually doing.

Good luck!

EDIT: This is probably real important. The sin and cos functions in the code accept their angles in radians. You may not know what those are, but just think of them as degrees. You'll get used to it.

Edited by - Floppy on November 26, 2001 11:40:31 PM

This topic is closed to new replies.

Advertisement