views:

53

answers:

3

I have a car model with different parts of the car described in vertex groups. I want to rotate the tires. The origin of the model is in the middle of the car. Want I'm thinking I need to do is translate the tires to the origin, rotate the tires, then translate them back to their original position.

My problems if the translating to the origin and back part. I'm thinking I can use the identity matrix to move it to the origin, but how do I move it back.

I think I need to find the center of my tire's vertex group and save this some how.

Can anyone lead me in the right direction of how I should be thinking about this?

+3  A: 

You know the current position of the tyre so you use a translation matrix built from the identity matrix and negated position to translate it to the origin:

1 0 0 -x
0 1 0 -y
0 0 1 -z
0 0 0  1

Then rotate and then apply the forward transformation to put it back:

1 0 0 x
0 1 0 y
0 0 1 z
0 0 0 1
ChrisF
This is the original poster. Thanks for the response. If I'm given a bunch of vertices that make up the tire, how do I find the center point of these vertices? I'm guessing that's what the x y z values will be in these translation matrices. Do I need to define a vector basis for my tire?
TheGambler
@TheGambler: Look up axis aligned bounding box (AABB). Find the AABB of your tire vertices. The center of that box will usually usually work well as the center of your tire. For more oddly shaped objects, you might instead want the center of mass, which is more complicated to calculate. In those cases it would usually be the artist's job to center the model in the appropriate place, or at least add some exportable marker to mark that position.
Alan
A: 

Here is the code:

glTranslatef(x_translate_factor,y_translate_factor,z_translate_factor);
glRotatef(global_car_rotate,0,1,0) ;
glRotatef(global_tire_rotate,0,1,0);
glRotatef(tire_speed,1,0,0);

Where tire_speed is tire spin speed, global_tire_rotate is the rotation of the tire relative to the car. Global_car_rotate is the rotation of the car (and objects with it such as wheel) relative to the z axis. *_translate_factor is the car's *-position inside your scence. It worked fine for me, hope it does for you ;).

Green Code
A: 

You might want to check out glPushMatrix() and glPopMatrix(). glPushMatrix() will save any translate/scale/rotate operations that you previously used (i.e. your old model view matrix). When you push your old model view matrix, you'll then be working at the origin. Then you can perform any new translate/rotate/scale operations to whatever you're currently drawing. Finally, you call glPopMatrix(). This will reload your old model view matrix. This is the cleanest and easiest way to think about these operations.

glPushMatrix();
// at this point you're working at the origin
// translate the tire here
// rotate the tire here
glPopMatrix();

In general, this is a good way to position something that you're drawing.

Ryan