# rotating objects in a double orbit

I have a object at the center. Other set of objects rotating around center in first orbit. Now i want other objects to rotate around the objects in 1st orbit.

In the above fig, set of triangles are rotating around the square and circles are rotating around the triangle. I have the code which works for 1st orbit, but i am not able to render that second orbit.

I am calling display in loop. i is static global variable. which transformations will do the second orbit?

You can do it by inserting another "push, transform, draw, pop" sequence before your pop. Something like this:

// your current code
glPushMatrix();
glRotatef(...);
glTranslatef(...);
glRotatef(...);
DrawTriangle();

// new code
glPushMatrix();
glRotatef(...);
glTranslatef(...);
glRotatef(...);
DrawCircle();
glPopMatrix();

glPopMatrix();


The idea is that since the operations for the circle are done before the triangle's transform is popped, all the triangle's transforms will apply to the circle as well. For instance, the translation to set the circle's position will be relative to the triangle's position.

BTW, your code is a bit odd in that you're applying two separate rotations to the translation vector. You're calculating the translation vector using cos and sin, so the vector will already be rotated by angle1. Then, since you're doing a glRotate before your glTranslate, the translation vector will be rotated again by i. You could simplify this by just rotating by angle1 + i from the beginning, and removing all the glRotate calls. (Since you're undoing the rotation by i before you draw, none of the rotations are applying to your actual models.)

• why do we need push matrix again in the second phase ? Commented Sep 11, 2013 at 4:25
• @Roshan If you just have one circle to draw, you could get rid of the inner push and pop. But if, say, you wanted to draw multiple circles relative to the same triangle, you would need a push/pop around each circle to restore the triangle's transform after each circle. Commented Sep 11, 2013 at 5:17