The result is that we had to do very little work to orbit the moon or keep
Earth-cam pointed at it. With Mars, we had to do all sorts of crazy sines and
cosines and distance calculations. We had to position Earth and Mars and
the camera with the animate() sequence. With frame of reference, we just rotate
one thing.
Laziness is a wonderful thing.
Oh, you might be wondering: why don’t we use the same trick for Earth? I’m
glad you asked.
14.4 Challenge: Create an Earth Orbit Frame of Reference
You can do this. Just follow the steps that we took for the moon:
- Create a 3D object to hold Earth, and add it to the sun.
- Add Earth to the new orbit frame of reference instead of the scene.
- Delete the animate() code that sets Earth’s position.
- Rotate Earth’s orbit.
Once you do that, you have a very complex astronomical simulation that is
built using nothing but simple frame of reference—no complicated sines or
cosines anywhere!
14.5 Pausing the Simulation
It’s pretty neat to see the moon revolve around Earth while Earth revolves
around the sun. But to really understand the moon’s phases we need to be
able to see the moon from Earth—like we would see it in the night sky. It
would also be helpful to pause everything so that we can switch back and
forth between above-cam and Earth-cam.
To pause, we need to make some changes to the animate() function. When the
simulation is paused, we still need to perform animation and render the scene,
but we shouldn’t update the position of Earth or the moon. This means we
have to move the renderer.render() call up in the animate() function—it will need
to render before we check if the simulation is paused. We also need to come
up with a different way of keeping time in the simulation. The THREE.Clock()
that we have been using cannot be paused.
So remove the clock=THREE.Clock statement above the animate() function. Replace
it with the three variables shown (time, speed, and pause):
vartime = 0,
speed = 1,
pause = false;
report erratum • discuss
Challenge: Create an Earth Orbit Frame of Reference • 129
Prepared exclusively for Michael Powell