phaser/build/p2.min.js

26 lines
77 KiB
JavaScript
Raw Normal View History

/**
* The MIT License (MIT)
*
* Copyright (c) 2013 p2.js authors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
!function(a){"object"==typeof exports?module.exports=a():"function"==typeof define&&define.amd?define(a):"undefined"!=typeof window?window.p2=a():"undefined"!=typeof global?self.p2=a():"undefined"!=typeof self&&(self.p2=a())}(function(){return function a(b,c,d){function e(g,h){if(!c[g]){if(!b[g]){var i="function"==typeof require&&require;if(!h&&i)return i(g,!0);if(f)return f(g,!0);throw new Error("Cannot find module '"+g+"'")}var j=c[g]={exports:{}};b[g][0].call(j.exports,function(a){var c=b[g][1][a];return e(c?c:a)},j,j.exports,a,b,c,d)}return c[g].exports}for(var f="function"==typeof require&&require,g=0;g<d.length;g++)e(d[g]);return e}({1:[function(a,b,c){var d={},e=new Float32Array([1,0,0,1]);if(!f)var f=1e-6;d.create=function(){return new Float32Array(e)},d.clone=function(a){var b=new Float32Array(4);return b[0]=a[0],b[1]=a[1],b[2]=a[2],b[3]=a[3],b},d.copy=function(a,b){return a[0]=b[0],a[1]=b[1],a[2]=b[2],a[3]=b[3],a},d.identity=function(a){return a[0]=1,a[1]=0,a[2]=0,a[3]=1,a},d.transpose=function(a,b){if(a===b){var c=b[1];a[1]=b[2],a[2]=c}else a[0]=b[0],a[1]=b[2],a[2]=b[1],a[3]=b[3];return a},d.invert=function(a,b){var c=b[0],d=b[1],e=b[2],f=b[3],g=c*f-e*d;return g?(g=1/g,a[0]=f*g,a[1]=-d*g,a[2]=-e*g,a[3]=c*g,a):null},d.adjoint=function(a,b){var c=b[0];return a[0]=b[3],a[1]=-b[1],a[2]=-b[2],a[3]=c,a},d.determinant=function(a){return a[0]*a[3]-a[2]*a[1]},d.multiply=function(a,b,c){var d=b[0],e=b[1],f=b[2],g=b[3],h=c[0],i=c[1],j=c[2],k=c[3];return a[0]=d*h+e*j,a[1]=d*i+e*k,a[2]=f*h+g*j,a[3]=f*i+g*k,a},d.mul=d.multiply,d.rotate=function(a,b,c){var d=b[0],e=b[1],f=b[2],g=b[3],h=Math.sin(c),i=Math.cos(c);return a[0]=d*i+e*h,a[1]=d*-h+e*i,a[2]=f*i+g*h,a[3]=f*-h+g*i,a},d.scale=function(a,b,c){var d=b[0],e=b[1],f=b[2],g=b[3],h=c[0],i=c[1];return a[0]=d*h,a[1]=e*i,a[2]=f*h,a[3]=g*i,a},d.str=function(a){return"mat2("+a[0]+", "+a[1]+", "+a[2]+", "+a[3]+")"},"undefined"!=typeof c&&(c.mat2=d)},{}],2:[function(a,b,c){var d={};if(!e)var e=1e-6;d.create=function(){return new Float32Array(2)},d.clone=function(a){var b=new Float32Array(2);return b[0]=a[0],b[1]=a[1],b},d.fromValues=function(a,b){var c=new Float32Array(2);return c[0]=a,c[1]=b,c},d.copy=function(a,b){return a[0]=b[0],a[1]=b[1],a},d.set=function(a,b,c){return a[0]=b,a[1]=c,a},d.add=function(a,b,c){return a[0]=b[0]+c[0],a[1]=b[1]+c[1],a},d.subtract=function(a,b,c){return a[0]=b[0]-c[0],a[1]=b[1]-c[1],a},d.sub=d.subtract,d.multiply=function(a,b,c){return a[0]=b[0]*c[0],a[1]=b[1]*c[1],a},d.mul=d.multiply,d.divide=function(a,b,c){return a[0]=b[0]/c[0],a[1]=b[1]/c[1],a},d.div=d.divide,d.min=function(a,b,c){return a[0]=Math.min(b[0],c[0]),a[1]=Math.min(b[1],c[1]),a},d.max=function(a,b,c){return a[0]=Math.max(b[0],c[0]),a[1]=Math.max(b[1],c[1]),a},d.scale=function(a,b,c){return a[0]=b[0]*c,a[1]=b[1]*c,a},d.distance=function(a,b){var c=b[0]-a[0],d=b[1]-a[1];return Math.sqrt(c*c+d*d)},d.dist=d.distance,d.squaredDistance=function(a,b){var c=b[0]-a[0],d=b[1]-a[1];return c*c+d*d},d.sqrDist=d.squaredDistance,d.length=function(a){var b=a[0],c=a[1];return Math.sqrt(b*b+c*c)},d.len=d.length,d.squaredLength=function(a){var b=a[0],c=a[1];return b*b+c*c},d.sqrLen=d.squaredLength,d.negate=function(a,b){return a[0]=-b[0],a[1]=-b[1],a},d.normalize=function(a,b){var c=b[0],d=b[1],e=c*c+d*d;return e>0&&(e=1/Math.sqrt(e),a[0]=b[0]*e,a[1]=b[1]*e),a},d.dot=function(a,b){return a[0]*b[0]+a[1]*b[1]},d.cross=function(a,b,c){var d=b[0]*c[1]-b[1]*c[0];return a[0]=a[1]=0,a[2]=d,a},d.lerp=function(a,b,c,d){var e=b[0],f=b[1];return a[0]=e+d*(c[0]-e),a[1]=f+d*(c[1]-f),a},d.transformMat2=function(a,b,c){var d=b[0],e=b[1];return a[0]=d*c[0]+e*c[1],a[1]=d*c[2]+e*c[3],a},d.forEach=function(){var a=new Float32Array(2);return function(b,c,d,e,f,g){var h,i;for(c||(c=2),d||(d=0),i=e?Math.min(e*c+d,b.length):b.length,h=d;i>h;h+=c)a[0]=b[h],a[1]=b[h+1],f(a,a,g),b[h]=a[0],b[h+1]=a[1];return b}}(),d.str=function(a){return"vec2("+a[0]+", "+a[1]+")"},"undefined"!=typeof c&&(c.vec2=d)},{}],3:[function(a,b){function c(){}var d=a("./Scalar");b.exports=c,c.lineInt=function(a,b,c){c=c||0;var e,f,g,h,i,j,k,l=[0,0];return
return h>i}},{"../collision/Broadphase":9,"../math/vec2":30,"../shapes/Circle":35,"../shapes/Particle":38,"../shapes/Plane":39,"../shapes/Shape":41}],15:[function(a,b){function c(a,b){this.equations=[],this.bodyA=a,this.bodyB=b}b.exports=c},{}],16:[function(a,b){function c(a,b){d.call(this,a,b,0,Number.MAX_VALUE),this.ri=e.create(),this.penetrationVec=e.create(),this.rj=e.create(),this.ni=e.create(),this.restitution=0,this.firstImpact=!1,this.shapeA=null,this.shapeB=null}{var d=a("./Equation"),e=a("../math/vec2");a("../math/mat2")}b.exports=c,c.prototype=new d,c.prototype.constructor=c,c.prototype.computeB=function(a,b,c){var d=this.bi,f=this.bj,g=this.ri,h=this.rj,i=d.position,j=f.position,k=this.penetrationVec,l=this.ni,m=this.G,n=e.crossLength(g,l),o=e.crossLength(h,l);m[0]=-l[0],m[1]=-l[1],m[2]=-n,m[3]=l[0],m[4]=l[1],m[5]=o,e.add(k,j,h),e.sub(k,k,i),e.sub(k,k,g);var p,q;this.firstImpact&&0!==this.restitution?(q=0,p=1/b*(1+this.restitution)*this.computeGW()):(p=this.computeGW(),q=e.dot(l,k));var r=this.computeGiMf(),s=-q*a-p*b-c*r;return s}},{"../math/mat2":28,"../math/vec2":30,"./Equation":18}],17:[function(a,b){function c(a,b,c,g){d.call(this,a,b),this.distance=c,"undefined"==typeof g&&(g=Number.MAX_VALUE);var h=new e(a,b,-g,g);this.equations=[h];var i=f.create();h.computeGq=function(){return f.sub(i,b.position,a.position),f.length(i)-c},this.setMaxForce(g)}var d=a("./Constraint"),e=a("./Equation"),f=a("../math/vec2");b.exports=c,c.prototype=new d;var g=f.create();c.prototype.update=function(){var a=this.equations[0],b=this.bodyA,c=this.bodyB,d=(this.distance,a.G);f.sub(g,c.position,b.position),f.normalize(g,g),d[0]=-g[0],d[1]=-g[1],d[3]=g[0],d[4]=g[1]},c.prototype.setMaxForce=function(a){var b=this.equations[0];b.minForce=-a,b.maxForce=a},c.prototype.getMaxForce=function(){var a=this.equations[0];return a.maxForce}},{"../math/vec2":30,"./Constraint":15,"./Equation":18}],18:[function(a,b){function c(a,b,c,d){this.minForce="undefined"==typeof c?-1e6:c,this.maxForce="undefined"==typeof d?1e6:d,this.bi=a,this.bj=b,this.stiffness=1e6,this.relaxation=4,this.G=new g.ARRAY_TYPE(6),this.offset=0,this.a=0,this.b=0,this.eps=0,this.h=0,this.updateSpookParams(1/60),this.multiplier=0}function d(a,b,c,d,e){return a[0]*b[0]+a[1]*b[1]+a[2]*c+a[3]*d[0]+a[4]*d[1]+a[5]*e}b.exports=c;var e=a("../math/vec2"),f=a("../math/mat2"),g=a("../utils/Utils");c.prototype.constructor=c,c.prototype.updateSpookParams=function(a){var b=this.stiffness,c=this.relaxation,d=a;this.a=4/(d*(1+4*c)),this.b=4*c/(1+4*c),this.eps=4/(d*d*b*(1+4*c)),this.h=a},c.prototype.computeB=function(a,b,c){var d=this.computeGW(),e=this.computeGq(),f=this.computeGiMf();return-e*a-d*b-f*c};var h=e.create(),i=e.create();c.prototype.computeGq=function(){var a=this.G,b=this.bi,c=this.bj,e=(b.position,c.position,b.angle),f=c.angle;return d(a,h,e,i,f)+this.offset};e.create(),e.create();c.prototype.transformedGmult=function(a,b,c,e,f){return d(a,b,c,e,f)},c.prototype.computeGW=function(){var a=this.G,b=this.bi,c=this.bj,d=b.velocity,e=c.velocity,f=b.angularVelocity,g=c.angularVelocity;return this.transformedGmult(a,d,f,e,g)},c.prototype.computeGWlambda=function(){var a=this.G,b=this.bi,c=this.bj,d=b.vlambda,e=c.vlambda,f=b.wlambda,g=c.wlambda;return this.transformedGmult(a,d,f,e,g)};var j=e.create(),k=e.create();c.prototype.computeGiMf=function(){var a=this.bi,b=this.bj,c=a.force,d=a.angularForce,f=b.force,g=b.angularForce,h=a.invMass,i=b.invMass,l=a.invInertia,m=b.invInertia,n=this.G;return e.scale(j,c,h),e.scale(k,f,i),this.transformedGmult(n,j,d*l,k,g*m)},c.prototype.computeGiMGt=function(){var a=this.bi,b=this.bj,c=a.invMass,d=b.invMass,e=a.invInertia,f=b.invInertia,g=this.G;return g[0]*g[0]*c+g[1]*g[1]*c+g[2]*g[2]*e+g[3]*g[3]*d+g[4]*g[4]*d+g[5]*g[5]*f};var l=e.create(),m=e.create(),n=e.create(),o=(e.create(),e.create(),f.create()),p=f.create();c.prototype.addToWlambda=function(a){var b=this.bi,c=this.bj,d=l,g=o,h=p,i=m,j=n,k=this.G;i[0]=k[0],i[1]=k[1],j[0]=k[3],j[1]=k[4],f.identity(g),f.identity(h),g[0]=g[3]=b.invMass,h[0]=h[3]=c.invMass,e.scale(d,e.transformMat2(d,i,g),a),e
},c.prototype.addEquation=function(a){this.equations.push(a)},c.prototype.addEquations=function(a){d.appendArray(this.equations,a)},c.prototype.removeEquation=function(a){var b=this.equations.indexOf(a);-1!=b&&this.equations.splice(b,1)},c.prototype.removeAllEquations=function(){this.equations.length=0}},{"../events/EventEmitter":25,"../utils/Utils":46}],46:[function(a,b){function c(){}b.exports=c,c.appendArray=function(a,b){if(b.length<15e4)a.push.apply(a,b);else for(var c=0,d=b.length;c!==d;++c)a.push(b[c])},c.ARRAY_TYPE=Float32Array||Array},{}],47:[function(a,b){function c(){return"undefined"==typeof performance?(new Date).getTime():performance.now?performance.now():performance.webkitNow?performance.webkitNow():void 0}function d(a){o.apply(this),a=a||{},this.springs=[],this.bodies=[],this.solver=a.solver||new e,this.narrowphase=new y,this.gravity=a.gravity||g.fromValues(0,-9.78),this.doProfiling=a.doProfiling||!1,this.lastStepTime=0,this.broadphase=a.broadphase||new f,this.constraints=[],this.defaultFriction=.3,this.lastTimeStep=1/60,this.applySpringForces=!0,this.applyDamping=!0,this.solveConstraints=!0,this.contactMaterials=[],this.time=0,this.emitImpactEvent=!0,this._constraintIdCounter=0,this._bodyIdCounter=0,this.postStepEvent={type:"postStep"},this.addBodyEvent={type:"addBody",body:null},this.removeBodyEvent={type:"removeBody",body:null},this.addSpringEvent={type:"addSpring",spring:null},this.impactEvent={type:"impact",bodyA:null,bodyB:null}}var e=a("../solver/GSSolver"),f=a("../collision/NaiveBroadphase"),g=a("../math/vec2"),h=a("../shapes/Circle"),i=a("../shapes/Rectangle"),j=a("../shapes/Convex"),k=a("../shapes/Line"),l=a("../shapes/Plane"),m=a("../shapes/Capsule"),n=a("../shapes/Particle"),o=a("../events/EventEmitter"),p=a("../objects/Body"),q=a("../objects/Spring"),r=a("../material/Material"),s=a("../material/ContactMaterial"),t=a("../constraints/DistanceConstraint"),u=a("../constraints/LockConstraint"),v=a("../constraints/RevoluteConstraint"),w=a("../constraints/PrismaticConstraint"),x=a("../../package.json"),y=(a("../collision/Broadphase"),a("../collision/Narrowphase"));b.exports=d;var z=x.version.split(".").slice(0,2).join(".");d.prototype=new Object(o.prototype),d.prototype.addConstraint=function(a){this.constraints.push(a)},d.prototype.addContactMaterial=function(a){this.contactMaterials.push(a)},d.prototype.removeContactMaterial=function(a){var b=this.contactMaterials.indexOf(a);-1!==b&&this.contactMaterials.splice(b,1)},d.prototype.getContactMaterial=function(a,b){for(var c=this.contactMaterials,d=0,e=c.length;d!==e;d++){var f=c[d];if(f.materialA===a&&f.materialB===b||f.materialA===b&&f.materialB===a)return f}return!1},d.prototype.removeConstraint=function(a){var b=this.constraints.indexOf(a);-1!==b&&this.constraints.splice(b,1)};var A=(g.create(),g.create(),g.create(),g.create(),g.create(),g.create(),g.create()),B=g.fromValues(0,0),C=g.fromValues(0,0),D=g.fromValues(0,0);d.prototype.step=function(a){{var b,e,f=this,h=this.doProfiling,i=this.springs.length,j=this.springs,k=this.bodies,l=this.gravity,m=this.solver,n=this.bodies.length,o=this.broadphase,p=this.narrowphase,q=this.constraints,r=A,s=(g.scale,g.add);g.rotate}this.lastTimeStep=a,h&&(b=c());var t=g.length(l);if(0!==t)for(var u=0;u!==n;u++){var v=k[u],w=v.force;g.scale(r,l,v.mass),s(w,w,r)}if(this.applySpringForces)for(var u=0;u!==i;u++){var x=j[u];x.applyForce()}if(this.applyDamping)for(var u=0;u!==n;u++){var v=k[u];v.applyDamping(a)}var y=o.getCollisionPairs(this);p.reset();for(var u=0,z=y.length;u!==z;u+=2)for(var B=y[u],C=y[u+1],E=0;E!==B.shapes.length;E++)for(var F=B.shapes[E],G=B.shapeOffsets[E]||D,H=B.shapeAngles[E]||0,I=0;I!==C.shapes.length;I++){var J=C.shapes[I],K=C.shapeOffsets[I]||D,L=C.shapeAngles[I]||0,M=this.defaultFriction,N=0;if(F.material&&J.material){var O=this.getContactMaterial(F.material,J.material);O&&(M=O.friction,N=O.restitution)}d.runNarrowphase(p,B,F,G,H,C,J,K,L,M,t,N)}m.addEquations(p.contactEquations),m.addEquations(p.frictionEquations);var P=q.length;for(u=0;u!==P;u++){var Q=q[u];Q.update(),m.addEquations(Q.equa