document body style height 400pt var sciptList https cdnjs cloudflare com ajax libs jquery 2 1 1 jquery min js https cdn jsdelivr net npm box 2d web 2 1 3 Box2dWeb 2 1 a 3 min js function loadScripts sc if sc sciptList length 1 main return console log script loaded sc let script document createElement script script type text javascript script src sciptList sc script onload function loadScripts sc 1 document head appendChild script loadScripts 0 let canvas document createElement canvas canvas width 800 canvas height 600 canvas id canvas canvas style border 1pt solid blue document body appendChild canvas function main console log Starting main var b2 Vec2 Box2D Common Math b2Vec2 AABB Box2D Collision b2AABB BodyDef Box2D Dynamics b2BodyDef Body Box2D Dynamics b2Body FixtureDef Box2D Dynamics b2FixtureDef Fixture Box2D Dynamics b2Fixture World Box2D Dynamics b2World MassData Box2D Collision Shapes b2MassData PolygonShape Box2D Collision Shapes b2PolygonShape CircleShape Box2D Collision Shapes b2CircleShape DebugDraw Box2D Dynamics b2DebugDraw MouseJointDef Box2D Dynamics Joints b2MouseJointDef var world new b2 World new b2 Vec2 0 9 8 true Sleep var worldHeight 600 var worldWidth 800 var worldScale 30 0 function getDebugDraw var debugDraw new b2 DebugDraw debugDraw SetSprite document getElementById canvas getContext 2d debugDraw SetDrawScale worldScale debugDraw SetFillAlpha 0 5 debugDraw SetLineThickness 1 0 debugDraw SetFlags b2 DebugDraw e_shapeBit return debugDraw function createFloor world var bodyDef new b2 BodyDef bodyDef type b2 Body b2_staticBody var fixDef new b2 FixtureDef fixDef density 1 0 fixDef friction 0 5 fixDef restitution 0 4 fixDef shape new b2 PolygonShape fixDef shape SetAsBox worldWidth worldScale 2 0 worldScale bodyDef position Set 0 worldHeight 2 worldScale world CreateBody bodyDef CreateFixture fixDef function createCircle world x y r var bodyDef new b2 BodyDef bodyDef type b2 Body b2_dynamicBody var fixDef new b2 FixtureDef fixDef density 1 0 fixDef friction 0 5 fixDef restitution 0 4 fixDef shape new b2 CircleShape r worldScale bodyDef position x x worldScale bodyDef position y y worldScale world CreateBody bodyDef CreateFixture fixDef function createBox world x y w h var bodyDef new b2 BodyDef bodyDef type b2 Body b2_dynamicBody var halfWidth w 2 0 var halfHeight h 2 0 var fixDef new b2 FixtureDef fixDef density 1 0 fixDef friction 0 5 fixDef restitution 0 4 fixDef shape new b2 PolygonShape fixDef shape SetAsBox halfWidth worldScale halfHeight worldScale bodyDef position Set x worldScale y worldScale world CreateBody bodyDef CreateFixture fixDef function randInt maxValue return 0 Math random maxValue btn1 click function var circleRadius 40 createCircle world circleRadius randInt worldWidth 2 circleRadius circleRadius circleRadius btn2 click function var boxLength 80 createBox world boxLength 2 0 randInt worldWidth boxLength boxLength boxLength boxLength btn3 click function deleteBodyArray for var bodyItem world GetBodyList bodyItem bodyItem bodyItem GetNext if bodyItem GetType b2 Body b2_dynamicBody deleteBodyArray push bodyItem for var i in deleteBodyArray world DestroyBody deleteBodyArray i function run world Step 1 60 0 10 10 world DrawDebugData world ClearForces function init createFloor world for i 0 i 10 i createCircle world i 80 40 i 40 40 createBox world i 80 40 i 40 79 79 world SetDebugDraw getDebugDraw function start init window setInterval run 1000 60 0 start
y r var bodyDef new b2 BodyDef bodyDef type b2 Body b2_dynamicBody var fixDef new b2 FixtureDef fixDef density 1 0 fixDef friction 0 5 fixDef restitution 0 4 fixDef shape new b2 CircleShape r worldScale bodyDef position x x worldScale bodyDef position y y worldScale world CreateBody bodyDef CreateFixture fixDef function createBox world x y w h var bodyDef new b2 BodyDef bodyDef type b2 Body b2_dynamicBody var halfWidth w 2 0 var halfHeight h 2 0 var fixDef new b2 FixtureDef fixDef density 1 0 fixDef friction 0 5 fixDef restitution 0 4 fixDef shape new b2 PolygonShape fixDef shape SetAsBox halfWidth worldScale halfHeight worldScale bodyDef position Set x worldScale y worldScale world CreateBody bodyDef CreateFixture fixDef function randInt maxValue return 0 Math random maxValue btn1 click function var circleRadius 40 createCircle world circleRadius randInt worldWidth 2 circleRadius circleRadius circleRadius btn2 click function var boxLength 80 createBox world boxLength 2 0 randInt worldWidth boxLength boxLength boxLength boxLength btn3 click function deleteBodyArray for var bodyItem world GetBodyList bodyItem bodyItem bodyItem GetNext if bodyItem GetType b2 Body b2_dynamicBody deleteBodyArray push bodyItem for var i in deleteBodyArray world DestroyBody deleteBodyArray i function run world Step 1 60 0 10 10 world DrawDebugData world ClearForces function init createFloor world for i 0 i 10 i createCircle world i 80 40 i 40 40 createBox world i 80 40 i 40 79 79 world SetDebugDraw getDebugDraw function start init window setInterval run 1000 60 0 start