Rotate an object around y-axis on its center point?

I have a 3D robot, the center point of the robot is at (0.5f, 0.0f, -1.0f). There is no y-axis movement of the robot, but the robot will rotate around Y-axis on its center point. I’ve tried to do that using glRotate and glTranslate.

 void drawRobot()
{

     glPushMatrix();
     //xr and zr are the moving distance of the robot
     float pivotX = 0.5 + xr;
     float pivotZ = -1.0 + zr;
     glTranslatef(pivotX,0.0f,pivotZ);
     glRotatef(robotAngle, 0.0, 1.0, 0.0); //default robotAngle is 0
     glTranslatef(-pivotX, 0.0f, -pivotZ);

     drawBase();
     drawWheel();
     drawWeapon();

     glPopMatrix();
}
void specialkey(unsigned char key, int x, int y)
{
    switch (key) {
    case 's':
	    robotAngle += 1.0;
	    FrontTurningRotation += 10.0;
	    glutPostRedisplay();
	    break;
    case 'S':
            robotAngle -= 1.0;
	    BackTurningRotation += 10.0;
	    glutPostRedisplay();
	    break;
    case 't':
	    stopRotation = true;
	    glutTimerFunc(10, weaponController, 0);
	    break;
    case 'T':
	    stopRotation = false;
	    break;
    }

}
void directionkey(int key, int x, int y)
{
    switch (key) {
    case GLUT_KEY_LEFT:
	    cout << "Forward" << endl;
	    xr -= 0.05;
	    Rotationangle += 10.0;
	    glutPostRedisplay();
	    break;
    case GLUT_KEY_RIGHT:
	    cout << "Backward" << endl;
	    xr += 0.05;
	    Rotationangle -= 10.0;
	    glutPostRedisplay();
	    break;
    case GLUT_KEY_F1:
	    cout << "BattleRobot controll guide:\nMoving forward using left/right key \nEnable/Disable 
the weapon use t/T \nTuring the bot use s/S key \n" << endl;
	    break;
    }
}

After using this method, my robot can rotate around its center point, but when I rotate the robot to another direction and move it, it only moves along with the x-axis even though the front of my robot is facing another direction.

How can I solve this problem, so that wherever I move the robot always rotate with its center point?

Your glTranslatef consumes a vector. You may convince yourself that you are working with a point (pivot). You’ve set yourself up only to stretch and shrink the vector (0,0)->pivot along the x-axis. This is independant of the rotation … as you have discovered.

You could redo pivot to be dependant on the robotAngle (a forward-direction). It’s probably not difficult to do that, but it will probably turn out unintuitive in the sense that your keys will loose their pre-defined direction (left, wright, up, down).
If you use the weel as pusher that adds a forward-direction unite vector to the pivot-vector it may work as you expect. something like
forw_vec=vec3(cos(robotAngle),0,sin(robotAngle))
pivot +=forw_vec;
… hm … having the wheel to rotate seems intuitive. Then using just one key for moving forward could work too. You’ll figure out what’s right for you.

I’m guessing that what you want is that the the object’s position and rotation is the accumulation of a series of rotations and translations, with each rotation about the objects’s current position and each translation based upon the object’s current rotation. For that, you can’t modify position and rotation independently and just compose them later (move then rotate gives a different result to rotate then move).

If that’s the only transformation you need, you could do it by simply calling glRotate and glTranslate in response to key presses, so long as you never reset the transformation. However, due to the rounding error involved in floating-point calculations, the matrix will decay over time (the axes will cease being perpendicular, resulting in a shear transformation).

Normally you maintain the transformation in the application, and either regenerate it periodically (using e.g. the Gram–Schmidt process) or maintain it in a condensed form which doesn’t suffer from this issue (i.e. position and angle(s), or position and unit quaternion).

For the latter, assuming motion in a plane you need something like:

const float turn_rate = 10.0; // degrees per step
const float move_rate = 1.0; // distance units per step
float angle;
float x, y;

    switch (key) {
    case GLUT_KEY_LEFT: // turn left (CCW)
	    angle += turn_rate;
	    break;
    case GLUT_KEY_RIGHT: // turn right (CW)
	    angle -= turn_rate;
	    break;
    // rotation assumes angle is measured CCW from the Y axis
    case GLUT_KEY_UP: // move forward
            x -= move_rate * sin(angle * M_PI / 180);
            y += move_rate * cos(angle * M_PI / 180);
	    break;
    case GLUT_KEY_DOWN: // move backward
            x += move_rate * sin(angle * M_PI / 180);
            y -= move_rate * cos(angle * M_PI / 180);
	    break;
}

For rendering, you can set the transformation with (assuming that X = East, Y = up, Z = North, which is what you seem to be using):

    glLoadIdentity();
    glTranslatef(x, 0, y);
    glRotatef(angle, 0, 1, 0);

Note that using angles becomes impractical for 6-axis motion (e.g. an aircraft), but it works fine for 2D motion or for 3D where “yaw” rotation is always about the global vertical axis (rather than a local vertical axis).