/** * matter.min.js 0.5.0-edge 2014-03-11 * http://brm.io/matter-js/ * License: MIT */ !function(){var a={},b={};!function(){var a=0,c=1;b.create=function(a){var c={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,path:"L 0 0 L 40 0 L 40 40 L 0 40",fillStyle:a.isStatic?"#eeeeee":n.choose(["#556270","#4ECDC4","#C7F464","#FF6B6B","#C44D58"]),lineWidth:1.5,groupId:0,slop:.05},d=n.extend(c,a);return b.updateProperties(d),d},b.nextId=function(){return a++},b.nextGroupId=function(){return c++},b.updateProperties=function(a){a.vertices=a.vertices||y.fromPath(a.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.strokeStyle=a.strokeStyle||n.shadeColor(a.fillStyle,-20),y.create(a.vertices,a);var b=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.lineWidth=1),s.set(a,a.isSleeping)},b.resetForcesAll=function(a){for(var b=0;be.max.x||g.bounds.max.ye.max.y||b.update(g,c,d)}},b.update=function(a,b,c){var d=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;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},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)}}();var c={};!function(){c.create=function(a){return n.extend({bodies:[],constraints:[],composites:[]},a)},c.add=function(a,b){return a.bodies&&b.bodies&&(a.bodies=a.bodies.concat(b.bodies)),a.constraints&&b.constraints&&(a.constraints=a.constraints.concat(b.constraints)),a.composites&&b.composites&&(a.composites=a.composites.concat(b.composites)),a},c.addBody=function(a,b){return a.bodies=a.bodies||[],a.bodies.push(b),a},c.addConstraint=function(a,b){return a.constraints=a.constraints||[],a.constraints.push(b),a}}();var d={};!function(){d.create=function(a){var b={gravity:{x:0,y:1},bodies:[],constraints:[],bounds:{min:{x:0,y:0},max:{x:800,y:600}}};return n.extend(b,a)},d.clear=function(a,b){a.bodies=b?a.bodies.filter(function(a){return a.isStatic}):[],a.constraints=[]}}();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=0;dq.bounds.width||u.bounds.max.y<0||u.bounds.min.y>q.bounds.height)){var v=b(c,u);if(!u.region||v.id!==u.region.id||k){s.broadphaseTests+=1,(!u.region||k)&&(u.region=v);var w=a(v,u.region);for(m=w.startCol;m<=w.endCol;m++)for(n=w.startRow;n<=w.endRow;n++){p=d(m,n),o=r[p];var x=m>=v.startCol&&m<=v.endCol&&n>=v.startRow&&n<=v.endRow,y=m>=u.region.startCol&&m<=u.region.endCol&&n>=u.region.startRow&&n<=u.region.endRow;!x&&y&&y&&o&&h(c,o,u),(u.region===v||x&&!y||k)&&(o||(o=e(r,p)),f(c,o,u))}u.region=v,t=!0}}}t&&(c.pairsList=j(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},f=function(a,b,c){for(var d=0;d0?g-1:0}}else b.splice(d,1)}},i=function(a,b){return a.id0&&d.push(c);return d}}();var h={};!function(){var a=1e3;h.updatePairs=function(a,b){var c,d,e,f,g=a.list,h=a.table,j=a.collisionStart,k=a.collisionEnd,l=a.collisionActive,m=[];for(j.length=0,k.length=0,l.length=0,f=0;fa&&j.push(f);for(f=0;fB*e.friction&&(C=B*e.friction*z);var D=x.cross(q,i),E=x.cross(r,i),F=l/(e.inverseMass+g.inverseInertia*D*D+h.inverseInertia*E*E);if(A*=F,C*=F,0>v&&v*v>a)o.normalImpulse=0,o.tangentImpulse=0;else{var G=o.normalImpulse;o.normalImpulse=Math.min(o.normalImpulse+A,0),A=o.normalImpulse-G;var H=o.tangentImpulse;o.tangentImpulse=n.clamp(o.tangentImpulse+C,-y,y),C=o.tangentImpulse-H}c.x=i.x*A+j.x*C,c.y=i.y*A+j.y*C,g.isStatic||g.isSleeping||(g.positionPrev.x+=c.x*g.inverseMass,g.positionPrev.y+=c.y*g.inverseMass,g.anglePrev+=x.cross(q,c)*g.inverseInertia),h.isStatic||h.isSleeping||(h.positionPrev.x-=c.x*h.inverseMass,h.positionPrev.y-=c.y*h.inverseMass,h.anglePrev-=x.cross(r,c)*h.inverseInertia)}}}}}();var k={};!function(){k.collides=function(b,d){var e,f,g,h={collided:!1,bodyA:b,bodyB:d};if(e=a(b.vertices,d.vertices,b.axes),0===e.overlap)return h;if(f=a(d.vertices,b.vertices,d.axes),0===f.overlap)return h;e.overlap0&&(h.normal=x.neg(h.normal)),h.tangent=x.perp(h.normal),h.penetration={x:h.normal.x*h.depth,y:h.normal.y*h.depth};var i=c(b,d,h.normal),j=[i[0]];if(y.contains(b.vertices,i[1]))j.push(i[1]);else{var k=c(d,b,x.neg(h.normal));y.contains(d.vertices,k[0])&&j.push(k[0]),j.length<2&&y.contains(d.vertices,k[1])&&j.push(k[1])}return h.supports=j,h.supportCorrected=x.sub(i[0],h.penetration),h};var a=function(a,c,d){for(var e,f,g={},h={},i={overlap:Number.MAX_VALUE},j=0;j=e)return{overlap:0};ee?e=g:d>g&&(d=g)}a.min=d,a.max=e},c=function(a,b,c){for(var d,e,f=Number.MAX_VALUE,g={x:0,y:0},h=b.vertices,i=a.position,j=h[0],k=h[1],l=0;ld&&(f=d,j=e);var m=j.index-1>=0?j.index-1:h.length-1;e=h[m],g.x=e.x-i.x,g.y=e.y-i.y,f=-x.dot(c,g),k=e;var n=(j.index+1)%h.length;return e=h[n],g.x=e.x-i.x,g.y=e.y-i.y,d=-x.dot(c,g),f>d&&(f=d,k=e),[j,k]}}();var l={};!function(){var a=1e-6;l.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?x.add(c.bodyA.position,c.pointA):c.pointA,e=c.bodyB?x.add(c.bodyB.position,c.pointB):c.pointB,f=x.magnitude(x.sub(d,e));return c.length=c.length||f||a,c.lineWidth=c.lineWidth||2,c.strokeStyle=c.strokeStyle||"#666",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},l.updateAll=function(a){for(var b=0;b0&&(C=0);var D,E={x:z.x*C,y:z.y*C};c&&!c.isStatic&&(D=x.cross(m,E)*c.inverseInertia*(1-b.angularStiffness),s.set(c,!1),D=n.clamp(D,-.01,.01),c.position.x-=A.x,c.position.y-=A.y,c.angle+=D,y.translate(c.vertices,A,-1),y.rotate(c.vertices,D,c.position),v.rotate(c.axes,D),w.update(c.bounds,c.vertices,c.velocity)),d&&!d.isStatic&&(D=x.cross(o,E)*d.inverseInertia*(1-b.angularStiffness),s.set(d,!1),D=n.clamp(D,-.01,.01),d.position.x+=A.x,d.position.y+=A.y,d.angle-=D,y.translate(d.vertices,A),y.rotate(d.vertices,-D,d.position),v.rotate(d.axes,-D),w.update(d.bounds,d.vertices,d.velocity))}}}();var m={};!function(){m.create=function(a){var b=l.create({pointA:a.position,pointB:{x:0,y:0},length:.01,stiffness:.1,angularStiffness:1,strokeStyle:"lightgreen",lineWidth:3});return{mouse:a,dragBody:null,dragPoint:null,constraints:[b]}},m.update=function(a,b){var c=a.mouse,d=a.constraints[0];if(0===c.button||2===c.button){if(!d.bodyB)for(var e=0;e>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)},n.shuffle=function(a){for(var b=a.length-1;b>0;b--){var c=Math.floor(Math.random()*(b+1)),d=a[b];a[b]=a[c],a[c]=d}return a},n.choose=function(a){return a[Math.floor(Math.random()*a.length)]},n.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}},n.clamp=function(a,b,c){return b>a?b:a>c?c:a},n.sign=function(a){return 0>a?-1:1},n.now=Date.now||function(){return+new Date},n.random=function(a,b){return a+Math.random()*(b-a)}}();var o={};!function(){var a=60,c=8,e=1e3/a,i=window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.msRequestAnimationFrame||function(a){window.setTimeout(function(){a(n.now())},e)};o.create=function(b,c){var h={enabled:!0,positionIterations:6,velocityIterations:4,constraintIterations:1,pairs:{table:{},list:[],collisionStart:[],collisionActive:[],collisionEnd:[]},enableSleeping:!1,timeScale:1,input:{},events:[],timing:{fps:a,timestamp:0,delta:e,correction:1,deltaMin:1e3/a,deltaMax:1e3/(.5*a)}},i=n.extend(h,c);return i=n.isElement(b)?i||{}:b,(!i.render||i.render&&!i.render.controller)&&(i.render=A.create(i.render),n.isElement(b)&&b.appendChild(i.render.canvas)),i.world=d.create(i.world),i.metrics=i.metrics||q.create(),i.input.mouse=i.input.mouse||r.create(i.render.canvas),i.mouseConstraint=i.mouseConstraint||m.create(i.input.mouse),d.addComposite(i.world,i.mouseConstraint),i.broadphase=i.broadphase||{current:"grid",grid:{controller:g,instance:g.create(),detector:f.collisions},bruteForce:{detector:f.bruteForce}},i},o.run=function(a){var b,d,f=a.timing,g=0,h=0,j=[];!function k(l){if(i(k),a.enabled){var m={timestamp:l};p.trigger(a,"beforeTick",m),b=l-f.timestamp||e,j.push(b),j=j.slice(-c),b=Math.min.apply(null,j),b=ba.timing.deltaMax?a.timing.deltaMax:b,d=b/f.delta,f.timestamp=l,f.correction=d,f.delta=b,h+=1,l-g>=1e3&&(f.fps=h*((l-g)/1e3),g=l,h=0),p.trigger(a,"tick beforeUpdate",m),o.update(a,b,d);var n=a.pairs;n.collisionStart.length>0&&p.trigger(a,"collisionStart",{pairs:n.collisionStart}),n.collisionActive.length>0&&p.trigger(a,"collisionActive",{pairs:n.collisionActive}),n.collisionEnd.length>0&&p.trigger(a,"collisionEnd",{pairs:n.collisionEnd}),p.trigger(a,"afterUpdate beforeRender",m),a.render.options.enabled&&a.render.controller.world(a),p.trigger(a,"afterTick afterRender",m)}}()},o.update=function(a,c,d){var e,f=a.world,g=a.broadphase[a.broadphase.current],i=[];for(q.reset(a.metrics),a.enableSleeping&&s.update(f.bodies),b.applyGravityAll(f.bodies,f.gravity),m.update(a.mouseConstraint,f.bodies,a.input),b.updateAll(f.bodies,c*a.timeScale,d,f.bounds),e=0;e0||e.force.y>0)s.set(e,!1);else{var g=Math.min(e.motion,f),h=Math.max(e.motion,f);e.motion=c*g+(1-c)*h,e.sleepThreshold>0&&e.motion=e.sleepThreshold&&s.set(e,!0)):e.sleepCounter>0&&(e.sleepCounter-=1)}}},s.afterCollisions=function(b){for(var c=0;ca&&s.set(h,!1)}}}},s.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)}}();var t={};!function(){t.rectangle=function(a,c,d,e,f){f=f||{};var g={position:{x:a,y:c},path:"L 0 0 L "+d+" 0 L "+d+" "+e+" L 0 "+e};return b.create(n.extend({},g,f))},t.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={position:{x:a,y:c},path:"L 0 0 L "+i+" "+-e+" L "+j+" "+-e+" L "+k+" 0"};return b.create(n.extend({},l,g))},t.circle=function(a,b,c,d,e){d=d||{},e=e||25;var f=Math.ceil(Math.max(10,Math.min(e,c)));return f%2===1&&(f+=1),d.circleRadius=c,t.polygon(a,b,f,c,d)},t.polygon=function(a,c,d,e,f){if(f=f||{},3>d)return t.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 o={position:{x:a,y:c},path:h};return b.create(n.extend({},o,f))}}();var u={};!function(){u.stack=function(a,d,e,f,g,h,i){for(var j,k=c.create(),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}}m+=p+h,l=a}return k},u.chain=function(a,b,d,e,f,g){for(var h=a.bodies,i=1;im)){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)}}})},u.newtonsCradle=function(a,b,d,e,f){for(var g=c.create(),h=0;d>h;h++){var i=1.9,j=t.circle(a+h*e*i,b+f,e,{restitution:1,friction:0,frictionAir:1e-4,slop:.01}),k=l.create({pointA:{x:a+h*e*i,y:b},bodyB:j});c.addBody(g,j),c.addConstraint(g,k)}return g},u.car=function(a,d,e,f,g){var h=b.nextGroupId(),i=-20,j=.5*-e+i,k=.5*e-i,m=0,n=c.create(),o=t.trapezoid(a,d,e,f,.3,{groupId:h,friction:.01}),p=t.circle(a+j,d+m,g,{groupId:h,restitution:.5,friction:.9,density:.01}),q=t.circle(a+k,d+m,g,{groupId:h,restitution:.5,friction:.9,density:.01}),r=l.create({bodyA:o,pointA:{x:j,y:m},bodyB:p,stiffness:.5}),s=l.create({bodyA:o,pointA:{x:k,y:m},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}}();var v={};!function(){v.fromVertices=function(a){for(var b={},c=0;ca.max.x&&(a.max.x=e.x),e.xa.max.y&&(a.max.y=e.y),e.y0?a.max.x+=c.x:a.min.x+=c.x,c.y>0?a.max.y+=c.y:a.min.y+=c.y)},w.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},w.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}}();var x={};!function(){x.magnitude=function(a){return Math.sqrt(a.x*a.x+a.y*a.y)},x.magnitudeSquared=function(a){return a.x*a.x+a.y*a.y},x.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}},x.rotateAbout=function(a,b,c){var d=Math.cos(b),e=Math.sin(b);return{x:c.x+((a.x-c.x)*d-(a.y-c.y)*e),y:c.y+((a.x-c.x)*e+(a.y-c.y)*d)}},x.normalise=function(a){var b=x.magnitude(a);return 0===b?{x:0,y:0}:{x:a.x/b,y:a.y/b}},x.dot=function(a,b){return a.x*b.x+a.y*b.y},x.cross=function(a,b){return a.x*b.y-a.y*b.x},x.add=function(a,b){return{x:a.x+b.x,y:a.y+b.y}},x.sub=function(a,b){return{x:a.x-b.x,y:a.y-b.y}},x.mult=function(a,b){return{x:a.x*b,y:a.y*b}},x.div=function(a,b){return{x:a.x/b,y:a.y/b}},x.perp=function(a,b){return b=b===!0?-1:1,{x:b*-a.y,y:b*a.x}},x.neg=function(a){return{x:-a.x,y:-a.y}}}();var y={};!function(){y.create=function(a,b){for(var c=0;c0)return!1}return!0}}();var z={};!function(){z.create=function(a,b){var c,e=window.dat&&window.localStorage;if(!e)return void console.log("Could not create GUI. Check dat.gui library is loaded first.");var f=new dat.GUI(b);Resurrect?(c=new Resurrect({prefix:"$"}),c.parse=c.resurrect):c=JSON;var g={datGui:f,amount:1,size:40,sides:4,density:.001,restitution:0,friction:.1,frictionAir:.01},h={addBody:function(){for(var b={density:g.density,friction:g.friction,frictionAir:g.frictionAir,restitution:g.restitution},c=0;c=500){var i="";i+="delta: "+a.timing.delta.toFixed(3)+h,i+="fps: "+Math.round(a.timing.fps)+h,i+="correction: "+a.timing.correction.toFixed(3)+h,i+="bodies: "+d.bodies.length+h,a.broadphase.controller===g&&(i+="buckets: "+a.metrics.buckets+h),i+="\n",i+="collisions: "+a.metrics.collisions+h,i+="pairs: "+a.pairs.list.length+h,i+="broad: "+a.metrics.broadEff+h,i+="mid: "+a.metrics.midEff+h,i+="narrow: "+a.metrics.narrowEff+h,e.debugString=i,e.debugTimestamp=a.timing.timestamp}if(e.debugString){c.font="12px Arial",c.fillStyle=f.wireframes?"rgba(255,255,255,0.5)":"rgba(0,0,0,0.5)";for(var j=e.debugString.split("\n"),k=0;k0){var j=b.activeContacts[0].vertex.x,k=b.activeContacts[0].vertex.y;2===b.activeContacts.length&&(j=(b.activeContacts[0].vertex.x+b.activeContacts[1].vertex.x)/2,k=(b.activeContacts[0].vertex.y+b.activeContacts[1].vertex.y)/2),d.beginPath(),d.moveTo(j-8*e.normal.x,k-8*e.normal.y),d.lineWidth=1,d.lineTo(j,k),d.strokeStyle=f.wireframes?"rgba(255,165,0,0.7)":"orange",d.stroke()}},A.grid=function(a,b,c){var d=c,e=a.render.options;d.lineWidth=1,d.strokeStyle=e.wireframes?"rgba(255,180,0,0.1)":"rgba(255,180,0,0.5)";for(var f=n.keys(b.buckets),g=0;g