Rotate the object around with its center point in OpenGL?

I have a robot, and I’ve determined the center point at (0.5f, 0.0f, -1.0f) . I’m trying to rotate the robot around its center point, I know that I need to translate the point to the origin first, rotate, translate back to the center. My robot can rotate around its center now, but when I moving the robot, it won’t be able to rotate around its center.

I did research, I need to figure out the current forward direction vector of my bot and update that vector every time when I moving my robot. I did the calculation and got the direction vector. However, I don’t know how to implement that into OpenGL.

Here is my code:

void drawRobot()
{

     glPushMatrix();

     glTranslatef(0.5f,0.0f,-1.0f);
     glRotatef(robotAngle, 0.0, 1.0, 0.0); //default robotAngle is 0
     glTranslatef(-0.5f, -0.0f, 1.0f);

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

     glPopMatrix();
}

If you’re implementing a self-propelled entity (vehicle, person, whatever), you should probably be calculating its motion in the application and just passing the final transformation to OpenGL via glLoadMatrix or glMultMatrix.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.