Open Source Xojo Box2D Port

I’m really excited to announce the immediate release of Xojo Physics. This is an open source feature-complete port of Box2D, arguably the most widely used 2D physics engine in the world.

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.

Features

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

Usage

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: Maths and 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.

Requirements

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.

Performance

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.

Port Progress

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.

Development background

I’ve been trying (on and off) since about 2019 to write a physics engine for Xojo. After multiple failed attempts at porting various physics engines written in C++ (such as the original Box2D, Bullet and Chipmunk) and Javascript ()Matter.js) I decided to write my own. After a lot of reading I released a simple engine called ImpulseEngine. Whilst I was pretty proud of this it was lacking in a number of areas. Firstly it was slow as it lacked a decent broadphase. Secondly I never managed to figure out how to implement joints which were very important to me.

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.

Video

I made a short YouTube video walking through the demo app that might be of interest to you.

YouTube Video of Xojo Physics

This might be of nostalgic interest to you @Alwaysbusy as I remember being very impressed with your ABPE port many years ago. I’m pleased to say this library is quite a bit more performant :slight_smile:

7 Likes

impressive as always, Garry !

1 Like

Java had a Box2D Port in 2014…after time I would say. But Xojo is mostly late with this stuffs.

Better late than never though. Let’s try to be positive shall we?

6 Likes

It is nice that it is available now but it is a sign for the Xojo World: technology changes reaching late.

Garry doesn’t work for Xojo.