Before we get started, letās take a look at why this book exists, what I expect you to know before you can start reading it, what the stuff inside will be like stylistically, and what you can expect to get out of it if you put a reasonable amount of effort into it.
ā 1.1 Why Does This Book Exist?
One thing that has changed in academia over the last 30 years is that we professors are no longer the Source of All Knowledge. In 1984 when I started out as a young assistant professor, the books and papers that we taught from were sparse and often difficult to obtain. Happily, the Internet has changed all that. Knowledge is everywhere. What we need to teach students now is understanding,1 not knowledge, which is something that we would have liked to have done all along, but we were stunned into complacency by the burden of memorization, repetition, and standardized tests.
Students thrive when they have an easy on-ramp to understanding difficult material. Letās use a visual metaphor to explain what I mean. When students are faced with a large block of new and difficult material (below, left) that they want to get on top of, they turn to manuals, tutorials, newsgroups, books, papers, whatever information they can gather together on the subject both online and in what we old-timers laughingly call the Real World. What they usually end up with is a jumble of mismatched and odd-shaped pieces of knowledge that are frustrating to put together (right):
Some of the documents that they find get too hard too fast, which means a steep on-ramp (below left). That could be because the author is very smart and has forgotten what itās like to be a beginner, or because he or she is writing for a very smart audience. On the other hand, some of the documents stay at a very shallow level, which can be slow and boring and not of much help in getting on top of the difficult material (right). These are often written by authors who think it is useful to teach very young children how to do trivial things with a computer, while ignoring the really difficult question of how to get them from there to the top of the block. (I suppose itās another example of Douglas Adamsā āSomebody Elseās Problemā field in action.)
Sometimes the author just isnāt very good at presenting the material, switching from very easy stuff to very hard stuff without warning (left) or leaving gaps that the average reader finds insurmountable (right):
Even worse, the material could be so badly organized that itās hardly an on-ramp at all. This often happens with new authors when their level of excitement is high and their organizational and pedagogical skills are low:
In my experience, students want an on-ramp that is neither too steep nor too flat, leading them almost to the top of the difficult material on a path that is as smooth and continuous as possible:
An on-ramp for game physics should probably be in 2D because it has almost all of the richness and complexity of 3D game physics, but it is, in a sense, only two-thirds as difficult since it is only missing a dimension. This is not so with game math (geometry and linear algebra). You could say that 2D game math is only one-third as hard as 3D game math because the former has only one axis of rotation while the latter has three. The Irish mathematician Sir William Rowan Hamilton (1805ā1865) discovered this to his dismay when he tried to generalize the concept of 2D rotations as complex numbers into three dimensions.2
You can imagine how happy I was when I found Box2D, Erin Cattoās excellent Open Source 2D physics engine [Catto 12]. It is, in my opinion, the ideal code base for teaching game physics. It is much less complex than a 3D physics engine, which means that students have a chance of mastering it during part of a 15-week semester. It requires only the addition of a 2D rendering engine to make a game, which is ideal for classroom use because, in my experience, a 2D minigame can be constructed in a few thousand lines of C++ code, whereas even a very simple 3D game starts at tens of thousands of lines of C++ code (for example, SAGE [Parberry et al. 07]).
Box2D is a real game physics engine in the sense that it has been used in some successful commercial games. Students like getting experience with a real piece of software instead of classroom code wherever they can. Best of all, Box2D is Open Source (which means it is free), it has a large and active online community behind it (which means it is relatively stable and bug-free), and it has been ported to multiple platforms (which means you can use it in your classes no matter what platform your students are proficient in).
So why this book? Box2D comes with an excellent online manual, but itās more useful for people who know what theyāre doing than it is to newbies. There are lots of tutorials and code examples online, but they are fragmentary and of variable quality, running the gamut from incoherent to excellent. My intention is to put all of this material together and craft it into a relatively smooth on-ramp for beginners.
ā 1.2 Preconditions
Letās talk about what you need to know before you can start reading this book. These are what a programmer would call preconditions and an educator would call prerequisites.
I hope youāre not afraid of mathematics. You should be familiar with a certain amount of high-school and early college mathematics, most importantly, linear algebra and geometry. Calculus up to the Fundamental Theorem of Calculus would be helpful too, but all I really expect from you in this area is a vague feeling of dĆ©jĆ vu. Iāll also be using a certain amount of what used to be called mechanics, a subject usually taught in applied mathematics or physics classes. If you can remember being taught s = ut + at2/2, then you are probably good to go.
Since this is a book about programming, I expect you to be able to write and debug code. Speaking of debugging, I am often struck by the fact that while we spend hours teaching students how to program, we spend very little time teaching them how to debug, yet even professional programmers seem to find themselves spending half3 their time debugging. Some instructors never mention it, some give it passing mention, some even make it seem that having bugs is a shameful thing that should never be mentioned in polite society.4 See Appendix B for some of my debugging tips.
To get the most out of this book you must be familiar with the C++ programming language and the object-oriented philosophy upon which it is based. The supplementary code that goes with this book will be written using Microsoft Visual Studio 10 and DirectX 9.0. If you are unfamiliar with these, I suggest that you get familiar with them fast. To get a job in the game industry, you need to be flexible and omnivorous about programming. You should be able to program for the major programming environments5 and, more importantly, be able to learn new ones fast. Your mantra should be āIf it takes code, then I can program it.ā
ā¢ IMPORTANT POINT ā¢
You need to have Visual Studio 10 and the DirectX 9.0 SDK installed on your computer before you can begin working with the source code. Obviously you need a PC running Windows. Visual Studio 10 is commercial software, but if you are a student, your school may very well have an MSDNAA subscription, in which case you can get it for free. The DirectX 9.0 SDK is a free download.
Sometimes it makes more sense pedagogically to look at more abstract algorithms and data structures rather than code. It might help to familiarize yourself with balanced trees by consulting a standard Algorithms text such as [Cormen et al. 01] before you read Section 8.4.