Short answer: by smart compositing rotation and translation. In the image below you can see the process (radius r is distance of your planet from star).

- If you rotate the moon by rot_m degrees(updated in main loop), it will circle the origin point.
- If you first rotate and then translate by radius r it will circle
in right distance but wont follow your target planet.
- If you both rotate and translate and then rotate again by same angle as your planet, it will circle both the planet and the origin point appearing it circles your planet. I also strongly advise you not-to use relative transformations unless you really need it (speed vector) so you can keep track where those objects are and update rotation angle in Update() function (probably setTo... in your library).
Then, you can move whole system in your world as you see fit. In pseudo code:
struct System
{
Moon moon; //or array if you got more
Planet planet
void Translate(vec3 tranlastion);
} Earth;
void System::Translate(vec3 tranlastion)
{ /*translate all componenets of the system if you want to move it!*/}
in draw() method:
Earth.moon.Rotate(Earth.moon.rotation, 0.f, 1.f, 0.f) //or any other axis
Earth.moon.Translate(Earth.planet.distance_from_sun, 0.f, 0.f);
Earth.moon.Rotate(Earth.planet.rotation,0.f, 1.f, 0.f);
//you should understand how to transform planet and system correctly by now
int Update() method:
Earth.moon.rotation += deltaRot_m;
Earth.moon.rotation %= 360;
Earth.planet.rotation += deltaRot_p;
Earth.planet.rotation %= 360;
.transform.translate(vectorToPlanet).rotate(axis, howeverManyDegrees)
might work. ThesetToTranslation
andsetToRotation
-methods seem to reset any other components of the matrix. \$\endgroup\$