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

8 lines
58 KiB
JavaScript
Raw Normal View History

2014-02-19 09:15:05 -05:00
/**
2014-12-28 13:37:43 -05:00
* matter.min.js 0.8.0-edge 2014-12-28
2014-02-19 09:15:05 -05:00
* http://brm.io/matter-js/
* License: MIT
*/
2014-12-28 13:37:43 -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){b.set(a,{bounds:a.bounds||y.create(a.vertices),positionPrev:a.positionPrev||z.clone(a.position),anglePrev:a.anglePrev||a.angle,vertices:a.vertices,isStatic:a.isStatic,isSleeping:a.isSleeping}),A.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":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":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;default:a[e]=d}},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.is
},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(o.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,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.cre