# rotate sphere horizontally around another sphere

I currently have an earth and a moon. What I'm trying to achieve is to have the moon physically rotate around the earth horizontally along the equator along a circular path.

    moonAngle = (moonAngle + 0.5f) % 360f;
xPath = (float) Math.sin(Math.toRadians(moonAngle)) * distance;
yPath = (float) Math.cos(Math.toRadians(moonAngle)) * distance;

gl.glTranslatef(xPath, yPath, -30f);


The above works fine, except the moon is rotating around the earth vertically around the Prime Meridian like a wall clock. How do I adjust the angle of rotation? I've tried modifying the glTranslatef, but with no success.

• What do you mean, exactly, with horizontal and vertical rotation? The first thing that comes to my mind with "horizontal rotation" is the rotationing at the start of this video: youtube.com/watch?v=LoQYw49saqc Jan 13 '13 at 23:07
• Aren’t you just confusing Y and Z in the glTranslatef call? Jan 13 '13 at 23:42
• @SamHocevar The problem is that I'm trying to maintain the z-position of -30f at the same time why moving along the X and Z paths which is also the same position as the zPosition as the earth. This way if I move the Earth along the zAxis, the moon will update accordingly as well. Jan 13 '13 at 23:56
• @ChocoMan Then you might want to try gl.glTranslatef(xPath, 0f, yPath-30f);. But most of all you want to remove hardcoded values and use named variables for the Earth position. Jan 14 '13 at 0:13
• Okay, I made it into an answer, but I think the question is a bit too localised for the site. Jan 14 '13 at 1:09

The Y offset you are computing should actually be applied to the Z axis:
gl.glTranslatef(xPath, 0f, yPath - 30f);