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
46 KiB
JavaScript
Raw Normal View History

2014-02-19 09:15:05 -05:00
/**
* matter.min.js 0.5.0-edge 2014-03-22
2014-02-19 09:15:05 -05:00
* 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,groupId:0,slop:.05,render:{visible:!0,sprite:null,path:"L 0 0 L 40 0 L 40 40 L 0 40",lineWidth:1.5}},d=n.extend(c,a);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),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.co
},save:function(){localStorage&&c&&localStorage.setItem("world",c.stringify(a.world))},load:function(){var b;localStorage&&c&&(b=c.parse(localStorage.getItem("world"))),b&&o.merge(a,{world:b})}},i=f.addFolder("Metrics");i.add(a.timing,"fps").listen(),a.metrics.extended?(i.add(a.timing,"delta").listen(),i.add(a.timing,"correction").listen(),i.add(a.metrics,"bodies").listen(),i.add(a.metrics,"collisions").listen(),i.add(a.metrics,"pairs").listen(),i.add(a.metrics,"broadEff").listen(),i.add(a.metrics,"midEff").listen(),i.add(a.metrics,"narrowEff").listen(),i.add(a.metrics,"narrowReuse").listen(),i.close()):i.open();var j=f.addFolder("Add Body");j.add(g,"amount",1,5).step(1),j.add(g,"size",5,150).step(1),j.add(g,"sides",1,8).step(1),j.add(g,"density",1e-4,.01).step(.001),j.add(g,"friction",0,1).step(.05),j.add(g,"frictionAir",0,10*g.frictionAir).step(g.frictionAir/10),j.add(g,"restitution",0,1).step(.1),j.add(h,"addBody"),j.open();var k=f.addFolder("World");k.add(h,"load"),k.add(h,"save"),k.add(h,"clear"),k.open();var l=k.addFolder("Gravity");l.add(a.world.gravity,"x",-1,1).step(.01),l.add(a.world.gravity,"y",-1,1).step(.01),l.open();var m=f.addFolder("Engine");m.add(a,"enableSleeping"),m.add(a.broadphase,"current",["grid","bruteForce"]),m.add(a,"timeScale",.1,2).step(.1),m.add(a,"velocityIterations",1,10).step(1),m.add(a,"positionIterations",1,10).step(1),m.add(a,"enabled"),m.open();var n=f.addFolder("Render");return n.add(g,"renderer",["canvas","webgl"]).onFinishChange(function(b){var c;"canvas"===b&&(c=A),"webgl"===b&&(c=B),a.render.element.removeChild(a.render.canvas);var d=a.render.options;a.render=c.create({element:a.render.element,options:d}),a.render.options=d,a.input.mouse=r.create(a.render.canvas),a.mouseConstraint.mouse=a.input.mouse}),n.add(a.render.options,"wireframes"),n.add(a.render.options,"showDebug"),n.add(a.render.options,"showPositions"),n.add(a.render.options,"showBroadphase"),n.add(a.render.options,"showBounds"),n.add(a.render.options,"showVelocity"),n.add(a.render.options,"showCollisions"),n.add(a.render.options,"showAxes"),n.add(a.render.options,"showAngleIndicator"),n.add(a.render.options,"showSleeping"),n.add(a.render.options,"showIds"),n.add(a.render.options,"showShadows"),n.add(a.render.options,"enabled"),n.open(),g},z.update=function(a,b){var c;b=b||a.datGui;for(c in b.__folders)z.update(a,b.__folders[c]);for(c in b.__controllers){var d=b.__controllers[c];d.updateDisplay&&d.updateDisplay()}},z.closeAll=function(a){var b=a.datGui;for(var c in b.__folders)b.__folders[c].close()}}();var A={};!function(){A.create=function(b){var c={controller:A,element:null,canvas:null,options:{width:800,height:600,background:"#fafafa",wireframeBackground:"#222",enabled:!0,wireframes:!0,showSleeping:!0,showDebug:!1,showBroadphase:!1,showBounds:!1,showVelocity:!1,showCollisions:!1,showAxes:!1,showPositions:!1,showAngleIndicator:!1,showIds:!1,showShadows:!1}},d=n.extend(c,b);return d.canvas=d.canvas||a(d.options.width,d.options.height),d.context=d.canvas.getContext("2d"),d.textures={},A.setBackground(d,d.options.background),n.isElement(d.element)?d.element.appendChild(d.canvas):n.log('No "render.element" passed, "render.canvas" was not inserted into document.',"warn"),d},A.setBackground=function(a,b){if(a.currentBackground!==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}},A.world=function(a){var b=a.render,c=a.world,d=b.canvas,e=b.context,f=b.options;f.wireframes?A.setBackground(b,f.wireframeBackground):A.setBackground(b,f.background),e.globalCompositeOperation="source-in",e.fillStyle="transparent",e.fillRect(0,0,d.width,d.height),e.globalCompositeOperation="source-over",!f.wireframes||a.enableSleeping&&f.showSleeping?A.bodies(a,c.bodies,e):A.bodyWireframes(a,c.bodies,e),f.showBounds&&A.bodyBounds(a,c.bodies,e),(f.showAxes||f.showAngleIndicator)&&A.bodyAxes(a,c.bodies,e),f.showPositions&&A.bodyPositions(a,c.bodies,e),f.showVelocity&&A.bodyVelocity(a,c.bodies,e),f.showIds&&A.bodyIds(a,c.bodies,e),f.showCollisions&&A.collis