Writing a physics engine (Part 1.1: Introduction)



Brief context

Several months ago, I became interested in training RL agents in physics-based environments (like training an ant to run forward, or motion imitation).

Of course, that meant that I needed to get ahold of a physics simulator. Not having much of a background in this space, I was immediately overwhelmed with just how many physics engines and solvers1 there are out there. There’s a whole handful of different engines that are used in graphics, robotics, biomechanics. To list a few, I’ve looked at:

  • MuJoCo. Widely popular for robotics and graphics.
  • XPBD. Popular for graphics.
  • AVBD. State of the art method for rigid body dynamics in graphics.
  • OpenSim/Simbody. Widely popular in biomechanics for studying human movement.
  • And some of the solvers implemented in Solver2D <- A really great resource!

Honestly, I didn’t really understand the difference between all of these solvers and engine. Something wasn’t really adding up to me: why are there so many physics engines out there? and what did each solver had to offer over the others? I decided to implement all of the above.

After doing so, it was a lot easier to understand what all these solvers are doing. I think that implementing your own engine is the fastest way to understand how they work.


What will this blog be about?

This will be an introduction to how to implement your own rigid-body physics engine, starting from first-principles and moving up to simulting things like the following:

Collisions between many bodies (left) and kuka allegro robot arm (right)

If you want to follow along, I won’t assume that you will have any background in rigid body physics, but having a couple pre-reqs/things in your back pocket will be helpful:

  • A basic understanding of linear algebra (e.g., linear operators, spatial transforms) and optimization (nonlinear optimization, iterative methods).
  • Some physics background: i.e., you understand \(F=ma\). If you’re comfortable with \(\frac{d}{dt} \left(\frac{\partial \mathcal L}{\partial \dot{q}}\right) - \frac{\partial \mathcal L}{\partial q} = 0\) that’s cool too.

Planned content

I’ll plan to structure the series of posts in something like the following order (I haven’t written anything yet so maybe this will also hold me accountable):

  1. Getting Started
    1. Introduction (you’re reading this now).
    2. What are degrees of freedom?
  2. Simple, free-body dynamics
    1. Rotations and orientations in 3D
    2. Computing accelerations with free-body dynamics (things are starting to move now!)
  3. Rigid body dynamics without articulated joints
    1. Rotations and orientations in 3D
    2. Mass matrix and contact Jacobian
    3. Collision resolution
  4. Articulated rigid body dynamics
    1. Generalized coordinates and velocities
    2. Redefining the mass matrix and contact Jacobian
  5. MuJoCo’s fast constraint solver
  6. Adaptive integration methods

Whether you’re interested in building simulators for robotics, implementing a physics engine into your video game, or just trying to better understand how these physics solvers work under the hood, hopefully these blog posts will be helpful!


  1. When I talk about “physics solvers,” I’ll be referring to the algorithms that handle the solving of constraints (such as those that arise from contacts and joint limits).