mirror of
https://github.com/liabru/matter-js.git
synced 2024-11-27 09:50:52 -05:00
8 lines
37 KiB
JavaScript
8 lines
37 KiB
JavaScript
|
/**
|
||
|
* matter-0.5.0.min.js 0.5.0-alpha 0.5.0 2014-02-28
|
||
|
* http://brm.io/matter-js/
|
||
|
* License: MIT
|
||
|
*/
|
||
|
|
||
|
!function(){var a={},b={};!function(){var a=0,c=1;b.create=function(a){var c={id:b.nextId(),angle:0,position:{x:0,y:0},force:{x:0,y:0},torque:0,positionImpulse:{x:0,y: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,path:"L 0 0 L 40 0 L 40 40 L 0 40",fillStyle:a.isStatic?"#eeeeee":n.choose(["#556270","#4ECDC4","#C7F464","#FF6B6B","#C44D58"]),lineWidth:1.5,groupId:0,slop:.05},d=n.extend(c,a);return b.updateProperties(d),d},b.nextId=function(){return a++},b.nextGroupId=function(){return c++},b.updateProperties=function(a){a.vertices=a.vertices||x.fromPath(a.path),a.axes=a.axes||u.fromVertices(a.vertices),a.area=x.area(a.vertices),a.bounds=v.create(a.vertices),a.mass=a.mass||a.density*a.area,a.inverseMass=1/a.mass,a.inertia=a.inertia||x.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.strokeStyle=a.strokeStyle||n.shadeColor(a.fillStyle,-20),x.create(a.vertices,a);var b=x.centre(a.vertices);x.translate(a.vertices,a.position),x.translate(a.vertices,b,-1),x.rotate(a.vertices,a.angle,a.position),u.rotate(a.axes,a.angle),v.update(a.bounds,a.vertices,a.velocity),a.isStatic&&(a.restitution=0,a.friction=1,a.mass=a.inertia=a.density=1/0,a.inverseMass=a.inverseInertia=0,a.lineWidth=1),r.set(a,a.isSleeping)},b.resetForcesAll=function(a,b){for(var c=0;c<a.length;c++){var d=a[c];d.isStatic||d.isSleeping||(d.force.x=0,d.force.y=0,d.torque=0,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,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=w.magnitude(a.velocity),a.angularSpeed=Math.abs(a.angularVelocity),x.translate(a.vertices,a.velocity),x.rotate(a.vertices,a.angularVelocity,a.position),u.rotate(a.axes,a.angularVelocity),v.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,x.translate(a.vertices,b),v.update(a.bounds,a.vertices,a.velocity)},b.rotate=function(a,b){a.anglePrev+=b,a.angle+=b,x.rotate(a.vertices,b,a.position),u.rotate(a.axes,b),v.update(a.bounds,a.vertices,a.velocity)}}();var c={};!function(){c.create=function(a){return n.extend({bodies:[],constraints:[],composites:[]},a)},c.add=function(a,b){return a.bodies&&b.bodies&&(a.bodies=a.bodies.concat(b.bodies)),a.constraints&&b.constraints&&(a.constraints=a.constraints.concat(b.constraints)),a.composites&&b.composites&&(a.composites=a.composites.concat(b.composites)),a},c.addBody=function(a,b){return a.bodies=a.bodies||[],a.bodies.push(b),a},c.addConstraint=function(a,b){return a.constraints=a.constraints||[],a.constraints.push(b),a}}();var d={};!function(){d.create=function(a){var b={gravity:{x:0,y:1},bodies:[],constraints:[],bounds:{min:{x:0,y:0},max:{x:800,y:600}}};return n.extend(b,a)},d.clear=function(a,b){a.bodies=b?a.bodies.filter(function(a){return a.isStatic}):[],a.constraints=[]}}();var e={};!function(){e.create=function(a){return{id:e.id(a),vertex:a,normalImpulse:0,tangentImpulse:0}},e.id=function(a){return a.body.id+"_"+a.index}}();var f={};!function(){f.collisions=function(a,b){for(var c=[],d=0;d<a.length;d++){var e=a[d][0],f=a[d][1];if(!(e.groupId&&f.groupId&&e.groupId===f.groupId||(e.isStatic||e.isSleeping)&&(f.isStatic
|
||
|
i+="delta: "+a.timing.delta.toFixed(3)+h,i+="fps: "+Math.round(a.timing.fps)+h,i+="correction: "+a.timing.correction.toFixed(3)+h,i+="bodies: "+d.bodies.length+h,a.broadphase.controller===g&&(i+="buckets: "+a.metrics.buckets+h),i+="\n",i+="collisions: "+a.metrics.collisions+h,i+="pairs: "+a.pairs.length+h,i+="broad: "+a.metrics.broadEff+h,i+="mid: "+a.metrics.midEff+h,i+="narrow: "+a.metrics.narrowEff+h,e.debugString=i,e.debugTimestamp=a.timing.timestamp}if(e.debugString){c.font="12px Arial",c.fillStyle=f.wireframes?"rgba(255,255,255,0.5)":"rgba(0,0,0,0.5)";for(var j=e.debugString.split("\n"),k=0;k<j.length;k++)c.fillText(j[k],50,50+18*k)}},z.constraint=function(a,b){var c=a.bodyA,d=a.bodyB,e=b;a.pointA&&a.pointB&&(c?(e.beginPath(),e.moveTo(c.position.x+a.pointA.x,c.position.y+a.pointA.y)):(e.beginPath(),e.moveTo(a.pointA.x,a.pointA.y)),d?e.lineTo(d.position.x+a.pointB.x,d.position.y+a.pointB.y):e.lineTo(a.pointB.x,a.pointB.y),e.lineWidth=a.lineWidth,e.strokeStyle=a.strokeStyle,e.stroke())},z.bodyShadow=function(a,b,c){var d=c,e=a.render;if(b.circleRadius)d.beginPath(),d.arc(b.position.x,b.position.y,b.circleRadius,0,2*Math.PI),d.closePath();else{d.beginPath(),d.moveTo(b.vertices[0].x,b.vertices[0].y);for(var f=1;f<b.vertices.length;f++)d.lineTo(b.vertices[f].x,b.vertices[f].y);d.closePath()}var g=b.position.x-.5*e.options.width,h=b.position.y-.2*e.options.height,i=Math.abs(g)+Math.abs(h);d.shadowColor="rgba(0,0,0,0.15)",d.shadowOffsetX=.05*g,d.shadowOffsetY=.05*h,d.shadowBlur=1+12*Math.min(1,i/1e3),d.fill(),d.shadowColor=null,d.shadowOffsetX=null,d.shadowOffsetY=null,d.shadowBlur=null},z.body=function(a,b,c){var d=c,e=a.render,f=e.options;if(f.showBounds&&(d.beginPath(),d.rect(b.bounds.min.x,b.bounds.min.y,b.bounds.max.x-b.bounds.min.x,b.bounds.max.y-b.bounds.min.y),d.lineWidth=1,d.strokeStyle=f.wireframes?"rgba(255,255,255,0.08)":"rgba(0,0,0,0.1)",d.stroke()),b.circleRadius)d.beginPath(),d.arc(b.position.x,b.position.y,b.circleRadius,0,2*Math.PI),d.closePath();else{d.beginPath(),d.moveTo(b.vertices[0].x,b.vertices[0].y);for(var g=1;g<b.vertices.length;g++)d.lineTo(b.vertices[g].x,b.vertices[g].y);d.closePath()}if(f.wireframes?(d.lineWidth=1,d.strokeStyle="#bbb",f.showSleeping&&b.isSleeping&&(d.strokeStyle="rgba(255,255,255,0.2)"),d.stroke()):(d.fillStyle=b.fillStyle,f.showSleeping&&b.isSleeping&&(d.fillStyle=n.shadeColor(b.fillStyle,50)),d.lineWidth=b.lineWidth,d.strokeStyle=b.strokeStyle,d.fill(),d.stroke()),f.showAngleIndicator&&!f.showAxes&&(d.beginPath(),d.moveTo(b.position.x,b.position.y),d.lineTo((b.vertices[0].x+b.vertices[b.vertices.length-1].x)/2,(b.vertices[0].y+b.vertices[b.vertices.length-1].y)/2),d.lineWidth=1,d.strokeStyle=f.wireframes?"indianred":b.strokeStyle,d.stroke()),f.showAxes)for(var h=0;h<b.axes.length;h++){var i=b.axes[h];d.beginPath(),d.moveTo(b.position.x,b.position.y),d.lineTo(b.position.x+20*i.x,b.position.y+20*i.y),d.lineWidth=1,d.strokeStyle=f.wireframes?"indianred":b.strokeStyle,d.stroke()}f.showPositions&&(d.beginPath(),d.arc(b.position.x,b.position.y,3,0,2*Math.PI,!1),d.fillStyle=f.wireframes?"indianred":"rgba(0,0,0,0.5)",d.fill(),d.beginPath(),d.arc(b.positionPrev.x,b.positionPrev.y,2,0,2*Math.PI,!1),d.fillStyle="rgba(255,165,0,0.8)",d.fill()),f.showVelocity&&(d.beginPath(),d.moveTo(b.position.x,b.position.y),d.lineTo(b.position.x+2*(b.position.x-b.positionPrev.x),b.position.y+2*(b.position.y-b.positionPrev.y)),d.lineWidth=3,d.strokeStyle="cornflowerblue",d.stroke()),f.showIds&&(d.font="12px Arial",d.fillStyle="rgba(255,255,255,0.5)",d.fillText(b.id,b.position.x+10,b.position.y-10))},z.collision=function(a,b,c){for(var d=c,e=b.collision,f=a.render.options,g=0;g<b.activeContacts.length;g++){var h=b.activeContacts[g],i=h.vertex;d.beginPath(),d.rect(i.x-1.5,i.y-1.5,3.5,3.5),d.fillStyle=f.wireframes?"rgba(255,255,255,0.7)":"orange",d.fill()}if(b.activeContacts.length>0){var j=b.activeContacts[0].vertex.x,k=b.activeContacts[0].vertex.y;2===b.activeContacts.length&&(j=(b.activeContacts[0].vertex.x+b.activeContacts[1].vertex.x)/2,k=(b.activeContacts[0].vertex.y+b.activeContacts[1].vertex.y)/
|