分别用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模拟的“别碰钉子”源码分享(含碰撞检测)的更多相关文章

  1. 实例介绍Cocos2d-x中Box2D物理引擎:HelloBox2D

    我们通过一个实例介绍一下,在Cocos2d-x 3.x中使用Box2D物理引擎的开发过程,熟悉这些API的使用.这个实例运行后的场景如图所示,当场景启动后,玩家可以触摸点击屏幕,每次触摸时候,就会在触 ...

  2. Web 开发中很实用的10个效果【附源码下载】

    在工作中,我们可能会用到各种交互效果.而这些效果在平常翻看文章的时候碰到很多,但是一时半会又想不起来在哪,所以养成知识整理的习惯是很有必要的.这篇文章给大家推荐10个在 Web 开发中很有用的效果,记 ...

  3. 3D语音天气球(源码分享)——在Unity中使用Android语音服务

    转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 开篇废话: 这个项目准备分四部分介绍: 一:创建可旋转的"3D球":3 ...

  4. C#中的WinFrom技术实现串口通讯助手(附源码)

    C#中的WinFrom技术实现串口通讯助手(附源码)   实现的功能: 1.实现自动加载可用串口. 2.打开串口,并且使用C#状态栏显示串口的状态. 3.实现了串口的接收数据和发送数据功能. 4.串口 ...

  5. Spring3.2 中 Bean 定义之基于 XML 配置方式的源码解析

    Spring3.2 中 Bean 定义之基于 XML 配置方式的源码解析 本文简要介绍了基于 Spring 的 web project 的启动流程,详细分析了 Spring 框架将开发人员基于 XML ...

  6. 实例介绍Cocos2d-x中Box2D物理引擎:碰撞检测

    在Box2D中碰撞事件通过实现b2ContactListener类函数实现,b2ContactListener是Box2D提供的抽象类,它的抽象函数:virtual void BeginContact ...

  7. 实例介绍Cocos2d-x中Box2D物理引擎:碰撞检測

    在Box2D中碰撞事件通过实现b2ContactListener类函数实现,b2ContactListener是Box2D提供的抽象类,它的抽象函数:virtual void BeginContact ...

  8. iOS中的物理引擎

    目前知名的2D物理引擎有 Box2d,和Chipmunk,这些是跨平台的.但苹果本身也封装了一个物理引擎, UIDynamic是从iOS 7开始引入的一种新技术,隶属于UIKit框架.这可以让开发人员 ...

  9. 实例介绍Cocos2d-x中Box2D物理引擎:使用关节

    下面我们将使用Box2D物理引擎技术进行重构.使得关节能够掌握如何在Box2D使用关节约束.HelloWorldScene.cpp中与使用关节的相关代码如下: void HelloWorld::add ...

随机推荐

  1. CentOS 7下安装配置FTP

    安装vsftpd yum install -y vsftpd 编辑ftp配置文件 vi /etc/vsftpd/vsftpd.conf anonymous_enable=NO #anonymous_e ...

  2. codevs 1450 xth 的旅行

     时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解  查看运行结果     题目描述 Description 毕业了,Xth很高兴,因为他要和他的 ra ...

  3. mfoc安装编译

    系统环境 UBUNTU16.04,要安装好各种基础的编译环境,这就不说了,如果不知道需要什么,那就编译时出错时看到什么安装什么吧 下载源码包libnfc, mfoc, mfcuk,都是github上, ...

  4. MyEclipse Html自动提示功能的设置方法

    操作方法: windows------preferences----------搜索 html----------找到 html Source这项--------- 右边视图,找到 content a ...

  5. 如何限制Dedecms文章或产品描述的字数

    在Dedecms系统中,文章摘要(可以通过infolen或description相关标签调用)被设置了字数上限为250字符,设置上限的主要目的是减少数据库的冗余,保证网站良好的性能.因此,如果对简介内 ...

  6. 爬虫:网页里元素的xpath结构,scrapy不一定就找的到

    这种情况原因是html界面关联的js文件可能会动态修改DOM结构,这样浏览器完成了动态修改DOM,在 浏览器上看到的DOM结构,就和后台抓到的DOM结构不通 举例:新浪微博发的微博,在浏览器通过fir ...

  7. 一种全新的自动调用ajax方法

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  8. EasyMvc入门教程-高级控件说明(14)列布局控件

    想起刚做网页时候,看着这么大的屏幕,一直在 想该如何布局呢,后来经过Table布局,Div布局,Border布局,列式布局. 目前EasyMvc主要支持12列的列式布局(手机兼容性好).请看下面的例子 ...

  9. C# 中的结构类型(struct type)

    ylbtech- .NET-Basic:C# 中的结构类型(struct type) C# 中的结构类型(struct type) 1.A,相关概念返回顶部   像类一样,结构(struct)是能够包 ...

  10. vue2.0 引用qrcode.js实现获取改变二维码的样式

    vue代码 <template> <div class="qart"> <div id="qrcode" ref="qr ...