Advertisement
Guest User

Box2Dweb default example

a guest
Oct 25th, 2014
1,162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.96 KB | None | 0 0
  1. <html>
  2.    <head>
  3.       <title>Box2dWeb example</title>
  4.    </head>
  5.    <body onload="init();">
  6.       <canvas id="canvas" width="600" height="400"></canvas>
  7.    </body>
  8.    <script type="text/javascript" src="Box2dWeb-2.1.a.3.min.js"></script>
  9.    <script type="text/javascript">
  10.       var world;
  11.      
  12.       function init() {
  13.          var   b2Vec2 = Box2D.Common.Math.b2Vec2
  14.             ,   b2BodyDef = Box2D.Dynamics.b2BodyDef
  15.             ,   b2Body = Box2D.Dynamics.b2Body
  16.             ,   b2FixtureDef = Box2D.Dynamics.b2FixtureDef
  17.             ,   b2Fixture = Box2D.Dynamics.b2Fixture
  18.             ,   b2World = Box2D.Dynamics.b2World
  19.             ,   b2MassData = Box2D.Collision.Shapes.b2MassData
  20.             ,   b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape
  21.             ,   b2CircleShape = Box2D.Collision.Shapes.b2CircleShape
  22.             ,   b2DebugDraw = Box2D.Dynamics.b2DebugDraw
  23.             ;
  24.          
  25.          world = new b2World(
  26.                new b2Vec2(0, 10)    //gravity
  27.             ,  true                 //allow sleep
  28.          );
  29.          
  30.          var fixDef = new b2FixtureDef;
  31.          fixDef.density = 1.0;
  32.          fixDef.friction = 0.5;
  33.          fixDef.restitution = 0.2;
  34.          
  35.          var bodyDef = new b2BodyDef;
  36.          
  37.          //create ground
  38.          bodyDef.type = b2Body.b2_staticBody;
  39.          bodyDef.position.x = 9;
  40.          bodyDef.position.y = 13;
  41.          fixDef.shape = new b2PolygonShape;
  42.          fixDef.shape.SetAsBox(10, 0.5);
  43.          world.CreateBody(bodyDef).CreateFixture(fixDef);
  44.          
  45.          //create some objects
  46.          bodyDef.type = b2Body.b2_dynamicBody;
  47.          for(var i = 0; i < 10; ++i) {
  48.            if(Math.random() > 0.5) {
  49.                fixDef.shape = new b2PolygonShape;
  50.                fixDef.shape.SetAsBox(
  51.                      Math.random() + 0.1 //half width
  52.                   ,  Math.random() + 0.1 //half height
  53.                );
  54.             } else {
  55.                fixDef.shape = new b2CircleShape(
  56.                   Math.random() + 0.1 //radius
  57.                );
  58.             }
  59.             bodyDef.position.x = Math.random() * 10;
  60.             bodyDef.position.y = Math.random() * 10;
  61.             world.CreateBody(bodyDef).CreateFixture(fixDef);
  62.          }
  63.          
  64.          //setup debug draw
  65.          var debugDraw = new b2DebugDraw();
  66.             debugDraw.SetSprite(document.getElementById("canvas").getContext("2d"));
  67.             debugDraw.SetDrawScale(30.0);
  68.             debugDraw.SetFillAlpha(0.3);
  69.             debugDraw.SetLineThickness(1.0);
  70.             debugDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit);
  71.             world.SetDebugDraw(debugDraw);
  72.          
  73.          window.setInterval(update, 1000 / 60);
  74.       };
  75.      
  76.       function update() {
  77.          world.Step(
  78.                1 / 60   //frame-rate
  79.             ,  10       //velocity iterations
  80.             ,  10       //position iterations
  81.          );
  82.          world.DrawDebugData();
  83.          world.ClearForces();
  84.       };
  85.    
  86.    </script>
  87.    
  88.    
  89. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement