Physics is a rigid body, high performance physics engine written in 100% native Xojo code that is completely self-contained in a single Xojo module.
Box2D is pretty well known but here are some of the features of the engine:
- Continuous collision detection with time of impact solver
- Convex polygons and circles
- Multiple shapes per body
- Dynamic tree broadphase
- Contact filtering
- Stable stacking with a linear time solver
- Multiple joints types including revolute, distance and constant volume
- Joint motors
- Variable body density, friction and restitution
- Sleep management
Everything you need to get started is in the repository. If you download and run the project you’ll be presented with a demo app that showcases a few of the engine’s features.
The engine is entirely contained within the
Physics module to make it easy to move between projects. There are two other modules in the project that the
Physics module depends on:
VMaths. You’ll need to copy these to any projects you want to use the engine in as well. The
Maths module contains some maths-specific methods and constants and the
VMaths module contains classes related to vector maths.
Included is the
Physics.DebugCanvas class which is a subclass of
DesktopCanvas. This is a drop-in control that will render the physics simulation. The
DebugCanvas is not intended to be used for a game (it could be better optimised) but it’s quite performant nonetheless.
The engine was developed on a 16" MacBook Pro with an M1 Pro using Xojo 2022 Release 2. The code is entirely API 2.0. I’ve not tested it on earlier Xojo releases.
Since the engine is 100% Xojo code it will run on all desktop platforms and iOS. I’m not much of an iOS developer but I do plan on adding an iOS version of the included
Physics.DebugCanvas that would render the simulation on iOS.
I’ve been pleasantly surprised by the performance of the demo app. In the built demo app, I’m consistently able to get 60 FPS on my M1 Pro Macbook Pro. Very surprisingly I found the performance was even better on my Windows 11 gaming rig where I was able to get 60 FPS with > 200 bodies on screen at once! This was not case on a Windows 11 laptop with an integrated GPU. Your mileage may vary.
Please note that performance will be much slower when running the demo in the debugger as a lot of additional checks are performed by the engine when in debug mode.
I’m about 95% of the way through porting the library. The engine is fully operational. All that is missing are a few joint types that I intend to port as well.
I’ve recently been dabbling in Flutter and Dart and came across the Flame game engine which uses a Dart-port of Box2D called Forge2D. Dart is similar enough in its object model to allow me to port it to Xojo.
I made a short YouTube video walking through the demo app that might be of interest to you.