Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm totally new to OpenGL and I'm working on a Sandbox game called CubeRealm. My problem you see is rotation. I've got it in my head that to rotate the 'camera' I just rotate all the scene by the negative value of the camera's rotational values. However so far it seems to not be working.

Here's the code snippets:

glPushMatrix();

lighting();
/*
 (640^2)/(64^2)=amount of cubes on one 640x640 plane(409,600,4,096)
 */
//TODO: fix z axis problem for translating
glRotatef(-player.camera.rotation.x,1.0,0.0,0.0);
glRotatef(-player.camera.rotation.y,0.0,1.0,0.0);
glRotatef(-player.camera.rotation.z,0.0,0.0,1.0);
renderGrid();

glPopMatrix();

glutSwapBuffers();

//SNIPPET2...

case LOOK_UP:
    if(player.camera.rotation.y!=-90.0){ //if they are not looking up
        //make them look up
        player.camera.rotation.y-=90.0;
        //TODO: set direction the camera is facing in
    }
    break;
case ROTATE_LEFT:
    if(player.camera.rotation.x!=0.0){
        player.camera.rotation.x-=90.0;
        //TODO: set direction the camera is facing in
    }else{
        player.camera.rotation.x=360.0;
        player.camera.rotation.x-=90.0;
        //TODO: set direction the camera is facing in
    }
    break;
case LOOK_DOWN:
    if(player.camera.rotation.y!=90.0){ //if they are not looking down
        //make them look down
        player.camera.rotation.y+=90.0;
        //TODO: set direction the camera is facing in
    }
    break;
case ROTATE_RIGHT:
    if(player.camera.rotation.x!=360.0){
        player.camera.rotation.x+=90.0;
        //TODO: set direction the camera is facing in
    }else{
        player.camera.rotation.x=0;
        player.camera.rotation.x+=90.0;
        //TODO: set direction the camera is facing in
    }
    break;

When I press the right arrow key(rotate to the right) it doesn't rotate also when I rotate up(up arrow) it goes weird instead of allowing me to see the top of the skybox it still shows me my test cubes.

So guys how do I fix the rotation, what am I doing wrong?

Note the 'TODO: set direction' stuff is for my local axis system. Those TODOs are irrelevant to the question.

share|improve this question
    
@DeadIMG have done. :) –  VoidElite Jul 5 '11 at 18:07
add comment

2 Answers

up vote 0 down vote accepted

LOOK_UP and LOOK_DOWN must rotate around the x-axis, not around the y-axis.

ROTATE_RIGHT and ROTATE_LEFT must rotate around the y-axis, not around the x-axis.

Immediately rotating around 90 degrees or initializing your angles with 90 degrees will bring your system into a Gimbal Lock state, resulting in weird rotations. You might want to use smaller angles and step-sizes for starters, particulary not 90 degrees or multiples of it.

Initialize all angles with 0 and start from there:

    // init your angles at startup, i.e. main or camera constructor
    player.camera.rotation.x = 0.0;
    player.camera.rotation.y = 0.0;
    player.camera.rotation.z = 0.0;

    // change the step-size and rotation-axis like this:
    case LOOK_UP:
        player.camera.rotation.x+=5.0;
        break;
    case ROTATE_LEFT:
        player.camera.rotation.y-=5.0;
        break;
    case LOOK_DOWN:
        player.camera.rotation.x-=5.0;
        break;
    case ROTATE_RIGHT:
        player.camera.rotation.y+=5.0;
        break;

Edit: After reviewing your code, here is the list of the bugs, the most important ones are 3), that messed up all the rendering, and 4) the correction for the camera rotation code

  1. Off by one error in void setupGrid(), change "<=" to "<" in the while

    while(_i<GRID_SIZE){
    
  2. set the initial camera position to {0, 0, 5} in Initialize(), otherwise you wont see the boxes:

    player.camera.position.x=0;
    player.camera.position.y=0;
    player.camera.position.z=5;
    player.camera.rotation.x=0;
    player.camera.rotation.y=0;
    player.camera.rotation.z=0;
    
  3. in Reshape change 3rd param of gluPerspective to 1.0

    gluPerspective(45.0,(double)screenWidth/(double)screenHeight,1.0,CUBE_UNIT*100.0);
    
  4. Finally the correction of the camera rotation code in Keyboard2() like already written above

        void Keyboard2(int key,int x,int y){
            switch(key){
                case GLUT_KEY_F1:
                    if(!wireframe){
                        glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
                        wireframe=TRUE;
                    }else{
                        glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
                        wireframe=FALSE;
                    }
                    break;
                case LOOK_UP:
                    player.camera.rotation.x+=5.0;
                    break;
                case ROTATE_LEFT:
                    player.camera.rotation.y+=5.0;
                    break;
                case LOOK_DOWN:
                    player.camera.rotation.x-=5.0;
                    break;
                case ROTATE_RIGHT:
                    player.camera.rotation.y-=5.0;
                    break;
            };
            return;
        }
    
share|improve this answer
    
I'm sorry to say but that creates an even more retarded result. The shapes seem be resized. One this that stands out about your suggestion is that you use x where y should be used and vice versa, why? Would you mind if I uploaded the full code to PasteBin so you could help me with it? Thanks. ;) –  VoidElite Jul 5 '11 at 20:29
    
Sure, the more code you show the easier we find the problem. When you want to rotate left/right you must rotate around the y axis, the one that goes upwards. What makes you think you should rotate around the x axis when rotating left/right? –  Maik Semder Jul 5 '11 at 20:41
    
X is on the left and right, Y is up and down. I'm confused. :\ –  VoidElite Jul 5 '11 at 20:48
    
Here's my GitHub repo for it: github.com/MarcAlexanderReed/CubeRealm –  VoidElite Jul 5 '11 at 20:49
1  
@VoidElite it actually works with the code in my answer, the problem is the rest of the code is a mess and has a bunch of bugs. I'm gonna put my edits into my answer in a sec. My advice to you is: As a beginner in OpenGL don't start with a full blown app, start small, draw a triangle, add a camera, make it step by step. There just has to be 1 wrong parameter anywhere and the whole programm doesn't work anymore, i.e. you have a wrong parameter in gluPerspective. Gimme a sec for the edit –  Maik Semder Jul 5 '11 at 21:33
show 14 more comments

The problem is that you can't simply add and subtract values from rotation. These values need to be calculated using math formulas using trigonometry. I was designing a project a while back and needed a camera to move in 3d space. Check out this project and the camera class from it. It will do precisely what you need granted you want to move freely in 3d space with minimal code.

http://www.paulsprojects.net/opengl/q3bsp/q3bsp.html

Edit: Here is another more straightforward example with the math involved as well: http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=Quaternion_Camera_Class

Nehe's OpenGL tutorials are a fantastic reference for beginners but if you are going to go much further, grab an OpenGL book with current standards/practices.

share|improve this answer
    
That's wrong, you can simply add values to each component of Euler Angles. Euler Angles suffer from Gimbal Lock, but that is because each rotation around the axes is appliad one after another, but you can of course simply add/subtract values for each component. -1 –  Maik Semder Jul 6 '11 at 19:01
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.