# Thread: Camera placement and movement

1. ## Camera placement and movement

What would be the easiest way to create a camera at a certain location and be able to move it around with the keyboard? To get an idea of what I'm trying to do you can imagine a canyon and I want a camera to be the viewer's eyes when your at the base bottom. Pressing forward and back keys will move you forward and left/right will turn rotate the camera.

2. ## Re: Camera placement and movement

Probably:

static int xcam, ycam, zcam = 0.0;

glRotated ( ( GLdouble ) xcam, 1.0, 0.0, 0.0 ); // For Rotating Camera on x-Axis
glRotated ( ( GLdouble ) ycam, 0.0, 1.0, 0.0 ); // For Rotating Camera on y-Axis
glRotated ( ( GLdouble ) zcam, 0.0, 0.0, 1.0 ); // For Rotating Camera on z-Axis

then on keyboard function:

Left-Key:
ycam = ( ycam - 1 ) % 360;

Right-Key:
ycam = ( ycam + 1 ) % 360;

Up-Key:
xcam = ( xcam - 1 ) % 360;

Down-Key:
xcam = ( xcam + 1 ) % 360;

I hope that helps.

3. ## Re: Camera placement and movement

whats problematic to me is, I'm using gluLookAt( ) because I can't get my camera to view without it. This is my current setup:

void display ( void )
{
float x1, y1, z1, x2, y2, z2, x3, y3, z3,
dx1, dy1, dz1, dx2, dy2, dz2;
/* origin of camera */
x1 = 5.0f; // 5.0; -5.0; -5.0;
y1 = 5.0f; // 3.5; 3.5; 3.5;
z1 = 4.0f; // -4.0; -4.0; 4.0;

/* point looking at */
x2 = 0.0f;
y2 = 0.2f;
z2 = 0.0f;

/* camera rotation */
x3 = 0.0f;
y3 = 1.0f;
z3 = 0.0f;

dx1 = -1.5;
dy1 = 0.5;
dz1 = 3.8;

dx2 = dx1;
dy2 = dy1;
dz2 = dz1 - 0.1;

if( overview == ON ) {
glMatrixMode ( GL_PROJECTION );
glFrustum( -0.33, 0.33, -0.33, 0.33, 0.5, 20.0 );
//gluPerspective( 45.0f, (800/600), 0.1f, 100.0f );
gluLookAt( x1, y1, z1, x2, y2, z2, x3, y3, z3);
glRotatef( yAngle, 0.0, 1.0, 0.0 );
glMatrixMode ( GL_MODELVIEW );
}
else {
glMatrixMode ( GL_PROJECTION );
//glOrtho( -0.33, 0.33, -0.33, 0.33, 0.1, 10.0 );
gluPerspective( 45.0f, (800/600), 0.1f, 10.0f );
gluLookAt( dx1, dy1, dz1, dx2, dy2, dz2, x3, y3, z3 );
glRotatef( newY, 0.0, 1.0, 0.0 );
glTranslatef( 0.0, 0.0, newZ);

glMatrixMode ( GL_MODELVIEW );
}
.......
}

void SpecialKeys(int key, int x, int y)
{
switch (key) {
case GLUT_KEY_LEFT:
if( overview == ON ) {
yAngle -= 1.0;
glutPostRedisplay( );
}
else {
newY -= 1;
glutPostRedisplay( );
}
break;
case GLUT_KEY_RIGHT:
if( overview == ON ) {
yAngle += 1.0;
glutPostRedisplay();
}
else {
newY += 1;
glutPostRedisplay( );
}
break;
case GLUT_KEY_UP:
if( overview == OFF ) {
newZ += 0.05;
glutPostRedisplay( );
}
break;
case GLUT_KEY_DOWN:
if( overview == OFF ) {
newZ -= 0.05;
glutPostRedisplay( );
}
break;
default:
break;
}
}

4. ## Re: Camera placement and movement

You can goto Steve's website at: http://www.gldomain.com/programs/FirstPersonShooter.htm

It comes with the source code.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•