Book Image

Game Physics Cookbook

By : Gabor Szauer
Book Image

Game Physics Cookbook

By: Gabor Szauer

Overview of this book

Physics is really important for game programmers who want to add realism and functionality to their games. Collision detection in particular is a problem that affects all game developers, regardless of the platform, engine, or toolkit they use. This book will teach you the concepts and formulas behind collision detection. You will also be taught how to build a simple physics engine, where Rigid Body physics is the main focus, and learn about intersection algorithms for primitive shapes. You’ll begin by building a strong foundation in mathematics that will be used throughout the book. We’ll guide you through implementing 2D and 3D primitives and show you how to perform effective collision tests for them. We then pivot to one of the harder areas of game development—collision detection and resolution. Further on, you will learn what a Physics engine is, how to set up a game window, and how to implement rendering. We’ll explore advanced physics topics such as constraint solving. You’ll also find out how to implement a rudimentary physics engine, which you can use to build an Angry Birds type of game or a more advanced game. By the end of the book, you will have implemented all primitive and some advanced collision tests, and you will be able to read on geometry and linear Algebra formulas to take forward to your own games!
Table of Contents (27 chapters)
Game Physics Cookbook
Credits
About the Author
Acknowledgements
About the Reviewer
Acknowledgements
www.PacktPub.com
Customer Feedback
Preface
Index

Stability improvements


We can make several improvements to the stability of our physics engine. We fixed the problem of sinking using linear projection in Chapter 15, Manifolds and Impulses. Linear projection introduces its own flaws into our engine: jitter and object crawling. We used heavy friction to cover these issues up. Older physics engines had similar issues; they tended to use aggressive sleeping to cover these issues up. When a rigid body is asleep, it has no forces acting on it (including gravity) and therefore does not sink.

The more modern approach to fixing these issues is called Baumgarte Stabilization. Baumgarte Stabilization works by adding extra energy to physics resolution. This extra energy causes some jitter, but fixes the issues of sinking and crawling. We can add slop to the system, similarly to how we added slop to linear projections to fix the jitter issue.

Baumgarte Stabilization requires us to accumulate impulses over frames. In order to accumulate impulses, we need to keep track of collisions over several frames. This is where an Arbiter comes in. An arbiter can also be used to build up a collision manifest over several frames from the result of the GJK algorithm.

Arbiters

An arbiter is a data structure that keeps track of the contact points between two objects over several frames. It effectively contains almost the same data as a collision manifold. It needs to keep track of which objects are colliding, as well as the collision normal and depth of each collision point. They are built up over several frames, and the face-to-face collision of two boxes might look something like this:

To implement an arbiter system, we have to keep a list of active arbiters. For each frame we look for collisions between rigid bodies. If the colliding bodies do not have an arbiter in the list, we have to create a new arbiter for them and add it to the list. If the two objects have an arbiter in the list, we insert the contact point between the bodies into the list.

In each frame, we loop through every arbiter in the arbiter list. If an arbiter has more than four contact points, we need to take the furthest point and remove it from the arbiter. If any points are too far apart, we must remove those points from the arbiter. If an arbiter has no contact points left, we remove that arbiter from the list.

As an arbiter is built over several frames, it can be used with GJK and EPA to build a stable manifest over several frames. The GJK will produce a new contact in each frame, which will register with the arbiter system. After about four frames, any colliding objects should stabilize.

Accumulated impulse

Using accumulated impulses will solve object crawling and help eliminate some jitter. The major advantage of using accumulated impulses is that the impulse of any single contact point can be negative. We still have to ensure that the total accumulated impulse of all the contact points is positive.

To implement accumulated impulses, we keep track of the sum of the impulses needed to resolve a collision in the arbiter. Then, once the impulse has been calculated for every contact point and summed up in the arbiter, we apply the impulse to the rigid body. Before applying the impulse, we need to ensure that the total impulse is positive. We just clamp the final impulse to zero if it is negative.