3D Game Programming

(C. Jardin) #1
This will produce a flat, green plane. (As always, you don’t need to type in the
comments that are in the code, though later they may help you figure out
what’s going on.)

At first glance, this looks a lot like the meshes that we’ve been building since
Chapter 1, Project: Creating Simple Shapes, on page 1. After looking a little
closer, however, you’ll notice some differences.

First off, we built a flat plane with 100 faces (squares). We want a lot of faces
so that pulling down a corner of one of the faces doesn’t pull down the entire
plane—just a tiny part of it. Most of the other code in the function is stuff
we’ve seen already—creating a physical material with Physijs.createMaterial() and
telling the mesh that it can both cast and receive shadows.

New in this code block is something called a height field mesh. This lets the
physics engine work with shapes that are warped (we’ll get to that in a second).
Also different in here is that we have to rotate this mesh in three directions.
Actually, the two 90° (Math.PI/2) turns are not very surprising—they make the
ground lay flat instead of standing up. The slight turn by 0.2 is a clever trick
to make it seem like the river is pushing the raft. Just like a ball will roll down
a hill, so will our raft. The game players don’t need to know that it’s a hill
making our raft move—they can believe that it’s the river.

Of course, we still don’t have a river, let alone a raft to float down it. Let’s fix
that now. We need to add two lines to makeGround() to dig the river. The first
line calls a function to do the actual digging, and the other adds to the ground
mesh the list of points in the river. We now add the following line just below
the line that makes the ground shape:

varriver_points = digRiver(shape, faces + 1);

report erratum • discuss

Warping Shapes to Make Unique Things • 191


Prepared exclusively for Michael Powell

Free download pdf