-
Notifications
You must be signed in to change notification settings - Fork 1
/
cp.min.js
28 lines (27 loc) · 60.1 KB
/
cp.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*
* Copyright (c) 2007-2013 Scott Lembcke and Howling Moon Software
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/*
* Chipmunk JS 6.2.0 - Port of Chipmunk Physics
* @author lytc
*/
!function(a,b){"use strict";b.cp=a;var c=6,d=2,e=0,f=a;"undefined"!=typeof module&&(module.exports=f);var g=function(){},h=function(a,b){for(var c in b)b.hasOwnProperty(c)&&(a[c]=b[c]);return a},i=function(a,b,c){var d=function(){this.constructor=b};return d.prototype=a.prototype,b.prototype=new d,c&&h(b.prototype,c),b.__super__=a.prototype,b},j=function(a,b){if(!a)throw console.trace(),new Error(b)},k=function(a,b){if(!a)throw console.trace(),new Error(b)},l=f.CP_HASH_PAIR=function(a,b){return b>a?a+" "+b:b+" "+a};f.versionString=c+"."+d+"."+e,f.momentForCircle=function(a,b,c,d){return a*(.5*(b*b+c*c)+$(d))},f.areaForCircle=function(a,b){return D*H(a*a-b*b)};var m=f.momentForSegment=function(a,b,c){var d=T(Q(b,c),.5);return a*(eb(c,b)/12+$(d))};f.areaForSegment=function(a,b,c){return c*(D*c+2*db(a,b))},f.momentForPoly=function(a,b,c){var d=b.length;if(2==d)return m(a,b[0],b[1]);for(var e=0,f=0,g=0;d>g;g++){var h=Q(b[g],c),i=Q(b[(g+1)%d],c),j=V(i,h),k=U(h,h)+U(h,i)+U(i,i);e+=j*k,f+=j}return a*e/(6*f)},f.areaForPoly=function(a){for(var b=0,c=a.length,d=0;c>d;d++)b+=V(a[d],a[(d+1)%c]);return-b/2};var n=f.centroidForPoly=function(a){for(var b=0,c=O,d=a.length,e=0;d>e;e++){var f=a[e],g=a[(e+1)%d],h=V(f,g);b+=h,c=Q(c,T(Q(f,g),h))}return T(c,1/(3*b))};f.recenterPoly=function(a){for(var b=n(a),c=a.length,d=0;c>d;d++)a[d]=R(a[d],b)};var o=f.momentForBox=function(a,b,c){return a*(b*b+c*c)/12};f.momentForBox2=function(a,b){var c=b.r-b.l,d=b.t-b.b,e=T(new N(b.l+b.r,b.b+b.t),.5);return o(a,c,d)+a*$(e)};var p=f.loopIndexes=function(a,b){for(var c=0,d=0,e=a[0],f=e,g=1;b>g;g++){var h=a[g];h.x<e.x||h.x==e.x&&h.y<e.y?(e=h,c=g):(h.x>f.x||h.x==f.x&&h.y>f.y)&&(f=h,d=g)}return[c,d]},q=function(a,b,c){var d=a[c];a[c]=a[b],a[b]=d},r=function(a,b,c,d,e,f){if(0==c)return 0;for(var g=0,h=b,i=R(e,d),j=f*_(i),k=b,l=b+c-1;l>=k;){var m=V(i,R(a[k],d));m>j?(m>g&&(g=m,h=k),k++):(q(a,k,l),l--)}return h!=b&&q(a,b,h),k-b},s=function(a,b,c,d,e,f,g,h){if(0>d)return 0;if(0==d)return b[h]=f,1;var i=r(b,c,d,e,f,a),j=s(a,b,c+1,i-1,e,b[c],f,h);b[h+j++]=f;var k=r(b,c+i,d-i,f,g,a);return j+s(a,b,c+i+1,k-1,f,b[c+i],g,h+j)};f.convexHull=function(a,b,c,d,e){if(c)for(var f=0;f<b.length;f++)c[f]=b[f];else c=b;var g=p(b,a),h=g[0],i=g[1];if(h==i)return c;q(c,0,h),q(c,1,0==i?h:i);var j=c[0],k=c[1],l=s(e,c,2,a-2,j,k,j,1)+1;return c.length=l,c};var t=f.fsqrt=Math.sqrt,u=f.fsin=Math.sin,v=f.fcos=Math.cos,w=f.facos=Math.acos,x=f.fatan2=Math.atan2,y=f.fmod=function(a,b){return a%b},z=f.fexp=Math.exp,A=f.fpow=Math.pow,B=f.ffloor=Math.floor,C=2.225074e-308,D=Math.PI,E=b.navigator&&-1!=b.navigator.userAgent.indexOf("Firefox"),F=f.fmax=E?Math.max:function(a,b){return a>b?a:b},G=f.fmin=E?Math.min:function(a,b){return b>a?a:b},H=f.fabs=function(a){return 0>a?-a:a},I=f.fclamp=function(a,b,c){return G(F(a,b),c)},J=function(a){return F(0,G(a,1))};f.flerp=function(a,b,c){return a*(1-c)+b*c},f.flerpconst=function(a,b,c){return a+I(b-a,-c,c)};var K=f.NO_GROUP=0,L=f.ALL_LAYERS=-1,M=function(a,b,c,d){this.a=a,this.b=b,this.c=c,this.d=d},N=f.Vect=function(a,b){this.x=a,this.y=b};f.v=function(a,b){return{x:a,y:b}};var O=f.vzero=new N(0,0),P=f.v.eql=function(a,b){return a.x==b.x&&a.y==b.y},Q=f.v.add=function(a,b){return new N(a.x+b.x,a.y+b.y)},R=f.v.sub=function(a,b){return new N(a.x-b.x,a.y-b.y)},S=f.v.neg=function(a){return new N(-a.x,-a.y)},T=f.v.mult=function(a,b){return new N(a.x*b,a.y*b)},U=f.v.dot=function(a,b){return a.x*b.x+a.y*b.y},V=f.v.cross=function(a,b){return a.x*b.y-a.y*b.x},W=f.v.perp=function(a){return new N(-a.y,a.x)};f.v.rperp=function(a){return new N(a.y,-a.x)};var X=f.v.project=function(a,b){var c=(a.x*b.x+a.y*b.y)/(b.x*b.x+b.y*b.y);return new N(b.x*c,b.y*c)};f.v.forangle=function(a){return new N(v(a),u(a))},f.v.toangle=function(a){return x(a.y,a.x)};var Y=f.v.rotate=function(a,b){return new N(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x)},Z=f.v.unrotate=function(a,b){return new N(a.x*b.x+a.y*b.y,a.y*b.x-a.x*b.y)},$=f.v.lengthsq=function(a){return a.x*a.x+a.y*a.y},_=f.v.len=function(a){return t(a.x*a.x+a.y*a.y)},ab=f.v.lerp=function(a,b,c){var d=1-c;return new N(a.x*d+b.x*c,a.y*d+b.y*c)},bb=f.v.normalize=function(a){var b=t(a.x*a.x+a.y*a.y)+C;return new N(a.x/b,a.y/b)};f.v.normalize_safe=function(a){return bb(a)};var cb=f.v.clamp=function(a,b){var c=a.x*a.x+a.y*a.y;if(c>b*b){var d=t(c)+C;return new N(a.x*b/d,a.y*b/d)}return a};f.v.lerpconst=function(a,b,c){return Q(a,cb(R(b,a),c))};var db=f.v.dist=function(a,b){var c=a.x-b.x,d=a.y-b.y;return t(c*c+d*d)},eb=f.v.distsq=function(a,b){var c=a.x-b.x,d=a.y-b.y;return c*c+d*d};f.v.near=function(a,b,c){return eb(a,b)<c*c},M.prototype.transform=function(a){var b=this;return new N(a.x*b.a+a.y*b.b,a.x*b.c+a.y*b.d)};var fb=f.v.slerp=function(a,b,c){var d=U(bb(a),bb(b)),e=w(I(d,-1,1));if(.001>e)return ab(a,b,c);var f=1/u(e);return Q(T(a,u((1-c)*e)*f),T(b,u(c*e)*f))};f.v.slerpconst=function(a,b,c){var d=U(bb(a),bb(b)),e=w(I(d,-1,1));return fb(a,b,G(c,e)/e)},f.v.str=function(a){return"("+a.x+", "+a.y+")"};var gb=f.Constraint=function(a,b){var c=this;c.a=a,c.b=b,c.space=null,c.next_a=null,c.next_b=null,c.maxForce=1/0,c.errorBias=A(.9,60),c.maxBias=1/0};gb.prototype.preSolve=g,gb.prototype.postSolve=g,gb.prototype.applyCachedImpulse=g;var hb=function(a,b){return(b-a.restAngle)*a.stiffness},ib=f.DampedRotarySpring=function(a,b,c,d,e){gb.apply(this,arguments);var f=this;f.restAngle=c,f.stiffness=d,f.damping=e,f.springTorqueFunc=hb,f.jAcc=0};i(gb,ib),ib.prototype.preStep=function(a){var b=this,c=b.a,d=b.b,e=c.i_inv+d.i_inv;b.iSum=1/e,b.w_coef=1-z(-b.damping*a*e),b.target_wrn=0;var f=b.springTorqueFunc(b,c.a-d.a)*a;b.jAcc=f,c.w-=f*c.i_inv,d.w+=f*d.i_inv},ib.prototype.applyImpulse=function(){var a=this,b=a.a,c=a.b,d=b.w-c.w,e=(a.target_wrn-d)*a.w_coef;a.target_wrn=d+e;var f=e*a.iSum;a.jAcc+=f,b.w+=f*b.i_inv,c.w-=f*c.i_inv},ib.prototype.getImpulse=function(){return this.jAcc};var jb=function(a,b){return(a.restLength-b)*a.stiffness},kb=f.DampedSpring=function(a,b,c,d,e,f,g){gb.apply(this,arguments);var h=this;h.anchr1=c,h.anchr2=d,h.restLength=e,h.stiffness=f,h.damping=g,h.springForceFunc=jb,h.jAcc=0};i(gb,kb),kb.prototype.preStep=function(a){var b=this,c=b.a,d=b.b;b.r1=Y(b.anchr1,c.rot),b.r2=Y(b.anchr2,d.rot);var e=R(Q(d.p,b.r2),Q(c.p,b.r1)),f=_(e);b.n=T(e,1/(f?f:1/0));var g=Nd(c,d,b.r1,b.r2,b.n);b.nMass=1/g,b.target_vrn=0,b.v_coef=1-z(-b.damping*a*g);var h=b.springForceFunc(b,f),i=b.jAcc=h*a;Ld(c,d,b.r1,b.r2,T(b.n,i))},kb.prototype.applyImpulse=function(){var a=this,b=a.a,c=a.b,d=a.n,e=a.r1,f=a.r2,g=Jd(b,c,e,f,d),h=(a.target_vrn-g)*a.v_coef;a.target_vrn=g+h;var i=h*a.nMass;a.jAcc+=i,Ld(b,c,a.r1,a.r2,T(a.n,i))},kb.prototype.getImpulse=function(){return this.jAcc};var lb=f.GearJoint=function(a,b,c,d){gb.apply(this,arguments);var e=this;e.phase=c,e.ratio=d,e.ratio_inv=1/d,e.jAcc=0};i(gb,lb),lb.prototype.preStep=function(a){var b=this,c=b.a,d=b.b;b.iSum=1/(c.i_inv*b.ratio_inv+b.ratio*d.i_inv);var e=b.maxBias;b.bias=I(-Pd(b.errorBias,a)*(d.a*b.ratio-c.a-b.phase)/a,-e,e)},lb.prototype.applyCachedImpulse=function(a){var b=this,c=b.a,d=b.b,e=b.jAcc*a;c.w-=e*c.i_inv*b.ratio_inv,d.w+=e*d.i_inv},lb.prototype.applyImpulse=function(a){var b=this,c=b.a,d=b.b,e=d.w*b.ratio-c.w,f=b.maxForce*a,g=(b.bias-e)*b.iSum,h=b.jAcc;b.jAcc=I(h+g,-f,f),g=b.jAcc-h,c.w-=g*c.i_inv*b.ratio_inv,d.w+=g*d.i_inv},lb.prototype.getImpulse=function(){var a=this;return H(a.jAcc)},lb.prototype.setRatio=function(a){var b=this;b.ratio=a,b.ratio_inv=1/a,b.activateBodies()};var mb=f.GrooveJoint=function(a,b,c,d,e){gb.apply(this,arguments);var f=this;f.grv_a=c,f.grv_b=d,f.grv_n=W(bb(R(d,c))),f.anchr2=e,f.jAcc=O};i(gb,mb),mb.prototype.preStep=function(a){var b=this,c=b.a,d=b.b,e=c.local2World(b.grv_a),f=c.local2World(b.grv_b),g=Y(b.grv_n,c.rot),h=U(e,g);b.grv_tn=g,b.r2=Y(b.anchr2,d.rot);var i=V(Q(d.p,b.r2),g);i<=V(e,g)?(b.clamp=1,b.r1=R(e,c.p)):i>=V(f,g)?(b.clamp=-1,b.r1=R(f,c.p)):(b.clamp=0,b.r1=R(Q(T(W(g),-i),T(g,h)),c.p)),b.k=Od(c,d,b.r1,b.r2);var j=R(Q(d.p,b.r2),Q(c.p,b.r1));b.bias=cb(T(j,-Pd(b.errorBias,a)/a),b.maxBias)},mb.prototype.applyCachedImpulse=function(a){var b=this,c=b.a,d=b.b;Ld(c,d,b.r1,b.r2,T(b.jAcc,a))},mb.prototype.grooveConstrain=function(a,b){var c=this,d=c.grv_tn,e=c.clamp*V(a,d)>0?a:X(a,d);return cb(e,c.maxForce*b)},mb.prototype.applyImpulse=function(a){var b=this,c=b.a,d=b.b,e=b.r1,f=b.r2,g=Id(c,d,e,f),h=b.k.transform(R(b.bias,g)),i=b.jAcc;b.jAcc=b.grooveConstrain(Q(i,h),a),h=R(b.jAcc,i),Ld(c,d,b.r1,b.r2,h)},mb.prototype.getImpulse=function(){return _(this.jAcc)},mb.prototype.setGrooveA=function(a){var b=this;b.grv_a=a,b.grv_n=W(bb(R(b.grv_b,a))),b.activateBodies()},mb.prototype.setGrooveB=function(a){var b=this;b.grv_b=a,b.grv_n=W(bb(R(a,b.grv_a))),b.activateBodies()};var nb=f.PinJoint=function(a,b,c,d){gb.apply(this,arguments);var e=this;e.anchr1=c,e.anchr2=d;var f=a?Q(a.p,Y(c,a.rot)):c,g=b?Q(b.p,Y(d,b.rot)):d;e.dist=_(R(g,f)),e.jnAcc=0};i(gb,nb),nb.prototype.preStep=function(a){var b=this,c=b.a,d=b.b;b.r1=Y(b.anchr1,c.rot),b.r2=Y(b.anchr2,d.rot);var e=R(Q(d.p,b.r2),Q(c.p,b.r1)),f=_(e);b.n=T(e,1/(f?f:1/0)),b.nMass=1/Nd(c,d,b.r1,b.r2,b.n);var g=b.maxBias;b.bias=I(-Pd(b.errorBias,a)*(f-b.dist)/a,-g,g)},nb.prototype.applyCachedImpulse=function(a){var b=this,c=b.a,d=b.b,e=T(b.n,b.jnAcc*a);Ld(c,d,b.r1,b.r2,e)},nb.prototype.applyImpulse=function(a){var b=this,c=b.a,d=b.b,e=b.n,f=Jd(c,d,b.r1,b.r2,e),g=b.maxForce*a,h=(b.bias-f)*b.nMass,i=b.jnAcc;b.jnAcc=I(i+h,-g,g),h=b.jnAcc-i,Ld(c,d,b.r1,b.r2,T(e,h))},nb.prototype.getImpulse=function(){return H(this.jnAcc)};var ob=f.PivotJoint=function(a,b,c,d){gb.apply(this,arguments);var e=this;if(!d){var f=c;c=a?a.world2Local(f):f,d=b?b.world2Local(f):f}e.anchr1=c,e.anchr2=d,e.jAcc=O};i(gb,ob),ob.prototype.preStep=function(a){var b=this,c=b.a,d=b.b;b.r1=Y(b.anchr1,c.rot),b.r2=Y(b.anchr2,d.rot),b.k=Od(c,d,b.r1,b.r2);var e=R(Q(d.p,b.r2),Q(c.p,b.r1));b.bias=cb(T(e,-Pd(b.errorBias,a)/a),b.maxBias)},ob.prototype.applyCachedImpulse=function(a){var b=this,c=b.a,d=b.b;Ld(c,d,b.r1,b.r2,T(b.jAcc,a))},ob.prototype.applyImpulse=function(a){var b=this,c=b.a,d=b.b,e=b.r1,f=b.r2,g=Id(c,d,e,f),h=b.k.transform(R(b.bias,g)),i=b.jAcc;b.jAcc=cb(Q(b.jAcc,h),b.maxForce*a),h=R(b.jAcc,i),Ld(c,d,b.r1,b.r2,h)},ob.prototype.getImpulse=function(){return _(this.jAcc)};var pb=f.RatchetJoint=function(a,b,c,d){gb.apply(this,arguments);var e=this;e.angle=0,e.phase=c,e.ratchet=d,e.angle=(b?b.a:0)-(a?a.a:0)};i(gb,pb),pb.prototype.preStep=function(a){var b=this,c=b.a,d=b.b,e=b.angle,f=b.phase,g=b.ratchet,h=d.a-c.a,i=e-h,j=0;i*g>0?j=i:b.angle=B((h-f)/g)*g+f,b.iSum=1/(c.i_inv+d.i_inv);var k=b.maxBias;b.bias=I(-Pd(b.errorBias,a)*j/a,-k,k),b.bias||(b.jAcc=0)},pb.prototype.applyCachedImpulse=function(a){var b=this,c=b.a,d=b.b,e=b.jAcc*a;c.w-=e*c.i_inv,d.w+=e*d.i_inv},pb.prototype.applyImpulse=function(a){var b=this;if(b.bias){var c=b.a,d=b.b,e=d.w-c.w,f=b.ratchet,g=b.maxForce*a,h=-(b.bias+e)*b.iSum,i=b.jAcc;b.jAcc=I((i+h)*f,0,g*H(f))/f,h=b.jAcc-i,c.w-=h*c.i_inv,d.w+=h*d.i_inv}},pb.prototype.getImpulse=function(){return H(this.jAcc)};var qb=f.RotaryLimitJoint=function(a,b,c,d){gb.apply(this,arguments);var e=this;return e.min=c,e.max=d,e};i(gb,qb),qb.prototype.preStep=function(a){var b=this,c=b.a,d=b.b,e=d.a-c.a,f=0;e>b.max?f=b.max-e:e<b.min&&(f=b.min-e),b.iSum=1/(1/c.i+1/d.i);var g=b.maxBias;b.bias=I(-Pd(b.errorBias,a)*f/a,-g,g),b.bias||(b.jAcc=0)},qb.prototype.applyCachedImpulse=function(a){var b=this,c=b.a,d=b.b,e=b.jAcc*a;c.w-=e*c.i_inv,d.w+=e*d.i_inv},qb.prototype.applyImpulse=function(a){var b=this;if(b.bias){var c=b.a,d=b.b,e=d.w-c.w,f=b.maxForce*a,g=-(b.bias+e)*b.iSum,h=b.jAcc;b.jAcc=b.bias<0?I(h+g,0,f):I(h+g,-f,0),g=b.jAcc-h,c.w-=g*c.i_inv,d.w+=g*d.i_inv}},qb.prototype.getImpulse=function(){return H(this.jAcc)};var rb=f.SimpleMotor=function(a,b,c){gb.apply(this,arguments);var d=this;d.rate=c,d.jAcc=0};i(gb,rb),rb.prototype.preStep=function(){var a=this,b=a.a,c=a.b;a.iSum=1/(b.i_inv+c.i_inv)},rb.prototype.applyCachedImpulse=function(a){var b=this,c=b.a,d=b.b,e=b.jAcc*a;c.w-=e*c.i_inv,d.w+=e*d.i_inv},rb.prototype.applyImpulse=function(a){var b=this,c=b.a,d=b.b,e=d.w-c.w+b.rate,f=b.maxForce*a,g=-e*b.iSum,h=b.jAcc;b.jAcc=I(h+g,-f,f),g=b.jAcc-h,c.w-=g*c.i_inv,d.w+=g*d.i_inv},rb.prototype.getImpulse=function(){return H(this.jAcc)};var sb=f.SlideJoint=function(a,b,c,d,e,f){gb.apply(this,arguments);var g=this;g.anchr1=c,g.anchr2=d,g.min=e,g.max=f,g.jnAcc=0};i(gb,sb),sb.prototype.preStep=function(a){var b=this,c=b.a,d=b.b;b.r1=Y(b.anchr1,c.rot),b.r2=Y(b.anchr2,d.rot);var e=R(Q(d.p,b.r2),Q(c.p,b.r1)),f=_(e),g=0;f>b.max?(g=f-b.max,b.n=bb(e)):f<b.min?(g=b.min-f,b.n=S(bb(e))):(b.n=O,b.jnAcc=0),b.nMass=1/Nd(c,d,b.r1,b.r2,b.n);var h=b.maxBias;b.bias=I(-Pd(b.errorBias,a)*g/a,-h,h)},sb.prototype.applyCachedImpulse=function(a){var b=this,c=b.a,d=b.b,e=T(b.n,b.jnAcc*a);Ld(c,d,b.r1,b.r2,e)},sb.prototype.applyImpulse=function(a){var b=this;if(!P(b.n,O)){var c=b.a,d=b.b,e=b.n,f=b.r1,g=b.r2,h=Id(c,d,f,g),i=U(h,e),j=(b.bias-i)*b.nMass,k=b.jnAcc;b.jnAcc=I(k+j,-b.maxForce*a,0),j=b.jnAcc-k,Ld(c,d,b.r1,b.r2,T(e,j))}},sb.prototype.getImpulse=function(){return H(this.jnAcc)};var tb=0,ub=1,vb=2,wb=3,xb=function(a,b,c,d){var e=this;e.p=a,e.n=b,e.dist=c,e.hash=d,e.r1=new N(0,0),e.r2=new N(0,0)};xb.prototype.jnAcc=0,xb.prototype.jtAcc=0;var yb=function(a,b){var c=a.threadForBody(b),d=c.prev,e=c.next;d?d.threadForBody(b).next=e:b.arbiterList==a&&(b.arbiterList=e),e&&(e.threadForBody(b).prev=d),c.prev=null,c.next=null},zb=function(a,b){this.next=a,this.prev=b},Ab=f.Arbiter=function(a,b){var c=this;c.surface_vr=new N(0,0),c.a=a,c.body_a=a.body,c.b=b,c.body_b=b.body,c.thread_a=new zb(null,null),c.thread_b=new zb(null,null)};Ab.prototype.handler=null,Ab.prototype.contacts=null,Ab.prototype.swappedColl=null,Ab.prototype.e=0,Ab.prototype.u=0,Ab.prototype.stamp=0,Ab.prototype.state=tb,Ab.prototype.data=null,Ab.prototype.reset=function(a,b){var c=this;c.handler=null,c.swappedColl=!1,c.e=0,c.u=0,c.surface_vr.x=c.surface_vr.y=0,c.contacts=null,c.a=a,c.body_a=a.body,c.b=b,c.body_b=b.body,c.thread_a.next=null,c.thread_a.prev=null,c.thread_b.next=null,c.thread_b.prev=null,c.stamp=0,c.state=tb,c.data=null},Ab.prototype.unthread=function(){var a=this;yb(a,a.body_a),yb(a,a.body_b)},Ab.prototype.isFirstContact=function(){return this.state==tb},Ab.prototype.getCount=function(){return this.state!=wb?this.contacts.length:0},Ab.prototype.getNormal=function(a){var b=this;j(a>=0&&a<b.getCount(),"Index error: The specified contact index is invalid for this arbiter");var c=b.contacts[a].n;return b.swappedColl?S(c):c},Ab.prototype.getPoint=function(a){return j(a>=0&&a<this.getCount(),"Index error: The specified contact index is invalid for this arbiter"),this.contacts[a].p},Ab.prototype.getDepth=function(a){return j(a>=0&&a<this.getCount(),"Index error: The specified contact index is invalid for this arbiter"),this.contacts[a].dist},Ab.prototype.getContactPointSet=function(){var a=this,b=new Db;b.count=a.getCount();for(var c,d=0;d<b.count;d++)c=a.contacts[d],b.points[d]=new Cb(c.p,c.n,c.dist);return b},Ab.prototype.setContactPointSet=function(a){var b=this,c=a.count;j(c==b.contacts.length,"The number of contact points cannot be changed.");for(var d=0;c>d;d++)b.contacts[d].p=a.points[d].point,b.contacts[d].n=a.points[d].normal,b.contacts[d].dist=a.points[d].dist},Ab.prototype.totalImpulse=function(){for(var a=this,b=a.contacts,c=O,d=0,e=a.getCount();e>d;d++){var f=b[d];c=Q(c,T(f.n,f.jnAcc))}return a.swappedColl?c:S(c)},Ab.prototype.totalImpulseWithFriction=function(){for(var a=this,b=a.contacts,c=O,d=0,e=a.getCount();e>d;d++){var f=b[d];c=Q(c,Y(f.n,new N(f.jnAcc,f.jtAcc)))}return a.swappedColl?c:S(c)},Ab.prototype.totalKE=function(){for(var a=this,b=(1-a.e)/(1+a.e),c=0,d=a.contacts,e=0,f=a.getCount();f>e;e++){var g=d[e],h=g.jnAcc,i=g.jtAcc;c+=b*h*h/g.nMass+i*i/g.tMass}return c},Ab.prototype.ignore=function(){this.state=vb},Ab.prototype.getSurfaceVelocity=function(){return T(this.surface_vr,this.swappedColl?-1:1)},Ab.prototype.setSurfaceVelocity=function(a){this.surface_vr=T(a,this.swappedColl?-1:1)},Ab.prototype.update=function(a,b,c,d){var e=this,f=a.length;if(e.contacts)for(var g=0;f>g;g++)for(var h=a[g],i=0,j=e.contacts.length;j>i;i++){var k=e.contacts[i];h.hash==k.hash&&(h.jnAcc=k.jnAcc,h.jtAcc=k.jtAcc)}e.contacts=a,e.handler=b,e.swappedColl=c.collision_type!=b.a,e.e=c.e*d.e,e.u=c.u*d.u;var l,m;f?(l=a[0].n.x,m=a[0].n.y):l=m=0;var n=c.surface_v.x-d.surface_v.x,o=c.surface_v.y-d.surface_v.y,p=n*l+o*m;e.surface_vr.x=n-l*p,e.surface_vr.y=o-m*p,e.a=c,e.body_a=c.body,e.b=d,e.body_b=d.body,e.state==wb&&(e.state=tb)},Ab.prototype.preStep=function(a,b,c){for(var d=this,e=d.body_a,f=d.body_b,g=0;g<d.contacts.length;g++){var h=d.contacts[g],i=h.r1,j=h.r2,k=h.n,l=h.p;i.x=l.x-e.p.x,i.y=l.y-e.p.y,j.x=l.x-f.p.x,j.y=l.y-f.p.y,h.nMass=1/Nd(e,f,i,j,k),h.tMass=1/Nd(e,f,i,j,W(k)),h.bias=-c*G(0,h.dist+b)/a,h.jBias=0,h.bounce=Jd(e,f,i,j,k)*d.e}},Ab.prototype.applyCachedImpulse=function(a){var b=this;if(!b.isFirstContact())for(var c=b.body_a,d=b.body_b,e=0;e<b.contacts.length;e++){var f=b.contacts[e],g=f.n.x*f.jnAcc-f.n.y*f.jtAcc,h=f.n.x*f.jtAcc+f.n.y*f.jnAcc;Ld(c,d,f.r1,f.r2,new N(g*a,h*a))}},Ab.prototype.applyImpulse=function(){for(var a=this,b=a.body_a,c=a.body_b,d=a.surface_vr,e=a.u,f=0,g=a.contacts.length;g>f;f++){var h=a.contacts[f],i=h.nMass,j=h.n,k=h.r1,l=h.r2,m=b.w_bias,n=c.w_bias,o=b.v_bias,p=c.v_bias,q=o.x,r=o.y,s=p.x,t=p.y,u=b.v,v=c.v,w=u.x,x=u.y,y=v.x,z=v.y,A=b.w,B=c.w,C=k.x,D=k.y,E=l.x,G=l.y,H=j.x,J=j.y,K=(s-G*n-q+D*m)*H+(t+E*n-r-C*m)*J,L=y-G*B-w+D*A+d.x,M=z+E*B-x-C*A+d.y,N=L*H+M*J,O=-L*J+M*H,P=(h.bias-K)*i,Q=h.jBias;h.jBias=F(Q+P,0);var R=-(h.bounce+N)*i,S=h.jnAcc;h.jnAcc=F(S+R,0);var T=e*h.jnAcc,U=-O*h.tMass,V=h.jtAcc;h.jtAcc=I(V+U,-T,T);var W=h.jBias-Q,X=H*W,Y=J*W,Z=b.m_inv,$=b.i_inv;o.x=q-X*Z,o.y=r-Y*Z,b.w_bias+=$*(-C*Y+D*X);var _=c.m_inv,ab=c.i_inv;p.x=s+X*_,p.y=t+Y*_,c.w_bias+=ab*(E*Y-G*X);var bb=h.jnAcc-S,cb=h.jtAcc-V;X=H*bb-J*cb,Y=H*cb+J*bb,u.x=w-X*Z,u.y=x-Y*Z,b.w+=$*(-C*Y+D*X),v.x=y+X*_,v.y=z+Y*_,c.w+=ab*(E*Y-G*X)}};var Bb=function(a,b,c,d,e,f,g){this.a=a,this.b=b,this.begin=c,this.preSolve=d,this.postSolve=e,this.separate=f,this.data=g};Ab.prototype.getShapes=function(){return this.swappedColl?[this.b,this.a]:[this.a,this.b]},Ab.prototype.getBodies=function(){var a=this.getShapes();return[a[0].body,a[1].body]};var Cb=function(a,b,c){this.point=a,this.normal=b,this.dist=c},Db=function(){this.count=0,this.points=[]},Eb=function(a,b){var c=a.indexOf(b);-1!=c&&(a[c]=a[a.length-1],a.pop())},Fb=f.BB=function(a,b,c,d){this.l=a,this.b=b,this.r=c,this.t=d},Gb=Fb.newForCircle=function(a,b){return new Fb(a.x-b,a.y-b,a.x+b,a.y+b)};Fb.prototype.intersects=function(a){var b=this;return!(a.l>b.r||a.r<b.l||a.t<b.b||a.b>b.t)},Fb.prototype.containsBB=function(a){var b=this;return!(b.l>a.l||b.r<a.r||b.b>a.b||b.t<a.t)},Fb.prototype.containsVect=function(a){var b=this;return b.l<=a.x&&b.r>=a.x&&b.b<=a.y&&b.t>=a.y},Fb.prototype.merge=function(a){var b=this;return new Fb(G(b.l,a.l),G(b.b,a.b),F(b.r,a.r),F(b.t,a.t))},Fb.prototype.expand=function(a){var b=this;return new Fb(G(b.l,a.x),G(b.b,a.y),F(b.r,a.x),F(b.t,a.y))},Fb.prototype.center=function(){var a=this;return ab(new N(a.l,a.b),new N(a.r,a.t),.5)},Fb.prototype.area=function(){var a=this;return(a.r-a.l)*(a.t-a.b)},Fb.prototype.mergedArea=function(a){var b=this;return(F(b.r,a.r)-G(b.l,a.l))*(F(b.t,a.t)-G(b.b,a.b))},Fb.prototype.segmentQuery=function(a,b){var c=this,d=1/(b.x-a.x),e=c.l==a.x?-1/0:(c.l-a.x)*d,f=c.r==a.x?1/0:(c.r-a.x)*d,g=G(e,f),h=F(e,f),i=1/(b.y-a.y),j=c.b==a.y?-1/0:(c.b-a.y)*i,k=c.t==a.y?1/0:(c.t-a.y)*i,l=G(j,k),m=F(j,k);if(h>=l&&m>=g){var n=F(g,l),o=G(h,m);if(o>=0&&1>=n)return F(n,0)}return 1/0},Fb.prototype.intersectsSegment=function(a,b){var c=this;return 1/0!=c.segmentQuery(a,b)},Fb.prototype.clampVect=function(a){var b=this;return new N(I(a.x,b.l,b.r),I(a.y,b.b,b.t))},Fb.prototype.wrapVect=function(a){var b=this,c=H(b.r-b.l),d=y(a.x-b.l,c),e=d>0?d:d+c,f=H(b.t-b.b),g=y(a.y-b.b,f),h=g>0?g:g+f;return new N(e+b.l,h+b.b)};var Hb=f.SpatialIndex=function(a,b){var c=this;c.bbfunc=a,c.staticIndex=b,b&&(j(!b.dynamicIndex,"This static index is already associated with a dynamic index."),b.dynamicIndex=c)},Ib=function(a,b,c,d){this.bbfunc=a,this.staticIndex=b,this.queryFunc=c,this.data=d},Jb=function(a,b){b.staticIndex.query(a,b.bbfunc(a),b.queryFunc,b.data)};Hb.prototype.collideStatic=function(a,b,c){var d=this;if(a&&a.count>0){var e=new Ib(d.bbfunc,a,b,c);d.each(Jb,e)}};var Kb=function(a,b){var c=this;c.bb=a.bb.merge(b.bb),c.parent=null,c.setA(a),c.setB(b)},Lb=function(a,b){this.bb=new Fb(0,0,0,0);var c=this;c.obj=b,a.getBB(b,c.bb),c.parent=null,c.STAMP=0,c.PAIRS=null},Mb=function(a,b,c,d,e){this.aPrev=null,this.aLeaf=a,this.aNext=b,this.bPrev=null,this.bLeaf=c,this.bNext=d,this.id=e},Nb=f.BBTree=function(a,b){var c=this;Hb.call(c,a,b),c.velocityFunc=null,c.leaves={},c.count=0,c.root=null,c.pooledNodes=null,c.pooledPairs=null,c.stamp=0};i(Hb,Nb),Nb.prototype.getBB=function(a,b){var c=this,d=c.bbfunc(a),e=c.velocityFunc;if(e){var f=.1,g=(d.r-d.l)*f,h=(d.t-d.b)*f,i=e(a),j=.1*i.x,k=.1*i.y;b.l=d.l+G(-g,j),b.b=d.b+G(-h,k),b.r=d.r+F(g,j),b.t=d.t+F(h,k)}else b.l=d.l,b.b=d.b,b.r=d.r,b.t=d.t},Nb.prototype.getMasterTree=function(){return this.dynamicIndex||this},Nb.prototype.incrementStamp=function(){(this.dynamicIndex||this).stamp++},Mb.prototype.recycle=function(a){var b=this;a=a.getMasterTree(),b.aNext=a.pooledPairs,a.pooledPairs=b},Nb.prototype.pairFromPool=function(a,b,c,d,e){var f=this;f=f.getMasterTree();var g=f.pooledPairs;if(g)return f.pooledPairs=g.aNext,g.constructor(a,b,c,d,e),g;var h=new Mb(a,b,c,d,e);return h};var Ob=function(a,b,c){c&&(c.aLeaf==b?c.aPrev=a:c.bPrev=a),a?a.aLeaf==b?a.aNext=c:a.bNext=c:b.PAIRS=c};Lb.prototype.pairsClear=function(a){var b=this,c=b.PAIRS;for(b.PAIRS=null;c;){if(c.aLeaf==b){var d=c.aNext;Ob(c.bPrev,c.bLeaf,c.bNext)}else{var d=c.bNext;Ob(c.aPrev,c.aLeaf,c.aNext)}c.recycle(a),c=d}};var Pb=function(a,b,c){var d=a.PAIRS,e=b.PAIRS,f=c.pairFromPool(a,d,b,e,0);a.PAIRS=b.PAIRS=f,d&&(d.aLeaf==a?d.aPrev=f:d.bPrev=f),e&&(e.aLeaf==b?e.aPrev=f:e.bPrev=f)};Kb.prototype.recycle=function(a){var b=this;b.parent=a.pooledNodes,a.pooledNodes=b},Nb.prototype.nodeFromPool=function(a,b){var c=this,d=c.pooledNodes;if(d)return c.pooledNodes=d.parent,d.constructor(a,b),d;var e=new Kb(a,b);return e},Kb.prototype.setA=function(a){var b=this;b.A=a,a.parent=b},Kb.prototype.setB=function(a){var b=this;b.B=a,a.parent=b},Kb.prototype.isLeaf=!1,Lb.prototype.isLeaf=!0,Kb.prototype.other=function(a){var b=this;return b.A==a?b.B:b.A},Kb.prototype.replaceChild=function(a,b,c){var d=this;d.A==a?(d.A.recycle(c),d.setA(b)):(d.B.recycle(c),d.setB(b));for(var e=d;e;e=e.parent)e.bb=e.A.bb.merge(e.B.bb)};var Qb=function(a,b){return H(a.l+a.r-b.l-b.r)+H(a.b+a.t-b.b-b.t)},Rb=function(a,b,c){if(null==a)return b;if(a.isLeaf)return c.nodeFromPool(b,a);var d=a.B.bb.area()+a.A.bb.mergedArea(b.bb),e=a.A.bb.area()+a.B.bb.mergedArea(b.bb);return d==e&&(d=Qb(a.A.bb,b.bb),e=Qb(a.B.bb,b.bb)),d>e?a.setB(Rb(a.B,b,c)):a.setA(Rb(a.A,b,c)),a.bb=a.bb.merge(b.bb),a},Sb=function(a,b,c,d,e){a.bb.intersects(c)&&(a.isLeaf?d(b,a.obj,0,e):(Sb(a.A,b,c,d,e),Sb(a.B,b,c,d,e)))},Tb=function(a,b,c,d,e,f,g){if(a.isLeaf)return f(b,a.obj,g);var h=a.A.bb.segmentQuery(c,d),i=a.B.bb.segmentQuery(c,d);return i>h?(e>h&&(e=G(e,Tb(a.A,b,c,d,e,f,g))),e>i&&(e=G(e,Tb(a.B,b,c,d,e,f,g)))):(e>i&&(e=G(e,Tb(a.B,b,c,d,e,f,g))),e>h&&(e=G(e,Tb(a.A,b,c,d,e,f,g)))),e};Nb.prototype.subtreeRecycle=function(a){var b=this;a.isLeaf||(b.subtreeRecycle(a.A),b.subtreeRecycle(a.B),a.recycle(b))};var Ub=function(a,b,c){if(b==a)return null;var d=b.parent;if(d==a){var e=a.other(b);return e.parent=a.parent,a.recycle(c),e}return d.parent.replaceChild(d,d.other(b),c),a};Kb.prototype.markLeafQuery=function(a,b,c,d,e){var f=this;a.bb.intersects(f.bb)&&(f.A.markLeafQuery(a,b,c,d,e),f.B.markLeafQuery(a,b,c,d,e))},Lb.prototype.markLeafQuery=function(a,b,c,d,e){var f=this;a.bb.intersects(f.bb)&&(b?Pb(a,f,c):(f.STAMP<a.STAMP&&Pb(f,a,c),d(a.obj,f.obj,0,e)))},Lb.prototype.markSubtree=function(a,b,c,d){var e=this;if(e.STAMP==a.getMasterTree().stamp){b&&b.markLeafQuery(e,!1,a,c,d);for(var f=e;f.parent;f=f.parent)f==f.parent.A?f.parent.B.markLeafQuery(e,!0,a,c,d):f.parent.A.markLeafQuery(e,!1,a,c,d)}else for(var g=e.PAIRS;g;)e==g.bLeaf?(g.id=c(g.aLeaf.obj,e.obj,g.id,d),g=g.bNext):g=g.aNext},Kb.prototype.markSubtree=function(a,b,c,d){this.A.markSubtree(a,b,c,d),this.B.markSubtree(a,b,c,d)},Lb.prototype.update=function(a){var b=this,c=a.root,d=a.bbfunc(b.obj);return b.bb.containsBB(d)?!1:(a.getBB(b.obj,b.bb),c=Ub(c,b,a),a.root=Rb(c,b,a),b.pairsClear(a),b.STAMP=a.getMasterTree().stamp,!0)};var Vb=function(a,b,c){return c};Lb.prototype.addPairs=function(a){var b=this,c=a.dynamicIndex;if(c){var d=c.root;if(d){var e=c;d.markLeafQuery(b,!0,e,g,null)}}else{var f=a.staticIndex.root;b.markSubtree(a,f,Vb,null)}},Nb.prototype.setVelocityFunc=function(a){this.velocityFunc=a},Nb.prototype.insert=function(a,b){var c=this,d=c.leaves[b]=new Lb(c,a),e=c.root;c.root=Rb(e,d,c),c.count++,d.STAMP=c.getMasterTree().stamp,d.addPairs(c),c.incrementStamp()},Nb.prototype.remove=function(a,b){var c=this,d=c.leaves[b];delete c.leaves[b],c.root=Ub(c.root,d,c),c.count--,d.pairsClear(c)},Nb.prototype.contains=function(a,b){return null!=this.leaves[b]},Nb.prototype.reindexQuery=function(a,b){var c=this;if(c.root){for(var d in c.leaves)c.leaves[d].update(c);var e=c.staticIndex,f=e&&e.root;c.root.markSubtree(c,f,a,b),e&&!f&&c.collideStatic(e,a,b),c.incrementStamp()}},Nb.prototype.reindex=function(){this.reindexQuery(Vb,null)},Nb.prototype.reindexObject=function(a,b){var c=this,d=c.leaves[b];d&&(d.update(c)&&d.addPairs(c),c.incrementStamp())},Nb.prototype.segmentQuery=function(a,b,c,d,e,f){var g=this,h=g.root;h&&Tb(h,a,b,c,d,e,f)},Nb.prototype.query=function(a,b,c,d){this.root&&Sb(this.root,a,b,c,d)},Nb.prototype.each=function(a,b){var c=this;for(var d in c.leaves)a(c.leaves[d].obj,b)};var Wb=0,Xb=f.Body=function(a,b){var c=this;c.hashid=Wb++,c.p=new N(0,0),c.v=new N(0,0),c.f=new N(0,0),c.v_bias=new N(0,0),this.rot=new N(1,0),c.setMass(a),c.setMoment(b)};Xb.prototype={space:null,shapeList:null,arbiterList:null,constraintList:null,nodeRoot:null,nodeNext:null,nodeIdleTime:0,w:0,t:0,w_bias:0,v_limit:1/0,w_limit:1/0,data:null,a:0},Xb.prototype.getPos=function(){return this.p},Xb.prototype.getAngVel=function(){return this.w},Xb.prototype.getVel=function(){return this.v},Xb.prototype.setVel=function(a){this.activate(),this.v=a},Xb.prototype.setAngVel=function(a){this.activate(),this.w=a},Xb.initStatic=function(a){return a.setMass(1/0),a.setMoment(1/0),a.nodeIdleTime=1/0,a},Xb.newStatic=function(){var a=new Xb(1/0,1/0);return a.nodeIdleTime=1/0,a};Xb.prototype.setMass=function(a){var b=this;j(a>0,"Mass must be positive and non-zero."),b.activate(),b.m=a,b.m_inv=1/a},Xb.prototype.setMoment=function(a){var b=this;j(a>0,"Moment of Inertia must be positive and non-zero."),b.activate(),b.i=a,b.i_inv=1/a},Xb.prototype.addShape=function(a){var b=this,c=b.shapeList;c&&(c.prev=a),a.next=c,b.shapeList=a},Xb.prototype.removeShape=function(a){var b=this,c=a.prev,d=a.next;c?c.next=d:b.shapeList=d,d&&(d.prev=c),a.prev=null,a.next=null};var Yb=function(a,b,c){return a==c?a.next(b):(a.a==b?a.next_a=Yb(a.next_a,b,c):a.next_b=Yb(a.next_b,b,c),a)};Xb.prototype.removeConstraint=function(a){var b=this;b.constraintList=Yb(b.constraintList,b,a)},Xb.prototype.setPos=function(a){var b=this;b.activate(),b.p=a};var Zb=function(a,b){k(b==b&&1/0!=H(b),"Body's angle is invalid."),a.a=b,a.rot.x=v(b),a.rot.y=u(b)};Xb.prototype.setAngle=function(a){var b=this;b.activate(),Zb(b,a)},Xb.prototype.updateVelocity=function(a,b,c){var d=this,e=d.v;e.x=e.x*b+(a.x+d.f.x*d.m_inv)*c,e.y=d.v.y*b+(a.y+d.f.y*d.m_inv)*c;var f=d.v_limit;if(1/0>f){var g=e.x*e.x+e.y*e.y;if(g>f*f){var h=t(g)+C;e.x*=f/h,e.y*=f/h}}var i=d.w_limit;d.w=I(d.w*b+d.t*d.i_inv*c,-i,i)},Xb.prototype.updatePosition=function(a){var b=this;b.p.x+=(b.v.x+b.v_bias.x)*a,b.p.y+=(b.v.y+b.v_bias.y)*a,Zb(b,b.a+(b.w+b.w_bias)*a),b.v_bias.x=b.v_bias.y=0,b.w_bias=0},Xb.prototype.resetForces=function(){var a=this;a.activate(),a.f.x=a.f.y=0,a.t=0},Xb.prototype.applyForce=function(a,b){var c=this;c.activate(),c.f.x+=a.x,c.f.y+=a.y,c.t+=V(b,a)},Xb.prototype.applyImpulse=function(a,b){var c=this;c.activate(),Kd(c,a,b)},Xb.prototype.getVelAtPoint=function(a){var b=this;return Q(b.v,T(W(a),b.w))},Xb.prototype.getVelAtWorldPoint=function(a){var b=this;return b.getVelAtPoint(R(a,b.p))},Xb.prototype.getVelAtLocalPoint=function(a){var b=this;return b.getVelAtPoint(Y(a,b.rot))},Xb.prototype.kScalar=function(a,b){return Md(this,a,b)},Xb.prototype.eachShape=function(a,b){for(var c=this,d=c.shapeList;d;){var e=d.next;a(c,d,b),d=e}},Xb.prototype.eachConstraint=function(a,b){for(var c=this,d=c.constraintList;d;){var e=d.next(c);a(c,d,b),d=e}},Xb.prototype.eachArbiter=function(a,b){for(var c=this,d=c.arbiterList;d;){var e=d.next(c);d.swappedColl=c==d.body_b,a(c,d,b),d=e}};var $b=1,_b=30,ac=30,bc=function(a,b,c,d,e){var f=c+d,g=b.x-a.x,h=b.y-a.y,i=g*g+h*h;if(f*f>i){var j=t(i),k=j?new N(g/j,h/j):new N(1,0);return new xb(ab(a,b,c/(c+d)),k,j-f,e)}},cc=function(a,b){for(var c=-1/0,d=0,e=0,f=a.length;f>e;e++){var g=a[e],h=g.x*b.x+g.y*b.y;h>c&&(c=h,d=e)}return d},dc=function(a,b){this.p=a,this.id=b},ec=function(a){return new dc(a.tc,0)},fc=function(a,b){return U(a.ta,b)>U(a.tb,b)?new dc(a.ta,0):new dc(a.tb,1)},gc=function(a,b){var c=a.tVerts,d=cc(c,b);return new dc(c[d],d)},hc=function(a,b){this.a=a.p,this.b=b.p,this.ab=new N(b.p.x-a.p.x,b.p.y-a.p.y),this.id=(255&a.id)<<8|255&b.id},ic=function(a,b,c,d){this.shape1=a,this.shape2=b,this.func1=c,this.func2=d},jc=function(a,b){var c=a.func1(a.shape1,new N(-b.x,-b.y)),d=a.func2(a.shape2,b);return new hc(c,d)},kc=function(a,b){this.p=a,this.hash=b},lc=function(a,b,c,d){this.a=a,this.b=b,this.r=c,this.n=d},mc=function(a,b){var c=a.verts.length,d=cc(a.tVerts,b),e=(d-1+c)%c,f=(d+1)%c,g=a.tVerts,h=a.tPlanes;if(U(b,h[d].n)>U(b,h[f].n)){var i=new lc(new kc(g[e],l(a.hashid,e)),new kc(g[d],l(a.hashid,d)),a.r,h[d].n);return i}var i=new lc(new kc(g[d],l(a.hashid,d)),new kc(g[f],l(a.hashid,f)),a.r,h[f].n);return i},nc=function(a,b){if(U(a.tn,b)>0){var c=new lc(new kc(a.ta,l(a.hashid,0)),new kc(a.tb,l(a.hashid,1)),a.r,a.tn);return c}var c=new lc(new kc(a.tb,l(a.hashid,1)),new kc(a.ta,l(a.hashid,0)),a.r,S(a.tn));return c},oc=function(a,b){var c=b.x-a.x,d=b.y-a.y;return-I((c*(a.x+b.x)+d*(a.y+b.y))/(c*c+d*d),-1,1)},pc=function(a,b,c){var d=.5*c,e=.5-d,f=.5+d;return new N(a.x*e+b.x*f,a.y*e+b.y*f)},qc=function(a,b){var c=oc(a.ab,b.ab),d=pc(a.ab,b.ab,c),e=pc(a.a,b.a,c),f=pc(a.b,b.b,c),g=(65535&a.id)<<16|65535&b.id,h=b.ab.x-a.ab.x,i=-b.ab.y+a.ab.y,j=t(i*i+h*h)+C,k=i/j,l=h/j,m=-(k*d.x+l*d.y);if(0>=m||c>0&&1>c)var n=new N(-k,-l);else{m=t(d.x*d.x+d.y*d.y);var j=m+C,n=new N(d.x/j,d.y/j)}this.a=e,this.b=f,this.n=n,this.d=m,this.id=g},rc=function(a,b){return $(pc(a,b,oc(a,b)))},sc=function(a,b,c,d){for(var e=0,f=1/0,g=0,h=b-1;b>g;h=g,g++){var i=rc(c[h].ab,c[g].ab);f>i&&(f=i,e=h)}var j=c[e],k=c[(e+1)%b],l=j.ab.x,m=j.ab.y,n=k.ab.x,o=k.ab.y,p=jc(a,new N(m-o,n-l)),q=2*(n*(p.ab.y-m)+l*(o-p.ab.y)+p.ab.x*(m-o));if(q>0&&ac>d){var r=1,s=new Array(b+1);s[0]=p;for(var h=0;b>h;h++){var t=(e+1+h)%b,u=s[r-1].ab,v=c[t].ab,w=(b>h+1?c[(t+1)%b]:p).ab;(w.x-u.x)*(v.y-u.y)-(w.y-u.y)*(v.x-u.x)>0&&(s[r]=c[t],r++)}return sc(a,r,s,d+1)}return new qc(j,k)},tc=function(a,b,c,d){var e=[b,c,d];return sc(a,3,e,1)},uc=function(a,b,c,d){if(d>_b)return new qc(b,c);var e=b.ab.x,f=b.ab.y,g=c.ab.x,h=c.ab.y,i=g-e,j=h-f;if(i*(f+h)-j*(e+g)>0)return uc(a,c,b,d+1);var k=oc(b.ab,c.ab),l=k>-1&&1>k?new N(-j,i):S(pc(b.ab,c.ab,k)),m=jc(a,l),n=m.ab.x,o=m.ab.y;if((g-n)*(h+o)-(h-o)*(g+n)>0&&0>(e-n)*(f+o)-(f-o)*(e+n))return tc(a,b,m,c);var p=l.x,q=l.y;return n*p+o*q<=F(e*p+f*q,g*p+h*q)?new qc(b,c):rc(b.ab,m.ab)<rc(m.ab,c.ab)?uc(a,b,m,d+1):uc(a,m,c,d+1)},vc=function(a,b){switch(a.type){case Lc:return new dc(a.tc,0);case Mc:var c=a;return new dc(0==b?c.ta:c.tb,b);case Nc:var d=a,e=b<d.verts.length?b:0;return new dc(d.tVerts[e],e);default:return new dc(O,0)
}},wc=function(a,b){var c,d,e=b.id;if(e&&$b)c=new hc(vc(a.shape1,255&e>>24),vc(a.shape2,255&e>>16)),d=new hc(vc(a.shape1,255&e>>8),vc(a.shape2,255&e));else{var f=a.shape1.bb.center(),g=a.shape2.bb.center(),h=f.x-g.x,i=-f.y+g.y;c=jc(a,new N(i,h)),d=jc(a,new N(-i,-h))}var j=uc(a,c,d,1);return b.id=j.id,j},xc=function(a,b,c,d,e,f,g,h){var i=d+e,j=i>0?d/i:.5,k=ab(b,c,j);h.push(new xb(k,f,a-i,g))},yc=function(a,b,c,d,e,f,g,h,i){var j=b.x,k=b.y,l=c.x,m=c.y,n=f.x,o=f.y,p=a.x,q=a.y,r=j*o-k*n,s=l*o-m*n,t=p*o-q*n,u=1-J((s-t)/(s-r)),v=ab(b,c,u),w=(v.x-p)*n+(v.y-q)*o;if(u>0&&0>=w){var x=d+e,y=x>0?e*(1-(x+w)/x):-.5*w;return i.push(new xb(new N(v.x+n*y,v.y+o*y),g,w,h)),1}return 0},zc=function(a,b,c,d,e){var f=b.r,g=b.n.x*f,h=b.n.y*f,i=a.n,j=a.r,k=i.x*j,m=i.y*j,n=b.a.p,o=new N(n.x+g,n.y+h),p=b.b.p,q=new N(p.x+g,p.y+h),r=a.a.p,s=a.b.p,t=Dd(n,r,s),u=Dd(p,r,s),v=c.n,w=d*c.d,x=v.x*w,y=v.y*w,z=n.x-t.x-x,A=n.y-t.y-y,B=z*z+A*A,C=p.x-u.x-x,D=p.y-u.y-y,E=C*C+D*D,F=l(b.a.hash,a.b.hash),G=l(b.b.hash,a.a.hash);if(E>B){var H=new N(r.x+k,r.y+m);return xc(c.d,t,n,j,f,v,F,e),yc(H,o,q,j,f,i,v,G,e)+1}var H=new N(s.x+k,s.y+m);return xc(c.d,u,p,j,f,v,G,e),yc(H,q,o,j,f,i,v,F,e)+1},Ac=function(a,b,c,d){var e=a.r+b.r;if(c.d<=e){var f=U(a.n,c.n)+U(b.n,c.n);return 0!=f&&f>0||0==f&&eb(a.a.p,a.b.p)>eb(b.a.p,b.b.p)?zc(a,b,c,1,d):zc(b,a,c,-1,d)}return 0},Bc=function(a,b,c,d){var e=bc(a.tc,b.tc,a.r,b.r,0);return e?(d.push(e),1):0},Cc=function(a,b,c,d){var e,f=b.ta,g=b.tb,h=a.tc,i=g.x-f.x,j=g.y-f.y,k=J((i*(h.x-f.x)+j*(h.y-f.y))/(i*i+j*j)),l=new N(f.x+i*k,f.y+j*k);if(e=bc(h,l,a.r,b.r,0)){var m=e.n;if((0!=k||0==b.a_tangent.x&&0==b.a_tangent.y||U(m,Y(b.a_tangent,b.body.rot))>=0)&&(1!=k||0==b.b_tangent.x&&0==b.b_tangent.y||U(m,Y(b.b_tangent,b.body.rot))>=0))return d.push(e),1}return 0},Dc=function(a,b,c,d){var e=new ic(a,b,fc,fc),f=wc(e,c),g=f.n,h=a.body.rot,i=b.body.rot;return f.d<=a.r+b.r&&(!P(f.a,a.ta)||U(g,Y(a.a_tangent,h))<=0)&&(!P(f.a,a.tb)||U(g,Y(a.b_tangent,h))<=0)&&(!P(f.b,b.ta)||U(g,Y(b.a_tangent,i))>=0)&&(!P(f.b,b.tb)||U(g,Y(b.b_tangent,i))>=0)?Ac(nc(a,g),nc(b,S(g)),f,d):0},Ec=function(a,b,c,d){var e=new ic(a,b,gc,gc),f=wc(e,c);return f.d<=0||f.d-a.r-b.r<=0?Ac(mc(a,f.n),mc(b,S(f.n)),f,d):0},Fc=function(a,b,c,d){var e=new ic(a,b,fc,gc),f=wc(e,c),g=f.n,h=a.body.rot;return f.d-a.r-b.r<=0&&(0==a.a_tangent.x&&0==a.a_tangent.y||!P(f.a,a.ta)||U(g,Y(a.a_tangent,h))<=0)&&(0==a.b_tangent.x&&0==a.b_tangent.y||!P(f.a,a.tb)||U(g,Y(a.b_tangent,h))<=0)?Ac(nc(a,g),mc(b,S(g)),f,d):0},Gc=function(a,b,c,d){var e=new ic(a,b,ec,gc),f=wc(e,c),g=a.r+b.r;if(f.d-g<=0){var h=ab(f.a,f.b,a.r/g);return d.push(new xb(h,f.n,f.d-g,0)),1}return 0},Hc=[Bc,null,null,Cc,null,null,Gc,Fc,Ec],Ic=Hc,Jc=[Bc,null,null,Cc,Dc,null,Gc,Fc,Ec];f.enableSegmentToSegmentCollisions=function(){Ic=Jc};var Kc=function(a,b,c,d){var e=Ic[a.type+b.type*Oc],f=e?e(a,b,c,d):0;return f},Lc=f.CIRCLE_SHAPE=0,Mc=f.SEGMENT_SHAPE=1,Nc=f.POLY_SHAPE=2,Oc=3,Pc=function(a,b,c,d){this.shape=a,this.p=b,this.d=c,this.g=d},Qc=function(a,b,c){this.shape=a,this.t=b,this.n=c};Qc.prototype.hitPoint=function(a,b){return ab(a,b,this.t)},Qc.prototype.hitDist=function(a,b){return db(a,b)*this.t};var Rc=0,Sc=f.Shape=function(a){var b=this;b.hashid=Rc,Rc++,b.body=a,b.surface_v=new N(0,0),b.bb=new Fb(0,0,0,0)};Sc.prototype.sensor=0,Sc.prototype.e=0,Sc.prototype.u=0,Sc.prototype.collision_type=0,Sc.prototype.group=K,Sc.prototype.layers=L,Sc.prototype.data=null,Sc.prototype.space=null,Sc.prototype.next=null,Sc.prototype.prev=null,Sc.prototype.setElasticity=function(a){this.e=a},Sc.prototype.setFriction=function(a){this.body.activate(),this.u=a},Sc.prototype.getFriction=function(){return this.u},Sc.prototype.setLayers=function(a){this.body.activate(),this.layers=a},Sc.prototype.setSensor=function(a){this.body.activate(),this.sensor=a},Sc.prototype.setCollisionType=function(a){this.body.activate(),this.collision_type=a},Sc.prototype.getBody=function(){return this.body},Sc.prototype.getBB=function(){return this.bb},Sc.prototype.setRadius=function(a){this.r=a},Sc.prototype.setBody=function(a){var b=this;j(!b.active(),"You cannot change the body on an active shape. You must remove the shape from the space before changing the body."),b.body=a},Sc.prototype.cacheBB=function(){var a=this,b=a.body;return a.update(b.p,b.rot)},Sc.prototype.update=function(a,b){var c=this;return c.cacheData(a,b),c.bb},Sc.prototype.pointQuery=function(a){var b=this,c=b.nearestPointQuery(a);return c&&c.d<0},Sc.prototype.segmentQuery=function(a,b,c){var d=this,e=d.nearestPointQuery(d,a);if(e){if(e.d<=0){var f=bb(R(a,e.p));return new Qc(d,0,f)}return d.segmentQuery(d,a,b,c)}};var Tc=f.CircleShape=function(a,b,c){var d=this;d.c=c,d.r=b,d.tc=new N(0,0),Sc.apply(this,arguments)};i(Sc,Tc),Tc.prototype.type=Lc,Tc.prototype.cacheData=function(a,b){var c=this,d=c.bb,e=c.r,f=c.c.x,g=c.c.y,h=c.tc.x=a.x+f*b.x-g*b.y,i=c.tc.y=a.y+f*b.y+g*b.x;return d.l=h-e,d.b=i-e,d.r=h+e,d.t=i+e,d},Tc.prototype.nearestPointQuery=function(a){var b=this,c=R(a,b.tc),d=_(c),e=b.r,a=Q(b.tc,T(c,e/d)),d=d-e,f=d>Cd?T(c,1/d):new N(0,1);return new Pc(b,a,d,f)},Tc.prototype.segmentQuery=function(a,b){var c=this;return Ed(c,c.tc,c.r,a,b)};var Uc=f.SegmentShape=function(a,b,c,d){var e=this;e.a=b,e.b=c,e.n=W(bb(R(c,b))),e.r=d,e.a_tangent=new N(0,0),e.b_tangent=new N(0,0),Sc.apply(this,arguments)};i(Sc,Uc),Uc.prototype.type=Mc,Uc.prototype.cacheData=function(a,b){var c=this;c.ta=Q(a,Y(c.a,b)),c.tb=Q(a,Y(c.b,b)),c.tn=Y(c.n,b);var d,e,f,g;c.ta.x<c.tb.x?(d=c.ta.x,e=c.tb.x):(d=c.tb.x,e=c.ta.x),c.ta.y<c.tb.y?(f=c.ta.y,g=c.tb.y):(f=c.tb.y,g=c.ta.y);var h=c.r,i=c.bb;return i.l=d-h,i.b=f-h,i.r=e+h,i.t=g+h,i},Uc.prototype.nearestPointQuery=function(a){var b=this,c=Dd(a,b.ta,b.tb),d=R(a,c),e=_(d),f=b.r,g=T(d,1/e),a=e?Q(c,T(g,f)):c,e=e-f,g=e>Cd?g:b.n;return new Pc(b,a,e,g)},Uc.prototype.segmentQuery=function(a,b){var c=this,d=c.tn,e=U(R(c.ta,a),d),f=c.r,g=e>0?S(d):d,h=R(T(g,f),a),i=Q(c.ta,h),j=Q(c.tb,h),k=R(b,a);if(V(k,i)*V(k,j)<=0){var l=e+(e>0?-f:f),m=-l,n=U(k,d)-l;if(0>m*n)return{shape:c,t:m/(m-n),n:g}}else if(0!=f){var o=Ed(c,c.ta,c.r,a,b),p=Ed(c,c.tb,c.r,a,b);return o&&p?o.t<p.t?o:p:o||p}},Uc.prototype.setNeighbors=function(a,b){var c=this;c.a_tangent.x=a.x-c.a.x,c.a_tangent.y=a.y-c.a.y,c.b_tangent.x=b.x-c.b.x,c.b_tangent.y=b.y-c.b.y},Tc.prototype.setOffset=function(a){this.c=a},Uc.prototype.setEndpoints=function(a,b){var c=this;c.a=a,c.b=b,c.n=W(bb(R(b,a)))};var Vc=f.PolyShape=function(a,b,c){Yc.call(this,a,b,c,0)};i(Sc,Vc),Vc.prototype.type=Nc,Vc.prototype.transformVerts=function(a,b){for(var c=this,d=c.verts,e=c.tVerts,f=1/0,g=-1/0,h=1/0,i=-1/0,j=a.x,k=a.y,l=b.x,m=b.y,n=0;n<d.length;n++){var o=j+d[n].x*l-d[n].y*m,p=k+d[n].x*m+d[n].y*l;e[n]=new N(o,p),f=G(f,o),g=F(g,o),h=G(h,p),i=F(i,p)}var q=c.r,r=this.bb;r.l=f-q,r.b=h-q,r.r=g+q,r.t=i+q},Vc.prototype.transformAxes=function(a,b){for(var c=this,d=c.planes,e=c.tPlanes,f=b.x,g=b.y,h=a.x,i=a.y,j=0;j<d.length;j++){var k=d[j].n,l=k.x*f-k.y*g,m=k.x*g+k.y*f;e[j].n.x=l,e[j].n.y=m,e[j].d=h*l+i*m+d[j].d}},Vc.prototype.cacheData=function(a,b){var c=this;return c.transformAxes(a,b),c.transformVerts(a,b)},Vc.prototype.nearestPointQuery=function(a){for(var b=this,c=b.verts.length,d=b.tPlanes,e=b.tVerts,f=b.r,g=e[c-1],h=1/0,i=O,j=O,k=!1,l=0;c>l;l++){d[l].compare(a)>0&&(k=!0);var m=e[l],n=Dd(a,g,m),o=db(a,n);h>o&&(h=o,i=n,j=d[l].n),g=m}var o=k?h:-h,p=T(R(a,i),1/o),a=Q(i,T(p,f)),q=o-f;return p=h>Cd?p:j,new Pc(b,a,q,p)},Vc.prototype.segmentQuery=function(a,b){for(var c,d=this,e=d.tPlanes,f=d.tVerts,g=d.verts.length,h=d.r,i=0;g>i;i++){var j=e[i].n,k=U(a,j),l=e[i].d+h-k;if(!(l>0)){var m=U(b,j),n=l/(m-k);if(!(0>n||n>1)){var o=ab(a,b,n),p=-V(j,o),q=-V(j,f[(i-1+g)%g]),r=-V(j,f[i]);p>=q&&r>=p&&(c=new Qc(d,n,j))}}}if(h>0)for(var i=0;g>i;i++){var s=Ed(d,f[i],h,a,b);s&&(!c||s.t<c.t)&&(c=s)}return c};var Wc=function(a){for(var b=a.length,c=0;b>c;c++){var d=a[c],e=a[(c+1)%b],f=a[(c+2)%b];if(V(R(e,d),R(f,d))>0)return!1}return!0};Vc.prototype.getNumVerts=function(){return this.verts.length},Vc.prototype.getVert=function(a){var b=this;return j(a>=0&&a<b.getNumVerts(),"Index out of range."),b.verts[a]},Vc.prototype.getRadius=function(){return this.r};var Xc=function(a,b,c){j(Wc(b),"Polygon is concave or has a reversed winding. Consider using cp.convexHull()."),a.verts=[],a.planes=[],a.tVerts=[],a.tPlanes=[];for(var d=b.length,e=0;d>e;e++)a.verts[e]=Q(c,b[e]);for(e=0;e<b.length;e++)a.planes[e]=Gd(a.verts[(e-1+d)%d],a.verts[e]),a.tPlanes[e]=new Fd(new N(0,0),0)};Vc.prototype.setVerts=function(a,b){var c=this;Xc(c,a,b)};var Yc=f.PolyShape2=function(a,b,c,d){var e=this;Xc(e,b,c),Sc.call(e,a),e.r=d},Zc=f.BoxShape=function(a,b,c){var d=b/2,e=c/2;$c.call(this,a,new Fb(-d,-e,d,e))},$c=f.BoxShape2=function(a,b){_c.call(this,a,b,0)},_c=f.BoxShape3=function(a,b,c){var d=[new N(b.l,b.b),new N(b.l,b.t),new N(b.r,b.t),new N(b.r,b.b)];Yc.call(this,a,d,O,c)};i(Vc,Yc),i(Vc,Zc),i(Vc,$c),i(Vc,_c);var ad=function(){return 1},bd=function(a){return a.body.v},cd=function(a){return a.bb},dd=new Bb(0,0,ad,ad,g,g,null),ed=f.Space=function(){var a=this;return a.iterations=10,a.gravity=O,a.damping=1,a.collisionSlop=.1,a.collisionBias=A(.9,60),a.collisionPersistence=3,a.locked=0,a.curr_dt=0,a.stamp=0,a.staticShapes=new Nb(cd,null),a.activeShapes=new Nb(cd,a.staticShapes),a.activeShapes.setVelocityFunc(bd),a.bodies=[],a.sleepingComponents=[],a.rousedBodies=[],a.sleepTimeThreshold=1/0,a.idleSpeedThreshold=0,a.enableContactGraph=!1,a.arbiters=[],a.pooledArbiters=[],a.cachedArbiters={},a.constraints=[],a.defaultHandler=dd,a.collisionHandlers={},a.postStepCallbacks=[],a.skipPostStep=!1,a.staticBody=new Xb(1/0,1/0),a.staticBody.nodeIdleTime=1/0,a};ed.prototype.setIterations=function(a){this.iterations=a},ed.prototype.getCurrentTimeStep=function(){return this.curr_dt};var fd=function(a){j(!a.locked,"This operation cannot be done safely during a call to cpSpaceStep() or during a query. Put these calls into a post-step callback.")};ed.prototype.addCollisionHandler=function(a,b,c,d,e,f,h){var i=this;fd(i),i.removeCollisionHandler(a,b);var j=new Bb(a,b,c?c:ad,d?d:ad,e?e:g,f?f:g,h);i.collisionHandlers[l(a,b)]=j},ed.prototype.removeCollisionHandler=function(a,b){var c=this;fd(c),delete c.collisionHandlers[l(a,b)]},ed.prototype.setDefaultCollisionHandler=function(a,b,c,d,e){var f=this;fd(f);var h=new Bb(0,0,a?a:ad,b?b:ad,c?c:g,d?d:g,e);f.defaultHandler=h},ed.prototype.addShape=function(a){var b=this,c=a.body;return c.isStatic()?b.addStaticShape(a):(j(a.space!=b,"You have already added this shape to this space. You must not add it a second time."),j(!a.space,"You have already added this shape to another space. You cannot add it to a second."),fd(b),c.activate(),c.addShape(a),a.update(c.p,c.rot),b.activeShapes.insert(a,a.hashid),a.space=b,a)},ed.prototype.addStaticShape=function(a){var b=this;j(a.space!=b,"You have already added this shape to this space. You must not add it a second time."),j(!a.space,"You have already added this shape to another space. You cannot add it to a second."),j(a.body.isRogue(),"You are adding a static shape to a dynamic body. Did you mean to attach it to a static or rogue body? See the documentation for more information."),fd(b);var c=a.body;return c.addShape(a),a.update(c.p,c.rot),b.staticShapes.insert(a,a.hashid),a.space=b,a},ed.prototype.addBody=function(a){var b=this;return j(!a.isStatic(),"Do not add static bodies to a space. Static bodies do not move and should not be simulated."),j(a.space!=b,"You have already added this body to this space. You must not add it a second time."),j(!a.space,"You have already added this body to another space. You cannot add it to a second."),fd(b),b.bodies.push(a),a.space=b,a},ed.prototype.addConstraint=function(a){var b=this;j(a.space!=b,"You have already added this constraint to this space. You must not add it a second time."),j(!a.space,"You have already added this constraint to another space. You cannot add it to a second."),j(a.a&&a.b,"Constraint is attached to a null body."),fd(b),a.a.activate(),a.b.activate(),b.constraints.push(a);var c=a.a,d=a.b;return a.next_a=c.constraintList,c.constraintList=a,a.next_b=d.constraintList,d.constraintList=a,a.space=b,a},ed.prototype.filterArbiters=function(a,b){var c=this;c.lock();var d=c.cachedArbiters;for(var e in d){var f=d[e];(a==f.body_a&&(b==f.a||null==b)||a==f.body_b&&(b==f.b||null==b))&&(b&&f.state!=wb&&f.callSeparate(c),f.unthread(),Eb(c.arbiters,f),delete d[e])}c.unlock(!0)},ed.prototype.removeShape=function(a){var b=this,c=a.body;c.isStatic()?b.removeStaticShape(a):(j(b.containsShape(a),"Cannot remove a shape that was not added to the space. (Removed twice maybe?)"),fd(b),c.activate(),c.removeShape(a),b.filterArbiters(c,a),b.activeShapes.remove(a,a.hashid),a.space=null)},ed.prototype.removeStaticShape=function(a){var b=this;j(b.containsShape(a),"Cannot remove a static or sleeping shape that was not added to the space. (Removed twice maybe?)"),fd(b);var c=a.body;c.isStatic()&&c.activateStatic(a),c.removeShape(a),b.filterArbiters(c,a),b.staticShapes.remove(a,a.hashid),a.space=null},ed.prototype.removeBody=function(a){var b=this;j(b.containsBody(a),"Cannot remove a body that was not added to the space. (Removed twice maybe?)"),fd(b),a.activate(),Eb(b.bodies,a),a.space=null},ed.prototype.removeConstraint=function(a){var b=this;j(b.containsConstraint(a),"Cannot remove a constraint that was not added to the space. (Removed twice maybe?)"),fd(b),a.a.activate(),a.b.activate(),Eb(b.constraints,a),a.a.removeConstraint(a),a.b.removeConstraint(a),a.space=null},ed.prototype.containsShape=function(a){return a.space==this},ed.prototype.containsBody=function(a){return a.space==this},ed.prototype.containsConstraint=function(a){return a.space==this},ed.prototype.convertBodyToStatic=function(a){var b=this;j(!a.isStatic(),"Body is already static."),j(a.isRogue(),"Remove the body from the space before calling this function."),fd(b),a.setMass(1/0),a.setMoment(1/0),a.setVel(O),a.setAngVel(0),a.nodeIdleTime=1/0;for(var c=a.shapeList;c;c=c.next)b.activeShapes.remove(c,c.hashid),b.staticShapes.insert(c,c.hashid)},ed.prototype.convertBodyToDynamic=function(a,b,c){var d=this;j(a.isStatic(),"Body is already dynamic."),fd(d),a.activateStatic(null),a.setMass(b),a.setMoment(c),a.nodeIdleTime=0;for(var e=a.shapeList;e;e=e.next)d.staticShapes.remove(e,e.hashid),d.activeShapes.insert(e,e.hashid)},ed.prototype.eachBody=function(a,b){var c=this;c.lock();for(var d=c.bodies,e=0;e<d.length;e++)a(d[e],b);for(var f=c.sleepingComponents,e=0;e<f.length;e++)for(var g=f[e],h=g;h;){var i=h.nodeNext;a(h,b),h=i}c.unlock(!0)},ed.prototype.eachShape=function(a,b){var c=this;c.lock(),c.activeShapes.each(a,b),c.staticShapes.each(a,b),c.unlock(!0)},ed.prototype.eachConstraint=function(a,b){var c=this;c.lock();for(var d=c.constraints,e=0;e<d.length;e++)a(d[e],b);c.unlock(!0)};var gd=function(a){var b=a.body;a.update(b.p,b.rot)};ed.prototype.reindexStatic=function(){var a=this;j(!a.locked,"You cannot manually reindex objects while the space is locked. Wait until the current query or step is complete."),a.staticShapes.each(gd,null),a.staticShapes.reindex()},ed.prototype.reindexShape=function(a){var b=this;j(!b.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),b.activeShapes.reindexObject(a,a.hashid),b.staticShapes.reindexObject(a,a.hashid)},ed.prototype.reindexShapesForBody=function(a){for(var b=this,c=a.shapeList;c;c=c.next)b.reindexShape(c)},ed.prototype.activateBody=function(a){var b=this;if(j(!a.isRogue(),"Internal error: Attempting to activate a rogue body."),b.locked)-1==b.rousedBodies.indexOf(a)&&b.rousedBodies.push(a);else{b.bodies.push(a);for(var c=a.shapeList;c;c=c.next)b.staticShapes.remove(c,c.hashid),b.activeShapes.insert(c,c.hashid);for(var d=a.arbiterList;d;d=d.next(a)){var e=d.body_a;if(a==e||e.isStatic()){var f=d.a,g=d.b,h=l(f.hashid,g.hashid);b.cachedArbiters[h]=d,d.stamp=b.stamp,d.handler=b.lookupHandler(f.collision_type,g.collision_type),b.arbiters.push(d)}}for(var i=a.constraintList;i;i=i.next(a)){var e=i.a;(a==e||e.isStatic())&&b.constraints.push(i)}}},ed.prototype.deactivateBody=function(a){var b=this;j(!a.isRogue(),"Internal error: Attempting to deactivate a rouge body."),Eb(b.bodies,a);for(var c=a.shapeList;c;c=c.next)b.activeShapes.remove(c,c.hashid),b.staticShapes.insert(c,c.hashid);for(var d=a.arbiterList;d;d=d.next(a)){var e=d.body_a;(a==e||e.isStatic())&&b.uncacheArbiter(d)}for(var f=a.constraintList;f;f=f.next(a)){var e=f.a;(a==e||e.isStatic())&&Eb(b.constraints,f)}};var hd=function(a){return a?a.nodeRoot:null},id=function(a){if(a&&a.isSleeping()){j(!a.isRogue(),"Internal Error: ComponentActivate() called on a rogue body.");for(var b=a.space,c=a;c;){var d=c.nodeNext;c.nodeIdleTime=0,c.nodeRoot=null,c.nodeNext=null,b.activateBody(c),c=d}Eb(b.sleepingComponents,a)}};Xb.prototype.activate=function(){var a=this;a.isRogue()||(a.nodeIdleTime=0,id(hd(a)));for(var b=a.arbiterList;b;b=b.next(a)){var c=b.body_a==a?b.body_b:b.body_a;c.isStatic()||(c.nodeIdleTime=0)}},Xb.prototype.activateStatic=function(a){var b=this;j(b.isStatic(),"cpBodyActivateStatic() called on a non-static body.");for(var c=b.arbiterList;c;c=c.next(b))a&&a!=c.a&&a!=c.b||(c.body_a==b?c.body_b:c.body_a).activate()},Xb.prototype.pushArbiter=function(a){var b=this,c=b.arbiterList;a.threadForBody(b).next=c,c&&(c.threadForBody(b).prev=a),b.arbiterList=a};var jd=function(a,b){b.nodeRoot=a,b!=a&&(b.nodeNext=a.nodeNext,a.nodeNext=b)},kd=function(a,b){if(!b.isRogue()){var c=hd(b);if(null==c){jd(a,b);for(var d=b.arbiterList;d;d=d.next(b))kd(a,b==d.body_a?d.body_b:d.body_a);for(var e=b.constraintList;e;e=e.next(b))kd(a,b==e.a?e.b:e.a)}}},ld=function(a,b){for(var c=a;c;c=c.nodeNext)if(c.nodeIdleTime<b)return!0;return!1};ed.prototype.processComponents=function(a){var b,c,d=this,e=1/0!=d.sleepTimeThreshold,f=d.bodies;if(e)for(var g=d.idleSpeedThreshold,h=g?g*g:$(d.gravity)*a*a,b=0;b<f.length;b++){var c=f[b],i=h?c.m*h:0;c.nodeIdleTime=c.kineticEnergy()>i?0:c.nodeIdleTime+a}for(var j=d.arbiters,b=0,k=j.length;k>b;b++){var l=j[b],m=l.body_a,n=l.body_b;e&&((n.isRogue()&&!n.isStatic()||m.isSleeping())&&m.activate(),(m.isRogue()&&!m.isStatic()||n.isSleeping())&&n.activate()),m.pushArbiter(l),n.pushArbiter(l)}if(e){for(var o=d.constraints,b=0;b<o.length;b++){var p=o[b],m=p.a,n=p.b;n.isRogue()&&!n.isStatic()&&m.activate(),m.isRogue()&&!m.isStatic()&&n.activate()}for(var b=0;b<f.length;){var c=f[b];if(null!=hd(c)||(kd(c,c),ld(c,d.sleepTimeThreshold)))b++,c.nodeRoot=null,c.nodeNext=null;else{d.sleepingComponents.push(c);for(var q=c;q;q=q.nodeNext)d.deactivateBody(q)}}}},Xb.prototype.sleep=function(){var a=this;a.sleepWithGroup(null)},Xb.prototype.sleepWithGroup=function(a){var b=this;j(!b.isRogue(),"Rogue (and static) bodies cannot be put to sleep.");var c=b.space;if(j(!c.locked,"Bodies cannot be put to sleep during a query or a call to cpSpaceStep(). Put these calls into a post-step callback."),j(null==a||a.isSleeping(),"Cannot use a non-sleeping body as a group identifier."),b.isSleeping())return j(hd(b)==hd(a),"The body is already sleeping and it's group cannot be reassigned."),void 0;for(var d=b.shapeList;d;d=d.next)d.update(b.p,b.rot);if(c.deactivateBody(b),a){var e=hd(a);b.nodeRoot=e,b.nodeNext=e.nodeNext,b.nodeIdleTime=0,e.nodeNext=b}else b.nodeRoot=b,b.nodeNext=null,b.nodeIdleTime=0,c.sleepingComponents.push(b);Eb(c.bodies,b)};var md=function(a){a.body.activate()};ed.prototype.activateShapesTouchingShape=function(a){var b=this;1/0!=b.sleepTimeThreshold&&b.shapeQuery(a,md,a)};var nd=function(a,b,c,d,e){this.point=a,this.layers=b,this.group=c,this.func=d,this.data=e},od=function(a,b,c){return(!b.group||a.group!=b.group)&&a.layers&b.layers&&b.pointQuery(a.point)&&a.func(b,a.data),c};ed.prototype.pointQuery=function(a,b,c,d,e){var f=this,g=new nd(a,b,c,d,e),h=Gb(a,0);f.lock(),f.activeShapes.query(g,h,od,e),f.staticShapes.query(g,h,od,e),f.unlock(!0)},ed.prototype.pointQueryFirst=function(a,b,c){var d=this,e=null;return d.pointQuery(a,b,c,function(a){a.sensor||(e=a)}),e};var pd=function(a,b,c,d,e){this.point=a,this.maxDistance=b,this.layers=c,this.group=d,this.func=e},qd=function(a,b,c,d){if((!b.group||a.group!=b.group)&&a.layers&b.layers){var e=b.nearestPointQuery(a.point);e&&e.shape&&e.d<a.maxDistance&&a.func(b,e.d,e.p,d)}return c};ed.prototype.nearestPointQuery=function(a,b,c,d,e,f){var g=this,h=new pd(a,b,c,d,e),i=Fb.newForCircle(a,F(b,0));g.lock(),g.activeShapes.query(h,i,qd,f),g.staticShapes.query(h,i,qd,f),g.unlock(!0)};var rd=function(a,b,c,d){if((!b.group||a.group!=b.group)&&a.layers&b.layers&&!b.sensor){var e=b.nearestPointQuery(a.point);e&&e.d<d.d&&h(d,e)}return c};ed.prototype.nearestPointQueryNearest=function(a,b,c,d){var e=this,f=new Pc(null,O,b,O),g=new pd(a,b,c,d,null),h=Fb.newForCircle(a,F(b,0));return e.activeShapes.query(g,h,rd,f),e.staticShapes.query(g,h,rd,f),f.shape?f:null};var sd=function(a,b,c,d,e){this.start=a,this.end=b,this.layers=c,this.group=d,this.func=e},td=function(a,b,c){var d;return(!b.group||a.group!=b.group)&&a.layers&b.layers&&(d=b.segmentQuery(a.start,a.end))&&a.func(b,d.t,d.n,c),1};ed.prototype.segmentQuery=function(a,b,c,d,e,f){var g=this,h=new sd(a,b,c,d,e);g.lock(),g.staticShapes.segmentQuery(h,a,b,1,td,f),g.activeShapes.segmentQuery(h,a,b,1,td,f),g.unlock(!0)};var ud=function(a,b,c){var d;return(!b.group||a.group!=b.group)&&a.layers&b.layers&&!b.sensor&&(d=b.segmentQuery(a.start,a.end))&&d.t<c.t&&h(c,d),c.t};ed.prototype.segmentQueryFirst=function(a,b,c,d){var e=this,f=new Qc(null,1,O),g=new sd(a,b,c,d,null);return e.staticShapes.segmentQuery(g,a,b,1,ud,f),e.activeShapes.segmentQuery(g,a,b,f.t,ud,f),f.shape?f:null};var vd=function(a,b,c,d){this.bb=a,this.layers=b,this.group=c,this.func=d},wd=function(a,b,c,d){return(!b.group||a.group!=b.group)&&a.layers&b.layers&&a.bb.intersects(b.bb)&&a.func(b,d),c};ed.prototype.bBQuery=function(a,b,c,d,e){var f=this,g=new vd(a,b,c,d);f.lock(),f.activeShapes.query(g,a,wd,e),f.staticShapes.query(g,a,wd,e),f.unlock(!0)};var xd=function(a,b,c){this.func=a,this.data=b,this.anyCollision=c},yd=function(a,b,c,d){if(a.group&&a.group==b.group||!(a.layers&b.layers)||a==b)return c;var e=[],f=0;if(a.type<=b.type)Kc(a,b,c,e),f=e.length;else{Kc(b,a,c,e),f=e.length;for(var g=0;f>g;g++)e[g].n=S(e[g].n)}if(f&&(d.anyCollision=!(a.sensor||b.sensor),d.func)){var h=new Db;h.count=f;for(var i,g=0;f>g;g++)i=e[g],h.points[g]=new Cb(i.p,i.n,i.dist);d.func(b,h,d.data)}return c};ed.prototype.shapeQuery=function(a,b,c){var d=this,e=a.body,f=e?a.update(e.p,e.rot):a.bb,g=new xd(b,c,!1);return d.lock(),d.activeShapes.query(a,f,yd,g),d.staticShapes.query(a,f,yd,g),d.unlock(!0),g.anyCollision},ed.prototype.getPostStepCallback=function(a){for(var b=this,c=b.postStepCallbacks,d=0;d<c.length;d++){var e=c[d];if(e&&e.key==a)return e}return null},ed.prototype.addPostStepCallback=function(a,b,c){var d=this;if(d.getPostStepCallback(b))return!1;var e=new Hd(a?a:g,b,c);return d.postStepCallbacks.push(e),!0},ed.prototype.lock=function(){var a=this;a.locked++},ed.prototype.unlock=function(a){var b=this;if(b.locked--,j(b.locked>=0,"Internal Error: Space lock underflow."),0==b.locked){for(var c,d=b.rousedBodies;c=d.pop();)b.activateBody(c);if(0==b.locked&&a&&!b.skipPostStep){b.skipPostStep=!0;for(var e,f=b.postStepCallbacks;e=f.pop();){var g=e.func;e.func=null,g&&g(b,e.key,e.data)}b.skipPostStep=!1}}};var zd=function(a,b){var c=!a.bb.intersects(b.bb)||a.body==b.body||a.group&&a.group==b.group||!(a.layers&b.layers)||1/0==a.body.m&&1/0==b.body.m;return c},Ad=function(a,b,c,d){if(zd(a,b))return c;var e=d.lookupHandler(a.collision_type,b.collision_type),f=a.sensor||b.sensor;if(f&&e==dd)return c;if(a.type>b.type||a.type==b.type&&b>a){var g=a;a=b,b=g}var h=[],i={id:c};Kc(a,b,i,h);var j=h.length;if(!j)return i.id;var k=l(a.hashid,b.hashid),m=d.cachedArbiters[k];return m||(m=d.pooledArbiters.pop(),m?m.reset(a,b):m=d.cachedArbiters[k]=new Ab(a,b)),m.update(h,e,a,b),m.state!=tb||e.begin(m,d,e.data)||m.ignore(),m.state!=vb&&e.preSolve(m,d,e.data)&&!f?d.arbiters.push(m):(m.contacts=null,m.state!=vb&&(m.state=ub)),m.stamp=d.stamp,i.id};ed.prototype.arbiterSetFilter=function(a){var b=this,c=b.stamp-a.stamp,d=a.body_a,e=a.body_b;return(d.isStatic()||d.isSleeping())&&(e.isStatic()||e.isSleeping())?!0:(c>=1&&a.state!=wb&&(a.state=wb,a.callSeparate(b)),c>=b.collisionPersistence?(a.contacts=null,b.pooledArbiters.push(a),!1):!0)};var Bd=function(a){var b=a.body;a.update(b.p,b.rot)};ed.prototype.step=function(a){var b=this;if(0!=a){b.stamp++;var c=b.curr_dt;b.curr_dt=a;for(var d,e=b.bodies,f=b.constraints,g=b.arbiters;d=g.pop();)d.state=ub,d.body_a.isSleeping()||d.body_b.isSleeping()||d.unthread();b.lock();for(var h=0;h<e.length;h++){var i=e[h];i.updatePosition(a)}b.activeShapes.each(Bd,null),b.activeShapes.reindexQuery(Ad,b),b.unlock(!1),b.processComponents(a),b.lock();var j=b.cachedArbiters;for(var k in j)this.arbiterSetFilter(j[k])||delete j[k];for(var l=g.length,m=f.length,n=b.collisionSlop,o=1-A(b.collisionBias,a),h=0;l>h;h++)g[h].preStep(a,n,o);for(var h=0;m>h;h++){var p=f[h];p.preSolve(b),p.preStep(a)}for(var q=A(b.damping,a),r=b.gravity,h=0;h<e.length;h++)e[h].updateVelocity(r,q,a);for(var s=0==c?0:a/c,h=0;l>h;h++)g[h].applyCachedImpulse(s);for(var h=0;m>h;h++)f[h].applyCachedImpulse(s);for(var h=0;h<b.iterations;h++){for(var t=0;l>t;t++)g[t].applyImpulse();for(var t=0;m>t;t++)f[t].applyImpulse(a)}for(var h=0;m>h;h++)f[h].postSolve(b);for(var h=0;l>h;h++){var d=g[h],u=d.handler;u.postSolve(d,b,u.data)}b.unlock(!0)}},Xb.prototype.isSleeping=function(){return!!this.nodeRoot},Xb.prototype.isStatic=function(){return 1/0==this.nodeIdleTime},Xb.prototype.isRogue=function(){return!this.space},Xb.prototype.local2World=function(a){var b=this;return Q(b.p,Y(a,b.rot))},Xb.prototype.world2Local=function(a){var b=this;return Z(R(a,b.p),b.rot)},Xb.prototype.kineticEnergy=function(){var a=this,b=U(a.v,a.v),c=a.w*a.w;return(b?b*a.m:0)+(c?c*a.i:0)},ed.prototype.isLocked=function(){return this.locked};var Cd=1e-5;gb.prototype.next=function(a){var b=this;return b.a==a?b.next_a:b.next_b},Ab.prototype.next=function(a){var b=this;return b.body_a==a?b.thread_a.next:b.thread_b.next};var Dd=function(a,b,c){var d=b.x-c.x,e=b.y-c.y,f=J((d*(a.x-c.x)+e*(a.y-c.y))/(d*d+e*e));return new N(c.x+d*f,c.y+e*f)};Sc.prototype.active=function(){var a=this;return a.prev||a.body&&a.body.shapeList==a};var Ed=function(a,b,c,d,e){var f=R(d,b),g=R(e,b),h=U(f,f)-2*U(f,g)+U(g,g),i=-2*U(f,f)+2*U(f,g),j=U(f,f)-c*c,k=i*i-4*h*j;if(k>=0){var l=(-i-t(k))/(2*h);if(l>=0&&1>=l)return new Qc(a,l,bb(ab(f,g,l)))}},Fd=function(a,b){this.n=a,this.d=b},Gd=function(a,b){var c=bb(W(R(b,a))),d=U(c,a);return new Fd(c,d)};Fd.prototype.compare=function(a){var b=this;return U(b.n,a)-b.d};var Hd=function(a,b,c){this.func=a,this.key=b,this.data=c};ed.prototype.lookupHandler=function(a,b){return this.collisionHandlers[l(a,b)]||this.defaultHandler},ed.prototype.uncacheArbiter=function(a){var b=this,c=a.a,d=a.b,e=l(c.hashid,d.hashid);delete b.cachedArbiters[e],Eb(b.arbiters,a)},Ab.prototype.callSeparate=function(a){var b=this,c=a.lookupHandler(b.a.collision_type,b.b.collision_type);c.separate(b,a,c.data)},Ab.prototype.threadForBody=function(a){var b=this;return b.body_a==a?b.thread_a:b.thread_b},gb.prototype.activateBodies=function(){var a=this,b=a.a;b&&b.activate();var c=a.b;c&&c.activate()};var Id=function(a,b,c,d){var e=b.v.x-d.y*b.w-(a.v.x-c.y*a.w),f=b.v.y+d.x*b.w-(a.v.y+c.x*a.w);return new N(e,f)},Jd=function(a,b,c,d,e){var f=b.v.x-d.y*b.w-(a.v.x-c.y*a.w),g=b.v.y+d.x*b.w-(a.v.y+c.x*a.w);return f*e.x+g*e.y},Kd=function(a,b,c){a.v.x+=b.x*a.m_inv,a.v.y+=b.y*a.m_inv,a.w+=a.i_inv*(c.x*b.y-c.y*b.x)},Ld=function(a,b,c,d,e){var f=e.x,g=e.y;a.v.x+=-f*a.m_inv,a.v.y+=-g*a.m_inv,a.w+=a.i_inv*(-c.x*g+c.y*f),b.v.x+=f*b.m_inv,b.v.y+=g*b.m_inv,b.w+=b.i_inv*(d.x*g-d.y*f)},Md=function(a,b,c){var d=V(b,c);return a.m_inv+a.i_inv*d*d},Nd=function(a,b,c,d,e){var f=Md(a,c,e)+Md(b,d,e);return f},Od=function(a,b,c,d){var e=a.m_inv+b.m_inv,f=e,g=0,h=0,i=e,j=a.i_inv,k=c.x*c.x*j,l=c.y*c.y*j,m=-c.x*c.y*j;f+=l,g+=m,h+=m,i+=k;var n=b.i_inv,o=d.x*d.x*n,p=d.y*d.y*n,q=-d.x*d.y*n;f+=p,g+=q,h+=q,i+=o;var r=f*i-g*h,s=1/r;return new M(i*s,-g*s,-h*s,f*s)},Pd=f.biasCoef=function(a,b){return 1-A(a,b)}}({},function(){return this}());