Integrating Box2D in an Android project

Ray To

How do you make the reference to Box2D.h in your code?

I did everything like you, but I’ve got this error: “jni/…/…/Classes/HelloWorldScene.h:13:19: error: Box2D.h: No such file or directory”

Box2D.h stays outsite the Box2D include directories directory… so this usually means c include directory of Box2D.h is not set, maybe you have forgotten to add the include for the main box2d directory:

Like this:

$(LOCAL_PATH)/…/…/…/libs/Box2D \

hmm in my case I added:

#include Box2D/Box2D.h

in my helloworldscene.cpp file

in your case I’m guessing you have

#include Box2D.h

so instead of

$(LOCAL_PATH)/…/…/…/
I’d go for

$(LOCAL_PATH)/…/…/…/Box2D
btw to my earlier post I made a mistake… I pasted the contents of the android.mk file inside the jni folder

the one inside my classes folder looks like this after I modified it:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := game_logic

LOCAL_SRC_FILES := AppDelegate.cpp HelloWorldScene.cpp

LOCAL_C_INCLUDES := $(LOCAL_PATH)/…/…/cocos2dx $(LOCAL_PATH)/…/…/ $(LOCAL_PATH)/…/…/Box2D $(LOCAL_PATH)/…/…/cocos2dx/platform $(LOCAL_PATH)/…/…/cocos2dx/include $(LOCAL_PATH)/…/…/CocosDenshion/include $(LOCAL_PATH)/…/…/cocos2dx/lua_support

LOCAL_LDLIBS := ~~L$/…/android/libs/$) ~~lcocos2d lbox2dlcocosdenshion ~~L$/…/…/cocos2dx/platform/third_party/android/libraries/$)~~lcurl

include $(BUILD_SHARED_LIBRARY)

with all the changes I said before but with

$(LOCAL_PATH)/…/…/Box2D
since it might benefit in your case

It wasn’t enough.
I’m still getting these errors:
jni/…/…/Classes/HelloWorldScene.cpp: In constructor ‘HelloWorld::HelloWorld()’:
jni/…/…/Classes/HelloWorldScene.cpp:38: error: no matching function for call to ‘b2World::b2World(b2Vec2&, bool&)’
jni/…/…/Classes/…/…/Box2D/Dynamics/b2World.h:46: note: candidates are: b2World::b2World(const b2Vec2&)
jni/…/…/Classes/…/…/Box2D/Dynamics/b2World.h:42: note: b2World::b2World(const b2World&)
jni/…/…/Classes/HelloWorldScene.cpp:68: error: ‘class b2PolygonShape’ has no member named ‘SetAsEdge’
jni/…/…/Classes/HelloWorldScene.cpp:72: error: ‘class b2PolygonShape’ has no member named ‘SetAsEdge’
jni/…/…/Classes/HelloWorldScene.cpp:76: error: ‘class b2PolygonShape’ has no member named ‘SetAsEdge’
jni/…/…/Classes/HelloWorldScene.cpp:80: error: ‘class b2PolygonShape’ has no member named ‘SetAsEdge’
make: ** [obj/local/armeabi/objs-debug/game_logic/HelloWorldScene.o] Erro 1

PS: HelloWorldScene.cpp and HelloWorldScene.h are that iphone helloworld, where there are boxes (A,B,C,D) falling on the screen

Loot at my files:

android/jni/Android.mk

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

subdirs := $(addprefix $(LOCAL_PATH)/…/…/…/,$(addsuffix /Android.mk, Box2D cocos2dx CocosDenshion/android ))
subdirs += $(LOCAL_PATH)/…/…/Classes/Android.mk $(LOCAL_PATH)/helloworld/Android.mk

include $(subdirs)

android/jni/Application.mk

APP_STL := stlport_static
APP_MODULES := cocos2d cocosdenshion box2d game_logic game

android/jni/helloworld/Android.mk

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := game

LOCAL_SRC_FILES := main.cpp

LOCAL_C_INCLUDES := $(LOCAL_PATH)/…/…/…/…/cocos2dx $(LOCAL_PATH)/…/…/…/…/cocos2dx/platform $(LOCAL_PATH)/…/…/…/…/cocos2dx/include $(LOCAL_PATH)/…/…/…/Box2D $(LOCAL_PATH)/…/…/…/Classes

LOCAL_LDLIBS := ~~L$/…/…/libs/$) ~~lGLESv1_CM ~~lbox2d ~~lcocos2d lloglgame_logic

include $(BUILD_SHARED_LIBRARY)

Classes/Android.mk

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := game_logic

LOCAL_SRC_FILES := AppDelegate.cpp HelloWorldScene.cpp

LOCAL_C_INCLUDES := $(LOCAL_PATH)/…/…/cocos2dx $(LOCAL_PATH)/…/…/ $(LOCAL_PATH)/…/…/Box2D $(LOCAL_PATH)/…/…/cocos2dx/platform $(LOCAL_PATH)/…/…/cocos2dx/include $(LOCAL_PATH)/…/…/CocosDenshion/include $(LOCAL_PATH)/…/…/cocos2dx/lua_support

LOCAL_LDLIBS := ~~L$/…/android/libs/$) ~~lcocos2d lbox2dlcocosdenshion ~~L$/…/…/cocos2dx/platform/third_party/android/libraries/$)~~lcurl

include $(BUILD_SHARED_LIBRARY)

where is your Box2D directory located?

Mines is c:2dx\Box2d

exactly the same

I’m using new cocos2d-1.0.1-x-0.10.0.zip
The old one (cocos2d-1.0.1-x-0.9.2.zip) works well.

hmm going to try to simulate those errors

If you are using it in a demo project, could you send me it?
I would be so pleased. Thanks.

if you go into /game/android/libs/armeabi/

do you have libbox2d.so in there?

no

actually I am using one I translated from xcode to c*+
http://www.raywenderlich.com/457/intro-to-box2d-with-cocos2d-tutorial-bouncing-balls
project that is
Can you copy the error form the terminal starting from

Compile*+ thumb : game_logic <= HelloWorldScene.cpp

Could you paste your “Android.mk” and “Application.mk” files here?

Have houve tried replacing the box2d folder from your project with the box2d folder from cocos2dx root folder? T

hmm rather than typing in here and messing up this topic… do you have irc?

if so can you join irc.rizon.net

and can you join channel #gameofdeceit

my set up is c:2dx\Box2D… that is where my box2D folder is… my project is in C:2dx\myproject1
and the setup in myproject 1 folder is

myproject1-
~~android
~~classes
-resources

hmm I just noticed you don’t have libbox2d.so inside your armeabi folder… that should of been compiled… but i tihnk if you made the changes after you attempted to compile it… it wouldn’t of compiled.

can you edit your application.mk file… do anything to it…make a space… and delete that space… then save it. compile it with ./build_native.sh … and hopefuly all should compile correctly

ok… thanks…
I’m gonna do it.

Now libbox2d.so is OK.
The problem is: it looks like that we have changes in box2d.
This code used to work in old cocos2d-x version.

code:

b2Vec2 gravity;
gravity.Set(0.0f, –10.0f);
bool doSleep = true;
world = new b2World(gravity, doSleep);

error:

Compile*+ thumb : game_logic <= AppDelegate.cpp
Compile*+ thumb : game_logic <= HelloWorldScene.cpp
jni/…/…/Classes/HelloWorldScene.cpp: In constructor ‘HelloWorld::HelloWorld()’:
jni/…/…/Classes/HelloWorldScene.cpp:38: error: no matching function for call to ‘b2World::b2World(b2Vec2&, bool&)’
jni/…/…/Classes/…/…/Box2D/Dynamics/b2World.h:46: note: candidates are: b2World::b2World(const b2Vec2&)
jni/…/…/Classes/…/…/Box2D/Dynamics/b2World.h:42: note: b2World::b2World(const b2World&)
jni/…/…/Classes/HelloWorldScene.cpp:68: error: ‘class b2PolygonShape’ has no member named ‘SetAsEdge’
jni/…/…/Classes/HelloWorldScene.cpp:72: error: ‘class b2PolygonShape’ has no member named ‘SetAsEdge’
jni/…/…/Classes/HelloWorldScene.cpp:76: error: ‘class b2PolygonShape’ has no member named ‘SetAsEdge’
jni/…/…/Classes/HelloWorldScene.cpp:80: error: ‘class b2PolygonShape’ has no member named ‘SetAsEdge’
make: ** [obj/local/armeabi/objs-debug/game_logic/HelloWorldScene.o] Erro 1

This is one reason i told you to replace both the box2d and cocos2dx contents from whatever they are to the ones on the cocos2dx1.0 root directory (if you are not using them from the root folder). I used a lib directory the project creation constructed and had similar problems when building with the new HelloWorld using box2d, until i found out that the box2d (version) folder was not upto date with the new box2d they put in.