When I first started this blog several years ago, one of the goals was to put together a detailed walkthrough or tutorial for building a simple physics engine. Unfortunately, it’s now several years later and that tutorial hadn’t shown up, and for that I apologize. Now that I’ve started getting settled at my new job, I’m finally putting this article series together and hope that you still find it useful.
Before we dive in, there are a few things that need mentioning:
There is no single correct way to build a real-time physics engine. Each year, new papers and presentations surface with different variations and techniques for modeling and solving constraints, handling collision detection, modeling soft body interactions, and more. This is an active area of research and development. Not every application requires the same features, accuracy, or level of performance. This too affects the types of physics engines out there. Some are much simpler, easier to understand, and while they might be unable to handle the performance demands of a modern AAA game with heavy physics, are often more than sufficient for simpler games and other uses. Other engines are more exotic, require much more technical understanding to follow, but can stand up to the demands of even the most complex games of the current generation.
The physics we’ll explore in this blog will start simple. The aim is to provide the foundation to build more complex understanding on top of it. It will implement very straightforward algorithms in the most naïve way possible, avoiding overly fancy tricks and cleverness. With these basics, I hope to help readers achieve fun physics simulations that can be used for a wide variety of games, and the starting point for the ambitious to build and move forward towards developing something scalable and robust to handle next generation content. I will do my best to break each concept down into simple understandable pieces, and will keep each article small and focused, providing a chance for you to ask questions and start a discussion in the comments. Discussion is really the best way to learn, in my experience, and will help other readers as well.
Lastly, if you’re a bit rusty on basic linear algebra and game math, you may want to read through my refresher series on my blog. I’m going to assume readers are familiar with the topics I covered.
The sample code
As I write the articles, I will be building a reference implementation, called SamplePhysics2D.
The code in the repository may be further ahead than the articles, as I will be developing each portion first, then borrowing code snippets from it to write the posts. Ideally, I expect readers to either type out or copy the code snippets from the articles themselves to build out the samples, rather than just downloading the code. I feel this will lead to a better understanding of the material, but ultimately it’s up to each reader and how they learn best.
My personal development environment is Visual Studio 2013 on Windows, and that is what the project in the repository is based on. However, other than the placeholder graphics code for debugging purposes, the code should be fairly portable. On the other hand, I make no explicit guarantees about portability. If you have problems, ask in the comments and I will try my best to assist.
The engine being developed throughout this tutorial is going to be very basic, meaning it won’t handle many robustness and scalability aspects that production AAA games would. I will, however, comment on those areas and point out where a more robust solution would be needed. In later posts, I’ll help fill in some of those details, but I want to keep the core of the tutorial easy to follow.
Since the focus is physics and collision detection, a very trivial renderer will be provided just to get basic visualization on the screen. This renderer is not meant to be demonstrative of anything I would recommend using, and it is not meant to be sample code anyone borrows. It’s been put together as quickly as possible to cover the basic needs that we have to visualize the physics logic, and is nothing fancier than that.
For the code itself, I will be using C++ for the sample code and project. I prefer the STL and some C++ 11 features, and use them throughout the code base. I realize not every reader will like this decision, but the code should be easy to port if one would like to make a different version.
Also, my focus is on readability and simplicity. I will comment heavily, and I will avoid overly clever tricks if it obfuscates the code. I will prefer simplicity over performance, because the point of this code is to convey information to readers, not run as fast as possible. Later, after the basic tutorial, I may dive deeper into particular tech topics. At that time, I will explain acceleration techniques and algorithms.
Lastly, I’m not much of a style stickler. I’ve tried to use as neutral coding style as possible, but I’m sure someone will still disagree with it. Sorry if it’s not your cup of tea, but try and follow along and ignore style if you can. If there’s anything truly atrocious you would like to see me change throughout the course of the series, feel free to mention it in the comments.
For reference, here’re the articles I have planned. I will enable each link to the article as they get written and posted:
- Basics of Newtonian dynamics (linear).
- Shapes, and basic collision detection.
- Additional shapes and more complex contact data.
- Torque and rotation.
- Jitter. Persistent manifolds for stability.
- Tangent forces and friction.