3D Game Programming

(C. Jardin) #1

13.3 Earth-Cam!


Let’s add the ability to watch the planet Mars from Earth. As you watch Mars
from Earth over several months, Mars’s position in the sky changes in a
pretty strange way. It’s so strange that ancient astronomers couldn’t explain
it. But we’ll be able to.

To watch Mars from Earth, we’ll need another camera besides the one looking
down on our solar system. So the first thing we need to do is give our above-
the-solar-system camera a better name. Let’s call it above_cam. Change camera
to above_cam at the top of your code:

// This is what sees the stuff:
varaspect_ratio = window.innerWidth / window.innerHeight;
varabove_cam =newTHREE.PerspectiveCamera(75, aspect_ratio, 1, 1e6);
above_cam.position.z = 1000;
scene.add(above_cam);

Next let’s add a new camera named earth_cam.


varearth_cam =newTHREE.PerspectiveCamera(75, aspect_ratio, 1, 1e6);
scene.add(earth_cam);

The rest of our code expects a camera named camera, so let’s give it just that.
To start, let’s make camera mean the same thing as above_cam.

varcamera = above_cam;

report erratum • discuss

Earth-Cam! • 121


Prepared exclusively for Michael Powell

Free download pdf