Introduction to Game Physics with Box2D
eBook - ePub

Introduction to Game Physics with Box2D

  1. 261 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Introduction to Game Physics with Box2D

Book details
Book preview
Table of contents
Citations

About This Book

Written by a pioneer of game development in academia, Introduction to Game Physics with Box2D covers the theory and practice of 2D game physics in a relaxed and entertaining yet instructional style. It offers a cohesive treatment of the topics and code involved in programming the physics for 2D video games.

Focusing on writing elementary game physics code, the first half of the book helps you grasp the challenges of programming game physics from scratch, without libraries or outside help. It examines the mathematical foundation of game physics and illustrates how it is applied in practice through coding examples. The second half of the book shows you how to use Box2D, a popular open source 2D game physics engine. A companion website provides supplementary material, including source code and videos.

This book helps you become a capable 2D game physics programmer through its presentation of both the theory and applications of 2D game physics. After reading the book and experimenting with the code samples, you will understand the basics of 2D game physics and know how to use Box2D to make a 2D physics-based game.

Frequently asked questions

Simply head over to the account section in settings and click on ā€œCancel Subscriptionā€ - itā€™s as simple as that. After you cancel, your membership will stay active for the remainder of the time youā€™ve paid for. Learn more here.
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
Both plans give you full access to the library and all of Perlegoā€™s features. The only differences are the price and subscription period: With the annual plan youā€™ll save around 30% compared to 12 months on the monthly plan.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, weā€™ve got you covered! Learn more here.
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Yes, you can access Introduction to Game Physics with Box2D by Ian Parberry in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming Games. We have over one million books available in our catalogue for you to explore.

Information

Publisher
CRC Press
Year
2017
ISBN
9781315360614
Edition
1

1
Read Me First

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):
fig0004
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.)
fig0005
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):
fig0006
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:
fig0007
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:
fig0008
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.

ā— 1.3 Postconditions

Youā€™re probably interested in what youā€™ll gain from mastering this book. These are what a programmer would call postconditions and an educator would call outcomes. Let me list the expected outcomes chapter by chapter.

ā— Part I: Introduction to Game Physics

Part I is designed to help you come to grips with the trials and tribulations of programming game physics from scratch, by hand, with no libraries or outside help. It consists of three chapters.

ā— Chapter 2: Mathematics for Game Physics

We start off pedal to the metal by examining the mathematical foundation of game physics and how it can be applied in practice. Weā€™ll be doing some geometry, linear algebra, and calculus. Thanks to RenĆ© Descartes, we learn that geometry and linear algebra are really the same thing, which is really great news. The expected outcomes for Chapter 2 are
  1. understanding of vectors, angles, and reflection;
  2. recollection of the suppressed memories of how much we hated the linear algebra and geometry classes we took in school;
  3. realization that we didnā€™t need to pay for two classes when theyā€™re really the same thing;
  4. understanding of exactly why atan2(a,b) is better than atan(a/b);
  5. a vague recollection that sines and cosines have something t...

Table of contents

  1. Cover
  2. Half Title
  3. Title
  4. Copyright
  5. Dedication
  6. Contents
  7. Preface
  8. 1 Read Me First
  9. I Introduction to Game Physics
  10. II Game Physics with Box2D
  11. III Appendices
  12. Bibliography
  13. Index