After a lot of work, I've managed to bring Erin Catto's amazing Box2D library to Xojo.
Physics is a rigid body, high performance physics engine written in 100% native Xojo code. It's a direct port of Dart's Forge2D and so tutorials for using both Box2D and Forge2D should be closely compatible with
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
Stable stacking with a linear time solver
Multiple joints types including revolute, distance and constant volume
Variable body density, friction and restitution
Everything you need to get started is in the GitHub 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
Physicsmodule 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 main project is saved in Xojo's "Project" format since this plays nicely with version control. I've also saved the project as a binary file so free and "lite" license users of Xojo can experiment with the module too.
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've added an iOS demo which includes an iOS version of
Physics.DebugMobileCanvas that renders 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'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.