rotating objects

Started by
1 comment, last by dante99 22 years, 8 months ago
is there a way to connect two rotating objects that are rotating on a different axises with a line. This line will be growing and shrinking depending on where the rotating objects are. How can i do this
Advertisement
Well, for starters, when you draw the line that will connect the two objects, you''re going to need the state of both Modelview Matrices after you rotate each object. Since we can have one to manipulate at all times, we''ll just store the first one on the Modelview Matrix stack...

  glLoadIdentity();// Rotate/translate/scale viewport for 1st objectglBegin(GL_WHATEVER);    // Drawing codeglEnd();glPushMatrix();glLoadIdentity();// Rotate/translate/scale viewport for 2nd objectglBegin(GL_WHATEVER);    // Drawing codeglEnd();  


Now as you can see, we are still being affected by the 2nd rotation. So we can go ahead and begin drawing our line from the vertex it attaches to the 2nd polygon. (The vertex should be identical to one of the vertices you specified for it when you drew it...)

  glBegin(GL_LINES);    glVertex( /* Some vertex on 2nd polygon */ );  


However, we now need a point on the first polygon, which means we''re going to need to be under the 1st rotation. So we pop the matrix back to get back under the old rotation, then draw our second line

      glPopMatrix();    glVertex( /* Some vertex on 1st polygon */);glEnd();  


That should roughly do it! Good luck to you!

~ Dragonus
I did get your E-mail...

quote:dragonus, the is question is concerning the post to the rotating objects question in opengl. What if the objects rotating are two spheres on the different axises (x and y) how would you know where to put the beginning and ending of the line. Do you mind writing an example i don''t quite understand what you posted.


I would E-mail you the answer back; however, some of the people where I work might not like that, so I''ll just post your answer.

Well, this is actually an easier case than what I expected. Here''s the general idea for it...

  glTranslate( /* Distance to move camera to draw 1st sphere */ );glRotate( /* angle */ , 1.0, 0.0, 0.0);  // X Axis rotation// However in the world you draw your sphere goes here.// I''ll use GLUT; if you''re not, just edit your code over mineglutSolidSphere(2.0, 16, 16);  // Radius, # of lat & lon polygonsglPushMatrix();  // Save the translation/rotationglLoadIdentity();glTranslate( /* Distance to move camera to draw 2nd sphere */ );glRotate( /* angle */ , 0.0, 1.0, 0.0);  // Y Axis RotationglutSolidSphere(2.0, 16, 16);  // Draw 2nd sphere// Now we''ll draw the line to connect the two.  We''re going to// join the centers of the two spheres together to make a line.// Since Depth Buffering should be enabled, it will cut off the// line automatically where it should be cut off.  ;)glBegin(GL_LINES);{    glVertex(0.0, 0.0, 0.0);  // Put a point in the center of the 2nd sphere    glPopMatrix();  // Revert to the 1st translation/rotation    glVertex(0.0, 0.0, 0.0);  // Draw 2nd point and the line}glEnd();glLoadIdentity();  


I do hope this works.

~ Dragonus

This topic is closed to new replies.

Advertisement