cocos2d-js]chipmunk例子(一)

前端之家收集整理的这篇文章主要介绍了cocos2d-js]chipmunk例子(一)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. initChipmunk:function() {
  2. this.space = new cp.Space();
  3. this.setupDebugNode();
  4. //设置空间内刚体间联系的迭代计算器个数和弹性关系迭代计算器个数.
  5. chipmunk使用迭代计算器计算出空间内物体的受力关系.
  6. 它建立一个大列表存放物体间所有的碰撞,连接等相互影响关系.根据实际情况传递某些相互作用.
  7. 传递相互作用的数取决于迭代器的个数,每次迭代都使计算结果更精确.
  8. 如果进行了过多的迭代,虽然物理影响效果会更好,但是这也会消耗过多的cpu处理时间.
  9. 如果进行的迭代太少,物理模拟效果会不精确,或者使本该静止的物体没能静止下来.
  10. 使用迭代器的个数在于平衡cpu性能和物理模拟精确度之间权衡.
  11. this.space.iterations = 60;
  12. 设置空间的重力向量
  13. this.space.gravity = cp.v(0,-500);
  14. 休眠临界时间
  15. this.space.sleepTimeThreshold = 0.5;
  16. this.space.collisionSlop = 0.5;
  17. this.addFloor();
  18. this.addWalls();
  19. var width = 50;
  20. var height = var mass = width * height * 1/1000;
  21. var rock = this.space.addBody(new cp.Body(mass,cp.momentForBox(mass,width,height)));
  22. rock.setPos(cp.v(500,100));
  23. rock.setAngle(1);
  24. var shape = this.space.addShape(new cp.BoxShape(rock,height));
  25. shape.setFriction(0.3);
  26. shape.setElasticity(0.3);
  27. var radius = 20;
  28. mass = 3;
  29. var body = new cp.Body(mass,cp.momentForCircle(mass,radius,cp.v(0))));
  30. body.setPos(cp.v(200,(2 * radius + 5)));
  31. var circle = new cp.CircleShape(body,128); line-height:1.5!important">0)));
  32. circle.setElasticity(0.8);
  33. circle.setFriction(1);
  34. 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));
  35. ramp.setElasticity(1); 弹性
  36. ramp.setFriction( 摩擦
  37. ramp.setLayers(NOT_GRABABLE_MASK);
  38. var sprite = this.createPhysicsSprite(cc.p(400,128); line-height:1.5!important">200));
  39. this.addChild(sprite);
  40. },addFloor:function() {
  41. var floor = 0),128); line-height:1.5!important">640,128); line-height:1.5!important">0));
  42. floor.setElasticity(1);
  43. floor.setFriction(1);
  44. floor.setLayers(NOT_GRABABLE_MASK);
  45. },addWalls:function() {
  46. var wall1 = 480),128); line-height:1.5!important">0));
  47. wall1.setElasticity(1);
  48. wall1.setFriction(1);
  49. wall1.setLayers(NOT_GRABABLE_MASK);
  50. var wall2 = 0));
  51. wall2.setElasticity(1);
  52. wall2.setFriction(1);
  53. wall2.setLayers(NOT_GRABABLE_MASK);
  54. },createPhysicsSprite:function( pos ) {
  55. new cp.Body(1,cp.momentForBox(48,128); line-height:1.5!important">108) );
  56. body.setPos( pos );
  57. this.space.addBody( body );
  58. new cp.BoxShape( body,128); line-height:1.5!important">108);
  59. shape.setElasticity( 0.5 );
  60. shape.setFriction( 0.5 );
  61. this.space.addShape( shape );
  62. var sprite = cc.PhysicsSprite.create(res.b_ball_01);
  63. sprite.setBody( body );
  64. return sprite;
  65. }

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