3D Game Programming

(C. Jardin) #1
moon_orbit.add(earth_cam);
earth_cam.rotation.set(Math.PI/2, 0, 0);

Now comes the really cool part of frame of reference. Back inside the animate()
function, we’ll animate the rotation of the moon_orbit. Add the second line and
change the m_angle—the amount by which the moon’s orbit is changed—to be
as follows:

varm_angle = time * 4;
moon_orbit.rotation.set(0, 0, m_angle);

With that, the moon should be traveling around Earth!


If you look closely, you’ll notice that we’re not moving the moon. Instead,
we’re rotating the moon’s orbit—that is, we’re rotating the moon’s frame of
reference. If you hide the code and press the E key to switch to Earth-cam,
you’ll see that, since we added the camera to the moon’s frame of reference,
it spins to point at the moon at all times.

You can think of this frame of reference as a plate to which we glue the
camera and the moon:

When we spin the plate, the moon and camera spin along with it:


Chapter 14. Project: Phases of the Moon • 128


Prepared exclusively for Michael Powell report erratum • discuss

Free download pdf