ImpulseEngine - Open Source 2D Physics Engine

Post number two of the site and another open source giveaway :smile: :tada:

I’m pleased to announce the release of ImpulseEngine, a rigid body 2D physics engine written in Xojo. It’s a port of Randy Gaul’s C++ engine of the same name but tweaked to be more “Xojo-like” in its API.

What is it?

ImpulseEngine is a 2D physics simulation and collision detection engine. It could form part of a simple 2D game by simulating the forces acting on shapes added to the simulation and detecting when those shapes collide with each other. It’s similar in concept to projects such as Box2D, Chipmunk and Farseer. None of those are available to Xojo developers however.

Features

  • Convex polygons and circles
  • Real time hull creation from any set of points
  • Circle and polygon collision detection using the Separated Axis Theorem (SAT)
  • Impulse-based collision resolution
  • PureAPI 2.0 compliantXojo code and runs on macOS, Windows and Linux

Why did you create this?

I love gaming and creating games and I’m actively working on projects to simplify game creation for Xojo developers. One of the components that has been missing to Xojo developers is a 2D physics engine. As a matter of fact, I had tried on a number of occasions over the last few years to create a physics engine but always stumbled with the maths. When I came across Randy Gaul’s simplified engine and read his excellent tutorials I decided to port his Impulse Engine.

It’s worth mentioning that there is another 2D physics engine available to Xojo developers. It’s called ABPE (“Always Busy’s Physics Engine”). It was created by Alain Bailleul who used to be very active in the Xojo community until moving to B4X a few years ago. ABPE is a port of a defunct ActionScript physics engine called APE. The issue I had with ABPE was three fold:

  1. It was no longer supported
  2. It doesn’t compile on recent versions of Xojo
  3. It has no documentation. Nor does the original APE engine have any documentation

Also I wanted to write my own engine to better understand how they work.

Example usage

Getting started is a simple as adding the ImpulseEngine module to your project. The project includes a simple demo application illustrating how to create bodies and add them to the simulation. I’ve tried to keep the engine’s API simple and have thoroughly documented the code.

// I'm assuming your viewport (e.g: a canvas control) is 640 x 480 pixels.

Var dt As Double = 1/60 // 60 FPS.
Var iterations As Integer = 5

// Create a new world.
Var w As New World(dt, iterations)

// Add a thin box to act as the ground.
Var ground As Body = w.AddBox(320, 471, 639, 8)
ground.IsStatic = True

// Add a circle to the simulation.
Var circ As Body = w.AddCircle(320, 0, 15)

// Add a polygon with some spin.
Var poly As Body = MyWorld.AddPolygon(150, 50, 0, 0, 30, -50, 60, -20, 75, 20, 40, 40)
poly.AngularVelocity = 0.55

// Call World.Update every `dt` seconds.
// Assume we have a Timer elsewhere with an intervale of 1/fps that
// calls `w.Update` and then draws every body in the World in a canavs.
// See the demo app to see how this can be done.

Limitations and future development

ImpulseEngine is a great introduction to 2D physics simulation with Xojo but it has a couple of issues which (unless your game is simple) probably limit its use:

  • Inefficient broadphase, i.e: the more bodies you add to the simulation, the lower the performance
  • No support for joints
  • No composite bodies
  • No concave polygons

Fear not though. I’m actively addressing all of these issues in a current project which is a more feature-rich and complex physics engine that should be able to handle hundreds of simultaneous physics bodies, supports multiple joints and offers composite bodies. I will of course post more about this at a later date.

Where can I get it?

Like all of my open source projects, ImpulseEngine has its own GitHub repository. It has the MIT License and is therefore free for you to use in all projects, even commercial ones.