3D Game Programming

(C. Jardin) #1
Physijs.scripts.ammo = 'http://gamingJS.com/ammo.js';
Physijs.scripts.worker = 'http://gamingJS.com/physijs_worker.js';

var scene = new Physijs.Scene({ fixedTimeStep: 2 / 60 });
scene.setGravity(new THREE.Vector3( 0, -100, 0 ));

// This is what sees the stuff:
var aspect_ratio = window.innerWidth / window.innerHeight;
var camera = new THREE.PerspectiveCamera(75, aspect_ratio, 1, 10000);
camera.position.z = 200;
camera.position.y = 100;
scene.add(camera);

// This will draw what the camera sees onto the screen:
var renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

// ******** START CODING ON THE NEXT LINE ********
var ground = addGround();
var avatar = addAvatar();
var scoreboard = addScoreboard();
animate();
gameStep();

function addGround() {
document.body.style.backgroundColor = '#87CEEB';
ground = new Physijs.PlaneMesh(
new THREE.PlaneGeometry(1e6, 1e6),
new THREE.MeshBasicMaterial({color: 0x7CFC00})
);
ground.rotation.x = -Math.PI/2;
scene.add(ground);
return ground;
}

function addAvatar() {
avatar = new Physijs.BoxMesh(
new THREE.CubeGeometry(40, 50, 1),
new THREE.MeshBasicMaterial({visible: false})
);
var avatar_material = new THREE.MeshBasicMaterial({
map: THREE.ImageUtils.loadTexture('/images/purple_fruit_monster.png'),
transparent: true
});
var avatar_picture = new THREE.Mesh(
new THREE.PlaneGeometry(40, 50), avatar_material
);
avatar.add(avatar_picture);

Appendix 1. Project Code • 246


Prepared exclusively for Michael Powell report erratum • discuss

Free download pdf