Moving in XY and Z

Started by
0 comments, last by Ravuya 15 years, 9 months ago
Hi, I have a 3D world and want to move around in it. If the user presses the left or right arrow keys they rotate left or right by 4 degrees. If they press up they start moving forward. If they press it again their speed increases (and opposite for back arrow). I wrote the following in C++ and OpenGL. The code works perfectly as long as you never rotate while moving. If you stop and rotate and then start moving, the camera "moves" correctly down the line of sight. My knowledge of linear algebra is minimal and I am sure that is where my problem is. Also, when the program first loads the user is looking in the +Y direction. Here is my function that draws the scene every frame:

void display(void)
{             
     //Calculate current altitude  
     altitude = (altitude + -1*oz);           
               
     glutSetWindow(window3D);
     glTranslatef(ox,oy,oz); //Translate to a new position (if updated)   

     //Keep track of current acft position
     posx = posx + ox;
     posy = posy + oy;
     posz = posz + oz;
       
     glClearColor(0, 0, 1, 0.0);  //Set Sky color
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);             

     //Draw ground and orientate correctly
     glPushAttrib(GL_ALL_ATTRIB_BITS);                                                                      
     glPushMatrix();     
        glRotatef(90,1,0,0);   
        glScalef(worldx+10,1,worldy+10);
        glBegin(GL_QUADS);               
            glColor3f(wc1, wc2, wc3);        //Set color of ground
            glVertex3f( 0.5f,-0.5f, 0.5f);	
            glVertex3f(-0.5f,-0.5f, 0.5f);	
            glVertex3f(-0.5f,-0.5f,-0.5f);	
            glVertex3f( 0.5f,-0.5f,-0.5f);			
        glEnd();
     glPopAttrib();                      
     glPopMatrix();                     
     
     drawObjects();       //Call function to draw world
     glutSwapBuffers();       
}
Here is my code when the user presses a key:

//Right = posive
void rotateMe(int direction)
{
     glutSetWindow(window3D);     
     arrowAngle = (arrowAngle + (direction*5));
     glTranslatef(-posx,-posy,-posz); 
     glRotatef(direction*5,0,0,1);
     glTranslatef(posx,posy,posz);  
}//function

//Function that moves user
//Direction specifies forward or backwards
//Back    = negative
//Forward = posive
void moveMe(int direction)
{
     glutSetWindow(window3D);     
     rad = arrowAngle * (3.14/180);      //Convert to radians
     ox = (ox + direction*sin(rad));
     oy = (oy + direction*cos(rad));          
}//function

//Function to process special keys    
void specialKeyboard(int key, int kx, int ky)
{
 /*  LEGEND
 
     Page Up       -  Increase altitude
     Page Down     -  Decrease altitude
     Left Arrow    - Turn Left
     Right Arrow   - Turn Right
     Forward Arrow - Move forward
     Back Arrow    - Move backward
 */      
     switch (key)
     {
            case GLUT_KEY_PAGE_DOWN :
                 //check to ensure rate of climb is not too steep
                 if ((oz < maxclimb))  
                 {
                    counter--;                 
                    oz = oz + 0.3;                                                     
                 }
                 break;

            case GLUT_KEY_PAGE_UP :                 
                 //check to ensure rate of climb is not too steep
                 if ((oz > -1*maxclimb))                   
                 {
                    counter++;
                    oz = oz - 0.3;
                 }
                 break;

            case GLUT_KEY_LEFT :                    
                 rotateMe(-1);                                                                                                                   
                 break;

            case GLUT_KEY_RIGHT : 
                 rotateMe(+1);   
                 break;
 
            case GLUT_KEY_UP  :                                      
                 moveMe(-1);
                 break;

            case GLUT_KEY_DOWN :  
                 moveMe(+1);
                 break;                          
     } //Switch
  if (counter == 0)
     oz = 0;
} //Function
Advertisement
Don't double and cross post.

This topic is closed to new replies.

Advertisement