A very basic 2D physics engine written in the Zig programming language.
The goal is to provide a very basic physics solution for simple 2D games, eliminating the need to reimplement basic collision detection from scratch for every project. The main focus is on essential 2D collision detection and resolution without the complexity of a full physics simulation.
This project also serves as a learning exercise for me to understand the fundamentals of physics simulation.
The engine is not intended to solve complex collision scenarios. For a full fledged 2D physics engine, please use something like box2d.
- (Currently) only supports discrete collision detection (DCD).
- Collision detection only supports axis aligned bodies (no rotated bodies).
- Collision detection only supports rectangle and circle shapes.
The list of features I'm planning to implement:
- AABB vs AABB collision detection (discrete)
- Collision response for dynamic body vs static body collisions
- Fully inelastic collision response for dynamic body vs dynamic body collisions
- Circle vs AABB collision detection (discrete)
- Circle vs circle collision detection (discrete)
- Broad phase collision detection (via spatial partitioning, quadtrees, etc.)
- Spatial hash grid
- Quadtree
- Continuous collision detection for fast moving bodies
All examples use a fixed timestep of 1/60 seconds for physics simulation. This will provide consistent physics across frame rates and serves as sort of a "best practice" on how to use the engine.
zig build run-demoThis example demonstrates how multiple dynamic bodies fall onto a circle collider, bounce off and fall onto the ground.
It mainly serves as a reliable scenario for performance testing, as it does not allow for player interaction and should always produce the same output (i.e. should be deterministic).
In this example, it is obvious, how stacking multiple dynamic bodies on top of each other is unstable and causes jitting.
zig build run-platformerThis example demonstrates the use of gravity and a simple player controller for side-scrolling platformers.
Damping is used with two different values based on the player being on the ground or in the air. This ensures the player will fall naturally due to gravity, but will gradually decelerate while being on the ground.
The ContactListener's on_contact callback is used to destroy bodies to simulate coins being collected.
zig build run-topdownThis example demonstrates how to use the engine for top-down games (like RPG's, dungeon crawlers, survivor-likes, etc.), that usually don't have gravity involved, since it's sort of a "top-down" view.
For the player a circle shape is used, which provides smooth sliding past corners.

