Complete Box2D JNI wrapper for Android

Hi,

i created a full JNI wrapper for the native Box2D implementation (latest stable release). It's integrated with my Android game development lib called libgdx. You can find it at http://code.google.com/p/libgdx/. The only thing i haven't implemented yet is the raycasting APIs as well as pre-solve. Everything else is completely integrated and works pretty well, a lot better than the JBox2D. There's no garbage collection issues at all and the JNI call overhead is so minimal that i wouldn't give it a second thought. Another nice benefit is that it also works on the desktop (libgdx is actually cross platform, Win/MacOs/Linux/Android, albeit it only works on 32-bit VMs at the moment as i'm a lazy bastard and didn't compile for 64-bit yet). Oh, and the thing is Apache License 2 so you can basically do with it whatever you want, e.g. cut out all Box2D related code if you don't want to use libgdx 🙂.

You can find additional information on my blog a http://www.badlogicgames.com

Hope that helps some of you guys

Ciao,
Mario
Wow 😀 That's some neat work! And a strange coincidence, because I was going to release the same kind of thing! I based mine off the stable 2.1.2 release.

Getting it to compile was pretty simple. Box2D uses a tidbit of the C++ STL here and there, which Android doesn't support, but those were easily replaced by C counterparts (<limits> for infinite floats, <algorithm> for one std::sort call, and one seemingly redundant <memory>). I had to add one <new> somewhere to let the compiler know about the existence of placement new.

Using the almighty and amazing SWIG, the wrapping was almost effortless. I didn't wrap everything yet, because I'm renaming everything to match Java conventions. b2World becomes World (in package org.box2d.android), b2_fixedBody becomes BodyType.FIXED_BODY, etcetera. As you can see, I'm going for an interface that integrates seamlessly with other Java code.

One thing I'm worried about is the callback interfaces like b2DebugDraw; SWIG directors refuse to compile on the NDK, so it will require some manual labour, probably similar to what you did.

I'm also adding some convenience methods to prevent unnecessary construction and destruction of b2Vec2 objects. For example, a World constructor that takes (float gravityX, float gravityY, bool doSleep). I didn't measure whether this matters for performance in the long run, but it does save typing in many cases 🙂 And in a crippled language without operator overloading, b2Vec2 isn't all that useful anyway.

Since you appear to have taken largely the same approach, I think there's little point in me polishing my code for release anymore. If anyone is interested anyway, let me know!
Wow, cheers dude.

Just picked this one up intergrated with cocos over at:
http://code.google.com/p/cocos2d-android-1/ (update/improvement to: code.google.com/p/cocos2d-android/ )

Basically, it runs faster with 200 cubes on screen than the original cocos/Jbox2d droid package
did with around 16.

Really good job!

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