This book is a comprehensive guide to the linear algebra and collision detection games commonly use, but only briefly touches on the topic of collision resolution (Physics). The book is structured as follows:
All of of the topics covered in this book are used to progressivley build a rigid body physics engine. The final three chapters (14, 15 and 16) implement particle physics, rigid body physics and soft body physics (cloth). An appendinx is provided which briefly covers advanced topics, resources for exploring these topics as well as additional resources for exploring game physics.
Some of the figures created for the book did not translate well to print. To address this I have included the all of the book figures in this repository and published the figures online at: https://github.com/gamephysicscookbook/Figures
The first three chapters of the book are dedicated to teaching the basic linear algebra needed for game development. Every concept is explained in a mathematical context, source code is provided for every concept and images are also provided when something can be explored visually. Troughout the first three chapters the following data structures are created:
vec2
)vec3
)mat2
)mat3
)mat4
)When possible, matrix operations are implemented in a generic way. For example, code is provided to multiply two matrices of arbitrary sizes together.
The following intersections are covered in the book:
Point | Line | Ray | Sphere | AABB | OBB | Plane | Triangle | Frustum | |
---|---|---|---|---|---|---|---|---|---|
Point | |||||||||
Line | |||||||||
Ray | |||||||||
Sphere | |||||||||
AABB | |||||||||
OBB | |||||||||
Plane | |||||||||
Triangle | |||||||||
Frustum |
Chapter 14 covers naive particle physics, this chapter is meant as an introduction to setting up a physics loop and considering the general format of a physics loop. Chapter 15 is the most interesting chapter, it implements a basic rigid body physics engine. The basic engine has support for oriented boxes and spheres, stacking can be made to work but is not directly supported. Chapter 16 covers springs and how springs can be used to implement soft body physics. The final demo of the book is a soft body, cloth physics demo.
This being my first book, I've learned a lot about the writing process; perhaps even more about planning. The following is a list of things I've discovered while writing this book that I did not plan properly for:
Quaternions: Having a quaternion is a must! I planned to write the book using euler rotations and rotation matrices. While this worked, having access to quaternions would have made life a lot easyer.
Planes: Plane intersections should not return a boolean value, they should classify the intersection as: behind, intersecting, in front. Preferably, if the intersection is in front or behind, you want to return some indication of distance. This becomes very useful when doing Frustum culling. I didn't know how in-depth i was going to write about frustums, so i decided to do simple boolean intersectiosn.
Raycasting: I should have written raycasts to return a raycast result from the beginning. Having to later go back and re-write the raycasting API turned out to be more complicated to express in text than i anticipated. The tought behind this was to keep things simple at first, complicate only as needed.
There are various issues in the physics implementation of the source code. These issues come from the fact that the physics part of this book had to be condensed into three chapters. There was just not enough time to cover everything needed to make a robust rigid-body physics system. The biggest problem with the engine is the fact that there is no Arbiter.
Without an arbiter, we can't build a sequential impulse solver. This leaves us with a rather bare-bones naive impulse solver. The problem is, impulses are solved per contact per frame. This causes excessive sliding. I've compensated for the lack of sequential impulse with linear projection and agressive friciton biasing. Older physics engines do something similar, by relying on strong handed sleeping to mask the issue. Needless to say, sleeping was not implemented.
If I have a chance to write a second edition of this book, i will remove the chapters on two dimensional collisions (chapters 4, 5 and 6) and cut down the page count of the scene management chapters. I plan to use the extra pages to cover the following topics: