PDA

View Full Version : A wheel in openGL, finding the proper coordinates for rotation after translation



g0rush
12-11-2016, 08:01 PM
I'm trying to make a very, very simple scene in openGL. I'm stuck at a very basic point. I'm attempting to make the wheels of a toy car rotate on its axis. The highlighted portion does not behave like it should after translation. Basically I want it to behave like a wheel but i cannot find the correct coordinates for rotation. I have the following:


void display() {

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

eyeX = .5 + radius*cos(phi)*sin(theta);
eyeY = .5 + radius*sin(phi)*sin(theta);
eyeZ = .5 + radius*cos(theta);

//assuming modelview matrix mode is set
gluLookAt(eyeX, eyeY, eyeZ, 0, 0, 0, 0.0, 1.0, 0.0);

//rest of stuff ...

glPushMatrix();

glPushMatrix();
glScalef(2, .5, 1);
glutSolidCube(.5);
glPopMatrix();

glPushMatrix();

glPushMatrix();
glRotated(t, -.4, -.2, .30);
glTranslatef(-.4, -.2, .30);
glutSolidTorus(.06, .12, 8, 8);
glPopMatrix();

glPushMatrix();
glTranslatef(.7, 0, 0);
glTranslatef(-.4, -.2, .30);
glutSolidTorus(.06, .12, 8, 8);
glPopMatrix();

glPopMatrix();

glPopMatrix();

glutSwapBuffers();

}

void idle(void){
if (t >= 360) axis = (axis + 1) % 4;
t = (t < 360) ? t + dt : dt;
glutPostRedisplay();
}

BBeck1
12-11-2016, 09:13 PM
This is called rigid animation, when you go to google it.


The wheels should be a separate mesh from the body of the car. You can parent them to the body in the modeling program.

But it's pretty straight forward. You have 3 matrices: the body matrix, the wheel matrix, and the matrix for the wheel. So, you multiply the body matrix times the wheel matrix, which will give you a matrix of an orientation that is a child of the parent body matrix. Use that result matrix for drawing the wheel. This allows the body to be a parent of the wheels so that the wheels follow the body when the body moves. And the wheels can move independently of the body to spin or turn because they have their own matrix. But only by combining the two through multiplication can you parent the wheels to the body. Use that result matrix (body*wheel) to draw the wheel.

dorbie
12-13-2016, 11:06 AM
You have two options. 1 model the wheels at the origin, then rotate them and translate them to their location on the car, or model the wheels in situ, translate them to the origin rotate them and translate them back to their position (in practice the resulting matrix is a single transform.

You want a transform stack the way you are using old school fixed function pipeline OpenGL.

So in sequence:
multmatrix(camera_inverse)
pushmatrix
multmatrix(car_world_position)
draw_car_chassis
for_each_wheel
{
pushmatrix
multmatrix(wheel_position_relative_to_car_origin) // includes wheel rotation
draw_wheel
popmatrix
}
popmatrix