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 = 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,height)); shape.setFriction(0.3); shape.setElasticity(0.3); var radius = 20; mass = 3; var body = new cp.Body(mass,cp.momentForCircle(mass,radius,cp.v(0)))); body.setPos(cp.v(200,(2 * radius + 5))); var circle = new cp.CircleShape(body,128); line-height:1.5!important">0))); circle.setElasticity(0.8); circle.setFriction(1); var ramp = new cp.SegmentShape(this.space.staticBody,128); line-height:1.5!important">100,128); line-height:1.5!important">100),128); line-height:1.5!important">300,128); line-height:1.5!important">200),128); line-height:1.5!important">10)); ramp.setElasticity(1); 弹性 ramp.setFriction( 摩擦 ramp.setLayers(NOT_GRABABLE_MASK); var sprite = this.createPhysicsSprite(cc.p(400,128); line-height:1.5!important">200)); this.addChild(sprite); },addFloor:function() { var floor = 0),128); line-height:1.5!important">640,128); line-height:1.5!important">0)); floor.setElasticity(1); floor.setFriction(1); floor.setLayers(NOT_GRABABLE_MASK); },addWalls:function() { var wall1 = 480),128); line-height:1.5!important">0)); wall1.setElasticity(1); wall1.setFriction(1); wall1.setLayers(NOT_GRABABLE_MASK); var wall2 = 0)); wall2.setElasticity(1); wall2.setFriction(1); wall2.setLayers(NOT_GRABABLE_MASK); },createPhysicsSprite:function( pos ) { new cp.Body(1,cp.momentForBox(48,128); line-height:1.5!important">108) ); body.setPos( pos ); this.space.addBody( body ); new cp.BoxShape( body,128); line-height:1.5!important">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; }