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 http://code.google.com/p/libgdx/source/browse/trunk/gdx-tests/src/com/badlogic/gdx/tests/Box2DTest.java. 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 🙂