# First Person Camera

Started by Drakon, Nov 25 2001 09:58 AM

15 replies to this topic

###
#1
Members - Reputation: **120**

Posted 25 November 2001 - 09:58 AM

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;
}

###
#2
Members - Reputation: **122**

Posted 25 November 2001 - 11:22 AM

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(

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(

###
#5
Members - Reputation: **120**

Posted 26 November 2001 - 10:10 AM

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

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

###
#10
Members - Reputation: **122**

Posted 26 November 2001 - 04:39 PM

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

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

###
#11
Members - Reputation: **120**

Posted 26 November 2001 - 04:47 PM

Ok, thanks

Yes, ... (*thinks... remembering which algebra I''m in*) Man... I dont even remember what algebra book I''m in, but I think its two... but we haven''t gotten to cos and sin yet...

Its far too late to read that stuff now, but I''ll look at it in the morning.

Yes, ... (*thinks... remembering which algebra I''m in*) Man... I dont even remember what algebra book I''m in, but I think its two... but we haven''t gotten to cos and sin yet...

Its far too late to read that stuff now, but I''ll look at it in the morning.

###
#13
Members - Reputation: **122**

Posted 27 November 2001 - 03:26 AM

There are three ways to get measurments of a triangle: sine, cosine, and tangent. Each are methodical to SOHCAHTOA, meaning:

sine = opposite over hypotenuse

cosine= adjacent over hypotenuse

tangent = opposite over adjacent

You need to know these because when you turn left, for example, you would decrement a variable ''float angle''. Then, you need to figure out whether the sin, cos, or tangent is needed.

In the case of turning, the most commonly used are the negative cosine and the sine.

gluLookAt takes 9 arguments. The first three are the camera position (x,y,z), which are unaffected when you turn. The ones that are affected are the next three arguments, (lx, ly, lz) which are the look-x, look-y, and look-z. They determine the point where the camera is looking.

So when you move forward, you need to figure out what angle measurement between you and the z axis (and x axis) is so that you can move in the proper direction.

The mathematical (actually, it''s geometrical) way of calculating this properly is in the functions that I gave you.

Set up a system of BOOLs. Like this:

bool turnleft; bool turnright; bool forward; bool backward;

Then, (assuming that you are using GLUT), set up your keyboard up functions and keyboard down functions.

float turnangle;

void main(int argc, char **argv){

...

glutKeyboardUpFunc(KeysUp);

glutKeyboardFunc(KeysDown);

...

glutDisplayFunc(RenderScene);

...

glutMainLoop();

}

void KeysDown(unsigned char key, int x, int y){

switch(key){

case ''a'': turnleft=1; break;

case ''d'': turnright=1; break;

case ''w'': forward=1; break;

case ''s'': backward=1; break;

default: break;

}

}

void KeysUp(unsigned char key, int x, int y){

switch(key){

case ''a'': turnleft=0; break;

case ''d'': turnright=0; break;

case ''w'': forward=0; break;

case ''s'': backward=0; break;

default: break;

}

}

void RenderScene(void){

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

...

if(turnleft){

turnangle-=0.01;

moveLeftandRight(angle);

}

if(turnright){

turnangle+=0.01;

moveLeftanfRight(angle);

}

if(forward){

moveForwardandBackward(1);

}

if(backward){

moveForwardandBackward(-1);

}

Get it now? Geometry is the only way you can interpret the angles easily. Because the default setting for translating forward would be along the z axis, you not only need to compenstate your lx,ly,lz coords by translating along the z axis, but also the x axis.

This, my friend, is why geometry is critical.

~Jesse Lawson,

www.proggin.com/tutorials/opengl/index.htm

sine = opposite over hypotenuse

cosine= adjacent over hypotenuse

tangent = opposite over adjacent

You need to know these because when you turn left, for example, you would decrement a variable ''float angle''. Then, you need to figure out whether the sin, cos, or tangent is needed.

In the case of turning, the most commonly used are the negative cosine and the sine.

gluLookAt takes 9 arguments. The first three are the camera position (x,y,z), which are unaffected when you turn. The ones that are affected are the next three arguments, (lx, ly, lz) which are the look-x, look-y, and look-z. They determine the point where the camera is looking.

So when you move forward, you need to figure out what angle measurement between you and the z axis (and x axis) is so that you can move in the proper direction.

The mathematical (actually, it''s geometrical) way of calculating this properly is in the functions that I gave you.

Set up a system of BOOLs. Like this:

bool turnleft; bool turnright; bool forward; bool backward;

Then, (assuming that you are using GLUT), set up your keyboard up functions and keyboard down functions.

float turnangle;

void main(int argc, char **argv){

...

glutKeyboardUpFunc(KeysUp);

glutKeyboardFunc(KeysDown);

...

glutDisplayFunc(RenderScene);

...

glutMainLoop();

}

void KeysDown(unsigned char key, int x, int y){

switch(key){

case ''a'': turnleft=1; break;

case ''d'': turnright=1; break;

case ''w'': forward=1; break;

case ''s'': backward=1; break;

default: break;

}

}

void KeysUp(unsigned char key, int x, int y){

switch(key){

case ''a'': turnleft=0; break;

case ''d'': turnright=0; break;

case ''w'': forward=0; break;

case ''s'': backward=0; break;

default: break;

}

}

void RenderScene(void){

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

...

if(turnleft){

turnangle-=0.01;

moveLeftandRight(angle);

}

if(turnright){

turnangle+=0.01;

moveLeftanfRight(angle);

}

if(forward){

moveForwardandBackward(1);

}

if(backward){

moveForwardandBackward(-1);

}

Get it now? Geometry is the only way you can interpret the angles easily. Because the default setting for translating forward would be along the z axis, you not only need to compenstate your lx,ly,lz coords by translating along the z axis, but also the x axis.

This, my friend, is why geometry is critical.

~Jesse Lawson,

www.proggin.com/tutorials/opengl/index.htm

###
#15
Members - Reputation: **127**

Posted 27 November 2001 - 02:38 PM

??? Huh ???

I have been searching for an answer on how to correctly move after turning, so I tried to use your code Zues_. However, just like all things, it screwed up. I can get it to turn very strangley, but, when I implemented the forward and backward movement code, it would stand still when I hit forward, when I hit back, it would rotate the screen, then when I hit forward again, it just screws up some more, and makes me frustrated. Does anyone have any other answers? Oh, and um, Zues_, you said that sin, and -cos are often used to determine this. Um, how is this? I''ve taken Algebra, but not Trigo yet. SOMEONE HELP ME! If you don''t, then I will have to calculate the additions to X and Z for all three hundred and sixty degrees of movement. Also, if I just add and subtract values from the camera''s X axis view, why does it stop when I reach 180 degrees and 360 degrees?

En taro Adun!

Doom to all who threaten the homeworld!

*Protoss Zealot - Starcraft*

I have been searching for an answer on how to correctly move after turning, so I tried to use your code Zues_. However, just like all things, it screwed up. I can get it to turn very strangley, but, when I implemented the forward and backward movement code, it would stand still when I hit forward, when I hit back, it would rotate the screen, then when I hit forward again, it just screws up some more, and makes me frustrated. Does anyone have any other answers? Oh, and um, Zues_, you said that sin, and -cos are often used to determine this. Um, how is this? I''ve taken Algebra, but not Trigo yet. SOMEONE HELP ME! If you don''t, then I will have to calculate the additions to X and Z for all three hundred and sixty degrees of movement. Also, if I just add and subtract values from the camera''s X axis view, why does it stop when I reach 180 degrees and 360 degrees?

En taro Adun!

Doom to all who threaten the homeworld!

*Protoss Zealot - Starcraft*

###
#16
Members - Reputation: **122**

Posted 27 November 2001 - 03:21 PM

Do you know what sine, cosine, and tangent are? No?

Hmm... Look them up. To do what you want to do, the way you want to do it, you need to use SOHCAHTOA.

Do you know why?

Well, my examples involved incrementing an angle, right?

SOHCAHTOA was previously explained by me. Im serious, ask your math teacher about it, its hard to explain without visual contact. =)

Hmm... Look them up. To do what you want to do, the way you want to do it, you need to use SOHCAHTOA.

Do you know why?

Well, my examples involved incrementing an angle, right?

SOHCAHTOA was previously explained by me. Im serious, ask your math teacher about it, its hard to explain without visual contact. =)