赞
踩
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
闲来无事,尝试给公司的项目接入物理引擎。
环境:cocos2d-x3.5 + lua + 内置chipmunk物理引擎
我都干了啥?
1.物理引擎支持默认为关闭状态,我打开了它,但由于cocos2d-x3.13之前是不支持安卓64位架构的,
所以引擎未提供相对应的静态编译文件
(即缺少 external/chipmunk/prebuilt/android/arm64-v8a/libchipmunk.a),
所以我在这里进行了手动编译。
2.接入物理编辑器 PhysicsEditor,并进行lua-binding
提示:以下是本篇文章正文内容,下面案例可供参考
cocos提供了chipmunk和box2d两种内置物理引擎,但是只对chipmunk的支持更好(即节点、精灵、场景类内置了对chipmunk的支持,且lua-binding已经提供),但是box2d只是简单的塞在了引擎里,需要自己做各种支持。为了省事,这里选择使用chipmunk引擎。找到ccConfig.h文件,进行如下配置。
/** Use physics integration API. */ #ifndef CC_USE_PHYSICS #define CC_USE_PHYSICS 1 #endif #if (CC_USE_PHYSICS) /** Use chipmunk physics 2d engine. */ #ifndef CC_ENABLE_CHIPMUNK_INTEGRATION #define CC_ENABLE_CHIPMUNK_INTEGRATION 1 #endif /** or use box2d physics 2d engine. */ #ifndef CC_ENABLE_BOX2D_INTEGRATION #define CC_ENABLE_BOX2D_INTEGRATION 0 #endif #endif // CC_USE_PHYSICS
准备工作如下:
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_CFLAGS += -std=c99 LOCAL_MODULE := chipmunk_static LOCAL_MODULE_FILENAME := libchipmunk LOCAL_ARM_MODE := arm LOCAL_SRC_FILES := \ src/chipmunk.c \ src/cpArbiter.c \ src/cpArray.c \ src/cpBB.c \ src/cpBBTree.c \ src/cpBody.c \ src/cpCollision.c \ src/cpHashSet.c \ src/cpPolyShape.c \ src/cpShape.c \ src/cpSpace.c \ src/cpSpaceComponent.c \ src/cpSpaceHash.c \ src/cpSpaceQuery.c \ src/cpSpaceStep.c \ src/cpSpatialIndex.c \ src/cpSweep1D.c \ src/cpVect.c \ src/constraints/cpConstraint.c \ src/constraints/cpDampedRotarySpring.c \ src/constraints/cpDampedSpring.c \ src/constraints/cpGearJoint.c \ src/constraints/cpGrooveJoint.c \ src/constraints/cpPinJoint.c \ src/constraints/cpPivotJoint.c \ src/constraints/cpRatchetJoint.c \ src/constraints/cpRotaryLimitJoint.c \ src/constraints/cpSimpleMotor.c \ src/constraints/cpSlideJoint.c LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/.. LOCAL_C_INCLUDES := $(LOCAL_PATH)/.. \ $(LOCAL_PATH)/src/ \ include $(BUILD_STATIC_LIBRARY)
# it is needed for ndk-r5
APP_PLATFORM := android-10
# APP_STL := c++_static
# APP_CPPFLAGS := -frtti -std=c++11 -fsigned-char
APP_MODULES := chipmunk_static
APP_ABI := arm64-v8a
#APP_ABI :=x86
#APP_ABI :=mips mips-r2 mips-r2-sf armeabi
在chipmunk目录 输入命令
ndk-build
顺利的话会静态编译文件就成功生成在 chipmunk/obj/arm64-v8a/libchipmunk.a
直接官网下载安装即可,下载后可以选择免费试用
进入软件后 根据官网的提示:Exporter选择Cocos2d-x
然后点击 Download loader code 下载解析文件
包括:PhysicsShapeCache.cpp PhysicsShapeCache.h
在cocos2d.h中导入依赖
为了保证打包时候新增的文件被编译,根目录下的Android.mk文件中需要增加
在适当的位置加上 NS_CC_BEGIN 和 NS_CC_END
(不知道加在那可以参考其它的文件怎么加的)这里旨在吧该类加入cc空间
新建一个文件夹 参考上图
genbindings.py来自 tools/tolua
cocos2dx_physicsCache.ini是配置文件
userconf.ini是运行脚本后自动生成的配置文件
cpp和hpp就是自动生成的代码
[cocos2dx_physics] # the prefix to be added to the generated functions. You might or might not use this in your own # templates prefix = cocos2dx_physics # create a target namespace (in javascript, this would create some code like the equiv. to `ns = ns || {}`) # all classes will be embedded in that namespace target_namespace = cc macro_judgement = #if CC_USE_PHYSICS android_headers = -I%(androidndkdir)s/platforms/android-14/arch-arm/usr/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.7/libs/armeabi-v7a/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.7/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.8/libs/armeabi-v7a/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.8/include android_flags = -D_SIZE_T_DEFINED_ clang_headers = -I%(clangllvmdir)s/lib/clang/3.3/include clang_flags = -nostdinc -x c++ -std=c++11 -U __SSE__ cocos_headers = -I%(cocosdir)s/cocos -I%(cocosdir)s/cocos/platform/android cocos_flags = -DANDROID cxxgenerator_headers = # extra arguments for clang extra_arguments = %(android_headers)s %(clang_headers)s %(cxxgenerator_headers)s %(cocos_headers)s %(android_flags)s %(clang_flags)s %(cocos_flags)s %(extra_flags)s # what headers to parse headers = %(cocosdir)s/cocos/cocos2d.h # what classes to produce code for. You can use regular expressions here. When testing the regular # expression, it will be enclosed in "^$", like this: "^Menu*$". classes = PhysicsShapeCache.* # what should we skip? in the format ClassName::[function function] # ClassName is a regular expression, but will be used like this: "^ClassName$" functions are also # regular expressions, they will not be surrounded by "^$". If you want to skip a whole class, just # add a single "*" as functions. See bellow for several examples. A special class name is "*", which # will apply to all class names. This is a convenience wildcard to be able to skip similar named # functions from all classes. skip = rename_functions = rename_classes = # for all class names, should we remove something when registering in the target VM? remove_prefix = # classes for which there will be no "parent" lookup classes_have_no_parents = # base classes which will be skipped when their sub-classes found them. base_classes_to_skip = # classes that create no constructor # Set is special and we will use a hand-written constructor abstract_classes = # Determining whether to use script object(js object) to control the lifecycle of native(cpp) object or the other way around. Supported values are 'yes' or 'no'. script_control_cpp = no
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。