3D Game Programming

(C. Jardin) #1

)


);


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

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

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

functionmove(x) {
varv_y = avatar.getLinearVelocity().y,
v_x = avatar.getLinearVelocity().x;

if(Math.abs(v_x + x) > 200)return;
avatar.setLinearVelocity(
newTHREE.Vector3(v_x + x, v_y, 0)
);
}

vargoal =newPhysijs.ConvexMesh(
newTHREE.TorusGeometry(100, 25, 20, 30),
Physijs.createMaterial(
newTHREE.MeshBasicMaterial({color:0x00bb00})
),
0
);
goal.isGoal = true;

functionplaceGoal() {
varx = 0,
rand = Math.random();
if(rand < 0.33) x = width / -2;
if(rand > 0.66) x = width / 2;
goal.position.set(x, height/2, 0);
scene.add(goal);
}
placeGoal();

functionRamp(x, y) {
this.mesh =newPhysijs.ConvexMesh(

report erratum • discuss

Code: Cave Puzzle • 257


Prepared exclusively for Michael Powell

Free download pdf