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就这样写了

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := helloworld
LOCAL_ARM_MODE := arm
LOCAL_SRC_FILES := HelloWorld.cpp
LOCAL_C_INCLUDES += $(LOCAL_PATH) \
		    $(LOCAL_PATH)/Box2D \
		    $(LOCAL_PATH)/Collision \
		    $(LOCAL_PATH)/Common \
		    $(LOCAL_PATH)/Dynamics \
		    $(LOCAL_PATH)/Rope

#LOCAL_LDIBS := -lstdc++
LOCAL_STATIC_LIBRARIES := libbox2d
include $(BUILD_EXECUTABLE)
##################################
include $(call all-makefiles-under,$(LOCAL_PATH))

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

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

Android.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

BOX2D_SRC_FILES += \
		Collision/b2BroadPhase.cpp \
		Collision/b2CollideCircle.cpp \
		Collision/b2CollideEdge.cpp \
		Collision/b2CollidePolygon.cpp \
		Collision/b2Collision.cpp \
		Collision/b2Distance.cpp \
		Collision/b2DynamicTree.cpp \
		Collision/b2TimeOfImpact.cpp \
		Collision/Shapes/b2ChainShape.cpp \
		Collision/Shapes/b2CircleShape.cpp \
		Collision/Shapes/b2EdgeShape.cpp \
		Collision/Shapes/b2PolygonShape.cpp \
		Common/b2BlockAllocator.cpp \
		Common/b2Draw.cpp \
		Common/b2Math.cpp \
		Common/b2Settings.cpp \
		Common/b2StackAllocator.cpp \
		Common/b2Timer.cpp \
		Dynamics/b2Body.cpp \
		Dynamics/b2ContactManager.cpp \
		Dynamics/b2Fixture.cpp \
		Dynamics/b2Island.cpp \
		Dynamics/b2World.cpp \
		Dynamics/b2WorldCallbacks.cpp \
		Dynamics/Contacts/b2ChainAndCircleContact.cpp \
		Dynamics/Contacts/b2ChainAndPolygonContact.cpp \
		Dynamics/Contacts/b2CircleContact.cpp \
		Dynamics/Contacts/b2Contact.cpp \
		Dynamics/Contacts/b2ContactSolver.cpp \
		Dynamics/Contacts/b2EdgeAndCircleContact.cpp \
		Dynamics/Contacts/b2EdgeAndPolygonContact.cpp \
		Dynamics/Contacts/b2PolygonAndCircleContact.cpp \
		Dynamics/Contacts/b2PolygonContact.cpp \
		Dynamics/Joints/b2DistanceJoint.cpp \
		Dynamics/Joints/b2FrictionJoint.cpp \
		Dynamics/Joints/b2GearJoint.cpp \
		Dynamics/Joints/b2Joint.cpp \
		Dynamics/Joints/b2MouseJoint.cpp \
		Dynamics/Joints/b2PrismaticJoint.cpp \
		Dynamics/Joints/b2PulleyJoint.cpp \
		Dynamics/Joints/b2RevoluteJoint.cpp \
		Dynamics/Joints/b2RopeJoint.cpp \
		Dynamics/Joints/b2WeldJoint.cpp \
		Dynamics/Joints/b2WheelJoint.cpp \
		Rope/b2Rope.cpp

LOCAL_MODULE := box2d

LOCAL_C_INCLUDES += $(LOCAL_PATH) \
		    $(LOCAL_PATH)/../


LOCAL_SRC_FILES := $(BOX2D_SRC_FILES)

include $(BUILD_STATIC_LIBRARY)

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

然后就XXXXXXXXXXXXXXXXXXXXX

XXXXXXXXXXXXXXXXXXXXXXXX

XXXXXXXXXXXXXXXXXXXXXXX

一堆错误。

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

/***************************************************************************************************/
Box2D/Common/b2Math.h    注释#include <limits>
			//float32 infinity = std::numeric_limits<float32>::infinity(); 修改为以下
			float32 infinity = INFINITY;
/***************************************************************************************************/			
Box2D/Collision/b2BroadPhase.h   注释#include <algorithm>
				// 添加一个头文件  #include <stdlib.h>		
// 添加一个方法
static int b2PairQSORTLessThan (const void*element1, const void*element2)
{
	b2Pair *pair1 = (b2Pair*)element1;
	b2Pair *pair2 = (b2Pair*)element2;
	
	if (pair1->proxyIdA < pair2->proxyIdA)
		return -1;
	else if (pair1->proxyIdA > pair2->proxyIdA)
		return 1;
	else {
		if (pair1->proxyIdB < pair2->proxyIdB)
			return -1;
		else if (pair1->proxyIdB > pair2->proxyIdB)
			return 1;
	}
	return 0;
}

//std::sort(m_pairBuffer, m_pairBuffer + m_pairCount, b2PairLessThan);修改为以下
qsort (m_pairBuffer, m_pairCount, sizeof (b2Pair), b2PairQSORTLessThan);

/***************************************************************************************************/
Box2D/Dynamics/b2Body.h  
			注释#include <memory>  
			添加头文件 #include <memory.h>
/***************************************************************************************************/
Box2D/Common/b2BlockAllocator.cpp  //#include <memory> 注释掉
				   // 添加头文件 
				   #include <memory.h>
				  //#include <cstdlib> 注释掉
				  // 添加头文件 
			          #include <stdlib.h>

/***************************************************************************************************/
Box2D/Common/b2Settings.cpp
			//#include <cstdlib> 注释掉
			// 添加头文件
			#include <stdlib.h>

/***************************************************************************************************/
Box2D/Dynamics/b2Body.cpp
			// 添加头文件
			#include <new>
/***************************************************************************************************/

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

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

Android NDK编译Box2D_V2.2_第1张图片


修改过的源码下载地址 :

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


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


你可能感兴趣的:(android,Module,Path,include,pair,math.h)