3D Game Programming

(C. Jardin) #1
varshape =newTHREE.TorusGeometry(100, 25, 8, 25, 3.14);
varcover =newTHREE.MeshNormalMaterial();
vardonut =newTHREE.Mesh(shape, cover);
scene.add(donut);

That should make a half-eaten donut.


1.3 Animating the Shapes


Before we finish our first programming session, let’s do something cool. Let’s
make all of our shapes spin around like crazy.

In ICE, add the following code after all of the shapes:


varclock =newTHREE.Clock();

functionanimate() {
requestAnimationFrame(animate);
vart = clock.getElapsedTime();

ball.rotation.set(t, 2*t, 0);
box.rotation.set(t, 2*t, 0);
tube.rotation.set(t, 2*t, 0);
ground.rotation.set(t, 2*t, 0);
donut.rotation.set(t, 2*t, 0);

renderer.render(scene, camera);
}

animate();

Don’t worry about what everything means in that code—we’ll look at all of
these lines in greater detail later in the book. For now, it’s enough to know
that at specific time intervals, we’re changing the shape’s rotation. After each
change, we tell the rendering program to redraw the current shapes on the
screen.

If ICE Locks Up
When doing animations and other sophisticated programming, it’s
possible to completely lock up the ICE Code Editor. This is not a
big deal. If ICE stops responding, you’ll need to undo whatever
change you made last. Instructions on how to do that are in
Recovering When ICE Is Broken.

Chapter 1. Project: Creating Simple Shapes • 14


Prepared exclusively for Michael Powell report erratum • discuss

Free download pdf