-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathcp.min.js
1 lines (1 loc) · 57.9 KB
/
cp.min.js
1
!function(){Object.create=Object.create||function(t){function i(){}return i.prototype=t,new i};var t;"undefined"==typeof exports?(t={},"object"==typeof window&&(window.cp=t)):t=exports;var i,e,s=function(t,i){if(!t)throw new Error("Assertion failed: "+i)},n=function(t,i){!t&&console&&console.warn&&(console.warn("ASSERTION FAILED: "+i),console.trace&&console.trace())};"object"==typeof window&&window.navigator.userAgent.indexOf("Firefox")>-1?(i=Math.min,e=Math.max):(i=function(t,i){return t<i?t:i},e=function(t,i){return t>i?t:i});var r=function(t,i){return t<i?t+" "+i:i+" "+t},o=function(t,i){for(var e=0;e<t.length;e++)if(t[e]===i)return t[e]=t[t.length-1],void t.length--},a=function(t,i,e){var s=w(i,e),n=y(d(s,w(t,e))/P(s));return g(e,A(s,n))},h=function(t,i,e,s,n,r){var o=e-n,a=s-r,h=y(_(o,a,t-n,i-r)/L(o,a));return new v(n+o*h,r+a*h)};t.momentForCircle=function(t,i,e,s){return t*(.5*(i*i+e*e)+P(s))},t.areaForCircle=function(t,i){return Math.PI*Math.abs(t*t-i*i)},t.momentForSegment=function(t,i,e){var s=A(g(i,e),.5);return t*(V(e,i)/12+P(s))},t.areaForSegment=function(t,i,e){return e*(Math.PI*e+2*R(t,i))},t.momentForPoly=function(t,i,e){for(var s=0,n=0,r=i.length,o=0;o<r;o+=2){var a=i[o]+e.x,h=i[o+1]+e.y,c=i[(o+2)%r]+e.x,p=i[(o+3)%r]+e.y,u=B(c,p,a,h);s+=u*(_(a,h,a,h)+_(a,h,c,p)+_(c,p,c,p)),n+=u}return t*s/(6*n)},t.areaForPoly=function(t){for(var i=0,e=0,s=t.length;e<s;e+=2)i+=j(new v(t[e],t[e+1]),new v(t[(e+2)%s],t[(e+3)%s]));return-i/2},t.centroidForPoly=function(t){for(var i=0,e=new v(0,0),s=0,n=t.length;s<n;s+=2){var r=new v(t[s],t[s+1]),o=new v(t[(s+2)%n],t[(s+3)%n]),a=j(r,o);i+=a,e=g(e,A(g(r,o),a))}return A(e,1/(3*i))},t.recenterPoly=function(i){for(var e=t.centroidForPoly(i),s=0;s<i.length;s+=2)i[s]-=e.x,i[s+1]-=e.y},t.momentForBox=function(t,i,e){return t*(i*i+e*e)/12},t.momentForBox2=function(i,e){var s=e.r-e.l,n=e.t-e.b,r=A([e.l+e.r,e.b+e.t],.5);return t.momentForBox(i,s,n)+i*P(r)};var c=t.loopIndexes=function(t){var i,e,s,n,r=0,o=0;i=s=t[0],e=n=t[1];for(var a=t.length>>1,h=1;h<a;h++){var c=t[2*h],p=t[2*h+1];c<i||c==i&&p<e?(i=c,e=p,r=h):(c>s||c==s&&p>n)&&(s=c,n=p,o=h)}return[r,o]},p=function(t,i,e){var s=t[2*i];t[2*i]=t[2*e],t[2*e]=s,s=t[2*i+1],t[2*i+1]=t[2*e+1],t[2*e+1]=s},u=function(t,i,e,s,n,r){if(0===e)return 0;for(var o=0,a=i,h=w(n,s),c=r*x(h),u=i,l=i+e-1;u<=l;){var b=new v(t[2*u],t[2*u+1]),y=j(h,w(b,s));y>c?(y>o&&(o=y,a=u),u++):(p(t,u,l),l--)}return a!=i&&p(t,i,a),u-i},l=function(t,i,e,s,n,r,o,a){if(s<0)return 0;if(0==s)return i[2*a]=r.x,i[2*a+1]=r.y,1;var h=u(i,e,s,n,r,t),c=new v(i[2*e],i[2*e+1]),p=l(t,i,e+1,h-1,n,c,r,a),b=a+p++;i[2*b]=r.x,i[2*b+1]=r.y;var y=u(i,e+h,s-h,r,o,t),f=new v(i[2*(e+h)],i[2*(e+h)+1]);return p+l(t,i,e+h+1,y-1,r,f,o,a+p)};t.convexHull=function(t,i,e){if(i)for(var s=0;s<t.length;s++)i[s]=t[s];else i=t;var r=c(t),o=r[0],a=r[1];if(o==a)return i.length=2,i;p(i,0,o),p(i,1,0==a?o:a);var h=new v(i[0],i[1]),u=new v(i[2],i[3]),b=t.length>>1,y=l(e,i,2,b-2,h,u,h,1)+1;return i.length=2*y,n(Z(i),"Internal error: cpConvexHull() and cpPolyValidate() did not agree.Please report this error with as much info as you can."),i};var b=function(t,s,n){return i(e(t,s),n)},y=function(t){return e(0,i(t,1))},v=t.Vect=function(t,i){this.x=t,this.y=i};t.v=function(t,i){return new v(t,i)};var f=t.vzero=new v(0,0),d=t.v.dot=function(t,i){return t.x*i.x+t.y*i.y},_=function(t,i,e,s){return t*e+i*s},x=t.v.len=function(t){return Math.sqrt(d(t,t))},m=t.v.len2=function(t,i){return Math.sqrt(t*t+i*i)},g=(t.v.eql=function(t,i){return t.x===i.x&&t.y===i.y},t.v.add=function(t,i){return new v(t.x+i.x,t.y+i.y)});v.prototype.add=function(t){return this.x+=t.x,this.y+=t.y,this};var w=t.v.sub=function(t,i){return new v(t.x-i.x,t.y-i.y)};v.prototype.sub=function(t){return this.x-=t.x,this.y-=t.y,this};var S=t.v.neg=function(t){return new v(-t.x,-t.y)};v.prototype.neg=function(){return this.x=-this.x,this.y=-this.y,this};var A=t.v.mult=function(t,i){return new v(t.x*i,t.y*i)};v.prototype.mult=function(t){return this.x*=t,this.y*=t,this};var j=t.v.cross=function(t,i){return t.x*i.y-t.y*i.x},B=function(t,i,e,s){return t*s-i*e},C=t.v.perp=function(t){return new v(-t.y,t.x)},k=(t.v.pvrperp=function(t){return new v(t.y,-t.x)},t.v.project=function(t,i){return A(i,d(t,i)/P(i))});v.prototype.project=function(t){return this.mult(d(this,t)/P(t)),this};var M=t.v.rotate=function(t,i){return new v(t.x*i.x-t.y*i.y,t.x*i.y+t.y*i.x)};v.prototype.rotate=function(t){return this.x=this.x*t.x-this.y*t.y,this.y=this.x*t.y+this.y*t.x,this};var I=t.v.unrotate=function(t,i){return new v(t.x*i.x+t.y*i.y,t.y*i.x-t.x*i.y)},P=t.v.lengthsq=function(t){return d(t,t)},L=t.v.lengthsq2=function(t,i){return t*t+i*i},N=t.v.lerp=function(t,i,e){return g(A(t,1-e),A(i,e))},F=t.v.normalize=function(t){return A(t,1/x(t))},Q=t.v.normalize_safe=function(t){return 0===t.x&&0===t.y?f:F(t)},T=t.v.clamp=function(t,i){return d(t,t)>i*i?A(F(t),i):t},R=(t.v.lerpconst=function(t,i,e){return g(t,T(w(i,t),e))},t.v.dist=function(t,i){return x(w(t,i))}),V=t.v.distsq=function(t,i){return P(w(t,i))},O=(t.v.near=function(t,i,e){return V(t,i)<e*e},t.v.slerp=function(t,i,e){var s=Math.acos(d(t,i));if(s){var n=1/Math.sin(s);return g(A(t,Math.sin((1-e)*s)*n),A(i,Math.sin(e*s)*n))}return t}),q=(t.v.slerpconst=function(t,e,s){var n=Math.acos(d(t,e));return O(t,e,i(s,n)/n)},t.v.forangle=function(t){return new v(Math.cos(t),Math.sin(t))},t.v.toangle=function(t){return Math.atan2(t.y,t.x)},t.v.str=function(t){return"("+t.x.toFixed(3)+", "+t.y.toFixed(3)+")"},0),E=t.BB=function(t,i,e,s){this.l=t,this.b=i,this.r=e,this.t=s,q++};t.bb=function(t,i,e,s){return new E(t,i,e,s)};var H=function(t,i){return new E(t.x-i,t.y-i,t.x+i,t.y+i)},D=function(t,i,e,s,n){return t.l<=s&&i<=t.r&&t.b<=n&&e<=t.t},G=0,W=(t.NO_GROUP=0,t.ALL_LAYERS=-1);t.resetShapeIdCounter=function(){G=0};var z=t.Shape=function(t){this.body=t,this.bb_l=this.bb_b=this.bb_r=this.bb_t=0,this.hashid=G++,this.sensor=!1,this.e=0,this.u=0,this.surface_v=f,this.collision_type=0,this.group=0,this.layers=W,this.space=null,this.collisionCode=this.collisionCode};z.prototype.setElasticity=function(t){this.e=t},z.prototype.setFriction=function(t){this.body.activate(),this.u=t},z.prototype.setLayers=function(t){this.body.activate(),this.layers=t},z.prototype.setSensor=function(t){this.body.activate(),this.sensor=t},z.prototype.setCollisionType=function(t){this.body.activate(),this.collision_type=t},z.prototype.getBody=function(){return this.body},z.prototype.active=function(){return this.body&&-1!==this.body.shapeList.indexOf(this)},z.prototype.setBody=function(t){s(!this.active(),"You cannot change the body on an active shape. You must remove the shape from the space before changing the body."),this.body=t},z.prototype.cacheBB=function(){return this.update(this.body.p,this.body.rot)},z.prototype.update=function(t,i){s(!isNaN(i.x),"Rotation is NaN"),s(!isNaN(t.x),"Position is NaN"),this.cacheData(t,i)},z.prototype.pointQuery=function(t){var i=this.nearestPointQuery(t);if(i.d<0)return i},z.prototype.getBB=function(){return new E(this.bb_l,this.bb_b,this.bb_r,this.bb_t)};var J=function(t,i,e){this.shape=t,this.p=i,this.d=e},U=function(t,i,e){this.shape=t,this.t=i,this.n=e};U.prototype.hitPoint=function(t,i){return N(t,i,this.t)},U.prototype.hitDist=function(t,i){return R(t,i)*this.t};var Y=t.CircleShape=function(t,i,e){this.c=this.tc=e,this.r=i,this.type="circle",z.call(this,t)};Y.prototype=Object.create(z.prototype),Y.prototype.cacheData=function(t,i){var e=this.tc=M(this.c,i).add(t),s=this.r;this.bb_l=e.x-s,this.bb_b=e.y-s,this.bb_r=e.x+s,this.bb_t=e.y+s},Y.prototype.nearestPointQuery=function(t){var i=t.x-this.tc.x,e=t.y-this.tc.y,s=m(i,e),n=this.r,r=new v(this.tc.x+i*n/s,this.tc.y+e*n/s);return new J(this,r,s-n)};var K=function(t,i,e,s,n,r){s=w(s,i),n=w(n,i);var o=d(s,s)-2*d(s,n)+d(n,n),a=-2*d(s,s)+2*d(s,n),h=a*a-4*o*(d(s,s)-e*e);if(h>=0){var c=(-a-Math.sqrt(h))/(2*o);if(0<=c&&c<=1)return new U(t,c,F(N(s,n,c)))}};Y.prototype.segmentQuery=function(t,i){return K(this,this.tc,this.r,t,i)};var X=t.SegmentShape=function(t,i,e,s){this.a=i,this.b=e,this.n=C(F(w(e,i))),this.ta=this.tb=this.tn=null,this.r=s,this.a_tangent=f,this.b_tangent=f,this.type="segment",z.call(this,t)};X.prototype=Object.create(z.prototype),X.prototype.cacheData=function(t,i){this.ta=g(t,M(this.a,i)),this.tb=g(t,M(this.b,i)),this.tn=M(this.n,i);var e,s,n,r;this.ta.x<this.tb.x?(e=this.ta.x,s=this.tb.x):(e=this.tb.x,s=this.ta.x),this.ta.y<this.tb.y?(n=this.ta.y,r=this.tb.y):(n=this.tb.y,r=this.ta.y);var o=this.r;this.bb_l=e-o,this.bb_b=n-o,this.bb_r=s+o,this.bb_t=r+o},X.prototype.nearestPointQuery=function(t){var i=a(t,this.ta,this.tb),e=t.x-i.x,s=t.y-i.y,n=m(e,s),r=this.r,o=n?g(i,A(new v(e,s),r/n)):i;return new J(this,o,n-r)},X.prototype.segmentQuery=function(t,i){var e=this.tn,s=d(w(this.ta,t),e),n=this.r,r=s>0?S(e):e,o=w(A(r,n),t),a=g(this.ta,o),h=g(this.tb,o),c=w(i,t);if(j(c,a)*j(c,h)<=0){var p=s+(s>0?-n:n),u=-p,l=d(c,e)-p;if(u*l<0)return new U(this,u/(u-l),r)}else if(0!==n){var b=K(this,this.ta,this.r,t,i),y=K(this,this.tb,this.r,t,i);return b?y&&y.t<b.t?y:b:y}},X.prototype.setNeighbors=function(t,i){this.a_tangent=w(t,this.a),this.b_tangent=w(i,this.b)},X.prototype.setEndpoints=function(t,i){this.a=t,this.b=i,this.n=C(F(w(i,t)))};var Z=function(t){for(var i=t.length,e=0;e<i;e+=2){var s=t[e],n=t[e+1],r=t[(e+2)%i],o=t[(e+3)%i],a=t[(e+4)%i],h=t[(e+5)%i];if(B(r-s,o-n,a-r,h-o)>0)return!1}return!0},$=t.PolyShape=function(t,i,e){this.setVerts(i,e),this.type="poly",z.call(this,t)};$.prototype=Object.create(z.prototype);var tt=function(t,i){this.n=t,this.d=i};tt.prototype.compare=function(t){return d(this.n,t)-this.d},$.prototype.setVerts=function(t,i){s(t.length>=4,"Polygons require some verts"),s("number"==typeof t[0],"Polygon verticies should be specified in a flattened list (eg [x1,y1,x2,y2,x3,y3,...])"),s(Z(t),"Polygon is concave or has a reversed winding. Consider using cpConvexHull()");var e=t.length,n=e>>1;this.verts=new Array(e),this.tVerts=new Array(e),this.planes=new Array(n),this.tPlanes=new Array(n);for(var r=0;r<e;r+=2){var o=t[r]+i.x,a=t[r+1]+i.y,h=t[(r+2)%e]+i.x,c=t[(r+3)%e]+i.y,p=F(C(new v(h-o,c-a)));this.verts[r]=o,this.verts[r+1]=a,this.planes[r>>1]=new tt(p,_(p.x,p.y,o,a)),this.tPlanes[r>>1]=new tt(new v(0,0),0)}};t.BoxShape=function(t,i,e){var s=i/2,n=e/2;return it(t,new E(-s,-n,s,n))};var it=t.BoxShape2=function(t,i){var e=[i.l,i.b,i.l,i.t,i.r,i.t,i.r,i.b];return new $(t,e,f)};$.prototype.transformVerts=function(t,s){for(var n=this.verts,r=this.tVerts,o=1/0,a=-1/0,h=1/0,c=-1/0,p=0;p<n.length;p+=2){var u=n[p],l=n[p+1],b=t.x+u*s.x-l*s.y,y=t.y+u*s.y+l*s.x;r[p]=b,r[p+1]=y,o=i(o,b),a=e(a,b),h=i(h,y),c=e(c,y)}this.bb_l=o,this.bb_b=h,this.bb_r=a,this.bb_t=c},$.prototype.transformAxes=function(t,i){for(var e=this.planes,s=this.tPlanes,n=0;n<e.length;n++){var r=M(e[n].n,i);s[n].n=r,s[n].d=d(t,r)+e[n].d}},$.prototype.cacheData=function(t,i){this.transformAxes(t,i),this.transformVerts(t,i)},$.prototype.nearestPointQuery=function(t){for(var i=this.tPlanes,e=this.tVerts,s=e[e.length-2],n=e[e.length-1],r=1/0,o=f,a=!1,c=0;c<i.length;c++){i[c].compare(t)>0&&(a=!0);var p=e[2*c],u=e[2*c+1],l=h(t.x,t.y,s,n,p,u),b=R(t,l);b<r&&(r=b,o=l),s=p,n=u}return new J(this,o,a?r:-r)},$.prototype.segmentQuery=function(t,i){for(var e=this.tPlanes,s=this.tVerts,n=e.length,r=2*n,o=0;o<n;o++){var a=e[o].n,h=d(t,a);if(!(e[o].d>h)){var c=d(i,a),p=(e[o].d-h)/(c-h);if(!(p<0||1<p)){var u=N(t,i,p),l=-j(a,u),b=-B(a.x,a.y,s[2*o],s[2*o+1]),y=-B(a.x,a.y,s[(2*o+2)%r],s[(2*o+3)%r]);if(b<=l&&l<=y)return new U(this,p,a)}}}},$.prototype.valueOnAxis=function(t,e){for(var s=this.tVerts,n=_(t.x,t.y,s[0],s[1]),r=2;r<s.length;r+=2)n=i(n,_(t.x,t.y,s[r],s[r+1]));return n-e},$.prototype.containsVert=function(t,i){for(var e=this.tPlanes,s=0;s<e.length;s++){var n=e[s].n;if(_(n.x,n.y,t,i)-e[s].d>0)return!1}return!0},$.prototype.containsVertPartial=function(t,i,e){for(var s=this.tPlanes,n=0;n<s.length;n++){var r=s[n].n;if(!(d(r,e)<0)&&_(r.x,r.y,t,i)-s[n].d>0)return!1}return!0},$.prototype.getNumVerts=function(){return this.verts.length/2},$.prototype.getVert=function(t){return new v(this.verts[2*t],this.verts[2*t+1])};var et=t.Body=function(t,i){this.p=new v(0,0),this.vx=this.vy=0,this.f=new v(0,0),this.w=0,this.t=0,this.v_limit=1/0,this.w_limit=1/0,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(t),this.setMoment(i),this.rot=new v(0,0),this.setAngle(0)};if("undefined"!=typeof DEBUG&&DEBUG){var st=function(t,i){s(t.x==t.x&&t.y==t.y,i)},nt=function(t,i){s(Math.abs(t.x)!==1/0&&Math.abs(t.y)!==1/0,i)},rt=function(t,i){st(t,i),nt(t,i)};et.prototype.sanityCheck=function(){s(this.m===this.m&&this.m_inv===this.m_inv,"Body's mass is invalid."),s(this.i===this.i&&this.i_inv===this.i_inv,"Body's moment is invalid."),rt(this.p,"Body's position is invalid."),rt(this.f,"Body's force is invalid."),s(this.vx===this.vx&&Math.abs(this.vx)!==1/0,"Body's velocity is invalid."),s(this.vy===this.vy&&Math.abs(this.vy)!==1/0,"Body's velocity is invalid."),s(this.a===this.a&&Math.abs(this.a)!==1/0,"Body's angle is invalid."),s(this.w===this.w&&Math.abs(this.w)!==1/0,"Body's angular velocity is invalid."),s(this.t===this.t&&Math.abs(this.t)!==1/0,"Body's torque is invalid."),rt(this.rot,"Body's rotation vector is invalid."),s(this.v_limit===this.v_limit,"Body's velocity limit is invalid."),s(this.w_limit===this.w_limit,"Body's angular velocity limit is invalid.")}}else et.prototype.sanityCheck=function(){};et.prototype.getPos=function(){return this.p},et.prototype.getVel=function(){return new v(this.vx,this.vy)},et.prototype.getAngVel=function(){return this.w},et.prototype.isSleeping=function(){return null!==this.nodeRoot},et.prototype.isStatic=function(){return this.nodeIdleTime===1/0},et.prototype.isRogue=function(){return null===this.space},et.prototype.setMass=function(t){s(t>0,"Mass must be positive and non-zero."),this.activate(),this.m=t,this.m_inv=1/t},et.prototype.setMoment=function(t){s(t>0,"Moment of Inertia must be positive and non-zero."),this.activate(),this.i=t,this.i_inv=1/t},et.prototype.addShape=function(t){this.shapeList.push(t)},et.prototype.removeShape=function(t){o(this.shapeList,t)};var ot=function(t,i,e){return t===e?t.next(i):(t.a===i?t.next_a=ot(t.next_a,i,e):t.next_b=ot(t.next_b,i,e),t)};et.prototype.removeConstraint=function(t){this.constraintList=ot(this.constraintList,this,t)},et.prototype.setPos=function(i){this.activate(),this.sanityCheck(),i===f&&(i=t.v(0,0)),this.p=i},et.prototype.setVel=function(t){this.activate(),this.vx=t.x,this.vy=t.y},et.prototype.setAngVel=function(t){this.activate(),this.w=t},et.prototype.setAngleInternal=function(t){s(!isNaN(t),"Internal Error: Attempting to set body's angle to NaN"),this.a=t,this.rot.x=Math.cos(t),this.rot.y=Math.sin(t)},et.prototype.setAngle=function(t){this.activate(),this.sanityCheck(),this.setAngleInternal(t)},et.prototype.velocity_func=function(t,i,e){var s=this.vx*i+(t.x+this.f.x*this.m_inv)*e,n=this.vy*i+(t.y+this.f.y*this.m_inv)*e,r=this.v_limit,o=s*s+n*n,a=o>r*r?r/Math.sqrt(o):1;this.vx=s*a,this.vy=n*a;var h=this.w_limit;this.w=b(this.w*i+this.t*this.i_inv*e,-h,h),this.sanityCheck()},et.prototype.position_func=function(t){this.p.x+=(this.vx+this.v_biasx)*t,this.p.y+=(this.vy+this.v_biasy)*t,this.setAngleInternal(this.a+(this.w+this.w_bias)*t),this.v_biasx=this.v_biasy=0,this.w_bias=0,this.sanityCheck()},et.prototype.resetForces=function(){this.activate(),this.f=new v(0,0),this.t=0},et.prototype.applyForce=function(t,i){this.activate(),this.f=g(this.f,t),this.t+=j(i,t)},et.prototype.applyImpulse=function(t,i){this.activate(),si(this,t.x,t.y,i)},et.prototype.getVelAtPoint=function(t){return g(new v(this.vx,this.vy),A(C(t),this.w))},et.prototype.getVelAtWorldPoint=function(t){return this.getVelAtPoint(w(t,this.p))},et.prototype.getVelAtLocalPoint=function(t){return this.getVelAtPoint(M(t,this.rot))},et.prototype.eachShape=function(t){for(var i=0,e=this.shapeList.length;i<e;i++)t(this.shapeList[i])},et.prototype.eachConstraint=function(t){for(var i=this.constraintList;i;){var e=i.next(this);t(i),i=e}},et.prototype.eachArbiter=function(t){for(var i=this.arbiterList;i;){var e=i.next(this);i.swappedColl=this===i.body_b,t(i),i=e}},et.prototype.local2World=function(t){return g(this.p,M(t,this.rot))},et.prototype.world2Local=function(t){return I(w(t,this.p),this.rot)},et.prototype.kineticEnergy=function(){var t=this.vx*this.vx+this.vy*this.vy,i=this.w*this.w;return(t?t*this.m:0)+(i?i*this.i:0)};var at=t.SpatialIndex=function(t){if(this.staticIndex=t,t){if(t.dynamicIndex)throw new Error("This static index is already associated with a dynamic index.");t.dynamicIndex=this}};at.prototype.collideStatic=function(t,i){t.count>0&&this.each(function(e){t.query(new E(e.bb_l,e.bb_b,e.bb_r,e.bb_t),function(t){i(e,t)})})};var ht=t.BBTree=function(t){at.call(this,t),this.velocityFunc=null,this.leaves={},this.count=0,this.root=null,this.pooledNodes=null,this.pooledPairs=null,this.stamp=0};ht.prototype=Object.create(at.prototype);var ct=0,pt=function(t,s,n){this.obj=null,this.bb_l=i(s.bb_l,n.bb_l),this.bb_b=i(s.bb_b,n.bb_b),this.bb_r=e(s.bb_r,n.bb_r),this.bb_t=e(s.bb_t,n.bb_t),this.parent=null,this.setA(s),this.setB(n)};ht.prototype.makeNode=function(t,i){var e=this.pooledNodes;return e?(this.pooledNodes=e.parent,e.constructor(this,t,i),e):(ct++,new pt(this,t,i))};var ut=0,lt=function(t,i){this.obj=i,t.getBB(i,this),this.parent=null,this.stamp=1,this.pairs=null,ut++};ht.prototype.getBB=function(t,s){var n=this.velocityFunc;if(n){var r=.1*(t.bb_r-t.bb_l),o=.1*(t.bb_t-t.bb_b),a=A(n(t),.1);s.bb_l=t.bb_l+i(-r,a.x),s.bb_b=t.bb_b+i(-o,a.y),s.bb_r=t.bb_r+e(r,a.x),s.bb_t=t.bb_t+e(o,a.y)}else s.bb_l=t.bb_l,s.bb_b=t.bb_b,s.bb_r=t.bb_r,s.bb_t=t.bb_t},ht.prototype.getStamp=function(){var t=this.dynamicIndex;return t&&t.stamp?t.stamp:this.stamp},ht.prototype.incrementStamp=function(){this.dynamicIndex&&this.dynamicIndex.stamp?this.dynamicIndex.stamp++:this.stamp++};var bt=0,yt=function(t,i,e,s){this.prevA=null,this.leafA=t,this.nextA=i,this.prevB=null,this.leafB=e,this.nextB=s};ht.prototype.makePair=function(t,i,e,s){var n=this.pooledPairs;return n?(this.pooledPairs=n.prevA,n.prevA=null,n.leafA=t,n.nextA=i,n.prevB=null,n.leafB=e,n.nextB=s,n):(bt++,new yt(t,i,e,s))},yt.prototype.recycle=function(t){this.prevA=t.pooledPairs,t.pooledPairs=this};var vt=function(t,i,e){e&&(e.leafA===i?e.prevA=t:e.prevB=t),t?t.leafA===i?t.nextA=e:t.nextB=e:i.pairs=e};lt.prototype.clearPairs=function(t){var i,e=this.pairs;for(this.pairs=null;e;)e.leafA===this?(i=e.nextA,vt(e.prevB,e.leafB,e.nextB)):(i=e.nextB,vt(e.prevA,e.leafA,e.nextA)),e.recycle(t),e=i};var ft=function(t,i,e){var s=t.pairs,n=i.pairs,r=e.makePair(t,s,i,n);t.pairs=i.pairs=r,s&&(s.leafA===t?s.prevA=r:s.prevB=r),n&&(n.leafA===i?n.prevA=r:n.prevB=r)};pt.prototype.recycle=function(t){this.parent=t.pooledNodes,t.pooledNodes=this},lt.prototype.recycle=function(t){},pt.prototype.setA=function(t){this.A=t,t.parent=this},pt.prototype.setB=function(t){this.B=t,t.parent=this},lt.prototype.isLeaf=!0,pt.prototype.isLeaf=!1,pt.prototype.otherChild=function(t){return this.A==t?this.B:this.A},pt.prototype.replaceChild=function(t,s,r){n(t==this.A||t==this.B,"Node is not a child of parent."),this.A==t?(this.A.recycle(r),this.setA(s)):(this.B.recycle(r),this.setB(s));for(var o=this;o;o=o.parent){var a=o.A,h=o.B;o.bb_l=i(a.bb_l,h.bb_l),o.bb_b=i(a.bb_b,h.bb_b),o.bb_r=e(a.bb_r,h.bb_r),o.bb_t=e(a.bb_t,h.bb_t)}},pt.prototype.bbArea=lt.prototype.bbArea=function(){return(this.bb_r-this.bb_l)*(this.bb_t-this.bb_b)};var dt=function(t,s){return(e(t.bb_r,s.bb_r)-i(t.bb_l,s.bb_l))*(e(t.bb_t,s.bb_t)-i(t.bb_b,s.bb_b))},_t=function(t,i){return Math.abs(t.bb_l+t.bb_r-i.bb_l-i.bb_r)+Math.abs(t.bb_b+t.bb_t-i.bb_b-i.bb_t)},xt=function(t,s,n){if(null==t)return s;if(t.isLeaf)return n.makeNode(s,t);var r=t.B.bbArea()+dt(t.A,s),o=t.A.bbArea()+dt(t.B,s);return r===o&&(r=_t(t.A,s),o=_t(t.B,s)),o<r?t.setB(xt(t.B,s,n)):t.setA(xt(t.A,s,n)),t.bb_l=i(t.bb_l,s.bb_l),t.bb_b=i(t.bb_b,s.bb_b),t.bb_r=e(t.bb_r,s.bb_r),t.bb_t=e(t.bb_t,s.bb_t),t};pt.prototype.intersectsBB=lt.prototype.intersectsBB=function(t){return this.bb_l<=t.r&&t.l<=this.bb_r&&this.bb_b<=t.t&&t.b<=this.bb_t};var mt=function(t,i,e){t.intersectsBB(i)&&(t.isLeaf?e(t.obj):(mt(t.A,i,e),mt(t.B,i,e)))},gt=function(t,s,n){var r=1/(n.x-s.x),o=t.bb_l==s.x?-1/0:(t.bb_l-s.x)*r,a=t.bb_r==s.x?1/0:(t.bb_r-s.x)*r,h=i(o,a),c=e(o,a),p=1/(n.y-s.y),u=t.bb_b==s.y?-1/0:(t.bb_b-s.y)*p,l=t.bb_t==s.y?1/0:(t.bb_t-s.y)*p,b=i(u,l),y=e(u,l);if(b<=c&&h<=y){var v=e(h,b);if(0<=i(c,y)&&v<=1)return e(v,0)}return 1/0},wt=function(t,e,s,n,r){if(t.isLeaf)return r(t.obj);var o=gt(t.A,e,s),a=gt(t.B,e,s);return o<a?(o<n&&(n=i(n,wt(t.A,e,s,n,r))),a<n&&(n=i(n,wt(t.B,e,s,n,r)))):(a<n&&(n=i(n,wt(t.B,e,s,n,r))),o<n&&(n=i(n,wt(t.A,e,s,n,r)))),n};ht.prototype.subtreeRecycle=function(t){t.isLeaf&&(this.subtreeRecycle(t.A),this.subtreeRecycle(t.B),t.recycle(this))};var St=function(t,i,e){if(i==t)return null;var s=i.parent;if(s==t){var n=t.otherChild(i);return n.parent=t.parent,t.recycle(e),n}return s.parent.replaceChild(s,s.otherChild(i),e),t},At=function(t,i){return t.bb_l<=i.bb_r&&i.bb_l<=t.bb_r&&t.bb_b<=i.bb_t&&i.bb_b<=t.bb_t};lt.prototype.markLeafQuery=function(t,i,e,s){At(t,this)&&(i?ft(t,this,e):(this.stamp<t.stamp&&ft(this,t,e),s&&s(t.obj,this.obj)))},pt.prototype.markLeafQuery=function(t,i,e,s){At(t,this)&&(this.A.markLeafQuery(t,i,e,s),this.B.markLeafQuery(t,i,e,s))},lt.prototype.markSubtree=function(t,i,e){if(this.stamp==t.getStamp()){i&&i.markLeafQuery(this,!1,t,e);for(var s=this;s.parent;s=s.parent)s==s.parent.A?s.parent.B.markLeafQuery(this,!0,t,e):s.parent.A.markLeafQuery(this,!1,t,e)}else for(var n=this.pairs;n;)this===n.leafB?(e&&e(n.leafA.obj,this.obj),n=n.nextB):n=n.nextA},pt.prototype.markSubtree=function(t,i,e){this.A.markSubtree(t,i,e),this.B.markSubtree(t,i,e)},lt.prototype.containsObj=function(t){return this.bb_l<=t.bb_l&&this.bb_r>=t.bb_r&&this.bb_b<=t.bb_b&&this.bb_t>=t.bb_t},lt.prototype.update=function(t){var i=t.root,e=this.obj;return!this.containsObj(e)&&(t.getBB(this.obj,this),i=St(i,this,t),t.root=xt(i,this,t),this.clearPairs(t),this.stamp=t.getStamp(),!0)},lt.prototype.addPairs=function(t){var i=t.dynamicIndex;if(i){var e=i.root;e&&e.markLeafQuery(this,!0,i,null)}else{var s=t.staticIndex.root;this.markSubtree(t,s,null)}},ht.prototype.insert=function(t,i){var e=new lt(this,t);this.leaves[i]=e,this.root=xt(this.root,e,this),this.count++,e.stamp=this.getStamp(),e.addPairs(this),this.incrementStamp()},ht.prototype.remove=function(t,i){var e=this.leaves[i];delete this.leaves[i],this.root=St(this.root,e,this),this.count--,e.clearPairs(this),e.recycle(this)},ht.prototype.contains=function(t,i){return null!=this.leaves[i]};var jt=function(t,i){};ht.prototype.reindexQuery=function(t){if(this.root){var i,e=this.leaves;for(i in e)e[i].update(this);var s=this.staticIndex,n=s&&s.root;this.root.markSubtree(this,n,t),s&&!n&&this.collideStatic(this,s,t),this.incrementStamp()}},ht.prototype.reindex=function(){this.reindexQuery(jt)},ht.prototype.reindexObject=function(t,i){var e=this.leaves[i];e&&(e.update(this)&&e.addPairs(this),this.incrementStamp())},ht.prototype.pointQuery=function(t,i){this.query(new E(t.x,t.y,t.x,t.y),i)},ht.prototype.segmentQuery=function(t,i,e,s){this.root&&wt(this.root,t,i,e,s)},ht.prototype.query=function(t,i){this.root&&mt(this.root,t,i)},ht.prototype.count=function(){return this.count},ht.prototype.each=function(t){var i;for(i in this.leaves)t(this.leaves[i].obj)};var Bt=function(t,s,n,r,o){return(e(t.bb_r,r)-i(t.bb_l,s))*(e(t.bb_t,o)-i(t.bb_b,n))},Ct=function(t,s,n,r){if(1==r)return s[n];if(2==r)return t.makeNode(s[n],s[n+1]);for(var o=(j=s[n]).bb_l,a=j.bb_b,h=j.bb_r,c=j.bb_t,p=n+r,u=n+1;u<p;u++)j=s[u],o=i(o,j.bb_l),a=i(a,j.bb_b),h=e(h,j.bb_r),c=e(c,j.bb_t);var l=h-o>c-a,b=new Array(2*r);if(l)for(u=n;u<p;u++)b[2*u+0]=s[u].bb_l,b[2*u+1]=s[u].bb_r;else for(u=n;u<p;u++)b[2*u+0]=s[u].bb_b,b[2*u+1]=s[u].bb_t;b.sort(function(t,i){return t-i});var y=.5*(b[r-1]+b[r]),v=o,f=a,d=h,_=c,x=o,m=a,g=h,w=c;l?d=x=y:_=m=y;for(var S=p,A=n;A<S;){j=s[A];Bt(j,x,m,g,w)<Bt(j,v,f,d,_)?(S--,s[A]=s[S],s[S]=j):A++}if(S==r){for(var j=null,u=n;u<p;u++)j=xt(j,s[u],t);return j}return NodeNew(t,Ct(t,s,n,S-n),Ct(t,s,S,p-S))};ht.prototype.optimize=function(){var t=new Array(this.count),i=0;for(var e in this.leaves)t[i++]=this.nodes[e];tree.subtreeRecycle(root),this.root=Ct(tree,t,t.length)};var kt=function(t,i){!t.isLeaf&&i<=10&&(kt(t.A,i+1),kt(t.B,i+1));for(var e="",s=0;s<i;s++)e+=" ";console.log(e+t.bb_b+" "+t.bb_t)};ht.prototype.log=function(){this.root&&kt(this.root,0)};var Mt=t.CollisionHandler=function(){this.a=this.b=0};Mt.prototype.begin=function(t,i){return!0},Mt.prototype.preSolve=function(t,i){return!0},Mt.prototype.postSolve=function(t,i){},Mt.prototype.separate=function(t,i){};var It=function(t,i){this.e=0,this.u=0,this.surface_vr=f,this.a=t,this.body_a=t.body,this.b=i,this.body_b=i.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"};It.prototype.getShapes=function(){return this.swappedColl?[this.b,this.a]:[this.a,this.b]},It.prototype.totalImpulse=function(){for(var t=this.contacts,i=new v(0,0),e=0,s=t.length;e<s;e++){var n=t[e];i.add(A(n.n,n.jnAcc))}return this.swappedColl?i:i.neg()},It.prototype.totalImpulseWithFriction=function(){for(var t=this.contacts,i=new v(0,0),e=0,s=t.length;e<s;e++){var n=t[e];i.add(new v(n.jnAcc,n.jtAcc).rotate(n.n))}return this.swappedColl?i:i.neg()},It.prototype.totalKE=function(){for(var t=(1-this.e)/(1+this.e),i=0,e=this.contacts,s=0,n=e.length;s<n;s++){var r=e[s],o=r.jnAcc,a=r.jtAcc;i+=t*o*o/r.nMass+a*a/r.tMass}return i},It.prototype.ignore=function(){this.state="ignore"},It.prototype.getA=function(){return this.swappedColl?this.b:this.a},It.prototype.getB=function(){return this.swappedColl?this.a:this.b},It.prototype.isFirstContact=function(){return"first coll"===this.state};var Pt=function(t,i,e){this.point=t,this.normal=i,this.dist=e};It.prototype.getContactPointSet=function(){var t,i=new Array(this.contacts.length);for(t=0;t<i.length;t++)i[t]=new Pt(this.contacts[t].p,this.contacts[t].n,this.contacts[t].dist);return i},It.prototype.getNormal=function(t){var i=this.contacts[t].n;return this.swappedColl?S(i):i},It.prototype.getPoint=function(t){return this.contacts[t].p},It.prototype.getDepth=function(t){return this.contacts[t].dist};var Lt=function(t,i,e,s){e?e.body_a===i?e.thread_a_next=s:e.thread_b_next=s:i.arbiterList===t&&(i.arbiterList=s),s&&(s.body_a===i?s.thread_a_prev=e:s.thread_b_prev=e)};It.prototype.unthread=function(){Lt(this,this.body_a,this.thread_a_prev,this.thread_a_next),Lt(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},It.prototype.update=function(t,i,e,s){if(this.contacts)for(var n=0;n<this.contacts.length;n++)for(var r=this.contacts[n],o=0;o<t.length;o++){var a=t[o];a.hash===r.hash&&(a.jnAcc=r.jnAcc,a.jtAcc=r.jtAcc)}this.contacts=t,this.handler=i,this.swappedColl=e.collision_type!==i.a,this.e=e.e*s.e,this.u=e.u*s.u,this.surface_vr=w(e.surface_v,s.surface_v),this.a=e,this.body_a=e.body,this.b=s,this.body_b=s.body,"cached"==this.state&&(this.state="first coll")},It.prototype.preStep=function(t,e,s){for(var n=this.body_a,r=this.body_b,o=0;o<this.contacts.length;o++){var a=this.contacts[o];a.r1=w(a.p,n.p),a.r2=w(a.p,r.p),a.nMass=1/ai(n,r,a.r1,a.r2,a.n),a.tMass=1/ai(n,r,a.r1,a.r2,C(a.n)),a.bias=-s*i(0,a.dist+e)/t,a.jBias=0,a.bounce=ei(n,r,a.r1,a.r2,a.n)*this.e}},It.prototype.applyCachedImpulse=function(t){if(!this.isFirstContact())for(var i=this.body_a,e=this.body_b,s=0;s<this.contacts.length;s++){var n=this.contacts[s],r=n.n.x,o=n.n.y,a=r*n.jnAcc-o*n.jtAcc,h=r*n.jtAcc+o*n.jnAcc;ni(i,e,n.r1,n.r2,a*t,h*t)}};var Nt=0,Ft=0;It.prototype.applyImpulse=function(){Nt++;for(var t=this.body_a,i=this.body_b,s=this.surface_vr,n=this.u,r=0;r<this.contacts.length;r++){Ft++;var o=this.contacts[r],a=o.nMass,h=o.n,c=o.r1,p=o.r2,u=i.vx-p.y*i.w-(t.vx-c.y*t.w),l=i.vy+p.x*i.w-(t.vy+c.x*t.w),y=h.x*(i.v_biasx-p.y*i.w_bias-t.v_biasx+c.y*t.w_bias)+h.y*(p.x*i.w_bias+i.v_biasy-c.x*t.w_bias-t.v_biasy),v=_(u,l,h.x,h.y),f=_(u+s.x,l+s.y,-h.y,h.x),d=(o.bias-y)*a,x=o.jBias;o.jBias=e(x+d,0);var m=-(o.bounce+v)*a,g=o.jnAcc;o.jnAcc=e(g+m,0);var w=n*o.jnAcc,S=-f*o.tMass,A=o.jtAcc;o.jtAcc=b(A+S,-w,w);var j=h.x*(o.jBias-x),B=h.y*(o.jBias-x);ri(t,-j,-B,c),ri(i,j,B,p);var C=o.jnAcc-g,k=o.jtAcc-A;ni(t,i,c,p,h.x*C-h.y*k,h.x*k+h.y*C)}},It.prototype.callSeparate=function(t){t.lookupHandler(this.a.collision_type,this.b.collision_type).separate(this,t)},It.prototype.next=function(t){return this.body_a==t?this.thread_a_next:this.thread_b_next};var Qt=0,Tt=function(t,i,e,s){this.p=t,this.n=i,this.dist=e,this.r1=this.r2=f,this.nMass=this.tMass=this.bounce=this.bias=0,this.jnAcc=this.jtAcc=this.jBias=0,this.hash=s,Qt++},Rt=[],Vt=function(t,i,e,s){var n=e+s,r=w(i,t),o=P(r);if(!(o>=n*n)){var a=Math.sqrt(o);return new Tt(g(t,A(r,.5+(e-.5*n)/(a||1/0))),a?A(r,1/a):new v(1,0),a-n,0)}},Ot=0,qt=function(t,i){var e=0,s=t.valueOnAxis(i[0].n,i[0].d);if(s>0)return-1;for(var n=1;n<i.length;n++){var r=t.valueOnAxis(i[n].n,i[n].d);if(r>0)return-1;r>s&&(s=r,e=n)}return Ot=s,e},Et=function(t,i,e,s){for(var n=[],o=t.tVerts,a=0;a<o.length;a+=2){var h=o[a],c=o[a+1];i.containsVertPartial(h,c,S(e))&&n.push(new Tt(new v(h,c),e,s,r(t.hashid,a)))}for(var p=i.tVerts,a=0;a<p.length;a+=2){var h=p[a],c=p[a+1];t.containsVertPartial(h,c,e)&&n.push(new Tt(new v(h,c),e,s,r(i.hashid,a)))}return n},Ht=function(t,i,e,s){for(var n=[],o=t.tVerts,a=0;a<o.length;a+=2){var h=o[a],c=o[a+1];i.containsVert(h,c)&&n.push(new Tt(new v(h,c),e,s,r(t.hashid,a>>1)))}for(var p=i.tVerts,a=0;a<p.length;a+=2){var h=p[a],c=p[a+1];t.containsVert(h,c)&&n.push(new Tt(new v(h,c),e,s,r(i.hashid,a>>1)))}return n.length?n:Et(t,i,e,s)},Dt=function(t,e,s){var n=d(e,t.ta)-t.r,r=d(e,t.tb)-t.r;return i(n,r)-s},Gt=function(t,i,e,s,n){for(var o=j(i.tn,i.ta),a=j(i.tn,i.tb),h=A(i.tn,n),c=e.tVerts,p=0;p<c.length;p+=2){var u=c[p],l=c[p+1];if(_(u,l,h.x,h.y)<d(i.tn,i.ta)*n+i.r){var b=B(i.tn.x,i.tn.y,u,l);o>=b&&b>=a&&t.push(new Tt(new v(u,l),h,s,r(e.hashid,p)))}}};Y.prototype.collisionCode=0,X.prototype.collisionCode=1,$.prototype.collisionCode=2,Y.prototype.collisionTable=[function(t,i){var e=Vt(t.tc,i.tc,t.r,i.r);return e?[e]:Rt},function(t,i){var e=i.ta,s=i.tb,n=t.tc,r=w(s,e),o=y(d(r,w(n,e))/P(r)),a=g(e,A(r,o)),h=Vt(n,a,t.r,i.r);if(h){var c=h.n;return 0===o&&d(c,i.a_tangent)<0||1===o&&d(c,i.b_tangent)<0?Rt:[h]}return Rt},function(t,i){for(var e=i.tPlanes,s=0,n=d(e[0].n,t.tc)-e[0].d-t.r,r=0;r<e.length;r++){var o=d(e[r].n,t.tc)-e[r].d-t.r;if(o>0)return Rt;o>n&&(n=o,s=r)}var a=e[s].n,h=i.tVerts,c=h.length,p=s<<1,u=h[p],l=h[p+1],b=h[(p+2)%c],y=h[(p+3)%c],f=B(a.x,a.y,u,l),_=B(a.x,a.y,b,y),x=j(a,t.tc);if(x<_)return(m=Vt(t.tc,new v(b,y),t.r,0))?[m]:Rt;if(x<f)return[new Tt(w(t.tc,A(a,t.r+n/2)),S(a),n,0)];var m=Vt(t.tc,new v(u,l),t.r,0);return m?[m]:Rt}],X.prototype.collisionTable=[null,function(t,i){return Rt},function(t,i){var e=[],s=i.tPlanes,n=s.length,o=d(t.tn,t.ta),a=i.valueOnAxis(t.tn,o)-t.r,h=i.valueOnAxis(S(t.tn),-o)-t.r;if(h>0||a>0)return Rt;var c=0,p=Dt(t,s[0].n,s[0].d);if(p>0)return Rt;for(var u=0;u<n;u++){var l=Dt(t,s[u].n,s[u].d);if(l>0)return Rt;l>p&&(p=l,c=u)}var b=S(s[c].n),y=g(t.ta,A(b,t.r)),f=g(t.tb,A(b,t.r));if(i.containsVert(y.x,y.y)&&e.push(new Tt(y,b,p,r(t.hashid,0))),i.containsVert(f.x,f.y)&&e.push(new Tt(f,b,p,r(t.hashid,1))),(a>=p||h>=p)&&(a>h?Gt(e,t,i,a,1):Gt(e,t,i,h,-1)),0===e.length){var _,x=2*c,m=i.tVerts,w=new v(m[x],m[x+1]);if(_=Vt(t.ta,w,t.r,0))return[_];if(_=Vt(t.tb,w,t.r,0))return[_];var j=2*n,B=new v(m[(x+2)%j],m[(x+3)%j]);if(_=Vt(t.ta,B,t.r,0))return[_];if(_=Vt(t.tb,B,t.r,0))return[_]}return e}],$.prototype.collisionTable=[null,null,function(t,i){var e=qt(i,t.tPlanes);if(-1==e)return Rt;var s=Ot,n=qt(t,i.tPlanes);if(-1==n)return Rt;var r=Ot;return s>r?Ht(t,i,t.tPlanes[e].n,s):Ht(t,i,S(i.tPlanes[n].n),r)}];var Wt=t.collideShapes=function(t,i){return s(t.collisionCode<=i.collisionCode,"Collided shapes must be sorted by type"),t.collisionTable[i.collisionCode](t,i)},zt=new Mt,Jt=t.Space=function(){this.stamp=0,this.curr_dt=0,this.bodies=[],this.rousedBodies=[],this.sleepingComponents=[],this.staticShapes=new ht(null),this.activeShapes=new ht(this.staticShapes),this.arbiters=[],this.contactBuffersHead=null,this.cachedArbiters={},this.constraints=[],this.locked=0,this.collisionHandlers={},this.defaultHandler=zt,this.postStepCallbacks=[],this.iterations=10,this.gravity=f,this.damping=1,this.idleSpeedThreshold=0,this.sleepTimeThreshold=1/0,this.collisionSlop=.1,this.collisionBias=Math.pow(.9,60),this.collisionPersistence=3,this.enableContactGraph=!1,this.staticBody=new et(1/0,1/0),this.staticBody.nodeIdleTime=1/0,this.collideShapes=this.makeCollideShapes()};Jt.prototype.getCurrentTimeStep=function(){return this.curr_dt},Jt.prototype.setIterations=function(t){this.iterations=t},Jt.prototype.isLocked=function(){return this.locked};var Ut=function(t){s(!t.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.")};Jt.prototype.addCollisionHandler=function(t,i,e,s,n,o){Ut(this),this.removeCollisionHandler(t,i);var a=new Mt;a.a=t,a.b=i,e&&(a.begin=e),s&&(a.preSolve=s),n&&(a.postSolve=n),o&&(a.separate=o),this.collisionHandlers[r(t,i)]=a},Jt.prototype.removeCollisionHandler=function(t,i){Ut(this),delete this.collisionHandlers[r(t,i)]},Jt.prototype.setDefaultCollisionHandler=function(t,i,e,s){Ut(this);var n=new Mt;t&&(n.begin=t),i&&(n.preSolve=i),e&&(n.postSolve=e),s&&(n.separate=s),this.defaultHandler=n},Jt.prototype.lookupHandler=function(t,i){return this.collisionHandlers[r(t,i)]||this.defaultHandler},Jt.prototype.addShape=function(t){var i=t.body;return i.isStatic()?this.addStaticShape(t):(s(!t.space,"This shape is already added to a space and cannot be added to another."),Ut(this),i.activate(),i.addShape(t),t.update(i.p,i.rot),this.activeShapes.insert(t,t.hashid),t.space=this,t)},Jt.prototype.addStaticShape=function(t){s(!t.space,"This shape is already added to a space and cannot be added to another."),Ut(this);var i=t.body;return i.addShape(t),t.update(i.p,i.rot),this.staticShapes.insert(t,t.hashid),t.space=this,t},Jt.prototype.addBody=function(t){return s(!t.isStatic(),"Static bodies cannot be added to a space as they are not meant to be simulated."),s(!t.space,"This body is already added to a space and cannot be added to another."),Ut(this),this.bodies.push(t),t.space=this,t},Jt.prototype.addConstraint=function(t){s(!t.space,"This shape is already added to a space and cannot be added to another."),Ut(this);var i=t.a,e=t.b;return i.activate(),e.activate(),this.constraints.push(t),t.next_a=i.constraintList,i.constraintList=t,t.next_b=e.constraintList,e.constraintList=t,t.space=this,t},Jt.prototype.filterArbiters=function(t,i){for(var e in this.cachedArbiters){var s=this.cachedArbiters[e];(t!==s.body_a||i!==s.a&&null!==i)&&(t!==s.body_b||i!==s.b&&null!==i)||(i&&"cached"!==s.state&&s.callSeparate(this),s.unthread(),o(this.arbiters,s),delete this.cachedArbiters[e])}},Jt.prototype.removeShape=function(t){var i=t.body;i.isStatic()?this.removeStaticShape(t):(s(this.containsShape(t),"Cannot remove a shape that was not added to the space. (Removed twice maybe?)"),Ut(this),i.activate(),i.removeShape(t),this.filterArbiters(i,t),this.activeShapes.remove(t,t.hashid),t.space=null)},Jt.prototype.removeStaticShape=function(t){s(this.containsShape(t),"Cannot remove a static or sleeping shape that was not added to the space. (Removed twice maybe?)"),Ut(this);var i=t.body;i.isStatic()&&i.activateStatic(t),i.removeShape(t),this.filterArbiters(i,t),this.staticShapes.remove(t,t.hashid),t.space=null},Jt.prototype.removeBody=function(t){s(this.containsBody(t),"Cannot remove a body that was not added to the space. (Removed twice maybe?)"),Ut(this),t.activate(),o(this.bodies,t),t.space=null},Jt.prototype.removeConstraint=function(t){s(this.containsConstraint(t),"Cannot remove a constraint that was not added to the space. (Removed twice maybe?)"),Ut(this),t.a.activate(),t.b.activate(),o(this.constraints,t),t.a.removeConstraint(t),t.b.removeConstraint(t),t.space=null},Jt.prototype.containsShape=function(t){return t.space===this},Jt.prototype.containsBody=function(t){return t.space==this},Jt.prototype.containsConstraint=function(t){return t.space==this},Jt.prototype.uncacheArbiter=function(t){delete this.cachedArbiters[r(t.a.hashid,t.b.hashid)],o(this.arbiters,t)},Jt.prototype.eachBody=function(t){this.lock();for(var i=this.bodies,e=0;e<i.length;e++)t(i[e]);for(var s=this.sleepingComponents,e=0;e<s.length;e++)for(var n=s[e];n;){var r=n.nodeNext;t(n),n=r}this.unlock(!0)},Jt.prototype.eachShape=function(t){this.lock(),this.activeShapes.each(t),this.staticShapes.each(t),this.unlock(!0)},Jt.prototype.eachConstraint=function(t){this.lock();for(var i=this.constraints,e=0;e<i.length;e++)t(i[e]);this.unlock(!0)},Jt.prototype.reindexStatic=function(){s(!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(t){var i=t.body;t.update(i.p,i.rot)}),this.staticShapes.reindex()},Jt.prototype.reindexShape=function(t){s(!this.locked,"You cannot manually reindex objects while the space is locked. Wait until the current query or step is complete.");var i=t.body;t.update(i.p,i.rot),this.activeShapes.reindexObject(t,t.hashid),this.staticShapes.reindexObject(t,t.hashid)},Jt.prototype.reindexShapesForBody=function(t){for(var i=t.shapeList;i;i=i.next)this.reindexShape(i)},Jt.prototype.useSpatialHash=function(t,i){throw new Error("Spatial Hash not implemented.")},Jt.prototype.activateBody=function(t){if(s(!t.isRogue(),"Internal error: Attempting to activate a rogue body."),this.locked)-1===this.rousedBodies.indexOf(t)&&this.rousedBodies.push(t);else{this.bodies.push(t);for(var i=0;i<t.shapeList.length;i++){var e=t.shapeList[i];this.staticShapes.remove(e,e.hashid),this.activeShapes.insert(e,e.hashid)}for(var n=t.arbiterList;n;n=n.next(t))if(t===(c=n.body_a)||c.isStatic()){var o=n.a,a=n.b;this.cachedArbiters[r(o.hashid,a.hashid)]=n,n.stamp=this.stamp,n.handler=this.lookupHandler(o.collision_type,a.collision_type),this.arbiters.push(n)}for(var h=t.constraintList;h;h=h.nodeNext){var c=h.a;(t===c||c.isStatic())&&this.constraints.push(h)}}},Jt.prototype.deactivateBody=function(t){s(!t.isRogue(),"Internal error: Attempting to deactivate a rogue body."),o(this.bodies,t);for(var i=0;i<t.shapeList.length;i++){var e=t.shapeList[i];this.activeShapes.remove(e,e.hashid),this.staticShapes.insert(e,e.hashid)}for(var n=t.arbiterList;n;n=n.next(t))(t===(a=n.body_a)||a.isStatic())&&this.uncacheArbiter(n);for(var r=t.constraintList;r;r=r.nodeNext){var a=r.a;(t===a||a.isStatic())&&o(this.constraints,r)}};var Yt=function(t){return t?t.nodeRoot:null},Kt=function(t){if(t&&t.isSleeping(t)){s(!t.isRogue(),"Internal Error: componentActivate() called on a rogue body.");for(var i=t.space,e=t;e;){var n=e.nodeNext;e.nodeIdleTime=0,e.nodeRoot=null,e.nodeNext=null,i.activateBody(e),e=n}o(i.sleepingComponents,t)}};et.prototype.activate=function(){this.isRogue()||(this.nodeIdleTime=0,Kt(Yt(this)))},et.prototype.activateStatic=function(t){s(this.isStatic(),"Body.activateStatic() called on a non-static body.");for(var i=this.arbiterList;i;i=i.next(this))t&&t!=i.a&&t!=i.b||(i.body_a==this?i.body_b:i.body_a).activate()},et.prototype.pushArbiter=function(t){n(null===(t.body_a===this?t.thread_a_next:t.thread_b_next),"Internal Error: Dangling contact graph pointers detected. (A)"),n(null===(t.body_a===this?t.thread_a_prev:t.thread_b_prev),"Internal Error: Dangling contact graph pointers detected. (B)");var i=this.arbiterList;n(null===i||null===(i.body_a===this?i.thread_a_prev:i.thread_b_prev),"Internal Error: Dangling contact graph pointers detected. (C)"),t.body_a===this?t.thread_a_next=i:t.thread_b_next=i,i&&(i.body_a===this?i.thread_a_prev=t:i.thread_b_prev=t),this.arbiterList=t};var Xt=function(t,i){i.nodeRoot=t,i!==t&&(i.nodeNext=t.nodeNext,t.nodeNext=i)},Zt=function(t,i){if(!i.isRogue()){var e=Yt(i);if(null==e){Xt(t,i);for(var s=i.arbiterList;s;s=s.next(i))Zt(t,i==s.body_a?s.body_b:s.body_a);for(var r=i.constraintList;r;r=r.next(i))Zt(t,i==r.a?r.b:r.a)}else n(e===t,"Internal Error: Inconsistency detected in the contact graph.")}},$t=function(t,i){for(var e=t;e;e=e.nodeNext)if(e.nodeIdleTime<i)return!0;return!1};Jt.prototype.processComponents=function(t){for(var i=this.sleepTimeThreshold!==1/0,e=this.bodies,s=0;s<e.length;s++){a=e[s];n(null===a.nodeNext,"Internal Error: Dangling next pointer detected in contact graph."),n(null===a.nodeRoot,"Internal Error: Dangling root pointer detected in contact graph.")}if(i)for(var r=this.idleSpeedThreshold,o=r?r*r:P(this.gravity)*t*t,s=0;s<e.length;s++){var a=e[s],h=o?a.m*o:0;a.nodeIdleTime=a.kineticEnergy()>h?0:a.nodeIdleTime+t}for(var c=this.arbiters,s=0,p=c.length;s<p;s++){var u=c[s],l=u.body_a,b=u.body_b;i&&((b.isRogue()&&!b.isStatic()||l.isSleeping())&&l.activate(),(l.isRogue()&&!l.isStatic()||b.isSleeping())&&b.activate()),l.pushArbiter(u),b.pushArbiter(u)}if(i){for(var y=this.constraints,s=0;s<y.length;s++){var v=y[s],l=v.a;(b=v.b).isRogue()&&!b.isStatic()&&l.activate(),l.isRogue()&&!l.isStatic()&&b.activate()}for(s=0;s<e.length;){a=e[s];if(null!==Yt(a)||(Zt(a,a),$t(a,this.sleepTimeThreshold)))s++,a.nodeRoot=null,a.nodeNext=null;else{this.sleepingComponents.push(a);for(var f=a;f;f=f.nodeNext)this.deactivateBody(f)}}}},et.prototype.sleep=function(){this.sleepWithGroup(null)},et.prototype.sleepWithGroup=function(t){s(!this.isStatic()&&!this.isRogue(),"Rogue and static bodies cannot be put to sleep.");var i=this.space;if(s(i,"Cannot put a rogue body to sleep."),s(!i.locked,"Bodies cannot be put to sleep during a query or a call to cpSpaceStep(). Put these calls into a post-step callback."),s(null===t||t.isSleeping(),"Cannot use a non-sleeping body as a group identifier."),this.isSleeping())s(Yt(this)===Yt(t),"The body is already sleeping and it's group cannot be reassigned.");else{for(var e=0;e<this.shapeList.length;e++)this.shapeList[e].update(this.p,this.rot);if(i.deactivateBody(this),t){var n=Yt(t);this.nodeRoot=n,this.nodeNext=n.nodeNext,this.nodeIdleTime=0,n.nodeNext=this}else this.nodeRoot=this,this.nodeNext=null,this.nodeIdleTime=0,i.sleepingComponents.push(this);o(i.bodies,this)}},Jt.prototype.activateShapesTouchingShape=function(t){this.sleepTimeThreshold!==1/0&&this.shapeQuery(t,function(t,i){t.body.activate()})},Jt.prototype.pointQuery=function(t,i,e,s){var n=function(n){(!n.group||e!==n.group)&&i&n.layers&&n.pointQuery(t)&&s(n)},r=new E(t.x,t.y,t.x,t.y);this.lock(),this.activeShapes.query(r,n),this.staticShapes.query(r,n),this.unlock(!0)},Jt.prototype.pointQueryFirst=function(t,i,e){var s=null;return this.pointQuery(t,i,e,function(t){t.sensor||(s=t)}),s},Jt.prototype.nearestPointQuery=function(t,i,e,s,n){var r=function(r){if((!r.group||s!==r.group)&&e&r.layers){var o=r.nearestPointQuery(t);o.d<i&&n(r,o.d,o.p)}},o=H(t,i);this.lock(),this.activeShapes.query(o,r),this.staticShapes.query(o,r),this.unlock(!0)},Jt.prototype.nearestPointQueryNearest=function(t,i,e,s){var n,r=function(r){if((!r.group||s!==r.group)&&e&r.layers&&!r.sensor){var o=r.nearestPointQuery(t);o.d<i&&(!n||o.d<n.d)&&(n=o)}},o=H(t,i);return this.activeShapes.query(o,r),this.staticShapes.query(o,r),n},Jt.prototype.segmentQuery=function(t,i,e,s,n){var r=function(r){var o;return(!r.group||s!==r.group)&&e&r.layers&&(o=r.segmentQuery(t,i))&&n(r,o.t,o.n),1};this.lock(),this.staticShapes.segmentQuery(t,i,1,r),this.activeShapes.segmentQuery(t,i,1,r),this.unlock(!0)},Jt.prototype.segmentQueryFirst=function(t,i,e,s){var n=null,r=function(r){var o;return(!r.group||s!==r.group)&&e&r.layers&&!r.sensor&&(o=r.segmentQuery(t,i))&&(null===n||o.t<n.t)&&(n=o),n?n.t:1};return this.staticShapes.segmentQuery(t,i,1,r),this.activeShapes.segmentQuery(t,i,n?n.t:1,r),n},Jt.prototype.bbQuery=function(t,i,e,s){var n=function(n){(!n.group||e!==n.group)&&i&n.layers&&D(t,n.bb_l,n.bb_b,n.bb_r,n.bb_t)&&s(n)};this.lock(),this.activeShapes.query(t,n),this.staticShapes.query(t,n),this.unlock(!0)},Jt.prototype.shapeQuery=function(t,i){var e=t.body;e&&t.update(e.p,e.rot);var s=new E(t.bb_l,t.bb_b,t.bb_r,t.bb_t),n=!1,r=function(e){var s=t;if((!s.group||s.group!==e.group)&&s.layers&e.layers&&s!==e){var r;if(s.collisionCode<=e.collisionCode)r=Wt(s,e);else{r=Wt(e,s);for(a=0;a<r.length;a++)r[a].n=S(r[a].n)}if(r.length&&(n=!(s.sensor||e.sensor),i)){for(var o=new Array(r.length),a=0;a<r.length;a++)o[a]=new Pt(r[a].p,r[a].n,r[a].dist);i(e,o)}}};return this.lock(),this.activeShapes.query(s,r),this.staticShapes.query(s,r),this.unlock(!0),n},Jt.prototype.addPostStepCallback=function(t){n(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(t)},Jt.prototype.runPostStepCallbacks=function(){for(var t=0;t<this.postStepCallbacks.length;t++)this.postStepCallbacks[t]();this.postStepCallbacks=[]},Jt.prototype.lock=function(){this.locked++},Jt.prototype.unlock=function(t){if(this.locked--,s(this.locked>=0,"Internal Error: Space lock underflow."),0===this.locked&&t){for(var i=this.rousedBodies,e=0;e<i.length;e++)this.activateBody(i[e]);i.length=0,this.runPostStepCallbacks()}},Jt.prototype.makeCollideShapes=function(){var t=this;return function(i,e){var s=t;if(i.bb_l<=e.bb_r&&e.bb_l<=i.bb_r&&i.bb_b<=e.bb_t&&e.bb_b<=i.bb_t&&i.body!==e.body&&(!i.group||i.group!==e.group)&&i.layers&e.layers){var n=s.lookupHandler(i.collision_type,e.collision_type),o=i.sensor||e.sensor;if(!o||n!==zt){if(i.collisionCode>e.collisionCode){var a=i;i=e,e=a}var h=Wt(i,e);if(0!==h.length){var c=r(i.hashid,e.hashid),p=s.cachedArbiters[c];p||(p=s.cachedArbiters[c]=new It(i,e)),p.update(h,n,i,e),"first coll"!=p.state||n.begin(p,s)||p.ignore(),"ignore"!==p.state&&n.preSolve(p,s)&&!o?s.arbiters.push(p):(p.contacts=null,"ignore"!==p.state&&(p.state="normal")),p.stamp=s.stamp}}}}},Jt.prototype.arbiterSetFilter=function(t){var i=this.stamp-t.stamp,e=t.body_a,s=t.body_b;return!(!e.isStatic()&&!e.isSleeping()||!s.isStatic()&&!s.isSleeping())||(i>=1&&"cached"!=t.state&&(t.callSeparate(this),t.state="cached"),!(i>=this.collisionPersistence)||(t.contacts=null,!1))};var ti=function(t){var i=t.body;t.update(i.p,i.rot)};Jt.prototype.step=function(t){if(0!==t){s(0===f.x&&0===f.y,"vzero is invalid"),this.stamp++;var i=this.curr_dt;this.curr_dt=t;var e,n,r,o=this.bodies,a=this.constraints,h=this.arbiters;for(e=0;e<h.length;e++){var c=h[e];c.state="normal",c.body_a.isSleeping()||c.body_b.isSleeping()||c.unthread()}for(h.length=0,this.lock(),e=0;e<o.length;e++)o[e].position_func(t);this.activeShapes.each(ti),this.activeShapes.reindexQuery(this.collideShapes),this.unlock(!1),this.processComponents(t),this.lock();for(r in this.cachedArbiters)this.arbiterSetFilter(this.cachedArbiters[r])||delete this.cachedArbiters[r];var p=this.collisionSlop,u=1-Math.pow(this.collisionBias,t);for(e=0;e<h.length;e++)h[e].preStep(t,p,u);for(e=0;e<a.length;e++){var l=a[e];l.preSolve(this),l.preStep(t)}var b=Math.pow(this.damping,t),y=this.gravity;for(e=0;e<o.length;e++)o[e].velocity_func(y,b,t);var v=0===i?0:t/i;for(e=0;e<h.length;e++)h[e].applyCachedImpulse(v);for(e=0;e<a.length;e++)a[e].applyCachedImpulse(v);for(e=0;e<this.iterations;e++){for(n=0;n<h.length;n++)h[n].applyImpulse();for(n=0;n<a.length;n++)a[n].applyImpulse()}for(e=0;e<a.length;e++)a[e].postSolve(this);for(e=0;e<h.length;e++)h[e].handler.postSolve(h[e],this);this.unlock(!0)}};var ii=function(t,i,e,s){var n=t.vx+-e.y*t.w,r=t.vy+e.x*t.w,o=i.vx+-s.y*i.w,a=i.vy+s.x*i.w;return new v(o-n,a-r)},ei=function(t,i,e,s,n){var r=t.vx+-e.y*t.w,o=t.vy+e.x*t.w,a=i.vx+-s.y*i.w,h=i.vy+s.x*i.w;return _(a-r,h-o,n.x,n.y)},si=function(t,i,e,s){t.vx+=i*t.m_inv,t.vy+=e*t.m_inv,t.w+=t.i_inv*(s.x*e-s.y*i)},ni=function(t,i,e,s,n,r){si(t,-n,-r,e),si(i,n,r,s)},ri=function(t,i,e,s){t.v_biasx+=i*t.m_inv,t.v_biasy+=e*t.m_inv,t.w_bias+=t.i_inv*B(s.x,s.y,i,e)},oi=function(t,i,e){var s=j(i,e);return t.m_inv+t.i_inv*s*s},ai=function(t,i,e,s,r){var o=oi(t,e,r)+oi(i,s,r);return n(0!==o,"Unsolvable collision or constraint."),o},hi=function(t,i,e,s,r,o){var a,h,c,p,u=t.m_inv+i.m_inv;a=u,h=0,c=0,p=u;var l=t.i_inv,b=e.x*e.x*l,y=e.y*e.y*l,v=-e.x*e.y*l;a+=y,h+=v,c+=v,p+=b;var f=i.i_inv,d=s.x*s.x*f,_=s.y*s.y*f,x=-s.x*s.y*f,m=(a+=_)*(p+=d)-(h+=x)*(c+=x);n(0!==m,"Unsolvable constraint.");var g=1/m;r.x=p*g,r.y=-h*g,o.x=-c*g,o.y=a*g},ci=function(t,i,e){return new v(d(t,i),d(t,e))},pi=function(t,i){return 1-Math.pow(t,i)},ui=t.Constraint=function(t,i){this.a=t,this.b=i,this.space=null,this.next_a=null,this.next_b=null,this.maxForce=1/0,this.errorBias=Math.pow(.9,60),this.maxBias=1/0};ui.prototype.activateBodies=function(){this.a&&this.a.activate(),this.b&&this.b.activate()},ui.prototype.preStep=function(t){},ui.prototype.applyCachedImpulse=function(t){},ui.prototype.applyImpulse=function(){},ui.prototype.getImpulse=function(){return 0},ui.prototype.preSolve=function(t){},ui.prototype.postSolve=function(t){},ui.prototype.next=function(t){return this.a===t?this.next_a:this.next_b};var li=t.PinJoint=function(t,i,e,s){ui.call(this,t,i),this.anchr1=e,this.anchr2=s;var r=t?g(t.p,M(e,t.rot)):e,o=i?g(i.p,M(s,i.rot)):s;this.dist=x(w(o,r)),n(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};li.prototype=Object.create(ui.prototype),li.prototype.preStep=function(t){var i=this.a,e=this.b;this.r1=M(this.anchr1,i.rot),this.r2=M(this.anchr2,e.rot);var s=w(g(e.p,this.r2),g(i.p,this.r1)),n=x(s);this.n=A(s,1/(n||1/0)),this.nMass=1/ai(i,e,this.r1,this.r2,this.n);var r=this.maxBias;this.bias=b(-pi(this.errorBias,t)*(n-this.dist)/t,-r,r),this.jnMax=this.maxForce*t},li.prototype.applyCachedImpulse=function(t){var i=A(this.n,this.jnAcc*t);ni(this.a,this.b,this.r1,this.r2,i.x,i.y)},li.prototype.applyImpulse=function(){var t=this.a,i=this.b,e=this.n,s=ei(t,i,this.r1,this.r2,e),n=(this.bias-s)*this.nMass,r=this.jnAcc;this.jnAcc=b(r+n,-this.jnMax,this.jnMax),n=this.jnAcc-r,ni(t,i,this.r1,this.r2,e.x*n,e.y*n)},li.prototype.getImpulse=function(){return Math.abs(this.jnAcc)};var bi=t.SlideJoint=function(t,i,e,s,n,r){ui.call(this,t,i),this.anchr1=e,this.anchr2=s,this.min=n,this.max=r,this.r1=this.r2=this.n=null,this.nMass=0,this.jnAcc=this.jnMax=0,this.bias=0};bi.prototype=Object.create(ui.prototype),bi.prototype.preStep=function(t){var i=this.a,e=this.b;this.r1=M(this.anchr1,i.rot),this.r2=M(this.anchr2,e.rot);var s=w(g(e.p,this.r2),g(i.p,this.r1)),n=x(s),r=0;n>this.max?(r=n-this.max,this.n=Q(s)):n<this.min?(r=this.min-n,this.n=S(Q(s))):(this.n=f,this.jnAcc=0),this.nMass=1/ai(i,e,this.r1,this.r2,this.n);var o=this.maxBias;this.bias=b(-pi(this.errorBias,t)*r/t,-o,o),this.jnMax=this.maxForce*t},bi.prototype.applyCachedImpulse=function(t){var i=this.jnAcc*t;ni(this.a,this.b,this.r1,this.r2,this.n.x*i,this.n.y*i)},bi.prototype.applyImpulse=function(){if(0!==this.n.x||0!==this.n.y){var t=this.a,i=this.b,e=this.n,s=this.r1,n=this.r2,r=ii(t,i,s,n),o=d(r,e),a=(this.bias-o)*this.nMass,h=this.jnAcc;this.jnAcc=b(h+a,-this.jnMax,0),a=this.jnAcc-h,ni(t,i,this.r1,this.r2,e.x*a,e.y*a)}},bi.prototype.getImpulse=function(){return Math.abs(this.jnAcc)};var yi=t.PivotJoint=function(t,i,e,s){if(ui.call(this,t,i),void 0===s){var n=e;e=t?t.world2Local(n):n,s=i?i.world2Local(n):n}this.anchr1=e,this.anchr2=s,this.r1=this.r2=f,this.k1=new v(0,0),this.k2=new v(0,0),this.jAcc=f,this.jMaxLen=0,this.bias=f};yi.prototype=Object.create(ui.prototype),yi.prototype.preStep=function(t){var i=this.a,e=this.b;this.r1=M(this.anchr1,i.rot),this.r2=M(this.anchr2,e.rot),hi(i,e,this.r1,this.r2,this.k1,this.k2),this.jMaxLen=this.maxForce*t;var s=w(g(e.p,this.r2),g(i.p,this.r1));this.bias=T(A(s,-pi(this.errorBias,t)/t),this.maxBias)},yi.prototype.applyCachedImpulse=function(t){ni(this.a,this.b,this.r1,this.r2,this.jAcc.x*t,this.jAcc.y*t)},yi.prototype.applyImpulse=function(){var t=this.a,i=this.b,e=this.r1,s=this.r2,n=ii(t,i,e,s),r=ci(w(this.bias,n),this.k1,this.k2),o=this.jAcc;this.jAcc=T(g(this.jAcc,r),this.jMaxLen),ni(t,i,this.r1,this.r2,this.jAcc.x-o.x,this.jAcc.y-o.y)},yi.prototype.getImpulse=function(){return x(this.jAcc)};var vi=t.GrooveJoint=function(t,i,e,s,n){ui.call(this,t,i),this.grv_a=e,this.grv_b=s,this.grv_n=C(F(w(s,e))),this.anchr2=n,this.grv_tn=null,this.clamp=0,this.r1=this.r2=null,this.k1=new v(0,0),this.k2=new v(0,0),this.jAcc=f,this.jMaxLen=0,this.bias=null};vi.prototype=Object.create(ui.prototype),vi.prototype.preStep=function(t){var i=this.a,e=this.b,s=i.local2World(this.grv_a),n=i.local2World(this.grv_b),r=M(this.grv_n,i.rot),o=d(s,r);this.grv_tn=r,this.r2=M(this.anchr2,e.rot);var a=j(g(e.p,this.r2),r);a<=j(s,r)?(this.clamp=1,this.r1=w(s,i.p)):a>=j(n,r)?(this.clamp=-1,this.r1=w(n,i.p)):(this.clamp=0,this.r1=w(g(A(C(r),-a),A(r,o)),i.p)),hi(i,e,this.r1,this.r2,this.k1,this.k2),this.jMaxLen=this.maxForce*t;var h=w(g(e.p,this.r2),g(i.p,this.r1));this.bias=T(A(h,-pi(this.errorBias,t)/t),this.maxBias)},vi.prototype.applyCachedImpulse=function(t){ni(this.a,this.b,this.r1,this.r2,this.jAcc.x*t,this.jAcc.y*t)},vi.prototype.grooveConstrain=function(t){var i=this.grv_tn,e=this.clamp*j(t,i)>0?t:k(t,i);return T(e,this.jMaxLen)},vi.prototype.applyImpulse=function(){var t=this.a,i=this.b,e=this.r1,s=this.r2,n=ii(t,i,e,s),r=ci(w(this.bias,n),this.k1,this.k2),o=this.jAcc;this.jAcc=this.grooveConstrain(g(o,r)),ni(t,i,this.r1,this.r2,this.jAcc.x-o.x,this.jAcc.y-o.y)},vi.prototype.getImpulse=function(){return x(this.jAcc)},vi.prototype.setGrooveA=function(t){this.grv_a=t,this.grv_n=C(F(w(this.grv_b,t))),this.activateBodies()},vi.prototype.setGrooveB=function(t){this.grv_b=t,this.grv_n=C(F(w(t,this.grv_a))),this.activateBodies()};var fi=function(t,i){return(t.restLength-i)*t.stiffness},di=t.DampedSpring=function(t,i,e,s,n,r,o){ui.call(this,t,i),this.anchr1=e,this.anchr2=s,this.restLength=n,this.stiffness=r,this.damping=o,this.springForceFunc=fi,this.target_vrn=this.v_coef=0,this.r1=this.r2=null,this.nMass=0,this.n=null};di.prototype=Object.create(ui.prototype),di.prototype.preStep=function(t){var i=this.a,e=this.b;this.r1=M(this.anchr1,i.rot),this.r2=M(this.anchr2,e.rot);var s=w(g(e.p,this.r2),g(i.p,this.r1)),r=x(s);this.n=A(s,1/(r||1/0));var o=ai(i,e,this.r1,this.r2,this.n);n(0!==o,"Unsolvable this."),this.nMass=1/o,this.target_vrn=0,this.v_coef=1-Math.exp(-this.damping*t*o);var a=this.springForceFunc(this,r);ni(i,e,this.r1,this.r2,this.n.x*a*t,this.n.y*a*t)},di.prototype.applyCachedImpulse=function(t){},di.prototype.applyImpulse=function(){var t=this.a,i=this.b,e=this.n,s=this.r1,n=this.r2,r=ei(t,i,s,n,e),o=(this.target_vrn-r)*this.v_coef;this.target_vrn=r+o,o*=this.nMass,ni(t,i,this.r1,this.r2,this.n.x*o,this.n.y*o)},di.prototype.getImpulse=function(){return 0};var _i=function(t,i){return(i-t.restAngle)*t.stiffness},xi=t.DampedRotarySpring=function(t,i,e,s,n){ui.call(this,t,i),this.restAngle=e,this.stiffness=s,this.damping=n,this.springTorqueFunc=_i,this.target_wrn=0,this.w_coef=0,this.iSum=0};xi.prototype=Object.create(ui.prototype),xi.prototype.preStep=function(t){var i=this.a,e=this.b,s=i.i_inv+e.i_inv;n(0!==s,"Unsolvable spring."),this.iSum=1/s,this.w_coef=1-Math.exp(-this.damping*t*s),this.target_wrn=0;var r=this.springTorqueFunc(this,i.a-e.a)*t;i.w-=r*i.i_inv,e.w+=r*e.i_inv},xi.prototype.applyImpulse=function(){var t=this.a,i=this.b,e=t.w-i.w,s=(this.target_wrn-e)*this.w_coef;this.target_wrn=e+s;var n=s*this.iSum;t.w+=n*t.i_inv,i.w-=n*i.i_inv};var mi=t.RotaryLimitJoint=function(t,i,e,s){ui.call(this,t,i),this.min=e,this.max=s,this.jAcc=0,this.iSum=this.bias=this.jMax=0};mi.prototype=Object.create(ui.prototype),mi.prototype.preStep=function(t){var i=this.a,e=this.b,s=e.a-i.a,n=0;s>this.max?n=this.max-s:s<this.min&&(n=this.min-s),this.iSum=1/(1/i.i+1/e.i);var r=this.maxBias;this.bias=b(-pi(this.errorBias,t)*n/t,-r,r),this.jMax=this.maxForce*t,this.bias||(this.jAcc=0)},mi.prototype.applyCachedImpulse=function(t){var i=this.a,e=this.b,s=this.jAcc*t;i.w-=s*i.i_inv,e.w+=s*e.i_inv},mi.prototype.applyImpulse=function(){if(this.bias){var t=this.a,i=this.b,e=i.w-t.w,s=-(this.bias+e)*this.iSum,n=this.jAcc;this.bias<0?this.jAcc=b(n+s,0,this.jMax):this.jAcc=b(n+s,-this.jMax,0),s=this.jAcc-n,t.w-=s*t.i_inv,i.w+=s*i.i_inv}},mi.prototype.getImpulse=function(){return Math.abs(joint.jAcc)};var gi=t.RatchetJoint=function(t,i,e,s){ui.call(this,t,i),this.angle=0,this.phase=e,this.ratchet=s,this.angle=(i?i.a:0)-(t?t.a:0),this.iSum=this.bias=this.jAcc=this.jMax=0};gi.prototype=Object.create(ui.prototype),gi.prototype.preStep=function(t){var i=this.a,e=this.b,s=this.angle,n=this.phase,r=this.ratchet,o=e.a-i.a,a=s-o,h=0;a*r>0?h=a:this.angle=Math.floor((o-n)/r)*r+n,this.iSum=1/(i.i_inv+e.i_inv);var c=this.maxBias;this.bias=b(-pi(this.errorBias,t)*h/t,-c,c),this.jMax=this.maxForce*t,this.bias||(this.jAcc=0)},gi.prototype.applyCachedImpulse=function(t){var i=this.a,e=this.b,s=this.jAcc*t;i.w-=s*i.i_inv,e.w+=s*e.i_inv},gi.prototype.applyImpulse=function(){if(this.bias){var t=this.a,i=this.b,e=i.w-t.w,s=this.ratchet,n=-(this.bias+e)*this.iSum,r=this.jAcc;this.jAcc=b((r+n)*s,0,this.jMax*Math.abs(s))/s,n=this.jAcc-r,t.w-=n*t.i_inv,i.w+=n*i.i_inv}},gi.prototype.getImpulse=function(t){return Math.abs(t.jAcc)};var wi=t.GearJoint=function(t,i,e,s){ui.call(this,t,i),this.phase=e,this.ratio=s,this.ratio_inv=1/s,this.jAcc=0,this.iSum=this.bias=this.jMax=0};wi.prototype=Object.create(ui.prototype),wi.prototype.preStep=function(t){var i=this.a,e=this.b;this.iSum=1/(i.i_inv*this.ratio_inv+this.ratio*e.i_inv);var s=this.maxBias;this.bias=b(-pi(this.errorBias,t)*(e.a*this.ratio-i.a-this.phase)/t,-s,s),this.jMax=this.maxForce*t},wi.prototype.applyCachedImpulse=function(t){var i=this.a,e=this.b,s=this.jAcc*t;i.w-=s*i.i_inv*this.ratio_inv,e.w+=s*e.i_inv},wi.prototype.applyImpulse=function(){var t=this.a,i=this.b,e=i.w*this.ratio-t.w,s=(this.bias-e)*this.iSum,n=this.jAcc;this.jAcc=b(n+s,-this.jMax,this.jMax),s=this.jAcc-n,t.w-=s*t.i_inv*this.ratio_inv,i.w+=s*i.i_inv},wi.prototype.getImpulse=function(){return Math.abs(this.jAcc)},wi.prototype.setRatio=function(t){this.ratio=t,this.ratio_inv=1/t,this.activateBodies()};var Si=t.SimpleMotor=function(t,i,e){ui.call(this,t,i),this.rate=e,this.jAcc=0,this.iSum=this.jMax=0};Si.prototype=Object.create(ui.prototype),Si.prototype.preStep=function(t){this.iSum=1/(this.a.i_inv+this.b.i_inv),this.jMax=this.maxForce*t},Si.prototype.applyCachedImpulse=function(t){var i=this.a,e=this.b,s=this.jAcc*t;i.w-=s*i.i_inv,e.w+=s*e.i_inv},Si.prototype.applyImpulse=function(){var t=this.a,i=this.b,e=-(i.w-t.w+this.rate)*this.iSum,s=this.jAcc;this.jAcc=b(s+e,-this.jMax,this.jMax),e=this.jAcc-s,t.w-=e*t.i_inv,i.w+=e*i.i_inv},Si.prototype.getImpulse=function(){return Math.abs(this.jAcc)}}();