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

9 lines
68 KiB
JavaScript
Raw Normal View History

2014-02-19 09:15:05 -05:00
/**
2014-05-01 09:09:06 -04:00
* matter.min.js 0.7.0-edge 2014-05-01
2014-02-19 09:15:05 -05:00
* http://brm.io/matter-js/
* License: MIT
*/
2014-05-01 09:09:06 -04:00
!function(){var a={},b={};!function(){var a=1;b.create=function(a){var b={id:o.nextId(),type:"body",label:"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,timeScale:1,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=o.extend(b,a);return c(d),d},b.nextGroupId=function(){return a++};var c=function(a){a.vertices=a.vertices||z.fromPath(a.render.path),a.axes=a.axes||w.fromVertices(a.vertices),a.area=z.area(a.vertices),a.bounds=x.create(a.vertices),a.mass=a.mass||a.density*a.area,a.inverseMass=1/a.mass,a.inertia=a.inertia||z.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":o.choose(["#556270","#4ECDC4","#C7F464","#FF6B6B","#C44D58"])),a.render.strokeStyle=a.render.strokeStyle||o.shadeColor(a.render.fillStyle,-20),z.create(a.vertices,a);var c=z.centre(a.vertices);z.translate(a.vertices,a.position),z.translate(a.vertices,c,-1),z.rotate(a.vertices,a.angle,a.position),w.rotate(a.axes,a.angle),x.update(a.bounds,a.vertices,a.velocity),b.setStatic(a,a.isStatic),t.set(a,a.isSleeping)};b.setStatic=function(a,b){a.isStatic=b,b&&(a.restitution=0,a.friction=1,a.mass=a.inertia=a.density=1/0,a.inverseMass=a.inverseInertia=0,a.render.lineWidth=1,a.positionPrev.x=a.position.x,a.positionPrev.y=a.position.y,a.anglePrev=a.angle,a.angularVelocity=0,a.speed=0,a.angularSpeed=0,a.motion=0)},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*a.timeScale,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=y.magnitude(a.velocity),a.angularSpeed=Math.abs(a.angularVelocity),z.translate(a.vertices,a.velocity),0!==a.angularVelocity&&(z.rotate(a.vertices,a.angularVelocity,a.position),w.rotate(a.axes,a.angularVelocity)),x.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,z.translate(a.vertices,b),x.update(a.bounds,a.vertices,a.velocity)},b.rotate=function(a,b){a.anglePrev+=b,a.angle+=b,z.rotate(a.vertices,b,a.position),w.rotate(a.axes,b),x.update(a.bounds,a.vertices,a.velocity)},b.scale=function(a,b,c,d){z.scale(a.vertices,b,c,d),a.axes=w.fromVertices(a.vertices),a.area=z.area(a.vertices),a.mass=a.density*a.area,a.inverseMass=1/a.mass,z.translate(a.vertices,{x:-a.position.x,y:-a.position.y}),a.inertia=z.inertia(a.vertices,a.mass),a.inverseInertia=1/a.inertia,z.translate(a.vertices,{x:a.position.x,y:a.position.y}),x.update(a.bounds,a.vertices,a.velocity)}}();var c={};!function(){c.create=function(a){return o.extend({id:o.nextId(),type:"composite",parent:null,isModified:!1,bodies:[],constraints:[],composites:[],label:"Composite"},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.co
c.addConstraint(a,m.create(s))}return a.label+=" Chain",a},v.mesh=function(a,b,d,e,f){var g,h,i,j,k,l=a.bodies;for(g=0;d>g;g++){for(h=0;b>h;h++)h>0&&(i=l[h-1+g*b],j=l[h+g*b],c.addConstraint(a,m.create(o.extend({bodyA:i,bodyB:j},f))));for(h=0;b>h;h++)g>0&&(i=l[h+(g-1)*b],j=l[h+g*b],c.addConstraint(a,m.create(o.extend({bodyA:i,bodyB:j},f))),e&&h>0&&(k=l[h-1+(g-1)*b],c.addConstraint(a,m.create(o.extend({bodyA:k,bodyB:j},f)))),e&&b-1>h&&(k=l[h+1+(g-1)*b],c.addConstraint(a,m.create(o.extend({bodyA:k,bodyB:j},f)))))}return a.label+=" Mesh",a},v.pyramid=function(a,c,d,e,f,g,h){return v.stack(a,c,d,e,f,g,function(c,g,i,j,k,l){var m=Math.min(e,Math.ceil(d/2)),n=k?k.bounds.max.x-k.bounds.min.x:0;if(!(j>m)){j=m-j;var o=j,p=d-1-j;if(!(o>i||i>p)){1===l&&b.translate(k,{x:(i+(d%2===1?1:-1))*n,y:0});var q=k?i*n:0;return h(a+q+i*f,g,i,j,k,l)}}})},v.newtonsCradle=function(a,b,d,e,f){for(var g=c.create({label:"Newtons Cradle"}),h=0;d>h;h++){var i=1.9,j=u.circle(a+h*e*i,b+f,e,{inertia:99999,restitution:1,friction:0,frictionAir:1e-4,slop:.01}),k=m.create({pointA:{x:a+h*e*i,y:b},bodyB:j});c.addBody(g,j),c.addConstraint(g,k)}return g},v.car=function(a,d,e,f,g){var h=b.nextGroupId(),i=-20,j=.5*-e+i,k=.5*e-i,l=0,n=c.create({label:"Car"}),o=u.trapezoid(a,d,e,f,.3,{groupId:h,friction:.01}),p=u.circle(a+j,d+l,g,{groupId:h,restitution:.5,friction:.9,density:.01}),q=u.circle(a+k,d+l,g,{groupId:h,restitution:.5,friction:.9,density:.01}),r=m.create({bodyA:o,pointA:{x:j,y:l},bodyB:p,stiffness:.5}),s=m.create({bodyA:o,pointA:{x:k,y:l},bodyB:q,stiffness:.5});return c.addBody(n,o),c.addBody(n,p),c.addBody(n,q),c.addConstraint(n,r),c.addConstraint(n,s),n},v.softBody=function(a,b,c,d,e,f,g,h,i,j){i=o.extend({inertia:1/0},i),j=o.extend({stiffness:.4},j);var k=v.stack(a,b,c,d,e,f,function(a,b){return u.circle(a,b,h,i)});return v.mesh(k,c,d,g,j),k.label="Soft Body",k}}();var w={};!function(){w.fromVertices=function(a){for(var b={},c=0;c<a.length;c++){var d=(c+1)%a.length,e=y.normalise({x:a[d].y-a[c].y,y:a[c].x-a[d].x}),f=0===e.y?1/0:e.x/e.y;f=f.toFixed(3).toString(),b[f]=e}return o.values(b)},w.rotate=function(a,b){if(0!==b)for(var c=Math.cos(b),d=Math.sin(b),e=0;e<a.length;e++){var f,g=a[e];f=g.x*c-g.y*d,g.y=g.x*d+g.y*c,g.x=f}}}();var x={};!function(){x.create=function(a){var b={min:{x:0,y:0},max:{x:0,y:0}};return a&&x.update(b,a),b},x.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)},x.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},x.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},x.translate=function(a,b){a.min.x+=b.x,a.max.x+=b.x,a.min.y+=b.y,a.max.y+=b.y},x.shift=function(a,b){var c=a.max.x-a.min.x,d=a.max.y-a.min.y;a.min.x=b.x,a.max.x=b.x+c,a.min.y=b.y,a.max.y=b.y+d}}();var y={};!function(){y.magnitude=function(a){return Math.sqrt(a.x*a.x+a.y*a.y)},y.magnitudeSquared=function(a){return a.x*a.x+a.y*a.y},y.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}},y.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)}},y.normalise=function(a){var b=y.magnitude(a);return 0===b?{x:0,y:0}:{x:a.x/b,y:a.y/b}},y.dot=function(a,b){return a.x*b.x+a.y*b.y},y.cross=function(a,b){return a.x*b.y-a.y*b.x},y.add=function(a,b){return{x:a.x+b.x,y:a.y+b.y}},y.sub=function(a,b){return{x:a.x-b.x,y:a.y-b.y}},y.mult=function(a,b){return{x:a.x*b,y:a.y*b}},y.div=function(a,b){return{x:a.x/b,y:a.y/b}},y.perp=function(a,b){return b=b===!0?-1:1,{x:b*-a.y,y:b*a.x}},y.neg=function(a){return{x:-a.x,y:-a.y}},y.angle=function(a,b){return Math.atan2(b.y-a.y,b.x-a.x)}}();var z={};!function(){z.create=function(a,b){for(var c=0;c<a.length;c++)a[c].index=c,a[c].body=b},z.fromPath=function(a){var b=/L\s*([\-\
b.translate(f,i),f.positionPrev.x=f.position.x,f.positionPrev.y=f.position.y;break;case"constraint":var j=f.pointA;f.bodyA&&(j=f.pointB),j.x=c-e.mousedownOffset.x,j.y=d-e.mousedownOffset.y;var k=f.bodyA?y.add(f.bodyA.position,f.pointA):f.pointA,l=f.bodyB?y.add(f.bodyB.position,f.pointB):f.pointB;f.length=y.magnitude(y.sub(k,l))}},t=function(a,c,d){for(var e,f,g=a.selected,h=0;h<g.length;h++)switch(e=g[h],f=e.data,f.type){case"body":b.scale(f,c,d,f.position),f.circleRadius&&(f.circleRadius*=c)}},u=function(a,c){for(var d,e,f=a.selected,g=0;g<f.length;g++)switch(d=f[g],e=d.data,e.type){case"body":b.rotate(e,c)}},v=function(a,b){b?(a.autoRewind&&(w(a,[]),C.loadState(a.serializer,a.engine,"pauseState")),a.engine.timing.timeScale=0,a.isPaused=!0,a.controls.pauseButton.text("Play"),q.trigger(a,"paused")):(a.autoRewind&&C.saveState(a.serializer,a.engine,"pauseState"),a.engine.timing.timeScale=1,a.isPaused=!1,a.controls.pauseButton.text("Pause"),q.trigger(a,"play"))},w=function(a,b){var c,d,e=a.controls.worldTree.data("jstree");for(d=0;d<a.selected.length;d++)c=a.selected[d].data,e.deselect_node(c.type+"_"+c.id,!0);for(a.selected=[],console.clear(),d=0;d<b.length;d++)c=b[d],c&&(A(a,c),5>d?console.log(c.label+" "+c.id+": %O",c):6===d&&console.warn("Omitted inspecting "+(b.length-5)+" more objects"))},A=function(a,b){if(b){var c=a.controls.worldTree.data("jstree");a.selected.push({data:b}),c.select_node(b.type+"_"+b.id,!0)}},B=function(a,b){b[0].state=b[0].state||{opened:!0},a.settings.core.data=b,a.refresh(-1)},E=function(a,b,c){var d=[],e={id:"composite_"+a.id,data:{compositeId:b},type:"composite",text:(a.label?a.label:"Composite")+" "+a.id,li_attr:{"class":"jstree-node-type-composite"}},f=F(a.composites,a.id);return f.id="composites_"+a.id,d.push(f),c?f.children:(f=G(a.bodies,a.id),f.id="bodies_"+a.id,d.push(f),f=H(a.constraints,a.id),f.id="constraints_"+a.id,d.push(f),e.children=d,e)},F=function(a,b){for(var c={type:"composites",text:"Composites",data:{compositeId:b},children:[],li_attr:{"class":"jstree-node-type-composites"}},d=0;d<a.length;d++){var e=a[d];c.children.push(E(e,b))}return c},G=function(a,b){for(var c={type:"bodies",text:"Bodies",data:{compositeId:b},children:[],li_attr:{"class":"jstree-node-type-bodies"}},d=0;d<a.length;d++){var e=a[d];c.children.push({type:"body",id:"body_"+e.id,data:{compositeId:b},text:(e.label?e.label:"Body")+" "+e.id,li_attr:{"class":"jstree-node-type-body"}})}return c},H=function(a,b){for(var c={type:"constraints",text:"Constraints",data:{compositeId:b},children:[],li_attr:{"class":"jstree-node-type-constraints"}},d=0;d<a.length;d++){var e=a[d];c.children.push({type:"constraint",id:"constraint_"+e.id,data:{compositeId:b},text:(e.label?e.label:"Constraint")+" "+e.id,li_attr:{"class":"jstree-node-type-constraint"}})}return c},I=function(a){var b=c.create();c.add(a.root,b),a.root.composites.splice(a.root.composites.length-1,1),a.root.composites.unshift(b),c.setModified(a.engine.world,!0,!0,!1)},J=function(a){a.engine;if(0===a.selected.length)return void alert("No objects were selected, so export could not be created. Can only export objects that are in the World composite.");for(var b="export-objects",d=c.create({label:"Exported Objects"}),f=0;f<a.selected.length;f++){var g=a.selected[f].data;c.get(d,g.id,g.type)||(c.add(d,g),1===a.selected.length&&(b="export-"+g.label+"-"+g.id))}b=b.toLowerCase().replace(/[^\w\-]/g,"")+".json";var h=C.serialise(a.serializer,d,a.exportIndent);if(e){var i=new Blob([h],{type:"application/json"}),j=document.createElement("a");j.download=b,j.href=(window.webkitURL||window.URL).createObjectURL(i),j.dataset.downloadurl=["application/json",j.download,j.href].join(":"),j.click()}else window.open("data:application/json;charset=utf-8,"+escape(h));q.trigger(a,"export")},K=function(a){var b,d=(a.engine,document.createElement("div"));d.innerHTML='<input type="file">',b=d.firstChild,b.addEventListener("change",function(){var d=b.files[0];if(d.name.match(/\.(txt|json)$/)){var e=new FileReader;e.onload=function(){var b=a.serializer.parse(e.result);if(b){b.label="Imported O