Android NDK编译Box2D_V2.2

如果大家有留意的话,就知道现在很流行的 愤怒的小鸟 的物理引擎是使用了Box2D,那么如何将这好东西移植到Android上呢?在网上找了一遍,发现资料还是少得可怜,于是自己研究研究一下,遇到问题就google一下~

其实用ndk编译Box2D,无非就是那么几个问题:

Box2D/Common/b2Math.h:27:18: error: limits: No such file or directory
Box2D/Collision/b2BroadPhase.h:25:21: error: algorithm: No such file or directory
Box2D/Dynamics/b2Body.h:24:18: error: memory: No such file or directory
Box2D/Common/b2Math.h:39: error: 'numeric_limits' is not a member of 'std'
Box2D/Common/b2Math.h:39: error: '::infinity' has not been declared
Box2D/Collision/b2BroadPhase.h:207: error: 'sort' is not a member of 'std'
Box2D/Common/b2BlockAllocator.cpp:20:
android/android-ndk-r5b/sources/cxx-stl/system/include/cstdlib:53: error: '::clearenv' has not been declared
Box2D/Common/b2Settings.cpp:20:


Box2D/Dynamics/b2Body.cpp:162: error: no matching function for call to 'operator new(unsigned int, void*&)'
<built-in>:0: note: candidates are: void* operator new(unsigned int)

将这些问题逐个击破就可以了。


我使用的测试例子是Box2D自带的HelloWorld

那Android.mk就这样写了

[plain]  view plain copy print ?
  1. LOCAL_PATH := $(call my-dir)  
  2. include $(CLEAR_VARS)  
  3. LOCAL_MODULE := helloworld  
  4. LOCAL_ARM_MODE := arm  
  5. LOCAL_SRC_FILES := HelloWorld.cpp  
  6. LOCAL_C_INCLUDES += $(LOCAL_PATH) \  
  7.             $(LOCAL_PATH)/Box2D \  
  8.             $(LOCAL_PATH)/Collision \  
  9.             $(LOCAL_PATH)/Common \  
  10.             $(LOCAL_PATH)/Dynamics \  
  11.             $(LOCAL_PATH)/Rope  
  12.   
  13. #LOCAL_LDIBS := -lstdc++  
  14. LOCAL_STATIC_LIBRARIES := libbox2d  
  15. include $(BUILD_EXECUTABLE)  
  16. ##################################  
  17. include $(call all-makefiles-under,$(LOCAL_PATH))  

目的是生成 一个可执行的文件。

以下是编译Box2D的文件,目的是生成一个静态连接库:

Android.mk

[plain]  view plain copy print ?
  1. LOCAL_PATH := $(call my-dir)  
  2.   
  3. include $(CLEAR_VARS)  
  4.   
  5. BOX2D_SRC_FILES += \  
  6.         Collision/b2BroadPhase.cpp \  
  7.         Collision/b2CollideCircle.cpp \  
  8.         Collision/b2CollideEdge.cpp \  
  9.         Collision/b2CollidePolygon.cpp \  
  10.         Collision/b2Collision.cpp \  
  11.         Collision/b2Distance.cpp \  
  12.         Collision/b2DynamicTree.cpp \  
  13.         Collision/b2TimeOfImpact.cpp \  
  14.         Collision/Shapes/b2ChainShape.cpp \  
  15.         Collision/Shapes/b2CircleShape.cpp \  
  16.         Collision/Shapes/b2EdgeShape.cpp \  
  17.         Collision/Shapes/b2PolygonShape.cpp \  
  18.         Common/b2BlockAllocator.cpp \  
  19.         Common/b2Draw.cpp \  
  20.         Common/b2Math.cpp \  
  21.         Common/b2Settings.cpp \  
  22.         Common/b2StackAllocator.cpp \  
  23.         Common/b2Timer.cpp \  
  24.         Dynamics/b2Body.cpp \  
  25.         Dynamics/b2ContactManager.cpp \  
  26.         Dynamics/b2Fixture.cpp \  
  27.         Dynamics/b2Island.cpp \  
  28.         Dynamics/b2World.cpp \  
  29.         Dynamics/b2WorldCallbacks.cpp \  
  30.         Dynamics/Contacts/b2ChainAndCircleContact.cpp \  
  31.         Dynamics/Contacts/b2ChainAndPolygonContact.cpp \  
  32.         Dynamics/Contacts/b2CircleContact.cpp \  
  33.         Dynamics/Contacts/b2Contact.cpp \  
  34.         Dynamics/Contacts/b2ContactSolver.cpp \  
  35.         Dynamics/Contacts/b2EdgeAndCircleContact.cpp \  
  36.         Dynamics/Contacts/b2EdgeAndPolygonContact.cpp \  
  37.         Dynamics/Contacts/b2PolygonAndCircleContact.cpp \  
  38.         Dynamics/Contacts/b2PolygonContact.cpp \  
  39.         Dynamics/Joints/b2DistanceJoint.cpp \  
  40.         Dynamics/Joints/b2FrictionJoint.cpp \  
  41.         Dynamics/Joints/b2GearJoint.cpp \  
  42.         Dynamics/Joints/b2Joint.cpp \  
  43.         Dynamics/Joints/b2MouseJoint.cpp \  
  44.         Dynamics/Joints/b2PrismaticJoint.cpp \  
  45.         Dynamics/Joints/b2PulleyJoint.cpp \  
  46.         Dynamics/Joints/b2RevoluteJoint.cpp \  
  47.         Dynamics/Joints/b2RopeJoint.cpp \  
  48.         Dynamics/Joints/b2WeldJoint.cpp \  
  49.         Dynamics/Joints/b2WheelJoint.cpp \  
  50.         Rope/b2Rope.cpp  
  51.   
  52. LOCAL_MODULE := box2d  
  53.   
  54. LOCAL_C_INCLUDES += $(LOCAL_PATH) \  
  55.             $(LOCAL_PATH)/../  
  56.   
  57.   
  58. LOCAL_SRC_FILES := $(BOX2D_SRC_FILES)  
  59.   
  60. include $(BUILD_STATIC_LIBRARY)  

好了,准备的功夫就绪了,ndk-build吧。

然后就XXXXXXXXXXXXXXXXXXXXX

XXXXXXXXXXXXXXXXXXXXXXXX

XXXXXXXXXXXXXXXXXXXXXXX

一堆错误。

毕竟ndk编译还是有一点不足,那么我们就要对源码进行移向的修改,修改记录如下:

  1. /***************************************************************************************************/  
  2. Box2D/Common/b2Math.h    注释#include <limits>  
  3.             //float32 infinity = std::numeric_limits<float32>::infinity(); 修改为以下  
  4.             float32 infinity = INFINITY;  
  5. /***************************************************************************************************/             
  6. Box2D/Collision/b2BroadPhase.h   注释#include <algorithm>  
  7.                 // 添加一个头文件  #include <stdlib.h>         
  8. // 添加一个方法  
  9. static int b2PairQSORTLessThan (const void*element1, const void*element2)  
  10. {  
  11.     b2Pair *pair1 = (b2Pair*)element1;  
  12.     b2Pair *pair2 = (b2Pair*)element2;  
  13.       
  14.     if (pair1->proxyIdA < pair2->proxyIdA)  
  15.         return -1;  
  16.     else if (pair1->proxyIdA > pair2->proxyIdA)  
  17.         return 1;  
  18.     else {  
  19.         if (pair1->proxyIdB < pair2->proxyIdB)  
  20.             return -1;  
  21.         else if (pair1->proxyIdB > pair2->proxyIdB)  
  22.             return 1;  
  23.     }  
  24.     return 0;  
  25. }  
  26.   
  27. //std::sort(m_pairBuffer, m_pairBuffer + m_pairCount, b2PairLessThan);修改为以下  
  28. qsort (m_pairBuffer, m_pairCount, sizeof (b2Pair), b2PairQSORTLessThan);  
  29.   
  30. /***************************************************************************************************/  
  31. Box2D/Dynamics/b2Body.h    
  32.             注释#include <memory>    
  33.             添加头文件 #include <memory.h>  
  34. /***************************************************************************************************/  
  35. Box2D/Common/b2BlockAllocator.cpp  //#include <memory> 注释掉  
  36.                    // 添加头文件   
  37.                    #include <memory.h>  
  38.                   //#include <cstdlib> 注释掉  
  39.                   // 添加头文件   
  40.                       #include <stdlib.h>  
  41.   
  42. /***************************************************************************************************/  
  43. Box2D/Common/b2Settings.cpp  
  44.             //#include <cstdlib> 注释掉  
  45.             // 添加头文件  
  46.             #include <stdlib.h>  
  47.   
  48. /***************************************************************************************************/  
  49. Box2D/Dynamics/b2Body.cpp  
  50.             // 添加头文件  
  51.             #include <new>  
  52. /***************************************************************************************************/  

好了,好了,再编译一下吧。一切顺利了吧~

最后会生成 helloworld的可执行文件,将它push 到模拟器运行一下,好了,没问题了~



修改过的源码下载地址 :

http://download.csdn.net/detail/feifei454498130/3583581


个人是比较懒了,不想多说了。^^

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值