当前位置:   article > 正文

【cocos2dx3.5接入chipmunk物理引擎】_cocos2dx-lua chipmunk raycast

cocos2dx-lua chipmunk raycast

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

闲来无事,尝试给公司的项目接入物理引擎。
环境:cocos2d-x3.5 + lua + 内置chipmunk物理引擎
我都干了啥?
	1.物理引擎支持默认为关闭状态,我打开了它,但由于cocos2d-x3.13之前是不支持安卓64位架构的,
	所以引擎未提供相对应的静态编译文件
	(即缺少 external/chipmunk/prebuilt/android/arm64-v8a/libchipmunk.a),
	所以我在这里进行了手动编译。
	2.接入物理编辑器 PhysicsEditor,并进行lua-binding
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

提示:以下是本篇文章正文内容,下面案例可供参考

一、chipmunk接入。

1.开启chipmunk选项

cocos提供了chipmunk和box2d两种内置物理引擎,但是只对chipmunk的支持更好(即节点、精灵、场景类内置了对chipmunk的支持,且lua-binding已经提供),但是box2d只是简单的塞在了引擎里,需要自己做各种支持。为了省事,这里选择使用chipmunk引擎。找到ccConfig.h文件,进行如下配置。
  • 1
/** 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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

2.手动编译arm64-v8a架构的chipmunk静态库

准备工作如下:

①下载chipmunk源码

②放到指定位置

目录结构1
目录结构2

③添加并修改Android.mk

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

④添加并修改Application.mk

# 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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

⑤ndk编译

在chipmunk目录 输入命令

ndk-build
  • 1

顺利的话会静态编译文件就成功生成在 chipmunk/obj/arm64-v8a/libchipmunk.a

二、接入物理编辑器 PhysicsEditor

①下载

直接官网下载安装即可,下载后可以选择免费试用
进入软件后 根据官网的提示:Exporter选择Cocos2d-x
然后点击 Download loader code 下载解析文件 
包括:PhysicsShapeCache.cpp PhysicsShapeCache.h
  • 1
  • 2
  • 3
  • 4

请添加图片描述
在cocos2d.h中导入依赖
打开cocos2d.h导入PhysicsShapeCache.h
为了保证打包时候新增的文件被编译,根目录下的Android.mk文件中需要增加
请添加图片描述

②lua-binding

1.修改上述俩文件
在适当的位置加上 NS_CC_BEGIN 和 NS_CC_END
(不知道加在那可以参考其它的文件怎么加的)这里旨在吧该类加入cc空间
  • 1
  • 2
2.使用cocos提供的自动生成绑定的脚本生成lua-bangding代码

请添加图片描述
新建一个文件夹 参考上图
genbindings.py来自 tools/tolua
cocos2dx_physicsCache.ini是配置文件
userconf.ini是运行脚本后自动生成的配置文件
cpp和hpp就是自动生成的代码

3.cocos2dx_physicsCache.ini配置如下
[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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/105227
推荐阅读
相关标签
  

闽ICP备14008679号