3D Game Programming

(C. Jardin) #1

Figure 9—The River Trench


one for the lid, which will be invisible. We already have empty addWater() and
addLid() functions that just need to be defined. We add the following lines to
draw the water:

functionaddWater(ground, size) {
varwater =newPhysijs.ConvexMesh(
newTHREE.CubeGeometry(1.4*size, 1.4*size, 10),
Physijs.createMaterial(
newTHREE.MeshBasicMaterial({color: 0x0000bb}),
0, // No friction (slippery as ice)
0.01// Not very bouncy at all
),
0 // Never move
);
water.position.z = -20;
water.receiveShadow = true;
ground.add(water);
}

We’re familiar with everything in there, though a couple of things are worth
mentioning. We use a cube rather than a plane to make it hard to accidentally
fall through the water. Physics engines are cool, but they are not perfect.
Giving the water a little thickness makes it less likely that kind of mistake
will happen.

The water is 1.4 times bigger than the ground so that the raft won’t fall off
the world when it reaches the finish line. The last thing to note is that we’re
changing the Z position instead of the usual Y to move up and down. We do
this because the ground was rotated when we added it to the scene.

Chapter 20. Project: River Rafting • 194


Prepared exclusively for Michael Powell report erratum • discuss

Free download pdf