3D Game Programming

(C. Jardin) #1
our planets to move up and down as well. This is what the cosine is for. When
the sine function would move the planet through the sun, the cosine pushes
it away in the other direction. Over time, this makes a perfect circle.

With that, we should have our planets moving around the sun! In real life,
the planets don’t move in perfect circles, but this is pretty cool anyway, right?

Our space simulation is still missing something: stars. To make stars, we’ll
use a particle system. Be careful while adding the particle system with the
following code (see the warning that follows).

varstars =newTHREE.Geometry();
while(stars.vertices.length < 1e4) {
varlat = Math.PI * Math.random() - Math.PI/2;
varlon = 2*Math.PI * Math.random();

stars.vertices.push(newTHREE.Vector3(
1e5 * Math.cos(lon) * Math.cos(lat),
1e5 * Math.sin(lon) * Math.cos(lat),
1e5 * Math.sin(lat)
));
}
varstar_stuff =newTHREE.ParticleBasicMaterial({size: 500});
varstar_system =newTHREE.ParticleSystem(stars, star_stuff);
scene.add(star_system);

Be Careful Adding while Statements in ICE
A while statement will continue to run until something stops it. If
nothing stops it, then the browser will lock up. When that happens
in ICE, your only option is to switch to edit-only mode (see Recov-
ering When ICE Is Broken).

To prevent freezes, you can comment out the while statement until
you have typed the entire code block. That is, you can put the
double slashes for a comment before the while and type everything
else in the code block. Then go back and remove the double slashes
to see the results of the while onscreen.

We’re not going to worry much about the details of a particle system. In
essence, particle systems are a way of adding a whole lot of things to a simu-
lation in a way that doesn’t make the computer work very hard. In this case,
we add a whole lot of stars to the scene.

Chapter 13. Project: Build Your Own Solar System • 120


Prepared exclusively for Michael Powell report erratum • discuss

Free download pdf