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?