Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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. enter image description here

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?

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

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.)

share|improve this answer
    
why do we need push matrix again in the second phase ? –  user1737197 Sep 11 '13 at 4:25
1  
@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. –  Nathan Reed Sep 11 '13 at 5:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.