分别用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. ELK之filebeat替代logstash收集日志

    filebeat->redis->logstash->elasticsearch 官网下载地址:https://www.elastic.co/downloads/beats/file ...

  2. 深入理解OAuth2.0 XSS CSRF CORS 原理

    基于Token的WEB后台认证机制 http://www.cnblogs.com/xiekeli/p/5607107.html 深入理解OAuth2.0协议http://blog.csdn.net/s ...

  3. 测试Apache服务器及httpd: Could not reliably determine the server's fully qualified domain name解决办法

    测试Apache服务器: 重启apache: sudo /usr/local/apache/bin/apachectl restart 若出现错误: httpd: Could not reliably ...

  4. ZRender实现粒子网格动画实战

    注:本博文代码基于ZRender 3.4.3版本号开发,相应版本号库地址:ZRender 库. 效果 实现分析 通过上面显示的效果图,能够看出,这样的效果就是在Canvas中生成多个可移动的点,然后依 ...

  5. nginx rewrite arg 带问号的地址转发参数处理?Nginx重定向的参数问题

    Nginx重定向的参数问题 在给某网站写rewrite重定向规则时,碰到了这个关于重定向的参数处理问题.默认的情况下,Nginx在进行rewrite后都会自动添加上旧地址中的参数部分,而这对于重定向到 ...

  6. 软件业的发展方向:云、Web以及App

    随着行业互联网的发展,未来的软件发展方向是云技术.Web软件以及基于移动设备的Apps. 桌面软件主要负责大型的计算.渲染和消耗非常大CPU和内存的图形软件,以及基于这些软件的二次开发软件如Revit ...

  7. 【SharePoint】SharePoint2013中使用客户端对象模型给用户控件赋初值

    本文要实现的功能:新建一条列表记录,打开新建记录画面时,自动给[申请人]赋值为当前登录用户. 在SharePoint2010中,可以使用SPServices的SPFindPeoplePicker方法来 ...

  8. vue2.0 仿手机新闻站(七)过滤器、动画效果

    1.全局过滤器 (1)normalTime.js  自定义 将 时间戳 转换成 日期格式 过滤器 /** * 将 时间戳 转换成 日期格式 */ export const normalTime = ( ...

  9. merge-sorted-array——合并两个有序数组

    Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume th ...

  10. 苹果证书的申请、unityoc交互基础

    注冊开发人员账号时:注意不要使用中国邮箱 99美金证书 :仅仅支持上传AppStore. 299美金证书:指的的我开发的应用是仅仅支持打包安装.企业级的. 假设申请了开发人员账号.也就是交了那几百美金 ...