Box2D for Android NDK

Hey guys,

I'm trying to build a JNI wrapper for using Box2D on Android (building with the NDK). My problem is that currently Android does not support the STL.
From what I've seen there's minimal use of STL in Box2D, the main usage being #include <memory> for the b2BlockAllocator.

I was wondering if anyone could point me in the right direction on how to go about removing this dependency? Or generally help me out in doing this.
I'm a bit of a C++ crumpet really, primarily a Java background, so I'm a bit lost as to where to start.

Thanks,
Danny.
Sorry, maybe I'm being silly and are confusing STL includes with something else. Just noticed on the Box2D feature list it mentions:
- Highly portable C++ with no use of STL containers

Shows how much I know 😉
tehdanish wrote:
Sorry, maybe I'm being silly and are confusing STL includes with something else. Just noticed on the Box2D feature list it mentions:
- Highly portable C++ with no use of STL containers

Shows how much I know 😉

<memory> is a STL header; it does not however specify containers (AFAIK). 🙂 I guess that's the only piece of STL Box2d is using.
mh114 wrote:
tehdanish wrote:
Sorry, maybe I'm being silly and are confusing STL includes with something else. Just noticed on the Box2D feature list it mentions:
- Highly portable C++ with no use of STL containers

Shows how much I know 😉

<memory> is a STL header; it does not however specify containers (AFAIK). 🙂 I guess that's the only piece of STL Box2d is using.


Ah containers. I think I'm making progress.. blindly.. replaced:
- <cstring> with "string.h"
- <climits> with "limits.h"
- <cstdlib> with "stdlib.h"
And I've added #include <new> to places where I was previously getting errors for lines like:
# b2.. thing = new (mem) b2..

Currently breaking on:
sources/abox2d/box2d-2.0.2/Collision/b2DynamicTree.cpp: In member function 'uint16 b2DynamicTree::AllocateNode()':
sources/abox2d/box2d-2.0.2/Collision/b2DynamicTree.cpp:64: error: invalid initialization of reference of type 'const b2Vec2&' from expression of type 'int'
sources/abox2d/box2d-2.0.2/Collision/../Common/b2Math.h:678: error: in passing argument 1 of 'b2Vec2 b2Min(const b2Vec2&, const b2Vec2&)'
make: *** [out/apps/abox2d/android-1.5-arm/objs/abox2d/box2d-2.0.2/Collision/b2DynamicTree.o] Error 1

That I'm looking into, knowing it the previous blind "fixes" are probably compiling but causing issues elsewhere.
Ooh, I got it compiling with only a few warnings about missing libraries..
The error above was around the b2Min method using templates.. or whatever the C++ fanciness word is for "Generics" 😉

template <typename T>
inline T b2Min(T a, T b)
{
return a < b ? a : b;
}

Added:
inline int32 b2Min(const int32& a, const int32& b) {
return a < b ? a : b;
}

And it used that with no issues.
Sounds like you are making progress.

I imagine once you get it built, you'll only need a minimal JNI interface. Most people probably only access a subset of the objects and methods. Off the top of my head:

World
Vec2
AABB
Body
BodyDef
Shape (and all of the derived subclasses)
and the ListenerFunctions.

Everything else can probably be left opaque.

Let us know how it works out.

Kyle
Will do.
So far I've been able to run the HelloWorld example and see the position changing in text output of the Android emulator. Now I'm hacking out some slop to draw some balls bouncing on Android.
If that all works out nicely I'll get some interfaces defined and post the NDK project 🙂

Not totally sure how I want to go about doing it yet, cleanly anyway. Androids role will be to draw the objects and handle user interaction.
Still need to decide on a way to pass the drawing information back to the android code. If you have any ideas let me know.
I'm thinking:
- Callbacks from C++ to Java handing it drawing information; or
- Some kind of object graph in android representing the bodies, each step the C++ code updates the state of these objects (i.e. pass in a context or something)
They all feel a little.. icky.. but yeah will try a few things and see what works best / feels nice.
Ok, I've got something running. I'm just adding 10 balls to a world and letting them drop.
Current setup:
- Android makes a JNI call to setup the world and step
- A local object for each ball, stored in an array
- Handler running every 10ms calling update()
- update() calls step via JNI, C++ makes a callback to the android code updating the balls information.

It's all pretty hardcoded and sloppy, but works and figured I'd post.
I'm pretty sure my timeStep settings are wrong, need to tweak them a bit. It feels like it's running slow (visually) but smooth for the most part.

The only fixes in the Box2D code were:
- adding #include <new> to some files that we're failing on new (mem) b2Object
- changing #includes <cstring> -> "string.h", <memory> -> "memory.h", <climits> -> "limits.h", <cstdlib> -> "stdlib.h"

Anyway here's the source if someone wants to play or point out places that could be changed for the better.

http://danish.boredprogrammers.com/abox2d.tar.bz2 - The NDK source code.
http://danish.boredprogrammers.com/chronobox.tar.bz2 - The android source code.
tehdanish wrote:
Code:
inline int32 b2Min(const int32& a, const int32& b) {
   return a < b ? a : b;
}

And it used that with no issues.


If int32 is just a typedef for signed integer (which I assume it is), you can just do
Code:
inline int32 b2Min(int32 a, int32 b) {
   return a < b ? a : b;
}
No need for references. 🙂
Ah cool, thanks! Not totally sure why I did that, probably something in the error it generated.
I actually left out the & when I upgraded to the latest box2d. But yeah good to know 🙂

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