Box2D physics implementation and Python bindings
date: 10/2/2026

Anvil Engine now has a proper 2D physics system using Box2D.
This has been something I have wanted to get working for a while, especially because the Python scripting side of the engine was starting to become much more usable. Physics was one of the bigger missing pieces.
Along with the Box2D implementation, I also added the Python bindings needed to actually interact with the new physics system from scripts.
The Implementation
Section titled “The Implementation”Under AnvilEngine/src, there is now a new Physics folder that contains most of the Box2D-specific code.
The goal with this was to keep as much of Box2D as possible behind Anvil’s own systems instead of letting Box2D types leak everywhere throughout the engine.
Physics World 2D
Section titled “Physics World 2D”PhysicsWorld2D is responsible for managing the Box2D world.
It creates the world, steps the simulation, and acts as the main interface between the engine and Box2D.
The basic physics update now looks roughly like this:
Scene Update ↓PhysicsWorld2D ↓Box2D World Step ↓Collision EventsAfter each physics step, Anvil polls Box2D for any collision or sensor events that happened during that frame.
Rigid Bodies and Colliders
Section titled “Rigid Bodies and Colliders”
The existing physics components now create their Box2D counterparts.
A RigidBody2D creates the actual Box2D body, while collider components create shapes that are attached to that body.
One important part of this was figuring out how to get from a Box2D collision back to an Anvil entity.
Box2D gives me shape IDs when a collision happens, but the scripting system obviously does not care about b2ShapeId or b2BodyId.
So the current path looks like this:
Shape ↓Body ↓Body User Data ↓entt::entityWhen the Box2D body is created, I store the entity in the body’s user data.
That means when two shapes collide, Anvil can resolve both shapes back into their original entities and then pass those entities into the scripting system.
Collision Events
Section titled “Collision Events”There is also now a CollisionListener that handles Box2D events.
It currently polls for:
Contact BeginContact EndSensor BeginSensor EndThese events are converted into an Anvil PhysicsCollisionEvent2D before being dispatched.
The listener resolves both Box2D shapes back into entities and checks whether either entity has a script attached.
If it does, the event gets forwarded to PythonScriptEngine.
So the whole thing ends up looking something like:
Box2D Collision ↓CollisionListener ↓Resolve Shapes to Entities ↓PythonScriptEngine ↓Entity ScriptGetting sensor events working gave me a little trouble.
There were two things I ended up finding.
First, the entity needs an active rigid body for the current system to work correctly.
Second, I originally had:
shapeDef.enableSensorEvents = collider.IsSensor;which seemed logical at first.
The problem is that the other shape entering the sensor also needs sensor events enabled.
For now I changed it to:
shapeDef.enableSensorEvents = true;for every physics shape.
Whether the shape actually behaves as a sensor is still controlled separately by:
shapeDef.isSensorThat fixed the sensor event issue.
Python Bindings
Section titled “Python Bindings”The other major part of this update was exposing the new physics system to Python.
The scripting system already had access to entities and components, so the next step was making the physics components useful from scripts.
Rigid bodies can now be accessed through the Python entity wrapper and can perform actions such as applying forces and impulses.
The bigger addition, though, is collision callbacks.
Python scripts can now define an on_collision function and receive physics events directly.
Something like:
def on_collision(self, event): print("Collision")The event contains the other entity involved in the collision as well as information about what kind of event occurred.
That means scripts can tell the difference between things like a normal collision and entering a sensor.
This makes it possible to start writing actual gameplay logic around the physics system without having to touch C++ every time.
For example, things like:
PickupsTrigger AreasDamage ZonesProjectilesCheckpointsEnemy Detectioncan now mostly be handled through Python scripts.
A player controller script can now have something along the lines of this
def on_collision(self, event: PhysicsCollisionEvent2D): # Get the name of the entity that collided into this one name = event.entity.get_component(TagComponent).value
# Handle a contact/hit event if event.kind == CollisionEventKind2D.CONTACT: if event.phase == CollisionEventPhase2D.BEGIN: anvil.log(f"Begin Contact with {name}") if event.phase == CollisionEventPhase2D.END: anvil.log(f"End Contact with {name}")
# Handle a sensor event if event.kind == CollisionEventKind2D.SENSOR: if event.phase == CollisionEventPhase2D.BEGIN: anvil.log(f"Begin Contact with {name}") if event.phase == CollisionEventPhase2D.END: anvil.log(f"End Contact with {name}")Where It Is Now
Section titled “Where It Is Now”The main physics pipeline is finally working.
Anvil Entity ↓Physics Components ↓Box2D ↓Collision / Sensor Event ↓Entity Resolution ↓Python ScriptThere is still plenty left to improve.
I want to clean up how physics components depend on each other, add more collider types, improve the Python physics API, and make the whole system easier to work with from the editor.
But the important part is that the foundation is there now.
Anvil can simulate physics, detect collisions and sensors, resolve those collisions back into engine entities, and send those events directly into Python gameplay scripts.
That is a pretty big step toward making the scripting side of Anvil actually useful for building games.