3D Game Programming

(C. Jardin) #1
varavatar =newPhysijs.ConvexMesh(

newTHREE.CylinderGeometry(30, 30, 5, 16),
Physijs.createMaterial(

newTHREE.MeshBasicMaterial({color:0xbb0000}), 0.2, 0.5
)
);

Since this is a physics simulation, we make the material slippery with the 0.2
number (1.0 would be very hard to move) and somewhat bouncy with the 0.5
number (1.0 would be very bouncy).

Next we add the avatar to the scene:


avatar.rotation.set(Math.PI/2, 0, 0);
avatar.position.set(0.5 * width/-2, -height/2 + 25 + 30, 0);
scene.add(avatar);

avatar.setAngularFactor(newTHREE.Vector3( 0, 0, 0 ));// don't rotate
avatar.setLinearFactor(newTHREE.Vector3( 1, 1, 0 ));// only move on X and Y axis

We rotate the avatar 90 degrees (Math.PI/2) so that it’s standing up rather than
lying flat. We position it a bit to the left and just above the bottom boundary
( 25 is half the boundary’s width and 30 is the size of the avatar). As in Project:
The Purple Fruit Monster Game, we set the angular factor so that the avatar
won’t fall flat, and we set the linear factor so that it moves only up and down
(not in and out of the screen).

Next let’s decide what to do if the avatar collides with something. In most
cases we won’t care. It doesn’t matter if the avatar bumps into a wall or ramp.
It only matters if the object is a goal:

avatar.addEventListener('collision',function(object) {
if(object.isGoal) gameOver();
});

We’ll worry about the isGoal property when we add the goal a little later.


Next we need to handle interaction with the keyboard:


document.addEventListener("keydown",function(event) {
varcode = event.keyCode;
if(code == 37) move(-50);// left arrow
if(code == 39) move(50); // right arrow
});

There’s nothing new there. We still need to tell the avatar to increase its speed
by 50 whenever the left-right arrow keys are pressed:

report erratum • discuss

Setting the Game’s Boundaries • 169


Prepared exclusively for Michael Powell

Free download pdf