您的位置:首页 > 移动开发 > Cocos引擎

[Cocos2d-js]chipmunk例子

2015-08-15 14:10 483 查看
<span style="font-size:14px;">initChipmunk:function() {
this.space = new cp.Space();

this.setupDebugNode();

//设置空间内刚体间联系的迭代计算器个数和弹性关系迭代计算器个数.
//chipmunk使用迭代计算器计算出空间内物体的受力关系.
//它建立一个大列表存放物体间所有的碰撞,连接等相互影响关系.根据实际情况传递某些相互作用.
//传递相互作用的数取决于迭代器的个数,每次迭代都使计算结果更精确.
//如果进行了过多的迭代,虽然物理影响效果会更好,但是这也会消耗过多的cpu处理时间.
//如果进行的迭代太少,物理模拟效果会不精确,或者使本该静止的物体没能静止下来.
//使用迭代器的个数在于平衡CPU性能和物理模拟精确度之间权衡.
this.space.iterations = 60;
//设置空间的重力向量
this.space.gravity = cp.v(0, -500);
// 休眠临界时间
this.space.sleepTimeThreshold = 0.5;
this.space.collisionSlop = 0.5;

this.addFloor();
this.addWalls();

var width = 50;
var height = 60;
var mass = width * height * 1/1000;
var rock = this.space.addBody(new cp.Body(mass, cp.momentForBox(mass, width, height)));
rock.setPos(cp.v(500, 100));
rock.setAngle(1);
var shape = this.space.addShape(new cp.BoxShape(rock, width, height));
shape.setFriction(0.3);
shape.setElasticity(0.3);

var radius = 20;
mass = 3;
var body = this.space.addBody(new cp.Body(mass, cp.momentForCircle(mass, 0, radius,cp.v(0, 0))));
body.setPos(cp.v(200, (2 * radius + 5)));
var circle = this.space.addShape(new cp.CircleShape(body, radius,cp.v(0, 0)));

circle.setElasticity(0.8);
circle.setFriction(1);

var ramp = this.space.addShape(new cp.SegmentShape(this.space.staticBody,cp.v(100, 100),cp.v(300, 200), 10));
ramp.setElasticity(1); // 弹性
ramp.setFriction(1); // 摩擦
ramp.setLayers(NOT_GRABABLE_MASK);

var sprite = this.createPhysicsSprite(cc.p(400,200));
this.addChild(sprite);
},
addFloor:function() {
var floor = this.space.addShape(new cp.SegmentShape(this.space.staticBody,cp.v(0, 0),cp.v(640, 0), 0));
floor.setElasticity(1);
floor.setFriction(1);
floor.setLayers(NOT_GRABABLE_MASK);
},
addWalls:function() {
var wall1 = this.space.addShape(new cp.SegmentShape(this.space.staticBody,cp.v(0, 0),cp.v(0, 480), 0));
wall1.setElasticity(1);
wall1.setFriction(1);
wall1.setLayers(NOT_GRABABLE_MASK);

var wall2 = this.space.addShape(new cp.SegmentShape(this.space.staticBody,cp.v(640, 0),cp.v(640, 480), 0));
wall2.setElasticity(1);
wall2.setFriction(1);
wall2.setLayers(NOT_GRABABLE_MASK);
},
createPhysicsSprite:function( pos ) {
var body = new cp.Body(1, cp.momentForBox(1, 48, 108) );
body.setPos( pos );
this.space.addBody( body );
var shape = new cp.BoxShape( body, 48, 108);
shape.setElasticity( 0.5 );
shape.setFriction( 0.5 );
this.space.addShape( shape );

var sprite = cc.PhysicsSprite.create(res.b_ball_01);
sprite.setBody( body );
return sprite;
}</span>


var TAG_SPRITE_BALL = 1;
var TAG_SPRITE_USER = 2;
var TAG_SPRITE_NPC = 3;
var PTM_RATIO = 32;
var GRABABLE_MASK_BIT = 1<<31;
var NOT_GRABABLE_MASK = ~GRABABLE_MASK_BIT;

var MainLayer = cc.Layer.extend({
_ball:null,
_man:null,
_rpc:null,
_leftGoal:null,
_rightGoal:null,
_leftPower:null,
_rightPower:null,
_time:0,

space:null,

ctor:function () {
this._super();
var size = cc.director.getWinSize();

this._ball = new Ball();
this._ball.x = size.width / 2;
this._ball.y = size.height / 2;
this.addChild(this._ball);

// 左方向
var btnLeft = cc.MenuItemImage.create(
res.btn_left_up,
res.btn_left_down,
function () {
this._man.runLeft();
}, this);
btnLeft.x = 50;

// 右方向
var btnRight = cc.MenuItemImage.create(
res.btn_right_up,
res.btn_right_down,
function () {
this._man.runRight();
}, this);
btnRight.x = 125;

// power
var btnPower = cc.MenuItemImage.create(
res.btn_power_up,
res.btn_power_down,
function () {
this._man.power();
}, this);
btnPower.x = 250;

// jump
var btnJump = cc.MenuItemImage.create(
res.btn_jump_up,
res.btn_jump_down,
function () {
this._man.jump();
}, this);
btnJump.x = 330;

// kick
var btnKick = cc.MenuItemImage.create(
res.btn_kick_up,
res.btn_kick_down,
function () {
this._man.kick();
}, this);
btnKick.x = 420;

// 暂停
var btnPause = cc.MenuItemImage.create(
"res/pause.png",
"res/pause2.png",
function () {
this.onStop();
}, this);
btnPause.x = size.width - 30;
btnPause.y = size.height - 50;

var menu = cc.Menu.create(btnLeft, btnRight, btnPower, btnJump, btnKick, btnPause);
menu.x = 0;
menu.y = 20;
this.addChild(menu, 10);

// 主角
this._man = new Footballer_cn();
this._man.flippedX = true;
this._man.x = 150;
this._man.y = 50;
this._man.anchorX = 0.5;
this._man.anchorY = 0;
this.addChild(this._man,0,TAG_SPRITE_USER);
this._npc = new Footballer_br();
this._npc.x = size.width - 150;
this._npc.y = 50;
this._npc.anchorX = 0.5;
this._npc.anchorY = 0;
this.addChild(this._npc,0,TAG_SPRITE_NPC);

// 球门
this._leftGoal = new Goalpost(true);
this._leftGoal.x = 0;
this._leftGoal.y = 50;
this._leftGoal.anchorX = 0;
this._leftGoal.anchorY = 0;
this.addChild(this._leftGoal);

this._rightGoal = new Goalpost();
this._rightGoal.x = size.width;
this._rightGoal.y = 50;
this._rightGoal.anchorX = 1;
this._rightGoal.anchorY = 0;
this.addChild(this._rightGoal);

// power
this._leftPower = new PowerProgress(size.width/2-15,size.height-20,1,0.5,this);
this.addChild(this._leftPower, 10);
this._rightPower = new PowerProgress(size.width/2+15,size.height-20,0,0.5,this);
this.addChild(this._rightPower, 10);

// 系统计划任务,即每帧调用update函数
this.scheduleUpdate();
// 自定义计划任务
this.schedule(this.uiSchedule, 1);

cc.sys.dumpRoot();
cc.sys.garbageCollect();

this.initChipmunk();

return true;
},
onStop:function () {
this._bStop = !this._bStop;
if (this._bStop == true) {
cc.director.pause();
}
else {
cc.director.resume();
}
},

update:function (dt) {
this.space.step(dt);
},
uiSchedule:function () {
this._time++;
this._leftPower.showPower();
this._rightPower.showPower();
},
initChipmunk:function() {
this.space = new cp.Space();
var sprite =  this.createPhysicsSprite( cc.p(cc.director.getWinSize().width/2 , cc.director.getWinSize().height-50) );
this.addChild( sprite,100 );

this.addWalls();
this.space.gravity = cp.v(0, -100);
},
initPhysics:function() {
var space = this.space ;
var staticBody = space.staticBody;
var winSize = cc.director.getWinSize();

// Walls
var walls = [ new cp.SegmentShape( staticBody, cp.v(0,0), cp.v(winSize.width,0), 0 ),                // 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(1);
space.addStaticShape( shape );
}

// Gravity
space.gravity = cp.v(0, -100);
},
addWalls:function() {
// Walls
var winSize = cc.director.getWinSize();
var walls = [ new cp.SegmentShape( this.space.staticBody, cp.v(0,0), cp.v(winSize.width,0), 0 ),                // bottom
new cp.SegmentShape( this.space.staticBody, cp.v(0,winSize.height), cp.v(winSize.width,winSize.height), 0),    // top
new cp.SegmentShape( this.space.staticBody, cp.v(0,0), cp.v(0,winSize.height), 0),                // left
new cp.SegmentShape( this.space.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(0.8);
shape.setFriction(0.1);
this.space.addStaticShape( shape );
}
},
createPhysicsSprite:function( pos ) {

var radius = 20;
var mass = 1;

var body = new cp.Body(mass, cp.momentForCircle(mass, 0, radius,cp.v(0, 0)));
body.setPos( pos );
this.space.addBody( body );
var shape = new cp.CircleShape(body, radius,cp.v(0, 0)); //new cp.BoxShape( body, 48, 108);
shape.setElasticity( 1 );
shape.setFriction( 0.1 );

this.space.addShape( shape );

var sprite = cc.PhysicsSprite.create(res.b_ball_01);
sprite.setBody( body );
return sprite;
},
setupDebugNode:function()
{
// debug only
this._debugNode = cc.PhysicsDebugNode.create( this.space );
this._debugNode.visible = false ;
this.addChild( this._debugNode );
}
});

var MainScene = cc.Scene.extend({
onEnter:function () {
this._super();
this.addChild(new GameBackgroundLayer());
this.addChild(new MainLayer());
}
});


源引:http://www.cnblogs.com/linn/p/3656041.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: