# a moving truck

Hi, I would like to simulate a simple moving truck. And here comes a problem. I have no idea how to rotate the truck individually (not along the original coordinate system) after it goes for a certain distance…or say, not a real car rotaion and moving …

Any help is appreciated…

Here is my code:

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

``````glPushMatrix();
glRotated((GLdouble) truck_rot_angle, 0.0, 1.0, 0.0);
glTranslatef(distance, 0.0, -150.0);

glPushMatrix();
glCallList(TRUCK);
glPopMatrix();

//2 front wheels
glPushMatrix();
glTranslatef(135.0, -104.0, -60.0);
glRotatef((GLdouble) wheel_rot_angle, 0.0, 0.0, 1.0);
glCallList(WHEEL);
glPopMatrix();

glPushMatrix();
glTranslated(135.0, -104.0, 60.0);
glRotatef((GLdouble) wheel_rot_angle, 0.0, 0.0, 1.0);
glCallList(WHEEL);
glPopMatrix();

//2 rear wheels
glPushMatrix();
glTranslated(-135.0, -104.0, -60.0);
glRotatef((GLdouble) wheel_rot_angle, 0.0, 0.0, 1.0);
glCallList(WHEEL);
glPopMatrix();

glPushMatrix();
glTranslated(-135.0, -104.0, 60.0);
glRotatef((GLdouble) wheel_rot_angle, 0.0, 0.0, 1.0);
glCallList(WHEEL);
glPopMatrix();

glPopMatrix();

glFlush();
glutSwapBuffers();
``````

}

void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case ‘f’:
wheel_rot_angle = (wheel_rot_angle + 5) % 360;
distance += 8;
glutPostRedisplay();
break;

``````case 'b':
wheel_rot_angle = (wheel_rot_angle - 5) % 360;
distance -= 8;
glutPostRedisplay();
break;

case 'r':
truck_rot_angle = (truck_rot_angle + 5) % 360;
glutPostRedisplay();
break;

case 'e':
truck_rot_angle = (truck_rot_angle - 5) % 360;
glutPostRedisplay();
break;

case 'v':
view -= 10;
glutPostRedisplay();
break;
case 'c':
view += 10;
glutPostRedisplay();
break;
default:
break;
}
``````

}

Are you saying that whenever you move your truck, say forward a bit, when you try to rotate it, it doesnt rotate around its own centre?

If that is the case you need to translate it back to the origin (0,0,0) then rotate, then translate back to where it was.

heh David is right… I remember this kind of problem when I was working in BGI