0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-11-23 09:26:51 -05:00
liabru-matter-js/build/matter.min.js

9 lines
66 KiB
JavaScript
Raw Normal View History

2014-02-19 09:15:05 -05:00
/**
2015-07-05 10:57:31 -04:00
* matter.min.js edge-master 2015-07-05
2014-02-19 09:15:05 -05:00
* http://brm.io/matter-js/
* License: MIT
*/
2015-07-05 10:57:31 -04:00
!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.vel
"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.trigg
}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)}();