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

8 lines
56 KiB
JavaScript
Raw Normal View History

2014-02-19 09:15:05 -05:00
/**
2014-12-02 16:40:34 -05:00
* matter.min.js 0.8.0-edge 2014-12-02
2014-02-19 09:15:05 -05:00
* http://brm.io/matter-js/
* License: MIT
*/
2014-12-02 16:40:34 -05:00
!function(){var a={},b={};!function(){b._inertiaScale=4;var a=1,c=-1;b.create=function(a){var b={id:o.nextId(),type:"body",label:"Body",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},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,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 d(c,a),c},b.nextGroup=function(b){return b?c--:a++};var d=function(a,c){a.bounds=a.bounds||y.create(a.vertices),a.positionPrev=a.positionPrev||z.clone(a.position),a.anglePrev=a.anglePrev||a.angle,b.setVertices(a,a.vertices),b.setStatic(a,a.isStatic),u.set(a,a.isSleeping),A.rotate(a.vertices,a.angle,a.position),x.rotate(a.axes,a.angle),y.update(a.bounds,a.vertices,a.velocity),a.axes=c.axes||a.axes,a.area=c.area||a.area,b.setMass(a,c.mass||a.mass),b.setInertia(a,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.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.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.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=x.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),y.update(a.bounds,a.vertices,a.velocity)},b.setPosition=function(a,b){var c=z.sub(b,a.position);a.position.x=b.x,a.position.y=b.y,a.positionPrev.x+=c.x,a.positionPrev.y+=c.y,A.translate(a.vertices,c),y.update(a.bounds,a.vertices,a.velocity)},b.setAngle=function(a,b){var c=b-a.angle;a.angle=b,a.anglePrev+=c,A.rotate(a.vertices,c,a.position),x.rotate(a.axes,c),y.update(a.bounds,a.vertices,a.velocity)},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.velocity.y=b.y,a.speed=z.magnitude(a.velocity)},b.setAngularVelocity=function(a,b){a.anglePrev=a.angle-b,a.angularVelocity=b,a.angularSpeed=Math.abs(a.angularVelocity)},b.translate=function(a,c){b.setPosition(a,z.add(a.position,c))},b.rotate=function(a,c){b.setAngle(a,a.angle+c)},b.scale=function(a,c,d,e){A.scale(a.vertices,c,d,e),a.axes=x.fromVertices(a.vertices),a.area=A.area(a.vertices),b.setMass(a,a.density*a.area),A.translate(a.vertices,{x:-a.position.x,y:-a.position.y}),b.setInertia(a,A.inertia(a.vertices,a.mass)),A.translate(a.vertices,{x:a.position.x,y:a.position.y}),y.update(a.bounds,a.vertices,a.velocity)},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,f){for(var g=0;g<a.length;g++){var h=a[g];h.isStatic||h.isSleeping||h.bounds.max.x<f.min.x||h.bounds.min.x>f.max.x||h.bounds.max.y<f.min.y||h.bounds.min.y>f.max.y||b.update(h,c,d,e)}},b.update=function(a,b,c,d){var e=Math.pow(b*c*a.timeScale,2),f=1-a.frictionAir*c*a.timeScale,g=a.position.x-a.positionPrev.x,h=a.position.y-a.positionPrev.y;a.velocity.x=g*f*d+a.force.x/a.mass*e,a.velocity.y=h*f*d+a.force.y/a.mass*e,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.
2014-07-30 12:29:21 -04:00
},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,m=a.timing;if(m.frameRequestId=d(i),a.enabled){var n={timestamp:j};q.trigger(a,"beforeTick",n),m.isFixed?k=m.delta:(k=j-c||m.delta,c=j,g.push(k),g=g.slice(-b),k=Math.min.apply(null,g),k=k<m.deltaMin?m.deltaMin:k,k=k>m.deltaMax?m.deltaMax:k,l=k/m.delta,m.delta=k),0!==h&&(l*=m.timeScale/h),0===m.timeScale&&(l=0),h=m.timeScale,f+=1,j-e>=1e3&&(m.fps=f*((j-e)/1e3),e=j,f=0),q.trigger(a,"tick",n),a.world.isModified&&a.render.controller.clear(a.render),p.update(a,k,l),p.render(a),q.trigger(a,"afterTick",n)}}()},t.stop=function(a){e(a.timing.frameRequestId)}}();var u={};!function(){u._motionWakeThreshold=.18,u._motionSleepThreshold=.08,u._minBias=.9,u.update=function(a,b){for(var c=b*b*b,d=0;d<a.length;d++){var e=a[d],f=e.speed*e.speed+e.angularSpeed*e.angularSpeed;if(e.force.x>0||e.force.y>0)u.set(e,!1);else{var g=Math.min(e.motion,f),h=Math.max(e.motion,f);e.motion=u._minBias*g+(1-u._minBias)*h,e.sleepThreshold>0&&e.motion<u._motionSleepThreshold*c?(e.sleepCounter+=1,e.sleepCounter>=e.sleepThreshold&&u.set(e,!0)):e.sleepCounter>0&&(e.sleepCounter-=1)}}},u.afterCollisions=function(a,b){for(var c=b*b*b,d=0;d<a.length;d++){var e=a[d];if(e.isActive){var f=e.collision,g=f.bodyA,h=f.bodyB;if(!(g.isSleeping&&h.isSleeping||g.isStatic||h.isStatic)&&(g.isSleeping||h.isSleeping)){var i=g.isSleeping&&!g.isStatic?g:h,j=i===g?h:g;!i.isStatic&&j.motion>u._motionWakeThreshold*c&&u.set(i,!1)}}}},u.set=function(a,b){b?(a.isSleeping=!0,a.sleepCounter=a.sleepThreshold,a.positionImpulse.x=0,a.positionImpulse.y=0,a.positionPrev.x=a.position.x,a.positionPrev.y=a.position.y,a.anglePrev=a.angle,a.speed=0,a.angularSpeed=0,a.motion=0):(a.isSleeping=!1,a.sleepCounter=0)}}();var v={};!function(){v.rectangle=function(a,c,d,e,f){f=f||{};var g={label:"Rectangle Body",position:{x:a,y:c},vertices:A.fromPath("L 0 0 L "+d+" 0 L "+d+" "+e+" L 0 "+e)};if(f.chamfer){var h=f.chamfer;g.vertices=A.chamfer(g.vertices,h.radius,h.quality,h.qualityMin,h.qualityMax),delete f.chamfer}return b.create(o.extend({},g,f))},v.trapezoid=function(a,c,d,e,f,g){g=g||{},f*=.5;var h=(1-2*f)*d,i=d*f,j=i+h,k=j+i,l={label:"Trapezoid Body",position:{x:a,y:c},vertices:A.fromPath("L 0 0 L "+i+" "+-e+" L "+j+" "+-e+" L "+k+" 0")};if(g.chamfer){var m=g.chamfer;l.vertices=A.chamfer(l.vertices,m.radius,m.quality,m.qualityMin,m.qualityMax),delete g.chamfer}return b.create(o.extend({},l,g))},v.circle=function(a,b,c,d,e){d=d||{},d.label="Circle Body",e=e||25;var f=Math.ceil(Math.max(10,Math.min(e,c)));return f%2===1&&(f+=1),d.circleRadius=c,v.polygon(a,b,f,c,d)},v.polygon=function(a,c,d,e,f){if(f=f||{},3>d)return v.circle(a,c,e,f);for(var g=2*Math.PI/d,h="",i=.5*g,j=0;d>j;j+=1){var k=i+j*g,l=Math.cos(k)*e,m=Math.sin(k)*e;h+="L "+l.toFixed(3)+" "+m.toFixed(3)+" "}var n={label:"Polygon Body",position:{x:a,y:c},vertices:A.fromPath(h)};if(f.chamfer){var p=f.chamfer;n.vertices=A.chamfer(n.vertices,p.radius,p.quality,p.qualityMin,p.qualityMax),delete f.chamfer}return b.create(o.extend({},n,f))}}();var w={};!function(){w.stack=function(a,d,e,f,g,h,i){for(var j,k=c.create({label:"Stack"}),l=a,m=d,n=0,o=0;f>o;o++){for(var p=0,q=0;e>q;q++){var r=i(l,m,q,o,j,n);if(r){var s=r.bounds.max.y-r.bounds.min.y,t=r.bounds.max.x-r.bounds.min.x;s>p&&(p=s),b.translate(r,{x:.5*t,y:.5*s}),l=r.bounds.max.x+g,c.addBody(k,r),j=r,n+=1}}m+=p+h,l=a}return k},w.chain=function(a,b,d,e,f,g){for(var h=a.bodies,i=1;i<h.length;i++){var j=h[i-1],k=h[i],l=j.bounds.max.y-j.bounds.min.y,n=j.bounds.max.x-j.bounds.min.x,p=k.bounds.max.y-k.bounds.min.y,q=k.bounds.max.x-k.bounds.min.x,r={bodyA:j,pointA:{x:n*b,y:l*d},bodyB:k,pointB:{x:q*e,y:p*f}},s=o.extend(r,g);c.addConstraint(a,m.create(s))}return a.label+=" Chain",a},w.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.addConst