Public Group

# Moving in XY and Z

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

## 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);
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);
}//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 on other sites
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...).

1. 1
2. 2
3. 3
Rutin
14
4. 4
5. 5

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633674
• Total Posts
3013275
×