Thread: looking up and down (opengl)

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    305

    looking up and down (opengl)

    i am writing a camera class in opengl. ive done the x and z axis, but i cant seem to be able to get the y axis working correctly. here is what i have:
    Code:
    #define FORWARD  0
    #define BACKWARD 1
    #define LEFT     2 
    #define RIGHT    3  
    #define UP       4
    #define DOWN     5
    
    class Camera{
      public:
        GLfloat angle;      //the angle to use for turning the camera
        GLfloat x, y, z;    //camera position
        GLfloat lx, ly, lz; //line of sight
        GLfloat pitch;      //angle of the y axis
    
        Camera(){
            angle = 0.0f;
            x = 0.0f; y = 0.0f; z = 0.0f;
    	lx = 0.0f; ly = 0.0f; lz = -1.0f;
            pitch = 0.0f;
        }
    
        void Move( int direction ){
     	x = x + direction * lx * 0.1f;
    	z = z + direction * lz * 0.1f;
        }
     
        void Orient(){
    	lx = sin( angle );
    	lz = -cos( angle );
        } 
    
        void ChangePitch(){
            ly = sin( pitch ) * lz;
            lz = cos( pitch ) * lz;
        }
    
        void move( int direction ){
    	switch( direction ){
    	    case FORWARD:
                    Move( 1 );
                    break;
                case BACKWARD:
                    Move( -1 );
                    break;
                case LEFT:
                    angle -= 0.01f;
                    Orient();
                    break;
                case RIGHT:
                    angle += 0.01f;
                    Orient();
                    break;
                case UP:
                    if( pitch <= -1.0f ) break;
                    pitch -= 0.01f;
                    ChangePitch();
                    break;
                case DOWN:
    		if( pitch >= 1.0f ) break;    
                    pitch += 0.01f;
                    ChangePitch();
                    break;            
                default:
                    break;
    	}
        }
       
        void View(){
            glLoadIdentity();
    	gluLookAt( x, y, z, 
    		   x + lx, y + ly, z + lz,
    		   0.0f, 1.0f, 0.0f );
        }    
    };
    first bug: when i go all the way down or all the way up, then try to go back to the middle, the screen goes blank while i am going back.

    second bug: when i rotate around on the x axis before the y and then i rotate on the y, the camera swings in the direction that ive turned on the x.

  2. #2
    Unregistered
    Guest
    I don't know about the first problem, but the second problem is what we call "gimbal lock", look it up. You have to convert everything to Quarterions (spelling) and then turn 'em back. So look up gimbal lock on Google, and you'll find a solution (although its rather mathy).

Popular pages Recent additions subscribe to a feed