Posted By


whoppingchill on 09/12/13

Tagged


Statistics


Viewed 120 times
Favorited by 0 user(s)

4 Balls Collision II


/ Published in: JavaScript
Save to your folder(s)

Simulation of the collision of 4 balls using limejs and box2d.


Copy this code and paste it in your HTML
  1. //set main namespace
  2. goog.provide('collisionLime');
  3. //get requirements
  4. goog.require('lime.Director');
  5. goog.require('lime.Scene');
  6. goog.require('lime.Layer');
  7. goog.require('lime.Circle');
  8. goog.require('box2d.World');
  9. goog.require('box2d.Body');
  10. goog.require('box2d.BodyDef');
  11. goog.require('box2d.CircleDef');
  12. goog.require('box2d.Vec2');
  13.  
  14.  
  15. // entrypoint
  16. collisionLime.start = function(){
  17. // colours
  18. var red = 'rgb(155, 0, 0)';
  19. var green = 'rgb(0, 155, 0)';
  20. var blue = 'rgb(0, 0, 155)';
  21. var yellow = 'rgb(155, 155, 0)';
  22.  
  23. // director
  24. var director = new lime.Director(document.body, 600, 600);
  25. var scene = new lime.Scene();
  26. var layer = new lime.Layer; // photoshop layer; append child
  27. // layer.setPosition(300, 300);
  28. scene.appendChild(layer);
  29. director.replaceScene(scene);
  30.  
  31. // box2d
  32. var gravity = new box2d.Vec2(0, 0); // no gravity in x and y direction
  33. var bounds = new box2d.AABB();
  34. bounds.minVertex.Set(0, 0);
  35. bounds.maxVertex.Set(600, 600);
  36. var world = new box2d.World(bounds, gravity, false);
  37.  
  38. // create a circle
  39. function createCircle(radius, x, y, color, layer) {
  40. // limejs
  41. var circle = (new lime.Circle)
  42. .setFill(color)
  43. .setSize(radius * 2, radius * 2);
  44. layer.appendChild(circle);
  45.  
  46. // box2d
  47. var bodyDef = new box2d.BodyDef;
  48. bodyDef.type = box2d.Body.b2_dynamicBody; // can move and rotate
  49. bodyDef.position.Set(x, y);
  50. bodyDef.awake = true;
  51. bodyDef.angularDamping = 0; // friction without contact - 0.001
  52. bodyDef.userData = circle;
  53.  
  54. var circleDef = new box2d.CircleDef;
  55. circleDef.radius = radius;
  56. circleDef.density = 5; // kg/m^2
  57. circleDef.friction = 0.5; // 0 to 1, 0 means no friction
  58. circleDef.restitution = 0.5; // 0 to 1, 0 means won't bounce
  59. bodyDef.AddShape(circleDef);
  60.  
  61. var body = world.CreateBody(bodyDef);
  62. return body;
  63. }
  64.  
  65. function createRect(x, y, width, height, rotation, layer) {
  66. // limejs
  67. var rect = (new lime.Sprite)
  68. .setFill('#ccc')
  69. .setSize(width, height);
  70. layer.appendChild(rect);
  71.  
  72. // box2d
  73. var bodyDef = new box2d.BodyDef;
  74. bodyDef.type = box2d.Body.b2_staticBody;
  75. bodyDef.position.Set(x, y);
  76. bodyDef.rotation = -rotation / 180 * Math.PI;
  77. bodyDef.userData = rect;
  78.  
  79. var rectDef = new box2d.BoxDef;
  80. rectDef.density = 0; // static object, so not required
  81. rectDef.friction = 1;
  82. rectDef.restitution = 0.9;
  83. rectDef.extents.Set(width / 2, height / 2);
  84. bodyDef.AddShape(rectDef);
  85.  
  86. var body = world.CreateBody(bodyDef);
  87. return body;
  88. }
  89.  
  90. function autoMove(shape, vel_x, vel_y) {
  91. shape._body.SetLinearVelocity(new box2d.Vec2(vel_x, vel_y));
  92. }
  93.  
  94. function updateBody(body){
  95. var rot = body.GetRotation();
  96. var shape = body.GetUserData();
  97. shape.setRotation(-rot / Math.PI * 180);
  98. shape.setPosition(body.GetCenterPosition().x, body.GetCenterPosition().y);
  99. }
  100.  
  101. var circleP1 = createCircle(30, 50, 300, red, layer),
  102. circleP2 = createCircle(30, 300, 50, green, layer),
  103. circleP3 = createCircle(30, 550, 300, blue, layer),
  104. circleP4 = createCircle(30, 300, 550, yellow, layer),
  105. rectLeft = createRect(10, 300, 20, 600, 0, layer),
  106. rectTop = createRect(300, 10, 600, 20, 0, layer),
  107. rectRight = createRect(590, 300, 20, 600, 0, layer),
  108. rectBottom = createRect(300, 590, 600, 20, 0, layer);
  109.  
  110. var vel_x = 50, vel_y = 50;
  111. circleP1.SetLinearVelocity(new box2d.Vec2(vel_x, 0));
  112. circleP2.SetLinearVelocity(new box2d.Vec2(0, vel_y));
  113. circleP3.SetLinearVelocity(new box2d.Vec2(-vel_x, 0));
  114. circleP4.SetLinearVelocity(new box2d.Vec2(0 , -vel_y));
  115.  
  116. lime.scheduleManager.schedule(function(dt) {
  117. world.Step(dt / 60, 6, 2); // 60Hz, velocityIt, positionIt
  118.  
  119. updateBody(circleP1);
  120. updateBody(circleP2);
  121. updateBody(circleP3);
  122. updateBody(circleP4);
  123. updateBody(rectLeft);
  124. updateBody(rectTop);
  125. updateBody(rectRight);
  126. updateBody(rectBottom);
  127. },this);
  128. }
  129.  
  130.  
  131. //this is required for outside access after code is compiled in ADVANCED_COMPILATIONS mode
  132. goog.exportSymbol('collisionLime.start', collisionLime.start);

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.