# Rotating a vector around a vector in 3D space

I am trying to rotate a moon around a planet (the planet is also rotating around an axis) but I am having trouble doing so.

I tried to make the model move through space like so:

@Override
public void render() {
moon.getInstance().transform.setToTranslation(moon.center.x + 0.5f, moon.center.y + 0.5f, moon.center.z);
}


I know that this wil not give me the correct (or indeed any) rotation, but the model doesn't even move. When I use moon.getInstance().transform.setToRotation(1f, 0, 0, 10f) it does rotate (around its axis) so I assumed that I could use .setToTranslation the same way.

So, my question is actually two questions:

1. How can I move a model in 3D space
2. How can I let it move around a certain vector, following a circular path
• I don't know libgdx, but the docs suggest something like .transform.translate(vectorToPlanet).rotate(axis, howeverManyDegrees) might work. The setToTranslation and setToRotation-methods seem to reset any other components of the matrix.
– Anko
Jul 25 '14 at 20:40

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;