使用cocos2d-js-3.0RC1中的物理引擎chipmunk模拟的“别碰钉子”源码分享(含碰撞检测)
分别用box2d和chipmunk实现了一下,不过box2d没整理,也懒得整理了。
chipmunk整理了一下,分享给大家吧。
刚开始研究,抛砖引玉
简要说明:
1、初始化物理环境,增加边界
initPhysics: function () {
var space = this.space ;
var staticBody = space.staticBody;
//开启物体形状测试
//this.initDebugMode();
// Gravity
space.gravity = cp.v(0, -980); //重力
space.sleepTimeThreshold = 0.5; //休眠临界时间
space.collisionSlop = 0.5; //
// Walls--四个边界
var walls = [ new cp.SegmentShape( staticBody, cp.v(0,0-1), cp.v(winSize.width,0), 0-1 ), // bottom
new cp.SegmentShape( staticBody, cp.v(0,winSize.height), cp.v(winSize.width,winSize.height), 0), // top
new cp.SegmentShape( staticBody, cp.v(0,0), cp.v(0,winSize.height), 0), // left
new cp.SegmentShape( staticBody, cp.v(winSize.width,0), cp.v(winSize.width,winSize.height), 0) // right
];
for( var i=0; i < walls.length; i++ ) {
var shape = walls<i>;
shape.setElasticity(1); //弹性
shape.setFriction(0); //摩擦
//space.addStaticShape( shape );
space.addShape( shape );
if(i >= 2){
shape.setCollisionType(3);
}
shape.setLayers(1);
}
},</i>
2、物体形状测试
initDebugMode: function () {
this._debugNode = cc.PhysicsDebugNode.create(this.space);
this.addChild(this._debugNode);
},
3、物体定义
initBoxWithBody: function () {
//物体的定义
var mass = 1;
var boxWidth = 32;
var body = new cp.Body(mass, cp.momentForBox(mass, boxWidth, boxWidth) );
body.setPos( cc.p(winSize.width/2, winSize.height/2) );
this.space.addBody( body );
var shape = new cp.BoxShape( body, boxWidth, boxWidth);
shape.setElasticity( 0.5 );
shape.setFriction( 0.3 );
shape.setCollisionType(1);
shape.setLayers(3);
this.space.addShape( shape );
//创建一个箱子
var v_texture = cc.textureCache.addImage(res.box_png);
this.box = cc.PhysicsSprite.create(v_texture,cc.rect(0,0,boxWidth,boxWidth));
this.box.setBody(body);
this.addChild(this.box,1);
this.box.setTag(101);
//上下移动
var moveTo1 = cc.MoveTo.create(0.5, winSize.width / 2, this.box.y + 40);
var moveTo2 = cc.MoveTo.create(0.5, winSize.width / 2, this.box.y - 40);
this.downUpAction = cc.RepeatForever.create(cc.Sequence.create(moveTo1,moveTo2));
this.box.runAction(this.downUpAction);
},
4、增加点击事件、碰撞检测监听
onEnter: function () {
this._super();
cc.sys.dumpRoot();
cc.sys.garbageCollect();
//事件处理
if( 'touches' in cc.sys.capabilities ){
cc.eventManager.addListener({
event: cc.EventListener.TOUCH_ALL_AT_ONCE,
onTouchesEnded: function(touches, event){
event.getCurrentTarget().processEvent( touches[0] );
}
}, this);
} else if( 'mouse' in cc.sys.capabilities ){
cc.eventManager.addListener({
event: cc.EventListener.MOUSE,
onMouseDown: function(event){
event.getCurrentTarget().processEvent( event );
}
}, this);
}
//重置数据
this.resetDatas();
//
this.scheduleUpdate();
//添加碰撞监听事件
// 1 & 2 检测box和上下BLOCK碰撞
this.space.addCollisionHandler( 1, 2,
this.collisionBegin.bind(this),
this.collisionPre.bind(this),
this.collisionPost.bind(this),
this.collisionSeparate.bind(this)
);
// 1 & 3 检测box和左右边界碰撞
this.space.addCollisionHandler( 1, 3,
this.collisionBegin.bind(this),
this.collisionPre.bind(this),
this.collisionPost.bind(this),
this.collisionSeparate.bind(this)
);
// 1 & 4 检测box和左右BLOCK碰撞
this.space.addCollisionHandler( 1, 4,
this.collisionBegin.bind(this),
this.collisionPre.bind(this),
this.collisionPost.bind(this),
this.collisionSeparate.bind(this)
);
},
5、碰撞检测
collisionBegin : function ( arbiter, space ) {
var shapes = arbiter.getShapes();
var shapeA = shapes[0];
var shapeB = shapes[1];
var collTypeA = shapeA.collision_type;
var collTypeB = shapeB.collision_type;
if(collTypeB == 3){
console.log( 'Collision Type A:' + collTypeA );
console.log( 'end Collision Type B:' + collTypeB );
this.boxDirectionX = -this.boxDirectionX;
this.space.addPostStepCallback(function () {
this.updateBoxAndBlocks();
}.bind(this));
}else if(collTypeB == 2 || collTypeB == 4)
{//碰到上下墙壁 或者 左右出来的BLOCKS 就Gameover
this.gameOver();
}
return true;
},
collisionPre : function ( arbiter, space ) {
//console.log('collision pre');
return true;
},
collisionPost : function ( arbiter, space ) {
//console.log('collision post');
},
collisionSeparate : function ( arbiter, space ) {
//console.log('collision separate');
}
使用cocos2d-js-3.0RC1中的物理引擎chipmunk模拟的“别碰钉子”源码分享(含碰撞检测)的更多相关文章
- 实例介绍Cocos2d-x中Box2D物理引擎:HelloBox2D
我们通过一个实例介绍一下,在Cocos2d-x 3.x中使用Box2D物理引擎的开发过程,熟悉这些API的使用.这个实例运行后的场景如图所示,当场景启动后,玩家可以触摸点击屏幕,每次触摸时候,就会在触 ...
- Web 开发中很实用的10个效果【附源码下载】
在工作中,我们可能会用到各种交互效果.而这些效果在平常翻看文章的时候碰到很多,但是一时半会又想不起来在哪,所以养成知识整理的习惯是很有必要的.这篇文章给大家推荐10个在 Web 开发中很有用的效果,记 ...
- 3D语音天气球(源码分享)——在Unity中使用Android语音服务
转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 开篇废话: 这个项目准备分四部分介绍: 一:创建可旋转的"3D球":3 ...
- C#中的WinFrom技术实现串口通讯助手(附源码)
C#中的WinFrom技术实现串口通讯助手(附源码) 实现的功能: 1.实现自动加载可用串口. 2.打开串口,并且使用C#状态栏显示串口的状态. 3.实现了串口的接收数据和发送数据功能. 4.串口 ...
- Spring3.2 中 Bean 定义之基于 XML 配置方式的源码解析
Spring3.2 中 Bean 定义之基于 XML 配置方式的源码解析 本文简要介绍了基于 Spring 的 web project 的启动流程,详细分析了 Spring 框架将开发人员基于 XML ...
- 实例介绍Cocos2d-x中Box2D物理引擎:碰撞检测
在Box2D中碰撞事件通过实现b2ContactListener类函数实现,b2ContactListener是Box2D提供的抽象类,它的抽象函数:virtual void BeginContact ...
- 实例介绍Cocos2d-x中Box2D物理引擎:碰撞检測
在Box2D中碰撞事件通过实现b2ContactListener类函数实现,b2ContactListener是Box2D提供的抽象类,它的抽象函数:virtual void BeginContact ...
- iOS中的物理引擎
目前知名的2D物理引擎有 Box2d,和Chipmunk,这些是跨平台的.但苹果本身也封装了一个物理引擎, UIDynamic是从iOS 7开始引入的一种新技术,隶属于UIKit框架.这可以让开发人员 ...
- 实例介绍Cocos2d-x中Box2D物理引擎:使用关节
下面我们将使用Box2D物理引擎技术进行重构.使得关节能够掌握如何在Box2D使用关节约束.HelloWorldScene.cpp中与使用关节的相关代码如下: void HelloWorld::add ...
随机推荐
- 参数化1--jmeter参数化数据(_csvread函数、用户自定义变量等)
以下是转载内容,仔细看过后,觉得用得最多的应该是csvread函数.用户自定义变量以及CSV DATA CONFIG控制器这几个,但是做练习之后,在结果树和聚合报告中怎么查看执行结果是个问题,没找到对 ...
- gitlab gitlab runner
1.安装gitlab https://about.gitlab.com/installation/#ubuntu 2.安装runner https://docs.gitlab.com/runner/i ...
- Delphi 释放数组中的数据
FillChar(aryTest[Low(aryTest)], Length(aryTest) * SizeOf(aryTest[Low(aryTest)]), 0);
- WinRAR4.20注册文件key文件注册码
1.首先安装rar4.2官方版 2.在WinRAR已安装文件夹内新建文本文档,打开文档,把下面代码复制进去 RAR registration datawncnUnlimited Company Lic ...
- Cocos2d-x 3.0 屏幕触摸及消息分发机制
***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...
- 【2048小游戏】——原生js爬坑之封装行的移动算法&事件
引言:2048小游戏的核心玩法是移动行,包括横行和纵行,玩家可以选择4个方向,然后所有行内的数字就会随着行的移动而向特定的方向移动.这个行的移动是一个需要重复调用的算法,所以这里就要将一行的移动算法封 ...
- Web自动化测试框架改进
Web自动化测试框架(WebTestFramework)是基于Selenium框架且采用PageObject设计模式进行二次开发形成的框架. 一.适用范围:传统Web功能自动化测试.H5功能自动化测试 ...
- Putty的噩梦——渗透工具PuttyRider使用心得分享
我们在入侵到一台主机的时候,经常会看到管理员的桌面会放着putty.exe,这说明有很大的可能性管理员是使用putty远程管理主机的. 该工具主要是针对SSH客户端putty的利用,采用DLL注入的方 ...
- C 语言经典100例
C 语言经典100例 C 语言练习实例1 C 语言练习实例2 C 语言练习实例3 C 语言练习实例4 C 语言练习实例5 C 语言练习实例6 C 语言练习实例7 C 语言练习实例8 C 语言练习实例9 ...
- 通过apache,和nginx模块去除html中的空格和tab
最近一个项目中,合作方要求去除html中的空格,不想改代码,所以百度了一下通过apache,和nginx模块去除html中的空格和tab的方案,下面记录下来: 一.nginx nginx可以通过mod ...