3D Game Programming

(C. Jardin) #1

A1.10Code: Collisions


This is the avatar code after we added collisions in Chapter 10, Project: Colli-
sions, on page 93.

<body></body>
<script src="http://gamingJS.com/Three.js"></script>
<script src="http://gamingJS.com/Tween.js"></script>
<script src="http://gamingJS.com/ChromeFixes.js"></script>
<script>
// This is where stuffinour game will happen:
varscene =newTHREE.Scene();

// This is what sees the stuff:
varaspect_ratio = window.innerWidth / window.innerHeight;
varcamera =newTHREE.PerspectiveCamera(75, aspect_ratio, 1, 10000);
camera.position.z = 500;
//scene.add(camera);

// This will draw what the camera sees onto the screen:
varrenderer =newTHREE.CanvasRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

// ******** START CODING ON THE NEXT LINE ********
varnot_allowed = [];

varmarker =newTHREE.Object3D();
scene.add(marker);

varcover =newTHREE.MeshNormalMaterial();
varbody =newTHREE.SphereGeometry(100);
varavatar =newTHREE.Mesh(body, cover);
marker.add(avatar);

varhand =newTHREE.SphereGeometry(50);

varright_hand =newTHREE.Mesh(hand, cover);
right_hand.position.set(-150, 0, 0);
avatar.add(right_hand);

varleft_hand =newTHREE.Mesh(hand, cover);
left_hand.position.set(150, 0, 0);
avatar.add(left_hand);

varfoot =newTHREE.SphereGeometry(50);

varright_foot =newTHREE.Mesh(foot, cover);
right_foot.position.set(-75, -125, 0);
avatar.add(right_foot);

Appendix 1. Project Code • 230


Prepared exclusively for Michael Powell report erratum • discuss

Free download pdf