Position of an orbiting sphere

I’m trying to get the position of a sphere that is rotating around an idle object in my opengl application. This is how I perform the orbiting:

            glTranslatef(positions[i].getPosX(), //center of rotation (yellow ball)
                           positions[i].getPosY(),
                           positions[i].getPosZ());
            glRotatef(rotation_angle,0.0,1.0,0.0); //angle of rotation
            glTranslatef(distance[i].getPosX(), //distance from the center of rotation
                           distance[i].getPosY(),
                           distance[i].getPosZ());

Variable rotation_angle loops from 0 to 360 endlessly. In the distance vector I’m only changing the z-distance of the object, for example let’s say the idle object is in (0,0,0), the distance vector could be (0,0,200). I need the position of the object that is orbiting to perform collision detection.

In which case, forget about using any OpenGL matrix functions other than glLoadMatrix(). Just construct the matrices in the application (either use GLM or write your own code).

For a single object and a simple program, you can get the matrices out of OpenGL using glGetDoublev() and use gluUnProject(). But if you have many objects or performance matters, that’s highly inefficient.

I’m trying to get the position of a sphere that is rotating around an idle object in my opengl application.

The first translate is o.k. I’d take out the glRotate and replace the second glTranslate with something like this -


   float r = 200.0,
         x = r * cos (rotation_angle),       // This assumes rotation_angle is in units of radians
         y = 0.0,
         z = r * sin (rotation_angle);

   glTranslatef (x, y, z);