3D Game Programming

(C. Jardin) #1
beam3.position.set(40, 0, -40);
beam3.receiveShadow = true;
beam.add(beam3);

varbeam4 =newPhysijs.BoxMesh(
newTHREE.CubeGeometry(200, 2, 50),
material
);
beam4.position.set(40, 0, 40);
beam4.receiveShadow = true;
beam.add(beam4);

beam.rotation.set(0.1, 0, 0);
scene.add(beam);
returnbeam;
}

There’s a lot of code in there, but you know most of it. We create four beams
and combine them all together to make the game board. At the very end, we
tilt the board a bit (to get the ball rolling) and add it to the scene. Note that
we mark each of the beams as able to have shadows on them.

One thing that’s new is the 0 in the first beam:


varbeam =newPhysijs.BoxMesh(
newTHREE.CubeGeometry(50, 2, 200),
material,
0
);

The 0 tells the physics library that gravity doesn’t apply to this object (or
anything added to it). Without the zero, our game board would fall right off
the screen!

Uncomment the call to addBoard() in the code outline, and you should have the
ball hovering over the game board.

Enable Animation


Before we enable the game-board controls, we need to animate the scene. At
the very bottom of our code, move the renderer.render() line into an animate()
function as follows:

functionanimate() {
requestAnimationFrame(animate);
scene.simulate();// run physics
renderer.render(scene, camera);
}

report erratum • discuss

Outline the Game • 151


Prepared exclusively for Michael Powell

Free download pdf