0

I'm using OpenGL and have a stack of cylinders connected by a sphere that I want to rotate.

I'm making a sort of 'pipeline' thing and want the rotations to be smooth (so when the bottom cylinder rotates, the top one moves accordingly, but the top one could rotate without moving the bottom one

enter image description here

The only issue is that I want to build on from this, but even now with just cylinders I can't get the top one rotating without moving the whole thing.

My code is:

void draw_cylinder(float translate_x, float translate_y, float angle1) {
GLUquadricObj *qobj = gluNewQuadric();

  //first cylinder
  glPushMatrix();
  glTranslatef(translate_x, translate_y-0.5, 0.0);
  glRotatef(angle1, 0.0, 0.0, 1.0);

    glPushMatrix();
    glRotatef(90.0, -45.0, 0.0, 0.0);
    gluCylinder(qobj, 1.0, 1.0, 10.0, 20, 20);
    gluQuadricOrientation (qobj, GLU_INSIDE);
    gluDisk(qobj, 0.0, 1.0, 20, 20);
    glTranslatef(0.0, 0.0, 10.0);
    gluQuadricOrientation (qobj, GLU_OUTSIDE);
    gluDisk(qobj, 0.0, 1.0, 20, 20);
    glPopMatrix();

    glPushMatrix();  //Connecting sphere
      glTranslatef(translate_x, translate_y+10.5, 0.0);
      glutSolidSphere(1,20,20);
    glPopMatrix();

      //second cylinder
      glPushMatrix();
      glTranslatef(translate_x, translate_y+11.3, 0.0);
      glRotatef(angle2, 0.0, 0.0, 1.0);

        glPushMatrix();
        glRotatef(90.0, -45.0, 7.0, -7.0);
        gluCylinder(qobj, 1.0, 1.0, 5.0, 20, 20);
        gluQuadricOrientation (qobj, GLU_INSIDE);
        gluDisk(qobj, 0.0, 1.0, 20, 20);
        glTranslatef(0.0, 0.0, 5.0);
        gluQuadricOrientation (qobj, GLU_OUTSIDE);
        gluDisk(qobj, 0.0, 1.0, 20, 20);
        glPopMatrix();

  glPopMatrix();
glPopMatrix(); }

any help would be appreciated

Berke Cagkan Toptas
  • 1,034
  • 3
  • 21
  • 31
Tasifx
  • 1
  • This sounds like skeletal animation, have a research on this topic and you will probably get your answer – abcdef Mar 18 '15 at 07:15

1 Answers1

0

What you are asking is I guess rotation around different points rather than the origin. You can check this question.

In your case:

  • when you want to rotate your whole arm, use bottom center point of your lower cylinder as a reference point and apply the process(in the linked answer) to all of your cylinders.

  • when you you want to rotate your upper arm, use center of connection sphere as reference point and apply the process(int the linked answer) to your upper cylinder.

Community
  • 1
  • 1
Berke Cagkan Toptas
  • 1,034
  • 3
  • 21
  • 31