Box2D on Android via JNI

Hi there,

i thought i'd post a link here for future reference. I wrote a complete JNI bridge to the latest Box2D release for Android. All relevant classes are wrapped via lightweight Java counter parts, all listeners except the destroy listener are exposed, all joints are support and so on and so forth. The only things missing are some of the body->GetNextBody() methods which would just not work out very well in Java (well, it's possible but not very wise performance wise). The focus of this port was on getting a zero dynamic allocation version of Box2D directly accessible in Java and i think i succeeded in that. You can find more info about it at http://apistudios.com/hosted/marzec/badlogic/wordpress/?p=586.

I altered the source only marginally, some of the include definitions needed reworking (at least i couldn't get it to work any other way) and the dependence on the stl algorithm package for sorting has been replaced by a simple qsort. Other than that it's a vanilla Box2D version that's used. Also note that this does work on the desktop as well (libgdx is "crossplatform" ).

I consider making a seperate project out of this so it is not dependant on libgdx. For the time being i keep it there though as it eases my pains of managing multiple projects a little.

Hope that helps out a couple of you guys a little,
Mario (badlogic games)
Hey there.. nice work on the bridge.

Just curious, how is it dealing with things like collision callbacks? I have a similar bridging between Android and Box2D but I have deliberately made mine very "coarse" so that I don't need to call back through JNI every time there is something in box2d of "significance".

For example:

Consider two bodies that collide in box2d. Let's say you need to work out whether you care about the collision, and then whether to handle it by playing a sound (or something). In "pure" box2d land you would work all this out in your b2ContactListener, but if the JNI bridge is fine-grained then you would presumably be calling back to Java to resolve the collision? This probably makes sense from an architecture perspective, but as the number of bodies increases I imagine you'll be flooded with [expensive] JNI callbacks.

In my case I have most of my logic relating to bodies maintained in box2d, then once all collisions are resolved and the step is complete I do a single callback to java with only the data I care about. This is particularly important if you want to do things like disable contacts during contact resolution in box2d (eg: viewtopic.php?f=3&t=4985&start=0&hilit=one+sided+platform).

Naturally this is pretty custom, and would be difficult (impossible?) to create as a generic library as you have done, but I'm curious to know whether this performance hit is significant or not.

Maybe it's a case of creating a set of pre-defined object types with fixed behavior in box2d. So for example you could create a "platform" object in the Java API which corresponded to a set of defined behaviors in box2d land (one-sided, moving etc). You then just send a "platform configuration" from java to box2d. This is obviously out of scope for your library.. just thinking out loud.

Anyway, great work on the library

Original thread: https://box2d.org/forum/viewtopic.php?t=4970