Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. physics.createWorld = function() {
  2. var gravity = new b2Vec2(0, 9.8);
  3. this.world = new b2World(gravity, /*allow sleep= */ true);
  4.  
  5. // create two temoporary bodies
  6. var bodyDef = new b2BodyDef;
  7. var fixDef = new b2FixtureDef;
  8.  
  9. bodyDef.type = b2Body.b2_staticBody;
  10. bodyDef.position.x = 100/pxPerMeter;
  11.  
  12.  
  13. A Ball-shooting Machine with Physics Engine
  14.  
  15. 258
  16.  
  17. bodyDef.position.y = 100/pxPerMeter;
  18.  
  19. fixDef.shape = new b2PolygonShape();
  20. fixDef.shape.SetAsBox(20/pxPerMeter, 20/pxPerMeter);
  21.  
  22. this.world.CreateBody(bodyDef).CreateFixture(fixDef);
  23.  
  24. bodyDef.type = b2Body.b2_dynamicBody;
  25. bodyDef.position.x = 200/pxPerMeter;
  26. bodyDef.position.y = 100/pxPerMeter;
  27.  
  28. this.world.CreateBody(bodyDef).CreateFixture(fixDef);
  29. // end of temporary code
  30. }
  31. 5. The update method is the game loop's tick event for the physics engine.
  32. It calculates the world step and refreshes debug draw. The world step
  33. upgrades the physics world. We'll discuss it later:
  34. physics.update = function() {
  35. this.world.Step(1/60, 10, 10);
  36. if (shouldDrawDebug) {
  37. this.world.DrawDebugData();
  38. }
  39. this.world.ClearForces();
  40. };
  41. 6. Before we can refresh the debug draw, we need to set it up. We pass a canvas
  42. reference to the Box2D debug draw instance and configure the drawing settings:
  43. physics.showDebugDraw = function() {
  44. shouldDrawDebug = true;
  45.  
  46. //set up debug draw
  47. var debugDraw = new b2DebugDraw();
  48. debugDraw.SetSprite(document.getElementById("debug-canvas").
  49. getContext("2d"));
  50. debugDraw.SetDrawScale(pxPerMeter);
  51. debugDraw.SetFillAlpha(0.3);
  52. debugDraw.SetLineThickness(1.0);
  53. debugDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_
  54. jointBit);
  55. this.world.SetDebugDraw(debugDraw);
  56. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement