Game Physics Cookbook

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:

• Chapters 1, 2 and 3 cover the basics Linear Algebra.
• Chapters 4, 5 and 6 cover two dimensional primitives and how to detect intersections between them.
• Chapters 7, 8, 9 and 10 cover three dimensional primitives and the most efficient way to determine intersections between them.
• Chapters 11, 12 and 13 cover meshes, scenes and scene organization.
• Chapters 14, 15 and 16 cover physics. Throughout these three chapters we built a very basic rigid body physics engine.

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.

Figures

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

Math

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:

• 2 dimensional vector (`vec2`)
• 3 dimensional vector (`vec3`)
• 2x2 matrix (`mat2`)
• 3x3 matrix (`mat3`)
• 4x4 matrix (`mat4`)

When possible, matrix operations are implemented in a generic way. For example, code is provided to multiply two matrices of arbitrary sizes together.

Collision Detection

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

Physics

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.

Learnings

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.

Issues

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.

Future

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:

• In depth discussion and implementation of GJK
• A proper arbiter system and a more stable collision resolution model.
• Capsule primitive
• Convex hull generation and collision testing

Book Info

1. Vectors
• Introduction
• Vector definition
• Component wise operations
• Dot product
• Magnitude
• Normalizing
• Cross product
• Angles
• Projection
• Reflection
2. Matrices
• Introduction
• Matrix definition
• Transpose
• Multiplication
• Identity matrix
• Determinant of a 2x2 matrix
• Matrix of minors
• Cofactor
• Determinant of a 3x3 matrix
• Operations on a 4x4 matrix
• Matrix inverse
3. Matrix Transforms
• Introduction
• Matrix majors
• Translation
• Scaling
• How rotations work
• Rotation matrices
• Axis angle rotation
• Vector matrix multiplication
• Transform matrix
• Projection matrix
4. 2D Primitive Shapes
• Introduction
• 2D points
• 2D lines
• Circle
• Rectangle
• Oriented rectangle
• Point containment
• Line intersection
5. 2D Collisions
• Introduction
• Circle to circle
• Circle to rectangle
• Circle to oriented rectangle
• Rectangle to rectangle
• Seperating Axis Theorem
• Rectangle to oriented rectangle
• Oriented rectangle to oriented rectangle
6. 2D Optimizations
• Introduction
• Containing circle
• Containing rectangle
• Simple and complex shapes
7. 3D Primitive Shapes
• Introduction
• Point
• Line segment
• Ray
• Sphere
• Axis aligned bounding box
• Oriented buonding box
• Plane
• Triangle
8. 3D Point Tests
• Introduction
• Point and sphere
• Point and axis aligned bounding box
• Point and oriented bounding box
• Point and plane
• Point and line
• Point and ray
9. 3D Shape Intersections
• Introduction
• Sphere to sphere
• Sphere to axis aligned bounding box
• Sphere to oriented bounding box
• Sphere to plane
• Axis aligned bounding box to axis aligned bounding box
• Axis aligned bounding box to oriented bounding box
• Axis aligned bounding box to plane
• Oriented bounding box to oriented bounding box
• Oriented bounding box to plane
• Plane to plane
10. 3D Line Intersections
• Introduction
• Raycast sphere
• Raycast axis aligned bounding box
• Raycast oriented bounding box
• Raycast plane
• Linetest sphere
• Linetest axis aligned bounding box
• Linetest oriented bounding box
• Linetest plane
11. Triangles and Meshes
• Introduction
• Point in triangle
• Closest point triangle
• Triangle to sphere
• Triangle to axis aligned bounding box
• Triangle to oriented box
• Triangle to plane
• Triangle to triangle
• robustness of the seperating axis theorem
• Raycast triangle
• Linetest triangle
• Mesh object
• Mesh optimization
• Mesh operations
12. Models and Scenes
• Introduction
• The model object
• Operations on models
• The scene object
• Operations on the scene
• The octree object
• Operations on the octree
• Octree and scene integration
13. Camera and Frustum
• Introduction
• Camera object
• Camera controls
• Frustum object
• Frusum from matrix
• Sphere in frustum
• Bounding box in frustum
• Octree culling
• Picking
14. Constraint Solving
• Introduction
• Framework introduction
• Raycast sphere
• Raycast bounding box
• Raycast plane and triangle
• Physics system
• Integrating particles
• Solving constraints
• Verlet integration
15. Manifolds and Impulses
• Introduction
• Manifold for spheres
• Manifold for boxes
• Rigid body modifications
• Linera velocity
• Linear impulse
• Physics system update
• Angular velocity
• Angular impulse
16. Sprints and Joints
• Introductions
• Particle modifications
• Springs
• Cloth
• Physics System modification
• Joints
• Introduction
• Generic collisions
• Stability improvements
• Sprints
• Open source physics engines
• Books
• Online resources
• Summary

Cover

Box2D is a 2D physics engine for games

Build Status Box2D Box2D is a 2D physics engine for games. Contributing Please do not submit pull requests with new features or core library changes.

Minetest is an open source voxel game engine with easy modding and game creation

Minetest is an open source voxel game engine with easy modding and game creation

My 3D game engine source code.

Rogy-Engine Development My 3D game engine source code. (NOT THE FINAL VERSION) Features: PBR shading and reflection probes with parallax correction. S

Rogy-Engine- - My 3D game engine source code.

Rogy-Engine Development My 3D game engine. (NOT THE FINAL VERSION- Windows only) Features: PBR shading and reflection probes with parallax correction.

A cycle-accurate Game Boy and Game Boy Color Emulator, with rewind feature.

Azayaka is a free and open-source Game Boy and Game Boy Color emulator written in C++. Features Cycle-Accurate emulation. Console based Debugg

The Game Boy ROM of the Game Boy bitcoin miner!

game-boy-bitcoin-miner The Game Boy ROM of the Game Boy bitcoin miner! To build this, currently this patch needs to be applied to GBDK: https://gist.g

CLUSEK-RT is a complex game engine written in C++ and the successor of the CLUSEK game engine

CLUSEK-RT is a complex game engine written in C++ and the successor of the CLUSEK game engine. This engine has been designed with a cross-platform design in mind. Thanks to Vulkan API it delivers a next-gen experience with ray tracing to both Linux and Windows platforms

Ground Engine is an easy to use Game Engine for 3D Game Development written in C++

Ground Engine is an easy to use Game Engine Framework for 3D Game Development written in C++. It's currently under development and its creation will b

A game made for the Game (Engineless) Jam using Raylib

Fastest Pizza Delivery A fun little 3D game made for the Game (Engineless) Jam. It is still is development but the basic gameplay is something l

• Wrong axes in OBB intersection tests?

The current code tests these axes:

``````Cross(test[0], test[0])
Cross(test[0], test[1])
Cross(test[0], test[2])

Cross(test[1], test[0])
Cross(test[1], test[1])
Cross(test[1], test[2])

Cross(test[2], test[0])
Cross(test[2], test[1])
Cross(test[2], test[2])
``````

while it should actually check these (all combinations of the axes of both boxes):

``````Cross(test[0], test[3])
Cross(test[0], test[4])
Cross(test[0], test[5])

Cross(test[1], test[3])
Cross(test[1], test[4])
Cross(test[1], test[5])

Cross(test[2], test[3])
Cross(test[2], test[4])
Cross(test[2], test[5])
``````
opened by vincent23 0
• GetInterval in OBBOBB test

I was looking on OBB vs OBB test and found that it's calling GetInterval() with obb1 twice.

https://github.com/gszauer/GamePhysicsCookbook/blob/15810bbf902c1cc19064c176a7e0626eda3b83bd/Code/Geometry3D.cpp#L384-L388

Is this intentional? or is second call supposed to be with obb2?

opened by bsy6766 0
Gabor Szauer
Devrel at Oculus. I make games, have a blog, and used to write books.
Improved version of the X-Ray Engine, the game engine used in the world-famous S.T.A.L.K.E.R. game series by GSC Game World.

OpenXRay OpenXRay is an improved version of the X-Ray Engine, the game engine used in the world-famous S.T.A.L.K.E.R. game series by GSC Game World. S

2.2k Jan 1, 2023
Stealthy way to hijack the existing game process handle within the game launcher (currently supports Steam and Battle.net). Achieve external game process read/write with minimum footprint.

Launcher Abuser Stealthy way to hijack the existing game process handle within the game launcher (currently supports Steam and Battle.net). Achieve ex

80 Nov 25, 2022
Game Boy, Game Boy Color, and Game Boy Advanced Emulator

SkyEmu SkyEmu is low level cycle accurate GameBoy, GameBoy Color and Game Boy Advance emulator that I have been developing in my spare time. Its prima

321 Jan 4, 2023
C++ library for multi-physics simulation

Project Chrono represents a community effort aimed at producing a physics-based modelling and simulation infrastructure based on a platform-independent, open-source design.

1.6k Dec 29, 2022
Bounce is a 3D physics engine for games.

Bounce Welcome! Bounce is a 3D physics engine for games. Features Common Efficient data structures with no use of STL Fast memory allocators Built-in

72 Aug 3, 2022
Godot bindings for the Rapier3D physics engine

Godot bindings for the Rapier3D physics library How to use There are two parts: A Godot module which must be compiled with the engine.

28 Dec 26, 2022
A multi core friendly rigid body physics and collision detection library suitable for games and VR applications.

Jolt Physics Library A multi core friendly rigid body physics and collision detection library suitable for games and VR applications. A YouTube video

2.5k Dec 31, 2022
SKR_Physics is a lightweight and easy to use 2D physics engine which is written in C++.

SKR_Physics SKR_Physics is a lightweight and easy to use 2D physics engine which is written in C++. Features Rectangle based collision system Very sim

0 Mar 8, 2022
Sequential impulses physics engine made for learning purposes

A 3D physics engine that uses Separating Axis Test for collision detection, the clipping method for generating contact manifold, contact point reducti

1 Nov 24, 2021