A hand crafted 2D game engine

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.

Hello World

This is the very first entry in what I hope will become a long running development log for a project that I have (in one form or another) been working on for several years. This blog will chronicle the development of Dojo, a new 2D game engine / game-building app written in Xojo.

I love making dev tools and have always wanted to make a game. I'm fully aware that there are many, many game making apps and feature-rich game engines already in existence but I'm interested in making a game with Xojo.

Over the years I've prototyped several components required by a game engine in Xojo. Many of these were small simple side projects created out of boredom or for personal enjoyment and were never released to the public. A few (for example my rigid body physics engine - ImpulseEngine) are now open source. What I haven't done in the past is actually take these components and weave them into something useful for others.

I have two young daughters and both have shown some interest in programming. Game development is and always has been a great gateway for people to get into programming. The trouble is, I don't feel that the current game engines and game-making apps tick the boxes that I would like in order to get my kids involved. Most game-making apps are either too simplistic (e.g: Scratch) or too bloated (e.g: GameMaker Studio). Most game engines are too complicated to start using as a novice (e.g: Unity). I want to create an app that sits comfortably in the middle. An app that is easy to use but actually allows you to build a proper game that can be distrbuted and played by others. I also want the users of the app to be able to build the game they want to build. To do that, they need access proper code, not some drag and drop building block system like Scratch has. The code however needs to be accessible, not C++ or C#.

Ultimately, I hope Dojo will be an all-in-one game development IDE. My plan is that it will be a mixture of a visual editor for game creation but one that exposes code to control the elements of the game. At this stage, I'm thinking that the games will be written in Xojoscript and that the game will run in an optimised interpreter. I haven't ruled out transpiling the game into an established lanuage like javascript to be run inside an embedded HTML canvas. The third option I'm toying with is to use a custom language designed purely for Dojo. Afterall, it's not like I haven't already created a programming language before... 😊

So, what's the current status of Dojo? Currently there is no Dojo app. I'm presently porting a physics engine to Xojo that I anticipate will be the physics engine used by Dojo. The next blog post will talk all about it.