Physics Engines

An app like Dojo has so many inter-related components that it's hard to know where to begin when sitting down to create it. Arguably there is no correct answer so I figured I would start with a component that both interests me and that I have already some experience with. That component is the physics engine.

I have tried (and to some degree succeeded, see later) to both port or create physics engines using Xojo on many occasions over a number of years.

Arguably the most popular 2D physics engine in use today is Erin Catto's Box2D C++ library. It's what powers well known game engines such as GameMaker Studio, Unity and the Corona SDK. It has inspired derivatives such as Chipmunk and is used within Apple's SpriteKit. Attempts have been made in the past to port this library to Xojo by others (for instance, here) but have been unsuccessful. I myself attempted to port it years ago but I find C++ difficult to follow and the paradigms it uses are not always translatable to Xojo. Also, the documentation within the source code is pretty cryptic.

After trying to port Box2D I subsequently tried to port the Chipmunk library but as this was in C it was even more tricky for me (a die hard OOP fan) to get my head around.

These engines are not well documented internally so I set out to find one that was and stumbled upon Matter.js. This is a beautiful and cleanly commented engine but it's written in javascript. Since javascript is a functional / prototypical language and is not (strictly speaking) object oriented it just didn't make for a good fit for Xojo. Another port attempt abandoned...

Finally I decided to write an engine from scratch. After doing a lot of reading around the basic maths required and the general approach to simulation I hunted for any tutorials (with code) that might be of use. I came across a brilliant collection of tutorials by Randy Gaul written in C++. These culminated in his simple open source ImpulseEngine. This was a godsend in terms of helping me to wrap my head around the implementation details of an actual working physics engine. More importantly, I was actually able to successfully port it to Xojo. You can get the code here and see it in action below:

Unfortunately, I can't use ImpulseEngine as the 2D physics engine for Dojo for two reasons:

  1. It's slow.
  2. It doesn't support joints.

It's slow because it was designed for learning and it's not optimised. For example, it has no concept of a broadphase and narrowphase in collision detection. No support for joints won't do as that prohibits any type of game that uses more than a single shape for a body in the simulation.

My success in getting a physics engine up and running in Xojo spirited me on to continue searching for an engine that I could port. One (very happy) day I stumbled upon dyn4j. Dyn4j is a 100% open source 2D physics engine written in Java. It's principally written by William Bittle. Not only is it comparable in both features and performance to Box2D but it's the most lovingly commented library I have ever seen. Every method and property in the source code is dilligently documented and, being Java, it's 100% object-oriented which makes it a perfect fit to port to Xojo.

So what's the progress of the port then? Well, I've been working on it for a while now (sporadically at first but recently more intensively) and I've currently almost finished porting the geometry package. William has written exhaustive tests which I'm also porting and so far the engine is passing all tests (apart from one which I'm trying to debug as we speak!). There is still a lot of work to do but I strongly think that I will be able to successfully port this to Xojo. I've christened this port PhysicsKit and the code is open source on GitHub. Once finished, this will form the basis of Dojo's physics engine.