mirror of
https://github.com/liabru/matter-js.git
synced 2024-12-25 13:39:06 -05:00
fixed issue with MouseConstraint.create, closes #185
This commit is contained in:
parent
37a86cd321
commit
dff0e30cbc
3 changed files with 16 additions and 12 deletions
|
@ -3763,11 +3763,13 @@ var Bounds = require('../geometry/Bounds');
|
|||
MouseConstraint.create = function(engine, options) {
|
||||
var mouse = (engine ? engine.mouse : null) || (options ? options.mouse : null);
|
||||
|
||||
if (!mouse && engine && engine.render && engine.render.canvas) {
|
||||
mouse = Mouse.create(engine.render.canvas);
|
||||
} else {
|
||||
mouse = Mouse.create();
|
||||
Common.log('MouseConstraint.create: options.mouse was undefined, engine.render.canvas was undefined, may not function as expected', 'warn');
|
||||
if (!mouse) {
|
||||
if (engine && engine.render && engine.render.canvas) {
|
||||
mouse = Mouse.create(engine.render.canvas);
|
||||
} else {
|
||||
mouse = Mouse.create();
|
||||
Common.log('MouseConstraint.create: options.mouse was undefined, engine.render.canvas was undefined, may not function as expected', 'warn');
|
||||
}
|
||||
}
|
||||
|
||||
var constraint = Constraint.create({
|
||||
|
|
4
build/matter.min.js
vendored
4
build/matter.min.js
vendored
|
@ -30,8 +30,8 @@ l.overlap<=0)return d.collided=!1,d;a.overlap<l.overlap?(c=a,d.axisBody=t):(c=l,
|
|||
}return c},t=function(e,t,o){for(var n=r.dot(t[0],o),i=n,s=1;s<t.length;s+=1){var a=r.dot(t[s],o);a>i?i=a:n>a&&(n=a)}e.min=n,e.max=i},o=function(e,t,o){for(var n,i,s,a,l=Number.MAX_VALUE,c=r._temp[0],d=t.vertices,u=e.position,p=0;p<d.length;p++)i=d[p],c.x=i.x-u.x,c.y=i.y-u.y,n=-r.dot(o,c),l>n&&(l=n,s=i);var f=s.index-1>=0?s.index-1:d.length-1;i=d[f],c.x=i.x-u.x,c.y=i.y-u.y,l=-r.dot(o,c),a=i;var v=(s.index+1)%d.length;return i=d[v],c.x=i.x-u.x,c.y=i.y-u.y,n=-r.dot(o,c),l>n&&(a=i),[s,a]}}()},{"../geometry/Vector":26,"../geometry/Vertices":27}],12:[function(e,t,o){var n={};t.exports=n;var i=e("../geometry/Vertices"),r=e("../geometry/Vector"),s=e("../core/Sleeping"),a=e("../geometry/Bounds"),l=e("../geometry/Axes"),c=e("../core/Common");!function(){var e=1e-6,t=.001;n.create=function(t){var o=t;o.bodyA&&!o.pointA&&(o.pointA={x:0,y:0}),o.bodyB&&!o.pointB&&(o.pointB={x:0,y:0});var n=o.bodyA?r.add(o.bodyA.position,o.pointA):o.pointA,i=o.bodyB?r.add(o.bodyB.position,o.pointB):o.pointB,s=r.magnitude(r.sub(n,i));
|
||||
o.length=o.length||s||e;var a={visible:!0,lineWidth:2,strokeStyle:"#666"};return o.render=c.extend(a,o.render),o.id=o.id||c.nextId(),o.label=o.label||"Constraint",o.type="constraint",o.stiffness=o.stiffness||1,o.angularStiffness=o.angularStiffness||0,o.angleA=o.bodyA?o.bodyA.angle:o.angleA,o.angleB=o.bodyB?o.bodyB.angle:o.angleB,o},n.solveAll=function(e,t){for(var o=0;o<e.length;o++)n.solve(e[o],t)},n.solve=function(o,n){var i=o.bodyA,a=o.bodyB,l=o.pointA,d=o.pointB;i&&!i.isStatic&&(o.pointA=r.rotate(l,i.angle-o.angleA),o.angleA=i.angle),a&&!a.isStatic&&(o.pointB=r.rotate(d,a.angle-o.angleB),o.angleB=a.angle);var u=l,p=d;if(i&&(u=r.add(i.position,l)),a&&(p=r.add(a.position,d)),u&&p){var f=r.sub(u,p),v=r.magnitude(f);0===v&&(v=e);var y=(v-o.length)/v,m=r.div(f,v),g=r.mult(f,.5*y*o.stiffness*n*n);if(!(Math.abs(1-v/o.length)<t*n)){var x,h,b,S,w,C,A,B;i&&!i.isStatic?(b={x:u.x-i.position.x+g.x,y:u.y-i.position.y+g.y},i.velocity.x=i.position.x-i.positionPrev.x,i.velocity.y=i.position.y-i.positionPrev.y,
|
||||
i.angularVelocity=i.angle-i.anglePrev,x=r.add(i.velocity,r.mult(r.perp(b),i.angularVelocity)),w=r.dot(b,m),A=i.inverseMass+i.inverseInertia*w*w):(x={x:0,y:0},A=i?i.inverseMass:0),a&&!a.isStatic?(S={x:p.x-a.position.x-g.x,y:p.y-a.position.y-g.y},a.velocity.x=a.position.x-a.positionPrev.x,a.velocity.y=a.position.y-a.positionPrev.y,a.angularVelocity=a.angle-a.anglePrev,h=r.add(a.velocity,r.mult(r.perp(S),a.angularVelocity)),C=r.dot(S,m),B=a.inverseMass+a.inverseInertia*C*C):(h={x:0,y:0},B=a?a.inverseMass:0);var P=r.sub(h,x),M=r.dot(m,P)/(A+B);M>0&&(M=0);var k,I={x:m.x*M,y:m.y*M};i&&!i.isStatic&&(k=r.cross(b,I)*i.inverseInertia*(1-o.angularStiffness),s.set(i,!1),k=c.clamp(k,-.01,.01),i.constraintImpulse.x-=g.x,i.constraintImpulse.y-=g.y,i.constraintImpulse.angle+=k,i.position.x-=g.x,i.position.y-=g.y,i.angle+=k),a&&!a.isStatic&&(k=r.cross(S,I)*a.inverseInertia*(1-o.angularStiffness),s.set(a,!1),k=c.clamp(k,-.01,.01),a.constraintImpulse.x+=g.x,a.constraintImpulse.y+=g.y,a.constraintImpulse.angle-=k,
|
||||
a.position.x+=g.x,a.position.y+=g.y,a.angle-=k)}}},n.postSolveAll=function(e){for(var t=0;t<e.length;t++){var o=e[t],n=o.constraintImpulse;if(0!==n.x||0!==n.y||0!==n.angle){for(var s=0;s<o.parts.length;s++){var c=o.parts[s];i.translate(c.vertices,n),s>0&&(c.position.x+=n.x,c.position.y+=n.y),0!==n.angle&&(i.rotate(c.vertices,n.angle,o.position),l.rotate(c.axes,n.angle),s>0&&r.rotateAbout(c.position,n.angle,o.position,c.position)),a.update(c.bounds,c.vertices,o.velocity)}n.angle=0,n.x=0,n.y=0}}}}()},{"../core/Common":14,"../core/Sleeping":20,"../geometry/Axes":23,"../geometry/Bounds":24,"../geometry/Vector":26,"../geometry/Vertices":27}],13:[function(e,t,o){var n={};t.exports=n;var i=e("../geometry/Vertices"),r=e("../core/Sleeping"),s=e("../core/Mouse"),a=e("../core/Events"),l=e("../collision/Detector"),c=e("./Constraint"),d=e("../body/Composite"),u=e("../core/Common"),p=e("../geometry/Bounds");!function(){n.create=function(t,o){var i=(t?t.mouse:null)||(o?o.mouse:null);!i&&t&&t.render&&t.render.canvas?i=s.create(t.render.canvas):(i=s.create(),
|
||||
u.log("MouseConstraint.create: options.mouse was undefined, engine.render.canvas was undefined, may not function as expected","warn"));var r=c.create({label:"Mouse Constraint",pointA:i.position,pointB:{x:0,y:0},length:.01,stiffness:.1,angularStiffness:1,render:{strokeStyle:"#90EE90",lineWidth:3}}),l={type:"mouseConstraint",mouse:i,body:null,constraint:r,collisionFilter:{category:1,mask:4294967295,group:0}},p=u.extend(l,o);return a.on(t,"tick",function(){var o=d.allBodies(t.world);n.update(p,o),e(p)}),p},n.update=function(e,t){var o=e.mouse,n=e.constraint,s=e.body;if(0===o.button){if(n.bodyB)r.set(n.bodyB,!1),n.pointA=o.position;else for(var c=0;c<t.length;c++)if(s=t[c],p.contains(s.bounds,o.position)&&l.canCollide(s.collisionFilter,e.collisionFilter))for(var d=s.parts.length>1?1:0;d<s.parts.length;d++){var u=s.parts[d];if(i.contains(u.vertices,o.position)){n.pointA=o.position,n.bodyB=e.body=s,n.pointB={x:o.position.x-s.position.x,y:o.position.y-s.position.y},n.angleB=s.angle,r.set(s,!1),
|
||||
a.position.x+=g.x,a.position.y+=g.y,a.angle-=k)}}},n.postSolveAll=function(e){for(var t=0;t<e.length;t++){var o=e[t],n=o.constraintImpulse;if(0!==n.x||0!==n.y||0!==n.angle){for(var s=0;s<o.parts.length;s++){var c=o.parts[s];i.translate(c.vertices,n),s>0&&(c.position.x+=n.x,c.position.y+=n.y),0!==n.angle&&(i.rotate(c.vertices,n.angle,o.position),l.rotate(c.axes,n.angle),s>0&&r.rotateAbout(c.position,n.angle,o.position,c.position)),a.update(c.bounds,c.vertices,o.velocity)}n.angle=0,n.x=0,n.y=0}}}}()},{"../core/Common":14,"../core/Sleeping":20,"../geometry/Axes":23,"../geometry/Bounds":24,"../geometry/Vector":26,"../geometry/Vertices":27}],13:[function(e,t,o){var n={};t.exports=n;var i=e("../geometry/Vertices"),r=e("../core/Sleeping"),s=e("../core/Mouse"),a=e("../core/Events"),l=e("../collision/Detector"),c=e("./Constraint"),d=e("../body/Composite"),u=e("../core/Common"),p=e("../geometry/Bounds");!function(){n.create=function(t,o){var i=(t?t.mouse:null)||(o?o.mouse:null);i||(t&&t.render&&t.render.canvas?i=s.create(t.render.canvas):(i=s.create(),
|
||||
u.log("MouseConstraint.create: options.mouse was undefined, engine.render.canvas was undefined, may not function as expected","warn")));var r=c.create({label:"Mouse Constraint",pointA:i.position,pointB:{x:0,y:0},length:.01,stiffness:.1,angularStiffness:1,render:{strokeStyle:"#90EE90",lineWidth:3}}),l={type:"mouseConstraint",mouse:i,body:null,constraint:r,collisionFilter:{category:1,mask:4294967295,group:0}},p=u.extend(l,o);return a.on(t,"tick",function(){var o=d.allBodies(t.world);n.update(p,o),e(p)}),p},n.update=function(e,t){var o=e.mouse,n=e.constraint,s=e.body;if(0===o.button){if(n.bodyB)r.set(n.bodyB,!1),n.pointA=o.position;else for(var c=0;c<t.length;c++)if(s=t[c],p.contains(s.bounds,o.position)&&l.canCollide(s.collisionFilter,e.collisionFilter))for(var d=s.parts.length>1?1:0;d<s.parts.length;d++){var u=s.parts[d];if(i.contains(u.vertices,o.position)){n.pointA=o.position,n.bodyB=e.body=s,n.pointB={x:o.position.x-s.position.x,y:o.position.y-s.position.y},n.angleB=s.angle,r.set(s,!1),
|
||||
a.trigger(e,"startdrag",{mouse:o,body:s});break}}}else n.bodyB=e.body=null,n.pointB=null,s&&a.trigger(e,"enddrag",{mouse:o,body:s})};var e=function(e){var t=e.mouse,o=t.sourceEvents;o.mousemove&&a.trigger(e,"mousemove",{mouse:t}),o.mousedown&&a.trigger(e,"mousedown",{mouse:t}),o.mouseup&&a.trigger(e,"mouseup",{mouse:t}),s.clearSourceEvents(t)}}()},{"../body/Composite":2,"../collision/Detector":5,"../core/Common":14,"../core/Events":16,"../core/Mouse":18,"../core/Sleeping":20,"../geometry/Bounds":24,"../geometry/Vertices":27,"./Constraint":12}],14:[function(e,t,o){var n={};t.exports=n,function(){n._nextId=0,n._seed=0,n.extend=function(e,t){var o,i,r;"boolean"==typeof t?(o=2,r=t):(o=1,r=!0),i=Array.prototype.slice.call(arguments,o);for(var s=0;s<i.length;s++){var a=i[s];if(a)for(var l in a)r&&a[l]&&a[l].constructor===Object?e[l]&&e[l].constructor!==Object?e[l]=a[l]:(e[l]=e[l]||{},n.extend(e[l],r,a[l])):e[l]=a[l]}return e},n.clone=function(e,t){return n.extend({},t,e)},n.keys=function(e){if(Object.keys)return Object.keys(e);
|
||||
var t=[];for(var o in e)t.push(o);return t},n.values=function(e){var t=[];if(Object.keys){for(var o=Object.keys(e),n=0;n<o.length;n++)t.push(e[o[n]]);return t}for(var i in e)t.push(e[i]);return t},n.shadeColor=function(e,t){var o=parseInt(e.slice(1),16),n=Math.round(2.55*t),i=(o>>16)+n,r=(o>>8&255)+n,s=(255&o)+n;return"#"+(16777216+65536*(255>i?1>i?0:i:255)+256*(255>r?1>r?0:r:255)+(255>s?1>s?0:s:255)).toString(16).slice(1)},n.shuffle=function(e){for(var t=e.length-1;t>0;t--){var o=Math.floor(n.random()*(t+1)),i=e[t];e[t]=e[o],e[o]=i}return e},n.choose=function(e){return e[Math.floor(n.random()*e.length)]},n.isElement=function(e){try{return e instanceof HTMLElement}catch(t){return"object"==typeof e&&1===e.nodeType&&"object"==typeof e.style&&"object"==typeof e.ownerDocument}},n.isArray=function(e){return"[object Array]"===Object.prototype.toString.call(e)},n.clamp=function(e,t,o){return t>e?t:e>o?o:e},n.sign=function(e){return 0>e?-1:1},n.now=function(){var e=window.performance||{};return e.now=function(){
|
||||
return e.now||e.webkitNow||e.msNow||e.oNow||e.mozNow||function(){return+new Date}}(),e.now()},n.random=function(t,o){return t="undefined"!=typeof t?t:0,o="undefined"!=typeof o?o:1,t+e()*(o-t)},n.colorToNumber=function(e){return e=e.replace("#",""),3==e.length&&(e=e.charAt(0)+e.charAt(0)+e.charAt(1)+e.charAt(1)+e.charAt(2)+e.charAt(2)),parseInt(e,16)},n.log=function(e,t){if(console&&console.log&&console.warn)switch(t){case"warn":console.warn("Matter.js:",e);break;case"error":console.log("Matter.js:",e)}},n.nextId=function(){return n._nextId++},n.indexOf=function(e,t){if(e.indexOf)return e.indexOf(t);for(var o=0;o<e.length;o++)if(e[o]===t)return o;return-1};var e=function(){return n._seed=(9301*n._seed+49297)%233280,n._seed/233280}}()},{}],15:[function(e,t,o){var n={};t.exports=n;var i=e("../body/World"),r=e("./Sleeping"),s=e("../collision/Resolver"),a=e("../render/Render"),l=e("../collision/Pairs"),c=(e("./Metrics"),e("../collision/Grid")),d=e("./Events"),u=e("../body/Composite"),p=e("../constraint/Constraint"),f=e("./Common"),v=e("../body/Body");
|
||||
|
|
|
@ -36,11 +36,13 @@ var Bounds = require('../geometry/Bounds');
|
|||
MouseConstraint.create = function(engine, options) {
|
||||
var mouse = (engine ? engine.mouse : null) || (options ? options.mouse : null);
|
||||
|
||||
if (!mouse && engine && engine.render && engine.render.canvas) {
|
||||
mouse = Mouse.create(engine.render.canvas);
|
||||
} else {
|
||||
mouse = Mouse.create();
|
||||
Common.log('MouseConstraint.create: options.mouse was undefined, engine.render.canvas was undefined, may not function as expected', 'warn');
|
||||
if (!mouse) {
|
||||
if (engine && engine.render && engine.render.canvas) {
|
||||
mouse = Mouse.create(engine.render.canvas);
|
||||
} else {
|
||||
mouse = Mouse.create();
|
||||
Common.log('MouseConstraint.create: options.mouse was undefined, engine.render.canvas was undefined, may not function as expected', 'warn');
|
||||
}
|
||||
}
|
||||
|
||||
var constraint = Constraint.create({
|
||||
|
|
Loading…
Reference in a new issue