0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-12-27 13:59:01 -05:00
liabru-matter-js/build/matter.min.js

8 lines
50 KiB
JavaScript
Raw Normal View History

2014-02-19 09:15:05 -05:00
/**
2014-04-01 08:47:17 -04:00
* matter.min.js 0.7.0-edge 2014-04-01
2014-02-19 09:15:05 -05:00
* http://brm.io/matter-js/
* License: MIT
*/
2014-04-01 08:47:17 -04:00
!function(){var a={},b={};!function(){var a=0,c=1;b.create=function(a){var c={id:b.nextId(),type:"body",angle:0,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,render:{visible:!0,sprite:{xScale:1,yScale:1},path:"L 0 0 L 40 0 L 40 40 L 0 40",lineWidth:1.5}},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.render.path),a.axes=a.axes||v.fromVertices(a.vertices),a.area=y.area(a.vertices),a.bounds=w.create(a.vertices),a.mass=a.mass||a.density*a.area,a.inverseMass=1/a.mass,a.inertia=a.inertia||y.inertia(a.vertices,a.mass),a.inverseInertia=1/a.inertia,a.positionPrev=a.positionPrev||{x:a.position.x,y:a.position.y},a.anglePrev=a.anglePrev||a.angle,a.render.fillStyle=a.render.fillStyle||(a.isStatic?"#eeeeee":n.choose(["#556270","#4ECDC4","#C7F464","#FF6B6B","#C44D58"])),a.render.strokeStyle=a.render.strokeStyle||n.shadeColor(a.render.fillStyle,-20),y.create(a.vertices,a);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.render.lineWidth=1),s.set(a,a.isSleeping)},b.resetForcesAll=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}},b.applyGravityAll=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)}},b.updateAll=function(a,c,d,e){for(var f=0;f<a.length;f++){var g=a[f];g.isStatic||g.isSleeping||g.bounds.max.x<e.min.x||g.bounds.min.x>e.max.x||g.bounds.max.y<e.min.y||g.bounds.min.y>e.max.y||b.update(g,c,d)}},b.update=function(a,b,c){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),0!==a.angularVelocity&&(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(){var a=0;c.create=function(a){return n.extend({id:c.nextId(),type:"composite",parent:null,isModified:!1,bodies:[],constraints:[],composites:[]},a)},c.nextId=function(){return 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){for(var d=[].concat(b),e=0;e<d.length;e++){var f=d[e];switch(f.type){case"body":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 a},c.remove=function(a,b,d){for(var e=[].concat(b),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,
2014-03-30 14:45:30 -04:00
f=g.x*c-g.y*d,g.y=g.x*d+g.y*c,g.x=f}}}();var w={};!function(){w.create=function(a){var b={min:{x:0,y:0},max:{x:0,y:0}};return w.update(b,a),b},w.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)},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;c<a.length;c++)a[c].index=c,a[c].body=b},y.fromPath=function(a){var b=/L\s*([\-\d\.]*)\s*([\-\d\.]*)/gi,c=[];return a.replace(b,function(a,b,d){c.push({x:parseFloat(b,10),y:parseFloat(d,10)})}),c},y.centre=function(a){for(var b=0,c=0,d=0;d<a.length;d++)b+=a[d].x,c+=a[d].y;return{x:b/a.length,y:c/a.length}},y.area=function(a){for(var b=0,c=a.length-1,d=0;d<a.length;d++)b+=(a[c].x-a[d].x)*(a[c].y+a[d].y),c=d;return Math.abs(b)/2},y.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(x.cross(g[d],g[h])),e+=c*(x.dot(g[d],g[d])+x.dot(g[d],g[h])+x.dot(g[h],g[h])),f+=c;return b/6*(e/f)},y.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},y.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)}},y.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}}();var z={};!function(){z.create=function(a,b){var e,f=window.dat&&window.localStorage;if(!f)return void console.log("Could not create GUI. Check dat.gui library is loaded first.");var g=new dat.GUI(b);Resurrect?(e=new Resurrect({prefix:"$"}),e.parse=e.resurrect):e=JSON;var h={datGui:g,amount:1,size:40,sides:4,density:.001,restitution:0,friction:.1,frictionAir:.01,renderer:"canvas"},i={addBody:function(){for(var b={density:h.density,friction:h.friction,frictionAir:h.frictionAir,restitution:h.restitution},c=0;c<h.amount;c++)d.add(a.world,t.polygon(120+c*h.size+50*c,200,h.sides,h.size,b))},clear:function(){d.clear(a.world,!0),o.clear(a);var b=a.render.controller;b.clear&&b.clear(a.render),p.trigger(h,"clear")},save:function(){localStorage&&e&&localStorage.setItem("world",e.stringify(a.world)),p.trigger(h,"save")},load:function(){var b;localStorage&&e&&(b=e.parse(localStorage.getItem("world"))),b&&o.merge(a,{world:b}),p.trigger(h,"load")}},j=g.addFolder("Metrics");j.add(a.timing,"fps").listen(),a.metrics.extended?(j.add(a.timing,"delta").listen(),j.add(a.timing,"correction").listen(),j.add(a.metrics,"bodies").listen(),j.add(a.metrics,"collisions").listen(),j.add(a.metrics,"pairs").listen(),j.add(a.metrics,"broadEff").listen(),j.add(a.metrics,"midEff").listen(),j.add(a.metrics,"narrowEff").listen(),j.add(a.metrics,"narrowReuse").listen(),j.close()):j.open();var k=g.addFolder("Add Body");k.add(h,"amount",1,5).step(1),k.add(h,"si