2D Physics Engine

2D Physics Engine

-
completed
1
1.5 months
C++/SDL2/Tracy
November 2024
Physics Optimization C++ Low Level Engine 2D SAE

Context

This physics engine was programmed during a course module on physics, maths, optimisation, CPU architecture, Assembly and profiling at SAE Institut Geneva in the 2nd year of a bachelor’s degree in Games Programming.

The aim was to write the engine as an API that anyone could use, and to optimise it accordingly. The engine had to be capable of running a sample with 1000 colliders in trigger mode at a minimum of 60fps.

We wrote our own maths library as well as standard C++ classes such as smart pointers in order to use our custom allocators to profile the program’s memory management.

Showcase

Here is a short showcase video:

The main optimisations made to my engine are:

  • The creation of a broadphase with a Quadtree
  • Pre-allocate as much memory as possible whenever possible
  • Use stack memory as much as possible
  • Use data structures that store objects linearly in memory to speed up memory accesses.

Here is a comparison of the performance of the sample of 1000 trigger colliders before and after optimisations:

1000 circle colliders before optimizations.

1000 circle colliders after optimizations.

GIFs are at 30 fps, so it's hard to tell whether the right-hand version is actually fluid.

Let’s have a look at some frame images from Tracy:

One frame of the version without optimizations.

One frame with the final version with optimizations.

Let’s take a look at the time saved when executing the world’s “Update” function:

World UpdateMean (ms)Median (ms)Std Dev (ms)Observations
Without optimizations792.32828.95157.1616
With optimizations2.642.440.6221291
Table: Comparison of World Update Time with and without Optimizations
Mean Difference (ms)Standard Error (ms)Margin of Error (ms)Lower Limit (ms)Greater Limit (ms)
808.3223.2147.71760.61856.03
Table: Difference of Means and Confidence Interval (95% Confidence Level)

With all the changes made to my engine I saved between 760ms and 856ms.

Features

  • Creation of bodies.
    • Attributes:
      • Position
      • Velocity
      • Mass
    • Dynamic bodies:
      • Forces can be applied to them.
      • Physical collision calculations.
    • Kinematic bodies
      • Not impacted by forces.
      • No physical collisions.
    • Static bodies
      • Don’t move.
      • Physical collision calculations.
  • Creation of collider:
    • Attributes
      • Shape
        • Circle
        • Rectangle
        • Convex Polygon (only work for trigger for now)
    • Restitution
    • Trigger mode
  • Broad Phase with Quadtree

What did I learn ?

  • Basics of Calculus
  • Basics of Linear Algebra
  • Basics of Physics
  • Basics of Assembly language
  • Choose and use the right C++ data structures in a given context
  • Low level C++ API programming
  • Template programming
  • Writing custom allocators
  • Profiling code and memory management
  • Optimisation tricks
    • Memory layout
    • Cache hit
    • Quadtree
  • How a modern CPU works
  • Performing statistical tests

Related Blog Posts

© 2025 Pachoud Olivier