0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-11-27 09:50:52 -05:00
liabru-matter-js/build/matter.min.js
2015-07-05 15:57:31 +01:00

9 lines
No EOL
66 KiB
JavaScript

/**
* matter.min.js edge-master 2015-07-05
* http://brm.io/matter-js/
* License: MIT
*/
!function(){var a={},b={};!function(){b._inertiaScale=4;var a=1,c=-1,d=1;b.create=function(a){var b={id:o.nextId(),type:"body",label:"Body",parts:[],angle:0,vertices:A.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},totalContacts: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,frictionStatic:.5,frictionAir:.01,collisionFilter:{category:1,mask:4294967295,group:0},slop:.05,timeScale:1,render:{visible:!0,sprite:{xScale:1,yScale:1},lineWidth:1.5}},c=o.extend(b,a);return e(c,a),c},b.nextGroup=function(b){return b?c--:a++},b.nextCategory=function(){return d<<=1};var e=function(a,c){b.set(a,{bounds:a.bounds||x.create(a.vertices),positionPrev:a.positionPrev||z.clone(a.position),anglePrev:a.anglePrev||a.angle,vertices:a.vertices,parts:a.parts||[a],isStatic:a.isStatic,isSleeping:a.isSleeping,parent:a.parent||a}),A.rotate(a.vertices,a.angle,a.position),w.rotate(a.axes,a.angle),x.update(a.bounds,a.vertices,a.velocity),b.set(a,{axes:c.axes||a.axes,area:c.area||a.area,mass:c.mass||a.mass,inertia:c.inertia||a.inertia});var d=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.set=function(a,c,d){var e;"string"==typeof c&&(e=c,c={},c[e]=d);for(e in c)if(d=c[e],c.hasOwnProperty(e))switch(e){case"isStatic":b.setStatic(a,d);break;case"isSleeping":t.set(a,d);break;case"mass":b.setMass(a,d);break;case"density":b.setDensity(a,d);break;case"inertia":b.setInertia(a,d);break;case"vertices":b.setVertices(a,d);break;case"position":b.setPosition(a,d);break;case"angle":b.setAngle(a,d);break;case"velocity":b.setVelocity(a,d);break;case"angularVelocity":b.setAngularVelocity(a,d);break;case"parts":b.setParts(a,d);break;default:a[e]=d}},b.setStatic=function(a,b){for(var c=0;c<a.parts.length;c++){var d=a.parts[c];d.isStatic=b,b&&(d.restitution=0,d.friction=1,d.mass=d.inertia=d.density=1/0,d.inverseMass=d.inverseInertia=0,d.positionPrev.x=d.position.x,d.positionPrev.y=d.position.y,d.anglePrev=d.angle,d.angularVelocity=0,d.speed=0,d.angularSpeed=0,d.motion=0)}},b.setMass=function(a,b){a.mass=b,a.inverseMass=1/a.mass,a.density=a.mass/a.area},b.setDensity=function(a,c){b.setMass(a,c*a.area),a.density=c},b.setInertia=function(a,b){a.inertia=b,a.inverseInertia=1/a.inertia},b.setVertices=function(a,c){a.vertices=c[0].body===a?c:A.create(c,a),a.axes=w.fromVertices(a.vertices),a.area=A.area(a.vertices),b.setMass(a,a.density*a.area);var d=A.centre(a.vertices);A.translate(a.vertices,d,-1),b.setInertia(a,b._inertiaScale*A.inertia(a.vertices,a.mass)),A.translate(a.vertices,a.position),x.update(a.bounds,a.vertices,a.velocity)},b.setParts=function(a,c,d){var e;for(c=c.slice(0),a.parts.length=0,a.parts.push(a),a.parent=a,e=0;e<c.length;e++){var g=c[e];g!==a&&(g.parent=a,a.parts.push(g))}if(1!==a.parts.length){if(d="undefined"!=typeof d?d:!0){var h=[];for(e=0;e<c.length;e++)h=h.concat(c[e].vertices);A.clockwiseSort(h);var i=A.hull(h),j=A.centre(i);b.setVertices(a,i),A.translate(a.vertices,j)}var k=f(a);a.area=k.area,a.parent=a,a.position.x=k.centre.x,a.position.y=k.centre.y,a.positionPrev.x=k.centre.x,a.positionPrev.y=k.centre.y,b.setMass(a,k.mass),b.setInertia(a,k.inertia),b.setPosition(a,k.centre)}},b.setPosition=function(a,b){var c=z.sub(b,a.position);a.positionPrev.x+=c.x,a.positionPrev.y+=c.y;for(var d=0;d<a.parts.length;d++){var e=a.parts[d];e.position.x+=c.x,e.position.y+=c.y,A.translate(e.vertices,c),x.update(e.bounds,e.vertices,a.velocity)}},b.setAngle=function(a,b){var c=b-a.angle;a.anglePrev+=c;for(var d=0;d<a.parts.length;d++){var e=a.parts[d];e.angle+=c,A.rotate(e.vertices,c,a.position),w.rotate(e.axes,c),x.update(e.bounds,e.vertices,a.velocity),d>0&&z.rotateAbout(e.position,c,a.position,e.position)}},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=z.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,z.add(a.position,c))},b.rotate=function(a,c){b.setAngle(a,a.angle+c)},b.scale=function(a,c,d){for(var e=0;e<a.parts.length;e++){var g=a.parts[e];A.scale(g.vertices,c,d,a.position),g.axes=w.fromVertices(g.vertices),a.isStatic||(g.area=A.area(g.vertices),b.setMass(g,a.density*g.area),A.translate(g.vertices,{x:-g.position.x,y:-g.position.y}),b.setInertia(g,A.inertia(g.vertices,g.mass)),A.translate(g.vertices,{x:g.position.x,y:g.position.y})),x.update(g.bounds,g.vertices,a.velocity)}if(!a.isStatic){var h=f(a);a.area=h.area,b.setMass(a,h.mass),b.setInertia(a,h.inertia)}},b.update=function(a,b,c,d){var e=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=z.magnitude(a.velocity),a.angularSpeed=Math.abs(a.angularVelocity);for(var i=0;i<a.parts.length;i++){var j=a.parts[i];A.translate(j.vertices,a.velocity),i>0&&(j.position.x+=a.velocity.x,j.position.y+=a.velocity.y),0!==a.angularVelocity&&(A.rotate(j.vertices,a.angularVelocity,a.position),w.rotate(j.axes,a.angularVelocity),i>0&&z.rotateAbout(j.position,a.angularVelocity,a.position,j.position)),x.update(j.bounds,j.vertices,a.velocity)}},b.applyForce=function(a,b,c){a.force.x+=c.x,a.force.y+=c.y;var d={x:b.x-a.position.x,y:b.y-a.position.y};a.torque+=(d.x*c.y-d.y*c.x)*a.inverseInertia};var f=function(a){for(var b={mass:0,area:0,inertia:0,centre:{x:0,y:0}},c=1===a.parts.length?0:1;c<a.parts.length;c++){var d=a.parts[c];b.mass+=d.mass,b.area+=d.area,b.inertia+=d.inertia,b.centre=z.add(b.centre,z.mult(d.position,1/0!==d.mass?d.mass:1))}return b.centre=z.div(b.centre,1/0!==b.mass?b.mass:a.parts.length),b}}();var c={};!function(){c.create=function(a){return o.extend({id:o.nextId(),type:"composite",parent:null,isModified:!1,bodies:[],constraints:[],composites:[],label:"Composite"},a)},c.setModified=function(a,b,d,e){if(a.isModified=b,d&&a.parent&&c.setModified(a.parent,b,d,e),e)for(var f=0;f<a.composites.length;f++){var g=a.composites[f];c.setModified(g,b,d,e)}},c.add=function(a,b){var d=[].concat(b);q.trigger(a,"beforeAdd",{object:b});for(var e=0;e<d.length;e++){var f=d[e];switch(f.type){case"body":if(f.parent!==f){o.log("Composite.add: skipped adding a compound body part (you must add its parent instead)","warn");break}c.addBody(a,f);break;case"constraint":c.addConstraint(a,f);break;case"composite":c.addComposite(a,f);break;case"mouseConstraint":c.addConstraint(a,f.constraint)}}return q.trigger(a,"afterAdd",{object:b}),a},c.remove=function(a,b,d){var e=[].concat(b);q.trigger(a,"beforeRemove",{object:b});for(var f=0;f<e.length;f++){var g=e[f];switch(g.type){case"body":c.removeBody(a,g,d);break;case"constraint":c.removeConstraint(a,g,d);break;case"composite":c.removeComposite(a,g,d);break;case"mouseConstraint":c.removeConstraint(a,g.constraint)}}return q.trigger(a,"afterRemove",{object:b}),a},c.addComposite=function(a,b){return a.composites.push(b),b.parent=a,c.setModified(a,!0,!0,!1),a},c.removeComposite=function(a,b,d){var e=o.indexOf(a.composites,b);if(-1!==e&&(c.removeCompositeAt(a,e),c.setModified(a,!0,!0,!1)),d)for(var f=0;f<a.composites.length;f++)c.removeComposite(a.composites[f],b,!0);return a},c.removeCompositeAt=function(a,b){return a.composites.splice(b,1),c.setModified(a,!0,!0,!1),a},c.addBody=function(a,b){return a.bodies.push(b),c.setModified(a,!0,!0,!1),a},c.removeBody=function(a,b,d){var e=o.indexOf(a.bodies,b);if(-1!==e&&(c.removeBodyAt(a,e),c.setModified(a,!0,!0,!1)),d)for(var f=0;f<a.composites.length;f++)c.removeBody(a.composites[f],b,!0);return a},c.removeBodyAt=function(a,b){return a.bodies.splice(b,1),c.setModified(a,!0,!0,!1),a},c.addConstraint=function(a,b){return a.constraints.push(b),c.setModified(a,!0,!0,!1),a},c.removeConstraint=function(a,b,d){var e=o.indexOf(a.constraints,b);if(-1!==e&&c.removeConstraintAt(a,e),d)for(var f=0;f<a.composites.length;f++)c.removeConstraint(a.composites[f],b,!0);return a},c.removeConstraintAt=function(a,b){return a.constraints.splice(b,1),c.setModified(a,!0,!0,!1),a},c.clear=function(a,b,d){if(d)for(var e=0;e<a.composites.length;e++)c.clear(a.composites[e],b,!0);return b?a.bodies=a.bodies.filter(function(a){return a.isStatic}):a.bodies.length=0,a.constraints.length=0,a.composites.length=0,c.setModified(a,!0,!0,!1),a},c.allBodies=function(a){for(var b=[].concat(a.bodies),d=0;d<a.composites.length;d++)b=b.concat(c.allBodies(a.composites[d]));return b},c.allConstraints=function(a){for(var b=[].concat(a.constraints),d=0;d<a.composites.length;d++)b=b.concat(c.allConstraints(a.composites[d]));return b},c.allComposites=function(a){for(var b=[].concat(a.composites),d=0;d<a.composites.length;d++)b=b.concat(c.allComposites(a.composites[d]));return b},c.get=function(a,b,d){var e,f;switch(d){case"body":e=c.allBodies(a);break;case"constraint":e=c.allConstraints(a);break;case"composite":e=c.allComposites(a).concat(a)}return e?(f=e.filter(function(a){return a.id.toString()===b.toString()}),0===f.length?null:f[0]):null},c.move=function(a,b,d){return c.remove(a,b),c.add(d,b),a},c.rebase=function(a){for(var b=c.allBodies(a).concat(c.allConstraints(a)).concat(c.allComposites(a)),d=0;d<b.length;d++)b[d].id=o.nextId();return c.setModified(a,!0,!0,!1),a},c.translate=function(a,d,e){for(var f=e?c.allBodies(a):a.bodies,g=0;g<f.length;g++)b.translate(f[g],d);return c.setModified(a,!0,!0,!1),a},c.rotate=function(a,d,e,f){for(var g=Math.cos(d),h=Math.sin(d),i=f?c.allBodies(a):a.bodies,j=0;j<i.length;j++){var k=i[j],l=k.position.x-e.x,m=k.position.y-e.y;b.setPosition(k,{x:e.x+(l*g-m*h),y:e.y+(l*h+m*g)}),b.rotate(k,d)}return c.setModified(a,!0,!0,!1),a},c.scale=function(a,d,e,f,g){for(var h=g?c.allBodies(a):a.bodies,i=0;i<h.length;i++){var j=h[i],k=j.position.x-f.x,l=j.position.y-f.y;b.setPosition(j,{x:f.x+k*d,y:f.y+l*e}),b.scale(j,d,e)}return c.setModified(a,!0,!0,!1),a}}();var d={};!function(){d.create=function(a){var b=c.create(),d={label:"World",gravity:{x:0,y:1},bounds:{min:{x:-1/0,y:-1/0},max:{x:1/0,y:1/0}}};return o.extend(b,d,a)}}();var e={};!function(){e.create=function(a){return{id:e.id(a),vertex:a,normalImpulse:0,tangentImpulse:0}},e.id=function(a){return a.body.id+"_"+a.index}}();var f={};!function(){f.collisions=function(a,b){for(var c=[],d=b.pairs.table,e=0;e<a.length;e++){var g=a[e][0],i=a[e][1];if((!g.isStatic&&!g.isSleeping||!i.isStatic&&!i.isSleeping)&&f.canCollide(g.collisionFilter,i.collisionFilter)&&x.overlaps(g.bounds,i.bounds))for(var j=g.parts.length>1?1:0;j<g.parts.length;j++)for(var k=g.parts[j],m=i.parts.length>1?1:0;m<i.parts.length;m++){var n=i.parts[m];if(k===g&&n===i||x.overlaps(k.bounds,n.bounds)){var o,p=h.id(k,n),q=d[p];o=q&&q.isActive?q.collision:null;var r=l.collides(k,n,o);r.collided&&c.push(r)}}}return c},f.bruteForce=function(a,b){for(var c=[],d=b.pairs.table,e=0;e<a.length;e++)for(var g=e+1;g<a.length;g++){var i=a[e],j=a[g];if((!i.isStatic&&!i.isSleeping||!j.isStatic&&!j.isSleeping)&&f.canCollide(i.collisionFilter,j.collisionFilter)&&x.overlaps(i.bounds,j.bounds)){var k,m=h.id(i,j),n=d[m];k=n&&n.isActive?n.collision:null;var o=l.collides(i,j,k);o.collided&&c.push(o)}}return c},f.canCollide=function(a,b){return a.group===b.group&&0!==a.group?a.group>0:0!==(a.mask&b.category)&&0!==(b.mask&a.category)}}();var g={};!function(){g.create=function(a){var b={controller:g,detector:f.collisions,buckets:{},pairs:{},pairsList:[],bucketWidth:48,bucketHeight:48};return o.extend(b,a)},g.update=function(c,f,g,h){var l,m,n,o,p,q=g.world,r=c.buckets,s=!1;for(l=0;l<f.length;l++){var t=f[l];if((!t.isSleeping||h)&&!(t.bounds.max.x<0||t.bounds.min.x>q.bounds.width||t.bounds.max.y<0||t.bounds.min.y>q.bounds.height)){var u=b(c,t);if(!t.region||u.id!==t.region.id||h){(!t.region||h)&&(t.region=u);var v=a(u,t.region);for(m=v.startCol;m<=v.endCol;m++)for(n=v.startRow;n<=v.endRow;n++){p=d(m,n),o=r[p];var w=m>=u.startCol&&m<=u.endCol&&n>=u.startRow&&n<=u.endRow,x=m>=t.region.startCol&&m<=t.region.endCol&&n>=t.region.startRow&&n<=t.region.endRow;!w&&x&&x&&o&&j(c,o,t),(t.region===u||w&&!x||h)&&(o||(o=e(r,p)),i(c,o,t))}t.region=u,s=!0}}}s&&(c.pairsList=k(c))},g.clear=function(a){a.buckets={},a.pairs={},a.pairsList=[]};var a=function(a,b){var d=Math.min(a.startCol,b.startCol),e=Math.max(a.endCol,b.endCol),f=Math.min(a.startRow,b.startRow),g=Math.max(a.endRow,b.endRow);return c(d,e,f,g)},b=function(a,b){var d=b.bounds,e=Math.floor(d.min.x/a.bucketWidth),f=Math.floor(d.max.x/a.bucketWidth),g=Math.floor(d.min.y/a.bucketHeight),h=Math.floor(d.max.y/a.bucketHeight);return c(e,f,g,h)},c=function(a,b,c,d){return{id:a+","+b+","+c+","+d,startCol:a,endCol:b,startRow:c,endRow:d}},d=function(a,b){return a+","+b},e=function(a,b){var c=a[b]=[];return c},i=function(a,b,c){for(var d=0;d<b.length;d++){var e=b[d];if(!(c.id===e.id||c.isStatic&&e.isStatic)){var f=h.id(c,e),g=a.pairs[f];g?g[2]+=1:a.pairs[f]=[c,e,1]}}b.push(c)},j=function(a,b,c){b.splice(o.indexOf(b,c),1);for(var d=0;d<b.length;d++){var e=b[d],f=h.id(c,e),g=a.pairs[f];g&&(g[2]-=1)}},k=function(a){var b,c,d=[];b=o.keys(a.pairs);for(var e=0;e<b.length;e++)c=a.pairs[b[e]],c[2]>0?d.push(c):delete a.pairs[b[e]];return d}}();var h={};!function(){h.create=function(a,b){var c=a.bodyA,d=a.bodyB,e=a.parentA,f=a.parentB,g={id:h.id(c,d),bodyA:c,bodyB:d,contacts:{},activeContacts:[],separation:0,isActive:!0,timeCreated:b,timeUpdated:b,inverseMass:e.inverseMass+f.inverseMass,friction:Math.min(e.friction,f.friction),frictionStatic:Math.max(e.frictionStatic,f.frictionStatic),restitution:Math.max(e.restitution,f.restitution),slop:Math.max(e.slop,f.slop)};return h.update(g,a,b),g},h.update=function(a,b,c){var d=a.contacts,f=b.supports,g=a.activeContacts,i=b.parentA,j=b.parentB;if(a.collision=b,a.inverseMass=i.inverseMass+j.inverseMass,a.friction=Math.min(i.friction,j.friction),a.frictionStatic=Math.max(i.frictionStatic,j.frictionStatic),a.restitution=Math.max(i.restitution,j.restitution),a.slop=Math.max(i.slop,j.slop),g.length=0,b.collided){for(var k=0;k<f.length;k++){var l=f[k],m=e.id(l),n=d[m];g.push(n?n:d[m]=e.create(l))}a.separation=b.depth,h.setActive(a,!0,c)}else a.isActive===!0&&h.setActive(a,!1,c)},h.setActive=function(a,b,c){b?(a.isActive=!0,a.timeUpdated=c):(a.isActive=!1,a.activeContacts.length=0)},h.id=function(a,b){return a.id<b.id?a.id+"_"+b.id:b.id+"_"+a.id}}();var i={};!function(){var a=1e3;i.create=function(a){return o.extend({table:{},list:[],collisionStart:[],collisionActive:[],collisionEnd:[]},a)},i.update=function(a,b,c){var d,e,f,g,i=a.list,j=a.table,k=a.collisionStart,l=a.collisionEnd,m=a.collisionActive,n=[];for(k.length=0,l.length=0,m.length=0,g=0;g<b.length;g++)d=b[g],d.collided&&(e=h.id(d.bodyA,d.bodyB),n.push(e),f=j[e],f?(f.isActive?m.push(f):k.push(f),h.update(f,d,c)):(f=h.create(d,c),j[e]=f,k.push(f),i.push(f)));for(g=0;g<i.length;g++)f=i[g],f.isActive&&-1===o.indexOf(n,f.id)&&(h.setActive(f,!1,c),l.push(f))},i.removeOld=function(b,c){var d,e,f,g,h=b.list,i=b.table,j=[];for(g=0;g<h.length;g++)d=h[g],e=d.collision,e.bodyA.isSleeping||e.bodyB.isSleeping?d.timeUpdated=c:c-d.timeUpdated>a&&j.push(g);for(g=0;g<j.length;g++)f=j[g]-g,d=h[f],delete i[d.id],h.splice(f,1)},i.clear=function(a){return a.table={},a.list.length=0,a.collisionStart.length=0,a.collisionActive.length=0,a.collisionEnd.length=0,a}}();var j={};!function(){j.ray=function(a,b,c,d){d=d||1e-100;for(var e=z.angle(b,c),f=z.magnitude(z.sub(b,c)),g=.5*(c.x+b.x),h=.5*(c.y+b.y),i=u.rectangle(g,h,f,d,{angle:e}),j=[],k=0;k<a.length;k++){var m=a[k];if(x.overlaps(m.bounds,i.bounds))for(var n=1===m.parts.length?0:1;n<m.parts.length;n++){var o=m.parts[n];if(x.overlaps(o.bounds,i.bounds)){var p=l.collides(o,i);if(p.collided){p.body=p.bodyA=p.bodyB=m,j.push(p);break}}}}return j},j.region=function(a,b,c){for(var d=[],e=0;e<a.length;e++){var f=a[e],g=x.overlaps(f.bounds,b);(g&&!c||!g&&c)&&d.push(f)}return d},j.point=function(a,b){for(var c=[],d=0;d<a.length;d++){var e=a[d];if(x.contains(e.bounds,b))for(var f=1===e.parts.length?0:1;f<e.parts.length;f++){var g=e.parts[f];if(x.contains(g.bounds,b)&&A.contains(g.vertices,b)){c.push(e);break}}}return c}}();var k={};!function(){k._restingThresh=4,k._positionDampen=.9,k._positionWarming=.8,k._frictionNormalMultiplier=5,k.preSolvePosition=function(a){var b,c,d;for(b=0;b<a.length;b++)c=a[b],c.isActive&&(d=c.activeContacts.length,c.collision.parentA.totalContacts+=d,c.collision.parentB.totalContacts+=d)},k.solvePosition=function(a,b){var c,d,e,f,g,h,i,j,l,m=z._temp[0],n=z._temp[1],o=z._temp[2],p=z._temp[3];for(c=0;c<a.length;c++)d=a[c],d.isActive&&(e=d.collision,f=e.parentA,g=e.parentB,h=e.normal,i=z.sub(z.add(g.positionImpulse,g.position,m),z.add(f.positionImpulse,z.sub(g.position,e.penetration,n),o),p),d.separation=z.dot(h,i));for(c=0;c<a.length;c++)d=a[c],!d.isActive||d.separation<0||(e=d.collision,f=e.parentA,g=e.parentB,h=e.normal,l=(d.separation-d.slop)*b,(f.isStatic||g.isStatic)&&(l*=2),f.isStatic||f.isSleeping||(j=k._positionDampen/f.totalContacts,f.positionImpulse.x+=h.x*l*j,f.positionImpulse.y+=h.y*l*j),g.isStatic||g.isSleeping||(j=k._positionDampen/g.totalContacts,g.positionImpulse.x-=h.x*l*j,g.positionImpulse.y-=h.y*l*j))},k.postSolvePosition=function(a){for(var b=0;b<a.length;b++){var c=a[b];if(c.totalContacts=0,0!==c.positionImpulse.x||0!==c.positionImpulse.y){for(var d=0;d<c.parts.length;d++){var e=c.parts[d];A.translate(e.vertices,c.positionImpulse),x.update(e.bounds,e.vertices,c.velocity),e.position.x+=c.positionImpulse.x,e.position.y+=c.positionImpulse.y}c.positionPrev.x+=c.positionImpulse.x,c.positionPrev.y+=c.positionImpulse.y,z.dot(c.positionImpulse,c.velocity)<0?(c.positionImpulse.x=0,c.positionImpulse.y=0):(c.positionImpulse.x*=k._positionWarming,c.positionImpulse.y*=k._positionWarming)}}},k.preSolveVelocity=function(a){var b,c,d,e,f,g,h,i,j,k,l,m,n,o,p=z._temp[0],q=z._temp[1];for(b=0;b<a.length;b++)if(d=a[b],d.isActive)for(e=d.activeContacts,f=d.collision,g=f.parentA,h=f.parentB,i=f.normal,j=f.tangent,c=0;c<e.length;c++)k=e[c],l=k.vertex,m=k.normalImpulse,n=k.tangentImpulse,(0!==m||0!==n)&&(p.x=i.x*m+j.x*n,p.y=i.y*m+j.y*n,g.isStatic||g.isSleeping||(o=z.sub(l,g.position,q),g.positionPrev.x+=p.x*g.inverseMass,g.positionPrev.y+=p.y*g.inverseMass,g.anglePrev+=z.cross(o,p)*g.inverseInertia),h.isStatic||h.isSleeping||(o=z.sub(l,h.position,q),h.positionPrev.x-=p.x*h.inverseMass,h.positionPrev.y-=p.y*h.inverseMass,h.anglePrev-=z.cross(o,p)*h.inverseInertia))},k.solveVelocity=function(a,b){for(var c=b*b,d=z._temp[0],e=z._temp[1],f=z._temp[2],g=z._temp[3],h=z._temp[4],i=z._temp[5],j=0;j<a.length;j++){var l=a[j];if(l.isActive){var m=l.collision,n=m.parentA,p=m.parentB,q=m.normal,r=m.tangent,s=l.activeContacts,t=1/s.length;n.velocity.x=n.position.x-n.positionPrev.x,n.velocity.y=n.position.y-n.positionPrev.y,p.velocity.x=p.position.x-p.positionPrev.x,p.velocity.y=p.position.y-p.positionPrev.y,n.angularVelocity=n.angle-n.anglePrev,p.angularVelocity=p.angle-p.anglePrev;for(var u=0;u<s.length;u++){var v=s[u],w=v.vertex,x=z.sub(w,n.position,e),y=z.sub(w,p.position,f),A=z.add(n.velocity,z.mult(z.perp(x),n.angularVelocity),g),B=z.add(p.velocity,z.mult(z.perp(y),p.angularVelocity),h),C=z.sub(A,B,i),D=z.dot(q,C),E=z.dot(r,C),F=Math.abs(E),G=o.sign(E),H=(1+l.restitution)*D,I=o.clamp(l.separation+D,0,1)*k._frictionNormalMultiplier,J=E,K=1/0;F>l.friction*l.frictionStatic*I*c&&(J=l.friction*G*c,K=F);var L=z.cross(x,q),M=z.cross(y,q),N=n.inverseMass+p.inverseMass+n.inverseInertia*L*L+p.inverseInertia*M*M;if(H*=t/N,J*=t/(1+N),0>D&&D*D>k._restingThresh*c)v.normalImpulse=0,v.tangentImpulse=0;else{var O=v.normalImpulse;v.normalImpulse=Math.min(v.normalImpulse+H,0),H=v.normalImpulse-O;var P=v.tangentImpulse;v.tangentImpulse=o.clamp(v.tangentImpulse+J,-K,K),J=v.tangentImpulse-P}d.x=q.x*H+r.x*J,d.y=q.y*H+r.y*J,n.isStatic||n.isSleeping||(n.positionPrev.x+=d.x*n.inverseMass,n.positionPrev.y+=d.y*n.inverseMass,n.anglePrev+=z.cross(x,d)*n.inverseInertia),p.isStatic||p.isSleeping||(p.positionPrev.x-=d.x*p.inverseMass,p.positionPrev.y-=d.y*p.inverseMass,p.anglePrev-=z.cross(y,d)*p.inverseInertia)}}}}}();var l={};!function(){l.collides=function(b,d,e){var f,g,h,i,j=e,k=!1;if(j){var l=b.parent,m=d.parent,n=l.speed*l.speed+l.angularSpeed*l.angularSpeed+m.speed*m.speed+m.angularSpeed*m.angularSpeed;k=j&&j.collided&&.2>n,i=j}else i={collided:!1,bodyA:b,bodyB:d};if(j&&k){var o=i.axisBody,p=o===b?d:b,q=[o.axes[j.axisNumber]];if(h=a(o.vertices,p.vertices,q),i.reused=!0,h.overlap<=0)return i.collided=!1,i}else{if(f=a(b.vertices,d.vertices,b.axes),f.overlap<=0)return i.collided=!1,i;if(g=a(d.vertices,b.vertices,d.axes),g.overlap<=0)return i.collided=!1,i;f.overlap<g.overlap?(h=f,i.axisBody=b):(h=g,i.axisBody=d),i.axisNumber=h.axisNumber}i.bodyA=b.id<d.id?b:d,i.bodyB=b.id<d.id?d:b,i.collided=!0,i.normal=h.axis,i.depth=h.overlap,i.parentA=i.bodyA.parent,i.parentB=i.bodyB.parent,b=i.bodyA,d=i.bodyB,z.dot(i.normal,z.sub(d.position,b.position))>0&&(i.normal=z.neg(i.normal)),i.tangent=z.perp(i.normal),i.penetration={x:i.normal.x*i.depth,y:i.normal.y*i.depth};var r=c(b,d,i.normal),s=i.supports||[];if(s.length=0,A.contains(b.vertices,r[0])&&s.push(r[0]),A.contains(b.vertices,r[1])&&s.push(r[1]),s.length<2){var t=c(d,b,z.neg(i.normal));A.contains(d.vertices,t[0])&&s.push(t[0]),s.length<2&&A.contains(d.vertices,t[1])&&s.push(t[1])}return s.length<1&&(s=[r[0]]),i.supports=s,i};var a=function(a,c,d){for(var e,f,g=z._temp[0],h=z._temp[1],i={overlap:Number.MAX_VALUE},j=0;j<d.length;j++){if(f=d[j],b(g,a,f),b(h,c,f),e=Math.min(g.max-h.min,h.max-g.min),0>=e)return i.overlap=e,i;e<i.overlap&&(i.overlap=e,i.axis=f,i.axisNumber=j)}return i},b=function(a,b,c){for(var d=z.dot(b[0],c),e=d,f=1;f<b.length;f+=1){var g=z.dot(b[f],c);g>e?e=g:d>g&&(d=g)}a.min=d,a.max=e},c=function(a,b,c){for(var d,e,f,g,h=Number.MAX_VALUE,i=z._temp[0],j=b.vertices,k=a.position,l=0;l<j.length;l++)e=j[l],i.x=e.x-k.x,i.y=e.y-k.y,d=-z.dot(c,i),h>d&&(h=d,f=e);var m=f.index-1>=0?f.index-1:j.length-1;e=j[m],i.x=e.x-k.x,i.y=e.y-k.y,h=-z.dot(c,i),g=e;var n=(f.index+1)%j.length;return e=j[n],i.x=e.x-k.x,i.y=e.y-k.y,d=-z.dot(c,i),h>d&&(g=e),[f,g]}}();var m={};!function(){var a=1e-6,b=.001;m.create=function(b){var c=b;c.bodyA&&!c.pointA&&(c.pointA={x:0,y:0}),c.bodyB&&!c.pointB&&(c.pointB={x:0,y:0});var d=c.bodyA?z.add(c.bodyA.position,c.pointA):c.pointA,e=c.bodyB?z.add(c.bodyB.position,c.pointB):c.pointB,f=z.magnitude(z.sub(d,e));c.length=c.length||f||a;var g={visible:!0,lineWidth:2,strokeStyle:"#666"};return c.render=o.extend(g,c.render),c.id=c.id||o.nextId(),c.label=c.label||"Constraint",c.type="constraint",c.stiffness=c.stiffness||1,c.angularStiffness=c.angularStiffness||0,c.angleA=c.bodyA?c.bodyA.angle:c.angleA,c.angleB=c.bodyB?c.bodyB.angle:c.angleB,c},m.solveAll=function(a,b){for(var c=0;c<a.length;c++)m.solve(a[c],b)},m.solve=function(c,d){var e=c.bodyA,f=c.bodyB,g=c.pointA,h=c.pointB;e&&!e.isStatic&&(c.pointA=z.rotate(g,e.angle-c.angleA),c.angleA=e.angle),f&&!f.isStatic&&(c.pointB=z.rotate(h,f.angle-c.angleB),c.angleB=f.angle);var i=g,j=h;if(e&&(i=z.add(e.position,g)),f&&(j=z.add(f.position,h)),i&&j){var k=z.sub(i,j),l=z.magnitude(k);0===l&&(l=a);var m=(l-c.length)/l,n=z.div(k,l),p=z.mult(k,.5*m*c.stiffness*d*d);if(!(Math.abs(1-l/c.length)<b*d)){var q,r,s,u,v,w,x,y;e&&!e.isStatic?(s={x:i.x-e.position.x+p.x,y:i.y-e.position.y+p.y},e.velocity.x=e.position.x-e.positionPrev.x,e.velocity.y=e.position.y-e.positionPrev.y,e.angularVelocity=e.angle-e.anglePrev,q=z.add(e.velocity,z.mult(z.perp(s),e.angularVelocity)),v=z.dot(s,n),x=e.inverseMass+e.inverseInertia*v*v):(q={x:0,y:0},x=e?e.inverseMass:0),f&&!f.isStatic?(u={x:j.x-f.position.x-p.x,y:j.y-f.position.y-p.y},f.velocity.x=f.position.x-f.positionPrev.x,f.velocity.y=f.position.y-f.positionPrev.y,f.angularVelocity=f.angle-f.anglePrev,r=z.add(f.velocity,z.mult(z.perp(u),f.angularVelocity)),w=z.dot(u,n),y=f.inverseMass+f.inverseInertia*w*w):(r={x:0,y:0},y=f?f.inverseMass:0);var A=z.sub(r,q),B=z.dot(n,A)/(x+y);B>0&&(B=0);var C,D={x:n.x*B,y:n.y*B};e&&!e.isStatic&&(C=z.cross(s,D)*e.inverseInertia*(1-c.angularStiffness),t.set(e,!1),C=o.clamp(C,-.01,.01),e.constraintImpulse.x-=p.x,e.constraintImpulse.y-=p.y,e.constraintImpulse.angle+=C,e.position.x-=p.x,e.position.y-=p.y,e.angle+=C),f&&!f.isStatic&&(C=z.cross(u,D)*f.inverseInertia*(1-c.angularStiffness),t.set(f,!1),C=o.clamp(C,-.01,.01),f.constraintImpulse.x+=p.x,f.constraintImpulse.y+=p.y,f.constraintImpulse.angle-=C,f.position.x+=p.x,f.position.y+=p.y,f.angle-=C)}}},m.postSolveAll=function(a){for(var b=0;b<a.length;b++){for(var c=a[b],d=c.constraintImpulse,e=0;e<c.parts.length;e++){var f=c.parts[e];A.translate(f.vertices,d),e>0&&(f.position.x+=d.x,f.position.y+=d.y),0!==d.angle&&(A.rotate(f.vertices,d.angle,c.position),w.rotate(f.axes,d.angle),e>0&&z.rotateAbout(f.position,d.angle,c.position,f.position)),x.update(f.bounds,f.vertices)}d.angle=0,d.x=0,d.y=0}}}();var n={};!function(){n.create=function(b,d){var e=(b?b.mouse:null)||(d?d.mouse:null);!e&&b&&b.render&&b.render.canvas?e=r.create(b.render.canvas):(e=r.create(),o.log("MouseConstraint.create: options.mouse was undefined, engine.render.canvas was undefined, may not function as expected","warn"));var f=m.create({label:"Mouse Constraint",pointA:e.position,pointB:{x:0,y:0},length:.01,stiffness:.1,angularStiffness:1,render:{strokeStyle:"#90EE90",lineWidth:3}}),g={type:"mouseConstraint",mouse:e,body:null,constraint:f,collisionFilter:{category:1,mask:4294967295,group:0}},h=o.extend(g,d);return q.on(b,"tick",function(){var d=c.allBodies(b.world);n.update(h,d),a(h)}),h},n.update=function(a,b){var c=a.mouse,d=a.constraint,e=a.body;if(0===c.button){if(d.bodyB)t.set(d.bodyB,!1),d.pointA=c.position;else for(var g=0;g<b.length;g++)if(e=b[g],x.contains(e.bounds,c.position)&&f.canCollide(e.collisionFilter,a.collisionFilter))for(var h=e.parts.length>1?1:0;h<e.parts.length;h++){var i=e.parts[h];if(A.contains(i.vertices,c.position)){d.pointA=c.position,d.bodyB=a.body=e,d.pointB={x:c.position.x-e.position.x,y:c.position.y-e.position.y},d.angleB=e.angle,t.set(e,!1),q.trigger(a,"startdrag",{mouse:c,body:e});break}}}else d.bodyB=a.body=null,d.pointB=null,e&&q.trigger(a,"enddrag",{mouse:c,body:e})};var a=function(a){var b=a.mouse,c=b.sourceEvents;c.mousemove&&q.trigger(a,"mousemove",{mouse:b}),c.mousedown&&q.trigger(a,"mousedown",{mouse:b}),c.mouseup&&q.trigger(a,"mouseup",{mouse:b}),r.clearSourceEvents(b)}}();var o={};!function(){o._nextId=0,o._seed=0,o.extend=function(a,b){var c,d,e;"boolean"==typeof b?(c=2,e=b):(c=1,e=!0),d=Array.prototype.slice.call(arguments,c);for(var f=0;f<d.length;f++){var g=d[f];if(g)for(var h in g)e&&g[h]&&g[h].constructor===Object?a[h]&&a[h].constructor!==Object?a[h]=g[h]:(a[h]=a[h]||{},o.extend(a[h],e,g[h])):a[h]=g[h]}return a},o.clone=function(a,b){return o.extend({},b,a)},o.keys=function(a){if(Object.keys)return Object.keys(a);var b=[];for(var c in a)b.push(c);return b},o.values=function(a){var b=[];if(Object.keys){for(var c=Object.keys(a),d=0;d<c.length;d++)b.push(a[c[d]]);return b}for(var e in a)b.push(a[e]);return b},o.shadeColor=function(a,b){var c=parseInt(a.slice(1),16),d=Math.round(2.55*b),e=(c>>16)+d,f=(c>>8&255)+d,g=(255&c)+d;return"#"+(16777216+65536*(255>e?1>e?0:e:255)+256*(255>f?1>f?0:f:255)+(255>g?1>g?0:g:255)).toString(16).slice(1)},o.shuffle=function(a){for(var b=a.length-1;b>0;b--){var c=Math.floor(o.random()*(b+1)),d=a[b];a[b]=a[c],a[c]=d}return a},o.choose=function(a){return a[Math.floor(o.random()*a.length)]},o.isElement=function(a){try{return a instanceof HTMLElement}catch(b){return"object"==typeof a&&1===a.nodeType&&"object"==typeof a.style&&"object"==typeof a.ownerDocument}},o.isArray=function(a){return"[object Array]"===Object.prototype.toString.call(a)},o.clamp=function(a,b,c){return b>a?b:a>c?c:a},o.sign=function(a){return 0>a?-1:1},o.now=function(){var a=window.performance||{};return a.now=function(){return a.now||a.webkitNow||a.msNow||a.oNow||a.mozNow||function(){return+new Date}}(),a.now()},o.random=function(b,c){return b="undefined"!=typeof b?b:0,c="undefined"!=typeof c?c:1,b+a()*(c-b)},o.colorToNumber=function(a){return a=a.replace("#",""),3==a.length&&(a=a.charAt(0)+a.charAt(0)+a.charAt(1)+a.charAt(1)+a.charAt(2)+a.charAt(2)),parseInt(a,16)},o.log=function(a,b){if(console&&console.log&&console.warn)switch(b){case"warn":console.warn("Matter.js:",a);break;case"error":console.log("Matter.js:",a)}},o.nextId=function(){return o._nextId++},o.indexOf=function(a,b){if(a.indexOf)return a.indexOf(b);for(var c=0;c<a.length;c++)if(a[c]===b)return c;return-1};var a=function(){return o._seed=(9301*o._seed+49297)%233280,o._seed/233280}}();var p={};!function(){var a=60,e=1e3/a;p.create=function(b,c){c=o.isElement(b)?c:b,b=o.isElement(b)?b:null;var f={enabled:!0,positionIterations:6,velocityIterations:4,constraintIterations:2,enableSleeping:!1,events:[],timing:{fps:a,timestamp:0,delta:e,correction:1,deltaMin:1e3/a,deltaMax:1e3/(.5*a),timeScale:1,isFixed:!1,frameRequestId:0},broadphase:{controller:g}},h=o.extend(f,c);if(b||h.render){var j={element:b,controller:B};h.render=o.extend(j,h.render)}return h.render&&h.render.controller&&(h.render=h.render.controller.create(h.render)),h.world=d.create(h.world),h.pairs=i.create(),h.broadphase=h.broadphase.controller.create(h.broadphase),h.metrics=h.metrics||{extended:!1},h},p.update=function(a,b,d){d="undefined"!=typeof d?d:1;var e,g=a.world,l=a.timing,n=a.broadphase,o=[];l.timestamp+=b*l.timeScale,l.correction=d;var p={timestamp:a.timing.timestamp};q.trigger(a,"beforeUpdate",p);var r=c.allBodies(g),s=c.allConstraints(g);for(a.enableSleeping&&t.update(r,l.timeScale),h(r,g.gravity),j(r,b,l.timeScale,d,g.bounds),e=0;e<a.constraintIterations;e++)m.solveAll(s,l.timeScale);m.postSolveAll(r),n.controller?(g.isModified&&n.controller.clear(n),n.controller.update(n,r,a,g.isModified),o=n.pairsList):o=r;var u=n.detector(o,a),v=a.pairs,w=l.timestamp;for(i.update(v,u,w),i.removeOld(v,w),a.enableSleeping&&t.afterCollisions(v.list,l.timeScale),v.collisionStart.length>0&&q.trigger(a,"collisionStart",{pairs:v.collisionStart}),k.preSolvePosition(v.list),e=0;e<a.positionIterations;e++)k.solvePosition(v.list,l.timeScale);for(k.postSolvePosition(r),k.preSolveVelocity(v.list),e=0;e<a.velocityIterations;e++)k.solveVelocity(v.list,l.timeScale);return v.collisionActive.length>0&&q.trigger(a,"collisionActive",{pairs:v.collisionActive}),v.collisionEnd.length>0&&q.trigger(a,"collisionEnd",{pairs:v.collisionEnd}),f(r),g.isModified&&c.setModified(g,!1,!1,!0),q.trigger(a,"afterUpdate",p),a},p.render=function(a){var b={timestamp:a.timing.timestamp};q.trigger(a,"beforeRender",b),a.render.controller.world(a),q.trigger(a,"afterRender",b)},p.merge=function(a,b){if(o.extend(a,b),b.world){a.world=b.world,p.clear(a);for(var d=c.allBodies(a.world),e=0;e<d.length;e++){var f=d[e];t.set(f,!1),f.id=o.nextId()}}},p.clear=function(a){var b=a.world;i.clear(a.pairs);var d=a.broadphase;if(d.controller){var e=c.allBodies(b);d.controller.clear(d),d.controller.update(d,e,a,!0)}};var f=function(a){for(var b=0;b<a.length;b++){var c=a[b];c.force.x=0,c.force.y=0,c.torque=0}},h=function(a,b){for(var c=0;c<a.length;c++){var d=a[c];d.isStatic||d.isSleeping||(d.force.y+=d.mass*b.y*.001,d.force.x+=d.mass*b.x*.001)}},j=function(a,c,d,e){for(var f=0;f<a.length;f++){var g=a[f];g.isStatic||g.isSleeping||b.update(g,c,d,e)}}}();var q={};!function(){q.on=function(a,b,c){for(var d,e=b.split(" "),f=0;f<e.length;f++)d=e[f],a.events=a.events||{},a.events[d]=a.events[d]||[],a.events[d].push(c);return c},q.off=function(a,b,c){if(!b)return void(a.events={});
"function"==typeof b&&(c=b,b=o.keys(a.events).join(" "));for(var d=b.split(" "),e=0;e<d.length;e++){var f=a.events[d[e]],g=[];if(c)for(var h=0;h<f.length;h++)f[h]!==c&&g.push(f[h]);a.events[d[e]]=g}},q.trigger=function(a,b,c){var d,e,f,g;if(a.events){c||(c={}),d=b.split(" ");for(var h=0;h<d.length;h++)if(e=d[h],f=a.events[e]){g=o.clone(c,!1),g.name=e,g.source=a;for(var i=0;i<f.length;i++)f[i].apply(a,[g])}}}}();var r={};!function(){r.create=function(b){var c={};return b||o.log("Mouse.create: element was undefined, defaulting to document.body","warn"),c.element=b||document.body,c.absolute={x:0,y:0},c.position={x:0,y:0},c.mousedownPosition={x:0,y:0},c.mouseupPosition={x:0,y:0},c.offset={x:0,y:0},c.scale={x:1,y:1},c.wheelDelta=0,c.button=-1,c.pixelRatio=c.element.getAttribute("data-pixel-ratio")||1,c.sourceEvents={mousemove:null,mousedown:null,mouseup:null,mousewheel:null},c.mousemove=function(b){var d=a(b,c.element,c.pixelRatio),e=b.changedTouches;e&&(c.button=0,b.preventDefault()),c.absolute.x=d.x,c.absolute.y=d.y,c.position.x=c.absolute.x*c.scale.x+c.offset.x,c.position.y=c.absolute.y*c.scale.y+c.offset.y,c.sourceEvents.mousemove=b},c.mousedown=function(b){var d=a(b,c.element,c.pixelRatio),e=b.changedTouches;e?(c.button=0,b.preventDefault()):c.button=b.button,c.absolute.x=d.x,c.absolute.y=d.y,c.position.x=c.absolute.x*c.scale.x+c.offset.x,c.position.y=c.absolute.y*c.scale.y+c.offset.y,c.mousedownPosition.x=c.position.x,c.mousedownPosition.y=c.position.y,c.sourceEvents.mousedown=b},c.mouseup=function(b){var d=a(b,c.element,c.pixelRatio),e=b.changedTouches;e&&b.preventDefault(),c.button=-1,c.absolute.x=d.x,c.absolute.y=d.y,c.position.x=c.absolute.x*c.scale.x+c.offset.x,c.position.y=c.absolute.y*c.scale.y+c.offset.y,c.mouseupPosition.x=c.position.x,c.mouseupPosition.y=c.position.y,c.sourceEvents.mouseup=b},c.mousewheel=function(a){c.wheelDelta=Math.max(-1,Math.min(1,a.wheelDelta||-a.detail)),a.preventDefault()},r.setElement(c,c.element),c},r.setElement=function(a,b){a.element=b,b.addEventListener("mousemove",a.mousemove),b.addEventListener("mousedown",a.mousedown),b.addEventListener("mouseup",a.mouseup),b.addEventListener("mousewheel",a.mousewheel),b.addEventListener("DOMMouseScroll",a.mousewheel),b.addEventListener("touchmove",a.mousemove),b.addEventListener("touchstart",a.mousedown),b.addEventListener("touchend",a.mouseup)},r.clearSourceEvents=function(a){a.sourceEvents.mousemove=null,a.sourceEvents.mousedown=null,a.sourceEvents.mouseup=null,a.sourceEvents.mousewheel=null,a.wheelDelta=0},r.setOffset=function(a,b){a.offset.x=b.x,a.offset.y=b.y,a.position.x=a.absolute.x*a.scale.x+a.offset.x,a.position.y=a.absolute.y*a.scale.y+a.offset.y},r.setScale=function(a,b){a.scale.x=b.x,a.scale.y=b.y,a.position.x=a.absolute.x*a.scale.x+a.offset.x,a.position.y=a.absolute.y*a.scale.y+a.offset.y};var a=function(a,b,c){var d,e,f=b.getBoundingClientRect(),g=document.documentElement||document.body.parentNode||document.body,h=void 0!==window.pageXOffset?window.pageXOffset:g.scrollLeft,i=void 0!==window.pageYOffset?window.pageYOffset:g.scrollTop,j=a.changedTouches;return j?(d=j[0].pageX-f.left-h,e=j[0].pageY-f.top-i):(d=a.pageX-f.left-h,e=a.pageY-f.top-i),{x:d/(b.clientWidth/b.width*c),y:e/(b.clientHeight/b.height*c)}}}();var s={};!function(){if("undefined"!=typeof window){var a=60,b=a,c=1e3/a,d=window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.msRequestAnimationFrame||function(a){window.setTimeout(function(){a(o.now())},c)},e=window.cancelAnimationFrame||window.mozCancelAnimationFrame||window.webkitCancelAnimationFrame||window.msCancelAnimationFrame;s.run=function(a){var c,e=0,f=0,g=[],h=1;!function i(j){var k,l=a.timing,m=1;if(l.frameRequestId=d(i),a.enabled){var n={timestamp:j};q.trigger(a,"beforeTick",n),l.isFixed?k=l.delta:(k=j-c||l.delta,c=j,g.push(k),g=g.slice(-b),k=Math.min.apply(null,g),k=k<l.deltaMin?l.deltaMin:k,k=k>l.deltaMax?l.deltaMax:k,m=k/l.delta,l.delta=k),0!==h&&(m*=l.timeScale/h),0===l.timeScale&&(m=0),h=l.timeScale,f+=1,j-e>=1e3&&(l.fps=f*((j-e)/1e3),e=j,f=0),q.trigger(a,"tick",n),a.world.isModified&&a.render&&a.render.controller&&a.render.controller.clear&&a.render.controller.clear(a.render),p.update(a,k,m),a.render&&p.render(a),q.trigger(a,"afterTick",n)}}()},s.stop=function(a){e(a.timing.frameRequestId)}}}();var t={};!function(){t._motionWakeThreshold=.18,t._motionSleepThreshold=.08,t._minBias=.9,t.update=function(a,b){for(var c=b*b*b,d=0;d<a.length;d++){var e=a[d],f=e.speed*e.speed+e.angularSpeed*e.angularSpeed;if(0===e.force.x&&0===e.force.y){var g=Math.min(e.motion,f),h=Math.max(e.motion,f);e.motion=t._minBias*g+(1-t._minBias)*h,e.sleepThreshold>0&&e.motion<t._motionSleepThreshold*c?(e.sleepCounter+=1,e.sleepCounter>=e.sleepThreshold&&t.set(e,!0)):e.sleepCounter>0&&(e.sleepCounter-=1)}else t.set(e,!1)}},t.afterCollisions=function(a,b){for(var c=b*b*b,d=0;d<a.length;d++){var e=a[d];if(e.isActive){var f=e.collision,g=f.bodyA.parent,h=f.bodyB.parent;if(!(g.isSleeping&&h.isSleeping||g.isStatic||h.isStatic)&&(g.isSleeping||h.isSleeping)){var i=g.isSleeping&&!g.isStatic?g:h,j=i===g?h:g;!i.isStatic&&j.motion>t._motionWakeThreshold*c&&t.set(i,!1)}}}},t.set=function(a,b){var c=a.isSleeping;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,c||q.trigger(a,"sleepStart")):(a.isSleeping=!1,a.sleepCounter=0,c&&q.trigger(a,"sleepEnd"))}}();var u={};!function(){u.rectangle=function(a,c,d,e,f){f=f||{};var g={label:"Rectangle Body",position:{x:a,y:c},vertices:A.fromPath("L 0 0 L "+d+" 0 L "+d+" "+e+" L 0 "+e)};if(f.chamfer){var h=f.chamfer;g.vertices=A.chamfer(g.vertices,h.radius,h.quality,h.qualityMin,h.qualityMax),delete f.chamfer}return b.create(o.extend({},g,f))},u.trapezoid=function(a,c,d,e,f,g){g=g||{},f*=.5;var h=(1-2*f)*d,i=d*f,j=i+h,k=j+i,l={label:"Trapezoid Body",position:{x:a,y:c},vertices:A.fromPath("L 0 0 L "+i+" "+-e+" L "+j+" "+-e+" L "+k+" 0")};if(g.chamfer){var m=g.chamfer;l.vertices=A.chamfer(l.vertices,m.radius,m.quality,m.qualityMin,m.qualityMax),delete g.chamfer}return b.create(o.extend({},l,g))},u.circle=function(a,b,c,d,e){d=d||{},d.label="Circle Body",e=e||25;var f=Math.ceil(Math.max(10,Math.min(e,c)));return f%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)return u.circle(a,c,e,f);for(var g=2*Math.PI/d,h="",i=.5*g,j=0;d>j;j+=1){var k=i+j*g,l=Math.cos(k)*e,m=Math.sin(k)*e;h+="L "+l.toFixed(3)+" "+m.toFixed(3)+" "}var n={label:"Polygon Body",position:{x:a,y:c},vertices:A.fromPath(h)};if(f.chamfer){var p=f.chamfer;n.vertices=A.chamfer(n.vertices,p.radius,p.quality,p.qualityMin,p.qualityMax),delete f.chamfer}return b.create(o.extend({},n,f))},u.fromVertices=function(a,c,d,e,f,g,h){var i,j,k,l,m,n,p,q,r;for(e=e||{},j=[],f="undefined"!=typeof f?f:!1,g="undefined"!=typeof g?g:.01,h="undefined"!=typeof h?h:10,window.decomp||o.log("Bodies.fromVertices: poly-decomp.js required. Could not decompose vertices. Fallback to convex hull.","warn"),o.isArray(d[0])||(d=[d]),q=0;q<d.length;q+=1)if(l=d[q],k=A.isConvex(l),k||!window.decomp)l=k?A.clockwiseSort(l):A.hull(l),j.push({position:{x:a,y:c},vertices:l});else{var s=new decomp.Polygon;for(m=0;m<l.length;m++)s.vertices.push([l[m].x,l[m].y]);s.makeCCW(),g!==!1&&s.removeCollinearPoints(g);var t=s.quickDecomp();for(m=0;m<t.length;m++){var u=t[m],v=[];for(n=0;n<u.vertices.length;n++)v.push({x:u.vertices[n][0],y:u.vertices[n][1]});h>0&&A.area(v)<h||j.push({position:A.centre(v),vertices:v})}}for(m=0;m<j.length;m++)j[m]=b.create(o.extend(j[m],e));if(f){var w=5;for(m=0;m<j.length;m++){var y=j[m];for(n=m+1;n<j.length;n++){var B=j[n];if(x.overlaps(y.bounds,B.bounds)){var C=y.vertices,D=B.vertices;for(p=0;p<y.vertices.length;p++)for(r=0;r<B.vertices.length;r++){var E=z.magnitudeSquared(z.sub(C[(p+1)%C.length],D[r])),F=z.magnitudeSquared(z.sub(C[p],D[(r+1)%D.length]));w>E&&w>F&&(C[p].isInternal=!0,D[r].isInternal=!0)}}}}}return j.length>1?(i=b.create(o.extend({parts:j.slice(0)},e)),b.setPosition(i,{x:a,y:c}),i):j[0]}}();var v={};!function(){v.stack=function(a,d,e,f,g,h,i){for(var j,k=c.create({label:"Stack"}),l=a,m=d,n=0,o=0;f>o;o++){for(var p=0,q=0;e>q;q++){var r=i(l,m,q,o,j,n);if(r){var s=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}else l+=g}m+=p+h,l=a}return k},v.chain=function(a,b,d,e,f,g){for(var h=a.bodies,i=1;i<h.length;i++){var j=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))}return a.label+=" Chain",a},v.mesh=function(a,b,d,e,f){var g,h,i,j,k,l=a.bodies;for(g=0;d>g;g++){for(h=1;b>h;h++)i=l[h-1+g*b],j=l[h+g*b],c.addConstraint(a,m.create(o.extend({bodyA:i,bodyB:j},f)));if(g>0)for(h=0;b>h;h++)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))))}return a.label+=" Mesh",a},v.pyramid=function(a,c,d,e,f,g,h){return v.stack(a,c,d,e,f,g,function(c,g,i,j,k,l){var m=Math.min(e,Math.ceil(d/2)),n=k?k.bounds.max.x-k.bounds.min.x:0;if(!(j>m)){j=m-j;var o=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});var q=k?i*n:0;return h(a+q+i*f,g,i,j,k,l)}}})},v.newtonsCradle=function(a,b,d,e,f){for(var g=c.create({label:"Newtons Cradle"}),h=0;d>h;h++){var i=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)}return g},v.car=function(a,d,e,f,g){var h=b.nextGroup(!0),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,{collisionFilter:{group:h},friction:.01,chamfer:{radius:10}}),p=u.circle(a+j,d+l,g,{collisionFilter:{group:h},restitution:.5,friction:.9,frictionStatic:10,slop:.5,density:.01}),q=u.circle(a+k,d+l,g,{collisionFilter:{group:h},restitution:.5,friction:.9,frictionStatic:10,slop:.5,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});return c.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);var k=v.stack(a,b,c,d,e,f,function(a,b){return u.circle(a,b,h,i)});return v.mesh(k,c,d,g,j),k.label="Soft Body",k}}();var w={};!function(){w.fromVertices=function(a){for(var b={},c=0;c<a.length;c++){var d=(c+1)%a.length,e=z.normalise({x:a[d].y-a[c].y,y:a[c].x-a[d].x}),f=0===e.y?1/0:e.x/e.y;f=f.toFixed(3).toString(),b[f]=e}return o.values(b)},w.rotate=function(a,b){if(0!==b)for(var c=Math.cos(b),d=Math.sin(b),e=0;e<a.length;e++){var f,g=a[e];f=g.x*c-g.y*d,g.y=g.x*d+g.y*c,g.x=f}}}();var x={};!function(){x.create=function(a){var b={min:{x:0,y:0},max:{x:0,y:0}};return a&&x.update(b,a),b},x.update=function(a,b,c){a.min.x=Number.MAX_VALUE,a.max.x=Number.MIN_VALUE,a.min.y=Number.MAX_VALUE,a.max.y=Number.MIN_VALUE;for(var d=0;d<b.length;d++){var e=b[d];e.x>a.max.x&&(a.max.x=e.x),e.x<a.min.x&&(a.min.x=e.x),e.y>a.max.y&&(a.max.y=e.y),e.y<a.min.y&&(a.min.y=e.y)}c&&(c.x>0?a.max.x+=c.x:a.min.x+=c.x,c.y>0?a.max.y+=c.y:a.min.y+=c.y)},x.contains=function(a,b){return b.x>=a.min.x&&b.x<=a.max.x&&b.y>=a.min.y&&b.y<=a.max.y},x.overlaps=function(a,b){return a.min.x<=b.max.x&&a.max.x>=b.min.x&&a.max.y>=b.min.y&&a.min.y<=b.max.y},x.translate=function(a,b){a.min.x+=b.x,a.max.x+=b.x,a.min.y+=b.y,a.max.y+=b.y},x.shift=function(a,b){var c=a.max.x-a.min.x,d=a.max.y-a.min.y;a.min.x=b.x,a.max.x=b.x+c,a.min.y=b.y,a.max.y=b.y+d}}();var y={};!function(){y.pathToVertices=function(b,c){var d,e,f,g,h,i,j,k,l,m,n,o,p=[],q=0,r=0,s=0;c=c||15;var t=function(a,b,c){var d=c%2===1&&c>1;if(!l||a!=l.x||b!=l.y){l&&d?(n=l.x,o=l.y):(n=0,o=0);var e={x:n+a,y:o+b};(d||!l)&&(l=e),p.push(e),r=n+a,s=o+b}},u=function(a){var b=a.pathSegTypeAsLetter.toUpperCase();if("Z"!==b){switch(b){case"M":case"L":case"T":case"C":case"S":case"Q":r=a.x,s=a.y;break;case"H":r=a.x;break;case"V":s=a.y}t(r,s,a.pathSegType)}};for(a(b),f=b.getTotalLength(),i=[],d=0;d<b.pathSegList.numberOfItems;d+=1)i.push(b.pathSegList.getItem(d));for(j=i.concat();f>q;){if(m=b.getPathSegAtLength(q),h=i[m],h!=k){for(;j.length&&j[0]!=h;)u(j.shift());k=h}switch(h.pathSegTypeAsLetter.toUpperCase()){case"C":case"T":case"S":case"Q":case"A":g=b.getPointAtLength(q),t(g.x,g.y,0)}q+=c}for(d=0,e=j.length;e>d;++d)u(j[d]);return p};var a=function(a){for(var b,c,d,e,f,g,h=a.pathSegList,i=0,j=0,k=h.numberOfItems,l=0;k>l;++l){var m=h.getItem(l),n=m.pathSegTypeAsLetter;if(/[MLHVCSQTA]/.test(n))"x"in m&&(i=m.x),"y"in m&&(j=m.y);else switch("x1"in m&&(d=i+m.x1),"x2"in m&&(f=i+m.x2),"y1"in m&&(e=j+m.y1),"y2"in m&&(g=j+m.y2),"x"in m&&(i+=m.x),"y"in m&&(j+=m.y),n){case"m":h.replaceItem(a.createSVGPathSegMovetoAbs(i,j),l);break;case"l":h.replaceItem(a.createSVGPathSegLinetoAbs(i,j),l);break;case"h":h.replaceItem(a.createSVGPathSegLinetoHorizontalAbs(i),l);break;case"v":h.replaceItem(a.createSVGPathSegLinetoVerticalAbs(j),l);break;case"c":h.replaceItem(a.createSVGPathSegCurvetoCubicAbs(i,j,d,e,f,g),l);break;case"s":h.replaceItem(a.createSVGPathSegCurvetoCubicSmoothAbs(i,j,f,g),l);break;case"q":h.replaceItem(a.createSVGPathSegCurvetoQuadraticAbs(i,j,d,e),l);break;case"t":h.replaceItem(a.createSVGPathSegCurvetoQuadraticSmoothAbs(i,j),l);break;case"a":h.replaceItem(a.createSVGPathSegArcAbs(i,j,m.r1,m.r2,m.angle,m.largeArcFlag,m.sweepFlag),l);break;case"z":case"Z":i=b,j=c}("M"==n||"m"==n)&&(b=i,c=j)}}}();var z={};!function(){z.create=function(a,b){return{x:a||0,y:b||0}},z.clone=function(a){return{x:a.x,y:a.y}},z.magnitude=function(a){return Math.sqrt(a.x*a.x+a.y*a.y)},z.magnitudeSquared=function(a){return a.x*a.x+a.y*a.y},z.rotate=function(a,b){var c=Math.cos(b),d=Math.sin(b);return{x:a.x*c-a.y*d,y:a.x*d+a.y*c}},z.rotateAbout=function(a,b,c,d){var e=Math.cos(b),f=Math.sin(b);d||(d={});var g=c.x+((a.x-c.x)*e-(a.y-c.y)*f);return d.y=c.y+((a.x-c.x)*f+(a.y-c.y)*e),d.x=g,d},z.normalise=function(a){var b=z.magnitude(a);return 0===b?{x:0,y:0}:{x:a.x/b,y:a.y/b}},z.dot=function(a,b){return a.x*b.x+a.y*b.y},z.cross=function(a,b){return a.x*b.y-a.y*b.x},z.cross3=function(a,b,c){return(b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x)},z.add=function(a,b,c){return c||(c={}),c.x=a.x+b.x,c.y=a.y+b.y,c},z.sub=function(a,b,c){return c||(c={}),c.x=a.x-b.x,c.y=a.y-b.y,c},z.mult=function(a,b){return{x:a.x*b,y:a.y*b}},z.div=function(a,b){return{x:a.x/b,y:a.y/b}},z.perp=function(a,b){return b=b===!0?-1:1,{x:b*-a.y,y:b*a.x}},z.neg=function(a){return{x:-a.x,y:-a.y}},z.angle=function(a,b){return Math.atan2(b.y-a.y,b.x-a.x)},z._temp=[z.create(),z.create(),z.create(),z.create(),z.create(),z.create()]}();var A={};!function(){A.create=function(a,b){for(var c=[],d=0;d<a.length;d++){var e=a[d],f={x:e.x,y:e.y,index:d,body:b,isInternal:!1};c.push(f)}return c},A.fromPath=function(a,b){var c=/L?\s*([\-\d\.e]+)[\s,]*([\-\d\.e]+)*/gi,d=[];return a.replace(c,function(a,b,c){d.push({x:parseFloat(b),y:parseFloat(c)})}),A.create(d,b)},A.centre=function(a){for(var b,c,d,e=A.area(a,!0),f={x:0,y:0},g=0;g<a.length;g++)d=(g+1)%a.length,b=z.cross(a[g],a[d]),c=z.mult(z.add(a[g],a[d]),b),f=z.add(f,c);return z.div(f,6*e)},A.mean=function(a){for(var b={x:0,y:0},c=0;c<a.length;c++)b.x+=a[c].x,b.y+=a[c].y;return z.div(b,a.length)},A.area=function(a,b){for(var c=0,d=a.length-1,e=0;e<a.length;e++)c+=(a[d].x-a[e].x)*(a[d].y+a[e].y),d=e;return b?c/2:Math.abs(c)/2},A.inertia=function(a,b){for(var c,d,e=0,f=0,g=a,h=0;h<g.length;h++)d=(h+1)%g.length,c=Math.abs(z.cross(g[d],g[h])),e+=c*(z.dot(g[d],g[d])+z.dot(g[d],g[h])+z.dot(g[h],g[h])),f+=c;return b/6*(e/f)},A.translate=function(a,b,c){var d;if(c)for(d=0;d<a.length;d++)a[d].x+=b.x*c,a[d].y+=b.y*c;else for(d=0;d<a.length;d++)a[d].x+=b.x,a[d].y+=b.y;return a},A.rotate=function(a,b,c){if(0!==b){for(var d=Math.cos(b),e=Math.sin(b),f=0;f<a.length;f++){var g=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)}return a}},A.contains=function(a,b){for(var c=0;c<a.length;c++){var d=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},A.scale=function(a,b,c,d){if(1===b&&1===c)return a;d=d||A.centre(a);for(var e,f,g=0;g<a.length;g++)e=a[g],f=z.sub(e,d),a[g].x=d.x+f.x*b,a[g].y=d.y+f.y*c;return a},A.chamfer=function(a,b,c,d,e){b=b||[8],b.length||(b=[b]),c="undefined"!=typeof c?c:-1,d=d||2,e=e||14;for(var f=[],g=0;g<a.length;g++){var h=a[g-1>=0?g-1:a.length-1],i=a[g],j=a[(g+1)%a.length],k=b[g<b.length?g:b.length-1];if(0!==k){var l=z.normalise({x:i.y-h.y,y:h.x-i.x}),m=z.normalise({x:j.y-i.y,y:i.x-j.x}),n=Math.sqrt(2*Math.pow(k,2)),p=z.mult(o.clone(l),k),q=z.normalise(z.mult(z.add(l,m),.5)),r=z.sub(i,z.mult(q,n)),s=c;-1===c&&(s=1.75*Math.pow(k,.32)),s=o.clamp(s,d,e),s%2===1&&(s+=1);for(var t=Math.acos(z.dot(l,m)),u=t/s,v=0;s>v;v++)f.push(z.add(z.rotate(p,u*v),r))}else f.push(i)}return f},A.clockwiseSort=function(a){var b=A.mean(a);return a.sort(function(a,c){return z.angle(b,a)-z.angle(b,c)}),a},A.isConvex=function(a){var b,c,d,e,f=0,g=a.length;if(3>g)return null;for(b=0;g>b;b++)if(c=(b+1)%g,d=(b+2)%g,e=(a[c].x-a[b].x)*(a[d].y-a[c].y),e-=(a[c].y-a[b].y)*(a[d].x-a[c].x),0>e?f|=1:e>0&&(f|=2),3===f)return!1;return 0!==f?!0:null},A.hull=function(a){var b,c,d=[],e=[];for(a=a.slice(0),a.sort(function(a,b){var c=a.x-b.x;return 0!==c?c:a.y-b.y}),c=0;c<a.length;c++){for(b=a[c];e.length>=2&&z.cross3(e[e.length-2],e[e.length-1],b)<=0;)e.pop();e.push(b)}for(c=a.length-1;c>=0;c--){for(b=a[c];d.length>=2&&z.cross3(d[d.length-2],d[d.length-1],b)<=0;)d.pop();d.push(b)}return d.pop(),e.pop(),d.concat(e)}}();var B={};!function(){B.create=function(b){var c={controller:B,element:null,canvas:null,options:{width:800,height:600,pixelRatio:1,background:"#fafafa",wireframeBackground:"#222",hasBounds:!1,enabled:!0,wireframes:!0,showSleeping:!0,showDebug:!1,showBroadphase:!1,showBounds:!1,showVelocity:!1,showCollisions:!1,showSeparations:!1,showAxes:!1,showPositions:!1,showAngleIndicator:!1,showIds:!1,showShadows:!1,showVertexNumbers:!1,showConvexHulls:!1,showInternalEdges:!1}},d=o.extend(c,b);return d.canvas=d.canvas||a(d.options.width,d.options.height),d.context=d.canvas.getContext("2d"),d.textures={},d.bounds=d.bounds||{min:{x:0,y:0},max:{x:d.options.width,y:d.options.height}},1!==d.options.pixelRatio&&B.setPixelRatio(d,d.options.pixelRatio),o.isElement(d.element)?d.element.appendChild(d.canvas):o.log("Render.create: options.element was undefined, render.canvas was created but not appended","warn"),d},B.setPixelRatio=function(a,c){var d=a.options,e=a.canvas;"auto"===c&&(c=b(e)),d.pixelRatio=c,e.setAttribute("data-pixel-ratio",c),e.width=d.width*c,e.height=d.height*c,e.style.width=d.width+"px",e.style.height=d.height+"px",a.context.scale(c,c)},B.world=function(a){var b,d=a.render,f=a.world,h=d.canvas,i=d.context,j=d.options,k=c.allBodies(f),l=c.allConstraints(f),m=j.wireframes?j.wireframeBackground:j.background,n=[],o=[];if(d.currentBackground!==m&&e(d,m),i.globalCompositeOperation="source-in",i.fillStyle="transparent",i.fillRect(0,0,h.width,h.height),i.globalCompositeOperation="source-over",j.hasBounds){var p=d.bounds.max.x-d.bounds.min.x,q=d.bounds.max.y-d.bounds.min.y,r=p/j.width,s=q/j.height;for(b=0;b<k.length;b++){var t=k[b];x.overlaps(t.bounds,d.bounds)&&n.push(t)}for(b=0;b<l.length;b++){var u=l[b],v=u.bodyA,w=u.bodyB,y=u.pointA,A=u.pointB;v&&(y=z.add(v.position,u.pointA)),w&&(A=z.add(w.position,u.pointB)),y&&A&&(x.contains(d.bounds,y)||x.contains(d.bounds,A))&&o.push(u)}i.scale(1/r,1/s),i.translate(-d.bounds.min.x,-d.bounds.min.y)}else o=l,n=k;!j.wireframes||a.enableSleeping&&j.showSleeping?B.bodies(a,n,i):(j.showConvexHulls&&B.bodyConvexHulls(a,n,i),B.bodyWireframes(a,n,i)),j.showBounds&&B.bodyBounds(a,n,i),(j.showAxes||j.showAngleIndicator)&&B.bodyAxes(a,n,i),j.showPositions&&B.bodyPositions(a,n,i),j.showVelocity&&B.bodyVelocity(a,n,i),j.showIds&&B.bodyIds(a,n,i),j.showSeparations&&B.separations(a,a.pairs.list,i),j.showCollisions&&B.collisions(a,a.pairs.list,i),j.showVertexNumbers&&B.vertexNumbers(a,n,i),B.constraints(o,i),j.showBroadphase&&a.broadphase.controller===g&&B.grid(a,a.broadphase,i),j.showDebug&&B.debug(a,i),j.hasBounds&&i.setTransform(j.pixelRatio,0,0,j.pixelRatio,0,0)},B.debug=function(a,b){var d=b,e=a.world,f=a.render,g=f.options,h=(c.allBodies(e)," ");if(a.timing.timestamp-(f.debugTimestamp||0)>=500){var i="";i+="fps: "+Math.round(a.timing.fps)+h,f.debugString=i,f.debugTimestamp=a.timing.timestamp}if(f.debugString){d.font="12px Arial",d.fillStyle=g.wireframes?"rgba(255,255,255,0.5)":"rgba(0,0,0,0.5)";for(var j=f.debugString.split("\n"),k=0;k<j.length;k++)d.fillText(j[k],50,50+18*k)}},B.constraints=function(a,b){for(var c=b,d=0;d<a.length;d++){var e=a[d];if(e.render.visible&&e.pointA&&e.pointB){var f=e.bodyA,g=e.bodyB;f?(c.beginPath(),c.moveTo(f.position.x+e.pointA.x,f.position.y+e.pointA.y)):(c.beginPath(),c.moveTo(e.pointA.x,e.pointA.y)),g?c.lineTo(g.position.x+e.pointB.x,g.position.y+e.pointB.y):c.lineTo(e.pointB.x,e.pointB.y),c.lineWidth=e.render.lineWidth,c.strokeStyle=e.render.strokeStyle,c.stroke()}}},B.bodyShadows=function(a,b,c){for(var d=c,e=a.render,f=0;f<b.length;f++){var g=b[f];if(g.render.visible){if(g.circleRadius)d.beginPath(),d.arc(g.position.x,g.position.y,g.circleRadius,0,2*Math.PI),d.closePath();else{d.beginPath(),d.moveTo(g.vertices[0].x,g.vertices[0].y);for(var h=1;h<g.vertices.length;h++)d.lineTo(g.vertices[h].x,g.vertices[h].y);d.closePath()}var i=g.position.x-.5*e.options.width,j=g.position.y-.2*e.options.height,k=Math.abs(i)+Math.abs(j);d.shadowColor="rgba(0,0,0,0.15)",d.shadowOffsetX=.05*i,d.shadowOffsetY=.05*j,d.shadowBlur=1+12*Math.min(1,k/1e3),d.fill(),d.shadowColor=null,d.shadowOffsetX=null,d.shadowOffsetY=null,d.shadowBlur=null}}},B.bodies=function(a,b,c){var e,f,g,h,i=c,j=a.render,k=j.options;for(g=0;g<b.length;g++)if(e=b[g],e.render.visible)for(h=e.parts.length>1?1:0;h<e.parts.length;h++)if(f=e.parts[h],f.render.sprite&&f.render.sprite.texture&&!k.wireframes){var l=f.render.sprite,m=d(j,l.texture);k.showSleeping&&e.isSleeping&&(i.globalAlpha=.5),i.translate(f.position.x,f.position.y),i.rotate(f.angle),i.drawImage(m,m.width*-.5*l.xScale,m.height*-.5*l.yScale,m.width*l.xScale,m.height*l.yScale),i.rotate(-f.angle),i.translate(-f.position.x,-f.position.y),k.showSleeping&&e.isSleeping&&(i.globalAlpha=1)}else{if(f.circleRadius)i.beginPath(),i.arc(f.position.x,f.position.y,f.circleRadius,0,2*Math.PI);else{i.beginPath(),i.moveTo(f.vertices[0].x,f.vertices[0].y);for(var n=1;n<f.vertices.length;n++)i.lineTo(f.vertices[n].x,f.vertices[n].y);i.closePath()}k.wireframes?(i.lineWidth=1,i.strokeStyle="#bbb",k.showSleeping&&e.isSleeping&&(i.strokeStyle="rgba(255,255,255,0.2)"),i.stroke()):(i.fillStyle=k.showSleeping&&e.isSleeping?o.shadeColor(f.render.fillStyle,50):f.render.fillStyle,i.lineWidth=f.render.lineWidth,i.strokeStyle=f.render.strokeStyle,i.fill(),i.stroke())}},B.bodyWireframes=function(a,b,c){var d,e,f,g,h,i=c,j=a.render.options.showInternalEdges;for(i.beginPath(),f=0;f<b.length;f++)if(d=b[f],d.render.visible)for(h=d.parts.length>1?1:0;h<d.parts.length;h++){for(e=d.parts[h],i.moveTo(e.vertices[0].x,e.vertices[0].y),g=1;g<e.vertices.length;g++)!e.vertices[g-1].isInternal||j?i.lineTo(e.vertices[g].x,e.vertices[g].y):i.moveTo(e.vertices[g].x,e.vertices[g].y),e.vertices[g].isInternal&&!j&&i.moveTo(e.vertices[(g+1)%e.vertices.length].x,e.vertices[(g+1)%e.vertices.length].y);i.lineTo(e.vertices[0].x,e.vertices[0].y)}i.lineWidth=1,i.strokeStyle="#bbb",i.stroke()},B.bodyConvexHulls=function(a,b,c){var d,e,f,g=c;for(g.beginPath(),e=0;e<b.length;e++)if(d=b[e],d.render.visible&&1!==d.parts.length){for(g.moveTo(d.vertices[0].x,d.vertices[0].y),f=1;f<d.vertices.length;f++)g.lineTo(d.vertices[f].x,d.vertices[f].y);g.lineTo(d.vertices[0].x,d.vertices[0].y)}g.lineWidth=1,g.strokeStyle="rgba(255,255,255,0.2)",g.stroke()},B.vertexNumbers=function(a,b,c){var d,e,f,g=c;for(d=0;d<b.length;d++){var h=b[d].parts;for(f=h.length>1?1:0;f<h.length;f++){var i=h[f];for(e=0;e<i.vertices.length;e++)g.fillStyle="rgba(255,255,255,0.2)",g.fillText(d+"_"+e,i.position.x+.8*(i.vertices[e].x-i.position.x),i.position.y+.8*(i.vertices[e].y-i.position.y))}}},B.bodyBounds=function(a,b,c){var d=c,e=a.render,f=e.options;d.beginPath();for(var g=0;g<b.length;g++){var h=b[g];if(h.render.visible)for(var i=b[g].parts,j=i.length>1?1:0;j<i.length;j++){var k=i[j];d.rect(k.bounds.min.x,k.bounds.min.y,k.bounds.max.x-k.bounds.min.x,k.bounds.max.y-k.bounds.min.y)}}d.strokeStyle=f.wireframes?"rgba(255,255,255,0.08)":"rgba(0,0,0,0.1)",d.lineWidth=1,d.stroke()},B.bodyAxes=function(a,b,c){var d,e,f,g,h=c,i=a.render,j=i.options;for(h.beginPath(),e=0;e<b.length;e++){var k=b[e],l=k.parts;if(k.render.visible)if(j.showAxes)for(f=l.length>1?1:0;f<l.length;f++)for(d=l[f],g=0;g<d.axes.length;g++){var m=d.axes[g];h.moveTo(d.position.x,d.position.y),h.lineTo(d.position.x+20*m.x,d.position.y+20*m.y)}else for(f=l.length>1?1:0;f<l.length;f++)for(d=l[f],g=0;g<d.axes.length;g++)h.moveTo(d.position.x,d.position.y),h.lineTo((d.vertices[0].x+d.vertices[d.vertices.length-1].x)/2,(d.vertices[0].y+d.vertices[d.vertices.length-1].y)/2)}h.strokeStyle=j.wireframes?"indianred":"rgba(0,0,0,0.3)",h.lineWidth=1,h.stroke()},B.bodyPositions=function(a,b,c){var d,e,f,g,h=c,i=a.render,j=i.options;for(h.beginPath(),f=0;f<b.length;f++)if(d=b[f],d.render.visible)for(g=0;g<d.parts.length;g++)e=d.parts[g],h.arc(e.position.x,e.position.y,3,0,2*Math.PI,!1),h.closePath();for(h.fillStyle=j.wireframes?"indianred":"rgba(0,0,0,0.5)",h.fill(),h.beginPath(),f=0;f<b.length;f++)d=b[f],d.render.visible&&(h.arc(d.positionPrev.x,d.positionPrev.y,2,0,2*Math.PI,!1),h.closePath());h.fillStyle="rgba(255,165,0,0.8)",h.fill()},B.bodyVelocity=function(a,b,c){var d=c;d.beginPath();for(var e=0;e<b.length;e++){var f=b[e];f.render.visible&&(d.moveTo(f.position.x,f.position.y),d.lineTo(f.position.x+2*(f.position.x-f.positionPrev.x),f.position.y+2*(f.position.y-f.positionPrev.y)))}d.lineWidth=3,d.strokeStyle="cornflowerblue",d.stroke()},B.bodyIds=function(a,b,c){var d,e,f=c;for(d=0;d<b.length;d++)if(b[d].render.visible){var g=b[d].parts;for(e=g.length>1?1:0;e<g.length;e++){var h=g[e];f.font="12px Arial",f.fillStyle="rgba(255,255,255,0.5)",f.fillText(h.id,h.position.x+10,h.position.y-10)}}},B.collisions=function(a,b,c){var d,e,f,g,h=c,i=a.render.options;for(h.beginPath(),f=0;f<b.length;f++)if(d=b[f],d.isActive)for(e=d.collision,g=0;g<d.activeContacts.length;g++){var j=d.activeContacts[g],k=j.vertex;h.rect(k.x-1.5,k.y-1.5,3.5,3.5)}for(h.fillStyle=i.wireframes?"rgba(255,255,255,0.7)":"orange",h.fill(),h.beginPath(),f=0;f<b.length;f++)if(d=b[f],d.isActive&&(e=d.collision,d.activeContacts.length>0)){var l=d.activeContacts[0].vertex.x,m=d.activeContacts[0].vertex.y;2===d.activeContacts.length&&(l=(d.activeContacts[0].vertex.x+d.activeContacts[1].vertex.x)/2,m=(d.activeContacts[0].vertex.y+d.activeContacts[1].vertex.y)/2),e.bodyB===e.supports[0].body||e.bodyA.isStatic===!0?h.moveTo(l-8*e.normal.x,m-8*e.normal.y):h.moveTo(l+8*e.normal.x,m+8*e.normal.y),h.lineTo(l,m)}h.strokeStyle=i.wireframes?"rgba(255,165,0,0.7)":"orange",h.lineWidth=1,h.stroke()},B.separations=function(a,b,c){var d,e,f,g,h,i=c,j=a.render.options;for(i.beginPath(),h=0;h<b.length;h++)if(d=b[h],d.isActive){e=d.collision,f=e.bodyA,g=e.bodyB;var k=1;g.isStatic||f.isStatic||(k=.5),g.isStatic&&(k=0),i.moveTo(g.position.x,g.position.y),i.lineTo(g.position.x-e.penetration.x*k,g.position.y-e.penetration.y*k),k=1,g.isStatic||f.isStatic||(k=.5),f.isStatic&&(k=0),i.moveTo(f.position.x,f.position.y),i.lineTo(f.position.x+e.penetration.x*k,f.position.y+e.penetration.y*k)}i.strokeStyle=j.wireframes?"rgba(255,165,0,0.5)":"orange",i.stroke()},B.grid=function(a,b,c){var d=c,e=a.render.options;d.strokeStyle=e.wireframes?"rgba(255,180,0,0.1)":"rgba(255,180,0,0.5)",d.beginPath();for(var f=o.keys(b.buckets),g=0;g<f.length;g++){var h=f[g];if(!(b.buckets[h].length<2)){var i=h.split(",");d.rect(.5+parseInt(i[0],10)*b.bucketWidth,.5+parseInt(i[1],10)*b.bucketHeight,b.bucketWidth,b.bucketHeight)}}d.lineWidth=1,d.stroke()},B.inspector=function(a,b){var c,d=a.engine,e=a.selected,f=d.render,g=f.options;if(g.hasBounds){var h=f.bounds.max.x-f.bounds.min.x,i=f.bounds.max.y-f.bounds.min.y,j=h/f.options.width,k=i/f.options.height;b.scale(1/j,1/k),b.translate(-f.bounds.min.x,-f.bounds.min.y)}for(var l=0;l<e.length;l++){var m=e[l].data;switch(b.translate(.5,.5),b.lineWidth=1,b.strokeStyle="rgba(255,165,0,0.9)",b.setLineDash([1,2]),m.type){case"body":c=m.bounds,b.beginPath(),b.rect(Math.floor(c.min.x-3),Math.floor(c.min.y-3),Math.floor(c.max.x-c.min.x+6),Math.floor(c.max.y-c.min.y+6)),b.closePath(),b.stroke();break;case"constraint":var n=m.pointA;m.bodyA&&(n=m.pointB),b.beginPath(),b.arc(n.x,n.y,10,0,2*Math.PI),b.closePath(),b.stroke()}b.setLineDash([0]),b.translate(-.5,-.5)}null!==a.selectStart&&(b.translate(.5,.5),b.lineWidth=1,b.strokeStyle="rgba(255,165,0,0.6)",b.fillStyle="rgba(255,165,0,0.1)",c=a.selectBounds,b.beginPath(),b.rect(Math.floor(c.min.x),Math.floor(c.min.y),Math.floor(c.max.x-c.min.x),Math.floor(c.max.y-c.min.y)),b.closePath(),b.stroke(),b.fill(),b.translate(-.5,-.5)),g.hasBounds&&b.setTransform(1,0,0,1,0,0)};var a=function(a,b){var c=document.createElement("canvas");return c.width=a,c.height=b,c.oncontextmenu=function(){return!1},c.onselectstart=function(){return!1},c},b=function(a){var b=a.getContext("2d"),c=window.devicePixelRatio||1,d=b.webkitBackingStorePixelRatio||b.mozBackingStorePixelRatio||b.msBackingStorePixelRatio||b.oBackingStorePixelRatio||b.backingStorePixelRatio||1;return c/d},d=function(a,b){var c=a.textures[b];return c?c:(c=a.textures[b]=new Image,c.src=b,c)},e=function(a,b){var c=b;/(jpg|gif|png)$/.test(b)&&(c="url("+b+")"),a.canvas.style.background=c,a.canvas.style.backgroundSize="contain",a.currentBackground=b}}();var C={};!function(){C.create=function(a){var b={controller:C,element:null,canvas:null,options:{width:800,height:600,background:"#fafafa",wireframeBackground:"#222",hasBounds:!1,enabled:!0,wireframes:!0,showSleeping:!0,showDebug:!1,showBroadphase:!1,showBounds:!1,showVelocity:!1,showCollisions:!1,showAxes:!1,showPositions:!1,showAngleIndicator:!1,showIds:!1,showShadows:!1}},c=o.extend(b,a),d=!c.options.wireframes&&"transparent"===c.options.background;return c.context=new PIXI.WebGLRenderer(c.options.width,c.options.height,{view:c.canvas,transparent:d,antialias:!0,backgroundColor:a.background}),c.canvas=c.context.view,c.container=new PIXI.Container,c.bounds=c.bounds||{min:{x:0,y:0},max:{x:c.options.width,y:c.options.height}},c.textures={},c.sprites={},c.primitives={},c.spriteContainer=new PIXI.Container,c.container.addChild(c.spriteContainer),o.isElement(c.element)?c.element.appendChild(c.canvas):o.log('No "render.element" passed, "render.canvas" was not inserted into document.',"warn"),c.canvas.oncontextmenu=function(){return!1},c.canvas.onselectstart=function(){return!1},c},C.clear=function(a){for(var b=a.container,c=a.spriteContainer;b.children[0];)b.removeChild(b.children[0]);for(;c.children[0];)c.removeChild(c.children[0]);var d=a.sprites["bg-0"];a.textures={},a.sprites={},a.primitives={},a.sprites["bg-0"]=d,d&&b.addChildAt(d,0),a.container.addChild(a.spriteContainer),a.currentBackground=null,b.scale.set(1,1),b.position.set(0,0)},C.setBackground=function(a,b){if(a.currentBackground!==b){var c=b.indexOf&&-1!==b.indexOf("#"),e=a.sprites["bg-0"];if(c){var f=o.colorToNumber(b);a.context.backgroundColor=f,e&&a.container.removeChild(e)}else if(!e){var g=d(a,b);e=a.sprites["bg-0"]=new PIXI.Sprite(g),e.position.x=0,e.position.y=0,a.container.addChildAt(e,0)
}a.currentBackground=b}},C.world=function(a){var b,d=a.render,e=a.world,f=d.context,g=d.container,h=d.options,i=c.allBodies(e),j=c.allConstraints(e),k=[];h.wireframes?C.setBackground(d,h.wireframeBackground):C.setBackground(d,h.background);var l=d.bounds.max.x-d.bounds.min.x,m=d.bounds.max.y-d.bounds.min.y,n=l/d.options.width,o=m/d.options.height;if(h.hasBounds){for(b=0;b<i.length;b++){var p=i[b];p.render.sprite.visible=x.overlaps(p.bounds,d.bounds)}for(b=0;b<j.length;b++){var q=j[b],r=q.bodyA,s=q.bodyB,t=q.pointA,u=q.pointB;r&&(t=z.add(r.position,q.pointA)),s&&(u=z.add(s.position,q.pointB)),t&&u&&(x.contains(d.bounds,t)||x.contains(d.bounds,u))&&k.push(q)}g.scale.set(1/n,1/o),g.position.set(-d.bounds.min.x*(1/n),-d.bounds.min.y*(1/o))}else k=j;for(b=0;b<i.length;b++)C.body(a,i[b]);for(b=0;b<k.length;b++)C.constraint(a,k[b]);f.render(g)},C.constraint=function(a,b){var c=a.render,d=b.bodyA,e=b.bodyB,f=b.pointA,g=b.pointB,h=c.container,i=b.render,j="c-"+b.id,k=c.primitives[j];return k||(k=c.primitives[j]=new PIXI.Graphics),i.visible&&b.pointA&&b.pointB?(-1===o.indexOf(h.children,k)&&h.addChild(k),k.clear(),k.beginFill(0,0),k.lineStyle(i.lineWidth,o.colorToNumber(i.strokeStyle),1),d?k.moveTo(d.position.x+f.x,d.position.y+f.y):k.moveTo(f.x,f.y),e?k.lineTo(e.position.x+g.x,e.position.y+g.y):k.lineTo(g.x,g.y),void k.endFill()):void k.clear()},C.body=function(c,d){var e=c.render,f=d.render;if(f.visible)if(f.sprite&&f.sprite.texture){var g="b-"+d.id,h=e.sprites[g],i=e.spriteContainer;h||(h=e.sprites[g]=a(e,d)),-1===o.indexOf(i.children,h)&&i.addChild(h),h.position.x=d.position.x,h.position.y=d.position.y,h.rotation=d.angle,h.scale.x=f.sprite.xScale||1,h.scale.y=f.sprite.yScale||1}else{var j="b-"+d.id,k=e.primitives[j],l=e.container;k||(k=e.primitives[j]=b(e,d),k.initialAngle=d.angle),-1===o.indexOf(l.children,k)&&l.addChild(k),k.position.x=d.position.x,k.position.y=d.position.y,k.rotation=d.angle-k.initialAngle}};var a=function(a,b){var c=b.render,e=c.sprite.texture,f=d(a,e),g=new PIXI.Sprite(f);return g.anchor.x=.5,g.anchor.y=.5,g},b=function(a,b){var c,d=b.render,e=a.options,f=new PIXI.Graphics,g=o.colorToNumber(d.fillStyle),h=o.colorToNumber(d.strokeStyle),i=o.colorToNumber(d.strokeStyle),j=o.colorToNumber("#bbb"),k=o.colorToNumber("#CD5C5C");f.clear();for(var l=b.parts.length>1?1:0;l<b.parts.length;l++){c=b.parts[l],e.wireframes?(f.beginFill(0,0),f.lineStyle(1,j,1)):(f.beginFill(g,1),f.lineStyle(d.lineWidth,h,1)),f.moveTo(c.vertices[0].x-b.position.x,c.vertices[0].y-b.position.y);for(var m=1;m<c.vertices.length;m++)f.lineTo(c.vertices[m].x-b.position.x,c.vertices[m].y-b.position.y);f.lineTo(c.vertices[0].x-b.position.x,c.vertices[0].y-b.position.y),f.endFill(),(e.showAngleIndicator||e.showAxes)&&(f.beginFill(0,0),e.wireframes?f.lineStyle(1,k,1):f.lineStyle(1,i),f.moveTo(c.position.x-b.position.x,c.position.y-b.position.y),f.lineTo((c.vertices[0].x+c.vertices[c.vertices.length-1].x)/2-b.position.x,(c.vertices[0].y+c.vertices[c.vertices.length-1].y)/2-b.position.y),f.endFill())}return f},d=function(a,b){var c=a.textures[b];return c||(c=a.textures[b]=PIXI.Texture.fromImage(b)),c}}(),d.add=c.add,d.remove=c.remove,d.addComposite=c.addComposite,d.addBody=c.addBody,d.addConstraint=c.addConstraint,d.clear=c.clear,p.run=s.run,a.Body=b,a.Composite=c,a.World=d,a.Contact=e,a.Detector=f,a.Grid=g,a.Pairs=i,a.Pair=h,a.Resolver=k,a.SAT=l,a.Constraint=m,a.MouseConstraint=n,a.Common=o,a.Engine=p,a.Mouse=r,a.Sleeping=t,a.Bodies=u,a.Composites=v,a.Axes=w,a.Bounds=x,a.Vector=z,a.Vertices=A,a.Render=B,a.RenderPixi=C,a.Events=q,a.Query=j,a.Runner=s,a.Svg=y,"undefined"!=typeof exports&&("undefined"!=typeof module&&module.exports&&(exports=module.exports=a),exports.Matter=a),"function"==typeof define&&define.amd&&define("Matter",[],function(){return a}),"object"==typeof window&&"object"==typeof window.document&&(window.Matter=a)}();