Box2D on Android with libgdx

Yes, that’s what i’ve been up to in the past couple of days. I wrote a complete JNI wrapper for Box2D which is now part of libgdx. It features all the things the latest Box2D build offers, from various listeners to all joint and shape types and so on and so forth. This wrapper does not fuck with the garbage collector as JBox2D does and performs reasonably well (better than JBox2D but not 100% as good as a pure C++ implementation for obvious reasons). Here’s a little pic of a little demo app i wrote with the wrapper which shows how to setup a world, get the current position and rotation of all bodies, use the mouse/touchscreen to manipulate the world and so on.

You can find the source code for this example at I plan on writting a small libgdx tutorial in the next few days that explains the classes needed for the Box2DTest example and will then write up a small Box2D tutorial specifically aimed at libgdx.

What performance can you expect from this wrapper? 50 dynamic bodies take around 2ms to simulate (8 position iterations, 3 velocity iterations) on my Nexus one. That is more than enough for most applications. Fetching the data for bodies and contacts performs equally well and does not introduce to much of a slow down. I’m happy with that outcome and will use it in one of our upcoming games. Camel games watch out!

What’s not implemented/differs in the wrapper compared to the original Box2D?

  • Raycasting on the world/fixtures/shapes is not supported at the moment. I might add that if the need arises
  • The destroy callback is not available at the moment as there’s simply no need for it
  • Some functions like body->GetNextBody() are not implemented for performance reasons. Iterating over contacts/joints/bodies this way would fuck with performance

The rest is in there, fully documented (yes i copy & pasted all of the original source comments for your convenience :)). I have not yet tried out all of the features so there might still be bugs in there. However, the portions of the API used in the demo above all work 100% correct. I will release a new libgdx version this weekend including this little sucker.

If you want to try it out on your device use this link:

This is an apk containing all the tests i wrote for libgdx so far. Those of you with devices such as the Droid or Nexus One can also try out the OpenGL ES 2.0 tests which use shaders. They are not really fancy so don’t expect any eyecandy :). To try out the Box2DTest scroll down to the bottom of the list and enjoy. Note that the rendering performance is lousy as i didn’t try to optimize it. It currently uses the ImmediateModeRenderer which emulates the desktop OpenGL immediate mode rendering functionality. It’s only meant for debugging and testing purposes don’t use it in production code, it’s slow.

Quick update: i tried the demo on my HTC Hero with the results reported by Moritz. However, i also tried out space physics and i has the same behaviour, ~10fps with 15 drawn lines on screen. Additionally it slows down badly when touching the screen, but that’s not space physics fault 🙂

20 thoughts on “Box2D on Android with libgdx

  1. When I tried to scan the link, my phone said it was a contact to add to my address book and not a file to download. The problem may be with my phone. I have a Droid Eris using 1.5.

  2. After trying out the demo i have to report: i get 8-9 fps on a HTC Magic and most of the boxes fall into each other. a few (like 8-10) float on these boxes but the corners of them sink into the lower boxes.

    Good work though. 🙂

  3. that’s due to the rendering which is sluggish. In the bottom right you see how much the update of the physical world actually takes. i suspect it to be in the range of 4-8ms on the magic

  4. Could you tell me which did not work? Some of them are not meant to work on other devices due to files they expect on the sd-card.

  5. I’m sorry did another run and it seems that some of the test did take some time to load, But Sound test, input test, does not seem to start…

    And I think that the pong is broken too, I can’t win =P

  6. Heh ok. The input test actually only outputs stuff to logcat, i can’t remember what sound test did.

    The pong AI is a bit to perfect i guess. There’s also no winning state :p

    Thanks for getting back, i was worried that i had to buy myself a Desire 🙂

  7. @a There’s a port of Box2D for the Nintendo DS which uses 32-bit Q16 fixed point. The problem is that it is highly unstable and imposes some restrictions on your physical world. Chipmunk does not have fixed point support nor does the dev plan on adding it. If you happen to find something better please report back, i’d love to have a fixed point physics engine!

    I still don’t get why they didn’t make it a requirement that all Android devices must have an FPU. Fail…

  8. oi, that’s bad. That’s what you get for coding marathons. Thanks a bunch for fixing it, i’ll commit the change asap!

  9. Hi,

    thanks, this looks great – i have downloaded the source code and trying to run the box2dTest on the emulator, however, the screen just goes blank and then after a few seconds it returns to the menu options.

    I have run the box2d Collections Test and that drops two objects from

    There dont seem to be any obvious errors in the log -any ideas?

  10. Hi,

    i assume you tested the apk linked to in this post. It could be that i never tested it on the emulator and there might have been bugs. Given that this blog post is pretty old and that that was the first release of the box2d wrapper i’d suggest trying out the latest libgdx release. I’m pretty sure it will work 🙂

Leave a Reply

Your email address will not be published.