# How do I orbit one object around another?

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 in which the motion of other square is not in a orbit instead it moves along a diagonal of middle one. Let me know where it is going wrong. I have added expected image as well

• What exactly is not working as expected? Sep 3 '13 at 7:27
• @AdrianMaire the motion of other square is not in a orbit instead it moves along a diagonal of middle one. Sep 3 '13 at 8:05

First some little tips:

if (i > 360) i=0;


Try

i %= 360;


2) glPushMatrix and glPopMatrix are useless in your code, as you apply them on the identity matrix.

4) Avoid global variables, they cause a lot of problems.

Solution:

Probably a typo:

Change

glTranslatef(distance_x, distance_x, 0.0);


To

glTranslatef(distance_x, distance_y, 0.0);

• how can i achieve the same effect with glRotatef() without using above manipulation? Sep 4 '13 at 21:05
• You may use 3 manipulation: glRotatef(i, 0.0f, 0.0f , 1.0f); glTranslatef(0.3f, 0.0f, 0.0f); glRotatef(-i, 0.0f, 0.0f, 1.0f); Sep 5 '13 at 8:08