Box2D.XNA : a great 2D physics engine

Box2D is a 2D physics engine created by Erin Catto. On Win/Mac, I used a very early version of Box2D (it was released nearly two years ago!). I just made a few specific changes for the game. Of course, as I told in a previous article, XNA games must be made in C# and Box2D is made in C++. I could find multiple open-source projects of ports of Box2D in C#. They are based on the last version of the C++ engine and a lot of changes occured since I developed Spring Up!.  It means that to use a C# port, I have to adapt a lot of my physics code. But this is a good thing for the following reasons:

  • I will have the new version of the engine : there are probably some debug and optimisations.
  • New features : can be used to improve the game
  • Lots of C# code already written, I won’t have to port it myself.

Among the two or three C# ports available, I decided to go with Box2D.XNA. This port is written by Brandon and Nathan Furtwangler and they are doing a very good job! They are very active: when I fixed them a bug, they put it back in the code base in less than a day and they port the changes of the C++ version very quickly.
Compared to the old C++ engine I was using previously, the new version has been greatly improved, here are some examples:

  • Dynamic/Static/Kinetic types are included on bodies, no need to play with infinite mass directly.
  • Collision filtering is now included in the API. I developed my own system in Spring Up! but throw it away in C#.
  • Motors are included. I also developed basic motors myself in C++.
  • More joints types: only one type was available.
  • Collision detection includes polygons, not just only circles and rectangles.

As I’m still working with the level editor, I started to include new features in Spring Up! Here is a simple video showing a new possibility made with Box2D.XNA (showing motors, complex shapes, joints and collision detection).

As you can see, the gameplay does not seem that interesting in this sample but it’s just made to test the dynamics for now. I will concentrate on level design later and I am sure I will face a lot of difficulties.

In conclusion, if you need 2D physics in your game, use Box2D, and if it’s for XNA, use the Box2D.XNA port!