# OpenGL camera movement

This topic is 3843 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hey, i'm new to opengl and i need some help. lately i've been experimenting with camera movement and i was able to figure out how to move and rotate it using the arrow keys. however, when i want the camera to move "forward" or "backward", it moves on a slight angle as opposed to straight forward or backward. is there a way i can fix this? i tried changing where the camera started, which direction it's facing, and altering the formulas for the arrow keys. any help would be greatly appreciated. here's my code so far. ignore anything irrelevant! lol
#include <windows.h>
#include <gl.h>
#include <glut.h>
#include <math.h>

//angle of rotation
float xpos = 0, ypos = 0, zpos = 0, xrot = 0, yrot = 90, angle=0.0;

void cube (void) {
glColor3f(1.0f, 0.0f, 0.0f);
glutSolidCube(2);
}

void walls (void){
glColor3f(0.75f, 0.75f, 1.0f);
glBegin(GL_POLYGON);
glVertex3f(2.0,0.0,2.0);
glVertex3f(4.0,0.0,2.0);
glVertex3f(4.0,2.0,2.0);
glVertex3f(2.0,2.0,2.0);
glEnd();
glBegin(GL_POLYGON);
glVertex3f(2.0,0.0,2.0);
glVertex3f(2.0,0.0,4.0);
glVertex3f(2.0,2.0,4.0);
glVertex3f(2.0,2.0,2.0);
glEnd();
glFlush();
}

void init (void) {
glEnable (GL_DEPTH_TEST); //enable the depth testing
//glEnable (GL_LIGHTING); //enable the lighting
//glEnable (GL_LIGHT0); //enable LIGHT0, our Diffuse Light
}

void camera (void) {
glRotatef(xrot,1.0,0.0,0.0);
glRotatef(yrot,0.0,1.0,0.0);
glTranslated(-xpos,-ypos,-zpos);
}

void display (void) {
glClearColor (0.0,0.0,0.0,1.0); //clear the screen to black
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //clear the color buffer and the depth buffer
gluLookAt (1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0); //camera position, x,y,z, looking at x,y,z, Up Positions of the camera
camera();

glColor3f(0.75f, 1.00f, 0.75f);
glBegin(GL_POLYGON);
glVertex3f(-25.0,0.0,-25.0);
glVertex3f(-25.0,0.0,25.0);
glVertex3f(25.0,0.0,25.0);
glVertex3f(25.0,0.0,-25.0);
glEnd();
glFlush();

//cube(); //call the cube drawing function
walls();
glutSwapBuffers(); //swap the buffers
angle++; //increase the angle
}

void reshape (int w, int h) {
glViewport (0, 0, (GLsizei)w, (GLsizei)h); //set the viewport to the current window specifications
glMatrixMode (GL_PROJECTION); //set the matrix to projection
gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 100.0); //set the perspective (angle of sight, width, height, , depth)
glMatrixMode (GL_MODELVIEW); //set the matrix back to model
}

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

if (key == 27)
{
glutLeaveGameMode(); //set the resolution how it was
exit(0); //quit the program
}
}

void processSpecialKeys(int key, int x, int y) {

switch(key) {
case GLUT_KEY_RIGHT:
yrot += 1;
if (yrot > 360) yrot -= 360;
break;
case GLUT_KEY_LEFT:
yrot -= 1;
if (yrot < -360) yrot += 360;
break;
case GLUT_KEY_PAGE_UP:
xrot += 1;
if (xrot >360) xrot -= 360;
break;
case GLUT_KEY_PAGE_DOWN:
xrot -= 1;
if (xrot < -360) xrot += 360;
break;
case GLUT_KEY_UP:
yrotrad = (yrot / 180 * 3.141592654f);
xrotrad = (xrot / 180 * 3.141592654f);
break;
case GLUT_KEY_DOWN:
yrotrad = (yrot / 180 * 3.141592654f);
xrotrad = (xrot / 180 * 3.141592654f);
break;
}
}

int main (int argc, char **argv) {
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_DEPTH); //set the display to Double buffer, with depth
glutGameModeString( "990x768:32@75" ); //the settings for fullscreen mode
glutEnterGameMode(); //set glut to fullscreen using the settings in the line above
init (); //call the init function
glutDisplayFunc (display); //use the display function to draw everything
glutIdleFunc (display); //update any variables in display, display can be changed to anyhing, as long as you move the variables to be updated, in this case, angle++;
glutReshapeFunc (reshape); //reshape the window accordingly

glutKeyboardFunc(processNormalKeys);
glutSpecialFunc(processSpecialKeys);

glutMainLoop (); //call the main loop
return 0;
}
[Edited by - padlock87 on February 7, 2008 9:21:23 PM]

##### Share on other sites
Rotated movement is not as simple as a triangle (dist*sin(angle) or cos())

You will need 3D rotation equations:
assuming you have an axis of
|y
|__ x
z/

for rotation about the Y axis (left-right):
x' = distance*cos(yrotation)-distance*sin(yrotation)
z' = distance*cos(yrotation)+distance*sin(yrotation)

for rotation about the X axis (up-down):
y' = distance*cos(xrotation)-distance*sin(xrotation)
z' = distance*cos(xrotation)+distance*sin(xrotation)

for rotation about the Z axis (roll):
z' = distance*cos(zrotation)-distance*sin(zrotation)
x' = distance*cos(zrotation)+distance*sin(zrotation)

you will need to multiply the rotations together to get the full rotation

Google "3D Rotation" or "Matrix Rotation" and you'll get so much information you won't know what to do...

Quaternion is another method which is extremely powerful (a littler harder to understand tho). Matrix math (you may have to build in some matrix functions for them to work) is easier to understand, a little more cumbersome. And straight math (what I mentioned above) is pretty easy to understand but getting an arbitrary axis is a bit harder. Good luck

##### Share on other sites
Hey padlock, Im also a noob in opengl. Can you explain me how you used the camera(); function wrt the glulookat(); command to transform the camera. By the way you have made a really nice code for using glulookat();

##### Share on other sites
thanks for the compliment! lol

to be honest, i have no idea how i did it! lol i kind of just searched online for a couple of hours and copied and pasted code together. it was more of a trial and error kind of thing. i'd like to say that i knew what i was doing, but i'd be lying lol.

i can't explain camera() because i have no idea what those functions do. however, i can (hopefully) explain gluLookAt. somebody please correct me if any of this is wrong!

the first three numbers designate the camera position. this is pretty straight forward. its just a simple (x,y,z) coordinate.

the second three numbers designate the coordinates where the camera is looking. for example, if your code is gluLookAt (1.0, 1.0, 1.0, 0.0, 0.0, 0.0,... the camera is positioned at (1,1,1) and is looking down at (0,0,0)

the last three numbers really confused me at first. for a while i couldn't find anything online that explained to it in a way that i understood. basically, this coordinate tells the computer which way is up. i think that its based from (0,0,0) but im not sure. so for most programs, it would probably be (0,1,0)

i hope this helps!

##### Share on other sites
ya you are right about the first two sets of vectors that is the view and the position. The third vector is an up vector. This is the y-axis vector. Mostly it is passed as (0,1,0) because it is 1 unit above the origin that is the camera just rests on the grid(if their is any)focusing at the negative z-axis. If you want to tilt your camera then you can modify your up vector.
You can find more in the OpenGL Programming Guide. Its a really good book.
Yesterday i found tutorials about the camera stuff.
->Nate robins tutorials
->Apron tutorials(implemented openGL in win32). A little complex but You will understand. Good tutorials.(google it).
I said your tutorials where good because they where simple compared to the tutorials i found on the net.

1. 1
2. 2
3. 3
4. 4
frob
15
5. 5

• 16
• 12
• 20
• 12
• 14
• ### Forum Statistics

• Total Topics
632155
• Total Posts
3004477

×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!