Jump to content
  • Advertisement
Sign in to follow this  
mbanghart

Moving in XY and Z

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
Are you processing the specialKeyboard() function every frame?. How do you detect for multiple key presses?. It looks to me from that code that your only processing one key press at a time. That might be an issue with your code but I don't have any knowledge of using GLUT. I tend to use DirectInput and read the keyboard every frame and then base my player movement on that i.e. the player could be pressing up, down, elft right and fire at the same time (extreme case I know but...).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!