!function(){vara={},b={};!function(){b._inertiaScale=4;vara=1;b.create=function(a){varb={id:o.nextId(),type:"body",label:"Body",angle:0,vertices:z.fromPath("L 0 0 L 40 0 L 40 40 L 0 40"),position:{x:0,y:0},force:{x:0,y:0},torque:0,positionImpulse:{x:0,y:0},constraintImpulse:{x:0,y:0,angle: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,timeScale:1,render:{visible:!0,sprite:{xScale:1,yScale:1},lineWidth:1.5}},d=o.extend(b,a);returnc(d,a),d},b.nextGroupId=function(){returna++};varc=function(a,c){a.bounds=a.bounds||x.create(a.vertices),a.positionPrev=a.positionPrev||y.clone(a.position),a.anglePrev=a.anglePrev||a.angle,b.setVertices(a,a.vertices),b.setStatic(a,a.isStatic),t.set(a,a.isSleeping),z.rotate(a.vertices,a.angle,a.position),w.rotate(a.axes,a.angle),a.axes=c.axes||a.axes,a.area=c.area||a.area,a.mass=c.mass||a.mass,a.inertia=c.inertia||a.inertia,a.inverseMass=1/a.mass,a.inverseInertia=1/a.inertia;vard=a.isStatic?"#eeeeee":o.choose(["#556270","#4ECDC4","#C7F464","#FF6B6B","#C44D58"]),e=o.shadeColor(d,-20);a.render.fillStyle=a.render.fillStyle||d,a.render.strokeStyle=a.render.strokeStyle||e};b.setStatic=function(a,b){a.isStatic=b,b&&(a.restitution=0,a.friction=1,a.mass=a.inertia=a.density=1/0,a.inverseMass=a.inverseInertia=0,a.render.lineWidth=1,a.positionPrev.x=a.position.x,a.positionPrev.y=a.position.y,a.anglePrev=a.angle,a.angularVelocity=0,a.speed=0,a.angularSpeed=0,a.motion=0)},b.setVertices=function(a,c){a.vertices=c[0].body===a?c:z.create(c,a),a.axes=w.fromVertices(a.vertices),a.area=z.area(a.vertices),a.mass=a.density*a.area,a.inverseMass=1/a.mass;vard=z.centre(a.vertices);z.translate(a.vertices,d,-1),a.inertia=b._inertiaScale*z.inertia(a.vertices,a.mass),a.inverseInertia=1/a.inertia,z.translate(a.vertices,a.position),x.update(a.bounds,a.vertices,a.velocity)},b.setPosition=function(a,b){varc=y.sub(b,a.position);a.position.x=b.x,a.position.y=b.y,a.positionPrev.x+=c.x,a.positionPrev.y+=c.y,z.translate(a.vertices,c),x.update(a.bounds,a.vertices,a.velocity)},b.setAngle=function(a,b){varc=b-a.angle;a.angle=b,a.anglePrev+=c,z.rotate(a.vertices,c,a.position),w.rotate(a.axes,c),x.update(a.bounds,a.vertices,a.velocity)},b.setVelocity=function(a,b){a.positionPrev.x=a.position.x-b.x,a.positionPrev.y=a.position.y-b.y,a.velocity.x=b.x,a.velocity.y=b.y,a.speed=y.magnitude(a.velocity)},b.setAngularVelocity=function(a,b){a.anglePrev=a.angle-b,a.angularVelocity=b,a.angularSpeed=Math.abs(a.angularVelocity)},b.translate=function(a,c){b.setPosition(a,y.add(a.position,c))},b.rotate=function(a){b.setAngle(a,a.angle+angle)},b.scale=function(a,b,c,d){z.scale(a.vertices,b,c,d),a.axes=w.fromVertices(a.vertices),a.area=z.area(a.vertices),a.mass=a.density*a.area,a.inverseMass=1/a.mass,z.translate(a.vertices,{x:-a.position.x,y:-a.position.y}),a.inertia=z.inertia(a.vertices,a.mass),a.inverseInertia=1/a.inertia,z.translate(a.vertices,{x:a.position.x,y:a.position.y}),x.update(a.bounds,a.vertices,a.velocity)},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,f){for(varg=0;g<a.length;g++){varh=a[g];h.isStatic||h.isSleeping||h.bounds.max.x<f.min.x||h.bounds.min.x>f.max.x||h.bounds.max.y<f.min.y||h.bounds.min.y>f.max.y||b.update(h,c,d,e)}},b.update=function(a,b,c,d){vare=Math.pow(b*c*a.timeScale,2),f=1-a.frictionAir*c*a.timeScale,g=a.position.x-a.positionPrev.x,h=a.position.y-a.positionPrev.y;a.velocity.x=g*f*d+a.force.x/a.mass*e,a.velocity.y=h*f*d+a.force.y/a.mass*e,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)*f*d+a.torque/a.inertia*e,a.anglePrev=a.angle,a.angle+=a.angularVelocity,a.speed=y.magnitude(a.velocity),a.angularSpeed=Math.abs(a.angularVelocit
!h.isStatic&&i.motion>a&&t.set(h,!1)}}}},t.set=function(a,b){b?(a.isSleeping=!0,a.sleepCounter=a.sleepThreshold,a.positionImpulse.x=0,a.positionImpulse.y=0,a.positionPrev.x=a.position.x,a.positionPrev.y=a.position.y,a.anglePrev=a.angle,a.speed=0,a.angularSpeed=0,a.motion=0):(a.isSleeping=!1,a.sleepCounter=0)}}();varu={};!function(){u.rectangle=function(a,c,d,e,f){f=f||{};varg={label:"Rectangle Body",position:{x:a,y:c},vertices:z.fromPath("L 0 0 L "+d+" 0 L "+d+" "+e+" L 0 "+e)};if(f.chamfer){varh=f.chamfer;g.vertices=z.chamfer(g.vertices,h.radius,h.quality,h.qualityMin,h.qualityMax),deletef.chamfer}returnb.create(o.extend({},g,f))},u.trapezoid=function(a,c,d,e,f,g){g=g||{},f*=.5;varh=(1-2*f)*d,i=d*f,j=i+h,k=j+i,l={label:"Trapezoid Body",position:{x:a,y:c},vertices:z.fromPath("L 0 0 L "+i+" "+-e+" L "+j+" "+-e+" L "+k+" 0")};if(g.chamfer){varm=g.chamfer;l.vertices=z.chamfer(l.vertices,m.radius,m.quality,m.qualityMin,m.qualityMax),deleteg.chamfer}returnb.create(o.extend({},l,g))},u.circle=function(a,b,c,d,e){d=d||{},d.label="Circle Body",e=e||25;varf=Math.ceil(Math.max(10,Math.min(e,c)));returnf%2===1&&(f+=1),d.circleRadius=c,u.polygon(a,b,f,c,d)},u.polygon=function(a,c,d,e,f){if(f=f||{},3>d)returnu.circle(a,c,e,f);for(varg=2*Math.PI/d,h="",i=.5*g,j=0;d>j;j+=1){vark=i+j*g,l=Math.cos(k)*e,m=Math.sin(k)*e;h+="L "+l.toFixed(3)+" "+m.toFixed(3)+" "}varn={label:"Polygon Body",position:{x:a,y:c},vertices:z.fromPath(h)};if(f.chamfer){varp=f.chamfer;n.vertices=z.chamfer(n.vertices,p.radius,p.quality,p.qualityMin,p.qualityMax),deletef.chamfer}returnb.create(o.extend({},n,f))}}();varv={};!function(){v.stack=function(a,d,e,f,g,h,i){for(varj,k=c.create({label:"Stack"}),l=a,m=d,n=0,o=0;f>o;o++){for(varp=0,q=0;e>q;q++){varr=i(l,m,q,o,j,n);if(r){vars=r.bounds.max.y-r.bounds.min.y,t=r.bounds.max.x-r.bounds.min.x;s>p&&(p=s),b.translate(r,{x:.5*t,y:.5*s}),l=r.bounds.max.x+g,c.addBody(k,r),j=r,n+=1}}m+=p+h,l=a}returnk},v.chain=function(a,b,d,e,f,g){for(varh=a.bodies,i=1;i<h.length;i++){varj=h[i-1],k=h[i],l=j.bounds.max.y-j.bounds.min.y,n=j.bounds.max.x-j.bounds.min.x,p=k.bounds.max.y-k.bounds.min.y,q=k.bounds.max.x-k.bounds.min.x,r={bodyA:j,pointA:{x:n*b,y:l*d},bodyB:k,pointB:{x:q*e,y:p*f}},s=o.extend(r,g);c.addConstraint(a,m.create(s))}returna.label+=" Chain",a},v.mesh=function(a,b,d,e,f){varg,h,i,j,k,l=a.bodies;for(g=0;d>g;g++){for(h=0;b>h;h++)h>0&&(i=l[h-1+g*b],j=l[h+g*b],c.addConstraint(a,m.create(o.extend({bodyA:i,bodyB:j},f))));for(h=0;b>h;h++)g>0&&(i=l[h+(g-1)*b],j=l[h+g*b],c.addConstraint(a,m.create(o.extend({bodyA:i,bodyB:j},f))),e&&h>0&&(k=l[h-1+(g-1)*b],c.addConstraint(a,m.create(o.extend({bodyA:k,bodyB:j},f)))),e&&b-1>h&&(k=l[h+1+(g-1)*b],c.addConstraint(a,m.create(o.extend({bodyA:k,bodyB:j},f)))))}returna.label+=" Mesh",a},v.pyramid=function(a,c,d,e,f,g,h){returnv.stack(a,c,d,e,f,g,function(c,g,i,j,k,l){varm=Math.min(e,Math.ceil(d/2)),n=k?k.bounds.max.x-k.bounds.min.x:0;if(!(j>m)){j=m-j;varo=j,p=d-1-j;if(!(o>i||i>p)){1===l&&b.translate(k,{x:(i+(d%2===1?1:-1))*n,y:0});varq=k?i*n:0;returnh(a+q+i*f,g,i,j,k,l)}}})},v.newtonsCradle=function(a,b,d,e,f){for(varg=c.create({label:"Newtons Cradle"}),h=0;d>h;h++){vari=1.9,j=u.circle(a+h*e*i,b+f,e,{inertia:99999,restitution:1,friction:0,frictionAir:1e-4,slop:.01}),k=m.create({pointA:{x:a+h*e*i,y:b},bodyB:j});c.addBody(g,j),c.addConstraint(g,k)}returng},v.car=function(a,d,e,f,g){varh=b.nextGroupId(),i=-20,j=.5*-e+i,k=.5*e-i,l=0,n=c.create({label:"Car"}),o=u.trapezoid(a,d,e,f,.3,{groupId:h,friction:.01,chamfer:{radius:10}}),p=u.circle(a+j,d+l,g,{groupId:h,restitution:.5,friction:.9,density:.01}),q=u.circle(a+k,d+l,g,{groupId:h,restitution:.5,friction:.9,density:.01}),r=m.create({bodyA:o,pointA:{x:j,y:l},bodyB:p,stiffness:.5}),s=m.create({bodyA:o,pointA:{x:k,y:l},bodyB:q,stiffness:.5});returnc.addBody(n,o),c.addBody(n,p),c.addBody(n,q),c.addConstraint(n,r),c.addConstraint(n,s),n},v.softBody=function(a,b,c,d,e,f,g,h,i,j){i=o.extend({inertia:1/0},i),j=o.extend({stiffness:.4},j);vark=v.stack(a,b,c,d,e,f,function(a,b){returnu.circle(a,b,h,i)});returnv.mes