# Introduction to Game Physics with Box2D

## Ian Parberry

Language: English

Pages: 275

ISBN: 1466565764

Format: PDF / Kindle (mobi) / ePub

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.

GPU Pro 2: Advanced Rendering Techniques

Ontologies for Software Engineering and Software Technology

The Basics of Cloud Computing: Understanding the Fundamentals of Cloud Computing in Theory and Practice

Operating Systems: A Spiral Approach

The Magic of Computer Graphics: Landmarks in Rendering

Concise Guide to Databases: A Practical Introduction (Undergraduate Topics in Computer Science)

after 14 s? To get the answer, we use Equation (2.1): s = ut + at2 /2 = 0 + 5 × 142 m = 980 m. Inside the computer, we compute the distance moved in each frame and accumulate all those distances to get the total distance. We have to assume that the velocity is constant within each frame. We end up with the set of distances in Table 2.1, which is too small. The ﬁnal distance traveled is 910 m instead of 980. Of course, we can always change the code so that the velocity is updated ﬁrst: m_vVelocity

[ i ] = NULL ; m_nSize = size ; } // for } // constructor The destructor riﬄes through the object array and deletes any Game Objects that it points to. These will have been created by member function create below. Then, it destroys the Object List array. CObjectMana ge r ::~ CObjectManage r (){ for ( int i =0; i < MAX_OBJECTS ; i ++) delete m_pObjectList [ i ]; delete [] m_pObjectList ; } // destructor Speaking of function create, here it is. It ﬁnds the next empty place in m_pObjectList (if

this advice, you may find that Box2D’s numerical computations fail to stabilize. For example, if your bouncing balls seem to bounce forever, first check on your Physics World units. • 5.4 Our First Box2D App Let’s go ahead and make a simple Box2D App. It won’t be a game yet, but let’s at least prove that we can wire up Box2D to a rendering engine. Even if you don’t like the rendering engine in Chapter 3, at least it will serve as an example of how easy the process is. This is boring. Hit the

collide with circles and polygons but not with each other. The collision algorithms used by Box2D require that at least one of two colliding shapes have volume. Edge shapes have no volume, so edge-edge collision is not possible. Edge shapes are created as follows: b2Vec2 v1 (0.0 f , 0.0 f ); b2Vec2 v2 (1.0 f , 0.0 f ); b2EdgeShape edge ; edge . Set ( v1 , v2 ); Suppose we connect some edge shapes end-to-end, and a polygon slides along it. A ghost collision is caused when the polygon collides

PlayerHasWon ( float level ); The CObjectWorld constructor in ObjectWorld.cpp remains almost exactly the same as before but gets a new line of code to create the HUD. 7.3 • The Object World 173 Figure 7.14 • Getting a Game Object’s position. There’s a corresponding delete line in the destructor, too. m_pHeadsUpD is pl a y = new CHeadsUpDis pl ay (32); CreateHudObject asks m_pHeadsUpDisplay to create the requested HUD object. CGameObject * CObjectWorld :: CreateHudObj ec t ( GameObjectTyp e