3D Game Programming

(C. Jardin) #1
functionaddRiver(scene) {
varground = makeGround(500);
addWater(ground, 500);
addLid(ground, 500);
scene.add(ground);

returnground;
}

Our code is now calling three functions that don’t exist because we haven’t
written them yet. This will generate errors in the JavaScript console and, even
more important, break our code in such a way that nothing shows up on the
ICE Code Editor screen. To prevent this, add the following skeleton functions:

functionmakeGround(size) {
}
functionaddWater(ground, size) {
}
functionaddLid(ground, size) {
}

These functions won’t do anything when they’re called, but since they’re
defined there are no more errors and our code will run again.

Each of these functions will add flat planes to our game world. The only
complicated one will be the ground since we need to pull parts of it down to
expose the river water. The lid drawn with the last function will be an invisible
barrier over the ground so that our raft won’t jump out of the river.

Let’s get started with the makeGround() function:


functionmakeGround(size) {
varfaces = 100;
varshape =newTHREE.PlaneGeometry(size, size, faces, faces);
varcover = Physijs.createMaterial(
newTHREE.MeshPhongMaterial({
emissive:newTHREE.Color(0x339933),// a little green
specular:newTHREE.Color(0x333333) // dark gray / not shiny
}),
1, // high friction (hard to move across)
0.1// not very bouncy
);
varground =newPhysijs.HeightfieldMesh(
shape, cover, 0
);
ground.rotation.set(-Math.PI/2, 0.2, Math.PI/2);
ground.receiveShadow = true;
ground.castShadow = true;
returnground;
}

Chapter 20. Project: River Rafting • 190


Prepared exclusively for Michael Powell report erratum • discuss

Free download pdf