Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix post-step callbacks.

Also regenerate cp.js/cp.min.js
  • Loading branch information...
commit f99d390bcde6fd6e90e6d6add9998ffa7f952858 1 parent 5f28438
@nornagon nornagon authored
Showing with 6 additions and 4 deletions.
  1. +3 −2 cp.js
  2. +1 −1  cp.min.js
  3. +2 −1  lib/cpSpaceStep.js
View
5 cp.js
@@ -3928,7 +3928,7 @@ Body.prototype.activate = function()
Body.prototype.activateStatic = function(filter)
{
- assert(body.isStatic(this), "Body.activateStatic() called on a non-static body.");
+ assert(this.isStatic(), "Body.activateStatic() called on a non-static body.");
for(var arb = this.arbiterList; arb; arb = arb.next(this)){
if(!filter || filter == arb.a || filter == arb.b){
@@ -4336,7 +4336,7 @@ Space.prototype.shapeQuery = function(shape, func)
/// Schedule a post-step callback to be called when cpSpaceStep() finishes.
Space.prototype.addPostStepCallback = function(func)
{
- assertWarn(this.locked,
+ assertSoft(this.locked,
"Adding a post-step callback when the space is not locked is unnecessary. " +
"Post-step callbacks will not called until the end of the next call to cpSpaceStep() or the next query.");
@@ -4350,6 +4350,7 @@ Space.prototype.runPostStepCallbacks = function()
for(var i = 0; i < this.postStepCallbacks.length; i++){
this.postStepCallbacks[i]();
}
+ this.postStepCallbacks = []
@josephg Owner
josephg added a note

;

Also I think this.postStepCallbacks.length = 0; is faster..?

@nornagon Collaborator

Done. BTW, http://jsperf.com/array-reset-2 indicates arr = [] is rather faster.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
};
// **** Locking Functions
View
2  cp.min.js
@@ -1,2 +1,2 @@
(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 a<b?a:b},f=function(a,b){return a>b?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;c<a.length;c++)if(a[c]===b){a[c]=a[a.length-1],a.length--;return}},j=a.momentForCircle=function(a,b,c,d){return a*(.5*(b*b+c*c)+Q(d))},k=a.areaForCircle=function(a,b){return Math.PI*Math.abs(a*a-b*b)},l=a.momentForSegment=function(a,b,c){var d=D(G(c,b)),e=I(F(b,c),.5);return a*(d*d/12+Q(e))},m=a.areaForSegment=function(a,b,c){return c*(Math.PI*c+2*W(a,b))},n=a.momentForPoly=function(a,b,c){var d=0,e=0,f=b.length;for(var g=0;g<f;g+=2){var h=b[g]+c.x,i=b[g+1]+c.y,j=b[(g+2)%f]+c.x,k=b[(g+3)%f]+c.y,l=K(j,k,h,i),m=C(h,i,h,i)+C(h,i,j,k)+C(j,k,j,k);d+=l*m,e+=l}return a*d/(6*e)},o=a.areaForPoly=function(a){var b=0;for(var c=0,d=a.length;c<d;c+=2)b+=J(new z(a[c],a[c+1]),new z(a[(c+2)%d],a[(c+3)%d]));return-b/2},p=a.centroidForPoly=function(a){var b=0,c=new z(0,0);for(var d=0,e=a.length;d<e;d+=2){var f=new z(a[d],a[d+1]),g=new z(a[(d+2)%e],a[(d+3)%e]),h=J(f,g);b+=h,c=F(c,I(F(f,g),h))}return I(c,1/(3*b))},q=a.recenterPoly=function(a){var b=p(a);for(var c=0;c<a.length;c+=2)a[c]-=b.x,a[c+1]-=b.y},r=a.momentForBox=function(a,b,c){return a*(b*b+c*c)/12},s=a.momentForBox2=function(a,b){return width=b.r-b.l,height=b.t-b.b,offset=I([b.l+b.r,b.b+b.t],.5),r(a,width,height)+a*Q(offset)},t=function(a,b,c){return g(h(a,b),c)},u=function(a){return h(0,g(a,1))},v=function(a,b,c){return a*(1-c)+b*c},w=function(a,b,c){return a+t(b-a,-c,c)},x=0,y={},z=a.Vect=function(a,b){this.x=a,this.y=b,x++};a.v=function(a,b){return new z(a,b)};var A=a.vzero=new z(0,0),B=a.v.dot=function(a,b){return a.x*b.x+a.y*b.y},C=function(a,b,c,d){return a*c+b*d},D=a.v.len=function(a){return Math.sqrt(B(a,a))},E=a.v.eql=function(a,b){return a.x===b.x&&a.y===b.y},F=a.v.add=function(a,b){return new z(a.x+b.x,a.y+b.y)};z.prototype.add=function(a){return this.x+=a.x,this.y+=a.y,this};var G=a.v.sub=function(a,b){return new z(a.x-b.x,a.y-b.y)};z.prototype.sub=function(a){return this.x-=a.x,this.y-=a.y,this};var H=a.v.neg=function(a){return new z(-a.x,-a.y)};z.prototype.neg=function(){return this.x=-this.x,this.y=-this.y,this};var I=a.v.mult=function(a,b){return new z(a.x*b,a.y*b)};z.prototype.mult=function(a){return this.x*=a,this.y*=a,this};var J=a.v.cross=function(a,b){return a.x*b.y-a.y*b.x},K=function(a,b,c,d){return a*d-b*c},L=a.v.perp=function(a){return new z(-a.y,a.x)},M=a.v.pvrperp=function(a){return new z(a.y,-a.x)},N=a.v.project=function(a,b){return I(b,B(a,b)/Q(b))};z.prototype.project=function(a){return this.mult(B(this,a)/Q(a)),this};var O=a.v.rotate=function(a,b){return new z(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x)};z.prototype.rotate=function(a){return this.x=this.x*a.x-this.y*a.y,this.y=this.x*a.y+this.y*a.x,this};var P=a.v.unrotate=function(a,b){return new z(a.x*b.x+a.y*b.y,a.y*b.x-a.x*b.y)},Q=a.v.lengthsq=function(a){return B(a,a)},R=a.v.lerp=function(a,b,c){return F(I(a,1-c),I(b,c))},S=a.v.normalize=function(a){return I(a,1/D(a))},T=a.v.normalize_safe=function(a){return a.x===0&&a.y===0?A:S(a)},U=a.v.clamp=function(a,b){return B(a,a)>b*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)<c*c},Z=a.v.slerp=function(a,b,c){var d=Math.acos(B(a,b));if(d){var e=1/Math.sin(d);return F(I(a,Math.sin((1-c)*d)*e),I(b,Math.sin(c*d)*e))}return a},$=a.v.slerpconst=function(a,b,c){var d=Math.acos(B(a,b));return Z(a,b,g(c,d)/d)},_=a.v.forangle=function(a){return new z(Math.cos(a),Math.sin(a))},ba=a.v.toangle=function(a){return Math.atan2(a.y,a.x)},bb=a.v.str=function(a){return"("+a.x.toFixed(3)+", "+a.y.toFixed(3)+")"},bc=0,bd=a.BB=function(a,b,c,d){this.l=a,this.b=b,this.r=c,this.t=d,bc++};a.bb=function(a,b,c,d){return new bd(a,b,c,d)};var be=function(a,b){return new bd(a.x-b,a.y-b,a.x+b,a.y+b)},bf=function(a,b){return a.l<=b.r&&b.l<=a.r&&a.b<=b.t&&b.b<=a.t},bg=function(a,b,c,d,e){return a.l<=d&&b<=a.r&&a.b<=e&&c<=a.t},bh=function(a,b){return a.l<=b.l&&a.r>=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<d*d){var e=new bw(this),f=Math.sqrt(c);return e.d=d-f,e.n=I(b,1/f),e}};var bz=function(a,b,c,d,e,f){d=G(d,b),e=G(e,b);var g=B(d,d)-2*B(d,e)+B(e,e),h=-2*B(d,d)+2*B(d,e),i=B(d,d)-c*c,j=h*h-4*g*i;if(j>=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.x<this.tb.x?(c=this.ta.x,d=this.tb.x):(c=this.tb.x,d=this.ta.x),this.ta.y<this.tb.y?(e=this.ta.y,f=this.tb.y):(e=this.tb.y,f=this.ta.y);var g=this.r;this.bb_l=c-g,this.bb_b=e-g,this.bb_r=d+g,this.bb_t=f+g},bA.prototype.pointQuery=function(a){if(!bj(this.bb_l,this.bb_b,this.bb_r,this.bb_t,a))return;var b=this.ta,c=this.tb,d=G(c,b),e=u(B(d,G(a,b))/Q(d)),f=F(b,I(d,e)),g=G(a,f),h=Q(g),i=this.r;if(h<i*i){var j=new bw(this),k=Math.sqrt(h);return j.d=i-k,j.n=I(g,1/k),j}},bA.prototype.segmentQuery=function(a,b){var c=this.tn,d=B(G(this.ta,a),c),e=this.r,f=d>0?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.t<n.t?o:n:o}},bA.prototype.setNeighbors=function(a,b){this.a_tangent=G(a,this.a),this.b_tangent=G(b,this.b)},bA.prototype.setEndpoints=function(a,b){this.a=a,this.b=b,this.n=L(S(G(b,a)))};var bB=function(a){var b=a.length;for(var c=0;c<b;c+=2){var d=a[c],e=a[c+1],f=a[(c+2)%b],g=a[(c+3)%b],h=a[(c+4)%b],i=a[(c+5)%b];if(K(f-d,g-e,h-f,i-g)>0)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<c;e+=2){var f=a[e]+b.x,g=a[e+1]+b.y,h=a[(e+2)%c]+b.x,i=a[(e+3)%c]+b.y,j=S(L(new z(h-f,i-g)));this.verts[e]=f,this.verts[e+1]=g,this.axes[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;k<c.length;k+=2){var l=c[k],m=c[k+1],n=a.x+l*b.x-m*b.y,o=a.y+l*b.y+m*b.x;d[k]=n,d[k+1]=o,e=g(e,n),f=h(f,n),i=g(i,o),j=h(j,o)}this.bb_l=e,this.bb_b=i,this.bb_r=f,this.bb_t=j},bC.prototype.transformAxes=function(a,b){var c=this.axes,d=this.tAxes;for(var e=0;e<c.length;e++){var f=O(c[e].n,b);d[e].n=f,d[e].d=B(a,f)+c[e].d}},bC.prototype.cacheData=function(a,b){this.transformAxes(a,b),this.transformVerts(a,b)},bC.prototype.pointQuery=function(a){if(!bj(this.bb_l,this.bb_b,this.bb_r,this.bb_t,a))return;var b=new bw(this),c=this.tAxes;for(var d=0;d<c.length;d++){var e=c[d].n,f=c[d].d-B(e,a);if(f<0)return;f<b.d&&(b.d=f,b.n=e)}return b},bC.prototype.segmentQuery=function(a,b){var c=this.tAxes,d=this.tVerts,e=c.length,f=e*2;for(var g=0;g<e;g++){var h=c[g].n,i=B(a,h);if(c[g].d>i)continue;var j=B(b,h),k=(c[g].d-i)/(j-i);if(k<0||1<k)continue;var l=R(a,b,k),m=-J(h,l),n=-K(h.x,h.y,d[g*2],d[g*2+1]),o=-K(h.x,h.y,d[(g*2+2)%f],d[(g*2+3)%f]);if(n<=m&&m<=o)return new bx(this,k,h)}},bC.prototype.valueOnAxis=function(a,b){var c=this.tVerts,d=C(a.x,a.y,c[0],c[1]);for(var e=2;e<c.length;e+=2)d=g(d,C(a.x,a.y,c[e],c[e+1]));return d-b},bC.prototype.containsVert=function(a,b){var c=this.tAxes;for(var d=0;d<c.length;d++){var e=c[d].n,f=C(e.x,e.y,a,b)-c[d].d;if(f>0)return!1}return!0},bC.prototype.containsVertPartial=function(a,b,c){var d=this.tAxes;for(var e=0;e<d.length;e++){var c=d[e].n;if(B(c,c)<0)continue;var f=C(c.x,c.y,a,b)-d[e].d;if(f>0)return!1}return!0};var bG=a.Body=function(a,b){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.velocity_func=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.position_func=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;b<c;b++)a(this.shapeList[b])},bG.prototype.eachConstraint=function(a){var b=this.constraintList;while(b){var c=b.next(body);a(b),b=c}},bG.prototype.eachArbiter=function(a){var b=this.arbiterList;while(b){var c=b.next(this);b.swappedColl=this===b.body_b,a(b),b=c}},bG.prototype.local2World=function(a){return F(this.p,O(a,this.rot))},bG.prototype.world2Local=function(a){return P(G(a,this.p),this.rot)},bG.prototype.kineticEnergy=function(){var a=this.vx*this.vx+this.vy*this.vy,b=this.w*this.w;return(a?a*this.m:0)+(b?b*this.i:0)};var bM=a.SpatialIndex=function(a){this.staticIndex=a,a&&(b(!a.dynamicIndex,"This static index is already associated with a dynamic index."),a.dynamicIndex=this)};bM.prototype.collideStatic=function(a,b){if(a.count>0){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<d?a.setB(bX(a.B,b,c)):a.setA(bX(a.A,b,c)),a.bb_l=g(a.bb_l,b.bb_l),a.bb_b=g(a.bb_b,b.bb_b),a.bb_r=h(a.bb_r,b.bb_r),a.bb_t=h(a.bb_t,b.bb_t),a};bP.prototype.intersectsBB=bR.prototype.intersectsBB=function(a){return this.bb_l<=a.r&&a.l<=this.bb_r&&this.bb_b<=a.t&&a.b<=this.bb_t};var bY=function(a,b,c){a.intersectsBB(b)&&(a.isLeaf?c(a.obj):(bY(a.A,b,c),bY(a.B,b,c)))},bZ=function(a,b,c){var d=1/(c.x-b.x),e=a.bb_l==b.x?-Infinity:(a.bb_l-b.x)*d,f=a.bb_r==b.x?Infinity:(a.bb_r-b.x)*d,i=g(e,f),j=h(e,f),k=1/(c.y-b.y),l=a.bb_b==b.y?-Infinity:(a.bb_b-b.y)*k,m=a.bb_t==b.y?Infinity:(a.bb_t-b.y)*k,n=g(l,m),o=h(l,m);if(n<=j&&i<=o){var p=h(i,n),q=g(j,o);if(0<=q&&p<=1)return h(p,0)}return Infinity},b$=function(a,b,c,d,e){if(a.isLeaf)return e(a.obj);var f=bZ(a.A,b,c),h=bZ(a.B,b,c);return f<h?(f<d&&(d=g(d,b$(a.A,b,c,d,e))),h<d&&(d=g(d,b$(a.B,b,c,d,e)))):(h<d&&(d=g(d,b$(a.B,b,c,d,e))),f<d&&(d=g(d,b$(a.A,b,c,d,e)))),d},b_=function(a,b,c){if(b==a)return null;var d=b.parent;if(d==a){var e=a.otherChild(b);return e.parent=a.parent,e}return d.parent.replaceChild(d,d.otherChild(b),c),a},ca=function(a,b){return a.bb_l<=b.bb_r&&b.bb_l<=a.bb_r&&a.bb_b<=b.bb_t&&b.bb_b<=a.bb_t},cb=function(a,b,c,d,e){ca(b,a)&&(a.isLeaf?c?bU(b,a,d):(a.stamp<b.stamp&&bU(a,b,d),e&&e(b.obj,a.obj)):(cb(a.A,b,c,d,e),cb(a.B,b,c,d,e)))},cc=function(a,b,c,d){if(a.stamp==b.getStamp()){c&&cb(c,a,!1,b,d);for(var e=a;e.parent;e=e.parent)e==e.parent.A?cb(e.parent.B,a,!0,b,d):cb(e.parent.A,a,!1,b,d)}else{var f=a.pairs;while(f)a==f.b.leaf?(d&&d(f.a.leaf.obj,a.obj),f=f.b.next):f=f.a.next}},cd=function(a,b,c,d){a.isLeaf?cc(a,b,c,d):(cd(a.A,b,c,d),cd(a.B,b,c,d))};bR.prototype.containsObj=function(a){return this.bb_l<=a.bb_l&&this.bb_r>=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;m<l;m++)e=b[m],f=g(f,e.bb_l),i=g(i,e.bb_b),j=h(j,e.bb_r),k=h(k,e.bb_t);var n=j-f>k-i,o=new Array(d*2);if(n)for(var m=c;m<l;m++)o[2*m+0]=b[m].bb_l,o[2*m+1]=b[m].bb_r;else for(var m=c;m<l;m++)o[2*m+0]=b[m].bb_b,o[2*m+1]=b[m].bb_t;o.sort(function(a,b){return a-b});var p=(o[d-1]+o[d])*.5,q=f,r=i,s=j,t=k,u=f,v=i,w=j,x=k;n?s=u=p:t=v=p;var y=l;for(var z=c;z<y;){var e=b[z];cf(e,u,v,w,x)<cf(e,q,r,s,t)?(y--,b[z]=b[y],b[y]=e):z++}if(y==d){var e=null;for(var m=c;m<l;m++)e=bX(e,b[m],a);return e}return NodeNew(a,cg(a,b,c,y-c),cg(a,b,y,l-y))};bN.prototype.optimize=function(){var a=new Array(this.count),b=0;for(var c in this.leaves)a[b++]=this.nodes[c];this.root=cg(tree,a,a.length)};var ch=function(a,b){!a.isLeaf&&b<=10&&(ch(a.a,b+1),ch(a.b,b+1));var c="";for(var d=0;d<b;d++)c+=" "};bN.prototype.log=function(){this.root&&ch(this.root,0)};var ci=a.CollisionHandler=function(){this.a=this.b=0};ci.prototype.begin=function(a,b){return!0},ci.prototype.preSolve=function(a,b){return!0},ci.prototype.postSolve=function(a,b){},ci.prototype.separate=function(a,b){};var cj=4,ck=function(a,b){this.e=0,this.u=0,this.surface_vr=A,this.a=a,this.body_a=a.body,this.b=b,this.body_b=b.body,this.thread_a_next=this.thread_a_prev=null,this.thread_b_next=this.thread_b_prev=null,this.contacts=null,this.stamp=0,this.handler=null,this.swappedColl=!1,this.state="first coll"};ck.prototype.totalImpulse=function(){var a=this.contacts,b=new z(0,0);for(var c=0,d=a.length;c<d;c++){var e=a[c];b.add(I(e.n,e.jnAcc))}return this.swappedColl?b:b.neg()},ck.prototype.totalImpulseWithFriction=function(){var a=this.contacts,b=new z(0,0);for(var c=0,d=a.length;c<d;c++){var e=a[c];b.add((new z(e.jnAcc,e.jtAcc)).rotate(e.n))}return this.swappedColl?b:b.neg()},ck.prototype.totalKE=function(){var a=(1-arb.e)/(1+arb.e),b=0,c=arb.contacts;for(var d=0,e=c.length;d<e;d++){var f=c[d],g=f.jnAcc,h=f.jtAcc;b+=a*g*g/f.nMass+h*h/f.tMass}return b},ck.prototype.ignore=function(){this.state="ignore"},ck.prototype.getA=function(){return this.swappedColl?this.b:this.a},ck.prototype.getB=function(){return this.swappedColl?this.a:this.b},ck.prototype.isFirstContact=function(){return this.state==="first coll"};var cl=function(a,b,c){this.point=a,this.normal=b,this.dist=c};ck.prototype.getContactPointSet=function(){var a=new Array(this.contacts.length),b;for(b=0;b<a.length;b++)a[b]=new cl(this.contacts[b].p,this.contacts[b].n,this.contacts[b].dist);return a},ck.prototype.getNormal=function(a){var b=this.contacts[a].n;return this.swappedColl?H(b):b},ck.prototype.getPoint=function(a){return this.contacts[a].p},ck.prototype.getDepth=function(a){return this.contacts[a].dist};var cm=function(a,b,c,d){c?c.body_a===b?c.thread_a_next=d:c.thread_b_next=d:b.arbiterList=d,d&&(d.body_a===b?d.thread_a_prev=c:d.thread_b_prev=c)};ck.prototype.unthread=function(){cm(this,this.body_a,this.thread_a_prev,this.thread_a_next),cm(this,this.body_b,this.thread_b_prev,this.thread_b_next),this.thread_a_prev=this.thread_a_next=null,this.thread_b_prev=this.thread_b_next=null},ck.prototype.update=function(a,b,c,d){if(this.contacts)for(var e=0;e<this.contacts.length;e++){var f=this.contacts[e];for(var g=0;g<a.length;g++){var h=a[g];h.hash===f.hash&&(h.jnAcc=f.jnAcc,h.jtAcc=f.jtAcc)}}this.contacts=a,this.handler=b,this.swappedColl=c.collision_type!==b.a,this.e=c.e*d.e,this.u=c.u*d.u,this.surface_vr=G(c.surface_v,d.surface_v),this.a=c,this.body_a=c.body,this.b=d,this.body_b=d.body,this.state=="cached"&&(this.state="first coll")},ck.prototype.preStep=function(a,b,c){var d=this.body_a,e=this.body_b;for(var f=0;f<this.contacts.length;f++){var h=this.contacts[f];h.r1=G(h.p,d.p),h.r2=G(h.p,e.p),h.nMass=1/cU(d,e,h.r1,h.r2,h.n),h.tMass=1/cU(d,e,h.r1,h.r2,L(h.n)),h.bias=-c*g(0,h.dist+b)/a,h.jBias=0,h.bounce=cP(d,e,h.r1,h.r2,h.n)*this.e}},ck.prototype.applyCachedImpulse=function(a){if(this.isFirstContact())return;var b=this.body_a,c=this.body_b;for(var d=0;d<this.contacts.length;d++){var e=this.contacts[d],f=e.n.x,g=e.n.y,h=f*e.jnAcc-g*e.jtAcc,i=f*e.jtAcc+g*e.jnAcc;cR(b,c,e.r1,e.r2,h*a,i*a)}};var cn=0,co=0;ck.prototype.applyImpulse=function(){cn++;var a=this.body_a,b=this.body_b,c=this.surface_vr,d=this.u;for(var e=0;e<this.contacts.length;e++){co++;var f=this.contacts[e],g=f.nMass,i=f.n,j=f.r1,k=f.r2,l=b.vx-k.y*b.w-(a.vx-j.y*a.w),m=b.vy+k.x*b.w-(a.vy+j.x*a.w),n=i.x*(b.v_biasx-k.y*b.w_bias-a.v_biasx+j.y*a.w_bias)+i.y*(k.x*b.w_bias+b.v_biasy-j.x*a.w_bias-a.v_biasy),o=C(l,m,i.x,i.y),p=C(l+c.x,m+c.y,-i.y,i.x),q=(f.bias-n)*g,r=f.jBias;f.jBias=h(r+q,0);var s=-(f.bounce+o)*g,u=f.jnAcc;f.jnAcc=h(u+s,0);var v=d*f.jnAcc,w=-p*f.tMass,x=f.jtAcc;f.jtAcc=t(x+w,-v,v);var y=i.x*(f.jBias-r),z=i.y*(f.jBias-r);cS(a,-y,-z,j),cS(b,y,z,k);var A=f.jnAcc-u,B=f.jtAcc-x;cR(a,b,j,k,i.x*A-i.y*B,i.x*B+i.y*A)}},ck.prototype.callSeparate=function(a){var b=a.lookupHandler(this.a.collision_type,this.b.collision_type);b.separate(this,a)},ck.prototype.next=function(a){return this.body_a==a?this.thread_a_next:this.thread_b_next};var cp=0,cq=function(a,b,c,d){this.p=a,this.n=b,this.dist=c,this.r1=this.r2=A,this.nMass=this.tMass=this.bounce=this.bias=0,this.jnAcc=this.jtAcc=this.jBias=0,this.hash=d,cp++},cr=[],cs=function(a,b,c,d){var e=c+d,f=G(b,a),g=Q(f);if(g>=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;e<b.length;e++){var f=a.valueOnAxis(b[e].n,b[e].d);if(f>0)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<g.length;h+=2){var i=g[h],j=g[h+1];b.containsVertPartial(i,j,H(c))&&f.push(new cq(new z(i,j),c,e,d(a.hashid,h)))}var k=b.tVerts;for(var h=0;h<k.length;h+=2){var i=k[h],j=k[h+1];a.containsVertPartial(i,j,c)&&f.push(new cq(new z(i,j),c,e,d(b.hashid,h)))}return f},cy=function(a,b,c,e){var f=[],g=a.tVerts;for(var h=0;h<g.length;h+=2){var i=g[h],j=g[h+1];b.containsVert(i,j)&&f.push(new cq(new z(i,j),c,e,d(a.hashid,h>>1)))}var k=b.tVerts;for(var h=0;h<k.length;h+=2){var i=k[h],j=k[h+1];a.containsVert(i,j)&&f.push(new cq(new z(i,j),c,e,d(b.hashid,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<j.length;k+=2){var l=j[k],m=j[k+1];if(C(l,m,i.x,i.y)<B(b.tn,b.ta)*f+b.r){var n=K(b.tn.x,b.tn.y,l,m);g>=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;l<f;l++){var m=cA(a,e[l].n,e[l].d);if(m>0)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;f<c.length;f++){var g=B(c[f].n,a.tc)-c[f].d-a.r;if(g>0)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(r<q){var s=cs(a.tc,new z(n,o),a.r,0,s);return s?[s]:cr}if(r<p)return[new cq(G(a.tc,I(h,a.r+e/2)),H(h),e,0)];var s=cs(a.tc,new z(l,m),a.r,0,s);return s?[s]:cr};by.prototype.collisionCode=0,bA.prototype.collisionCode=1,bC.prototype.collisionCode=2,by.prototype.collisionTable=[ct,cu,cD],bA.prototype.collisionTable=[null,function(a,a){return cr},cC],bC.prototype.collisionTable=[null,null,cz];var cE=a.collideShapes=function(a,c){return b(a.collisionCode<=c.collisionCode,"Collided shapes must be sorted by type"),a.collisionTable[c.collisionCode](a,c)},cF=new ci,cG=a.Space=function(){this.stamp=0,this.curr_dt=0,this.bodies=[],this.rousedBodies=[],this.sleepingComponents=[],this.staticShapes=new bN(null),this.activeShapes=new bN(this.staticShapes),this.arbiters=[],this.contactBuffersHead=null,this.cachedArbiters={},this.constraints=[],this.locked=0,this.collisionHandlers={},this.defaultHandler=cF,this.postStepCallbacks=[],this.iterations=10,this.gravity=A,this.damping=1,this.idleSpeedThreshold=0,this.sleepTimeThreshold=Infinity,this.collisionSlop=.1,this.collisionBias=Math.pow(.9,60),this.collisionPersistence=3,this.enableContactGraph=!1,this.staticBody=new bG(Infinity,Infinity),this.staticBody.nodeIdleTime=Infinity,this.collideShapes=this.makeCollideShapes()};cG.prototype.isLocked=function(){return this.locked};var cH=function(a){b(!a.locked,"This addition/removal cannot be done safely during a call to cpSpaceStep() or during a query. Put these calls into a post-step callback.")};cG.prototype.addCollisionHandler=function(a,b,c,e,f,g){cH(this),this.removeCollisionHandler(a,b);var h=new ci;h.a=a,h.b=b,c&&(h.begin=c),e&&(h.preSolve=e),f&&(h.postSolve=f),g&&(h.separate=g),this.collisionHandlers[d(a,b)]=h},cG.prototype.removeCollisionHandler=function(a,b){cH(this),delete this.collisionHandlers[d(a,b)]},cG.prototype.setDefaultCollisionHandler=function(a,b,c,d){cH(this);var e=new ci;a&&(e.begin=a),b&&(e.preSolve=b),c&&(e.postSolve=c),d&&(e.separate=d),this.defaultHandler=e},cG.prototype.lookupHandler=function(a,b){return this.collisionHandlers[d(a,b)]||this.defaultHandler},cG.prototype.addShape=function(a){var c=a.body;return c.isStatic()?this.addStaticShape(a):(b(!a.space,"This shape is already added to a space and cannot be added to another."),cH(this),c.activate(),c.addShape(a),a.update(c.p,c.rot),this.activeShapes.insert(a,a.hashid),a.space=this,a)},cG.prototype.addStaticShape=function(a){b(!a.space,"This shape is already added to a space and cannot be added to another."),cH(this);var c=a.body;return c.addShape(a),a.update(c.p,c.rot),this.staticShapes.insert(a,a.hashid),a.space=this,a},cG.prototype.addBody=function(a){return b(!a.isStatic(),"Static bodies cannot be added to a space as they are not meant to be simulated."),b(!a.space,"This body is already added to a space and cannot be added to another."),cH(this),this.bodies.push(a
-),a.space=this,a},cG.prototype.addConstraint=function(a){b(!a.space,"This shape is already added to a space and cannot be added to another."),cH(this);var c=a.a,d=a.b;return c.activate(),d.activate(),this.constraints.push(a),a.next_a=c.constraintList,c.constraintList=a,a.next_b=d.constraintList,d.constraintList=a,a.space=this,a},cG.prototype.filterArbiters=function(a,b){for(var c in this.cachedArbiters){var d=this.cachedArbiters[c];if(a===d.body_a&&(b===d.a||b===null)||a===d.body_b&&(b===d.b||b===null))b&&d.state!=="cached"&&d.callSeparate(this),d.unthread(),i(this.arbiters,d),delete this.cachedArbiters[c]}},cG.prototype.removeShape=function(a){var c=a.body;c.isStatic()?this.removeStaticShape(a):(b(this.containsShape(a),"Cannot remove a shape that was not added to the space. (Removed twice maybe?)"),cH(this),c.activate(),c.removeShape(a),this.filterArbiters(c,a),this.activeShapes.remove(a,a.hashid),a.space=null)},cG.prototype.removeStaticShape=function(a){b(this.containsShape(a),"Cannot remove a static or sleeping shape that was not added to the space. (Removed twice maybe?)"),cH(this);var c=a.body;c.isStatic()&&c.activateStatic(a),c.removeShape(a),this.filterArbiters(c,a),this.staticShapes.remove(a,a.hashid),a.space=null},cG.prototype.removeBody=function(a){b(this.containsBody(a),"Cannot remove a body that was not added to the space. (Removed twice maybe?)"),cH(this),a.activate(),i(this.bodies,a),a.space=null},cG.prototype.removeConstraint=function(a){b(this.containsConstraint(a),"Cannot remove a constraint that was not added to the space. (Removed twice maybe?)"),cH(this),a.a.activate(),a.b.activate(),i(this.constraints,a),a.a.removeConstraint(a),a.b.removeConstraint(a),a.space=null},cG.prototype.containsShape=function(a){return a.space===this},cG.prototype.containsBody=function(a){return a.space==this},cG.prototype.containsConstraint=function(a){return a.space==this},cG.prototype.uncacheArbiter=function(a){delete this.cachedArbiters[d(a.a.hashid,a.b.hashid)],i(this.arbiters,a)},cG.prototype.eachBody=function(a){this.lock();var b=this.bodies;for(var c=0;c<b.length;c++)a(b[c]);var d=this.sleepingComponents;for(var c=0;c<d.length;c++){var e=d[c],f=e;while(f){var g=f.nodeNext;a(f),f=g}}this.unlock(!0)},cG.prototype.eachShape=function(a){this.lock(),this.activeShapes.each(a),this.staticShapes.each(a),this.unlock(!0)},cG.prototype.eachConstraint=function(a){this.lock();var b=this.constraints;for(var c=0;c<b.length;c++)a(b[c]);this.unlock(!0)},cG.prototype.reindexStatic=function(){b(!this.locked,"You cannot manually reindex objects while the space is locked. Wait until the current query or step is complete."),this.staticShapes.each(function(a){var b=a.body;a.update(b.p,b.rot)}),this.staticShapes.reindex()},cG.prototype.reindexShape=function(a){b(!this.locked,"You cannot manually reindex objects while the space is locked. Wait until the current query or step is complete.");var c=a.body;a.update(c.p,c.rot),this.activeShapes.reindexObject(a,a.hashid),this.staticShapes.reindexObject(a,a.hashid)},cG.prototype.reindexShapesForBody=function(a){for(var b=a.shapeList;b;b=b.next)this.reindexShape(b)},cG.prototype.useSpatialHash=function(a,b){throw new Error("Spatial Hash not yet implemented!");var c,d},cG.prototype.activateBody=function(a){b(!a.isRogue(),"Internal error: Attempting to activate a rogue body.");if(this.locked)this.rousedBodies.indexOf(a)===-1&&this.rousedBodies.push(a);else{this.bodies.push(a);for(var c=0;c<a.shapeList.length;c++){var e=a.shapeList[c];this.staticShapes.remove(e,e.hashid),this.activeShapes.insert(e,e.hashid)}for(var f=a.arbiterList;f;f=f.next(a)){var g=f.body_a;if(a===g||g.isStatic()){var h=f.a,i=f.b;this.cachedArbiters[d(h.hashid,i.hashid)]=f,f.stamp=this.stamp,f.handler=this.lookupHandler(h.collision_type,i.collision_type),this.arbiters.push(f)}}for(var j=a.constraintList;j;j=j.nodeNext){var g=j.a;(a===g||g.isStatic())&&this.constraints.push(j)}}},cG.prototype.deactivateBody=function(a){b(!a.isRogue(),"Internal error: Attempting to deactivate a rogue body."),i(this.bodies,a);for(var c=0;c<a.shapeList.length;c++){var d=a.shapeList[c];this.activeShapes.remove(d,d.hashid),this.staticShapes.insert(d,d.hashid)}for(var e=a.arbiterList;e;e=e.next(a)){var f=e.body_a;(a===f||f.isStatic())&&this.uncacheArbiter(e)}for(var g=a.constraintList;g;g=g.nodeNext){var f=g.a;(a===f||f.isStatic())&&i(this.constraints,g)}};var cI=function(a){return a?a.nodeRoot:null},cJ=function(a){if(!a||!a.isSleeping(a))return;b(!a.isRogue(),"Internal Error: componentActivate() called on a rogue body.");var c=a.space,d=a;while(d){var e=d.nodeNext;d.nodeIdleTime=0,d.nodeRoot=null,d.nodeNext=null,c.activateBody(d),d=e}i(c.sleepingComponents,a)};bG.prototype.activate=function(){this.isRogue()||(this.nodeIdleTime=0,cJ(cI(this)))},bG.prototype.activateStatic=function(a){b(body.isStatic(this),"Body.activateStatic() called on a non-static body.");for(var c=this.arbiterList;c;c=c.next(this))(!a||a==c.a||a==c.b)&&(c.body_a==this?c.body_b:c.body_a).activate()},bG.prototype.pushArbiter=function(a){c((a.body_a===this?a.thread_a_next:a.thread_b_next)===null,"Internal Error: Dangling contact graph pointers detected. (A)"),c((a.body_a===this?a.thread_a_prev:a.thread_b_prev)===null,"Internal Error: Dangling contact graph pointers detected. (B)");var b=this.arbiterList;c(b===null||(b.body_a===this?b.thread_a_prev:b.thread_b_prev)===null,"Internal Error: Dangling contact graph pointers detected. (C)"),a.body_a===this?a.thread_a_next=b:a.thread_b_next=b,b&&(b.body_a===this?b.thread_a_prev=a:b.thread_b_prev=a),this.arbiterList=a};var cK=function(a,b){b.nodeRoot=a,b!==a&&(b.nodeNext=a.nodeNext,a.nodeNext=b)},cL=function(a,b){if(!b.isRogue()){var d=cI(b);if(d==null){cK(a,b);for(var e=b.arbiterList;e;e=e.next(b))cL(a,b==e.body_a?e.body_b:e.body_a);for(var f=b.constraintList;f;f=f.next(b))cL(a,b==f.a?f.b:f.a)}else c(d===a,"Internal Error: Inconsistency detected in the contact graph.")}},cM=function(a,b){for(var c=a;c;c=c.nodeNext)if(c.nodeIdleTime<b)return!0;return!1};cG.prototype.processComponents=function(a){var b=this.sleepTimeThreshold!==Infinity,d=this.bodies;for(var e=0;e<d.length;e++){var f=d[e];c(f.nodeNext===null,"Internal Error: Dangling next pointer detected in contact graph."),c(f.nodeRoot===null,"Internal Error: Dangling root pointer detected in contact graph.")}if(b){var g=this.idleSpeedThreshold,h=g?g*g:Q(this.gravity)*a*a;for(var e=0;e<d.length;e++){var f=d[e],i=h?f.m*h:0;f.nodeIdleTime=f.kineticEnergy()>i?0:f.nodeIdleTime+a}}var j=this.arbiters;for(var e=0,k=j.length;e<k;e++){var l=j[e],m=l.body_a,n=l.body_b;b&&((n.isRogue()&&!n.isStatic()||m.isSleeping())&&m.activate(),(m.isRogue()&&!m.isStatic()||n.isSleeping())&&n.activate()),m.pushArbiter(l),n.pushArbiter(l)}if(b){var o=this.constraints;for(var e=0;e<o.length;e++){var p=o[e],m=p.a,n=p.b;n.isRogue()&&!n.isStatic()&&m.activate(),m.isRogue()&&!m.isStatic()&&n.activate()}for(var e=0;e<d.length;){var f=d[e];if(cI(f)===null){cL(f,f);if(!cM(f,this.sleepTimeThreshold)){this.sleepingComponents.push(f);for(var q=f;q;q=q.nodeNext)this.deactivateBody(q);continue}}e++,f.nodeRoot=null,f.nodeNext=null}}},bG.prototype.sleep=function(){this.sleepWithGroup(null)},bG.prototype.sleepWithGroup=function(a){b(!this.isStatic()&&!this.isRogue(),"Rogue and static bodies cannot be put to sleep.");var c=this.space;b(c,"Cannot put a rogue body to sleep."),b(!c.locked,"Bodies cannot be put to sleep during a query or a call to cpSpaceStep(). Put these calls into a post-step callback."),b(a===null||a.isSleeping(),"Cannot use a non-sleeping body as a group identifier.");if(this.isSleeping()){b(cI(this)===cI(a),"The body is already sleeping and it's group cannot be reassigned.");return}for(var d=0;d<body.shapeList.length;d++)body.shapeList.update(this.p,this.rot);c.deactivateBody(this);if(a){var e=cI(a);this.nodeRoot=e,this.nodeNext=e.nodeNext,this.nodeIdleTime=0,e.nodeNext=this}else this.nodeRoot=this,this.nodeNext=null,this.nodeIdleTime=0,c.sleepingComponents.push(this);i(c.bodies,this)},cG.prototype.activateShapesTouchingShape=function(a){this.sleepTimeThreshold!==Infinity&&this.shapeQuery(a,function(a,b){a.body.activate()})},cG.prototype.pointQuery=function(a,b,c,d){var e=function(e){(!e.group||c!==e.group)&&b&e.layers&&e.pointQuery(a)&&d(e)};this.lock(),this.activeShapes.pointQuery(a,e),this.staticShapes.pointQuery(a,e),this.unlock(!0)},cG.prototype.pointQueryFirst=function(a,b,c){var d=null;return this.pointQuery(a,b,c,function(a){a.sensor||(d=a)}),d},cG.prototype.segmentQuery=function(a,b,c,d,e){var f=function(f){var g;return(!f.group||d!==f.group)&&c&f.layers&&(g=f.segmentQuery(a,b))&&e(f,g.t,g.n),1};this.lock(),this.staticShapes.segmentQuery(a,b,1,f),this.activeShapes.segmentQuery(a,b,1,f),this.unlock(!0)},cG.prototype.segmentQueryFirst=function(a,b,c,d){var e=null,f=function(f){var g;return(!f.group||d!==f.group)&&c&f.layers&&!f.sensor&&(g=f.segmentQuery(a,b))&&(e===null||g.t<e.t)&&(e=g),e?e.t:1};return this.staticShapes.segmentQuery(a,b,1,f),this.activeShapes.segmentQuery(a,b,e?e.t:1,f),e},cG.prototype.bbQuery=function(a,b,c,d){var e=function(e){(!e.group||c!==e.group)&&b&e.layers&&bg(a,e.bb_l,e.bb_b,e.bb_r,e.bb_t)&&d(e)};this.lock(),this.activeShapes.query(a,e),this.staticShapes.query(a,e),this.unlock(!0)},cG.prototype.shapeQuery=function(a,b){var c=a.body;c&&a.update(c.p,c.rot);var d=new bd(a.bb_l,a.bb_b,a.bb_r,a.bb_t),e=!1,f=function(c){var d=a;if(d.group&&d.group===c.group||!(d.layers&c.layers)||d===c)return;var f;if(d.collisionCode<=c.collisionCode)f=cpCollideShapes(d,c);else{f=cpCollideShapes(c,d);for(var g=0;g<f.length;g++)f[g].n=H(f[g].n)}if(f.length){e=!d.sensor&&!c.sensor;if(b){var h=new Array(f.length);for(var g=0;g<f.length;g++)h[g]=new cl(f[g].p,f[g].n,f[g].dist);b(c,h)}}};return this.lock(),this.activeShapes.query(d,f),this.staticShapes.query(d,f),this.unlock(!0),e},cG.prototype.addPostStepCallback=function(a){assertWarn(this.locked,"Adding a post-step callback when the space is not locked is unnecessary. Post-step callbacks will not called until the end of the next call to cpSpaceStep() or the next query."),this.postStepCallbacks.push(a)},cG.prototype.runPostStepCallbacks=function(){for(var a=0;a<this.postStepCallbacks.length;a++)this.postStepCallbacks[a]()},cG.prototype.lock=function(){this.locked++},cG.prototype.unlock=function(a){this.locked--,b(this.locked>=0,"Internal Error: Space lock underflow.");if(!this.locked&&a){var c=this.rousedBodies;for(var d=0;d<c.length;d++)this.activateBody(c[d]);c.length=0,this.runPostStepCallbacks()}},cG.prototype.makeCollideShapes=function(){var a=this;return function(b,c){var e=a;if(!(b.bb_l<=c.bb_r&&c.bb_l<=b.bb_r&&b.bb_b<=c.bb_t&&c.bb_b<=b.bb_t)||b.body===c.body||b.group&&b.group===c.group||!(b.layers&c.layers))return;var f=e.lookupHandler(b.collision_type,c.collision_type),g=b.sensor||c.sensor;if(g&&f===cF)return;if(b.collisionCode>c.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;g<f.length;g++){var h=f[g];h.state="normal",!h.body_a.isSleeping()&&!h.body_b.isSleeping()&&h.unthread()}f.length=0,this.lock();for(var g=0;g<d.length;g++){var i=d[g];i.position_func(a)}this.activeShapes.each(cN),this.activeShapes.reindexQuery(this.collideShapes),this.unlock(!1),this.processComponents(a),this.lock();for(var j in this.cachedArbiters)this.arbiterSetFilter(this.cachedArbiters[j])||delete this.cachedArbiters[j];var k=this.collisionSlop,l=1-Math.pow(this.collisionBias,a);for(var g=0;g<f.length;g++)f[g].preStep(a,k,l);for(var g=0;g<e.length;g++){var m=e[g];m.preSolve(this),m.preStep(a)}var n=Math.pow(this.damping,a),o=this.gravity;for(var g=0;g<d.length;g++){var i=d[g];i.velocity_func(o,n,a)}var p=c===0?0:a/c;for(var g=0;g<f.length;g++)f[g].applyCachedImpulse(p);for(var g=0;g<e.length;g++){var m=e[g];m.applyCachedImpulse(p)}for(var g=0;g<this.iterations;g++){for(var q=0;q<f.length;q++)f[q].applyImpulse();for(var q=0;q<e.length;q++)e[q].applyImpulse()}for(var g=0;g<e.length;g++)e[g].postSolve(this);for(var g=0;g<f.length;g++){var h=f[g];h.handler.postSolve(h,this)}this.unlock(!0)};var cO=function(a,b,c,d){var e=a.vx+ -c.y*a.w,f=a.vy+c.x*a.w,g=b.vx+ -d.y*b.w,h=b.vy+d.x*b.w;return new z(g-e,h-f)},cP=function(a,b,c,d,e){var f=a.vx+ -c.y*a.w,g=a.vy+c.x*a.w,h=b.vx+ -d.y*b.w,i=b.vy+d.x*b.w;return C(h-f,i-g,e.x,e.y)},cQ=function(a,b,c,d){a.vx+=b*a.m_inv,a.vy+=c*a.m_inv,a.w+=a.i_inv*(d.x*c-d.y*b)},cR=function(a,b,c,d,e,f){cQ(a,-e,-f,c),cQ(b,e,f,d)},cS=function(a,b,c,d){a.v_biasx+=b*a.m_inv,a.v_biasy+=c*a.m_inv,a.w_bias+=a.i_inv*K(d.x,d.y,b,c)},cT=function(a,b,c){var d=J(b,c);return a.m_inv+a.i_inv*d*d},cU=function(a,b,d,e,f){var g=cT(a,d,f)+cT(b,e,f);return c(g!==0,"Unsolvable collision or constraint."),g},cV=function(a,b,d,e,f,g){var h,i,j,k,l=a.m_inv+b.m_inv;h=l,i=0,j=0,k=l;var m=a.i_inv,n=d.x*d.x*m,o=d.y*d.y*m,p=-d.x*d.y*m;h+=o,i+=p,j+=p,k+=n;var q=b.i_inv,r=e.x*e.x*q,s=e.y*e.y*q,t=-e.x*e.y*q;h+=s,i+=t,j+=t,k+=r;var u=h*k-i*j;c(u!==0,"Unsolvable constraint.");var v=1/u;f.x=k*v,f.y=-i*v,g.x=-j*v,g.y=h*v},cW=function(a,b,c){return new z(B(a,b),B(a,c))},cX=function(a,b){return 1-Math.pow(a,b)},cY=a.Constraint=function(a,b){this.a=a,this.b=b,this.space=null,this.next_a=null,this.next_b=null,this.maxForce=Infinity,this.errorBias=Math.pow(.9,60),this.maxBias=Infinity};cY.prototype.activateBodies=function(){this.a&&this.a.activate(),this.b&&this.b.activate()},cY.prototype.preStep=function(a){},cY.prototype.applyCachedImpulse=function(a){},cY.prototype.applyImpulse=function(){},cY.prototype.getImpulse=function(){return 0},cY.prototype.preSolve=function(a){},cY.prototype.postSolve=function(a){},cY.prototype.next=function(a){return this.a===a?this.next_a:this.next_b};var cZ=a.PinJoint=function(a,b,d,e){cY.call(this,a,b),this.anchr1=d,this.anchr2=e;var f=a?F(a.p,O(d,a.rot)):d,g=b?F(b.p,O(e,b.rot)):e;this.dist=D(G(g,f)),c(this.dist>0,"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<this.min?(f=this.min-e,this.n=H(T(d))):(this.n=A,this.jnAcc=0),this.nMass=1/cU(b,c,this.r1,this.r2,this.n);var g=this.maxBias;this.bias=t(-cX(this.errorBias,a)*f/a,-g,g),this.jnMax=this.maxForce*a},c$.prototype.applyCachedImpulse=function(a){var b=this.jnAcc*a;cR(this.a,this.b,this.r1,this.r2,this.n.x*b,this.n.y*b)},c$.prototype.applyImpulse=function(){if(this.n.x===0&&this.n.y===0)return;var a=this.a,b=this.b,c=this.n,d=this.r1,e=this.r2,f=cO(a,b,d,e),g=B(f,c),h=(this.bias-g)*this.nMass,i=this.jnAcc;this.jnAcc=t(i+h,-this.jnMax,0),h=this.jnAcc-i,cR(a,b,this.r1,this.r2,c.x*h,c.y*h)},c$.prototype.getImpulse=function(){return Math.abs(this.jnAcc)};var c_=a.PivotJoint=function(a,b,c,d){cY.call(this,a,b);if(typeof d=="undefined"){var e=c;c=a?a.world2Local(e):e,d=b?b.world2Local(e):e}this.anchr1=c,this.anchr2=d,this.r1=this.r2=A,this.k1=new z(0,0),this.k2=new z(0,0),this.jAcc=A,this.jMaxLen=0,this.bias=A};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),cV(b,c,this.r1,this.r2,this.k1,this.k2),this.jMaxLen=this.maxForce*a;var d=G(F(c.p,this.r2),F(b.p,this.r1));this.bias=U(I(d,-cX(this.errorBias,a)/a),this.maxBias)},c_.prototype.applyCachedImpulse=function(a){cR(this.a,this.b,this.r1,this.r2,this.jAcc.x*a,this.jAcc.y*a)},c_.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=U(F(this.jAcc,f),this.jMaxLen),cR(a,b,this.r1,this.r2,this.jAcc.x-g.x,this.jAcc.y-g.y)},c_.prototype.getImpulse=function(){return D(this.jAcc)};var da=a.GrooveJoint=function(a,b,c,d,e){cY.call(this,a,b),this.grv_a=c,this.grv_b=d,this.grv_n=L(S(G(d,c))),this.anchr2=e,this.grv_tn=null,this.clamp=0,this.r1=this.r2=null,this.k1=new z(0,0),this.k2=new z(0,0),this.jAcc=A,this.jMaxLen=0,this.bias=null};da.prototype=Object.create(cY.prototype),da.prototype.preStep=function(a){var b=this.a,c=this.b,d=b.local2World(this.grv_a),e=b.local2World(this.grv_b),f=O(this.grv_n,b.rot),g=B(d,f);this.grv_tn=f,this.r2=O(this.anchr2,c.rot);var h=J(F(c.p,this.r2),f);h<=J(d,f)?(this.clamp=1,this.r1=G(d,b.p)):h>=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:d<this.min&&(e=this.min-d),this.iSum=1/(1/b.i+1/c.i);var f=this.maxBias;this.bias=t(-cX(this.errorBias,a)*e/a,-f,f),this.jMax=this.maxForce*a,this.bias||(this.jAcc=0)},df.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},df.prototype.applyImpulse=function(){if(!this.bias)return;var a=this.a,b=this.b,c=b.w-a.w,d=-(this.bias+c)*this.iSum,e=this.jAcc;this.bias<0?this.jAcc=t(e+d,0,this.jMax):this.jAcc=t(e+d,-this.jMax,0),d=this.jAcc-e,a.w-=d*a.i_inv,b.w+=d*b.i_inv},df.prototype.getImpulse=function(){return Math.abs(joint.jAcc)};var dg=a.RatchetJoint=function(a,b,c,d){cY.call(this,a,b),this.angle=0,this.phase=c,this.ratchet=d,this.angle=(b?b.a:0)-(a?a.a:0),this.iSum=this.bias=this.jAcc=this.jMax=0};dg.prototype=Object.create(cY.prototype),dg.prototype.preStep=function(a){var b=this.a,c=this.b,d=this.angle,e=this.phase,f=this.ratchet,g=c.a-b.a,h=d-g,i=0;h*f>0?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)}})();
+),a.space=this,a},cG.prototype.addConstraint=function(a){b(!a.space,"This shape is already added to a space and cannot be added to another."),cH(this);var c=a.a,d=a.b;return c.activate(),d.activate(),this.constraints.push(a),a.next_a=c.constraintList,c.constraintList=a,a.next_b=d.constraintList,d.constraintList=a,a.space=this,a},cG.prototype.filterArbiters=function(a,b){for(var c in this.cachedArbiters){var d=this.cachedArbiters[c];if(a===d.body_a&&(b===d.a||b===null)||a===d.body_b&&(b===d.b||b===null))b&&d.state!=="cached"&&d.callSeparate(this),d.unthread(),i(this.arbiters,d),delete this.cachedArbiters[c]}},cG.prototype.removeShape=function(a){var c=a.body;c.isStatic()?this.removeStaticShape(a):(b(this.containsShape(a),"Cannot remove a shape that was not added to the space. (Removed twice maybe?)"),cH(this),c.activate(),c.removeShape(a),this.filterArbiters(c,a),this.activeShapes.remove(a,a.hashid),a.space=null)},cG.prototype.removeStaticShape=function(a){b(this.containsShape(a),"Cannot remove a static or sleeping shape that was not added to the space. (Removed twice maybe?)"),cH(this);var c=a.body;c.isStatic()&&c.activateStatic(a),c.removeShape(a),this.filterArbiters(c,a),this.staticShapes.remove(a,a.hashid),a.space=null},cG.prototype.removeBody=function(a){b(this.containsBody(a),"Cannot remove a body that was not added to the space. (Removed twice maybe?)"),cH(this),a.activate(),i(this.bodies,a),a.space=null},cG.prototype.removeConstraint=function(a){b(this.containsConstraint(a),"Cannot remove a constraint that was not added to the space. (Removed twice maybe?)"),cH(this),a.a.activate(),a.b.activate(),i(this.constraints,a),a.a.removeConstraint(a),a.b.removeConstraint(a),a.space=null},cG.prototype.containsShape=function(a){return a.space===this},cG.prototype.containsBody=function(a){return a.space==this},cG.prototype.containsConstraint=function(a){return a.space==this},cG.prototype.uncacheArbiter=function(a){delete this.cachedArbiters[d(a.a.hashid,a.b.hashid)],i(this.arbiters,a)},cG.prototype.eachBody=function(a){this.lock();var b=this.bodies;for(var c=0;c<b.length;c++)a(b[c]);var d=this.sleepingComponents;for(var c=0;c<d.length;c++){var e=d[c],f=e;while(f){var g=f.nodeNext;a(f),f=g}}this.unlock(!0)},cG.prototype.eachShape=function(a){this.lock(),this.activeShapes.each(a),this.staticShapes.each(a),this.unlock(!0)},cG.prototype.eachConstraint=function(a){this.lock();var b=this.constraints;for(var c=0;c<b.length;c++)a(b[c]);this.unlock(!0)},cG.prototype.reindexStatic=function(){b(!this.locked,"You cannot manually reindex objects while the space is locked. Wait until the current query or step is complete."),this.staticShapes.each(function(a){var b=a.body;a.update(b.p,b.rot)}),this.staticShapes.reindex()},cG.prototype.reindexShape=function(a){b(!this.locked,"You cannot manually reindex objects while the space is locked. Wait until the current query or step is complete.");var c=a.body;a.update(c.p,c.rot),this.activeShapes.reindexObject(a,a.hashid),this.staticShapes.reindexObject(a,a.hashid)},cG.prototype.reindexShapesForBody=function(a){for(var b=a.shapeList;b;b=b.next)this.reindexShape(b)},cG.prototype.useSpatialHash=function(a,b){throw new Error("Spatial Hash not yet implemented!");var c,d},cG.prototype.activateBody=function(a){b(!a.isRogue(),"Internal error: Attempting to activate a rogue body.");if(this.locked)this.rousedBodies.indexOf(a)===-1&&this.rousedBodies.push(a);else{this.bodies.push(a);for(var c=0;c<a.shapeList.length;c++){var e=a.shapeList[c];this.staticShapes.remove(e,e.hashid),this.activeShapes.insert(e,e.hashid)}for(var f=a.arbiterList;f;f=f.next(a)){var g=f.body_a;if(a===g||g.isStatic()){var h=f.a,i=f.b;this.cachedArbiters[d(h.hashid,i.hashid)]=f,f.stamp=this.stamp,f.handler=this.lookupHandler(h.collision_type,i.collision_type),this.arbiters.push(f)}}for(var j=a.constraintList;j;j=j.nodeNext){var g=j.a;(a===g||g.isStatic())&&this.constraints.push(j)}}},cG.prototype.deactivateBody=function(a){b(!a.isRogue(),"Internal error: Attempting to deactivate a rogue body."),i(this.bodies,a);for(var c=0;c<a.shapeList.length;c++){var d=a.shapeList[c];this.activeShapes.remove(d,d.hashid),this.staticShapes.insert(d,d.hashid)}for(var e=a.arbiterList;e;e=e.next(a)){var f=e.body_a;(a===f||f.isStatic())&&this.uncacheArbiter(e)}for(var g=a.constraintList;g;g=g.nodeNext){var f=g.a;(a===f||f.isStatic())&&i(this.constraints,g)}};var cI=function(a){return a?a.nodeRoot:null},cJ=function(a){if(!a||!a.isSleeping(a))return;b(!a.isRogue(),"Internal Error: componentActivate() called on a rogue body.");var c=a.space,d=a;while(d){var e=d.nodeNext;d.nodeIdleTime=0,d.nodeRoot=null,d.nodeNext=null,c.activateBody(d),d=e}i(c.sleepingComponents,a)};bG.prototype.activate=function(){this.isRogue()||(this.nodeIdleTime=0,cJ(cI(this)))},bG.prototype.activateStatic=function(a){b(this.isStatic(),"Body.activateStatic() called on a non-static body.");for(var c=this.arbiterList;c;c=c.next(this))(!a||a==c.a||a==c.b)&&(c.body_a==this?c.body_b:c.body_a).activate()},bG.prototype.pushArbiter=function(a){c((a.body_a===this?a.thread_a_next:a.thread_b_next)===null,"Internal Error: Dangling contact graph pointers detected. (A)"),c((a.body_a===this?a.thread_a_prev:a.thread_b_prev)===null,"Internal Error: Dangling contact graph pointers detected. (B)");var b=this.arbiterList;c(b===null||(b.body_a===this?b.thread_a_prev:b.thread_b_prev)===null,"Internal Error: Dangling contact graph pointers detected. (C)"),a.body_a===this?a.thread_a_next=b:a.thread_b_next=b,b&&(b.body_a===this?b.thread_a_prev=a:b.thread_b_prev=a),this.arbiterList=a};var cK=function(a,b){b.nodeRoot=a,b!==a&&(b.nodeNext=a.nodeNext,a.nodeNext=b)},cL=function(a,b){if(!b.isRogue()){var d=cI(b);if(d==null){cK(a,b);for(var e=b.arbiterList;e;e=e.next(b))cL(a,b==e.body_a?e.body_b:e.body_a);for(var f=b.constraintList;f;f=f.next(b))cL(a,b==f.a?f.b:f.a)}else c(d===a,"Internal Error: Inconsistency detected in the contact graph.")}},cM=function(a,b){for(var c=a;c;c=c.nodeNext)if(c.nodeIdleTime<b)return!0;return!1};cG.prototype.processComponents=function(a){var b=this.sleepTimeThreshold!==Infinity,d=this.bodies;for(var e=0;e<d.length;e++){var f=d[e];c(f.nodeNext===null,"Internal Error: Dangling next pointer detected in contact graph."),c(f.nodeRoot===null,"Internal Error: Dangling root pointer detected in contact graph.")}if(b){var g=this.idleSpeedThreshold,h=g?g*g:Q(this.gravity)*a*a;for(var e=0;e<d.length;e++){var f=d[e],i=h?f.m*h:0;f.nodeIdleTime=f.kineticEnergy()>i?0:f.nodeIdleTime+a}}var j=this.arbiters;for(var e=0,k=j.length;e<k;e++){var l=j[e],m=l.body_a,n=l.body_b;b&&((n.isRogue()&&!n.isStatic()||m.isSleeping())&&m.activate(),(m.isRogue()&&!m.isStatic()||n.isSleeping())&&n.activate()),m.pushArbiter(l),n.pushArbiter(l)}if(b){var o=this.constraints;for(var e=0;e<o.length;e++){var p=o[e],m=p.a,n=p.b;n.isRogue()&&!n.isStatic()&&m.activate(),m.isRogue()&&!m.isStatic()&&n.activate()}for(var e=0;e<d.length;){var f=d[e];if(cI(f)===null){cL(f,f);if(!cM(f,this.sleepTimeThreshold)){this.sleepingComponents.push(f);for(var q=f;q;q=q.nodeNext)this.deactivateBody(q);continue}}e++,f.nodeRoot=null,f.nodeNext=null}}},bG.prototype.sleep=function(){this.sleepWithGroup(null)},bG.prototype.sleepWithGroup=function(a){b(!this.isStatic()&&!this.isRogue(),"Rogue and static bodies cannot be put to sleep.");var c=this.space;b(c,"Cannot put a rogue body to sleep."),b(!c.locked,"Bodies cannot be put to sleep during a query or a call to cpSpaceStep(). Put these calls into a post-step callback."),b(a===null||a.isSleeping(),"Cannot use a non-sleeping body as a group identifier.");if(this.isSleeping()){b(cI(this)===cI(a),"The body is already sleeping and it's group cannot be reassigned.");return}for(var d=0;d<body.shapeList.length;d++)body.shapeList.update(this.p,this.rot);c.deactivateBody(this);if(a){var e=cI(a);this.nodeRoot=e,this.nodeNext=e.nodeNext,this.nodeIdleTime=0,e.nodeNext=this}else this.nodeRoot=this,this.nodeNext=null,this.nodeIdleTime=0,c.sleepingComponents.push(this);i(c.bodies,this)},cG.prototype.activateShapesTouchingShape=function(a){this.sleepTimeThreshold!==Infinity&&this.shapeQuery(a,function(a,b){a.body.activate()})},cG.prototype.pointQuery=function(a,b,c,d){var e=function(e){(!e.group||c!==e.group)&&b&e.layers&&e.pointQuery(a)&&d(e)};this.lock(),this.activeShapes.pointQuery(a,e),this.staticShapes.pointQuery(a,e),this.unlock(!0)},cG.prototype.pointQueryFirst=function(a,b,c){var d=null;return this.pointQuery(a,b,c,function(a){a.sensor||(d=a)}),d},cG.prototype.segmentQuery=function(a,b,c,d,e){var f=function(f){var g;return(!f.group||d!==f.group)&&c&f.layers&&(g=f.segmentQuery(a,b))&&e(f,g.t,g.n),1};this.lock(),this.staticShapes.segmentQuery(a,b,1,f),this.activeShapes.segmentQuery(a,b,1,f),this.unlock(!0)},cG.prototype.segmentQueryFirst=function(a,b,c,d){var e=null,f=function(f){var g;return(!f.group||d!==f.group)&&c&f.layers&&!f.sensor&&(g=f.segmentQuery(a,b))&&(e===null||g.t<e.t)&&(e=g),e?e.t:1};return this.staticShapes.segmentQuery(a,b,1,f),this.activeShapes.segmentQuery(a,b,e?e.t:1,f),e},cG.prototype.bbQuery=function(a,b,c,d){var e=function(e){(!e.group||c!==e.group)&&b&e.layers&&bg(a,e.bb_l,e.bb_b,e.bb_r,e.bb_t)&&d(e)};this.lock(),this.activeShapes.query(a,e),this.staticShapes.query(a,e),this.unlock(!0)},cG.prototype.shapeQuery=function(a,b){var c=a.body;c&&a.update(c.p,c.rot);var d=new bd(a.bb_l,a.bb_b,a.bb_r,a.bb_t),e=!1,f=function(c){var d=a;if(d.group&&d.group===c.group||!(d.layers&c.layers)||d===c)return;var f;if(d.collisionCode<=c.collisionCode)f=cpCollideShapes(d,c);else{f=cpCollideShapes(c,d);for(var g=0;g<f.length;g++)f[g].n=H(f[g].n)}if(f.length){e=!d.sensor&&!c.sensor;if(b){var h=new Array(f.length);for(var g=0;g<f.length;g++)h[g]=new cl(f[g].p,f[g].n,f[g].dist);b(c,h)}}};return this.lock(),this.activeShapes.query(d,f),this.staticShapes.query(d,f),this.unlock(!0),e},cG.prototype.addPostStepCallback=function(a){c(this.locked,"Adding a post-step callback when the space is not locked is unnecessary. Post-step callbacks will not called until the end of the next call to cpSpaceStep() or the next query."),this.postStepCallbacks.push(a)},cG.prototype.runPostStepCallbacks=function(){for(var a=0;a<this.postStepCallbacks.length;a++)this.postStepCallbacks[a]();this.postStepCallbacks=[]},cG.prototype.lock=function(){this.locked++},cG.prototype.unlock=function(a){this.locked--,b(this.locked>=0,"Internal Error: Space lock underflow.");if(!this.locked&&a){var c=this.rousedBodies;for(var d=0;d<c.length;d++)this.activateBody(c[d]);c.length=0,this.runPostStepCallbacks()}},cG.prototype.makeCollideShapes=function(){var a=this;return function(b,c){var e=a;if(!(b.bb_l<=c.bb_r&&c.bb_l<=b.bb_r&&b.bb_b<=c.bb_t&&c.bb_b<=b.bb_t)||b.body===c.body||b.group&&b.group===c.group||!(b.layers&c.layers))return;var f=e.lookupHandler(b.collision_type,c.collision_type),g=b.sensor||c.sensor;if(g&&f===cF)return;if(b.collisionCode>c.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;g<f.length;g++){var h=f[g];h.state="normal",!h.body_a.isSleeping()&&!h.body_b.isSleeping()&&h.unthread()}f.length=0,this.lock();for(var g=0;g<d.length;g++){var i=d[g];i.position_func(a)}this.activeShapes.each(cN),this.activeShapes.reindexQuery(this.collideShapes),this.unlock(!1),this.processComponents(a),this.lock();for(var j in this.cachedArbiters)this.arbiterSetFilter(this.cachedArbiters[j])||delete this.cachedArbiters[j];var k=this.collisionSlop,l=1-Math.pow(this.collisionBias,a);for(var g=0;g<f.length;g++)f[g].preStep(a,k,l);for(var g=0;g<e.length;g++){var m=e[g];m.preSolve(this),m.preStep(a)}var n=Math.pow(this.damping,a),o=this.gravity;for(var g=0;g<d.length;g++){var i=d[g];i.velocity_func(o,n,a)}var p=c===0?0:a/c;for(var g=0;g<f.length;g++)f[g].applyCachedImpulse(p);for(var g=0;g<e.length;g++){var m=e[g];m.applyCachedImpulse(p)}for(var g=0;g<this.iterations;g++){for(var q=0;q<f.length;q++)f[q].applyImpulse();for(var q=0;q<e.length;q++)e[q].applyImpulse()}for(var g=0;g<e.length;g++)e[g].postSolve(this);for(var g=0;g<f.length;g++){var h=f[g];h.handler.postSolve(h,this)}this.unlock(!0)};var cO=function(a,b,c,d){var e=a.vx+ -c.y*a.w,f=a.vy+c.x*a.w,g=b.vx+ -d.y*b.w,h=b.vy+d.x*b.w;return new z(g-e,h-f)},cP=function(a,b,c,d,e){var f=a.vx+ -c.y*a.w,g=a.vy+c.x*a.w,h=b.vx+ -d.y*b.w,i=b.vy+d.x*b.w;return C(h-f,i-g,e.x,e.y)},cQ=function(a,b,c,d){a.vx+=b*a.m_inv,a.vy+=c*a.m_inv,a.w+=a.i_inv*(d.x*c-d.y*b)},cR=function(a,b,c,d,e,f){cQ(a,-e,-f,c),cQ(b,e,f,d)},cS=function(a,b,c,d){a.v_biasx+=b*a.m_inv,a.v_biasy+=c*a.m_inv,a.w_bias+=a.i_inv*K(d.x,d.y,b,c)},cT=function(a,b,c){var d=J(b,c);return a.m_inv+a.i_inv*d*d},cU=function(a,b,d,e,f){var g=cT(a,d,f)+cT(b,e,f);return c(g!==0,"Unsolvable collision or constraint."),g},cV=function(a,b,d,e,f,g){var h,i,j,k,l=a.m_inv+b.m_inv;h=l,i=0,j=0,k=l;var m=a.i_inv,n=d.x*d.x*m,o=d.y*d.y*m,p=-d.x*d.y*m;h+=o,i+=p,j+=p,k+=n;var q=b.i_inv,r=e.x*e.x*q,s=e.y*e.y*q,t=-e.x*e.y*q;h+=s,i+=t,j+=t,k+=r;var u=h*k-i*j;c(u!==0,"Unsolvable constraint.");var v=1/u;f.x=k*v,f.y=-i*v,g.x=-j*v,g.y=h*v},cW=function(a,b,c){return new z(B(a,b),B(a,c))},cX=function(a,b){return 1-Math.pow(a,b)},cY=a.Constraint=function(a,b){this.a=a,this.b=b,this.space=null,this.next_a=null,this.next_b=null,this.maxForce=Infinity,this.errorBias=Math.pow(.9,60),this.maxBias=Infinity};cY.prototype.activateBodies=function(){this.a&&this.a.activate(),this.b&&this.b.activate()},cY.prototype.preStep=function(a){},cY.prototype.applyCachedImpulse=function(a){},cY.prototype.applyImpulse=function(){},cY.prototype.getImpulse=function(){return 0},cY.prototype.preSolve=function(a){},cY.prototype.postSolve=function(a){},cY.prototype.next=function(a){return this.a===a?this.next_a:this.next_b};var cZ=a.PinJoint=function(a,b,d,e){cY.call(this,a,b),this.anchr1=d,this.anchr2=e;var f=a?F(a.p,O(d,a.rot)):d,g=b?F(b.p,O(e,b.rot)):e;this.dist=D(G(g,f)),c(this.dist>0,"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<this.min?(f=this.min-e,this.n=H(T(d))):(this.n=A,this.jnAcc=0),this.nMass=1/cU(b,c,this.r1,this.r2,this.n);var g=this.maxBias;this.bias=t(-cX(this.errorBias,a)*f/a,-g,g),this.jnMax=this.maxForce*a},c$.prototype.applyCachedImpulse=function(a){var b=this.jnAcc*a;cR(this.a,this.b,this.r1,this.r2,this.n.x*b,this.n.y*b)},c$.prototype.applyImpulse=function(){if(this.n.x===0&&this.n.y===0)return;var a=this.a,b=this.b,c=this.n,d=this.r1,e=this.r2,f=cO(a,b,d,e),g=B(f,c),h=(this.bias-g)*this.nMass,i=this.jnAcc;this.jnAcc=t(i+h,-this.jnMax,0),h=this.jnAcc-i,cR(a,b,this.r1,this.r2,c.x*h,c.y*h)},c$.prototype.getImpulse=function(){return Math.abs(this.jnAcc)};var c_=a.PivotJoint=function(a,b,c,d){cY.call(this,a,b);if(typeof d=="undefined"){var e=c;c=a?a.world2Local(e):e,d=b?b.world2Local(e):e}this.anchr1=c,this.anchr2=d,this.r1=this.r2=A,this.k1=new z(0,0),this.k2=new z(0,0),this.jAcc=A,this.jMaxLen=0,this.bias=A};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),cV(b,c,this.r1,this.r2,this.k1,this.k2),this.jMaxLen=this.maxForce*a;var d=G(F(c.p,this.r2),F(b.p,this.r1));this.bias=U(I(d,-cX(this.errorBias,a)/a),this.maxBias)},c_.prototype.applyCachedImpulse=function(a){cR(this.a,this.b,this.r1,this.r2,this.jAcc.x*a,this.jAcc.y*a)},c_.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=U(F(this.jAcc,f),this.jMaxLen),cR(a,b,this.r1,this.r2,this.jAcc.x-g.x,this.jAcc.y-g.y)},c_.prototype.getImpulse=function(){return D(this.jAcc)};var da=a.GrooveJoint=function(a,b,c,d,e){cY.call(this,a,b),this.grv_a=c,this.grv_b=d,this.grv_n=L(S(G(d,c))),this.anchr2=e,this.grv_tn=null,this.clamp=0,this.r1=this.r2=null,this.k1=new z(0,0),this.k2=new z(0,0),this.jAcc=A,this.jMaxLen=0,this.bias=null};da.prototype=Object.create(cY.prototype),da.prototype.preStep=function(a){var b=this.a,c=this.b,d=b.local2World(this.grv_a),e=b.local2World(this.grv_b),f=O(this.grv_n,b.rot),g=B(d,f);this.grv_tn=f,this.r2=O(this.anchr2,c.rot);var h=J(F(c.p,this.r2),f);h<=J(d,f)?(this.clamp=1,this.r1=G(d,b.p)):h>=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:d<this.min&&(e=this.min-d),this.iSum=1/(1/b.i+1/c.i);var f=this.maxBias;this.bias=t(-cX(this.errorBias,a)*e/a,-f,f),this.jMax=this.maxForce*a,this.bias||(this.jAcc=0)},df.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},df.prototype.applyImpulse=function(){if(!this.bias)return;var a=this.a,b=this.b,c=b.w-a.w,d=-(this.bias+c)*this.iSum,e=this.jAcc;this.bias<0?this.jAcc=t(e+d,0,this.jMax):this.jAcc=t(e+d,-this.jMax,0),d=this.jAcc-e,a.w-=d*a.i_inv,b.w+=d*b.i_inv},df.prototype.getImpulse=function(){return Math.abs(joint.jAcc)};var dg=a.RatchetJoint=function(a,b,c,d){cY.call(this,a,b),this.angle=0,this.phase=c,this.ratchet=d,this.angle=(b?b.a:0)-(a?a.a:0),this.iSum=this.bias=this.jAcc=this.jMax=0};dg.prototype=Object.create(cY.prototype),dg.prototype.preStep=function(a){var b=this.a,c=this.b,d=this.angle,e=this.phase,f=this.ratchet,g=c.a-b.a,h=d-g,i=0;h*f>0?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)}})();
View
3  lib/cpSpaceStep.js
@@ -24,7 +24,7 @@
/// Schedule a post-step callback to be called when cpSpaceStep() finishes.
Space.prototype.addPostStepCallback = function(func)
{
- assertWarn(this.locked,
+ assertSoft(this.locked,
"Adding a post-step callback when the space is not locked is unnecessary. " +
"Post-step callbacks will not called until the end of the next call to cpSpaceStep() or the next query.");
@@ -38,6 +38,7 @@ Space.prototype.runPostStepCallbacks = function()
for(var i = 0; i < this.postStepCallbacks.length; i++){
this.postStepCallbacks[i]();
}
+ this.postStepCallbacks = []
@josephg Owner
josephg added a note

;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
};
// **** Locking Functions
Please sign in to comment.
Something went wrong with that request. Please try again.