3D Game Programming

(C. Jardin) #1
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

// ******** START CODING ON THE NEXT LINE ********
document.body.style.backgroundColor ='black';

varsurface =newTHREE.MeshPhongMaterial({ambient: 0xFFD700});
varstar =newTHREE.SphereGeometry(50, 28, 21);
varsun =newTHREE.Mesh(star, surface);
scene.add(sun);

varambient =newTHREE.AmbientLight(0xffffff);
scene.add(ambient);

varsunlight =newTHREE.PointLight(0xffffff, 5, 1000);
sun.add(sunlight);

varsurface =newTHREE.MeshPhongMaterial({ambient: 0x1a1a1a, color: 0x0000cd});
varplanet =newTHREE.SphereGeometry(20, 20, 15);
varearth =newTHREE.Mesh(planet, surface);
earth.position.set(250, 0, 0);
scene.add(earth);

varsurface =newTHREE.MeshPhongMaterial({ambient: 0x1a1a1a, color: 0xb22222});
varplanet =newTHREE.SphereGeometry(20, 20, 15);
varmars =newTHREE.Mesh(planet, surface);
mars.position.set(500, 0, 0);
scene.add(mars);

clock =newTHREE.Clock();

functionanimate() {
requestAnimationFrame(animate);

vartime = clock.getElapsedTime();

vare_angle = time * 0.8;
earth.position.set(250* Math.cos(e_angle), 250* Math.sin(e_angle), 0);

varm_angle = time * 0.3;
mars.position.set(500* Math.cos(m_angle), 500* Math.sin(m_angle), 0);

vary_diff = mars.position.y - earth.position.y,
x_diff = mars.position.x - earth.position.x,
angle = Math.atan2(x_diff, y_diff);

earth_cam.rotation.set(Math.PI/2, -angle, 0);
earth_cam.position.set(earth.position.x, earth.position.y, 22);

// Now, show what the camera sees on the screen:

Appendix 1. Project Code • 242


Prepared exclusively for Michael Powell report erratum • discuss

Free download pdf