Show Xojo: Open Source 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

21 Likes

This is awesome, Garry. It makes this old line-of-business dev want to take up gaming, lol.

And … it is definitely another demonstration that Xojo is very flexible and performant.

2 Likes

Thanks Bob. I’m pretty proud of it. As I mentioned
I’m also quite pleasantly surprised by the performance even without beginning my optimisation pass yet.

As indeed you should be proud. I wish this had been around to play with when I was tutoring my grandson in programming several years back. Predictably he wanted to learn game programming. We did some simple 2D stuff with JS and Python frameworks and TBH it was as much a learning experience for me as him, since I had zero experience with games. So … I have just a little bit of appreciation for what you’ve accomplished here. Kudos.

1 Like

Garry, this is extremely impressive work. I’m excited to play with this over the coming days. Thanks for sharing with us all.

2 Likes

It’s funny you should mention kids as my daughter expressing an interest in programming was a driving factor behind this. Yes I know there are umpteen “better” solutions for making games than Xojo but I want to try to change that and make it easier to make games with Xojo.

Hi Garry, Wonderful stuff. Kudos to you, sir.

A very minor thing, the link to Forge2D in the post and the Github README is not working.

https://github.com/flame-engine/forge2d%5D, shoud presumably be:

https://github.com/flame-engine/forge2d

Regards, Charlie

1 Like

Thanks Charlie. Fixed.

I’ve just added a binary project format save file to the repo so people with the free or lite version of Xojo can also use the module.

Even a free license can load a text project, they just can’t save them.

2 Likes

I’ve added prismatic joints.

These are cool because they can be used as moving platforms or elevators.

Here’s a GIF showing such a joint:

Prismatic Joint Demo

The demo app has been updated to include the new joint.

3 Likes

Now runs on iOS!

I’ve just finished adding a demo for the physics engine running on iOS to the repo.

I’ve never made an iOS app with Xojo before but I managed to port the desktop version (the same features) in about an hour so I’m pretty happy with that.

Here’s are a few of GIFs of the demo in action (the GIFs stutter a little but the animation is buttery smooth in the simulator for me):

click to add
prismatic
revolute

10 Likes

Added particle support

The Xojo Physics module now contains a full port of Google’s LiquidFun fluid and particle simulation.

A new demo has been added to both the desktop and iOS projects showing basic particle effects:

ezgif.com-gif-maker

9 Likes

Impressive stuff. Now need to figure out what to do with it.

4 Likes

we can learn alot about physics :slight_smile:
this circles remember me.
https://youtu.be/lBCVKcvhcnQ

Raycasting demo

Raycasting is the ability of a physics engine to cast a ray (straight line) out from a point and detect bodies in its path. I’ve now added a demo to the repo showing this. In the demo, the bodies even reflect the ray as if it was light:

ezgif.com-gif-maker

ezgif.com-gif-maker-2

The demo uses a fairly inefficient method to illustrate recasting but the physics engine supports very efficient raycasting through the use of callbacks which are easy enough to implement.

As always, the code is in the GitHub repo.

8 Likes

Added support for a WeldJoint

I just added Box2D’s weld joint to the Xojo Physics module. This essentially “glues” two bodies together:

weld-desktop

weld-ios

8 Likes

Garry this is great work as always from you sir. Thank you for lending this work to our Community!

5 Likes