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

9 lines
74 KiB
JavaScript
Raw Normal View History

2014-02-19 09:15:05 -05:00
/**
2015-08-17 18:50:03 -04:00
* matter.min.js edge-master 2015-08-17
2014-02-19 09:15:05 -05:00
* http://brm.io/matter-js/
* License: MIT
*/
2015-08-17 18:50:03 -04:00
!function(a){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=a();else if("function"==typeof define&&define.amd)define([],a);else{var b;b="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,b.Matter=a()}}(function(){return function a(b,c,d){function e(g,h){if(!c[g]){if(!b[g]){var i="function"==typeof require&&require;if(!h&&i)return i(g,!0);if(f)return f(g,!0);var j=new Error("Cannot find module '"+g+"'");throw j.code="MODULE_NOT_FOUND",j}var k=c[g]={exports:{}};b[g][0].call(k.exports,function(a){var c=b[g][1][a];return e(c?c:a)},k,k.exports,a,b,c,d)}return c[g].exports}for(var f="function"==typeof require&&require,g=0;g<d.length;g++)e(d[g]);return e}({1:[function(a,b){var c={};b.exports=c;var d=a("../geometry/Vertices"),e=a("../geometry/Vector"),f=a("../core/Sleeping"),g=(a("../render/Render"),a("../core/Common")),h=a("../geometry/Bounds"),i=a("../geometry/Axes");!function(){c._inertiaScale=4;var a=1,b=-1,j=1;c.create=function(a){var b={id:g.nextId(),type:"body",label:"Body",parts:[],angle:0,vertices:d.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=g.extend(b,a);return k(c,a),c},c.nextGroup=function(c){return c?b--:a++},c.nextCategory=function(){return j<<=1};var k=function(a,b){c.set(a,{bounds:a.bounds||h.create(a.vertices),positionPrev:a.positionPrev||e.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}),d.rotate(a.vertices,a.angle,a.position),i.rotate(a.axes,a.angle),h.update(a.bounds,a.vertices,a.velocity),c.set(a,{axes:b.axes||a.axes,area:b.area||a.area,mass:b.mass||a.mass,inertia:b.inertia||a.inertia});var f=a.isStatic?"#eeeeee":g.choose(["#556270","#4ECDC4","#C7F464","#FF6B6B","#C44D58"]),j=g.shadeColor(f,-20);a.render.fillStyle=a.render.fillStyle||f,a.render.strokeStyle=a.render.strokeStyle||j};c.set=function(a,b,d){var e;"string"==typeof b&&(e=b,b={},b[e]=d);for(e in b)if(d=b[e],b.hasOwnProperty(e))switch(e){case"isStatic":c.setStatic(a,d);break;case"isSleeping":f.set(a,d);break;case"mass":c.setMass(a,d);break;case"density":c.setDensity(a,d);break;case"inertia":c.setInertia(a,d);break;case"vertices":c.setVertices(a,d);break;case"position":c.setPosition(a,d);break;case"angle":c.setAngle(a,d);break;case"velocity":c.setVelocity(a,d);break;case"angularVelocity":c.setAngularVelocity(a,d);break;case"parts":c.setParts(a,d);break;default:a[e]=d}},c.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)}},c.setMass=function(a,b){a.mass=b,a.inverseMass=1/a.mass,a.density=a.mass/a.area},c.setDensity=function(a,b){c.setMass(a,b*a.area),a.density=b},c.setInertia=function(a,b){a.inertia=b,a.inverseInertia=1/a.inertia},c.setVertices=function(a,b){a.vertices=b[0].body===a?b:d.create(b,a),a.axes=i.fromVertices(a.vertices),a.area=d.area(a.vertices),c.setMass(a,a.density*a.area);var e=d.centre(a.vertices);d.translate(a.vertices,e,-1),c.setInertia(a,c._inertiaScale*d.inertia(a.vertices,a.mass)),d.translate(a.vertices,a.position),h.update(a.bounds,a.vertices,a.velocity)},c.setParts=function(a,b,e){var f;for(b=b.slice(0),a.parts.length=0,a.parts.push(a),a.parent=a,f=0;f<b.length;f++){var g=b[f];g!==a&&(g.parent=a,a.parts.push(g))}if(1!==a.parts.length){if(e="undefined"!=typeof e?e:!0){var h=[];for(f=0;f<b.length;f++)h=h.concat(b[f].vertices);d.clockwiseSort(h);var i=
},c.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)}},c.nextId=function(){return c._nextId++},c.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 c._seed=(9301*c._seed+49297)%233280,c._seed/233280}}()},{}],15:[function(a,b){var c={};b.exports=c;var d=a("../body/World"),e=a("./Sleeping"),f=a("../collision/Resolver"),g=a("../render/Render"),h=a("../collision/Pairs"),i=(a("./Metrics"),a("../collision/Grid")),j=a("./Events"),k=a("../body/Composite"),l=a("../constraint/Constraint"),m=a("./Common"),n=a("../body/Body");!function(){c.create=function(a,b){b=m.isElement(a)?b:a,a=m.isElement(a)?a:null;var c={positionIterations:6,velocityIterations:4,constraintIterations:2,enableSleeping:!1,events:[],timing:{timestamp:0,timeScale:1},broadphase:{controller:i}},e=m.extend(c,b);if(a||e.render){var f={element:a,controller:g};e.render=m.extend(f,e.render)}return e.render&&e.render.controller&&(e.render=e.render.controller.create(e.render)),e.world=d.create(e.world),e.pairs=h.create(),e.broadphase=e.broadphase.controller.create(e.broadphase),e.metrics=e.metrics||{extended:!1},e},c.update=function(c,d,g){g="undefined"!=typeof g?g:1;var i,m=c.world,n=c.timing,p=c.broadphase,q=[];n.timestamp+=d*n.timeScale;var r={timestamp:n.timestamp};j.trigger(c,"beforeUpdate",r);var s=k.allBodies(m),t=k.allConstraints(m);for(c.enableSleeping&&e.update(s,n.timeScale),b(s,m.gravity),o(s,d,n.timeScale,g,m.bounds),i=0;i<c.constraintIterations;i++)l.solveAll(t,n.timeScale);l.postSolveAll(s),p.controller?(m.isModified&&p.controller.clear(p),p.controller.update(p,s,c,m.isModified),q=p.pairsList):q=s;var u=p.detector(q,c),v=c.pairs,w=n.timestamp;for(h.update(v,u,w),h.removeOld(v,w),c.enableSleeping&&e.afterCollisions(v.list,n.timeScale),v.collisionStart.length>0&&j.trigger(c,"collisionStart",{pairs:v.collisionStart}),f.preSolvePosition(v.list),i=0;i<c.positionIterations;i++)f.solvePosition(v.list,n.timeScale);for(f.postSolvePosition(s),f.preSolveVelocity(v.list),i=0;i<c.velocityIterations;i++)f.solveVelocity(v.list,n.timeScale);return v.collisionActive.length>0&&j.trigger(c,"collisionActive",{pairs:v.collisionActive}),v.collisionEnd.length>0&&j.trigger(c,"collisionEnd",{pairs:v.collisionEnd}),a(s),m.isModified&&k.setModified(m,!1,!1,!0),j.trigger(c,"afterUpdate",r),c},c.merge=function(a,b){if(m.extend(a,b),b.world){a.world=b.world,c.clear(a);for(var d=k.allBodies(a.world),f=0;f<d.length;f++){var g=d[f];e.set(g,!1),g.id=m.nextId()}}},c.clear=function(a){var b=a.world;h.clear(a.pairs);var c=a.broadphase;if(c.controller){var d=k.allBodies(b);c.controller.clear(c),c.controller.update(c,d,a,!0)}};var a=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=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)}},o=function(a,b,c,d){for(var e=0;e<a.length;e++){var f=a[e];f.isStatic||f.isSleeping||n.update(f,b,c,d)}}}()},{"../body/Body":1,"../body/Composite":2,"../body/World":3,"../collision/Grid":6,"../collision/Pairs":8,"../collision/Resolver":10,"../constraint/Constraint":12,"../render/Render":29,"./Common":14,"./Events":16,"./Metrics":17,"./Sleeping":20}],16:[function(a,b){var c={};b.exports=c;var d=a("./Common");!function(){c.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},c.off=function(a,b,c){if(!b)return void(a.events={});"function"==typeof b&&(c=b,b=d.keys(a.events).join(" "));for(var e=b.split(" "),f=0;f<e.length;f++){var g=a.events[e[f]],h=[];if(c&&g)for(var i=0;i<g.length;i++)g[i]!==c&&h.push(g[i]);a.events[e[f]]=h}},c.trigger=function(a,b,c){var e,f,g,h;if(a.events){c||(c={}),e=b.split(" ");for(var i=0;i<e.length;i++)if(f=e[i],g=a.events[f]){h=d.clone(c,!1),h.name=f,h.source=a;for(var j=0;j<g.length;j++)g[j].apply(a,[h])}}}}()},{"./Common":14}],17:[functio
g.lineTo(d.vertices[0].x,d.vertices[0].y)}g.lineWidth=1,g.strokeStyle="rgba(255,255,255,0.2)",g.stroke()},c.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))}}},c.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()},c.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()},c.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()},c.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()},c.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)}}},c.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()},c.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()},c.grid=function(a,b,c){var e=c,f=a.render.options;e.strokeStyle=f.wireframes?"rgba(255,180,0,0.1)":"rgba(255,180,0,0.5)",e.beginPath();for(var g=d.keys(b.