3D Game Programming for KidsCreate Interactive Worlds with JavaScriptChris StromThe Pragmatic BookshelfDallas, Texas • Raleigh, North CarolinaMany of the designations used by manufacturers and sellers to distinguish their productsare claimed as trademarks. Where those designations appear in this book, and The PragmaticProgrammers, LLC was aware of a trademark claim, the designations have been printed ininitial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer,Pragmatic Programming, Pragmatic Bookshelf, PragProg and the linking g device are trade-marks of The Pragmatic Programmers, LLC.Every precaution was taken in the preparation of this book. However, the publisher assumesno responsibility for errors or omissions, or for damages that may result from the use ofinformation (including program listings) contained herein.Our Pragmatic courses, workshops, and other products can help you and your team createbetter software and have more fun. For more information, as well as the latest Pragmatictitles, please visit us at http://pragprog.com.Copyright © 2013 The Pragmatic Programmers, LLC.All rights reserved.No part of this publication may be reproduced, stored in a retrieval system, ortransmitted, in any form, or by any means, electronic, mechanical, photocopying,recording, or otherwise, without the prior consent of the publisher.Printed in the United States of America.ISBN-13: 978-1-937785-44-4Encoded using the finest acid-free high-entropy binary digits.Book version: B1.0—February 6, 2013IntroductionWelcome to the world of programming!I won’t lie, it can be a frustrating world sometimes (it makes me cry at leastonce a week). But it is totally worth the pain. You get to make this world dowhatever you want. You can share your world with others. You can buildthings that really make a difference.This book that you hold in your eager hands is a great way to get startedprogramming. It is chock full of clear and understandable explanations alongthe way. Best of all, we get to make some pretty cool games.This is going to be a blast.How I (the author) Learned to ProgramWhen I was a kid, I copied computer program games out of books. This wasa long time ago, so I bought books with nothing but programs, and typedthem into computers.When I first started doing it, I had no idea what I was doing. Eventually, Istarted to recognize certain things that were done over and over and almostunderstood them.I started to change things—little things at first—to see what happened. ThenI started making bigger changes. Eventually, I got pretty good at it. And aftera long time, I could write my own programs.I hope that this book will let you do the same. But with one important differ-ence: I am going to explain what is going on so you won’t have to guess quiteas much.What You Need for this BookNot all Web browsers can generate the cool 3D gaming objects that we willbuild in this book. To get the most out of the book, you should install Google• Click  HERE  to purchase this book now.  discussChrome1http://www.google.com/chrome. Other browsers will work, but some of theexercises in this book rely on features available only in Google Chrome. Onebrowser that will definitely not work with the exercises is Internet Explorer.For most of the exercises in the book, any old computer with Google Chromeinstalled will be sufficient. Later exercises that make use of interesting lighting,shadows and 3D materials, will need a computer capable of performing WebGL.You can find out if your computer supports WebGL by visiting the Get WebGLSite2http://get.webgl.org/. Don’t worry too much about WebGL, there is still a tonof programming you will be able to do if your computer can’t handle it.What is JavaScript?There are many, many programming languages. Some programmers enjoyarguing over which is the best programming language, but the truth is thatall languages offer unique and worthwhile things.In this book, we will use the JavaScript programming language. We programin JavaScript because it is the language of the Web. It is the only programminglanguage all Web browsers understand without needing any additional soft-ware. If you can program in JavaScript, not only can you make the kinds ofgames that we will learn in this book, but you can program just about everywebsite there is.We are not going to become experts in JavaScript.We will learn just enough JavaScript to be able to program the games in thisbook. It turns out that is quite a lot of JavaScript—enough to be ready tolearn the rest without too much difficulty.How to Read this BookThere are two different kinds of chapters in this book: project chapters andlearning chapters. The project chapters all start with “Project” like Chapter1, Project: Creating Simple Shapes, on page ?.If you want to learn programming the way that I did, just read the projectchapters and follow along with all the projects in them. You’ll create prettycool game players and worlds to play in. You’ll make spaceships. You’ll makeall sorts of cool stuff.If you have questions about why the games are written the way they are, thenread the learning chapters. We won’t go over everything about programming,1.https://www.google.com/chrome/2.http://get.webgl.org/Introduction • iv• Click  HERE  to purchase this book now.  discussbut it should be enough to understand why we do what we do. These are thechapters that I wish I had when I was a kid.Let’s Get Started!Enough introduction, let’s jump right into programming!• Click  HERE  to purchase this book now.  discussLet’s Get Started! • vThere will be plenty of time for learning later in this book. For now, let’s getstarted with programming!1.1Programming with the ICE Code EditorIn this book, we will be using the ICE Code Editor to do our programming.The ICE Code Editor runs right inside your browser. It lets us type in ourprogramming code and see the results right away.To get started, open the ICE Code Editor at http://gamingJS.com/ice using Google’sChrome Web browser. It should look something like this:That spinning, multi-sided thing is just a sample of some of the stuff that wewill be working on in this book. In this chapter, we will create a new projectnamed Shapes.To create a new project in the ICE Code Editor, we click on the menu button(the white button with three horizontal lines) on the upper right hand cornerof the screen and select New from the dropdown.• Click  HERE  to purchase this book now.  discussType the name of the project, Shapes, in the text field and click the Save button.Leave the template set as 3DStarterProject.Remember, none of the projects in this book will work if you are using ICECode Editor on Internet Explorer. While some of the exercises will work withMozilla Firefox, it is easier to just stick with a single browser (Google Chrome)for all our projects.Coding with the ICE Code EditorWe will be using the ICE Code Editor throughout this book. Youonly need web access the first time that you connect to http://gam-ingJS.com/ice/. After the first visit, ICE is stored in your browser soyou can keep working even if you are not connected to the Internet!When ICE opens a new 3D project, there is already a lot of code in the file.We will look closely at that code later, but for now, let’s begin our programmingadventure on line 20. Look for the line that says, “START CODING ON THENEXT LINE”:• 2• Click  HERE  to purchase this book now.  discussOn line 20, type the following:varshape=newTHREE.SphereGeometry(100);varcover=newTHREE.MeshNormalMaterial();varball=newTHREE.Mesh(shape,cover);scene.add(ball);Once you finish typing that, you should see something cool:The ball that we typed—the ball that we programmed—showed up in ICE.Congratulations! You just wrote your first JavaScript program!Don’t worry too much about the structure of the code just yet; we will getfamiliar with it in [xxx](#chapter.javascript_basics) For now, let’s take a closerlook at the 3D programming that we just did.3D things are built from two parts: the shape and a wrapper that covers theshape. These two things, the shape and its wrapper, are given a special namein 3D programming: Mesh.A mesh is a fancy word for 3D things. Meshes need shapes (sometimes calledgeometry) and something to cover them (sometimes called material).• Click  HERE  to purchase this book now.  discussProgramming with the ICE Code Editor • 3In this chapter we are going to look at different shapes. We will deal withdifferent covers for our shapes until [xxx](#chapter.lights_and_materials).Your work is automatically savedYour work is automatically saved, so you do not have to do ityourself. If you want to save the code yourself anyway, click thewhite menu button in ICE and select the Save option from thedropdown. That’s it!1.2Making Shapes with JavaScriptSo far, we have seen only one kind of shape: the sphere. Shapes can be simplelike cubes, pyramids, cones, and spheres. Shapes can also be more complexthings like faces or cars. In this book, we are going to stick with simple shapes.When we build things like trees, we will combine simple shapes, such asspheres and cylinders, to make them.Creating SpheresBalls are always called spheres in geometry and in 3D programming. Thereare two ways to control the shape of a sphere in JavaScript.Size: SphereGeometry(100)The first way that we can control a sphere is to describe how big it is. Wecreated a ball whose radius was 100 when we said newTHREE.SphereGeometry(100).What happens when you change the radius to 250?varshape=newTHREE.SphereGeometry(250);❶varcover=newTHREE.MeshNormalMaterial();varball=newTHREE.Mesh(shape,cover);scene.add(ball);❶This just points to where you should change the sphere’s size.This should make it much bigger:• 4• Click  HERE  to purchase this book now.  discussWhat happens if you change the 250 to 10? As you probably guessed, it getsmuch smaller. So that is one way that we can control a sphere’s shape. Whatis the other way?Chunky: SphereGeometry(100, 20, 15)If you click on the HideCode button in ICE, you may notice that our sphereisn’t _really_ a smooth ball:You can easily hide or show the codeIf you click the white HideCode button in the upper right corner ofthe ICE Editor window, then you will see just the game area andthe objects in the game. This is how you will play games in laterchapters. To get your code back, click the white ShowCode buttonwithin the ICE Editor.Computers cannot really make a ball. Instead they fake it by joining a bunchof squares (and sometimes triangles) to make something that looks like a ball.Normally, we will get the right number of chunks so that it is close enough.Sometimes, we want it to look a little smoother. To make it smoother, addsome extra numbers to the SphereGeometry() line:varshape=newTHREE.SphereGeometry(100,20,15);❶varcover=newTHREE.MeshNormalMaterial();• Click  HERE  to purchase this book now.  discussMaking Shapes with JavaScript • 5varball=newTHREE.Mesh(shape,cover);scene.add(ball);❶The first number is the size, the second number is the number of chunksaround the sphere, the third number is the number of chunks up anddown the sphere.This should make a sphere that is much smoother:Don’t Change the Chunkiness Unless You Have ToThe number of chunks that we get without telling SphereGeometry touse more may not seem great, but don’t change it unless you must.The more chunks that are in a shape, the harder the computer hasto work to draw it. As we will see in later chapters, it is usuallyeasier for a computer to make things look smooth by choosing adifferent wrapper for the shape.Play around with the numbers a bit more. You are already learning quite abit here and playing with the numbers is a great way to keep learning!When you are done playing, move the ball out of the way by setting its position:varshape=newTHREE.SphereGeometry(100);varcover=newTHREE.MeshNormalMaterial();varball=newTHREE.Mesh(shape,cover);scene.add(ball);ball.position.set(-250,250,-250);❶❶The three numbers move the ball to the left, up and back. Don’t worrytoo much about what the numbers do right now—we will talk aboutposition when we start building game characters in Chapter 3, Project:Making a Player, on page ?.Making Boxes with CubeThe next shape that we are going to make is a cube, which is another namefor a box. There are three different ways to change a cube’s shape: the width,the height and the depth.• 6• Click  HERE  to purchase this book now.  discussSize: CubeGeometry(300, 100, 20)To create a box, we are going to write more JavaScript below everything thatwe used to create our ball. Type the following:varshape=newTHREE.CubeGeometry(100,100,100);varcover=newTHREE.MeshNormalMaterial();varbox=newTHREE.Mesh(shape,cover);scene.add(box);If you have everything correct, you should see… a square:Well, that’s boring. Why do we see a square instead of a box? The answer isthat our camera, our perspective, is looking directly at one side of the box. Ifwe want to see more of the box, we need to move the camera, or turn the box.Let’s turn the box by rotating it:varshape=newTHREE.CubeGeometry(100,100,100);varcover=newTHREE.MeshNormalMaterial();varbox=newTHREE.Mesh(shape,cover);scene.add(box);box.rotation.set(0.5,0.5,0);❶❶These three numbers turn the box down, counterclockwise, and left-right.Play with the numbers if you like. To make a full turn in any direction,you would need a number around 7.3 (we’ll talk about that number later).This should get the cube rotated so that we can see that it really is a cube:Each side of a cube does not have to be the same size. Our box so far is 100wide (from left to right), 100 tall (up and down) and 100 deep (front to back).Let’s change it so that it is 300 wide, 100 tall and only 20 deep:varshape=newTHREE.CubeGeometry(300,100,20);varcover=newTHREE.MeshNormalMaterial();• Click  HERE  to purchase this book now.  discussMaking Shapes with JavaScript • 7varbox=newTHREE.Mesh(shape,cover);scene.add(box);box.rotation.set(0.5,0.5,0);This should show something like:Play around with the numbers to get a good feel for what they can do.Believe it or not, you already know a ton about JavaScript and 3D program-ming. There is still lots to learn, of course, but we can already make ballsand boxes. We can already move them and turn them. And we only had towrite 10 lines of JavaScript to do it all—nice!Like the ball, let’s move our box out of the way so that we can play with moreshapes:varshape=newTHREE.CubeGeometry(300,100,20);varcover=newTHREE.MeshNormalMaterial();varbox=newTHREE.Mesh(shape,cover);scene.add(box);box.rotation.set(0.5,0.5,0);box.position.set(250,250,-250);Using Cylinders for all Kinds of ShapesA cylinder, which is also sometimes called a tube, is a surprisingly usefulshape in 3D programming. Think about it: cylinders can be used as treetrunks, tin cans, wheels. Did you know that cylinders can be used to createcones, evergreen trees and even pyramids? Let’s find out how!Size: CylinderGeometry(20, 20, 100)Below the box code, type in the following to create a cylinder:varshape=newTHREE.CylinderGeometry(20,20,100);varcover=newTHREE.MeshNormalMaterial();vartube=newTHREE.Mesh(shape,cover);scene.add(tube);If you rotate that a little (you remember how to do that from the last section,right?) then you might see something like:• 8• Click  HERE  to purchase this book now.  discussIf you were not able to figure out how to rotate the tube, don’t worry. Justadd this line, after the line with scene.add(tube):tube.rotation.set(0.5,0,0);When making cylinders, the first two numbers describe how big the top andbottom of the cylinders are. The last number is how tall the cylinder is. Soour tube has a top and bottom that both are 20 in size and that is 100 in height.If you change the first two numbers to 100 and the last number to 20, whathappens? What happens if you make the top 1, the bottom 100 and the height100?Try This YourselfPlay with those numbers and see what you can create!What did you find?A flat cylinder is a disc:And a cylinder with either the top or bottom with a size of 1 is a cone:It should be clear that you can do a lot with cylinders, but we haven’t seeneverything yet. We have one trick left.• Click  HERE  to purchase this book now.  discussMaking Shapes with JavaScript • 9Pyramids: CylinderGeometry(1, 100, 100, 4)Did you notice that cylinders also look chunky? It should be no surprise then,that you can control the chunkiness of cylinders. If you set the number ofchunks to 20, for instance, with the disc:varshape=newTHREE.CylinderGeometry(100,100,10,20);varcover=newTHREE.MeshNormalMaterial();vartube=newTHREE.Mesh(shape,cover);scene.add(tube);tube.rotation.set(0.5,0,0);Then you should see something like:Just as with spheres, you should only use lots of chunks like that when youreally, really need to.Can you think how you might turn this into a pyramid? You have all of theclues that you need. See if you can figure it out.Try This YourselfPlay with different numbers and see what you can create!Were you able to figure it out? Don’t worry if you didn’t. It’s actually prettysneaky the way that we will do it.The answer is that you need to decrease the number of chunks that you useto make a cone. If you set the top to 1, the bottom to 100, the height to 100and the number of chunks to 4, then you will get:It might seem like a cheat to do something like this to create a pyramid, butthis brings us to a very important tip with any programming:• 10• Click  HERE  to purchase this book now.  discussCheat Whenever PossibleYou shouldn’t cheat in real life, but in programming—especially in3D programming—you should always look for easier ways of doingthings. Even if there is a usual way to do something, there may bea better way to do it.You are doing great so far. Let’s move on to the last two shapes that we aregoing to learn about in this chapter.• Click  HERE  to purchase this book now.  discussMaking Shapes with JavaScript • 11CHAPTER 5Functions: Use and Use AgainWe have come across functions more than once. Most recently we saw themin Chapter 3, Project: Making a Player, on page ?, where we used them tomake a forest. If you were paying close attention, you may have noticed thatwe also used a function to build the keyboard event listener in the samechapter.Although we have used functions already, we have not talked too much aboutthem. You may already have a sense that they are pretty powerful, so let’stake a closer look now.We are not going to talk about every aspect of functions—they can get quitecomplicated. We will talk just enough to be able to understand the functionsthat we use throughout the book.5.1Getting StartedCreate a new project in the ICE Code Editor. Use the EmptyProject templateand call it Functions.After the opening <script> tag, enter the following JavaScript.varlog=document.createElement('div');log.style.height='75px';log.style.width='450px';log.style.overflow='auto';log.style.border='1pxsolid#666';log.style.backgroundColor='#ccc';log.style.padding='8px';log.style.position='absolute';log.style.bottom='10px';log.style.right='20px';document.body.appendChild(log);varmessage=document.createElement('div');When you are done with this chapter, you will:•Learn a super-powerful tool (functions) forprogrammers•Know two reasons to use functions•Understand how JavaScript functions work• Click  HERE  to purchase this book now.  discussmessage.textContent='Hello,JavaScriptfunctions!';log.appendChild(message);message=document.createElement('div');message.textContent='MynameisChris.';log.appendChild(message);message=document.createElement('div');message.textContent='Ilikepopcorn.';log.appendChild(message);The first chunk of that code creates a place within the browser to log messages.The last three blocks of code write three different messages to that log. If youhave everything typed in correctly, you should see the three messages printedat the bottom-right of the page.Back in Chapter 3, Project: Making a Player, on page ?, we used a functionto avoid having to repeat the same process for creating a tree four times. Soyou can probably guess the first thing that we are going to change here. Let’schange the way that we log those three messages.Start by deleting everything starting with the first varmessage line all the waythrough the last log.appendChild line. Where that code was, add the following.logMessage('Hello,JavaScriptfunctions!',log);logMessage('MynameisChris.',log);logMessage('Ilikepopcorn.',log);functionlogMessage(message,log){varholder=document.createElement('div');holder.textContent=message;log.appendChild(holder);}When we write that code, a surprising thing happens—it gets easier to read.Even non-programmers could read those first three lines and figure out thatthey send a message to the log. Believe it or not, this is a huge win for pro-grammers like us.Chapter 5. Functions: Use and Use Again • 4• Click  HERE  to purchase this book now.  discussReadable code is easier to change laterOne of the skills that separates great programmers from good pro-grammers is the ability to change working code. And great program-mers know that it is easier to make changes when the code is easyto read.If we decide later that we want to add the time before each message, it ismuch easier to figure out where to make that change now. Obviously we needto change something inside the function. Before, it would have taken us sometime to figure out that those three code blocks were writing log messages andhow to change them.This also brings up a very important rule.Keep your code DRY—Don’t Repeat YourselfThis book was published by the same people behind a very famousbook called The Pragmatic Programmer. If you keep programming,you will read that book one day. In that book, there is a famous tipthat good programmers keep their code DRY—that they follow therule known as Don’t Repeat Yourself, or DRY for short.When we first wrote our code, we repeated three things:1.Creating a holder for the message2.Adding a text message to the holder3.Adding the message holder to the logIt was easy to see that we were repeating ourselves since the code in each ofthe three chunks was identical except for the message. This is anotheropportunity for us to be lazy. If we add more than three messages, we onlyhave to type one more line, not three.And of course, if we have to change something about the log message, weonly have to change one function, not three different blocks of code.We are not quite done using functions here. If you look at all of the code, youwill notice that it takes a long time to get to the important stuff.• Click  HERE  to purchase this book now.  discussGetting Started • 5The important work—writing the messages—does not start until line 15.Before we write messages to the log we need a log, but all of that other stuffis just noise.To fix that, let’s move the noise into a function below the logMessage() lines.functionmakeLog(){varholder=document.createElement('div');holder.style.height='75px';holder.style.width='450px';holder.style.overflow='auto';holder.style.border='1pxsolid#666';holder.style.backgroundColor='#ccc';holder.style.padding='8px';holder.style.position='absolute';holder.style.bottom='10px';holder.style.right='20px';document.body.appendChild(holder);returnholder;}Note that we have changed log to holder. Also, don’t forget the last line thatreturns holder.With that, we can create our log with this function. Our first four lines becomethe following:varlog=makeLog();logMessage('Hello,JavaScriptfunctions!',log);logMessage('MynameisChris.',log);logMessage('Ilikepopcorn.',log);That is some very easy-to-read code!Chapter 5. Functions: Use and Use Again • 6• Click  HERE  to purchase this book now.  discussIt turns out to be more difficult to write code like that than you would think.Really good programmers know that you don’t use functions until you havea good reason for them. In other words, good programmers do exactly whatwe have done here: write working code first, then look for ways to make itbetter.Always Start with Ugly CodeYou are a very smart person. You have to be to have made it thisfar. So you must be thinking, “Oh, I can just write readable codefirst.”Believe me when I say that you can’t. Programmers know this sowell that we have a special name for trying it: premature generaliza-tion. That’s just a fancy way to say it is a mistake to guess howfunctions are going to be used before writing ugly code first. Pro-grammers have fancy names for mistakes that we make a lot.5.2Understanding Simple FunctionsSo far we have looked at reasons why we want to use functions. Let’s see howfunctions actually work.Remove the three logMessage() lines from the code. Write the following after thevarlog=makeLog line.logMessage(hello("PresidentObama"),log);logMessage(hello("Mom"),log);logMessage(hello("YourName"),log);functionhello(name){return'Hello,'+name+'!Youlookveryprettytoday:)';}The result of this hello() function would be to return the phrase: “Hello, Presi-dent Obama! You look very pretty today :)”. Logging these phrases shouldlook something like:There is a lot going on in the hello function to make that work, so let’s breakthe function down into smaller pieces.• Click  HERE  to purchase this book now.  discussUnderstanding Simple Functions • 7functionhello(name){❶return'Hello,'+name+'!Youlookveryprettytoday:)';❷}The piece of a function are:❶The word function, which tells JavaScript that we are making a... function.The name of the function, hello in this case.Function arguments. In this case, we are accepting one argument (name)that we will use inside the function body. When we call the function withan argument—hello(Fred)—then we are telling the function that any time ituses the name argument, it is the same as using Fred.The body of the function starts with an open curly brace, {, and ends witha closing curly brace }. You have probably never used curly braces whenwriting English. You’ll use them a lot when writing JavaScript.❷The word return tells JavaScript what we want the result of the functionto be. It can be anything: numbers, letters, words, dates, even moreinteresting things.JavaScript lines, even those inside functions, should end with a semi-colon.Letters, words, and sentences are StringsThings inside quotes 'Hello', are called strings. Even in other program-ming languages, letters, words and sentences are usually calledstrings.Always be sure to close your quotes. If you forget, you will get veryweird errors that are hard to fix.Next, let’s try to break it intentionally so that we get an idea of what to dowhen things go wrong.5.3When Things Go WrongLet’s put our hacker hats on and try to break some functions. While it’s easyto do something wrong with JavaScript functions, it is not always easy tofigure out what you did wrong. The most common mistakes that programmersmake generate weird errors. Let’s take a look so that you might be betterprepared.Chapter 5. Functions: Use and Use Again • 8• Click  HERE  to purchase this book now.  discussUnexpected ErrorsThe most common thing to do is forget a curly brace://Missingacurlybrace-thiswon'twork!functionhello(name)return'Hello,'+name+'!Youlookveryprettytoday:)';}This is a compile-time error in JavaScript—one of the errors that JavaScriptcan detect when it trying to read, compile and run—that we met in[xxx](#section.console_debugging). Since it is a compile-time error, the ICECode editor will tell us about the problem.What happens if we put the curly brace back, but remove the curly braceafter the return statement?//Missingacurlybrace-thiswon'twork!functionhello(name){return'Hello,'+name+'!Youlookveryprettytoday:)';There are no errors in our hello function, but there is an error at the verybottom of our code.This can be a tough error to fix. Often programmers will type many lines andpossibly several functions before they realize that they have done somethingwrong. Then it just takes time to figure out where you meant to add a curlybrace.ChallengeTry the following broken code on your own. Where do the errors show up?Hint: as in Section 2.4, Debugging in the Console, on page ?, some of thesemay be run-time errors.Don’t type parentheses around the argument://Missingparenthesesaroundthearguments-thiswon'twork!functionhelloname{return'Hello,'+name+'!Youlookveryprettytoday:)';}• Click  HERE  to purchase this book now.  discussWhen Things Go Wrong • 9Don’t type an argument:functionhello(){return'Hello,'+name+'!Youlookveryprettytoday:)';}Use a different variable name in the function body:functionhello(name){return'Hello,'+person+'!Youlookveryprettytoday:)';}Call the function with the wrong name:logMessage(helo("PresidentObama"),log);functionhello(name){return'Hello,'+name+'!Youlookveryprettytoday:)';}Wow! There sure are a lot of ways to break functions. And believe me whenI tell you that you will break functions like these and many other ways asyou get to be a great programmer.Great programmers break things all the time. Because theybreak things so much, they are really good at fixing things. Thisis what makes great programmers great.Tip 1Don’t ever be upset at yourself if you break code. Broken code is a chance tolearn. And don’t forget to use the JavaScript Console like we learned inPlaying with the Console and Finding What’s Broken to help troubleshoot!5.4Stupid Function Tricks (or are they Awesome Tricks?)Functions are so special in JavaScript that you can do all sorts of crazy thingsto them.RecursionChange the hello like this:functionhello(name){varret='Hello,'+name+'!'+'Youlookveryprettytoday:)';if(!name.match(/again/)){ret=ret+'///'+hello(name+'(again)');❶}returnret;}Chapter 5. Functions: Use and Use Again • 10• Click  HERE  to purchase this book now.  discuss❶Look closely here. Inside the body of the function hello, we are calling thefunction hello!This will log the hello messages twice.A function that calls itself like this is actually not crazy. It is so common thatit has a special name: a recursive function.Be careful with recursive functions! If there is nothing that stops the recursivefunction from calling itself over and over, you will lock your browser and haveto go into edit-only mode to fix it (see Appendix 1, When ICE is Broke, on page?).In this case, we stop the recursion by only calling the hello function again ifthe name variable does not match the again. If name does not match again, thenwe call hello() with name+'(again)' so that the next call will include again.Recursion can be a tough concept, but you have already seen it in the nameof your code editor:•What does the I in ICE Code Editor stand for?•It stands for ICE Code Editor.•What does the I in ICE Code Editor stand for?•It stands for ICE Code Editor.•What does the I in ICE Code Editor stand for?•People will keep asking that question until they get sick of asking. Computersdon’t get sick of asking, so you have to tell them when to get sick of it.5.5What’s NextFunctions are a very powerful tool for JavaScript programmers. As you willsee shortly, we are going to make a lot of use of them in the upcoming chap-ters. Let’s get started in the next chapter as we teach our player avatar howto move its arms!• Click  HERE  to purchase this book now.  discussWhat’s Next • 11CHAPTER 10Project: CollisionsAfter Chapter 8, Project: Turning Our Player, on page ?, we have ourselves apretty slick game player. It moves, it walks, it even turns. But you may havenoticed something odd about our player. It can walk through trees.This will be another chapter where we use math, especially geometry concepts.And again, we should find them pretty easy.In this chapter, we are going to use tools that are built into our Three.js 3DJavaScript library to prevent the player-in-a-tree effect. As we will see inother chapters, there are other ways to do the same thing. There are timeswhen using an approach like what we cover in this chapter makes sense.10.1Getting StartedLet’s start by making a new copy of our player. From the menu in the ICECode Editor, select MakeaCopy and enter MyPlayer:Collisions as the new projectname.When you are done with this chapter, you will…•Be able to stop game elements from movingthrough each other•Understand collisions, which are importantin gaming•Have game boundaries for our player• Click  HERE  to purchase this book now.  discuss10.2Rays and IntersectionsThe way that we prevent our player from walking through trees is actuallyquite simple. Imagine an arrow pointing down from our player.In geometry, we call an arrow point a ray. A ray is what you get when youstart in one place and point in a direction. In this case, the place is whereour player is and the direction is down. It’s silly sometimes how we give namesto such simple ideas. But make no mistake, it is very important for program-mers to know these names.Programmers Like to Give Fancy Names to Simple IdeasKnowing the names for simple concepts makes it easier to talk toother people doing the same work. Programmers call these namespatterns.Now that we have our ray pointing down, imagine circles on the ground aroundour trees.Here is the crazy-simple way that we prevent our player from running into atree: we don’t! Instead, we prevent the player’s ray from pointing through thetree’s circle.Chapter 10. Project: Collisions • 4• Click  HERE  to purchase this book now.  discussIf, at any time, we find that the next movement would place the player’s rayso that it pointed through the circle, we stop the player from moving. That’sall there is to it!Star Trek II: The Wrath of KhanIt may seem strange, but watching certain science fiction movieswill make your life easier as a programmer. Sometimes, program-mers say silly things that turn out to be quotes from movies. It isnot a requirement to watch or even like these movies, but it canhelp.One such quote is from the classic Star Trek II: The Wrath of Khan.The quote is “He is intelligent, but not experienced. His patternindicates two-dimensional thinking.”The bad guy in the movie was not used to thinking in threedimensions and this was used against him. In this case, we aredoing exactly the opposite. In our three-dimensional game, we arethinking about collisions only in two dimensions (X and Z), com-pletely ignoring the up-and-down Y dimension.This is yet another example of cheating whenever possible. Real3D collisions are very hard and require new JavaScript libraries.But we can cheat and get the same effect in many cases usingeasier tricks.At this point, a picture should be beginning to form in your mind of what todo next. We are going to need a list of these tree circle boundaries that ourplayer will not be allowed to enter. We will need to build those circle bound-aries when we build the trees. We need to detect when the player is about to• Click  HERE  to purchase this book now.  discussRays and Intersections • 5enter a circle boundary. Last, we need to stop the player from entering theseforbidden areas.First, let’s establish the list that will hold all forbidden boundaries. Just belowthe “STARTCODINGONTHENEXTLINE” line, add the following.collisions/collisions.htmlvarnot_allowed=[];Recall from Section 7.4, Listing Things, on page ? that square brackets areJavaScript’s way of making lists. Here, our empty square brackets create anempty list. The not_allowed variable is an empty list of spaces in which theplayer is not allowed.Next, find where makeTree() is defined. When we make our tree, we are goingto make the boundaries as well. Add the following code after the line thatadds the treetop to the trunk, and before the line that sets the trunk position.collisions/collisions.htmlvarboundary=newTHREE.Mesh(newTHREE.CircleGeometry(300),newTHREE.MeshNormalMaterial());boundary.position.y=-100;boundary.rotation.x=-Math.PI/2;trunk.add(boundary);not_allowed.push(boundary);As an experienced 3D programmer, there is nothing too fancy there. We createour usual 3D Mesh—this time with a simple circle geometry. We rotate it sothat it lies flat and position it below the tree. And, of course, we finish byadding it to the tree.But unlike normal meshes, we are not quite done with our boundary mesh.We push it onto the list of not-allowed spaces. Now every time that we makea tree with the makeTreeAt() function we are building up this list. Let’s dosomething with that list.At the very bottom of our code, just above the </script> tag, add the followingcode to detect collisions.collisions/collisions.htmlfunctiondetectCollisions(){varvector=newTHREE.Vector3(0,-1,0);varray=newTHREE.Ray(marker.position,vector);varintersects=ray.intersectObjects(not_allowed);if(intersects.length>0)returntrue;returnfalse;Chapter 10. Project: Collisions • 6• Click  HERE  to purchase this book now.  discuss}This function returns a boolean—a yes or no answer—depending on whetheror not the player is colliding with a boundary. This is where we make our rayto see if it points through anything. As described earlier, a ray is the combi-nation of a direction, or vector (down in our case) and a point (the player’smarker.position). We then ask that ray if it goes through (intersects) any of thenot_allowed objects. If the ray does intersect a not-allowed object, then theintersects variable will have a length that is greater than zero. In that case, wehave detected a collision and we return true. Otherwise, there is no collisionand we return false.Collisions are a very hard problem to solve in many situations, so you aredoing great just following along with this. But we are not quite done. We candetect when a player is colliding with a boundary, but we have not actuallystopped the player yet. Let’s do this in the keydown listener.In the keydown listener, if an arrow key is pressed, we change the player’sposition.collisions/collisions.htmlcase37://leftmarker.position.x=marker.position.x-5;is_moving_left=true;break;case37://leftis_moving_left=false;break;It is possible that such a change means that the player is now in the boundary.If so, we have to undo the move right away. Add the following code at thebottom of the keydown event listener (just before the event.preventDefault() line).collisions/collisions.htmlif(detectCollisions()){if(is_moving_left)marker.position.x=marker.position.x+5;if(is_moving_right)marker.position.x=marker.position.x-5;if(is_moving_forward)marker.position.z=marker.position.z+5;if(is_moving_back)marker.position.z=marker.position.z-5;}Read through these lines to make sure that you understand them. That bitof code says that, if we detect a collision, then check the direction in whichwe are moving. If we are moving left, then reverse the movement that theplayer just did—go back the opposite direction the same amount.With that, our player can walk up to the tree boundaries, but go no farther.• Click  HERE  to purchase this book now.  discussRays and Intersections • 7Yay! That might seem like some pretty easy code, but you just solved a veryhard problem in game programming.10.3The Code So Farcollisions/collisions.html<body></body><scriptsrc="http://gamingJS.com/Three.js"></script><scriptsrc="http://gamingJS.com/Tween.js"></script><scriptsrc="http://gamingJS.com/ChromeFixes.js"></script><script>//Thisiswherestuffinourgamewillhappen:varscene=newTHREE.Scene();//Thisiswhatseesthestuff:varaspect_ratio=window.innerWidth/window.innerHeight;varcamera=newTHREE.PerspectiveCamera(75,aspect_ratio,1,10000);camera.position.z=500;//scene.add(camera);//Thiswilldrawwhatthecameraseesontothescreen:varrenderer=newTHREE.WebGLRenderer();renderer.setSize(window.innerWidth,window.innerHeight);document.body.appendChild(renderer.domElement);//********STARTCODINGONTHENEXTLINE********varnot_allowed=[];varmarker=newTHREE.Object3D();scene.add(marker);varcover=newTHREE.MeshNormalMaterial();varbody=newTHREE.SphereGeometry(100);varplayer=newTHREE.Mesh(body,cover);marker.add(player);varhand=newTHREE.SphereGeometry(50);varright_hand=newTHREE.Mesh(hand,cover);Chapter 10. Project: Collisions • 8• Click  HERE  to purchase this book now.  discussright_hand.position.set(-150,0,0);player.add(right_hand);varleft_hand=newTHREE.Mesh(hand,cover);left_hand.position.set(150,0,0);player.add(left_hand);varfoot=newTHREE.SphereGeometry(50);varright_foot=newTHREE.Mesh(foot,cover);right_foot.position.set(-75,-125,0);player.add(right_foot);varleft_foot=newTHREE.Mesh(foot,cover);left_foot.position.set(75,-125,0);player.add(left_foot);marker.add(camera);//TreesmakeTreeAt(500,0);makeTreeAt(-500,0);makeTreeAt(750,-1000);makeTreeAt(-750,-1000);functionmakeTreeAt(x,z){vartrunk=newTHREE.Mesh(newTHREE.CylinderGeometry(50,50,200),newTHREE.MeshBasicMaterial({color:0xA0522D}));vartop=newTHREE.Mesh(newTHREE.SphereGeometry(150),newTHREE.MeshBasicMaterial({color:0x228B22}));top.position.y=175;trunk.add(top);varboundary=newTHREE.Mesh(newTHREE.CircleGeometry(300),newTHREE.MeshNormalMaterial());boundary.position.y=-100;boundary.rotation.x=-Math.PI/2;trunk.add(boundary);not_allowed.push(boundary);trunk.position.set(x,-75,z);scene.add(trunk);• Click  HERE  to purchase this book now.  discussThe Code So Far • 9}//Now,animatewhatthecameraseesonthescreen:varclock=newTHREE.Clock(true);functionanimate(){requestAnimationFrame(animate);TWEEN.update();walk();turn();acrobatics();renderer.render(scene,camera);}animate();functionwalk(){if(!isWalking())return;varposition=Math.sin(clock.getElapsedTime()*5)*50;right_hand.position.z=position;left_hand.position.z=-position;right_foot.position.z=-position;left_foot.position.z=position;}functionturn(){vardirection=0;if(is_moving_forward)direction=0;if(is_moving_back)direction=Math.PI;if(is_moving_right)direction=Math.PI/2;if(is_moving_left)direction=-Math.PI/2;spinAvatar(direction);}functionspinAvatar(direction){newTWEEN.Tween({y:player.rotation.y}).to({y:direction},100).onUpdate(function(){player.rotation.y=this.y;}).start();}varis_cartwheeling=false;varis_flipping=false;functionacrobatics(){if(is_cartwheeling){player.rotation.z=player.rotation.z+0.05;}if(is_flipping){Chapter 10. Project: Collisions • 10• Click  HERE  to purchase this book now.  discussplayer.rotation.x=player.rotation.x+0.05;}}varis_moving_left,is_moving_right,is_moving_forward,is_moving_back;functionisWalking(){if(is_moving_right)returntrue;if(is_moving_left)returntrue;if(is_moving_forward)returntrue;if(is_moving_back)returntrue;returnfalse;}document.addEventListener('keydown',function(event){switch(event.keyCode){case37://leftmarker.position.x=marker.position.x-5;is_moving_left=true;break;case38://upmarker.position.z=marker.position.z-5;is_moving_forward=true;break;case39://rightmarker.position.x=marker.position.x+5;is_moving_right=true;break;case40://downmarker.position.z=marker.position.z+5;is_moving_back=true;break;case67://Cis_cartwheeling=!is_cartwheeling;break;case70://Fis_flipping=!is_flipping;break;}if(detectCollisions()){if(is_moving_left)marker.position.x=marker.position.x+5;if(is_moving_right)marker.position.x=marker.position.x-5;if(is_moving_forward)marker.position.z=marker.position.z+5;if(is_moving_back)marker.position.z=marker.position.z-5;}event.preventDefault();});document.addEventListener('keyup',function(event){switch(event.keyCode){• Click  HERE  to purchase this book now.  discussThe Code So Far • 11case37://leftis_moving_left=false;break;case38://upis_moving_forward=false;break;case39://rightis_moving_right=false;break;case40://downis_moving_back=false;break;}event.preventDefault();});functiondetectCollisions(){varvector=newTHREE.Vector3(0,-1,0);varray=newTHREE.Ray(marker.position,vector);varintersects=ray.intersectObjects(not_allowed);if(intersects.length>0)returntrue;returnfalse;}</script>10.4What’s NextCollision detection in games is a really tricky problem to solve, so congratula-tions on getting this far. It gets even harder once you have to worry aboutmoving up and down in addition to left/right/back/forward. But the conceptis the same.Usually we rely on code libraries written by other people to help us with thosecases. In some of the games coming shortly, we will use just such a codelibrary.But first, we are going to put the finishing touch on our player game. In thenext chapter, we are going to add a heads up display (HUD) and use that tolet our player go on a little scavenger hunt.Chapter 10. Project: Collisions • 12• Click  HERE  to purchase this book now.  discuss