diff --git a/cp.js b/cp.js index 7101cde..0098c68 100644 --- a/cp.js +++ b/cp.js @@ -142,10 +142,9 @@ var momentForPoly = cp.momentForPoly = function(m, verts, offset) var areaForPoly = cp.areaForPoly = function(verts) { - throw new Error('Not updated for flat verts'); var area = 0; - for(var i=0, len=verts.length; ib?a:b},g,h;typeof window=="object"&&window.navigator.userAgent.indexOf("Firefox")>-1?(g=Math.min,h=Math.max):(g=e,h=f);var i=function(a,b){for(var c=0;cb*b?I(S(a),b):a},V=a.v.lerpconst=function(a,b,c){return F(a,U(G(b,a),c))},W=a.v.dist=function(a,b){return D(G(a,b))},X=a.v.distsq=function(a,b){return Q(G(a,b))},Y=a.v.near=function(a,b,c){return X(a,b)=b.r&&a.b<=b.b&&a.t>=b.t},bi=function(a,b){return a.l<=b.x&&a.r>=b.x&&a.b<=b.y&&a.t>=b.y},bj=function(a,b,c,d,e){return a<=e.x&&c>=e.x&&b<=e.y&&d>=e.y},bk=function(a,b){return new bd(g(a.l,b.l),g(a.b,b.b),h(a.r,b.r),h(a.t,b.t))},bl=function(a,b){return new bd(g(a.l,b.x),g(a.b,b.y),h(a.r,b.x),h(a.t,b.y))},bm=function(a){return(a.r-a.l)*(a.t-a.b)},bn=function(a,b){return(h(a.r,b.r)-g(a.l,b.l))*(h(a.t,b.t)-g(a.b,b.b))},bo=function(a,b,c,d,e){return(h(a.r,d)-g(a.l,b))*(h(a.t,e)-g(a.b,c))},bp=function(a,b,c){return bbSegmentQuery(a,b,c)!=Infinity},bq=function(a,b){var c=g(h(a.l,b.x),a.r),d=g(h(a.b,b.y),a.t);return new z(c,d)},br=function(a,b){var c=Math.abs(a.r-a.l),d=(b.x-a.l)%c,e=d>0?d:d+c,f=Math.abs(a.t-a.b),g=(b.y-a.b)%f,h=g>0?g:g+f;return new z(e+a.l,h+a.b)},bs=0,bt=a.NO_GROUP=0,bu=a.ALL_LAYERS=-1;a.resetShapeIdCounter=function(){bs=0};var bv=a.Shape=function(a){this.body=a,this.bb_l=this.bb_b=this.bb_r=this.bb_t=0,this.hashid=bs++,this.sensor=!1,this.e=0,this.u=0,this.surface_v=A,this.collision_type=0,this.group=0,this.layers=bu,this.space=null,this.collisionCode=this.collisionCode};bv.prototype.setElasticity=function(a){this.e=a},bv.prototype.setFriction=function(a){this.body.activate(),this.u=a},bv.prototype.setLayers=function(a){this.body.activate(),this.layers=a},bv.prototype.active=function(){return this.body&&this.body.shapeList.indexOf(this)!==-1},bv.prototype.setBody=function(a){b(!this.active(),"You cannot change the body on an active shape. You must remove the shape, then "),this.body=a},bv.prototype.cacheBB=function(){return this.update(this.body.p,this.body.rot)},bv.prototype.update=function(a,c){b(!isNaN(c.x),"Rotation is NaN"),b(!isNaN(a.x),"Position is NaN"),this.cacheData(a,c)},bv.prototype.getBB=function(){return new bd(this.bb_l,this.bb_b,this.bb_r,this.bb_t)};var bw=function(a){this.shape=a,this.d=Infinity,this.n=A},bx=function(a,b,c){this.shape=a,this.t=b,this.n=c};bx.prototype.hitPoint=function(a,b){return R(a,b,this.t)},bx.prototype.hitDist=function(a,b){return W(a,b)*this.t};var by=a.CircleShape=function(a,b,c){this.c=this.tc=c,this.r=b,this.type="circle",bv.call(this,a)};by.prototype=Object.create(bv.prototype),by.prototype.cacheData=function(a,b){var c=this.tc=O(this.c,b).add(a),d=this.r;this.bb_l=c.x-d,this.bb_b=c.y-d,this.bb_r=c.x+d,this.bb_t=c.y+d},by.prototype.pointQuery=function(a){var b=G(a,this.tc),c=Q(b),d=this.r;if(c=0){var k=(-h-Math.sqrt(j))/(2*g);if(0<=k&&k<=1)return new bx(a,k,S(R(d,e,k)))}};by.prototype.segmentQuery=function(a,b){return bz(this,this.tc,this.r,a,b)};var bA=a.SegmentShape=function(a,b,c,d){this.a=b,this.b=c,this.n=L(S(G(c,b))),this.ta=this.tb=this.tn=null,this.r=d,this.a_tangent=A,this.b_tangent=A,this.type="segment",bv.call(this,a)};bA.prototype=Object.create(bv.prototype),bA.prototype.cacheData=function(a,b){this.ta=F(a,O(this.a,b)),this.tb=F(a,O(this.b,b)),this.tn=O(this.n,b);var c,d,e,f;this.ta.x0?H(c):c,g=G(I(f,e),a),h=F(this.ta,g),i=F(this.tb,g),j=G(b,a);if(J(j,h)*J(j,i)<=0){var k=d+(d>0?-e:e),l=-k,m=B(j,c)-k;if(l*m<0)return new bx(this,l/(l-m),f)}else if(e!=0){var n=bz(this,this.ta,this.r,a,b),o=bz(this,this.tb,this.r,a,b);return n?o&&o.t0)return!1}return!0},bC=a.PolyShape=function(a,c,d){b(c.length>=4,"Polygons require some verts"),b(typeof c[0]=="number","Polygon verticies should be specified in a flattened list"),b(bB(c),"Polygon is concave or has a reversed winding."),this.setVerts(c,d),this.type="poly",bv.call(this,a)};bC.prototype=Object.create(bv.prototype);var bD=function(a,b){this.n=a,this.d=b};bC.prototype.setVerts=function(a,b){var c=a.length,d=c>>1;this.verts=new Array(c),this.tVerts=new Array(c),this.axes=new Array(d),this.tAxes=new Array(d);for(var e=0;e>1]=new bD(j,C(j.x,j.y,f,g)),this.tAxes[e>>1]=new bD(new z(0,0),0)}};var bE=a.BoxShape=function(a,b,c){var d=b/2,e=c/2;return bF(a,new bd(-d,-e,d,e))},bF=a.BoxShape2=function(a,b){var c=[b.l,b.b,b.l,b.t,b.r,b.t,b.r,b.b];return new bC(a,c,A)};bC.prototype.transformVerts=function(a,b){var c=this.verts,d=this.tVerts,e=Infinity,f=-Infinity,i=Infinity,j=-Infinity;for(var k=0;ki)continue;var j=B(b,h),k=(c[g].d-i)/(j-i);if(k<0||10)return!1}return!0},bC.prototype.containsVertPartial=function(a,b,c){var d=this.tAxes;for(var e=0;e0)return!1}return!0};var bG=a.Body=function(a,b){bG.prototype.velocity_func=bG.prototype.updateVelocity,bG.prototype.position_func=bG.prototype.updatePosition,this.p=new z(0,0),this.vx=this.vy=0,this.f=new z(0,0),this.w=0,this.t=0,this.v_limit=Infinity,this.w_limit=Infinity,this.v_biasx=this.v_biasy=0,this.w_bias=0,this.space=null,this.shapeList=[],this.arbiterList=null,this.constraintList=null,this.nodeRoot=null,this.nodeNext=null,this.nodeIdleTime=0,this.setMass(a),this.setMoment(b),this.rot=new z(0,0),this.setAngle(0)},bH=function(){return body=new bG(Infinity,Infinity),body.nodeIdleTime=Infinity,body};if(typeof DEBUG!="undefined"&&DEBUG){var bI=function(a,c){b(a.x==a.x&&a.y==a.y,c)},bJ=function(a,c){b(Math.abs(a.x)!==Infinity&&Math.abs(a.y)!==Infinity,c)},bK=function(a,b){bI(a,b),bJ(a,b)};bG.prototype.sanityCheck=function(){b(this.m===this.m&&this.m_inv===this.m_inv,"Body's mass is invalid."),b(this.i===this.i&&this.i_inv===this.i_inv,"Body's moment is invalid."),bK(this.p,"Body's position is invalid."),bK(this.f,"Body's force is invalid."),b(this.vx===this.vx&&Math.abs(this.vx)!==Infinity,"Body's velocity is invalid."),b(this.vy===this.vy&&Math.abs(this.vy)!==Infinity,"Body's velocity is invalid."),b(this.a===this.a&&Math.abs(this.a)!==Infinity,"Body's angle is invalid."),b(this.w===this.w&&Math.abs(this.w)!==Infinity,"Body's angular velocity is invalid."),b(this.t===this.t&&Math.abs(this.t)!==Infinity,"Body's torque is invalid."),bK(this.rot,"Internal error: Body's rotation vector is invalid."),b(this.v_limit===this.v_limit,"Body's velocity limit is invalid."),b(this.w_limit===this.w_limit,"Body's angular velocity limit is invalid.")}}else bG.prototype.sanityCheck=function(){};bG.prototype.isSleeping=function(){return this.nodeRoot!==null},bG.prototype.isStatic=function(){return this.nodeIdleTime===Infinity},bG.prototype.isRogue=function(){return this.space===null},bG.prototype.setMass=function(a){b(a>0,"Mass must be positive and non-zero."),this.activate(),this.m=a,this.m_inv=1/a},bG.prototype.setMoment=function(a){b(a>0,"Moment of Inertia must be positive and non-zero."),this.activate(),this.i=a,this.i_inv=1/a},bG.prototype.addShape=function(a){this.shapeList.push(a)},bG.prototype.removeShape=function(a){i(this.shapeList,a)};var bL=function(a,b,c){return a===c?a.next(b):(a.a===b?a.next_a=bL(a.next_a,b,c):a.next_b=bL(a.next_b,b,c),a)};bG.prototype.removeConstraint=function(a){this.constraintList=bL(this.constraintList,this,a)},bG.prototype.setPos=function(a){this.activate(),this.sanityCheck(),this.p=a},bG.prototype.setVelocity=function(a){this.activate(),this.vx=a.x,this.vy=a.y},bG.prototype.setAngleInternal=function(a){b(!isNaN(a),"Internal Error: Attempting to set body's angle to NaN"),this.a=a,this.rot.x=Math.cos(a),this.rot.y=Math.sin(a)},bG.prototype.setAngle=function(a){this.activate(),this.sanityCheck(),this.setAngleInternal(a)},bG.prototype.updateVelocity=function(a,b,c){var d=this.vx*b+(a.x+this.f.x*this.m_inv)*c,e=this.vy*b+(a.y+this.f.y*this.m_inv)*c,f=this.v_limit,g=d*d+e*e,h=g>f*f?f/Math.sqrt(len):1;this.vx=d*h,this.vy=e*h;var i=this.w_limit;this.w=t(this.w*b+this.t*this.i_inv*c,-i,i),this.sanityCheck()},bG.prototype.updatePosition=function(a){this.p.x+=(this.vx+this.v_biasx)*a,this.p.y+=(this.vy+this.v_biasy)*a,this.setAngleInternal(this.a+(this.w+this.w_bias)*a),this.v_biasx=this.v_biasy=0,this.w_bias=0,this.sanityCheck()},bG.prototype.resetForces=function(){this.activate(),this.f=new z(0,0),this.t=0},bG.prototype.applyForce=function(a,b){this.activate(),this.f=F(this.f,a),this.t+=J(b,a)},bG.prototype.applyImpulse=function(a,b){this.activate(),cQ(this,a.x,a.y,b)},bG.prototype.getVelAtPoint=function(a){return F(new z(this.vx,this.vy),I(L(a),body.w))},bG.prototype.getVelAtWorldPoint=function(a){return this.getVelAtPoint(G(a,body.p))},bG.prototype.getVelAtLocalPoint=function(a){return this.getVelAtPoint(O(a,body.rot))},bG.prototype.eachShape=function(a){for(var b=0,c=this.shapeList.length;b0){var c=a.query;this.each(function(a){c(a,new bd(a.bb_l,a.bb_b,a.bb_r,a.bb_t),b)})}};var bN=a.BBTree=function(a){bM.call(this,a),this.velocityFunc=null,this.leaves={},this.count=0,this.root=null,this.stamp=0};bN.prototype=Object.create(bM.prototype);var bO=0,bP=function(a,b,c){this.obj=null,this.bb_l=g(b.bb_l,c.bb_l),this.bb_b=g(b.bb_b,c.bb_b),this.bb_r=h(b.bb_r,c.bb_r),this.bb_t=h(b.bb_t,c.bb_t),this.parent=null,this.setA(b),this.setB(c),bO++},bQ=0,bR=function(a,b){this.obj=b,a.getBB(b,this),this.parent=null,this.stamp=1,this.pairs=null,bQ++};bN.prototype.getBB=function(a,b){var c=this.velocityFunc;if(c){var d=.1,e=(a.bb_r-a.bb_l)*d,f=(a.bb_t-a.bb_b)*d,i=I(c(a),.1);b.bb_l=a.bb_l+g(-e,i.x),b.bb_b=a.bb_b+g(-f,i.y),b.bb_r=a.bb_r+h(e,i.x),b.bb_t=a.bb_t+h(f,i.y)}else b.bb_l=a.bb_l,b.bb_b=a.bb_b,b.bb_r=a.bb_r,b.bb_t=a.bb_t},bN.prototype.getStamp=function(){var a=this.dynamicIndex;return a&&a.stamp?a.stamp:this.stamp},bN.prototype.incrementStamp=function(){this.dynamicIndex&&this.dynamicIndex.stamp?this.dynamicIndex.stamp++:this.stamp++};var bS=function(a,b){this.a=a,this.b=b},bT=function(a,b){this.prev=null,this.next=b,this.leaf=a};bT.prototype.unlink=function(){var a=this.next,b=this.prev;a&&(a.a.leaf==this.leaf?a.a.prev=b:a.b.prev=b),b?b.a.leaf==this.leaf?b.a.next=a:b.b.next=a:this.leaf.pairs=a},bR.prototype.clearPairs=function(a){var b=this.pairs,c;this.pairs=null;while(b)b.a.leaf==this?(c=b.a.next,b.b.unlink(),b=c):(c=b.b.next,b.a.unlink(),b=c)};var bU=function(a,b,c){var d=a.pairs,e=b.pairs,f=new bS(new bT(a,d),new bT(b,e));a.pairs=b.pairs=f,d&&(d.a.leaf==a?d.a.prev=f:d.b.prev=f),e&&(e.a.leaf==b?e.a.prev=f:e.b.prev=f)};bP.prototype.setA=function(a){this.A=a,a.parent=this},bP.prototype.setB=function(a){this.B=a,a.parent=this},bR.prototype.isLeaf=!0,bP.prototype.isLeaf=!1,bP.prototype.otherChild=function(a){return this.A==a?this.B:this.A},bP.prototype.replaceChild=function(a,b,d){c(a==this.A||a==this.B,"Node is not a child of parent."),this.A==a?this.setA(b):this.setB(b);for(var e=this;e;e=e.parent){var f=e.A,i=e.B;e.bb_l=g(f.bb_l,i.bb_l),e.bb_b=g(f.bb_b,i.bb_b),e.bb_r=h(f.bb_r,i.bb_r),e.bb_t=h(f.bb_t,i.bb_t)}},bP.prototype.bbArea=bR.prototype.bbArea=function(){return(this.bb_r-this.bb_l)*(this.bb_t-this.bb_b)};var bV=function(a,b){return(h(a.bb_r,b.bb_r)-g(a.bb_l,b.bb_l))*(h(a.bb_t,b.bb_t)-g(a.bb_b,b.bb_b))},bW=function(a,b){return Math.abs(a.bb_l+a.bb_r-b.bb_l-b.bb_r)+Math.abs(a.bb_b+b.bb_t-b.bb_b-b.bb_t)},bX=function(a,b,c){if(a==null)return b;if(a.isLeaf)return new bP(c,b,a);var d=a.B.bbArea()+bV(a.A,b),e=a.A.bbArea()+bV(a.B,b);return d===e&&(d=bW(a.A,b),e=bW(a.B,b)),e=a.bb_r&&this.bb_b<=a.bb_b&&this.bb_t>=a.bb_t},bR.prototype.update=function(a){var b=a.root,c=this.obj;return this.containsObj(c)?!1:(a.getBB(this.obj,this),b=b_(b,this,a),a.root=bX(b,this,a),this.clearPairs(a),this.stamp=a.getStamp(),!0)},bR.prototype.addPairs=function(a){var b=a.dynamicIndex;if(b){var c=b.root;c&&cb(c,this,!0,b,null)}else{var d=a.staticIndex.root;cc(this,a,d,null)}},bN.prototype.insert=function(a,b){var c=new bR(this,a);this.leaves[b]=c,this.root=bX(this.root,c,this),this.count++,c.stamp=this.getStamp(),c.addPairs(this),this.incrementStamp()},bN.prototype.remove=function(a,b){var c=this.leaves[b];delete this.leaves[b],this.root=b_(this.root,c,this),this.count--,c.clearPairs(this)},bN.prototype.contains=function(a,b){return this.leaves[b]!=null};var ce=function(a,b){};bN.prototype.reindexQuery=function(a){if(!this.root)return;var b,c=this.leaves;for(b in c)c[b].update(this);var d=this.staticIndex,e=d&&d.root;cd(this.root,this,e,a),d&&!e&&this.collideStatic(this,d,a),this.incrementStamp()},bN.prototype.reindex=function(){this.reindexQuery(ce)},bN.prototype.reindexObject=function(a,b){var c=this.leaves[b];c&&(c.update(this)&&c.addPairs(this),this.incrementStamp())},bN.prototype.pointQuery=function(a,b){this.root&&bY(this.root,new bd(a.x,a.y,a.x,a.y),b)},bN.prototype.segmentQuery=function(a,b,c,d){this.root&&b$(this.root,a,b,c,d)},bN.prototype.query=function(a,b){this.root&&bY(this.root,a,b)},bN.prototype.count=function(){return this.count},bN.prototype.each=function(a){var b;for(b in this.leaves)a(this.leaves[b].obj)};var cf=function(a,b,c,d,e){return(h(a.bb_r,d)-g(a.bb_l,b))*(h(a.bb_t,e)-g(a.bb_b,c))},cg=function(a,b,c,d){if(d==1)return b[c];if(d==2)return new bP(a,b[c],b[c+1]);var e=b[c],f=e.bb_l,i=e.bb_b,j=e.bb_r,k=e.bb_t,l=c+d;for(var m=c+1;mk-i,o=new Array(d*2);if(n)for(var m=c;m=e*e)return;var h=Math.sqrt(g);return new cq(F(a,I(f,.5+(c-.5*e)/(h?h:Infinity))),h?I(f,1/h):new z(1,0),h-e,0)},ct=function(a,b){var c=cs(a.tc,b.tc,a.r,b.r);return c?[c]:cr},cu=function(a,b){var c=b.ta,d=b.tb,e=a.tc,f=G(d,c),g=u(B(f,G(e,c))/Q(f)),h=F(c,I(f,g)),i=cs(e,h,a.r,b.r);if(i){var j=i.n;return g===0&&B(j,b.a_tangent)<0||g===1&&B(j,b.b_tangent)<0?cr:[i]}return cr},cv=0,cw=function(a,b){var c=0,d=a.valueOnAxis(b[0].n,b[0].d);if(d>0)return-1;for(var e=1;e0)return-1;f>d&&(d=f,c=e)}return cv=d,c},cx=function(a,b,c,e){var f=[],g=a.tVerts;for(var h=0;h>1)))}var k=b.tVerts;for(var h=0;h>1)))}return f.length?f:cx(a,b,c,e)},cz=function(a,b){var c=cw(b,a.tAxes);if(c==-1)return cr;var d=cv,e=cw(a,b.tAxes);if(e==-1)return cr;var f=cv;return d>f?cy(a,b,a.tAxes[c].n,d):cy(a,b,H(b.tAxes[e].n),f)},cA=function(a,b,c){var d=B(b,a.ta)-a.r,e=B(b,a.tb)-a.r;return g(d,e)-c},cB=function(a,b,c,e,f){var g=J(b.tn,b.ta),h=J(b.tn,b.tb),i=I(b.tn,f),j=c.tVerts;for(var k=0;k=n&&n>=h&&a.push(new cq(new z(l,m),i,e,d(c.hashid,k)))}}},cC=function(a,b){var c=[],e=b.tAxes,f=e.length,g=B(a.tn,a.ta),h=b.valueOnAxis(a.tn,g)-a.r,i=b.valueOnAxis(H(a.tn),-g)-a.r;if(i>0||h>0)return cr;var j=0,k=cA(a,e[0].n,e[0].d);if(k>0)return cr;for(var l=0;l0)return cr;m>k&&(k=m,j=l)}var n=H(e[j].n),o=F(a.ta,I(n,a.r)),p=F(a.tb,I(n,a.r));b.containsVert(o.x,o.y)&&c.push(new cq(o,n,k,d(a.hashid,0))),b.containsVert(p.x,p.y)&&c.push(new cq(p,n,k,d(a.hashid,1)));if(h>=k||i>=k)h>i?cB(c,a,b,h,1):cB(c,a,b,i,-1);if(c.length===0){var q=j*2,r=b.tVerts,s=new z(r[q],r[q+1]),t;if(t=cs(a.ta,s,a.r,0,c))return[t];if(t=cs(a.tb,s,a.r,0,c))return[t];var u=f*2,v=new z(r[(q+2)%u],r[(q+3)%u]);if(t=cs(a.ta,v,a.r,0,c))return[t];if(t=cs(a.tb,v,a.r,0,c))return[t]}return c},cD=function(a,b){var c=b.tAxes,d=0,e=B(c[0].n,a.tc)-c[0].d-a.r;for(var f=0;f0)return cr;g>e&&(e=g,d=f)}var h=c[d].n,i=b.tVerts,j=i.length,k=d<<1,l=i[k],m=i[k+1],n=i[(k+2)%j],o=i[(k+3)%j],p=K(h.x,h.y,l,m),q=K(h.x,h.y,n,o),r=J(h,a.tc);if(ri?0:f.nodeIdleTime+a}}var j=this.arbiters;for(var e=0,k=j.length;e=0,"Internal Error: Space lock underflow.");if(!this.locked&&a){var c=this.rousedBodies;for(var d=0;dc.collisionCode){var h=b;b=c,c=h}var i=cE(b,c);if(i.length===0)return;var j=d(b.hashid,c.hashid),k=e.cachedArbiters[j];k||(k=e.cachedArbiters[j]=new ck(b,c)),k.update(i,f,b,c),k.state=="first coll"&&!f.begin(k,e)&&k.ignore(),k.state!=="ignore"&&f.preSolve(k,e)&&!g?e.arbiters.push(k):(k.contacts=null,k.state!=="ignore"&&(k.state="normal")),k.stamp=e.stamp}},cG.prototype.arbiterSetFilter=function(a){var b=this.stamp-a.stamp,c=a.body_a,d=a.body_b;return(c.isStatic()||c.isSleeping())&&(d.isStatic()||d.isSleeping())?!0:(b>=1&&a.state!="cached"&&(a.callSeparate(this),a.state="cached"),b>=this.collisionPersistence?(a.contacts=null,!1):!0)};var cN=function(a){var b=a.body;a.update(b.p,b.rot)};cG.prototype.step=function(a){if(a===0)return;b(A.x===0&&A.y===0,"vzero is invalid"),this.stamp++;var c=this.curr_dt;this.curr_dt=a;var d=this.bodies,e=this.constraints,f=this.arbiters;for(var g=0;g0,"You created a 0 length pin joint. A pivot joint will be much more stable."),this.r1=this.r2=null,this.n=null,this.nMass=0,this.jnAcc=this.jnMax=0,this.bias=0};cZ.prototype=Object.create(cY.prototype),cZ.prototype.preStep=function(a){var b=this.a,c=this.b;this.r1=O(this.anchr1,b.rot),this.r2=O(this.anchr2,c.rot);var d=G(F(c.p,this.r2),F(b.p,this.r1)),e=D(d);this.n=I(d,1/(e?e:Infinity)),this.nMass=1/cU(b,c,this.r1,this.r2,this.n);var f=this.maxBias;this.bias=t(-cX(this.errorBias,a)*(e-this.dist)/a,-f,f),this.jnMax=this.maxForce*a},cZ.prototype.applyCachedImpulse=function(a){var b=I(this.n,this.jnAcc*a);cR(this.a,this.b,this.r1,this.r2,b.x,b.y)},cZ.prototype.applyImpulse=function(){var a=this.a,b=this.b,c=this.n,d=cP(a,b,this.r1,this.r2,c),e=(this.bias-d)*this.nMass,f=this.jnAcc;this.jnAcc=t(f+e,-this.jnMax,this.jnMax),e=this.jnAcc-f,cR(a,b,this.r1,this.r2,c.x*e,c.y*e)},cZ.prototype.getImpulse=function(){return Math.abs(this.jnAcc)};var c$=a.SlideJoint=function(a,b,c,d,e,f){cY.call(this,a,b),this.anchr1=c,this.anchr2=d,this.min=e,this.max=f,this.r1=this.r2=this.n=null,this.nMass=0,this.jnAcc=this.jnMax=0,this.bias=0};c$.prototype=Object.create(cY.prototype),c$.prototype.preStep=function(a){var b=this.a,c=this.b;this.r1=O(this.anchr1,b.rot),this.r2=O(this.anchr2,c.rot);var d=G(F(c.p,this.r2),F(b.p,this.r1)),e=D(d),f=0;e>this.max?(f=e-this.max,this.n=T(d)):e=J(e,f)?(this.clamp=-1,this.r1=G(e,b.p)):(this.clamp=0,this.r1=G(F(I(L(f),-h),I(f,g)),b.p)),cV(b,c,this.r1,this.r2,this.k1,this.k2),this.jMaxLen=this.maxForce*a;var i=G(F(c.p,this.r2),F(b.p,this.r1));this.bias=U(I(i,-cX(this.errorBias,a)/a),this.maxBias)},da.prototype.applyCachedImpulse=function(a){cR(this.a,this.b,this.r1,this.r2,this.jAcc.x*a,this.jAcc.y*a)},da.prototype.grooveConstrain=function(a){var b=this.grv_tn,c=this.clamp*J(a,b)>0?a:N(a,b);return U(c,this.jMaxLen)},da.prototype.applyImpulse=function(){var a=this.a,b=this.b,c=this.r1,d=this.r2,e=cO(a,b,c,d),f=cW(G(this.bias,e),this.k1,this.k2),g=this.jAcc;this.jAcc=this.grooveConstrain(F(g,f)),cR(a,b,this.r1,this.r2,this.jAcc.x-g.x,this.jAcc.y-g.y)},da.prototype.getImpulse=function(){return D(this.jAcc)},da.prototype.setGrooveA=function(a){this.grv_a=a,this.grv_n=L(S(G(this.grv_b,a))),this.activateBodies()},da.prototype.setGrooveB=function(a){this.grv_b=a,this.grv_n=L(S(G(a,this.grv_a))),this.activateBodies()};var db=function(a,b){return(a.restLength-b)*a.stiffness},dc=a.DampedSpring=function(a,b,c,d,e,f,g){cY.call(this,a,b),this.anchr1=c,this.anchr2=d,this.restLength=e,this.stiffness=f,this.damping=g,this.springForceFunc=db,this.target_vrn=this.v_coef=0,this.r1=this.r2=null,this.nMass=0,this.n=null};dc.prototype=Object.create(cY.prototype),dc.prototype.preStep=function(a){var b=this.a,d=this.b;this.r1=O(this.anchr1,b.rot),this.r2=O(this.anchr2,d.rot);var e=G(F(d.p,this.r2),F(b.p,this.r1)),f=D(e);this.n=I(e,1/(f?f:Infinity));var g=cU(b,d,this.r1,this.r2,this.n);c(g!==0,"Unsolvable this."),this.nMass=1/g,this.target_vrn=0,this.v_coef=1-Math.exp(-this.damping*a*g);var h=this.springForceFunc(this,f);cR(b,d,this.r1,this.r2,this.n.x*h*a,this.n.y*h*a)},dc.prototype.applyCachedImpulse=function(a){},dc.prototype.applyImpulse=function(){var a=this.a,b=this.b,c=this.n,d=this.r1,e=this.r2,f=cP(a,b,d,e,c),g=(this.target_vrn-f)*this.v_coef;this.target_vrn=f+g,g*=this.nMass,cR(a,b,this.r1,this.r2,this.n.x*g,this.n.y*g)},dc.prototype.getImpulse=function(){return 0};var dd=function(a,b){return(b-a.restAngle)*a.stiffness},de=a.DampedRotarySpring=function(a,b,c,d,e){cY.call(this,a,b),this.restAngle=c,this.stiffness=d,this.damping=e,this.springTorqueFunc=dd,this.target_wrn=0,this.w_coef=0,this.iSum=0};de.prototype=Object.create(cY.prototype),de.prototype.preStep=function(a){var b=this.a,d=this.b,e=b.i_inv+d.i_inv;c(e!==0,"Unsolvable spring."),this.iSum=1/e,this.w_coef=1-Math.exp(-this.damping*a*e),this.target_wrn=0;var f=this.springTorqueFunc(this,b.a-d.a)*a;b.w-=f*b.i_inv,d.w+=f*d.i_inv},de.prototype.applyImpulse=function(){var a=this.a,b=this.b,c=a.w-b.w,d=(this.target_wrn-c)*this.w_coef;this.target_wrn=c+d;var e=d*this.iSum;a.w+=e*a.i_inv,b.w-=e*b.i_inv};var df=a.RotaryLimitJoint=function(a,b,c,d){cY.call(this,a,b),this.min=c,this.max=d,this.jAcc=0,this.iSum=this.bias=this.jMax=0};df.prototype=Object.create(cY.prototype),df.prototype.preStep=function(a){var b=this.a,c=this.b,d=c.a-b.a,e=0;d>this.max?e=this.max-d:d0?i=h:this.angle=Math.floor((g-e)/f)*f+e,this.iSum=1/(b.i_inv+c.i_inv);var j=this.maxBias;this.bias=t(-cX(this.errorBias,a)*i/a,-j,j),this.jMax=this.maxForce*a,this.bias||(this.jAcc=0)},dg.prototype.applyCachedImpulse=function(a){var b=this.a,c=this.b,d=this.jAcc*a;b.w-=d*b.i_inv,c.w+=d*c.i_inv},dg.prototype.applyImpulse=function(){if(!this.bias)return;var a=this.a,b=this.b,c=b.w-a.w,d=this.ratchet,e=-(this.bias+c)*this.iSum,f=this.jAcc;this.jAcc=t((f+e)*d,0,this.jMax*Math.abs(d))/d,e=this.jAcc-f,a.w-=e*a.i_inv,b.w+=e*b.i_inv},dg.prototype.getImpulse=function(a){return Math.abs(a.jAcc)};var dh=a.GearJoint=function(a,b,c,d){cY.call(this,a,b),this.phase=c,this.ratio=d,this.ratio_inv=1/d,this.jAcc=0,this.iSum=this.bias=this.jMax=0};dh.prototype=Object.create(cY.prototype),dh.prototype.preStep=function(a){var b=this.a,c=this.b;this.iSum=1/(b.i_inv*this.ratio_inv+this.ratio*c.i_inv);var d=this.maxBias;this.bias=t(-cX(this.errorBias,a)*(c.a*this.ratio-b.a-this.phase)/a,-d,d),this.jMax=this.maxForce*a},dh.prototype.applyCachedImpulse=function(a){var b=this.a,c=this.b,d=this.jAcc*a;b.w-=d*b.i_inv*this.ratio_inv,c.w+=d*c.i_inv},dh.prototype.applyImpulse=function(){var a=this.a,b=this.b,c=b.w*this.ratio-a.w,d=(this.bias-c)*this.iSum,e=this.jAcc;this.jAcc=t(e+d,-this.jMax,this.jMax),d=this.jAcc-e,a.w-=d*a.i_inv*this.ratio_inv,b.w+=d*b.i_inv},dh.prototype.getImpulse=function(){return Math.abs(this.jAcc)},dh.prototype.setRatio=function(a){this.ratio=a,this.ratio_inv=1/a,this.activateBodies()};var di=a.SimpleMotor=function(a,b,c){cY.call(this,a,b),this.rate=c,this.jAcc=0,this.iSum=this.jMax=0};di.prototype=Object.create(cY.prototype),di.prototype.preStep=function(a){this.iSum=1/(this.a.i_inv+this.b.i_inv),this.jMax=this.maxForce*a},di.prototype.applyCachedImpulse=function(a){var b=this.a,c=this.b,d=this.jAcc*a;b.w-=d*b.i_inv,c.w+=d*c.i_inv},di.prototype.applyImpulse=function(){var a=this.a,b=this.b,c=b.w-a.w+this.rate,d=-c*this.iSum,e=this.jAcc;this.jAcc=t(e+d,-this.jMax,this.jMax),d=this.jAcc-e,a.w-=d*a.i_inv,b.w+=d*b.i_inv},di.prototype.getImpulse=function(){return Math.abs(this.jAcc)}})() \ No newline at end of file +(function(){Object.create=Object.create||function(a){function b(){}return b.prototype=a,new b};var a;typeof exports=="undefined"?(a={},typeof window=="object"&&(window.cp=a)):a=exports;var b=function(a,b){if(!a)throw new Error("Assertion failed: "+b)},c=function(a,b){!a&&console&&console.warn&&(console.warn("ASSERTION FAILED: "+b),console.trace&&console.trace())},d=function(a,b){return a+" "+b},e=function(a,b){return ab?a:b},g,h;typeof window=="object"&&window.navigator.userAgent.indexOf("Firefox")>-1?(g=Math.min,h=Math.max):(g=e,h=f);var i=function(a,b){for(var c=0;cb*b?I(S(a),b):a},V=a.v.lerpconst=function(a,b,c){return F(a,U(G(b,a),c))},W=a.v.dist=function(a,b){return D(G(a,b))},X=a.v.distsq=function(a,b){return Q(G(a,b))},Y=a.v.near=function(a,b,c){return X(a,b)=b.r&&a.b<=b.b&&a.t>=b.t},bi=function(a,b){return a.l<=b.x&&a.r>=b.x&&a.b<=b.y&&a.t>=b.y},bj=function(a,b,c,d,e){return a<=e.x&&c>=e.x&&b<=e.y&&d>=e.y},bk=function(a,b){return new bd(g(a.l,b.l),g(a.b,b.b),h(a.r,b.r),h(a.t,b.t))},bl=function(a,b){return new bd(g(a.l,b.x),g(a.b,b.y),h(a.r,b.x),h(a.t,b.y))},bm=function(a){return(a.r-a.l)*(a.t-a.b)},bn=function(a,b){return(h(a.r,b.r)-g(a.l,b.l))*(h(a.t,b.t)-g(a.b,b.b))},bo=function(a,b,c,d,e){return(h(a.r,d)-g(a.l,b))*(h(a.t,e)-g(a.b,c))},bp=function(a,b,c){return bbSegmentQuery(a,b,c)!=Infinity},bq=function(a,b){var c=g(h(a.l,b.x),a.r),d=g(h(a.b,b.y),a.t);return new z(c,d)},br=function(a,b){var c=Math.abs(a.r-a.l),d=(b.x-a.l)%c,e=d>0?d:d+c,f=Math.abs(a.t-a.b),g=(b.y-a.b)%f,h=g>0?g:g+f;return new z(e+a.l,h+a.b)},bs=0,bt=a.NO_GROUP=0,bu=a.ALL_LAYERS=-1;a.resetShapeIdCounter=function(){bs=0};var bv=a.Shape=function(a){this.body=a,this.bb_l=this.bb_b=this.bb_r=this.bb_t=0,this.hashid=bs++,this.sensor=!1,this.e=0,this.u=0,this.surface_v=A,this.collision_type=0,this.group=0,this.layers=bu,this.space=null,this.collisionCode=this.collisionCode};bv.prototype.setElasticity=function(a){this.e=a},bv.prototype.setFriction=function(a){this.body.activate(),this.u=a},bv.prototype.setLayers=function(a){this.body.activate(),this.layers=a},bv.prototype.active=function(){return this.body&&this.body.shapeList.indexOf(this)!==-1},bv.prototype.setBody=function(a){b(!this.active(),"You cannot change the body on an active shape. You must remove the shape, then "),this.body=a},bv.prototype.cacheBB=function(){return this.update(this.body.p,this.body.rot)},bv.prototype.update=function(a,c){b(!isNaN(c.x),"Rotation is NaN"),b(!isNaN(a.x),"Position is NaN"),this.cacheData(a,c)},bv.prototype.getBB=function(){return new bd(this.bb_l,this.bb_b,this.bb_r,this.bb_t)};var bw=function(a){this.shape=a,this.d=Infinity,this.n=A},bx=function(a,b,c){this.shape=a,this.t=b,this.n=c};bx.prototype.hitPoint=function(a,b){return R(a,b,this.t)},bx.prototype.hitDist=function(a,b){return W(a,b)*this.t};var by=a.CircleShape=function(a,b,c){this.c=this.tc=c,this.r=b,this.type="circle",bv.call(this,a)};by.prototype=Object.create(bv.prototype),by.prototype.cacheData=function(a,b){var c=this.tc=O(this.c,b).add(a),d=this.r;this.bb_l=c.x-d,this.bb_b=c.y-d,this.bb_r=c.x+d,this.bb_t=c.y+d},by.prototype.pointQuery=function(a){var b=G(a,this.tc),c=Q(b),d=this.r;if(c=0){var k=(-h-Math.sqrt(j))/(2*g);if(0<=k&&k<=1)return new bx(a,k,S(R(d,e,k)))}};by.prototype.segmentQuery=function(a,b){return bz(this,this.tc,this.r,a,b)};var bA=a.SegmentShape=function(a,b,c,d){this.a=b,this.b=c,this.n=L(S(G(c,b))),this.ta=this.tb=this.tn=null,this.r=d,this.a_tangent=A,this.b_tangent=A,this.type="segment",bv.call(this,a)};bA.prototype=Object.create(bv.prototype),bA.prototype.cacheData=function(a,b){this.ta=F(a,O(this.a,b)),this.tb=F(a,O(this.b,b)),this.tn=O(this.n,b);var c,d,e,f;this.ta.x0?H(c):c,g=G(I(f,e),a),h=F(this.ta,g),i=F(this.tb,g),j=G(b,a);if(J(j,h)*J(j,i)<=0){var k=d+(d>0?-e:e),l=-k,m=B(j,c)-k;if(l*m<0)return new bx(this,l/(l-m),f)}else if(e!=0){var n=bz(this,this.ta,this.r,a,b),o=bz(this,this.tb,this.r,a,b);return n?o&&o.t0)return!1}return!0},bC=a.PolyShape=function(a,c,d){b(c.length>=4,"Polygons require some verts"),b(typeof c[0]=="number","Polygon verticies should be specified in a flattened list"),b(bB(c),"Polygon is concave or has a reversed winding."),this.setVerts(c,d),this.type="poly",bv.call(this,a)};bC.prototype=Object.create(bv.prototype);var bD=function(a,b){this.n=a,this.d=b};bC.prototype.setVerts=function(a,b){var c=a.length,d=c>>1;this.verts=new Array(c),this.tVerts=new Array(c),this.axes=new Array(d),this.tAxes=new Array(d);for(var e=0;e>1]=new bD(j,C(j.x,j.y,f,g)),this.tAxes[e>>1]=new bD(new z(0,0),0)}};var bE=a.BoxShape=function(a,b,c){var d=b/2,e=c/2;return bF(a,new bd(-d,-e,d,e))},bF=a.BoxShape2=function(a,b){var c=[b.l,b.b,b.l,b.t,b.r,b.t,b.r,b.b];return new bC(a,c,A)};bC.prototype.transformVerts=function(a,b){var c=this.verts,d=this.tVerts,e=Infinity,f=-Infinity,i=Infinity,j=-Infinity;for(var k=0;ki)continue;var j=B(b,h),k=(c[g].d-i)/(j-i);if(k<0||10)return!1}return!0},bC.prototype.containsVertPartial=function(a,b,c){var d=this.tAxes;for(var e=0;e0)return!1}return!0};var bG=a.Body=function(a,b){bG.prototype.velocity_func=bG.prototype.updateVelocity,bG.prototype.position_func=bG.prototype.updatePosition,this.p=new z(0,0),this.vx=this.vy=0,this.f=new z(0,0),this.w=0,this.t=0,this.v_limit=Infinity,this.w_limit=Infinity,this.v_biasx=this.v_biasy=0,this.w_bias=0,this.space=null,this.shapeList=[],this.arbiterList=null,this.constraintList=null,this.nodeRoot=null,this.nodeNext=null,this.nodeIdleTime=0,this.setMass(a),this.setMoment(b),this.rot=new z(0,0),this.setAngle(0)},bH=function(){return body=new bG(Infinity,Infinity),body.nodeIdleTime=Infinity,body};if(typeof DEBUG!="undefined"&&DEBUG){var bI=function(a,c){b(a.x==a.x&&a.y==a.y,c)},bJ=function(a,c){b(Math.abs(a.x)!==Infinity&&Math.abs(a.y)!==Infinity,c)},bK=function(a,b){bI(a,b),bJ(a,b)};bG.prototype.sanityCheck=function(){b(this.m===this.m&&this.m_inv===this.m_inv,"Body's mass is invalid."),b(this.i===this.i&&this.i_inv===this.i_inv,"Body's moment is invalid."),bK(this.p,"Body's position is invalid."),bK(this.f,"Body's force is invalid."),b(this.vx===this.vx&&Math.abs(this.vx)!==Infinity,"Body's velocity is invalid."),b(this.vy===this.vy&&Math.abs(this.vy)!==Infinity,"Body's velocity is invalid."),b(this.a===this.a&&Math.abs(this.a)!==Infinity,"Body's angle is invalid."),b(this.w===this.w&&Math.abs(this.w)!==Infinity,"Body's angular velocity is invalid."),b(this.t===this.t&&Math.abs(this.t)!==Infinity,"Body's torque is invalid."),bK(this.rot,"Internal error: Body's rotation vector is invalid."),b(this.v_limit===this.v_limit,"Body's velocity limit is invalid."),b(this.w_limit===this.w_limit,"Body's angular velocity limit is invalid.")}}else bG.prototype.sanityCheck=function(){};bG.prototype.isSleeping=function(){return this.nodeRoot!==null},bG.prototype.isStatic=function(){return this.nodeIdleTime===Infinity},bG.prototype.isRogue=function(){return this.space===null},bG.prototype.setMass=function(a){b(a>0,"Mass must be positive and non-zero."),this.activate(),this.m=a,this.m_inv=1/a},bG.prototype.setMoment=function(a){b(a>0,"Moment of Inertia must be positive and non-zero."),this.activate(),this.i=a,this.i_inv=1/a},bG.prototype.addShape=function(a){this.shapeList.push(a)},bG.prototype.removeShape=function(a){i(this.shapeList,a)};var bL=function(a,b,c){return a===c?a.next(b):(a.a===b?a.next_a=bL(a.next_a,b,c):a.next_b=bL(a.next_b,b,c),a)};bG.prototype.removeConstraint=function(a){this.constraintList=bL(this.constraintList,this,a)},bG.prototype.setPos=function(a){this.activate(),this.sanityCheck(),this.p=a},bG.prototype.setVelocity=function(a){this.activate(),this.vx=a.x,this.vy=a.y},bG.prototype.setAngleInternal=function(a){b(!isNaN(a),"Internal Error: Attempting to set body's angle to NaN"),this.a=a,this.rot.x=Math.cos(a),this.rot.y=Math.sin(a)},bG.prototype.setAngle=function(a){this.activate(),this.sanityCheck(),this.setAngleInternal(a)},bG.prototype.updateVelocity=function(a,b,c){var d=this.vx*b+(a.x+this.f.x*this.m_inv)*c,e=this.vy*b+(a.y+this.f.y*this.m_inv)*c,f=this.v_limit,g=d*d+e*e,h=g>f*f?f/Math.sqrt(len):1;this.vx=d*h,this.vy=e*h;var i=this.w_limit;this.w=t(this.w*b+this.t*this.i_inv*c,-i,i),this.sanityCheck()},bG.prototype.updatePosition=function(a){this.p.x+=(this.vx+this.v_biasx)*a,this.p.y+=(this.vy+this.v_biasy)*a,this.setAngleInternal(this.a+(this.w+this.w_bias)*a),this.v_biasx=this.v_biasy=0,this.w_bias=0,this.sanityCheck()},bG.prototype.resetForces=function(){this.activate(),this.f=new z(0,0),this.t=0},bG.prototype.applyForce=function(a,b){this.activate(),this.f=F(this.f,a),this.t+=J(b,a)},bG.prototype.applyImpulse=function(a,b){this.activate(),cQ(this,a.x,a.y,b)},bG.prototype.getVelAtPoint=function(a){return F(new z(this.vx,this.vy),I(L(a),body.w))},bG.prototype.getVelAtWorldPoint=function(a){return this.getVelAtPoint(G(a,body.p))},bG.prototype.getVelAtLocalPoint=function(a){return this.getVelAtPoint(O(a,body.rot))},bG.prototype.eachShape=function(a){for(var b=0,c=this.shapeList.length;b0){var c=a.query;this.each(function(a){c(a,new bd(a.bb_l,a.bb_b,a.bb_r,a.bb_t),b)})}};var bN=a.BBTree=function(a){bM.call(this,a),this.velocityFunc=null,this.leaves={},this.count=0,this.root=null,this.stamp=0};bN.prototype=Object.create(bM.prototype);var bO=0,bP=function(a,b,c){this.obj=null,this.bb_l=g(b.bb_l,c.bb_l),this.bb_b=g(b.bb_b,c.bb_b),this.bb_r=h(b.bb_r,c.bb_r),this.bb_t=h(b.bb_t,c.bb_t),this.parent=null,this.setA(b),this.setB(c),bO++},bQ=0,bR=function(a,b){this.obj=b,a.getBB(b,this),this.parent=null,this.stamp=1,this.pairs=null,bQ++};bN.prototype.getBB=function(a,b){var c=this.velocityFunc;if(c){var d=.1,e=(a.bb_r-a.bb_l)*d,f=(a.bb_t-a.bb_b)*d,i=I(c(a),.1);b.bb_l=a.bb_l+g(-e,i.x),b.bb_b=a.bb_b+g(-f,i.y),b.bb_r=a.bb_r+h(e,i.x),b.bb_t=a.bb_t+h(f,i.y)}else b.bb_l=a.bb_l,b.bb_b=a.bb_b,b.bb_r=a.bb_r,b.bb_t=a.bb_t},bN.prototype.getStamp=function(){var a=this.dynamicIndex;return a&&a.stamp?a.stamp:this.stamp},bN.prototype.incrementStamp=function(){this.dynamicIndex&&this.dynamicIndex.stamp?this.dynamicIndex.stamp++:this.stamp++};var bS=function(a,b){this.a=a,this.b=b},bT=function(a,b){this.prev=null,this.next=b,this.leaf=a};bT.prototype.unlink=function(){var a=this.next,b=this.prev;a&&(a.a.leaf==this.leaf?a.a.prev=b:a.b.prev=b),b?b.a.leaf==this.leaf?b.a.next=a:b.b.next=a:this.leaf.pairs=a},bR.prototype.clearPairs=function(a){var b=this.pairs,c;this.pairs=null;while(b)b.a.leaf==this?(c=b.a.next,b.b.unlink(),b=c):(c=b.b.next,b.a.unlink(),b=c)};var bU=function(a,b,c){var d=a.pairs,e=b.pairs,f=new bS(new bT(a,d),new bT(b,e));a.pairs=b.pairs=f,d&&(d.a.leaf==a?d.a.prev=f:d.b.prev=f),e&&(e.a.leaf==b?e.a.prev=f:e.b.prev=f)};bP.prototype.setA=function(a){this.A=a,a.parent=this},bP.prototype.setB=function(a){this.B=a,a.parent=this},bR.prototype.isLeaf=!0,bP.prototype.isLeaf=!1,bP.prototype.otherChild=function(a){return this.A==a?this.B:this.A},bP.prototype.replaceChild=function(a,b,d){c(a==this.A||a==this.B,"Node is not a child of parent."),this.A==a?this.setA(b):this.setB(b);for(var e=this;e;e=e.parent){var f=e.A,i=e.B;e.bb_l=g(f.bb_l,i.bb_l),e.bb_b=g(f.bb_b,i.bb_b),e.bb_r=h(f.bb_r,i.bb_r),e.bb_t=h(f.bb_t,i.bb_t)}},bP.prototype.bbArea=bR.prototype.bbArea=function(){return(this.bb_r-this.bb_l)*(this.bb_t-this.bb_b)};var bV=function(a,b){return(h(a.bb_r,b.bb_r)-g(a.bb_l,b.bb_l))*(h(a.bb_t,b.bb_t)-g(a.bb_b,b.bb_b))},bW=function(a,b){return Math.abs(a.bb_l+a.bb_r-b.bb_l-b.bb_r)+Math.abs(a.bb_b+b.bb_t-b.bb_b-b.bb_t)},bX=function(a,b,c){if(a==null)return b;if(a.isLeaf)return new bP(c,b,a);var d=a.B.bbArea()+bV(a.A,b),e=a.A.bbArea()+bV(a.B,b);return d===e&&(d=bW(a.A,b),e=bW(a.B,b)),e=a.bb_r&&this.bb_b<=a.bb_b&&this.bb_t>=a.bb_t},bR.prototype.update=function(a){var b=a.root,c=this.obj;return this.containsObj(c)?!1:(a.getBB(this.obj,this),b=b_(b,this,a),a.root=bX(b,this,a),this.clearPairs(a),this.stamp=a.getStamp(),!0)},bR.prototype.addPairs=function(a){var b=a.dynamicIndex;if(b){var c=b.root;c&&cb(c,this,!0,b,null)}else{var d=a.staticIndex.root;cc(this,a,d,null)}},bN.prototype.insert=function(a,b){var c=new bR(this,a);this.leaves[b]=c,this.root=bX(this.root,c,this),this.count++,c.stamp=this.getStamp(),c.addPairs(this),this.incrementStamp()},bN.prototype.remove=function(a,b){var c=this.leaves[b];delete this.leaves[b],this.root=b_(this.root,c,this),this.count--,c.clearPairs(this)},bN.prototype.contains=function(a,b){return this.leaves[b]!=null};var ce=function(a,b){};bN.prototype.reindexQuery=function(a){if(!this.root)return;var b,c=this.leaves;for(b in c)c[b].update(this);var d=this.staticIndex,e=d&&d.root;cd(this.root,this,e,a),d&&!e&&this.collideStatic(this,d,a),this.incrementStamp()},bN.prototype.reindex=function(){this.reindexQuery(ce)},bN.prototype.reindexObject=function(a,b){var c=this.leaves[b];c&&(c.update(this)&&c.addPairs(this),this.incrementStamp())},bN.prototype.pointQuery=function(a,b){this.root&&bY(this.root,new bd(a.x,a.y,a.x,a.y),b)},bN.prototype.segmentQuery=function(a,b,c,d){this.root&&b$(this.root,a,b,c,d)},bN.prototype.query=function(a,b){this.root&&bY(this.root,a,b)},bN.prototype.count=function(){return this.count},bN.prototype.each=function(a){var b;for(b in this.leaves)a(this.leaves[b].obj)};var cf=function(a,b,c,d,e){return(h(a.bb_r,d)-g(a.bb_l,b))*(h(a.bb_t,e)-g(a.bb_b,c))},cg=function(a,b,c,d){if(d==1)return b[c];if(d==2)return new bP(a,b[c],b[c+1]);var e=b[c],f=e.bb_l,i=e.bb_b,j=e.bb_r,k=e.bb_t,l=c+d;for(var m=c+1;mk-i,o=new Array(d*2);if(n)for(var m=c;m=e*e)return;var h=Math.sqrt(g);return new cq(F(a,I(f,.5+(c-.5*e)/(h?h:Infinity))),h?I(f,1/h):new z(1,0),h-e,0)},ct=function(a,b){var c=cs(a.tc,b.tc,a.r,b.r);return c?[c]:cr},cu=function(a,b){var c=b.ta,d=b.tb,e=a.tc,f=G(d,c),g=u(B(f,G(e,c))/Q(f)),h=F(c,I(f,g)),i=cs(e,h,a.r,b.r);if(i){var j=i.n;return g===0&&B(j,b.a_tangent)<0||g===1&&B(j,b.b_tangent)<0?cr:[i]}return cr},cv=0,cw=function(a,b){var c=0,d=a.valueOnAxis(b[0].n,b[0].d);if(d>0)return-1;for(var e=1;e0)return-1;f>d&&(d=f,c=e)}return cv=d,c},cx=function(a,b,c,e){var f=[],g=a.tVerts;for(var h=0;h>1)))}var k=b.tVerts;for(var h=0;h>1)))}return f.length?f:cx(a,b,c,e)},cz=function(a,b){var c=cw(b,a.tAxes);if(c==-1)return cr;var d=cv,e=cw(a,b.tAxes);if(e==-1)return cr;var f=cv;return d>f?cy(a,b,a.tAxes[c].n,d):cy(a,b,H(b.tAxes[e].n),f)},cA=function(a,b,c){var d=B(b,a.ta)-a.r,e=B(b,a.tb)-a.r;return g(d,e)-c},cB=function(a,b,c,e,f){var g=J(b.tn,b.ta),h=J(b.tn,b.tb),i=I(b.tn,f),j=c.tVerts;for(var k=0;k=n&&n>=h&&a.push(new cq(new z(l,m),i,e,d(c.hashid,k)))}}},cC=function(a,b){var c=[],e=b.tAxes,f=e.length,g=B(a.tn,a.ta),h=b.valueOnAxis(a.tn,g)-a.r,i=b.valueOnAxis(H(a.tn),-g)-a.r;if(i>0||h>0)return cr;var j=0,k=cA(a,e[0].n,e[0].d);if(k>0)return cr;for(var l=0;l0)return cr;m>k&&(k=m,j=l)}var n=H(e[j].n),o=F(a.ta,I(n,a.r)),p=F(a.tb,I(n,a.r));b.containsVert(o.x,o.y)&&c.push(new cq(o,n,k,d(a.hashid,0))),b.containsVert(p.x,p.y)&&c.push(new cq(p,n,k,d(a.hashid,1)));if(h>=k||i>=k)h>i?cB(c,a,b,h,1):cB(c,a,b,i,-1);if(c.length===0){var q=j*2,r=b.tVerts,s=new z(r[q],r[q+1]),t;if(t=cs(a.ta,s,a.r,0,c))return[t];if(t=cs(a.tb,s,a.r,0,c))return[t];var u=f*2,v=new z(r[(q+2)%u],r[(q+3)%u]);if(t=cs(a.ta,v,a.r,0,c))return[t];if(t=cs(a.tb,v,a.r,0,c))return[t]}return c},cD=function(a,b){var c=b.tAxes,d=0,e=B(c[0].n,a.tc)-c[0].d-a.r;for(var f=0;f0)return cr;g>e&&(e=g,d=f)}var h=c[d].n,i=b.tVerts,j=i.length,k=d<<1,l=i[k],m=i[k+1],n=i[(k+2)%j],o=i[(k+3)%j],p=K(h.x,h.y,l,m),q=K(h.x,h.y,n,o),r=J(h,a.tc);if(ri?0:f.nodeIdleTime+a}}var j=this.arbiters;for(var e=0,k=j.length;e=0,"Internal Error: Space lock underflow.");if(!this.locked&&a){var c=this.rousedBodies;for(var d=0;dc.collisionCode){var h=b;b=c,c=h}var i=cE(b,c);if(i.length===0)return;var j=d(b.hashid,c.hashid),k=e.cachedArbiters[j];k||(k=e.cachedArbiters[j]=new ck(b,c)),k.update(i,f,b,c),k.state=="first coll"&&!f.begin(k,e)&&k.ignore(),k.state!=="ignore"&&f.preSolve(k,e)&&!g?e.arbiters.push(k):(k.contacts=null,k.state!=="ignore"&&(k.state="normal")),k.stamp=e.stamp}},cG.prototype.arbiterSetFilter=function(a){var b=this.stamp-a.stamp,c=a.body_a,d=a.body_b;return(c.isStatic()||c.isSleeping())&&(d.isStatic()||d.isSleeping())?!0:(b>=1&&a.state!="cached"&&(a.callSeparate(this),a.state="cached"),b>=this.collisionPersistence?(a.contacts=null,!1):!0)};var cN=function(a){var b=a.body;a.update(b.p,b.rot)};cG.prototype.step=function(a){if(a===0)return;b(A.x===0&&A.y===0,"vzero is invalid"),this.stamp++;var c=this.curr_dt;this.curr_dt=a;var d=this.bodies,e=this.constraints,f=this.arbiters;for(var g=0;g0,"You created a 0 length pin joint. A pivot joint will be much more stable."),this.r1=this.r2=null,this.n=null,this.nMass=0,this.jnAcc=this.jnMax=0,this.bias=0};cZ.prototype=Object.create(cY.prototype),cZ.prototype.preStep=function(a){var b=this.a,c=this.b;this.r1=O(this.anchr1,b.rot),this.r2=O(this.anchr2,c.rot);var d=G(F(c.p,this.r2),F(b.p,this.r1)),e=D(d);this.n=I(d,1/(e?e:Infinity)),this.nMass=1/cU(b,c,this.r1,this.r2,this.n);var f=this.maxBias;this.bias=t(-cX(this.errorBias,a)*(e-this.dist)/a,-f,f),this.jnMax=this.maxForce*a},cZ.prototype.applyCachedImpulse=function(a){var b=I(this.n,this.jnAcc*a);cR(this.a,this.b,this.r1,this.r2,b.x,b.y)},cZ.prototype.applyImpulse=function(){var a=this.a,b=this.b,c=this.n,d=cP(a,b,this.r1,this.r2,c),e=(this.bias-d)*this.nMass,f=this.jnAcc;this.jnAcc=t(f+e,-this.jnMax,this.jnMax),e=this.jnAcc-f,cR(a,b,this.r1,this.r2,c.x*e,c.y*e)},cZ.prototype.getImpulse=function(){return Math.abs(this.jnAcc)};var c$=a.SlideJoint=function(a,b,c,d,e,f){cY.call(this,a,b),this.anchr1=c,this.anchr2=d,this.min=e,this.max=f,this.r1=this.r2=this.n=null,this.nMass=0,this.jnAcc=this.jnMax=0,this.bias=0};c$.prototype=Object.create(cY.prototype),c$.prototype.preStep=function(a){var b=this.a,c=this.b;this.r1=O(this.anchr1,b.rot),this.r2=O(this.anchr2,c.rot);var d=G(F(c.p,this.r2),F(b.p,this.r1)),e=D(d),f=0;e>this.max?(f=e-this.max,this.n=T(d)):e=J(e,f)?(this.clamp=-1,this.r1=G(e,b.p)):(this.clamp=0,this.r1=G(F(I(L(f),-h),I(f,g)),b.p)),cV(b,c,this.r1,this.r2,this.k1,this.k2),this.jMaxLen=this.maxForce*a;var i=G(F(c.p,this.r2),F(b.p,this.r1));this.bias=U(I(i,-cX(this.errorBias,a)/a),this.maxBias)},da.prototype.applyCachedImpulse=function(a){cR(this.a,this.b,this.r1,this.r2,this.jAcc.x*a,this.jAcc.y*a)},da.prototype.grooveConstrain=function(a){var b=this.grv_tn,c=this.clamp*J(a,b)>0?a:N(a,b);return U(c,this.jMaxLen)},da.prototype.applyImpulse=function(){var a=this.a,b=this.b,c=this.r1,d=this.r2,e=cO(a,b,c,d),f=cW(G(this.bias,e),this.k1,this.k2),g=this.jAcc;this.jAcc=this.grooveConstrain(F(g,f)),cR(a,b,this.r1,this.r2,this.jAcc.x-g.x,this.jAcc.y-g.y)},da.prototype.getImpulse=function(){return D(this.jAcc)},da.prototype.setGrooveA=function(a){this.grv_a=a,this.grv_n=L(S(G(this.grv_b,a))),this.activateBodies()},da.prototype.setGrooveB=function(a){this.grv_b=a,this.grv_n=L(S(G(a,this.grv_a))),this.activateBodies()};var db=function(a,b){return(a.restLength-b)*a.stiffness},dc=a.DampedSpring=function(a,b,c,d,e,f,g){cY.call(this,a,b),this.anchr1=c,this.anchr2=d,this.restLength=e,this.stiffness=f,this.damping=g,this.springForceFunc=db,this.target_vrn=this.v_coef=0,this.r1=this.r2=null,this.nMass=0,this.n=null};dc.prototype=Object.create(cY.prototype),dc.prototype.preStep=function(a){var b=this.a,d=this.b;this.r1=O(this.anchr1,b.rot),this.r2=O(this.anchr2,d.rot);var e=G(F(d.p,this.r2),F(b.p,this.r1)),f=D(e);this.n=I(e,1/(f?f:Infinity));var g=cU(b,d,this.r1,this.r2,this.n);c(g!==0,"Unsolvable this."),this.nMass=1/g,this.target_vrn=0,this.v_coef=1-Math.exp(-this.damping*a*g);var h=this.springForceFunc(this,f);cR(b,d,this.r1,this.r2,this.n.x*h*a,this.n.y*h*a)},dc.prototype.applyCachedImpulse=function(a){},dc.prototype.applyImpulse=function(){var a=this.a,b=this.b,c=this.n,d=this.r1,e=this.r2,f=cP(a,b,d,e,c),g=(this.target_vrn-f)*this.v_coef;this.target_vrn=f+g,g*=this.nMass,cR(a,b,this.r1,this.r2,this.n.x*g,this.n.y*g)},dc.prototype.getImpulse=function(){return 0};var dd=function(a,b){return(b-a.restAngle)*a.stiffness},de=a.DampedRotarySpring=function(a,b,c,d,e){cY.call(this,a,b),this.restAngle=c,this.stiffness=d,this.damping=e,this.springTorqueFunc=dd,this.target_wrn=0,this.w_coef=0,this.iSum=0};de.prototype=Object.create(cY.prototype),de.prototype.preStep=function(a){var b=this.a,d=this.b,e=b.i_inv+d.i_inv;c(e!==0,"Unsolvable spring."),this.iSum=1/e,this.w_coef=1-Math.exp(-this.damping*a*e),this.target_wrn=0;var f=this.springTorqueFunc(this,b.a-d.a)*a;b.w-=f*b.i_inv,d.w+=f*d.i_inv},de.prototype.applyImpulse=function(){var a=this.a,b=this.b,c=a.w-b.w,d=(this.target_wrn-c)*this.w_coef;this.target_wrn=c+d;var e=d*this.iSum;a.w+=e*a.i_inv,b.w-=e*b.i_inv};var df=a.RotaryLimitJoint=function(a,b,c,d){cY.call(this,a,b),this.min=c,this.max=d,this.jAcc=0,this.iSum=this.bias=this.jMax=0};df.prototype=Object.create(cY.prototype),df.prototype.preStep=function(a){var b=this.a,c=this.b,d=c.a-b.a,e=0;d>this.max?e=this.max-d:d0?i=h:this.angle=Math.floor((g-e)/f)*f+e,this.iSum=1/(b.i_inv+c.i_inv);var j=this.maxBias;this.bias=t(-cX(this.errorBias,a)*i/a,-j,j),this.jMax=this.maxForce*a,this.bias||(this.jAcc=0)},dg.prototype.applyCachedImpulse=function(a){var b=this.a,c=this.b,d=this.jAcc*a;b.w-=d*b.i_inv,c.w+=d*c.i_inv},dg.prototype.applyImpulse=function(){if(!this.bias)return;var a=this.a,b=this.b,c=b.w-a.w,d=this.ratchet,e=-(this.bias+c)*this.iSum,f=this.jAcc;this.jAcc=t((f+e)*d,0,this.jMax*Math.abs(d))/d,e=this.jAcc-f,a.w-=e*a.i_inv,b.w+=e*b.i_inv},dg.prototype.getImpulse=function(a){return Math.abs(a.jAcc)};var dh=a.GearJoint=function(a,b,c,d){cY.call(this,a,b),this.phase=c,this.ratio=d,this.ratio_inv=1/d,this.jAcc=0,this.iSum=this.bias=this.jMax=0};dh.prototype=Object.create(cY.prototype),dh.prototype.preStep=function(a){var b=this.a,c=this.b;this.iSum=1/(b.i_inv*this.ratio_inv+this.ratio*c.i_inv);var d=this.maxBias;this.bias=t(-cX(this.errorBias,a)*(c.a*this.ratio-b.a-this.phase)/a,-d,d),this.jMax=this.maxForce*a},dh.prototype.applyCachedImpulse=function(a){var b=this.a,c=this.b,d=this.jAcc*a;b.w-=d*b.i_inv*this.ratio_inv,c.w+=d*c.i_inv},dh.prototype.applyImpulse=function(){var a=this.a,b=this.b,c=b.w*this.ratio-a.w,d=(this.bias-c)*this.iSum,e=this.jAcc;this.jAcc=t(e+d,-this.jMax,this.jMax),d=this.jAcc-e,a.w-=d*a.i_inv*this.ratio_inv,b.w+=d*b.i_inv},dh.prototype.getImpulse=function(){return Math.abs(this.jAcc)},dh.prototype.setRatio=function(a){this.ratio=a,this.ratio_inv=1/a,this.activateBodies()};var di=a.SimpleMotor=function(a,b,c){cY.call(this,a,b),this.rate=c,this.jAcc=0,this.iSum=this.jMax=0};di.prototype=Object.create(cY.prototype),di.prototype.preStep=function(a){this.iSum=1/(this.a.i_inv+this.b.i_inv),this.jMax=this.maxForce*a},di.prototype.applyCachedImpulse=function(a){var b=this.a,c=this.b,d=this.jAcc*a;b.w-=d*b.i_inv,c.w+=d*c.i_inv},di.prototype.applyImpulse=function(){var a=this.a,b=this.b,c=b.w-a.w+this.rate,d=-c*this.iSum,e=this.jAcc;this.jAcc=t(e+d,-this.jMax,this.jMax),d=this.jAcc-e,a.w-=d*a.i_inv,b.w+=d*b.i_inv},di.prototype.getImpulse=function(){return Math.abs(this.jAcc)}})() \ No newline at end of file diff --git a/demo/PyramidStack.js b/demo/PyramidStack.js index 28f6be8..c8254b7 100644 --- a/demo/PyramidStack.js +++ b/demo/PyramidStack.js @@ -23,7 +23,7 @@ var PyramidStack = function() { Demo.call(this); var space = this.space; - space.iterations = 30; + //space.iterations = 30; space.gravity = v(0, -100); space.sleepTimeThreshold = 0.5; space.collisionSlop = 0.5; @@ -58,6 +58,7 @@ var PyramidStack = function() { PyramidStack.prototype = Object.create(Demo.prototype); +/* PyramidStack.prototype.update = function(dt) { var steps = 3; @@ -65,7 +66,7 @@ PyramidStack.prototype.update = function(dt) for (var i = 0; i < 3; i++){ this.space.step(dt); } -}; +};*/ addDemo('PyramidStack', PyramidStack); diff --git a/demo/demo.js b/demo/demo.js index 3f59e4e..2cfd077 100644 --- a/demo/demo.js +++ b/demo/demo.js @@ -162,6 +162,7 @@ Demo.prototype.draw = function() { }); // Draw collisions + /* ctx.strokeStyle = "red"; ctx.lineWidth = 2; @@ -181,7 +182,7 @@ Demo.prototype.draw = function() { ctx.lineTo(p.x - 2, p.y + 2); ctx.stroke(); } - } + }*/ if (this.mouseJoint) { ctx.beginPath(); diff --git a/lib/cpBB.js b/lib/cpBB.js index 7434ecb..559230d 100644 --- a/lib/cpBB.js +++ b/lib/cpBB.js @@ -24,7 +24,7 @@ var numBB = 0; // Bounding boxes are JS objects with {l, b, r, t} = left, bottom, right, top, respectively. -var BB = function(l, b, r, t) +var BB = cp.BB = function(l, b, r, t) { this.l = l; this.b = b; @@ -34,6 +34,8 @@ var BB = function(l, b, r, t) numBB++; }; +cp.bb = function(l, b, r, t) { return new BB(l, b, r, t); }; + var bbNewForCircle = function(p, r) { return new BB(