0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-11-30 10:20:52 -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-05-20 15:38:41 -04:00
* matter.min.js edge-master 2015-05-20
2014-02-19 09:15:05 -05:00
* http://brm.io/matter-js/
* License: MIT
*/
2015-05-20 15:38:41 -04:00
!function(){var a={},b={};!function(){b._inertiaScale=4;var a=1,c=-1,d=1;b.create=function(a){var b={id:p.nextId(),type:"body",label:"Body",parts:[],angle:0,vertices:B.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=p.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||y.create(a.vertices),positionPrev:a.positionPrev||A.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}),B.rotate(a.vertices,a.angle,a.position),x.rotate(a.axes,a.angle),y.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":p.choose(["#556270","#4ECDC4","#C7F464","#FF6B6B","#C44D58"]),e=p.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":u.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:B.create(c,a),a.axes=x.fromVertices(a.vertices),a.area=B.area(a.vertices),b.setMass(a,a.density*a.area);var d=B.centre(a.vertices);B.translate(a.vertices,d,-1),b.setInertia(a,b._inertiaScale*B.inertia(a.vertices,a.mass)),B.translate(a.vertices,a.position),y.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);B.clockwiseSort(h);var i=B.hull(h),j=B.centre(i);b.setVertices(a,i),B.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=A.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,B.translate(e.vertices,c),y.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,B.rotate(e.vertices,c,a.position),x.rotate(e.axes,c),y.update(e.bounds,e.vertices,a.velocity),d>0&&A.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
return c},r.off=function(a,b,c){if(!b)return void(a.events={});"function"==typeof b&&(c=b,b=p.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}},r.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=p.clone(c,!1),g.name=e,g.source=a;for(var i=0;i<f.length;i++)f[i].apply(a,[g])}}}}();var s={};!function(){s.create=function(b){var c={};return b||p.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()},s.setElement(c,c.element),c},s.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)},s.clearSourceEvents=function(a){a.sourceEvents.mousemove=null,a.sourceEvents.mousedown=null,a.sourceEvents.mouseup=null,a.sourceEvents.mousewheel=null,a.wheelDelta=0},s.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},s.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 t={};!function(){var a=60,b=a,c=1e3/a,d=window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.msRequestAnimationFrame||function(a){window.setTimeout(function(){a(p.now())},c)},e=window.cancelAnimationFrame||window.mozCancelAnimationFrame||window.webkitCancelAnimationFrame||window.msCancelAnimationFrame;t.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};r.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.fp
i.wireframes?D.setBackground(d,i.wireframeBackground):D.setBackground(d,i.background);var m=d.bounds.max.x-d.bounds.min.x,n=d.bounds.max.y-d.bounds.min.y,o=m/d.options.width,p=n/d.options.height;if(i.hasBounds){for(b=0;b<j.length;b++){var q=j[b];q.render.sprite.visible=y.overlaps(q.bounds,d.bounds)}for(b=0;b<k.length;b++){var r=k[b],s=r.bodyA,t=r.bodyB,u=r.pointA,v=r.pointB;s&&(u=A.add(s.position,r.pointA)),t&&(v=A.add(t.position,r.pointB)),u&&v&&(y.contains(d.bounds,u)||y.contains(d.bounds,v))&&l.push(r)}h.scale.set(1/o,1/p),h.position.set(-d.bounds.min.x*(1/o),-d.bounds.min.y*(1/p))}else l=k;for(b=0;b<j.length;b++)D.body(a,j[b]);for(b=0;b<l.length;b++)D.constraint(a,l[b]);f.render(g)},D.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===p.indexOf(h.children,k)&&h.addChild(k),k.clear(),k.beginFill(0,0),k.lineStyle(i.lineWidth,p.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()},D.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.spriteBatch;h||(h=e.sprites[g]=a(e,d)),-1===p.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===p.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=p.colorToNumber(d.fillStyle),h=p.colorToNumber(d.strokeStyle),i=p.colorToNumber(d.strokeStyle),j=p.colorToNumber("#bbb"),k=p.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,q.run=t.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=l,a.SAT=m,a.Constraint=n,a.MouseConstraint=o,a.Common=p,a.Engine=q,a.Mouse=s,a.Sleeping=u,a.Bodies=v,a.Composites=w,a.Axes=x,a.Bounds=y,a.Vector=A,a.Vertices=B,a.Render=C,a.RenderPixi=D,a.Events=r,a.Query=j,a.Runner=t,a.Svg=z,"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)}();