!function(){vara={},b={};!function(){vara=0,c=1;b.create=function(a){varc={id:b.nextId(),angle:0,position:{x:0,y:0},force:{x:0,y:0},torque:0,positionImpulse:{x:0,y:0},speed:0,angularSpeed:0,velocity:{x:0,y:0},angularVelocity:0,isStatic:!1,isSleeping:!1,motion:0,sleepThreshold:60,density:.001,restitution:0,friction:.1,frictionAir:.01,groupId:0,slop:.05,render:{visible:!0,sprite:null,path:"L 0 0 L 40 0 L 40 40 L 0 40",lineWidth:1.5}},d=n.extend(c,a);returnb.updateProperties(d),d},b.nextId=function(){returna++},b.nextGroupId=function(){returnc++},b.updateProperties=function(a){a.vertices=a.vertices||y.fromPath(a.render.path),a.axes=a.axes||v.fromVertices(a.vertices),a.area=y.area(a.vertices),a.bounds=w.create(a.vertices),a.mass=a.mass||a.density*a.area,a.inverseMass=1/a.mass,a.inertia=a.inertia||y.inertia(a.vertices,a.mass),a.inverseInertia=1/a.inertia,a.positionPrev=a.positionPrev||{x:a.position.x,y:a.position.y},a.anglePrev=a.anglePrev||a.angle,a.render.fillStyle=a.render.fillStyle||(a.isStatic?"#eeeeee":n.choose(["#556270","#4ECDC4","#C7F464","#FF6B6B","#C44D58"])),a.render.strokeStyle=a.render.strokeStyle||n.shadeColor(a.render.fillStyle,-20),y.create(a.vertices,a);varb=y.centre(a.vertices);y.translate(a.vertices,a.position),y.translate(a.vertices,b,-1),y.rotate(a.vertices,a.angle,a.position),v.rotate(a.axes,a.angle),w.update(a.bounds,a.vertices,a.velocity),a.isStatic&&(a.restitution=0,a.friction=1,a.mass=a.inertia=a.density=1/0,a.inverseMass=a.inverseInertia=0,a.render.lineWidth=1),s.set(a,a.isSleeping)},b.resetForcesAll=function(a){for(varb=0;b<a.length;b++){varc=a[b];c.force.x=0,c.force.y=0,c.torque=0}},b.applyGravityAll=function(a,b){for(varc=0;c<a.length;c++){vard=a[c];d.isStatic||d.isSleeping||(d.force.y+=d.mass*b.y*.001,d.force.x+=d.mass*b.x*.001)}},b.updateAll=function(a,c,d,e){for(varf=0;f<a.length;f++){varg=a[f];g.isStatic||g.isSleeping||g.bounds.max.x<e.min.x||g.bounds.min.x>e.max.x||g.bounds.max.y<e.min.y||g.bounds.min.y>e.max.y||b.update(g,c,d)}},b.update=function(a,b,c){vard=b*b,e=1-a.frictionAir,f=a.position.x-a.positionPrev.x,g=a.position.y-a.positionPrev.y;a.velocity.x=f*e*c+a.force.x/a.mass*d,a.velocity.y=g*e*c+a.force.y/a.mass*d,a.positionPrev.x=a.position.x,a.positionPrev.y=a.position.y,a.position.x+=a.velocity.x,a.position.y+=a.velocity.y,a.angularVelocity=(a.angle-a.anglePrev)*e*c+a.torque/a.inertia*d,a.anglePrev=a.angle,a.angle+=a.angularVelocity,a.speed=x.magnitude(a.velocity),a.angularSpeed=Math.abs(a.angularVelocity),y.translate(a.vertices,a.velocity),y.rotate(a.vertices,a.angularVelocity,a.position),v.rotate(a.axes,a.angularVelocity),w.update(a.bounds,a.vertices,a.velocity)},b.applyForce=function(a,b,c){a.force.x+=c.x,a.force.y+=c.y;vard={x:b.x-a.position.x,y:b.y-a.position.y};a.torque+=(d.x*c.y-d.y*c.x)*a.inverseInertia},b.translate=function(a,b){a.positionPrev.x+=b.x,a.positionPrev.y+=b.y,a.position.x+=b.x,a.position.y+=b.y,y.translate(a.vertices,b),w.update(a.bounds,a.vertices,a.velocity)},b.rotate=function(a,b){a.anglePrev+=b,a.angle+=b,y.rotate(a.vertices,b,a.position),v.rotate(a.axes,b),w.update(a.bounds,a.vertices,a.velocity)}}();varc={};!function(){vara=0;c.create=function(a){returnn.extend({id:c.nextId(),parent:null,isModified:!1,bodies:[],constraints:[],composites:[]},a)},c.nextId=function(){returna++},c.setModified=function(a,b,d,e){if(a.isModified=b,d&&a.parent&&c.setModified(a.parent,b,d,e),e)for(varf=0;f<a.composites.length;f++){varg=a.composites[f];c.setModified(g,b,d,e)}},c.addComposite=function(a,b){returna.composites.push(b),b.parent=a,c.setModified(a,!0,!0,!1),a},c.addBody=function(a,b){returna.bodies.push(b),c.setModified(a,!0,!0,!1),a},c.removeBody=function(a,b,d){vare=a.bodies.indexOf(b);if(-1!==e&&(c.removeBodyAt(a,e),c.setModified(a,!0,!0,!1)),d)for(varf=0;f<a.composites.length;f++)c.removeBody(a.composites[f],b,!0);returna},c.removeBodyAt=function(a,b){returna.bodies.splice(b,1),c.setModified(a,!0,!0,!1),a},c.addConstraint=function(a,b){returna.constraints.push(b),c.setModified(a,!0,!0,!1),a},c.removeConstraint=function(a,b,d){vare=a.const
returnb/6*(e/f)},y.translate=function(a,b,c){vard;if(c)for(d=0;d<a.length;d++)a[d].x+=b.x*c,a[d].y+=b.y*c;elsefor(d=0;d<a.length;d++)a[d].x+=b.x,a[d].y+=b.y},y.rotate=function(a,b,c){if(0!==b)for(vard=Math.cos(b),e=Math.sin(b),f=0;f<a.length;f++){varg=a[f],h=g.x-c.x,i=g.y-c.y;g.x=c.x+(h*d-i*e),g.y=c.y+(h*e+i*d)}},y.contains=function(a,b){for(varc=0;c<a.length;c++){vard=a[c],e=a[(c+1)%a.length];if((b.x-d.x)*(e.y-d.y)+(b.y-d.y)*(d.x-e.x)>0)return!1}return!0}}();varz={};!function(){z.create=function(a,b){vare,f=window.dat&&window.localStorage;if(!f)returnvoidconsole.log("Could not create GUI. Check dat.gui library is loaded first.");varg=newdat.GUI(b);Resurrect?(e=newResurrect({prefix:"$"}),e.parse=e.resurrect):e=JSON;varh={datGui:g,amount:1,size:40,sides:4,density:.001,restitution:0,friction:.1,frictionAir:.01,renderer:"canvas"},i={addBody:function(){for(varb={density:h.density,friction:h.friction,frictionAir:h.frictionAir,restitution:h.restitution},c=0;c<h.amount;c++)d.addBody(a.world,t.polygon(120+c*h.size+50*c,200,h.sides,h.size,b))},clear:function(){d.clear(a.world,!0),o.clear(a);varb=a.render.controller;b.clear&&b.clear(a.render)},save:function(){localStorage&&e&&localStorage.setItem("world",e.stringify(a.world))},load:function(){varb;localStorage&&e&&(b=e.parse(localStorage.getItem("world"))),b&&o.merge(a,{world:b})}},j=g.addFolder("Metrics");j.add(a.timing,"fps").listen(),a.metrics.extended?(j.add(a.timing,"delta").listen(),j.add(a.timing,"correction").listen(),j.add(a.metrics,"bodies").listen(),j.add(a.metrics,"collisions").listen(),j.add(a.metrics,"pairs").listen(),j.add(a.metrics,"broadEff").listen(),j.add(a.metrics,"midEff").listen(),j.add(a.metrics,"narrowEff").listen(),j.add(a.metrics,"narrowReuse").listen(),j.close()):j.open();vark=g.addFolder("Add Body");k.add(h,"amount",1,5).step(1),k.add(h,"size",5,150).step(1),k.add(h,"sides",1,8).step(1),k.add(h,"density",1e-4,.01).step(.001),k.add(h,"friction",0,1).step(.05),k.add(h,"frictionAir",0,10*h.frictionAir).step(h.frictionAir/10),k.add(h,"restitution",0,1).step(.1),k.add(i,"addBody"),k.open();varl=g.addFolder("World");l.add(i,"load"),l.add(i,"save"),l.add(i,"clear"),l.open();varm=l.addFolder("Gravity");m.add(a.world.gravity,"x",-1,1).step(.01),m.add(a.world.gravity,"y",-1,1).step(.01),m.open();varn=g.addFolder("Engine");n.add(a,"enableSleeping"),n.add(a.broadphase,"current",["grid","bruteForce"]).onFinishChange(function(){c.setModified(a.world,!0,!1,!1)}),n.add(a,"timeScale",.1,2).step(.1),n.add(a,"velocityIterations",1,10).step(1),n.add(a,"positionIterations",1,10).step(1),n.add(a,"enabled"),n.open();varp=g.addFolder("Render");returnp.add(h,"renderer",["canvas","webgl"]).onFinishChange(function(b){varc;"canvas"===b&&(c=A),"webgl"===b&&(c=B),a.render.element.removeChild(a.render.canvas);vard=a.render.options;a.render=c.create({element:a.render.element,options:d}),a.render.options=d,a.input.mouse=r.create(a.render.canvas),a.mouseConstraint.mouse=a.input.mouse}),p.add(a.render.options,"wireframes"),p.add(a.render.options,"showDebug"),p.add(a.render.options,"showPositions"),p.add(a.render.options,"showBroadphase"),p.add(a.render.options,"showBounds"),p.add(a.render.options,"showVelocity"),p.add(a.render.options,"showCollisions"),p.add(a.render.options,"showAxes"),p.add(a.render.options,"showAngleIndicator"),p.add(a.render.options,"showSleeping"),p.add(a.render.options,"showIds"),p.add(a.render.options,"showShadows"),p.add(a.render.options,"enabled"),p.open(),h},z.update=function(a,b){varc;b=b||a.datGui;for(cinb.__folders)z.update(a,b.__folders[c]);for(cinb.__controllers){vard=b.__controllers[c];d.updateDisplay&&d.updateDisplay()}},z.closeAll=function(a){varb=a.datGui;for(varcinb.__folders)b.__folders[c].close()}}();varA={};!function(){A.create=function(b){varc={controller:A,element:null,canvas:null,options:{width:800,height:600,background:"#fafafa",wireframeBackground:"#222",enabled:!0,wireframes:!0,showSleeping:!0,showDebug:!1,showBroadphase:!1,showBounds:!1,showVelocity:!1,showCollisions:!1,showAxes:!1,showPositions:!1,show