Bonus #2: Flying Controls

If we make a space simulation, we’re going to want to fly through it, right? We can use the same controls that we used to fly through the planets in Chapter 5, ​Functions: Use and Use Again​. Start by loading the fly controls code collection at the very top of our code:

​ <body></body>
​ <script src=​"/three.js"​></script>
»<script src=​"/controls/FlyControls.js"​></script>

We will need yet another camera for this. Just below the moon-cam code, add a ship-cam:

​ ​var​ shipCam = ​new​ THREE.PerspectiveCamera(75, aspectRatio, 1, 10000);
​ shipCam.position.​set​(0, 0, 500);
​ scene.add(shipCam);

Add the fly controls code just below that. (See The Hitchhiker’s Guide to the Galaxy

Get 3D Game Programming for Kids, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.