Animation with glut

I am trying to do animation using glut. I have 2 squares overlapping to each other and i want other square to rotate around this central object without rotating themselves. I want to rotate in orbit manner. I am calculating circular path using parametric equation and translating that one of the square to that distance. i am calling this function in a loop

Here is my code which is not working as per expected. Let me know where it is going wrong.
I have added expected image as well



void DrawShapes()
{
    static float angle = 0;

    glClear(GL_COLOR_BUFFER_BIT);

    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    glColor3f(0.0f, 0.3f, 0.2f); 


    glBegin(GL_POLYGON);
    glVertex2f(-0.2, -0.2);
    glVertex2f(0.2, -0.2);
    glVertex2f(0.2, 0.2);
    glVertex2f(-0.2, 0.2);
    glEnd();

    static int i = 0;
distance_x = 0.3 * cos(i*3.14 / 180);
 distance_y = 0.3*   sin(i*3.14 / 180);
 i++;
 if (i > 360)
     i = 0;

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glPushMatrix();
    glTranslatef(distance_x, distance_x, 0.0);  
    glBegin(GL_POLYGON);
    glVertex2f(-0.2, -0.2);
    glVertex2f(0.2, -0.2);
    glVertex2f(0.2, 0.2);
    glVertex2f(-0.2, 0.2);
    glEnd();

    glPopMatrix();
    glutSwapBuffers();

}

[ATTACH=CONFIG]512[/ATTACH]

Your display function should render one frame then return. If you want animation, register a timer function which calls glutPostRedisplay().

problem i am facing is not about the frames, i am able to get the animation, only thing is that the output is not as per expected.

Guys help me out please…

Here is my code which is not working as per expected.

In what way is it “not working as per expected”?

the motion of other square is not in a orbit instead it moves along a diagonal of middle one.

So it moves in a diagonal. Almost as if you passed the same value for the X and Y components of the position…

:frowning: I dont know how did i write same parameters for both axes … Thanks a lot.

// glTranslatef (distance_x, distance_x, 0.0);  
   glTranslatef (distance_x, distance_y, 0.0);

how can i achieve the same effect with glRotatef() without using above manipulation?

Now i am able to rotate it with glrotatef. But if i have the wall of objects around the central object which i want to rotate around fixed axis but it seems that the axis is not constant. here is my code


static int i = 0;

static int angle = 0;
float distance_x, distance_y;
glClear(GL_COLOR_BUFFER_BIT);
glViewport(0, 0, 7 * width / 8, 7 * height / 8);

glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glTranslatef(-0.25f, 0.1f, 0.0f);
DrawHouse();
glPopMatrix();
distance_x = 0.2 * cos(i*3.14 / 180);
distance_y = 0.2 * sin(i*3.14 / 180);

glPushMatrix();
glRotatef(i, 0.0f, 0.0f, 1.0f);     
glTranslatef(0.5f, 0.0f, 0.0f); 
glRotatef(-i, 0.0f, 0.0f, 1.0f);
//DrawTriangle();
int angle1 =0 ;
for (int k = 0; k < 20; k++)
{
distance_x = 0.5 * cos(angle1*3.14 / 180);
distance_y = 0.5 * sin(angle1*3.14 / 180);
angle1 += 20;
glPushMatrix(); 
glTranslatef(distance_x, distance_y, 0.0f);
DrawTriangle();
glPopMatrix();
}

distance_x = 0.3 * cos(i*3.14 / 180);
distance_y = 0.3 * sin(i*3.14 / 180);

glRotatef(i, 0.0f, 0.0f, 1.0f);
glTranslatef(0.3f, 0.0f, 0.0f);
glRotatef(-i, 0.0f, 0.0f, 1.0f);

i++;

i %= 360;
angle++;

glPopMatrix();

glutSwapBuffers();
glutTimerFunc(10, animation, 0);

The video is available at http://www.youtube.com/watch?v=d3-sAnrG8pE&feature=em-upload_owner

please correct me where i am going wrong