使用cocos2d-js-3.0RC1中的物理引擎chipmunk模拟的“别碰钉子”源码分享(含碰撞检测)

前端之家收集整理的这篇文章主要介绍了使用cocos2d-js-3.0RC1中的物理引擎chipmunk模拟的“别碰钉子”源码分享(含碰撞检测)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

http://www.cocoachina.com/bbs/read.PHP?tid=220250

管理提醒:本帖被 superdragon 执行加亮操作(2014-08-27)
分别用Box2d和chipmunk实现了一下,不过Box2d没整理,也懒得整理了。
chipmunk整理了一下,分享给大家吧。

演示地址: http://121.40.100.196/box/
刚开始研究,抛砖引玉
简要说明:
1、初始化物理环境,增加边界
?
@H_404_44@ 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
initPhysics: function () {
var space = this .space ;
staticBody = space.staticBody;
//开启物体形状测试
//this.initDebugMode();
// Gravity
space.gravity = cp.v(0,-980); //重力
space.sleepTimeThreshold = 0.5; //休眠临界时间
space.collisionSlop = 0.5; //
// Walls--四个边界
walls = [ new cp.SegmentShape( staticBody,cp.v(0,0-1),cp.v(winSize.width,0),0-1 ), // bottom
// top
// left
// right
];
for ( i=0; i < walls.length; i++ ) {
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、物体形状测试
?
4
initDebugMode: ._debugNode = cc.PhysicsDebugNode.create( .space);
.addChild( ._debugNode);
3、物体定义
28
initBoxWithBody: //物体的定义
mass = 1;
BoxWidth = 32;
body = cp.Body(mass,cp.momentForBox(mass,BoxWidth,BoxWidth) );
body.setPos( cc.p(winSize.width/2,winSize.height/2) );
.space.addBody( body );
shape = cp.BoxShape( body,BoxWidth);
shape.setElasticity( 0.5 );
shape.setFriction( 0.3 );
shape.setCollisionType(1);
shape.setLayers(3);
.space.addShape( shape );
//创建一个箱子
v_texture = cc.textureCache.addImage(res.Box_png);
.Box = cc.PhysicsSprite.create(v_texture,cc.rect(0,BoxWidth));
.Box.setBody(body);
.Box,1);
.Box.setTag(101);
//上下移动
moveTo1 = cc.MoveTo.create(0.5,winSize.width / 2, .Box.y + 40);
moveTo2 = cc.MoveTo.create(0.5,monospace!important; font-size:1em!important; min-height:inherit!important; color:black!important; background:none!important">.Box.y - 40);
.downUpAction = cc.RepeatForever.create(cc.Sequence.create(moveTo1,moveTo2));
.Box.runAction( .downUpAction);
4、增加点击事件、碰撞检测监听
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
onEnter: ._super();
cc.sys.dumpRoot();
cc.sys.garbageCollect();
//事件处理
( 'touches' in cc.sys.capabilities ){
cc.eventManager.addListener({
event: cc.EventListener.TOUCH_ALL_AT_ONCE,
onTouchesEnded: (touches,event){
event.getCurrentTarget().processEvent( touches[0] );
}
);
} else if 'mouse' cc.sys.capabilities ){
cc.eventManager.addListener({
event: cc.EventListener.MOUSE,
onMouseDown: (event){
event.getCurrentTarget().processEvent( event );
}
);
}
//重置数据
.resetDatas();
//
.scheduleUpdate();
//添加碰撞监听事件
// 1 & 2 检测Box和上下BLOCK碰撞
.space.addCollisionHandler( 1,2,
.collisionBegin.bind( ),
.collisionPre.bind( .collisionPost.bind( .collisionSeparate.bind( )
);
// 1 & 3 检测Box和左右边界碰撞
)
);
// 1 & 4 检测Box和左右BLOCK碰撞
)
);

5、碰撞检测
39
collisionBegin : ( arbiter,space ) {
shapes = arbiter.getShapes();
shapeA = shapes[0];
shapeB = shapes[1];
collTypeA = shapeA.collision_type;
collTypeB = shapeB.collision_type;
(collTypeB == 3){
console.log( 'Collision Type A:' + collTypeA );
'end Collision Type B:' + collTypeB );
.BoxDirectionX = - .BoxDirectionX;
.space.addPostStepCallback( () {
.updateBoxAndBlocks();
}.bind( ));
} (collTypeB == 2 || collTypeB == 4)
{ //碰到上下墙壁 或者 左右出来的BLOCKS 就Gameover
.gameOver();
}
return true ;
collisionPre : //console.log('collision pre');
;
collisionPost : //console.log('collision post');
collisionSeparate : //console.log('collision separate');
}
原文链接:https://www.f2er.com/cocos2dx/343950.html

猜你在找的Cocos2d-x相关文章