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; }