PDA

View Full Version : rotating objects in a double orbit



debonair
09-10-2013, 07:12 PM
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.

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



void display()
{
for (int k = 0; k < 10; k++)
{

distance_x = 0.4 * cos(angle1*3.14 / 180);
distance_y = 0.4 * sin(angle1*3.14 / 180);
angle1 += 60;
glPushMatrix();
glRotatef(i, 0.0f, 0.0f, 1.0f);
glTranslatef(distance_x, distance_y, 0.0f);
glRotatef(-i, 0.0f, 0.0f, 1.0f);

DrawTriangle();
glPopMatrix();
}
}


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

GClements
09-10-2013, 07:45 PM
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.


drawSun();
glPushMatrix();
glRotatef(planet_angle, 0, 0, 1);
glTranslatef(planet_distance, 0, 0);
drawPlanet();
glPushMatrix();
glRotatef(moon1_angle, 0, 0, 1);
glTranslatef(moon1_distance, 0, 0);
drawMoon1();
glPopMatrix();
glPushMatrix();
glRotatef(moon2_angle, 0, 0, 1);
glTranslatef(moon2_distance, 0, 0);
drawMoon2();
glPopMatrix();
glPopMatrix();

Note that the angles will accumulate, i.e. the moon angles are relative to the line from the sun to the planet. If necessary, you can subtract the previous angle (or add a counter-rotation) to maintain a consistent base direction.

debonair
09-11-2013, 12:08 PM
Thanks a lot..