PDA

View Full Version : Moving two objects together



Sklar
03-31-2004, 10:08 AM
Hello,

I have created an object by connecting a cylinder and a rectangle. I'm trying to translate them both together (in 2D) but can't figure out how to translate the rectangle with respect to the cylinder so it appears to be one object throughout their movement. Right now, they start out connected, but when they start moving, they become disjoined and fly in similar directions but not connected.

Any Ideas
Sklar

Deiussum
03-31-2004, 10:15 AM
glPushMatrix();
glTranslatef();
DrawCylinder();
DrawRectangle();
glPopMatrix();

nexusone
03-31-2004, 11:26 AM
Because matrix operations effect each object drawn after them, you must use a command to store the matrix state before you call routines like glRotate/translate.

// Setup Projection
// Setup Camera

//Create object 1
glPushMatrix(); //Since we want our camara/model matrix to stay the same for each object, we save the current matrix state

glTranslate3f(.....); //Location of our object.
glRotatef(....); // Rotation of whole object.
//draw rectangle
glPushMatrix(); // Save matrix again so that out
glTranslate3f(...); //Translate rectangle to is spot relative to cylinder
glRotatef(....); // Rotate rectangle
Draw_rectangle();
glPopMatrix(); // Now we have drawn rectangle restore matrix back for next object.
//draw cylinder
glPushMatrix();
glTranslate
glRotate
Drawcylinder();
glPopMatrix(); // End cylinder

glPopMatrix(); //End object

Note matrix operations are first in first out, which means the last saved matrix will be the first matrix to be restored when pop'ed.