Skip to content
Browse files

fix ReferenceError: cpCollideShapes is not defined

  • Loading branch information...
1 parent 321edfb commit 46913e71d8416653bd016319d2c0376b2cbdb1c9 @yetanotherportfolio yetanotherportfolio committed with nornagon Dec 29, 2012
Showing with 7 additions and 7 deletions.
  1. +3 −3 cp.js
  2. +1 −1 cp.min.js
  3. +3 −3 lib/cpSpaceQuery.js
View
6 cp.js
@@ -4312,11 +4312,11 @@ Space.prototype.shapeQuery = function(shape, func)
var contacts;
- // Shape 'a' should have the lower shape type. (required by cpCollideShapes() )
+ // Shape 'a' should have the lower shape type. (required by collideShapes() )
if(a.collisionCode <= b.collisionCode){
- contacts = cpCollideShapes(a, b);
+ contacts = collideShapes(a, b);
} else {
- contacts = cpCollideShapes(b, a);
+ contacts = collideShapes(b, a);
for(var i=0; i<contacts.length; i++) contacts[i].n = vneg(contacts[i].n);
}
View
2 cp.min.js
@@ -1,2 +1,2 @@
(function(){Object.create=Object.create||function(e){function t(){}return t.prototype=e,new t};var e;typeof exports=="undefined"?(e={},typeof window=="object"&&(window.cp=e)):e=exports;var t=function(e,t){if(!e)throw new Error("Assertion failed: "+t)},n=function(e,t){!e&&console&&console.warn&&(console.warn("ASSERTION FAILED: "+t),console.trace&&console.trace())},r=function(e,t){return e<t?e:t},i=function(e,t){return e>t?e:t},s,o;typeof window=="object"&&window.navigator.userAgent.indexOf("Firefox")>-1?(s=Math.min,o=Math.max):(s=r,o=i);var u=function(e,t){return e<t?e+" "+t:t+" "+e},a=function(e,t){for(var n=0;n<e.length;n++)if(e[n]===t){e[n]=e[e.length-1],e.length--;return}},f=e.momentForCircle=function(e,t,n,r){return e*(.5*(t*t+n*n)+U(r))},l=e.areaForCircle=function(e,t){return Math.PI*Math.abs(e*e-t*t)},c=e.momentForSegment=function(e,t,n){var r=A(_(n,t)),i=P(M(t,n),.5);return e*(r*r/12+U(i))},h=e.areaForSegment=function(e,t,n){return n*(Math.PI*n+2*J(e,t))},p=e.momentForPoly=function(e,t,n){var r=0,i=0,s=t.length;for(var o=0;o<s;o+=2){var u=t[o]+n.x,a=t[o+1]+n.y,f=t[(o+2)%s]+n.x,l=t[(o+3)%s]+n.y,c=B(f,l,u,a),h=L(u,a,u,a)+L(u,a,f,l)+L(f,l,f,l);r+=c*h,i+=c}return e*r/(6*i)},d=e.areaForPoly=function(e){var t=0;for(var n=0,r=e.length;n<r;n+=2)t+=H(new N(e[n],e[n+1]),new N(e[(n+2)%r],e[(n+3)%r]));return-t/2},v=e.centroidForPoly=function(e){var t=0,n=new N(0,0);for(var r=0,i=e.length;r<i;r+=2){var s=new N(e[r],e[r+1]),o=new N(e[(r+2)%i],e[(r+3)%i]),u=H(s,o);t+=u,n=M(n,P(M(s,o),u))}return P(n,1/(3*t))},m=e.recenterPoly=function(e){var t=v(e);for(var n=0;n<e.length;n+=2)e[n]-=t.x,e[n+1]-=t.y},g=e.momentForBox=function(e,t,n){return e*(t*t+n*n)/12},y=e.momentForBox2=function(e,t){return width=t.r-t.l,height=t.t-t.b,offset=P([t.l+t.r,t.b+t.t],.5),g(e,width,height)+e*U(offset)},b=function(e,t,n){return s(o(e,t),n)},w=function(e){return o(0,s(e,1))},E=function(e,t,n){return e*(1-n)+t*n},S=function(e,t,n){return e+b(t-e,-n,n)},x=0,T={},N=e.Vect=function(e,t){this.x=e,this.y=t,x++};e.v=function(e,t){return new N(e,t)};var C=e.vzero=new N(0,0),k=e.v.dot=function(e,t){return e.x*t.x+e.y*t.y},L=function(e,t,n,r){return e*n+t*r},A=e.v.len=function(e){return Math.sqrt(k(e,e))},O=e.v.eql=function(e,t){return e.x===t.x&&e.y===t.y},M=e.v.add=function(e,t){return new N(e.x+t.x,e.y+t.y)};N.prototype.add=function(e){return this.x+=e.x,this.y+=e.y,this};var _=e.v.sub=function(e,t){return new N(e.x-t.x,e.y-t.y)};N.prototype.sub=function(e){return this.x-=e.x,this.y-=e.y,this};var D=e.v.neg=function(e){return new N(-e.x,-e.y)};N.prototype.neg=function(){return this.x=-this.x,this.y=-this.y,this};var P=e.v.mult=function(e,t){return new N(e.x*t,e.y*t)};N.prototype.mult=function(e){return this.x*=e,this.y*=e,this};var H=e.v.cross=function(e,t){return e.x*t.y-e.y*t.x},B=function(e,t,n,r){return e*r-t*n},j=e.v.perp=function(e){return new N(-e.y,e.x)},F=e.v.pvrperp=function(e){return new N(e.y,-e.x)},I=e.v.project=function(e,t){return P(t,k(e,t)/U(t))};N.prototype.project=function(e){return this.mult(k(this,e)/U(e)),this};var q=e.v.rotate=function(e,t){return new N(e.x*t.x-e.y*t.y,e.x*t.y+e.y*t.x)};N.prototype.rotate=function(e){return this.x=this.x*e.x-this.y*e.y,this.y=this.x*e.y+this.y*e.x,this};var R=e.v.unrotate=function(e,t){return new N(e.x*t.x+e.y*t.y,e.y*t.x-e.x*t.y)},U=e.v.lengthsq=function(e){return k(e,e)},z=e.v.lerp=function(e,t,n){return M(P(e,1-n),P(t,n))},W=e.v.normalize=function(e){return P(e,1/A(e))},X=e.v.normalize_safe=function(e){return e.x===0&&e.y===0?C:W(e)},V=e.v.clamp=function(e,t){return k(e,e)>t*t?P(W(e),t):e},$=e.v.lerpconst=function(e,t,n){return M(e,V(_(t,e),n))},J=e.v.dist=function(e,t){return A(_(e,t))},K=e.v.distsq=function(e,t){return U(_(e,t))},Q=e.v.near=function(e,t,n){return K(e,t)<n*n},G=e.v.slerp=function(e,t,n){var r=Math.acos(k(e,t));if(r){var i=1/Math.sin(r);return M(P(e,Math.sin((1-n)*r)*i),P(t,Math.sin(n*r)*i))}return e},Y=e.v.slerpconst=function(e,t,n){var r=Math.acos(k(e,t));return G(e,t,s(n,r)/r)},Z=e.v.forangle=function(e){return new N(Math.cos(e),Math.sin(e))},et=e.v.toangle=function(e){return Math.atan2(e.y,e.x)},tt=e.v.str=function(e){return"("+e.x.toFixed(3)+", "+e.y.toFixed(3)+")"},nt=0,rt=e.BB=function(e,t,n,r){this.l=e,this.b=t,this.r=n,this.t=r,nt++};e.bb=function(e,t,n,r){return new rt(e,t,n,r)};var it=function(e,t){return new rt(e.x-t,e.y-t,e.x+t,e.y+t)},st=function(e,t){return e.l<=t.r&&t.l<=e.r&&e.b<=t.t&&t.b<=e.t},ot=function(e,t,n,r,i){return e.l<=r&&t<=e.r&&e.b<=i&&n<=e.t},ut=function(e,t){return e.l<=t.l&&e.r>=t.r&&e.b<=t.b&&e.t>=t.t},at=function(e,t){return e.l<=t.x&&e.r>=t.x&&e.b<=t.y&&e.t>=t.y},ft=function(e,t,n,r,i){return e<=i.x&&n>=i.x&&t<=i.y&&r>=i.y},lt=function(e,t){return new rt(s(e.l,t.l),s(e.b,t.b),o(e.r,t.r),o(e.t,t.t))},ct=function(e,t){return new rt(s(e.l,t.x),s(e.b,t.y),o(e.r,t.x),o(e.t,t.y))},ht=function(e){return(e.r-e.l)*(e.t-e.b)},pt=function(e,t){return(o(e.r,t.r)-s(e.l,t.l))*(o(e.t,t.t)-s(e.b,t.b))},dt=function(e,t,n,r,i){return(o(e.r,r)-s(e.l,t))*(o(e.t,i)-s(e.b,n))},vt=function(e,t,n){return bbSegmentQuery(e,t,n)!=Infinity},mt=function(e,t){var n=s(o(e.l,t.x),e.r),r=s(o(e.b,t.y),e.t);return new N(n,r)},gt=function(e,t){var n=Math.abs(e.r-e.l),r=(t.x-e.l)%n,i=r>0?r:r+n,s=Math.abs(e.t-e.b),o=(t.y-e.b)%s,u=o>0?o:o+s;return new N(i+e.l,u+e.b)},yt=0,bt=e.NO_GROUP=0,wt=e.ALL_LAYERS=-1;e.resetShapeIdCounter=function(){yt=0};var Et=e.Shape=function(e){this.body=e,this.bb_l=this.bb_b=this.bb_r=this.bb_t=0,this.hashid=yt++,this.sensor=!1,this.e=0,this.u=0,this.surface_v=C,this.collision_type=0,this.group=0,this.layers=wt,this.space=null,this.collisionCode=this.collisionCode};Et.prototype.setElasticity=function(e){this.e=e},Et.prototype.setFriction=function(e){this.body.activate(),this.u=e},Et.prototype.setLayers=function(e){this.body.activate(),this.layers=e},Et.prototype.setSensor=function(e){this.body.activate(),this.sensor=e},Et.prototype.setCollisionType=function(e){this.body.activate(),this.collision_type=e},Et.prototype.getBody=function(){return this.body},Et.prototype.active=function(){return this.body&&this.body.shapeList.indexOf(this)!==-1},Et.prototype.setBody=function(e){t(!this.active(),"You cannot change the body on an active shape. You must remove the shape, then "),this.body=e},Et.prototype.cacheBB=function(){return this.update(this.body.p,this.body.rot)},Et.prototype.update=function(e,n){t(!isNaN(n.x),"Rotation is NaN"),t(!isNaN(e.x),"Position is NaN"),this.cacheData(e,n)},Et.prototype.getBB=function(){return new rt(this.bb_l,this.bb_b,this.bb_r,this.bb_t)};var St=function(e){this.shape=e,this.d=Infinity,this.n=C},xt=function(e,t,n){this.shape=e,this.t=t,this.n=n};xt.prototype.hitPoint=function(e,t){return z(e,t,this.t)},xt.prototype.hitDist=function(e,t){return J(e,t)*this.t};var Tt=e.CircleShape=function(e,t,n){this.c=this.tc=n,this.r=t,this.type="circle",Et.call(this,e)};Tt.prototype=Object.create(Et.prototype),Tt.prototype.cacheData=function(e,t){var n=this.tc=q(this.c,t).add(e),r=this.r;this.bb_l=n.x-r,this.bb_b=n.y-r,this.bb_r=n.x+r,this.bb_t=n.y+r},Tt.prototype.pointQuery=function(e){var t=_(e,this.tc),n=U(t),r=this.r;if(n<r*r){var i=new St(this),s=Math.sqrt(n);return i.d=r-s,i.n=P(t,1/s),i}};var Nt=function(e,t,n,r,i,s){r=_(r,t),i=_(i,t);var o=k(r,r)-2*k(r,i)+k(i,i),u=-2*k(r,r)+2*k(r,i),a=k(r,r)-n*n,f=u*u-4*o*a;if(f>=0){var l=(-u-Math.sqrt(f))/(2*o);if(0<=l&&l<=1)return new xt(e,l,W(z(r,i,l)))}};Tt.prototype.segmentQuery=function(e,t){return Nt(this,this.tc,this.r,e,t)};var Ct=e.SegmentShape=function(e,t,n,r){this.a=t,this.b=n,this.n=j(W(_(n,t))),this.ta=this.tb=this.tn=null,this.r=r,this.a_tangent=C,this.b_tangent=C,this.type="segment",Et.call(this,e)};Ct.prototype=Object.create(Et.prototype),Ct.prototype.cacheData=function(e,t){this.ta=M(e,q(this.a,t)),this.tb=M(e,q(this.b,t)),this.tn=q(this.n,t);var n,r,i,s;this.ta.x<this.tb.x?(n=this.ta.x,r=this.tb.x):(n=this.tb.x,r=this.ta.x),this.ta.y<this.tb.y?(i=this.ta.y,s=this.tb.y):(i=this.tb.y,s=this.ta.y);var o=this.r;this.bb_l=n-o,this.bb_b=i-o,this.bb_r=r+o,this.bb_t=s+o},Ct.prototype.pointQuery=function(e){if(!ft(this.bb_l,this.bb_b,this.bb_r,this.bb_t,e))return;var t=this.ta,n=this.tb,r=_(n,t),i=w(k(r,_(e,t))/U(r)),s=M(t,P(r,i)),o=_(e,s),u=U(o),a=this.r;if(u<a*a){var f=new St(this),l=Math.sqrt(u);return f.d=a-l,f.n=P(o,1/l),f}},Ct.prototype.segmentQuery=function(e,t){var n=this.tn,r=k(_(this.ta,e),n),i=this.r,s=r>0?D(n):n,o=_(P(s,i),e),u=M(this.ta,o),a=M(this.tb,o),f=_(t,e);if(H(f,u)*H(f,a)<=0){var l=r+(r>0?-i:i),c=-l,h=k(f,n)-l;if(c*h<0)return new xt(this,c/(c-h),s)}else if(i!==0){var p=Nt(this,this.ta,this.r,e,t),d=Nt(this,this.tb,this.r,e,t);return p?d&&d.t<p.t?d:p:d}},Ct.prototype.setNeighbors=function(e,t){this.a_tangent=_(e,this.a),this.b_tangent=_(t,this.b)},Ct.prototype.setEndpoints=function(e,t){this.a=e,this.b=t,this.n=j(W(_(t,e)))};var kt=function(e){var t=e.length;for(var n=0;n<t;n+=2){var r=e[n],i=e[n+1],s=e[(n+2)%t],o=e[(n+3)%t],u=e[(n+4)%t],a=e[(n+5)%t];if(B(s-r,o-i,u-s,a-o)>0)return!1}return!0},Lt=e.PolyShape=function(e,n,r){t(n.length>=4,"Polygons require some verts"),t(typeof n[0]=="number","Polygon verticies should be specified in a flattened list"),t(kt(n),"Polygon is concave or has a reversed winding."),this.setVerts(n,r),this.type="poly",Et.call(this,e)};Lt.prototype=Object.create(Et.prototype);var At=function(e,t){this.n=e,this.d=t};Lt.prototype.setVerts=function(e,t){var n=e.length,r=n>>1;this.verts=new Array(n),this.tVerts=new Array(n),this.axes=new Array(r),this.tAxes=new Array(r);for(var i=0;i<n;i+=2){var s=e[i]+t.x,o=e[i+1]+t.y,u=e[(i+2)%n]+t.x,a=e[(i+3)%n]+t.y,f=W(j(new N(u-s,a-o)));this.verts[i]=s,this.verts[i+1]=o,this.axes[i>>1]=new At(f,L(f.x,f.y,s,o)),this.tAxes[i>>1]=new At(new N(0,0),0)}};var Ot=e.BoxShape=function(e,t,n){var r=t/2,i=n/2;return Mt(e,new rt(-r,-i,r,i))},Mt=e.BoxShape2=function(e,t){var n=[t.l,t.b,t.l,t.t,t.r,t.t,t.r,t.b];return new Lt(e,n,C)};Lt.prototype.transformVerts=function(e,t){var n=this.verts,r=this.tVerts,i=Infinity,u=-Infinity,a=Infinity,f=-Infinity;for(var l=0;l<n.length;l+=2){var c=n[l],h=n[l+1],p=e.x+c*t.x-h*t.y,d=e.y+c*t.y+h*t.x;r[l]=p,r[l+1]=d,i=s(i,p),u=o(u,p),a=s(a,d),f=o(f,d)}this.bb_l=i,this.bb_b=a,this.bb_r=u,this.bb_t=f},Lt.prototype.transformAxes=function(e,t){var n=this.axes,r=this.tAxes;for(var i=0;i<n.length;i++){var s=q(n[i].n,t);r[i].n=s,r[i].d=k(e,s)+n[i].d}},Lt.prototype.cacheData=function(e,t){this.transformAxes(e,t),this.transformVerts(e,t)},Lt.prototype.pointQuery=function(e){if(!ft(this.bb_l,this.bb_b,this.bb_r,this.bb_t,e))return;var t=new St(this),n=this.tAxes;for(var r=0;r<n.length;r++){var i=n[r].n,s=n[r].d-k(i,e);if(s<0)return;s<t.d&&(t.d=s,t.n=i)}return t},Lt.prototype.segmentQuery=function(e,t){var n=this.tAxes,r=this.tVerts,i=n.length,s=i*2;for(var o=0;o<i;o++){var u=n[o].n,a=k(e,u);if(n[o].d>a)continue;var f=k(t,u),l=(n[o].d-a)/(f-a);if(l<0||1<l)continue;var c=z(e,t,l),h=-H(u,c),p=-B(u.x,u.y,r[o*2],r[o*2+1]),d=-B(u.x,u.y,r[(o*2+2)%s],r[(o*2+3)%s]);if(p<=h&&h<=d)return new xt(this,l,u)}},Lt.prototype.valueOnAxis=function(e,t){var n=this.tVerts,r=L(e.x,e.y,n[0],n[1]);for(var i=2;i<n.length;i+=2)r=s(r,L(e.x,e.y,n[i],n[i+1]));return r-t},Lt.prototype.containsVert=function(e,t){var n=this.tAxes;for(var r=0;r<n.length;r++){var i=n[r].n,s=L(i.x,i.y,e,t)-n[r].d;if(s>0)return!1}return!0},Lt.prototype.containsVertPartial=function(e,t,n){var r=this.tAxes;for(var i=0;i<r.length;i++){var n=r[i].n;if(k(n,n)<0)continue;var s=L(n.x,n.y,e,t)-r[i].d;if(s>0)return!1}return!0},Lt.prototype.getNumVerts=function(){return this.verts.length/2},Lt.prototype.getVert=function(e){return new N(this.verts[e*2],this.verts[e*2+1])};var _t=e.Body=function(e,t){this.p=new N(0,0),this.vx=this.vy=0,this.f=new N(0,0),this.w=0,this.t=0,this.v_limit=Infinity,this.w_limit=Infinity,this.v_biasx=this.v_biasy=0,this.w_bias=0,this.space=null,this.shapeList=[],this.arbiterList=null,this.constraintList=null,this.nodeRoot=null,this.nodeNext=null,this.nodeIdleTime=0,this.setMass(e),this.setMoment(t),this.rot=new N(0,0),this.setAngle(0)},Dt=function(){return body=new _t(Infinity,Infinity),body.nodeIdleTime=Infinity,body};if(typeof DEBUG!="undefined"&&DEBUG){var Pt=function(e,n){t(e.x==e.x&&e.y==e.y,n)},Ht=function(e,n){t(Math.abs(e.x)!==Infinity&&Math.abs(e.y)!==Infinity,n)},Bt=function(e,t){Pt(e,t),Ht(e,t)};_t.prototype.sanityCheck=function(){t(this.m===this.m&&this.m_inv===this.m_inv,"Body's mass is invalid."),t(this.i===this.i&&this.i_inv===this.i_inv,"Body's moment is invalid."),Bt(this.p,"Body's position is invalid."),Bt(this.f,"Body's force is invalid."),t(this.vx===this.vx&&Math.abs(this.vx)!==Infinity,"Body's velocity is invalid."),t(this.vy===this.vy&&Math.abs(this.vy)!==Infinity,"Body's velocity is invalid."),t(this.a===this.a&&Math.abs(this.a)!==Infinity,"Body's angle is invalid."),t(this.w===this.w&&Math.abs(this.w)!==Infinity,"Body's angular velocity is invalid."),t(this.t===this.t&&Math.abs(this.t)!==Infinity,"Body's torque is invalid."),Bt(this.rot,"Internal error: Body's rotation vector is invalid."),t(this.v_limit===this.v_limit,"Body's velocity limit is invalid."),t(this.w_limit===this.w_limit,"Body's angular velocity limit is invalid.")}}else _t.prototype.sanityCheck=function(){};_t.prototype.getPos=function(){return this.p},_t.prototype.getVel=function(){return new N(this.vx,this.vy)},_t.prototype.getAngVel=function(){return this.w},_t.prototype.isSleeping=function(){return this.nodeRoot!==null},_t.prototype.isStatic=function(){return this.nodeIdleTime===Infinity},_t.prototype.isRogue=function(){return this.space===null},_t.prototype.setMass=function(e){t(e>0,"Mass must be positive and non-zero."),this.activate(),this.m=e,this.m_inv=1/e},_t.prototype.setMoment=function(e){t(e>0,"Moment of Inertia must be positive and non-zero."),this.activate(),this.i=e,this.i_inv=1/e},_t.prototype.addShape=function(e){this.shapeList.push(e)},_t.prototype.removeShape=function(e){a(this.shapeList,e)};var jt=function(e,t,n){return e===n?e.next(t):(e.a===t?e.next_a=jt(e.next_a,t,n):e.next_b=jt(e.next_b,t,n),e)};_t.prototype.removeConstraint=function(e){this.constraintList=jt(this.constraintList,this,e)},_t.prototype.setPos=function(t){this.activate(),this.sanityCheck(),t===C&&(t=e.v(0,0)),this.p=t},_t.prototype.setVel=function(e){this.activate(),this.vx=e.x,this.vy=e.y},_t.prototype.setAngVel=function(e){this.activate(),this.w=e},_t.prototype.setAngleInternal=function(e){t(!isNaN(e),"Internal Error: Attempting to set body's angle to NaN"),this.a=e,this.rot.x=Math.cos(e),this.rot.y=Math.sin(e)},_t.prototype.setAngle=function(e){this.activate(),this.sanityCheck(),this.setAngleInternal(e)},_t.prototype.velocity_func=function(e,t,n){var r=this.vx*t+(e.x+this.f.x*this.m_inv)*n,i=this.vy*t+(e.y+this.f.y*this.m_inv)*n,s=this.v_limit,o=r*r+i*i,u=o>s*s?s/Math.sqrt(o):1;this.vx=r*u,this.vy=i*u;var a=this.w_limit;this.w=b(this.w*t+this.t*this.i_inv*n,-a,a),this.sanityCheck()},_t.prototype.position_func=function(e){this.p.x+=(this.vx+this.v_biasx)*e,this.p.y+=(this.vy+this.v_biasy)*e,this.setAngleInternal(this.a+(this.w+this.w_bias)*e),this.v_biasx=this.v_biasy=0,this.w_bias=0,this.sanityCheck()},_t.prototype.resetForces=function(){this.activate(),this.f=new N(0,0),this.t=0},_t.prototype.applyForce=function(e,t){this.activate(),this.f=M(this.f,e),this.t+=H(t,e)},_t.prototype.applyImpulse=function(e,t){this.activate(),zn(this,e.x,e.y,t)},_t.prototype.getVelAtPoint=function(e){return M(new N(this.vx,this.vy),P(j(e),this.w))},_t.prototype.getVelAtWorldPoint=function(e){return this.getVelAtPoint(_(e,this.p))},_t.prototype.getVelAtLocalPoint=function(e){return this.getVelAtPoint(q(e,this.rot))},_t.prototype.eachShape=function(e){for(var t=0,n=this.shapeList.length;t<n;t++)e(this.shapeList[t])},_t.prototype.eachConstraint=function(e){var t=this.constraintList;while(t){var n=t.next(this);e(t),t=n}},_t.prototype.eachArbiter=function(e){var t=this.arbiterList;while(t){var n=t.next(this);t.swappedColl=this===t.body_b,e(t),t=n}},_t.prototype.local2World=function(e){return M(this.p,q(e,this.rot))},_t.prototype.world2Local=function(e){return R(_(e,this.p),this.rot)},_t.prototype.kineticEnergy=function(){var e=this.vx*this.vx+this.vy*this.vy,t=this.w*this.w;return(e?e*this.m:0)+(t?t*this.i:0)};var Ft=e.SpatialIndex=function(e){this.staticIndex=e,e&&(t(!e.dynamicIndex,"This static index is already associated with a dynamic index."),e.dynamicIndex=this)};Ft.prototype.collideStatic=function(e,t){if(e.count>0){var n=e.query;this.each(function(e){n(e,new rt(e.bb_l,e.bb_b,e.bb_r,e.bb_t),t)})}};var It=e.BBTree=function(e){Ft.call(this,e),this.velocityFunc=null,this.leaves={},this.count=0,this.root=null,this.stamp=0};It.prototype=Object.create(Ft.prototype);var qt=0,Rt=function(e,t,n){this.obj=null,this.bb_l=s(t.bb_l,n.bb_l),this.bb_b=s(t.bb_b,n.bb_b),this.bb_r=o(t.bb_r,n.bb_r),this.bb_t=o(t.bb_t,n.bb_t),this.parent=null,this.setA(t),this.setB(n),qt++},Ut=0,zt=function(e,t){this.obj=t,e.getBB(t,this),this.parent=null,this.stamp=1,this.pairs=null,Ut++};It.prototype.getBB=function(e,t){var n=this.velocityFunc;if(n){var r=.1,i=(e.bb_r-e.bb_l)*r,u=(e.bb_t-e.bb_b)*r,a=P(n(e),.1);t.bb_l=e.bb_l+s(-i,a.x),t.bb_b=e.bb_b+s(-u,a.y),t.bb_r=e.bb_r+o(i,a.x),t.bb_t=e.bb_t+o(u,a.y)}else t.bb_l=e.bb_l,t.bb_b=e.bb_b,t.bb_r=e.bb_r,t.bb_t=e.bb_t},It.prototype.getStamp=function(){var e=this.dynamicIndex;return e&&e.stamp?e.stamp:this.stamp},It.prototype.incrementStamp=function(){this.dynamicIndex&&this.dynamicIndex.stamp?this.dynamicIndex.stamp++:this.stamp++};var Wt=function(e,t){this.a=e,this.b=t},Xt=function(e,t){this.prev=null,this.next=t,this.leaf=e};Xt.prototype.unlink=function(){var e=this.next,t=this.prev;e&&(e.a.leaf==this.leaf?e.a.prev=t:e.b.prev=t),t?t.a.leaf==this.leaf?t.a.next=e:t.b.next=e:this.leaf.pairs=e},zt.prototype.clearPairs=function(e){var t=this.pairs,n;this.pairs=null;while(t)t.a.leaf==this?(n=t.a.next,t.b.unlink(),t=n):(n=t.b.next,t.a.unlink(),t=n)};var Vt=function(e,t,n){var r=e.pairs,i=t.pairs,s=new Wt(new Xt(e,r),new Xt(t,i));e.pairs=t.pairs=s,r&&(r.a.leaf==e?r.a.prev=s:r.b.prev=s),i&&(i.a.leaf==t?i.a.prev=s:i.b.prev=s)};Rt.prototype.setA=function(e){this.A=e,e.parent=this},Rt.prototype.setB=function(e){this.B=e,e.parent=this},zt.prototype.isLeaf=!0,Rt.prototype.isLeaf=!1,Rt.prototype.otherChild=function(e){return this.A==e?this.B:this.A},Rt.prototype.replaceChild=function(e,t,r){n(e==this.A||e==this.B,"Node is not a child of parent."),this.A==e?this.setA(t):this.setB(t);for(var i=this;i;i=i.parent){var u=i.A,a=i.B;i.bb_l=s(u.bb_l,a.bb_l),i.bb_b=s(u.bb_b,a.bb_b),i.bb_r=o(u.bb_r,a.bb_r),i.bb_t=o(u.bb_t,a.bb_t)}},Rt.prototype.bbArea=zt.prototype.bbArea=function(){return(this.bb_r-this.bb_l)*(this.bb_t-this.bb_b)};var $t=function(e,t){return(o(e.bb_r,t.bb_r)-s(e.bb_l,t.bb_l))*(o(e.bb_t,t.bb_t)-s(e.bb_b,t.bb_b))},Jt=function(e,t){return Math.abs(e.bb_l+e.bb_r-t.bb_l-t.bb_r)+Math.abs(e.bb_b+t.bb_t-t.bb_b-t.bb_t)},Kt=function(e,t,n){if(e==null)return t;if(e.isLeaf)return new Rt(n,t,e);var r=e.B.bbArea()+$t(e.A,t),i=e.A.bbArea()+$t(e.B,t);return r===i&&(r=Jt(e.A,t),i=Jt(e.B,t)),i<r?e.setB(Kt(e.B,t,n)):e.setA(Kt(e.A,t,n)),e.bb_l=s(e.bb_l,t.bb_l),e.bb_b=s(e.bb_b,t.bb_b),e.bb_r=o(e.bb_r,t.bb_r),e.bb_t=o(e.bb_t,t.bb_t),e};Rt.prototype.intersectsBB=zt.prototype.intersectsBB=function(e){return this.bb_l<=e.r&&e.l<=this.bb_r&&this.bb_b<=e.t&&e.b<=this.bb_t};var Qt=function(e,t,n){e.intersectsBB(t)&&(e.isLeaf?n(e.obj):(Qt(e.A,t,n),Qt(e.B,t,n)))},Gt=function(e,t,n){var r=1/(n.x-t.x),i=e.bb_l==t.x?-Infinity:(e.bb_l-t.x)*r,u=e.bb_r==t.x?Infinity:(e.bb_r-t.x)*r,a=s(i,u),f=o(i,u),l=1/(n.y-t.y),c=e.bb_b==t.y?-Infinity:(e.bb_b-t.y)*l,h=e.bb_t==t.y?Infinity:(e.bb_t-t.y)*l,p=s(c,h),d=o(c,h);if(p<=f&&a<=d){var v=o(a,p),m=s(f,d);if(0<=m&&v<=1)return o(v,0)}return Infinity},Yt=function(e,t,n,r,i){if(e.isLeaf)return i(e.obj);var o=Gt(e.A,t,n),u=Gt(e.B,t,n);return o<u?(o<r&&(r=s(r,Yt(e.A,t,n,r,i))),u<r&&(r=s(r,Yt(e.B,t,n,r,i)))):(u<r&&(r=s(r,Yt(e.B,t,n,r,i))),o<r&&(r=s(r,Yt(e.A,t,n,r,i)))),r},Zt=function(e,t,n){if(t==e)return null;var r=t.parent;if(r==e){var i=e.otherChild(t);return i.parent=e.parent,i}return r.parent.replaceChild(r,r.otherChild(t),n),e},en=function(e,t){return e.bb_l<=t.bb_r&&t.bb_l<=e.bb_r&&e.bb_b<=t.bb_t&&t.bb_b<=e.bb_t},tn=function(e,t,n,r,i){en(t,e)&&(e.isLeaf?n?Vt(t,e,r):(e.stamp<t.stamp&&Vt(e,t,r),i&&i(t.obj,e.obj)):(tn(e.A,t,n,r,i),tn(e.B,t,n,r,i)))},nn=function(e,t,n,r){if(e.stamp==t.getStamp()){n&&tn(n,e,!1,t,r);for(var i=e;i.parent;i=i.parent)i==i.parent.A?tn(i.parent.B,e,!0,t,r):tn(i.parent.A,e,!1,t,r)}else{var s=e.pairs;while(s)e==s.b.leaf?(r&&r(s.a.leaf.obj,e.obj),s=s.b.next):s=s.a.next}},rn=function(e,t,n,r){e.isLeaf?nn(e,t,n,r):(rn(e.A,t,n,r),rn(e.B,t,n,r))};zt.prototype.containsObj=function(e){return this.bb_l<=e.bb_l&&this.bb_r>=e.bb_r&&this.bb_b<=e.bb_b&&this.bb_t>=e.bb_t},zt.prototype.update=function(e){var t=e.root,n=this.obj;return this.containsObj(n)?!1:(e.getBB(this.obj,this),t=Zt(t,this,e),e.root=Kt(t,this,e),this.clearPairs(e),this.stamp=e.getStamp(),!0)},zt.prototype.addPairs=function(e){var t=e.dynamicIndex;if(t){var n=t.root;n&&tn(n,this,!0,t,null)}else{var r=e.staticIndex.root;nn(this,e,r,null)}},It.prototype.insert=function(e,t){var n=new zt(this,e);this.leaves[t]=n,this.root=Kt(this.root,n,this),this.count++,n.stamp=this.getStamp(),n.addPairs(this),this.incrementStamp()},It.prototype.remove=function(e,t){var n=this.leaves[t];delete this.leaves[t],this.root=Zt(this.root,n,this),this.count--,n.clearPairs(this)},It.prototype.contains=function(e,t){return this.leaves[t]!=null};var sn=function(e,t){};It.prototype.reindexQuery=function(e){if(!this.root)return;var t,n=this.leaves;for(t in n)n[t].update(this);var r=this.staticIndex,i=r&&r.root;rn(this.root,this,i,e),r&&!i&&this.collideStatic(this,r,e),this.incrementStamp()},It.prototype.reindex=function(){this.reindexQuery(sn)},It.prototype.reindexObject=function(e,t){var n=this.leaves[t];n&&(n.update(this)&&n.addPairs(this),this.incrementStamp())},It.prototype.pointQuery=function(e,t){this.root&&Qt(this.root,new rt(e.x,e.y,e.x,e.y),t)},It.prototype.segmentQuery=function(e,t,n,r){this.root&&Yt(this.root,e,t,n,r)},It.prototype.query=function(e,t){this.root&&Qt(this.root,e,t)},It.prototype.count=function(){return this.count},It.prototype.each=function(e){var t;for(t in this.leaves)e(this.leaves[t].obj)};var on=function(e,t,n,r,i){return(o(e.bb_r,r)-s(e.bb_l,t))*(o(e.bb_t,i)-s(e.bb_b,n))},un=function(e,t,n,r){if(r==1)return t[n];if(r==2)return new Rt(e,t[n],t[n+1]);var i=t[n],u=i.bb_l,a=i.bb_b,f=i.bb_r,l=i.bb_t,c=n+r;for(var h=n+1;h<c;h++)i=t[h],u=s(u,i.bb_l),a=s(a,i.bb_b),f=o(f,i.bb_r),l=o(l,i.bb_t);var p=f-u>l-a,d=new Array(r*2);if(p)for(var h=n;h<c;h++)d[2*h+0]=t[h].bb_l,d[2*h+1]=t[h].bb_r;else for(var h=n;h<c;h++)d[2*h+0]=t[h].bb_b,d[2*h+1]=t[h].bb_t;d.sort(function(e,t){return e-t});var v=(d[r-1]+d[r])*.5,m=u,g=a,y=f,b=l,w=u,E=a,S=f,x=l;p?y=w=v:b=E=v;var T=c;for(var N=n;N<T;){var i=t[N];on(i,w,E,S,x)<on(i,m,g,y,b)?(T--,t[N]=t[T],t[T]=i):N++}if(T==r){var i=null;for(var h=n;h<c;h++)i=Kt(i,t[h],e);return i}return NodeNew(e,un(e,t,n,T-n),un(e,t,T,c-T))};It.prototype.optimize=function(){var e=new Array(this.count),t=0;for(var n in this.leaves)e[t++]=this.nodes[n];this.root=un(tree,e,e.length)};var an=function(e,t){!e.isLeaf&&t<=10&&(an(e.a,t+1),an(e.b,t+1));var n="";for(var r=0;r<t;r++)n+=" "};It.prototype.log=function(){this.root&&an(this.root,0)};var fn=e.CollisionHandler=function(){this.a=this.b=0};fn.prototype.begin=function(e,t){return!0},fn.prototype.preSolve=function(e,t){return!0},fn.prototype.postSolve=function(e,t){},fn.prototype.separate=function(e,t){};var ln=4,cn=function(e,t){this.e=0,this.u=0,this.surface_vr=C,this.a=e,this.body_a=e.body,this.b=t,this.body_b=t.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"};cn.prototype.getShapes=function(){return this.swappedColl?[this.b,this.a]:[this.a,this.b]},cn.prototype.totalImpulse=function(){var e=this.contacts,t=new N(0,0);for(var n=0,r=e.length;n<r;n++){var i=e[n];t.add(P(i.n,i.jnAcc))}return this.swappedColl?t:t.neg()},cn.prototype.totalImpulseWithFriction=function(){var e=this.contacts,t=new N(0,0);for(var n=0,r=e.length;n<r;n++){var i=e[n];t.add((new N(i.jnAcc,i.jtAcc)).rotate(i.n))}return this.swappedColl?t:t.neg()},cn.prototype.totalKE=function(){var e=(1-this.e)/(1+this.e),t=0,n=this.contacts;for(var r=0,i=n.length;r<i;r++){var s=n[r],o=s.jnAcc,u=s.jtAcc;t+=e*o*o/s.nMass+u*u/s.tMass}return t},cn.prototype.ignore=function(){this.state="ignore"},cn.prototype.getA=function(){return this.swappedColl?this.b:this.a},cn.prototype.getB=function(){return this.swappedColl?this.a:this.b},cn.prototype.isFirstContact=function(){return this.state==="first coll"};var hn=function(e,t,n){this.point=e,this.normal=t,this.dist=n};cn.prototype.getContactPointSet=function(){var e=new Array(this.contacts.length),t;for(t=0;t<e.length;t++)e[t]=new hn(this.contacts[t].p,this.contacts[t].n,this.contacts[t].dist);return e},cn.prototype.getNormal=function(e){var t=this.contacts[e].n;return this.swappedColl?D(t):t},cn.prototype.getPoint=function(e){return this.contacts[e].p},cn.prototype.getDepth=function(e){return this.contacts[e].dist};var pn=function(e,t,n,r){n?n.body_a===t?n.thread_a_next=r:n.thread_b_next=r:t.arbiterList=r,r&&(r.body_a===t?r.thread_a_prev=n:r.thread_b_prev=n)};cn.prototype.unthread=function(){pn(this,this.body_a,this.thread_a_prev,this.thread_a_next),pn(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},cn.prototype.update=function(e,t,n,r){if(this.contacts)for(var i=0;i<this.contacts.length;i++){var s=this.contacts[i];for(var o=0;o<e.length;o++){var u=e[o];u.hash===s.hash&&(u.jnAcc=s.jnAcc,u.jtAcc=s.jtAcc)}}this.contacts=e,this.handler=t,this.swappedColl=n.collision_type!==t.a,this.e=n.e*r.e,this.u=n.u*r.u,this.surface_vr=_(n.surface_v,r.surface_v),this.a=n,this.body_a=n.body,this.b=r,this.body_b=r.body,this.state=="cached"&&(this.state="first coll")},cn.prototype.preStep=function(e,t,n){var r=this.body_a,i=this.body_b;for(var o=0;o<this.contacts.length;o++){var u=this.contacts[o];u.r1=_(u.p,r.p),u.r2=_(u.p,i.p),u.nMass=1/$n(r,i,u.r1,u.r2,u.n),u.tMass=1/$n(r,i,u.r1,u.r2,j(u.n)),u.bias=-n*s(0,u.dist+t)/e,u.jBias=0,u.bounce=Un(r,i,u.r1,u.r2,u.n)*this.e}},cn.prototype.applyCachedImpulse=function(e){if(this.isFirstContact())return;var t=this.body_a,n=this.body_b;for(var r=0;r<this.contacts.length;r++){var i=this.contacts[r],s=i.n.x,o=i.n.y,u=s*i.jnAcc-o*i.jtAcc,a=s*i.jtAcc+o*i.jnAcc;Wn(t,n,i.r1,i.r2,u*e,a*e)}};var dn=0,vn=0;cn.prototype.applyImpulse=function(){dn++;var e=this.body_a,t=this.body_b,n=this.surface_vr,r=this.u;for(var i=0;i<this.contacts.length;i++){vn++;var s=this.contacts[i],u=s.nMass,a=s.n,f=s.r1,l=s.r2,c=t.vx-l.y*t.w-(e.vx-f.y*e.w),h=t.vy+l.x*t.w-(e.vy+f.x*e.w),p=a.x*(t.v_biasx-l.y*t.w_bias-e.v_biasx+f.y*e.w_bias)+a.y*(l.x*t.w_bias+t.v_biasy-f.x*e.w_bias-e.v_biasy),d=L(c,h,a.x,a.y),v=L(c+n.x,h+n.y,-a.y,a.x),m=(s.bias-p)*u,g=s.jBias;s.jBias=o(g+m,0);var y=-(s.bounce+d)*u,w=s.jnAcc;s.jnAcc=o(w+y,0);var E=r*s.jnAcc,S=-v*s.tMass,x=s.jtAcc;s.jtAcc=b(x+S,-E,E);var T=a.x*(s.jBias-g),N=a.y*(s.jBias-g);Xn(e,-T,-N,f),Xn(t,T,N,l);var C=s.jnAcc-w,k=s.jtAcc-x;Wn(e,t,f,l,a.x*C-a.y*k,a.x*k+a.y*C)}},cn.prototype.callSeparate=function(e){var t=e.lookupHandler(this.a.collision_type,this.b.collision_type);t.separate(this,e)},cn.prototype.next=function(e){return this.body_a==e?this.thread_a_next:this.thread_b_next};var mn=0,gn=function(e,t,n,r){this.p=e,this.n=t,this.dist=n,this.r1=this.r2=C,this.nMass=this.tMass=this.bounce=this.bias=0,this.jnAcc=this.jtAcc=this.jBias=0,this.hash=r,mn++},yn=[],bn=function(e,t,n,r){var i=n+r,s=_(t,e),o=U(s);if(o>=i*i)return;var u=Math.sqrt(o);return new gn(M(e,P(s,.5+(n-.5*i)/(u?u:Infinity))),u?P(s,1/u):new N(1,0),u-i,0)},wn=function(e,t){var n=bn(e.tc,t.tc,e.r,t.r);return n?[n]:yn},En=function(e,t){var n=t.ta,r=t.tb,i=e.tc,s=_(r,n),o=w(k(s,_(i,n))/U(s)),u=M(n,P(s,o)),a=bn(i,u,e.r,t.r);if(a){var f=a.n;return o===0&&k(f,t.a_tangent)<0||o===1&&k(f,t.b_tangent)<0?yn:[a]}return yn},Sn=0,xn=function(e,t){var n=0,r=e.valueOnAxis(t[0].n,t[0].d);if(r>0)return-1;for(var i=1;i<t.length;i++){var s=e.valueOnAxis(t[i].n,t[i].d);if(s>0)return-1;s>r&&(r=s,n=i)}return Sn=r,n},Tn=function(e,t,n,r){var i=[],s=e.tVerts;for(var o=0;o<s.length;o+=2){var a=s[o],f=s[o+1];t.containsVertPartial(a,f,D(n))&&i.push(new gn(new N(a,f),n,r,u(e.hashid,o)))}var l=t.tVerts;for(var o=0;o<l.length;o+=2){var a=l[o],f=l[o+1];e.containsVertPartial(a,f,n)&&i.push(new gn(new N(a,f),n,r,u(t.hashid,o)))}return i},Nn=function(e,t,n,r){var i=[],s=e.tVerts;for(var o=0;o<s.length;o+=2){var a=s[o],f=s[o+1];t.containsVert(a,f)&&i.push(new gn(new N(a,f),n,r,u(e.hashid,o>>1)))}var l=t.tVerts;for(var o=0;o<l.length;o+=2){var a=l[o],f=l[o+1];e.containsVert(a,f)&&i.push(new gn(new N(a,f),n,r,u(t.hashid,o>>1)))}return i.length?i:Tn(e,t,n,r)},Cn=function(e,t){var n=xn(t,e.tAxes);if(n==-1)return yn;var r=Sn,i=xn(e,t.tAxes);if(i==-1)return yn;var s=Sn;return r>s?Nn(e,t,e.tAxes[n].n,r):Nn(e,t,D(t.tAxes[i].n),s)},kn=function(e,t,n){var r=k(t,e.ta)-e.r,i=k(t,e.tb)-e.r;return s(r,i)-n},Ln=function(e,t,n,r,i){var s=H(t.tn,t.ta),o=H(t.tn,t.tb),a=P(t.tn,i),f=n.tVerts;for(var l=0;l<f.length;l+=2){var c=f[l],h=f[l+1];if(L(c,h,a.x,a.y)<k(t.tn,t.ta)*i+t.r){var p=B(t.tn.x,t.tn.y,c,h);s>=p&&p>=o&&e.push(new gn(new N(c,h),a,r,u(n.hashid,l)))}}},An=function(e,t){var n=[],r=t.tAxes,i=r.length,s=k(e.tn,e.ta),o=t.valueOnAxis(e.tn,s)-e.r,a=t.valueOnAxis(D(e.tn),-s)-e.r;if(a>0||o>0)return yn;var f=0,l=kn(e,r[0].n,r[0].d);if(l>0)return yn;for(var c=0;c<i;c++){var h=kn(e,r[c].n,r[c].d);if(h>0)return yn;h>l&&(l=h,f=c)}var p=D(r[f].n),d=M(e.ta,P(p,e.r)),v=M(e.tb,P(p,e.r));t.containsVert(d.x,d.y)&&n.push(new gn(d,p,l,u(e.hashid,0))),t.containsVert(v.x,v.y)&&n.push(new gn(v,p,l,u(e.hashid,1)));if(o>=l||a>=l)o>a?Ln(n,e,t,o,1):Ln(n,e,t,a,-1);if(n.length===0){var m=f*2,g=t.tVerts,y=new N(g[m],g[m+1]),b;if(b=bn(e.ta,y,e.r,0,n))return[b];if(b=bn(e.tb,y,e.r,0,n))return[b];var w=i*2,E=new N(g[(m+2)%w],g[(m+3)%w]);if(b=bn(e.ta,E,e.r,0,n))return[b];if(b=bn(e.tb,E,e.r,0,n))return[b]}return n},On=function(e,t){var n=t.tAxes,r=0,i=k(n[0].n,e.tc)-n[0].d-e.r;for(var s=0;s<n.length;s++){var o=k(n[s].n,e.tc)-n[s].d-e.r;if(o>0)return yn;o>i&&(i=o,r=s)}var u=n[r].n,a=t.tVerts,f=a.length,l=r<<1,c=a[l],h=a[l+1],p=a[(l+2)%f],d=a[(l+3)%f],v=B(u.x,u.y,c,h),m=B(u.x,u.y,p,d),g=H(u,e.tc);if(g<m){var y=bn(e.tc,new N(p,d),e.r,0,y);return y?[y]:yn}if(g<v)return[new gn(_(e.tc,P(u,e.r+i/2)),D(u),i,0)];var y=bn(e.tc,new N(c,h),e.r,0,y);return y?[y]:yn};Tt.prototype.collisionCode=0,Ct.prototype.collisionCode=1,Lt.prototype.collisionCode=2,Tt.prototype.collisionTable=[wn,En,On],Ct.prototype.collisionTable=[null,function(e,e){return yn},An],Lt.prototype.collisionTable=[null,null,Cn];var Mn=e.collideShapes=function(e,n){return t(e.collisionCode<=n.collisionCode,"Collided shapes must be sorted by type"),e.collisionTable[n.collisionCode](e,n)},_n=new fn,Dn=e.Space=function(){this.stamp=0,this.curr_dt=0,this.bodies=[],this.rousedBodies=[],this.sleepingComponents=[],this.staticShapes=new It(null),this.activeShapes=new It(this.staticShapes),this.arbiters=[],this.contactBuffersHead=null,this.cachedArbiters={},this.constraints=[],this.locked=0,this.collisionHandlers={},this.defaultHandler=_n,this.postStepCallbacks=[],this.iterations=10,this.gravity=C,this.damping=1,this.idleSpeedThreshold=0,this.sleepTimeThreshold=Infinity,this.collisionSlop=.1,this.collisionBias=Math.pow(.9,60),this.collisionPersistence=3,this.enableContactGraph=!1,this.staticBody=new _t(Infinity,Infinity),this.staticBody.nodeIdleTime=Infinity,this.collideShapes=this.makeCollideShapes()};Dn.prototype.getCurrentTimeStep=function(){return this.curr_dt},Dn.prototype.isLocked=function(){return this.locked};var Pn=function(e){t(!e.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.")};Dn.prototype.addCollisionHandler=function(e,t,n,r,i,s){Pn(this),this.removeCollisionHandler(e,t);var o=new fn;o.a=e,o.b=t,n&&(o.begin=n),r&&(o.preSolve=r),i&&(o.postSolve=i),s&&(o.separate=s),this.collisionHandlers[u(e,t)]=o},Dn.prototype.removeCollisionHandler=function(e,t){Pn(this),delete this.collisionHandlers[u(e,t)]},Dn.prototype.setDefaultCollisionHandler=function(e,t,n,r){Pn(this);var i=new fn;e&&(i.begin=e),t&&(i.preSolve=t),n&&(i.postSolve=n),r&&(i.separate=r),this.defaultHandler=i},Dn.prototype.lookupHandler=function(e,t){return this.collisionHandlers[u(e,t)]||this.defaultHandler},Dn.prototype.addShape=
-function(e){var n=e.body;return n.isStatic()?this.addStaticShape(e):(t(!e.space,"This shape is already added to a space and cannot be added to another."),Pn(this),n.activate(),n.addShape(e),e.update(n.p,n.rot),this.activeShapes.insert(e,e.hashid),e.space=this,e)},Dn.prototype.addStaticShape=function(e){t(!e.space,"This shape is already added to a space and cannot be added to another."),Pn(this);var n=e.body;return n.addShape(e),e.update(n.p,n.rot),this.staticShapes.insert(e,e.hashid),e.space=this,e},Dn.prototype.addBody=function(e){return t(!e.isStatic(),"Static bodies cannot be added to a space as they are not meant to be simulated."),t(!e.space,"This body is already added to a space and cannot be added to another."),Pn(this),this.bodies.push(e),e.space=this,e},Dn.prototype.addConstraint=function(e){t(!e.space,"This shape is already added to a space and cannot be added to another."),Pn(this);var n=e.a,r=e.b;return n.activate(),r.activate(),this.constraints.push(e),e.next_a=n.constraintList,n.constraintList=e,e.next_b=r.constraintList,r.constraintList=e,e.space=this,e},Dn.prototype.filterArbiters=function(e,t){for(var n in this.cachedArbiters){var r=this.cachedArbiters[n];if(e===r.body_a&&(t===r.a||t===null)||e===r.body_b&&(t===r.b||t===null))t&&r.state!=="cached"&&r.callSeparate(this),r.unthread(),a(this.arbiters,r),delete this.cachedArbiters[n]}},Dn.prototype.removeShape=function(e){var n=e.body;n.isStatic()?this.removeStaticShape(e):(t(this.containsShape(e),"Cannot remove a shape that was not added to the space. (Removed twice maybe?)"),Pn(this),n.activate(),n.removeShape(e),this.filterArbiters(n,e),this.activeShapes.remove(e,e.hashid),e.space=null)},Dn.prototype.removeStaticShape=function(e){t(this.containsShape(e),"Cannot remove a static or sleeping shape that was not added to the space. (Removed twice maybe?)"),Pn(this);var n=e.body;n.isStatic()&&n.activateStatic(e),n.removeShape(e),this.filterArbiters(n,e),this.staticShapes.remove(e,e.hashid),e.space=null},Dn.prototype.removeBody=function(e){t(this.containsBody(e),"Cannot remove a body that was not added to the space. (Removed twice maybe?)"),Pn(this),e.activate(),a(this.bodies,e),e.space=null},Dn.prototype.removeConstraint=function(e){t(this.containsConstraint(e),"Cannot remove a constraint that was not added to the space. (Removed twice maybe?)"),Pn(this),e.a.activate(),e.b.activate(),a(this.constraints,e),e.a.removeConstraint(e),e.b.removeConstraint(e),e.space=null},Dn.prototype.containsShape=function(e){return e.space===this},Dn.prototype.containsBody=function(e){return e.space==this},Dn.prototype.containsConstraint=function(e){return e.space==this},Dn.prototype.uncacheArbiter=function(e){delete this.cachedArbiters[u(e.a.hashid,e.b.hashid)],a(this.arbiters,e)},Dn.prototype.eachBody=function(e){this.lock();var t=this.bodies;for(var n=0;n<t.length;n++)e(t[n]);var r=this.sleepingComponents;for(var n=0;n<r.length;n++){var i=r[n],s=i;while(s){var o=s.nodeNext;e(s),s=o}}this.unlock(!0)},Dn.prototype.eachShape=function(e){this.lock(),this.activeShapes.each(e),this.staticShapes.each(e),this.unlock(!0)},Dn.prototype.eachConstraint=function(e){this.lock();var t=this.constraints;for(var n=0;n<t.length;n++)e(t[n]);this.unlock(!0)},Dn.prototype.reindexStatic=function(){t(!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(e){var t=e.body;e.update(t.p,t.rot)}),this.staticShapes.reindex()},Dn.prototype.reindexShape=function(e){t(!this.locked,"You cannot manually reindex objects while the space is locked. Wait until the current query or step is complete.");var n=e.body;e.update(n.p,n.rot),this.activeShapes.reindexObject(e,e.hashid),this.staticShapes.reindexObject(e,e.hashid)},Dn.prototype.reindexShapesForBody=function(e){for(var t=e.shapeList;t;t=t.next)this.reindexShape(t)},Dn.prototype.useSpatialHash=function(e,t){throw new Error("Spatial Hash not yet implemented!");var n,r},Dn.prototype.activateBody=function(e){t(!e.isRogue(),"Internal error: Attempting to activate a rogue body.");if(this.locked)this.rousedBodies.indexOf(e)===-1&&this.rousedBodies.push(e);else{this.bodies.push(e);for(var n=0;n<e.shapeList.length;n++){var r=e.shapeList[n];this.staticShapes.remove(r,r.hashid),this.activeShapes.insert(r,r.hashid)}for(var i=e.arbiterList;i;i=i.next(e)){var s=i.body_a;if(e===s||s.isStatic()){var o=i.a,a=i.b;this.cachedArbiters[u(o.hashid,a.hashid)]=i,i.stamp=this.stamp,i.handler=this.lookupHandler(o.collision_type,a.collision_type),this.arbiters.push(i)}}for(var f=e.constraintList;f;f=f.nodeNext){var s=f.a;(e===s||s.isStatic())&&this.constraints.push(f)}}},Dn.prototype.deactivateBody=function(e){t(!e.isRogue(),"Internal error: Attempting to deactivate a rogue body."),a(this.bodies,e);for(var n=0;n<e.shapeList.length;n++){var r=e.shapeList[n];this.activeShapes.remove(r,r.hashid),this.staticShapes.insert(r,r.hashid)}for(var i=e.arbiterList;i;i=i.next(e)){var s=i.body_a;(e===s||s.isStatic())&&this.uncacheArbiter(i)}for(var o=e.constraintList;o;o=o.nodeNext){var s=o.a;(e===s||s.isStatic())&&a(this.constraints,o)}};var Hn=function(e){return e?e.nodeRoot:null},Bn=function(e){if(!e||!e.isSleeping(e))return;t(!e.isRogue(),"Internal Error: componentActivate() called on a rogue body.");var n=e.space,r=e;while(r){var i=r.nodeNext;r.nodeIdleTime=0,r.nodeRoot=null,r.nodeNext=null,n.activateBody(r),r=i}a(n.sleepingComponents,e)};_t.prototype.activate=function(){this.isRogue()||(this.nodeIdleTime=0,Bn(Hn(this)))},_t.prototype.activateStatic=function(e){t(this.isStatic(),"Body.activateStatic() called on a non-static body.");for(var n=this.arbiterList;n;n=n.next(this))(!e||e==n.a||e==n.b)&&(n.body_a==this?n.body_b:n.body_a).activate()},_t.prototype.pushArbiter=function(e){n((e.body_a===this?e.thread_a_next:e.thread_b_next)===null,"Internal Error: Dangling contact graph pointers detected. (A)"),n((e.body_a===this?e.thread_a_prev:e.thread_b_prev)===null,"Internal Error: Dangling contact graph pointers detected. (B)");var t=this.arbiterList;n(t===null||(t.body_a===this?t.thread_a_prev:t.thread_b_prev)===null,"Internal Error: Dangling contact graph pointers detected. (C)"),e.body_a===this?e.thread_a_next=t:e.thread_b_next=t,t&&(t.body_a===this?t.thread_a_prev=e:t.thread_b_prev=e),this.arbiterList=e};var jn=function(e,t){t.nodeRoot=e,t!==e&&(t.nodeNext=e.nodeNext,e.nodeNext=t)},Fn=function(e,t){if(!t.isRogue()){var r=Hn(t);if(r==null){jn(e,t);for(var i=t.arbiterList;i;i=i.next(t))Fn(e,t==i.body_a?i.body_b:i.body_a);for(var s=t.constraintList;s;s=s.next(t))Fn(e,t==s.a?s.b:s.a)}else n(r===e,"Internal Error: Inconsistency detected in the contact graph.")}},In=function(e,t){for(var n=e;n;n=n.nodeNext)if(n.nodeIdleTime<t)return!0;return!1};Dn.prototype.processComponents=function(e){var t=this.sleepTimeThreshold!==Infinity,r=this.bodies;for(var i=0;i<r.length;i++){var s=r[i];n(s.nodeNext===null,"Internal Error: Dangling next pointer detected in contact graph."),n(s.nodeRoot===null,"Internal Error: Dangling root pointer detected in contact graph.")}if(t){var o=this.idleSpeedThreshold,u=o?o*o:U(this.gravity)*e*e;for(var i=0;i<r.length;i++){var s=r[i],a=u?s.m*u:0;s.nodeIdleTime=s.kineticEnergy()>a?0:s.nodeIdleTime+e}}var f=this.arbiters;for(var i=0,l=f.length;i<l;i++){var c=f[i],h=c.body_a,p=c.body_b;t&&((p.isRogue()&&!p.isStatic()||h.isSleeping())&&h.activate(),(h.isRogue()&&!h.isStatic()||p.isSleeping())&&p.activate()),h.pushArbiter(c),p.pushArbiter(c)}if(t){var d=this.constraints;for(var i=0;i<d.length;i++){var v=d[i],h=v.a,p=v.b;p.isRogue()&&!p.isStatic()&&h.activate(),h.isRogue()&&!h.isStatic()&&p.activate()}for(var i=0;i<r.length;){var s=r[i];if(Hn(s)===null){Fn(s,s);if(!In(s,this.sleepTimeThreshold)){this.sleepingComponents.push(s);for(var m=s;m;m=m.nodeNext)this.deactivateBody(m);continue}}i++,s.nodeRoot=null,s.nodeNext=null}}},_t.prototype.sleep=function(){this.sleepWithGroup(null)},_t.prototype.sleepWithGroup=function(e){t(!this.isStatic()&&!this.isRogue(),"Rogue and static bodies cannot be put to sleep.");var n=this.space;t(n,"Cannot put a rogue body to sleep."),t(!n.locked,"Bodies cannot be put to sleep during a query or a call to cpSpaceStep(). Put these calls into a post-step callback."),t(e===null||e.isSleeping(),"Cannot use a non-sleeping body as a group identifier.");if(this.isSleeping()){t(Hn(this)===Hn(e),"The body is already sleeping and it's group cannot be reassigned.");return}for(var r=0;r<body.shapeList.length;r++)body.shapeList.update(this.p,this.rot);n.deactivateBody(this);if(e){var i=Hn(e);this.nodeRoot=i,this.nodeNext=i.nodeNext,this.nodeIdleTime=0,i.nodeNext=this}else this.nodeRoot=this,this.nodeNext=null,this.nodeIdleTime=0,n.sleepingComponents.push(this);a(n.bodies,this)},Dn.prototype.activateShapesTouchingShape=function(e){this.sleepTimeThreshold!==Infinity&&this.shapeQuery(e,function(e,t){e.body.activate()})},Dn.prototype.pointQuery=function(e,t,n,r){var i=function(i){(!i.group||n!==i.group)&&t&i.layers&&i.pointQuery(e)&&r(i)};this.lock(),this.activeShapes.pointQuery(e,i),this.staticShapes.pointQuery(e,i),this.unlock(!0)},Dn.prototype.pointQueryFirst=function(e,t,n){var r=null;return this.pointQuery(e,t,n,function(e){e.sensor||(r=e)}),r},Dn.prototype.segmentQuery=function(e,t,n,r,i){var s=function(s){var o;return(!s.group||r!==s.group)&&n&s.layers&&(o=s.segmentQuery(e,t))&&i(s,o.t,o.n),1};this.lock(),this.staticShapes.segmentQuery(e,t,1,s),this.activeShapes.segmentQuery(e,t,1,s),this.unlock(!0)},Dn.prototype.segmentQueryFirst=function(e,t,n,r){var i=null,s=function(s){var o;return(!s.group||r!==s.group)&&n&s.layers&&!s.sensor&&(o=s.segmentQuery(e,t))&&(i===null||o.t<i.t)&&(i=o),i?i.t:1};return this.staticShapes.segmentQuery(e,t,1,s),this.activeShapes.segmentQuery(e,t,i?i.t:1,s),i},Dn.prototype.bbQuery=function(e,t,n,r){var i=function(i){(!i.group||n!==i.group)&&t&i.layers&&ot(e,i.bb_l,i.bb_b,i.bb_r,i.bb_t)&&r(i)};this.lock(),this.activeShapes.query(e,i),this.staticShapes.query(e,i),this.unlock(!0)},Dn.prototype.shapeQuery=function(e,t){var n=e.body;n&&e.update(n.p,n.rot);var r=new rt(e.bb_l,e.bb_b,e.bb_r,e.bb_t),i=!1,s=function(n){var r=e;if(r.group&&r.group===n.group||!(r.layers&n.layers)||r===n)return;var s;if(r.collisionCode<=n.collisionCode)s=cpCollideShapes(r,n);else{s=cpCollideShapes(n,r);for(var o=0;o<s.length;o++)s[o].n=D(s[o].n)}if(s.length){i=!r.sensor&&!n.sensor;if(t){var u=new Array(s.length);for(var o=0;o<s.length;o++)u[o]=new hn(s[o].p,s[o].n,s[o].dist);t(n,u)}}};return this.lock(),this.activeShapes.query(r,s),this.staticShapes.query(r,s),this.unlock(!0),i},Dn.prototype.addPostStepCallback=function(e){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(e)},Dn.prototype.runPostStepCallbacks=function(){for(var e=0;e<this.postStepCallbacks.length;e++)this.postStepCallbacks[e]();this.postStepCallbacks=[]},Dn.prototype.lock=function(){this.locked++},Dn.prototype.unlock=function(e){this.locked--,t(this.locked>=0,"Internal Error: Space lock underflow.");if(!this.locked&&e){var n=this.rousedBodies;for(var r=0;r<n.length;r++)this.activateBody(n[r]);n.length=0,this.runPostStepCallbacks()}},Dn.prototype.makeCollideShapes=function(){var e=this;return function(t,n){var r=e;if(!(t.bb_l<=n.bb_r&&n.bb_l<=t.bb_r&&t.bb_b<=n.bb_t&&n.bb_b<=t.bb_t)||t.body===n.body||t.group&&t.group===n.group||!(t.layers&n.layers))return;var i=r.lookupHandler(t.collision_type,n.collision_type),s=t.sensor||n.sensor;if(s&&i===_n)return;if(t.collisionCode>n.collisionCode){var o=t;t=n,n=o}var a=Mn(t,n);if(a.length===0)return;var f=u(t.hashid,n.hashid),l=r.cachedArbiters[f];l||(l=r.cachedArbiters[f]=new cn(t,n)),l.update(a,i,t,n),l.state=="first coll"&&!i.begin(l,r)&&l.ignore(),l.state!=="ignore"&&i.preSolve(l,r)&&!s?r.arbiters.push(l):(l.contacts=null,l.state!=="ignore"&&(l.state="normal")),l.stamp=r.stamp}},Dn.prototype.arbiterSetFilter=function(e){var t=this.stamp-e.stamp,n=e.body_a,r=e.body_b;return(n.isStatic()||n.isSleeping())&&(r.isStatic()||r.isSleeping())?!0:(t>=1&&e.state!="cached"&&(e.callSeparate(this),e.state="cached"),t>=this.collisionPersistence?(e.contacts=null,!1):!0)};var qn=function(e){var t=e.body;e.update(t.p,t.rot)};Dn.prototype.step=function(e){if(e===0)return;t(C.x===0&&C.y===0,"vzero is invalid"),this.stamp++;var n=this.curr_dt;this.curr_dt=e;var r,i,s,o=this.bodies,u=this.constraints,a=this.arbiters;for(r=0;r<a.length;r++){var f=a[r];f.state="normal",!f.body_a.isSleeping()&&!f.body_b.isSleeping()&&f.unthread()}a.length=0,this.lock();for(r=0;r<o.length;r++)o[r].position_func(e);this.activeShapes.each(qn),this.activeShapes.reindexQuery(this.collideShapes),this.unlock(!1),this.processComponents(e),this.lock();for(s in this.cachedArbiters)this.arbiterSetFilter(this.cachedArbiters[s])||delete this.cachedArbiters[s];var l=this.collisionSlop,c=1-Math.pow(this.collisionBias,e);for(r=0;r<a.length;r++)a[r].preStep(e,l,c);for(r=0;r<u.length;r++){var h=u[r];h.preSolve(this),h.preStep(e)}var p=Math.pow(this.damping,e),d=this.gravity;for(r=0;r<o.length;r++)o[r].velocity_func(d,p,e);var v=n===0?0:e/n;for(r=0;r<a.length;r++)a[r].applyCachedImpulse(v);for(r=0;r<u.length;r++)u[r].applyCachedImpulse(v);for(r=0;r<this.iterations;r++){for(i=0;i<a.length;i++)a[i].applyImpulse();for(i=0;i<u.length;i++)u[i].applyImpulse()}for(r=0;r<u.length;r++)u[r].postSolve(this);for(r=0;r<a.length;r++)a[r].handler.postSolve(f,this);this.unlock(!0)};var Rn=function(e,t,n,r){var i=e.vx+ -n.y*e.w,s=e.vy+n.x*e.w,o=t.vx+ -r.y*t.w,u=t.vy+r.x*t.w;return new N(o-i,u-s)},Un=function(e,t,n,r,i){var s=e.vx+ -n.y*e.w,o=e.vy+n.x*e.w,u=t.vx+ -r.y*t.w,a=t.vy+r.x*t.w;return L(u-s,a-o,i.x,i.y)},zn=function(e,t,n,r){e.vx+=t*e.m_inv,e.vy+=n*e.m_inv,e.w+=e.i_inv*(r.x*n-r.y*t)},Wn=function(e,t,n,r,i,s){zn(e,-i,-s,n),zn(t,i,s,r)},Xn=function(e,t,n,r){e.v_biasx+=t*e.m_inv,e.v_biasy+=n*e.m_inv,e.w_bias+=e.i_inv*B(r.x,r.y,t,n)},Vn=function(e,t,n){var r=H(t,n);return e.m_inv+e.i_inv*r*r},$n=function(e,t,r,i,s){var o=Vn(e,r,s)+Vn(t,i,s);return n(o!==0,"Unsolvable collision or constraint."),o},Jn=function(e,t,r,i,s,o){var u,a,f,l,c=e.m_inv+t.m_inv;u=c,a=0,f=0,l=c;var h=e.i_inv,p=r.x*r.x*h,d=r.y*r.y*h,v=-r.x*r.y*h;u+=d,a+=v,f+=v,l+=p;var m=t.i_inv,g=i.x*i.x*m,y=i.y*i.y*m,b=-i.x*i.y*m;u+=y,a+=b,f+=b,l+=g;var w=u*l-a*f;n(w!==0,"Unsolvable constraint.");var E=1/w;s.x=l*E,s.y=-a*E,o.x=-f*E,o.y=u*E},Kn=function(e,t,n){return new N(k(e,t),k(e,n))},Qn=function(e,t){return 1-Math.pow(e,t)},Gn=e.Constraint=function(e,t){this.a=e,this.b=t,this.space=null,this.next_a=null,this.next_b=null,this.maxForce=Infinity,this.errorBias=Math.pow(.9,60),this.maxBias=Infinity};Gn.prototype.activateBodies=function(){this.a&&this.a.activate(),this.b&&this.b.activate()},Gn.prototype.preStep=function(e){},Gn.prototype.applyCachedImpulse=function(e){},Gn.prototype.applyImpulse=function(){},Gn.prototype.getImpulse=function(){return 0},Gn.prototype.preSolve=function(e){},Gn.prototype.postSolve=function(e){},Gn.prototype.next=function(e){return this.a===e?this.next_a:this.next_b};var Yn=e.PinJoint=function(e,t,r,i){Gn.call(this,e,t),this.anchr1=r,this.anchr2=i;var s=e?M(e.p,q(r,e.rot)):r,o=t?M(t.p,q(i,t.rot)):i;this.dist=A(_(o,s)),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};Yn.prototype=Object.create(Gn.prototype),Yn.prototype.preStep=function(e){var t=this.a,n=this.b;this.r1=q(this.anchr1,t.rot),this.r2=q(this.anchr2,n.rot);var r=_(M(n.p,this.r2),M(t.p,this.r1)),i=A(r);this.n=P(r,1/(i?i:Infinity)),this.nMass=1/$n(t,n,this.r1,this.r2,this.n);var s=this.maxBias;this.bias=b(-Qn(this.errorBias,e)*(i-this.dist)/e,-s,s),this.jnMax=this.maxForce*e},Yn.prototype.applyCachedImpulse=function(e){var t=P(this.n,this.jnAcc*e);Wn(this.a,this.b,this.r1,this.r2,t.x,t.y)},Yn.prototype.applyImpulse=function(){var e=this.a,t=this.b,n=this.n,r=Un(e,t,this.r1,this.r2,n),i=(this.bias-r)*this.nMass,s=this.jnAcc;this.jnAcc=b(s+i,-this.jnMax,this.jnMax),i=this.jnAcc-s,Wn(e,t,this.r1,this.r2,n.x*i,n.y*i)},Yn.prototype.getImpulse=function(){return Math.abs(this.jnAcc)};var Zn=e.SlideJoint=function(e,t,n,r,i,s){Gn.call(this,e,t),this.anchr1=n,this.anchr2=r,this.min=i,this.max=s,this.r1=this.r2=this.n=null,this.nMass=0,this.jnAcc=this.jnMax=0,this.bias=0};Zn.prototype=Object.create(Gn.prototype),Zn.prototype.preStep=function(e){var t=this.a,n=this.b;this.r1=q(this.anchr1,t.rot),this.r2=q(this.anchr2,n.rot);var r=_(M(n.p,this.r2),M(t.p,this.r1)),i=A(r),s=0;i>this.max?(s=i-this.max,this.n=X(r)):i<this.min?(s=this.min-i,this.n=D(X(r))):(this.n=C,this.jnAcc=0),this.nMass=1/$n(t,n,this.r1,this.r2,this.n);var o=this.maxBias;this.bias=b(-Qn(this.errorBias,e)*s/e,-o,o),this.jnMax=this.maxForce*e},Zn.prototype.applyCachedImpulse=function(e){var t=this.jnAcc*e;Wn(this.a,this.b,this.r1,this.r2,this.n.x*t,this.n.y*t)},Zn.prototype.applyImpulse=function(){if(this.n.x===0&&this.n.y===0)return;var e=this.a,t=this.b,n=this.n,r=this.r1,i=this.r2,s=Rn(e,t,r,i),o=k(s,n),u=(this.bias-o)*this.nMass,a=this.jnAcc;this.jnAcc=b(a+u,-this.jnMax,0),u=this.jnAcc-a,Wn(e,t,this.r1,this.r2,n.x*u,n.y*u)},Zn.prototype.getImpulse=function(){return Math.abs(this.jnAcc)};var er=e.PivotJoint=function(e,t,n,r){Gn.call(this,e,t);if(typeof r=="undefined"){var i=n;n=e?e.world2Local(i):i,r=t?t.world2Local(i):i}this.anchr1=n,this.anchr2=r,this.r1=this.r2=C,this.k1=new N(0,0),this.k2=new N(0,0),this.jAcc=C,this.jMaxLen=0,this.bias=C};er.prototype=Object.create(Gn.prototype),er.prototype.preStep=function(e){var t=this.a,n=this.b;this.r1=q(this.anchr1,t.rot),this.r2=q(this.anchr2,n.rot),Jn(t,n,this.r1,this.r2,this.k1,this.k2),this.jMaxLen=this.maxForce*e;var r=_(M(n.p,this.r2),M(t.p,this.r1));this.bias=V(P(r,-Qn(this.errorBias,e)/e),this.maxBias)},er.prototype.applyCachedImpulse=function(e){Wn(this.a,this.b,this.r1,this.r2,this.jAcc.x*e,this.jAcc.y*e)},er.prototype.applyImpulse=function(){var e=this.a,t=this.b,n=this.r1,r=this.r2,i=Rn(e,t,n,r),s=Kn(_(this.bias,i),this.k1,this.k2),o=this.jAcc;this.jAcc=V(M(this.jAcc,s),this.jMaxLen),Wn(e,t,this.r1,this.r2,this.jAcc.x-o.x,this.jAcc.y-o.y)},er.prototype.getImpulse=function(){return A(this.jAcc)};var tr=e.GrooveJoint=function(e,t,n,r,i){Gn.call(this,e,t),this.grv_a=n,this.grv_b=r,this.grv_n=j(W(_(r,n))),this.anchr2=i,this.grv_tn=null,this.clamp=0,this.r1=this.r2=null,this.k1=new N(0,0),this.k2=new N(0,0),this.jAcc=C,this.jMaxLen=0,this.bias=null};tr.prototype=Object.create(Gn.prototype),tr.prototype.preStep=function(e){var t=this.a,n=this.b,r=t.local2World(this.grv_a),i=t.local2World(this.grv_b),s=q(this.grv_n,t.rot),o=k(r,s);this.grv_tn=s,this.r2=q(this.anchr2,n.rot);var u=H(M(n.p,this.r2),s);u<=H(r,s)?(this.clamp=1,this.r1=_(r,t.p)):u>=H(i,s)?(this.clamp=-1,this.r1=_(i,t.p)):(this.clamp=0,this.r1=_(M(P(j(s),-u),P(s,o)),t.p)),Jn(t,n,this.r1,this.r2,this.k1,this.k2),this.jMaxLen=this.maxForce*e;var a=_(M(n.p,this.r2),M(t.p,this.r1));this.bias=V(P(a,-Qn(this.errorBias,e)/e),this.maxBias)},tr.prototype.applyCachedImpulse=function(e){Wn(this.a,this.b,this.r1,this.r2,this.jAcc.x*e,this.jAcc.y*e)},tr.prototype.grooveConstrain=function(e){var t=this.grv_tn,n=this.clamp*H(e,t)>0?e:I(e,t);return V(n,this.jMaxLen)},tr.prototype.applyImpulse=function(){var e=this.a,t=this.b,n=this.r1,r=this.r2,i=Rn(e,t,n,r),s=Kn(_(this.bias,i),this.k1,this.k2),o=this.jAcc;this.jAcc=this.grooveConstrain(M(o,s)),Wn(e,t,this.r1,this.r2,this.jAcc.x-o.x,this.jAcc.y-o.y)},tr.prototype.getImpulse=function(){return A(this.jAcc)},tr.prototype.setGrooveA=function(e){this.grv_a=e,this.grv_n=j(W(_(this.grv_b,e))),this.activateBodies()},tr.prototype.setGrooveB=function(e){this.grv_b=e,this.grv_n=j(W(_(e,this.grv_a))),this.activateBodies()};var nr=function(e,t){return(e.restLength-t)*e.stiffness},rr=e.DampedSpring=function(e,t,n,r,i,s,o){Gn.call(this,e,t),this.anchr1=n,this.anchr2=r,this.restLength=i,this.stiffness=s,this.damping=o,this.springForceFunc=nr,this.target_vrn=this.v_coef=0,this.r1=this.r2=null,this.nMass=0,this.n=null};rr.prototype=Object.create(Gn.prototype),rr.prototype.preStep=function(e){var t=this.a,r=this.b;this.r1=q(this.anchr1,t.rot),this.r2=q(this.anchr2,r.rot);var i=_(M(r.p,this.r2),M(t.p,this.r1)),s=A(i);this.n=P(i,1/(s?s:Infinity));var o=$n(t,r,this.r1,this.r2,this.n);n(o!==0,"Unsolvable this."),this.nMass=1/o,this.target_vrn=0,this.v_coef=1-Math.exp(-this.damping*e*o);var u=this.springForceFunc(this,s);Wn(t,r,this.r1,this.r2,this.n.x*u*e,this.n.y*u*e)},rr.prototype.applyCachedImpulse=function(e){},rr.prototype.applyImpulse=function(){var e=this.a,t=this.b,n=this.n,r=this.r1,i=this.r2,s=Un(e,t,r,i,n),o=(this.target_vrn-s)*this.v_coef;this.target_vrn=s+o,o*=this.nMass,Wn(e,t,this.r1,this.r2,this.n.x*o,this.n.y*o)},rr.prototype.getImpulse=function(){return 0};var ir=function(e,t){return(t-e.restAngle)*e.stiffness},sr=e.DampedRotarySpring=function(e,t,n,r,i){Gn.call(this,e,t),this.restAngle=n,this.stiffness=r,this.damping=i,this.springTorqueFunc=ir,this.target_wrn=0,this.w_coef=0,this.iSum=0};sr.prototype=Object.create(Gn.prototype),sr.prototype.preStep=function(e){var t=this.a,r=this.b,i=t.i_inv+r.i_inv;n(i!==0,"Unsolvable spring."),this.iSum=1/i,this.w_coef=1-Math.exp(-this.damping*e*i),this.target_wrn=0;var s=this.springTorqueFunc(this,t.a-r.a)*e;t.w-=s*t.i_inv,r.w+=s*r.i_inv},sr.prototype.applyImpulse=function(){var e=this.a,t=this.b,n=e.w-t.w,r=(this.target_wrn-n)*this.w_coef;this.target_wrn=n+r;var i=r*this.iSum;e.w+=i*e.i_inv,t.w-=i*t.i_inv};var or=e.RotaryLimitJoint=function(e,t,n,r){Gn.call(this,e,t),this.min=n,this.max=r,this.jAcc=0,this.iSum=this.bias=this.jMax=0};or.prototype=Object.create(Gn.prototype),or.prototype.preStep=function(e){var t=this.a,n=this.b,r=n.a-t.a,i=0;r>this.max?i=this.max-r:r<this.min&&(i=this.min-r),this.iSum=1/(1/t.i+1/n.i);var s=this.maxBias;this.bias=b(-Qn(this.errorBias,e)*i/e,-s,s),this.jMax=this.maxForce*e,this.bias||(this.jAcc=0)},or.prototype.applyCachedImpulse=function(e){var t=this.a,n=this.b,r=this.jAcc*e;t.w-=r*t.i_inv,n.w+=r*n.i_inv},or.prototype.applyImpulse=function(){if(!this.bias)return;var e=this.a,t=this.b,n=t.w-e.w,r=-(this.bias+n)*this.iSum,i=this.jAcc;this.bias<0?this.jAcc=b(i+r,0,this.jMax):this.jAcc=b(i+r,-this.jMax,0),r=this.jAcc-i,e.w-=r*e.i_inv,t.w+=r*t.i_inv},or.prototype.getImpulse=function(){return Math.abs(joint.jAcc)};var ur=e.RatchetJoint=function(e,t,n,r){Gn.call(this,e,t),this.angle=0,this.phase=n,this.ratchet=r,this.angle=(t?t.a:0)-(e?e.a:0),this.iSum=this.bias=this.jAcc=this.jMax=0};ur.prototype=Object.create(Gn.prototype),ur.prototype.preStep=function(e){var t=this.a,n=this.b,r=this.angle,i=this.phase,s=this.ratchet,o=n.a-t.a,u=r-o,a=0;u*s>0?a=u:this.angle=Math.floor((o-i)/s)*s+i,this.iSum=1/(t.i_inv+n.i_inv);var f=this.maxBias;this.bias=b(-Qn(this.errorBias,e)*a/e,-f,f),this.jMax=this.maxForce*e,this.bias||(this.jAcc=0)},ur.prototype.applyCachedImpulse=function(e){var t=this.a,n=this.b,r=this.jAcc*e;t.w-=r*t.i_inv,n.w+=r*n.i_inv},ur.prototype.applyImpulse=function(){if(!this.bias)return;var e=this.a,t=this.b,n=t.w-e.w,r=this.ratchet,i=-(this.bias+n)*this.iSum,s=this.jAcc;this.jAcc=b((s+i)*r,0,this.jMax*Math.abs(r))/r,i=this.jAcc-s,e.w-=i*e.i_inv,t.w+=i*t.i_inv},ur.prototype.getImpulse=function(e){return Math.abs(e.jAcc)};var ar=e.GearJoint=function(e,t,n,r){Gn.call(this,e,t),this.phase=n,this.ratio=r,this.ratio_inv=1/r,this.jAcc=0,this.iSum=this.bias=this.jMax=0};ar.prototype=Object.create(Gn.prototype),ar.prototype.preStep=function(e){var t=this.a,n=this.b;this.iSum=1/(t.i_inv*this.ratio_inv+this.ratio*n.i_inv);var r=this.maxBias;this.bias=b(-Qn(this.errorBias,e)*(n.a*this.ratio-t.a-this.phase)/e,-r,r),this.jMax=this.maxForce*e},ar.prototype.applyCachedImpulse=function(e){var t=this.a,n=this.b,r=this.jAcc*e;t.w-=r*t.i_inv*this.ratio_inv,n.w+=r*n.i_inv},ar.prototype.applyImpulse=function(){var e=this.a,t=this.b,n=t.w*this.ratio-e.w,r=(this.bias-n)*this.iSum,i=this.jAcc;this.jAcc=b(i+r,-this.jMax,this.jMax),r=this.jAcc-i,e.w-=r*e.i_inv*this.ratio_inv,t.w+=r*t.i_inv},ar.prototype.getImpulse=function(){return Math.abs(this.jAcc)},ar.prototype.setRatio=function(e){this.ratio=e,this.ratio_inv=1/e,this.activateBodies()};var fr=e.SimpleMotor=function(e,t,n){Gn.call(this,e,t),this.rate=n,this.jAcc=0,this.iSum=this.jMax=0};fr.prototype=Object.create(Gn.prototype),fr.prototype.preStep=function(e){this.iSum=1/(this.a.i_inv+this.b.i_inv),this.jMax=this.maxForce*e},fr.prototype.applyCachedImpulse=function(e){var t=this.a,n=this.b,r=this.jAcc*e;t.w-=r*t.i_inv,n.w+=r*n.i_inv},fr.prototype.applyImpulse=function(){var e=this.a,t=this.b,n=t.w-e.w+this.rate,r=-n*this.iSum,i=this.jAcc;this.jAcc=b(i+r,-this.jMax,this.jMax),r=this.jAcc-i,e.w-=r*e.i_inv,t.w+=r*t.i_inv},fr.prototype.getImpulse=function(){return Math.abs(this.jAcc)}})();
+function(e){var n=e.body;return n.isStatic()?this.addStaticShape(e):(t(!e.space,"This shape is already added to a space and cannot be added to another."),Pn(this),n.activate(),n.addShape(e),e.update(n.p,n.rot),this.activeShapes.insert(e,e.hashid),e.space=this,e)},Dn.prototype.addStaticShape=function(e){t(!e.space,"This shape is already added to a space and cannot be added to another."),Pn(this);var n=e.body;return n.addShape(e),e.update(n.p,n.rot),this.staticShapes.insert(e,e.hashid),e.space=this,e},Dn.prototype.addBody=function(e){return t(!e.isStatic(),"Static bodies cannot be added to a space as they are not meant to be simulated."),t(!e.space,"This body is already added to a space and cannot be added to another."),Pn(this),this.bodies.push(e),e.space=this,e},Dn.prototype.addConstraint=function(e){t(!e.space,"This shape is already added to a space and cannot be added to another."),Pn(this);var n=e.a,r=e.b;return n.activate(),r.activate(),this.constraints.push(e),e.next_a=n.constraintList,n.constraintList=e,e.next_b=r.constraintList,r.constraintList=e,e.space=this,e},Dn.prototype.filterArbiters=function(e,t){for(var n in this.cachedArbiters){var r=this.cachedArbiters[n];if(e===r.body_a&&(t===r.a||t===null)||e===r.body_b&&(t===r.b||t===null))t&&r.state!=="cached"&&r.callSeparate(this),r.unthread(),a(this.arbiters,r),delete this.cachedArbiters[n]}},Dn.prototype.removeShape=function(e){var n=e.body;n.isStatic()?this.removeStaticShape(e):(t(this.containsShape(e),"Cannot remove a shape that was not added to the space. (Removed twice maybe?)"),Pn(this),n.activate(),n.removeShape(e),this.filterArbiters(n,e),this.activeShapes.remove(e,e.hashid),e.space=null)},Dn.prototype.removeStaticShape=function(e){t(this.containsShape(e),"Cannot remove a static or sleeping shape that was not added to the space. (Removed twice maybe?)"),Pn(this);var n=e.body;n.isStatic()&&n.activateStatic(e),n.removeShape(e),this.filterArbiters(n,e),this.staticShapes.remove(e,e.hashid),e.space=null},Dn.prototype.removeBody=function(e){t(this.containsBody(e),"Cannot remove a body that was not added to the space. (Removed twice maybe?)"),Pn(this),e.activate(),a(this.bodies,e),e.space=null},Dn.prototype.removeConstraint=function(e){t(this.containsConstraint(e),"Cannot remove a constraint that was not added to the space. (Removed twice maybe?)"),Pn(this),e.a.activate(),e.b.activate(),a(this.constraints,e),e.a.removeConstraint(e),e.b.removeConstraint(e),e.space=null},Dn.prototype.containsShape=function(e){return e.space===this},Dn.prototype.containsBody=function(e){return e.space==this},Dn.prototype.containsConstraint=function(e){return e.space==this},Dn.prototype.uncacheArbiter=function(e){delete this.cachedArbiters[u(e.a.hashid,e.b.hashid)],a(this.arbiters,e)},Dn.prototype.eachBody=function(e){this.lock();var t=this.bodies;for(var n=0;n<t.length;n++)e(t[n]);var r=this.sleepingComponents;for(var n=0;n<r.length;n++){var i=r[n],s=i;while(s){var o=s.nodeNext;e(s),s=o}}this.unlock(!0)},Dn.prototype.eachShape=function(e){this.lock(),this.activeShapes.each(e),this.staticShapes.each(e),this.unlock(!0)},Dn.prototype.eachConstraint=function(e){this.lock();var t=this.constraints;for(var n=0;n<t.length;n++)e(t[n]);this.unlock(!0)},Dn.prototype.reindexStatic=function(){t(!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(e){var t=e.body;e.update(t.p,t.rot)}),this.staticShapes.reindex()},Dn.prototype.reindexShape=function(e){t(!this.locked,"You cannot manually reindex objects while the space is locked. Wait until the current query or step is complete.");var n=e.body;e.update(n.p,n.rot),this.activeShapes.reindexObject(e,e.hashid),this.staticShapes.reindexObject(e,e.hashid)},Dn.prototype.reindexShapesForBody=function(e){for(var t=e.shapeList;t;t=t.next)this.reindexShape(t)},Dn.prototype.useSpatialHash=function(e,t){throw new Error("Spatial Hash not yet implemented!");var n,r},Dn.prototype.activateBody=function(e){t(!e.isRogue(),"Internal error: Attempting to activate a rogue body.");if(this.locked)this.rousedBodies.indexOf(e)===-1&&this.rousedBodies.push(e);else{this.bodies.push(e);for(var n=0;n<e.shapeList.length;n++){var r=e.shapeList[n];this.staticShapes.remove(r,r.hashid),this.activeShapes.insert(r,r.hashid)}for(var i=e.arbiterList;i;i=i.next(e)){var s=i.body_a;if(e===s||s.isStatic()){var o=i.a,a=i.b;this.cachedArbiters[u(o.hashid,a.hashid)]=i,i.stamp=this.stamp,i.handler=this.lookupHandler(o.collision_type,a.collision_type),this.arbiters.push(i)}}for(var f=e.constraintList;f;f=f.nodeNext){var s=f.a;(e===s||s.isStatic())&&this.constraints.push(f)}}},Dn.prototype.deactivateBody=function(e){t(!e.isRogue(),"Internal error: Attempting to deactivate a rogue body."),a(this.bodies,e);for(var n=0;n<e.shapeList.length;n++){var r=e.shapeList[n];this.activeShapes.remove(r,r.hashid),this.staticShapes.insert(r,r.hashid)}for(var i=e.arbiterList;i;i=i.next(e)){var s=i.body_a;(e===s||s.isStatic())&&this.uncacheArbiter(i)}for(var o=e.constraintList;o;o=o.nodeNext){var s=o.a;(e===s||s.isStatic())&&a(this.constraints,o)}};var Hn=function(e){return e?e.nodeRoot:null},Bn=function(e){if(!e||!e.isSleeping(e))return;t(!e.isRogue(),"Internal Error: componentActivate() called on a rogue body.");var n=e.space,r=e;while(r){var i=r.nodeNext;r.nodeIdleTime=0,r.nodeRoot=null,r.nodeNext=null,n.activateBody(r),r=i}a(n.sleepingComponents,e)};_t.prototype.activate=function(){this.isRogue()||(this.nodeIdleTime=0,Bn(Hn(this)))},_t.prototype.activateStatic=function(e){t(this.isStatic(),"Body.activateStatic() called on a non-static body.");for(var n=this.arbiterList;n;n=n.next(this))(!e||e==n.a||e==n.b)&&(n.body_a==this?n.body_b:n.body_a).activate()},_t.prototype.pushArbiter=function(e){n((e.body_a===this?e.thread_a_next:e.thread_b_next)===null,"Internal Error: Dangling contact graph pointers detected. (A)"),n((e.body_a===this?e.thread_a_prev:e.thread_b_prev)===null,"Internal Error: Dangling contact graph pointers detected. (B)");var t=this.arbiterList;n(t===null||(t.body_a===this?t.thread_a_prev:t.thread_b_prev)===null,"Internal Error: Dangling contact graph pointers detected. (C)"),e.body_a===this?e.thread_a_next=t:e.thread_b_next=t,t&&(t.body_a===this?t.thread_a_prev=e:t.thread_b_prev=e),this.arbiterList=e};var jn=function(e,t){t.nodeRoot=e,t!==e&&(t.nodeNext=e.nodeNext,e.nodeNext=t)},Fn=function(e,t){if(!t.isRogue()){var r=Hn(t);if(r==null){jn(e,t);for(var i=t.arbiterList;i;i=i.next(t))Fn(e,t==i.body_a?i.body_b:i.body_a);for(var s=t.constraintList;s;s=s.next(t))Fn(e,t==s.a?s.b:s.a)}else n(r===e,"Internal Error: Inconsistency detected in the contact graph.")}},In=function(e,t){for(var n=e;n;n=n.nodeNext)if(n.nodeIdleTime<t)return!0;return!1};Dn.prototype.processComponents=function(e){var t=this.sleepTimeThreshold!==Infinity,r=this.bodies;for(var i=0;i<r.length;i++){var s=r[i];n(s.nodeNext===null,"Internal Error: Dangling next pointer detected in contact graph."),n(s.nodeRoot===null,"Internal Error: Dangling root pointer detected in contact graph.")}if(t){var o=this.idleSpeedThreshold,u=o?o*o:U(this.gravity)*e*e;for(var i=0;i<r.length;i++){var s=r[i],a=u?s.m*u:0;s.nodeIdleTime=s.kineticEnergy()>a?0:s.nodeIdleTime+e}}var f=this.arbiters;for(var i=0,l=f.length;i<l;i++){var c=f[i],h=c.body_a,p=c.body_b;t&&((p.isRogue()&&!p.isStatic()||h.isSleeping())&&h.activate(),(h.isRogue()&&!h.isStatic()||p.isSleeping())&&p.activate()),h.pushArbiter(c),p.pushArbiter(c)}if(t){var d=this.constraints;for(var i=0;i<d.length;i++){var v=d[i],h=v.a,p=v.b;p.isRogue()&&!p.isStatic()&&h.activate(),h.isRogue()&&!h.isStatic()&&p.activate()}for(var i=0;i<r.length;){var s=r[i];if(Hn(s)===null){Fn(s,s);if(!In(s,this.sleepTimeThreshold)){this.sleepingComponents.push(s);for(var m=s;m;m=m.nodeNext)this.deactivateBody(m);continue}}i++,s.nodeRoot=null,s.nodeNext=null}}},_t.prototype.sleep=function(){this.sleepWithGroup(null)},_t.prototype.sleepWithGroup=function(e){t(!this.isStatic()&&!this.isRogue(),"Rogue and static bodies cannot be put to sleep.");var n=this.space;t(n,"Cannot put a rogue body to sleep."),t(!n.locked,"Bodies cannot be put to sleep during a query or a call to cpSpaceStep(). Put these calls into a post-step callback."),t(e===null||e.isSleeping(),"Cannot use a non-sleeping body as a group identifier.");if(this.isSleeping()){t(Hn(this)===Hn(e),"The body is already sleeping and it's group cannot be reassigned.");return}for(var r=0;r<body.shapeList.length;r++)body.shapeList.update(this.p,this.rot);n.deactivateBody(this);if(e){var i=Hn(e);this.nodeRoot=i,this.nodeNext=i.nodeNext,this.nodeIdleTime=0,i.nodeNext=this}else this.nodeRoot=this,this.nodeNext=null,this.nodeIdleTime=0,n.sleepingComponents.push(this);a(n.bodies,this)},Dn.prototype.activateShapesTouchingShape=function(e){this.sleepTimeThreshold!==Infinity&&this.shapeQuery(e,function(e,t){e.body.activate()})},Dn.prototype.pointQuery=function(e,t,n,r){var i=function(i){(!i.group||n!==i.group)&&t&i.layers&&i.pointQuery(e)&&r(i)};this.lock(),this.activeShapes.pointQuery(e,i),this.staticShapes.pointQuery(e,i),this.unlock(!0)},Dn.prototype.pointQueryFirst=function(e,t,n){var r=null;return this.pointQuery(e,t,n,function(e){e.sensor||(r=e)}),r},Dn.prototype.segmentQuery=function(e,t,n,r,i){var s=function(s){var o;return(!s.group||r!==s.group)&&n&s.layers&&(o=s.segmentQuery(e,t))&&i(s,o.t,o.n),1};this.lock(),this.staticShapes.segmentQuery(e,t,1,s),this.activeShapes.segmentQuery(e,t,1,s),this.unlock(!0)},Dn.prototype.segmentQueryFirst=function(e,t,n,r){var i=null,s=function(s){var o;return(!s.group||r!==s.group)&&n&s.layers&&!s.sensor&&(o=s.segmentQuery(e,t))&&(i===null||o.t<i.t)&&(i=o),i?i.t:1};return this.staticShapes.segmentQuery(e,t,1,s),this.activeShapes.segmentQuery(e,t,i?i.t:1,s),i},Dn.prototype.bbQuery=function(e,t,n,r){var i=function(i){(!i.group||n!==i.group)&&t&i.layers&&ot(e,i.bb_l,i.bb_b,i.bb_r,i.bb_t)&&r(i)};this.lock(),this.activeShapes.query(e,i),this.staticShapes.query(e,i),this.unlock(!0)},Dn.prototype.shapeQuery=function(e,t){var n=e.body;n&&e.update(n.p,n.rot);var r=new rt(e.bb_l,e.bb_b,e.bb_r,e.bb_t),i=!1,s=function(n){var r=e;if(r.group&&r.group===n.group||!(r.layers&n.layers)||r===n)return;var s;if(r.collisionCode<=n.collisionCode)s=Mn(r,n);else{s=Mn(n,r);for(var o=0;o<s.length;o++)s[o].n=D(s[o].n)}if(s.length){i=!r.sensor&&!n.sensor;if(t){var u=new Array(s.length);for(var o=0;o<s.length;o++)u[o]=new hn(s[o].p,s[o].n,s[o].dist);t(n,u)}}};return this.lock(),this.activeShapes.query(r,s),this.staticShapes.query(r,s),this.unlock(!0),i},Dn.prototype.addPostStepCallback=function(e){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(e)},Dn.prototype.runPostStepCallbacks=function(){for(var e=0;e<this.postStepCallbacks.length;e++)this.postStepCallbacks[e]();this.postStepCallbacks=[]},Dn.prototype.lock=function(){this.locked++},Dn.prototype.unlock=function(e){this.locked--,t(this.locked>=0,"Internal Error: Space lock underflow.");if(!this.locked&&e){var n=this.rousedBodies;for(var r=0;r<n.length;r++)this.activateBody(n[r]);n.length=0,this.runPostStepCallbacks()}},Dn.prototype.makeCollideShapes=function(){var e=this;return function(t,n){var r=e;if(!(t.bb_l<=n.bb_r&&n.bb_l<=t.bb_r&&t.bb_b<=n.bb_t&&n.bb_b<=t.bb_t)||t.body===n.body||t.group&&t.group===n.group||!(t.layers&n.layers))return;var i=r.lookupHandler(t.collision_type,n.collision_type),s=t.sensor||n.sensor;if(s&&i===_n)return;if(t.collisionCode>n.collisionCode){var o=t;t=n,n=o}var a=Mn(t,n);if(a.length===0)return;var f=u(t.hashid,n.hashid),l=r.cachedArbiters[f];l||(l=r.cachedArbiters[f]=new cn(t,n)),l.update(a,i,t,n),l.state=="first coll"&&!i.begin(l,r)&&l.ignore(),l.state!=="ignore"&&i.preSolve(l,r)&&!s?r.arbiters.push(l):(l.contacts=null,l.state!=="ignore"&&(l.state="normal")),l.stamp=r.stamp}},Dn.prototype.arbiterSetFilter=function(e){var t=this.stamp-e.stamp,n=e.body_a,r=e.body_b;return(n.isStatic()||n.isSleeping())&&(r.isStatic()||r.isSleeping())?!0:(t>=1&&e.state!="cached"&&(e.callSeparate(this),e.state="cached"),t>=this.collisionPersistence?(e.contacts=null,!1):!0)};var qn=function(e){var t=e.body;e.update(t.p,t.rot)};Dn.prototype.step=function(e){if(e===0)return;t(C.x===0&&C.y===0,"vzero is invalid"),this.stamp++;var n=this.curr_dt;this.curr_dt=e;var r,i,s,o=this.bodies,u=this.constraints,a=this.arbiters;for(r=0;r<a.length;r++){var f=a[r];f.state="normal",!f.body_a.isSleeping()&&!f.body_b.isSleeping()&&f.unthread()}a.length=0,this.lock();for(r=0;r<o.length;r++)o[r].position_func(e);this.activeShapes.each(qn),this.activeShapes.reindexQuery(this.collideShapes),this.unlock(!1),this.processComponents(e),this.lock();for(s in this.cachedArbiters)this.arbiterSetFilter(this.cachedArbiters[s])||delete this.cachedArbiters[s];var l=this.collisionSlop,c=1-Math.pow(this.collisionBias,e);for(r=0;r<a.length;r++)a[r].preStep(e,l,c);for(r=0;r<u.length;r++){var h=u[r];h.preSolve(this),h.preStep(e)}var p=Math.pow(this.damping,e),d=this.gravity;for(r=0;r<o.length;r++)o[r].velocity_func(d,p,e);var v=n===0?0:e/n;for(r=0;r<a.length;r++)a[r].applyCachedImpulse(v);for(r=0;r<u.length;r++)u[r].applyCachedImpulse(v);for(r=0;r<this.iterations;r++){for(i=0;i<a.length;i++)a[i].applyImpulse();for(i=0;i<u.length;i++)u[i].applyImpulse()}for(r=0;r<u.length;r++)u[r].postSolve(this);for(r=0;r<a.length;r++)a[r].handler.postSolve(f,this);this.unlock(!0)};var Rn=function(e,t,n,r){var i=e.vx+ -n.y*e.w,s=e.vy+n.x*e.w,o=t.vx+ -r.y*t.w,u=t.vy+r.x*t.w;return new N(o-i,u-s)},Un=function(e,t,n,r,i){var s=e.vx+ -n.y*e.w,o=e.vy+n.x*e.w,u=t.vx+ -r.y*t.w,a=t.vy+r.x*t.w;return L(u-s,a-o,i.x,i.y)},zn=function(e,t,n,r){e.vx+=t*e.m_inv,e.vy+=n*e.m_inv,e.w+=e.i_inv*(r.x*n-r.y*t)},Wn=function(e,t,n,r,i,s){zn(e,-i,-s,n),zn(t,i,s,r)},Xn=function(e,t,n,r){e.v_biasx+=t*e.m_inv,e.v_biasy+=n*e.m_inv,e.w_bias+=e.i_inv*B(r.x,r.y,t,n)},Vn=function(e,t,n){var r=H(t,n);return e.m_inv+e.i_inv*r*r},$n=function(e,t,r,i,s){var o=Vn(e,r,s)+Vn(t,i,s);return n(o!==0,"Unsolvable collision or constraint."),o},Jn=function(e,t,r,i,s,o){var u,a,f,l,c=e.m_inv+t.m_inv;u=c,a=0,f=0,l=c;var h=e.i_inv,p=r.x*r.x*h,d=r.y*r.y*h,v=-r.x*r.y*h;u+=d,a+=v,f+=v,l+=p;var m=t.i_inv,g=i.x*i.x*m,y=i.y*i.y*m,b=-i.x*i.y*m;u+=y,a+=b,f+=b,l+=g;var w=u*l-a*f;n(w!==0,"Unsolvable constraint.");var E=1/w;s.x=l*E,s.y=-a*E,o.x=-f*E,o.y=u*E},Kn=function(e,t,n){return new N(k(e,t),k(e,n))},Qn=function(e,t){return 1-Math.pow(e,t)},Gn=e.Constraint=function(e,t){this.a=e,this.b=t,this.space=null,this.next_a=null,this.next_b=null,this.maxForce=Infinity,this.errorBias=Math.pow(.9,60),this.maxBias=Infinity};Gn.prototype.activateBodies=function(){this.a&&this.a.activate(),this.b&&this.b.activate()},Gn.prototype.preStep=function(e){},Gn.prototype.applyCachedImpulse=function(e){},Gn.prototype.applyImpulse=function(){},Gn.prototype.getImpulse=function(){return 0},Gn.prototype.preSolve=function(e){},Gn.prototype.postSolve=function(e){},Gn.prototype.next=function(e){return this.a===e?this.next_a:this.next_b};var Yn=e.PinJoint=function(e,t,r,i){Gn.call(this,e,t),this.anchr1=r,this.anchr2=i;var s=e?M(e.p,q(r,e.rot)):r,o=t?M(t.p,q(i,t.rot)):i;this.dist=A(_(o,s)),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};Yn.prototype=Object.create(Gn.prototype),Yn.prototype.preStep=function(e){var t=this.a,n=this.b;this.r1=q(this.anchr1,t.rot),this.r2=q(this.anchr2,n.rot);var r=_(M(n.p,this.r2),M(t.p,this.r1)),i=A(r);this.n=P(r,1/(i?i:Infinity)),this.nMass=1/$n(t,n,this.r1,this.r2,this.n);var s=this.maxBias;this.bias=b(-Qn(this.errorBias,e)*(i-this.dist)/e,-s,s),this.jnMax=this.maxForce*e},Yn.prototype.applyCachedImpulse=function(e){var t=P(this.n,this.jnAcc*e);Wn(this.a,this.b,this.r1,this.r2,t.x,t.y)},Yn.prototype.applyImpulse=function(){var e=this.a,t=this.b,n=this.n,r=Un(e,t,this.r1,this.r2,n),i=(this.bias-r)*this.nMass,s=this.jnAcc;this.jnAcc=b(s+i,-this.jnMax,this.jnMax),i=this.jnAcc-s,Wn(e,t,this.r1,this.r2,n.x*i,n.y*i)},Yn.prototype.getImpulse=function(){return Math.abs(this.jnAcc)};var Zn=e.SlideJoint=function(e,t,n,r,i,s){Gn.call(this,e,t),this.anchr1=n,this.anchr2=r,this.min=i,this.max=s,this.r1=this.r2=this.n=null,this.nMass=0,this.jnAcc=this.jnMax=0,this.bias=0};Zn.prototype=Object.create(Gn.prototype),Zn.prototype.preStep=function(e){var t=this.a,n=this.b;this.r1=q(this.anchr1,t.rot),this.r2=q(this.anchr2,n.rot);var r=_(M(n.p,this.r2),M(t.p,this.r1)),i=A(r),s=0;i>this.max?(s=i-this.max,this.n=X(r)):i<this.min?(s=this.min-i,this.n=D(X(r))):(this.n=C,this.jnAcc=0),this.nMass=1/$n(t,n,this.r1,this.r2,this.n);var o=this.maxBias;this.bias=b(-Qn(this.errorBias,e)*s/e,-o,o),this.jnMax=this.maxForce*e},Zn.prototype.applyCachedImpulse=function(e){var t=this.jnAcc*e;Wn(this.a,this.b,this.r1,this.r2,this.n.x*t,this.n.y*t)},Zn.prototype.applyImpulse=function(){if(this.n.x===0&&this.n.y===0)return;var e=this.a,t=this.b,n=this.n,r=this.r1,i=this.r2,s=Rn(e,t,r,i),o=k(s,n),u=(this.bias-o)*this.nMass,a=this.jnAcc;this.jnAcc=b(a+u,-this.jnMax,0),u=this.jnAcc-a,Wn(e,t,this.r1,this.r2,n.x*u,n.y*u)},Zn.prototype.getImpulse=function(){return Math.abs(this.jnAcc)};var er=e.PivotJoint=function(e,t,n,r){Gn.call(this,e,t);if(typeof r=="undefined"){var i=n;n=e?e.world2Local(i):i,r=t?t.world2Local(i):i}this.anchr1=n,this.anchr2=r,this.r1=this.r2=C,this.k1=new N(0,0),this.k2=new N(0,0),this.jAcc=C,this.jMaxLen=0,this.bias=C};er.prototype=Object.create(Gn.prototype),er.prototype.preStep=function(e){var t=this.a,n=this.b;this.r1=q(this.anchr1,t.rot),this.r2=q(this.anchr2,n.rot),Jn(t,n,this.r1,this.r2,this.k1,this.k2),this.jMaxLen=this.maxForce*e;var r=_(M(n.p,this.r2),M(t.p,this.r1));this.bias=V(P(r,-Qn(this.errorBias,e)/e),this.maxBias)},er.prototype.applyCachedImpulse=function(e){Wn(this.a,this.b,this.r1,this.r2,this.jAcc.x*e,this.jAcc.y*e)},er.prototype.applyImpulse=function(){var e=this.a,t=this.b,n=this.r1,r=this.r2,i=Rn(e,t,n,r),s=Kn(_(this.bias,i),this.k1,this.k2),o=this.jAcc;this.jAcc=V(M(this.jAcc,s),this.jMaxLen),Wn(e,t,this.r1,this.r2,this.jAcc.x-o.x,this.jAcc.y-o.y)},er.prototype.getImpulse=function(){return A(this.jAcc)};var tr=e.GrooveJoint=function(e,t,n,r,i){Gn.call(this,e,t),this.grv_a=n,this.grv_b=r,this.grv_n=j(W(_(r,n))),this.anchr2=i,this.grv_tn=null,this.clamp=0,this.r1=this.r2=null,this.k1=new N(0,0),this.k2=new N(0,0),this.jAcc=C,this.jMaxLen=0,this.bias=null};tr.prototype=Object.create(Gn.prototype),tr.prototype.preStep=function(e){var t=this.a,n=this.b,r=t.local2World(this.grv_a),i=t.local2World(this.grv_b),s=q(this.grv_n,t.rot),o=k(r,s);this.grv_tn=s,this.r2=q(this.anchr2,n.rot);var u=H(M(n.p,this.r2),s);u<=H(r,s)?(this.clamp=1,this.r1=_(r,t.p)):u>=H(i,s)?(this.clamp=-1,this.r1=_(i,t.p)):(this.clamp=0,this.r1=_(M(P(j(s),-u),P(s,o)),t.p)),Jn(t,n,this.r1,this.r2,this.k1,this.k2),this.jMaxLen=this.maxForce*e;var a=_(M(n.p,this.r2),M(t.p,this.r1));this.bias=V(P(a,-Qn(this.errorBias,e)/e),this.maxBias)},tr.prototype.applyCachedImpulse=function(e){Wn(this.a,this.b,this.r1,this.r2,this.jAcc.x*e,this.jAcc.y*e)},tr.prototype.grooveConstrain=function(e){var t=this.grv_tn,n=this.clamp*H(e,t)>0?e:I(e,t);return V(n,this.jMaxLen)},tr.prototype.applyImpulse=function(){var e=this.a,t=this.b,n=this.r1,r=this.r2,i=Rn(e,t,n,r),s=Kn(_(this.bias,i),this.k1,this.k2),o=this.jAcc;this.jAcc=this.grooveConstrain(M(o,s)),Wn(e,t,this.r1,this.r2,this.jAcc.x-o.x,this.jAcc.y-o.y)},tr.prototype.getImpulse=function(){return A(this.jAcc)},tr.prototype.setGrooveA=function(e){this.grv_a=e,this.grv_n=j(W(_(this.grv_b,e))),this.activateBodies()},tr.prototype.setGrooveB=function(e){this.grv_b=e,this.grv_n=j(W(_(e,this.grv_a))),this.activateBodies()};var nr=function(e,t){return(e.restLength-t)*e.stiffness},rr=e.DampedSpring=function(e,t,n,r,i,s,o){Gn.call(this,e,t),this.anchr1=n,this.anchr2=r,this.restLength=i,this.stiffness=s,this.damping=o,this.springForceFunc=nr,this.target_vrn=this.v_coef=0,this.r1=this.r2=null,this.nMass=0,this.n=null};rr.prototype=Object.create(Gn.prototype),rr.prototype.preStep=function(e){var t=this.a,r=this.b;this.r1=q(this.anchr1,t.rot),this.r2=q(this.anchr2,r.rot);var i=_(M(r.p,this.r2),M(t.p,this.r1)),s=A(i);this.n=P(i,1/(s?s:Infinity));var o=$n(t,r,this.r1,this.r2,this.n);n(o!==0,"Unsolvable this."),this.nMass=1/o,this.target_vrn=0,this.v_coef=1-Math.exp(-this.damping*e*o);var u=this.springForceFunc(this,s);Wn(t,r,this.r1,this.r2,this.n.x*u*e,this.n.y*u*e)},rr.prototype.applyCachedImpulse=function(e){},rr.prototype.applyImpulse=function(){var e=this.a,t=this.b,n=this.n,r=this.r1,i=this.r2,s=Un(e,t,r,i,n),o=(this.target_vrn-s)*this.v_coef;this.target_vrn=s+o,o*=this.nMass,Wn(e,t,this.r1,this.r2,this.n.x*o,this.n.y*o)},rr.prototype.getImpulse=function(){return 0};var ir=function(e,t){return(t-e.restAngle)*e.stiffness},sr=e.DampedRotarySpring=function(e,t,n,r,i){Gn.call(this,e,t),this.restAngle=n,this.stiffness=r,this.damping=i,this.springTorqueFunc=ir,this.target_wrn=0,this.w_coef=0,this.iSum=0};sr.prototype=Object.create(Gn.prototype),sr.prototype.preStep=function(e){var t=this.a,r=this.b,i=t.i_inv+r.i_inv;n(i!==0,"Unsolvable spring."),this.iSum=1/i,this.w_coef=1-Math.exp(-this.damping*e*i),this.target_wrn=0;var s=this.springTorqueFunc(this,t.a-r.a)*e;t.w-=s*t.i_inv,r.w+=s*r.i_inv},sr.prototype.applyImpulse=function(){var e=this.a,t=this.b,n=e.w-t.w,r=(this.target_wrn-n)*this.w_coef;this.target_wrn=n+r;var i=r*this.iSum;e.w+=i*e.i_inv,t.w-=i*t.i_inv};var or=e.RotaryLimitJoint=function(e,t,n,r){Gn.call(this,e,t),this.min=n,this.max=r,this.jAcc=0,this.iSum=this.bias=this.jMax=0};or.prototype=Object.create(Gn.prototype),or.prototype.preStep=function(e){var t=this.a,n=this.b,r=n.a-t.a,i=0;r>this.max?i=this.max-r:r<this.min&&(i=this.min-r),this.iSum=1/(1/t.i+1/n.i);var s=this.maxBias;this.bias=b(-Qn(this.errorBias,e)*i/e,-s,s),this.jMax=this.maxForce*e,this.bias||(this.jAcc=0)},or.prototype.applyCachedImpulse=function(e){var t=this.a,n=this.b,r=this.jAcc*e;t.w-=r*t.i_inv,n.w+=r*n.i_inv},or.prototype.applyImpulse=function(){if(!this.bias)return;var e=this.a,t=this.b,n=t.w-e.w,r=-(this.bias+n)*this.iSum,i=this.jAcc;this.bias<0?this.jAcc=b(i+r,0,this.jMax):this.jAcc=b(i+r,-this.jMax,0),r=this.jAcc-i,e.w-=r*e.i_inv,t.w+=r*t.i_inv},or.prototype.getImpulse=function(){return Math.abs(joint.jAcc)};var ur=e.RatchetJoint=function(e,t,n,r){Gn.call(this,e,t),this.angle=0,this.phase=n,this.ratchet=r,this.angle=(t?t.a:0)-(e?e.a:0),this.iSum=this.bias=this.jAcc=this.jMax=0};ur.prototype=Object.create(Gn.prototype),ur.prototype.preStep=function(e){var t=this.a,n=this.b,r=this.angle,i=this.phase,s=this.ratchet,o=n.a-t.a,u=r-o,a=0;u*s>0?a=u:this.angle=Math.floor((o-i)/s)*s+i,this.iSum=1/(t.i_inv+n.i_inv);var f=this.maxBias;this.bias=b(-Qn(this.errorBias,e)*a/e,-f,f),this.jMax=this.maxForce*e,this.bias||(this.jAcc=0)},ur.prototype.applyCachedImpulse=function(e){var t=this.a,n=this.b,r=this.jAcc*e;t.w-=r*t.i_inv,n.w+=r*n.i_inv},ur.prototype.applyImpulse=function(){if(!this.bias)return;var e=this.a,t=this.b,n=t.w-e.w,r=this.ratchet,i=-(this.bias+n)*this.iSum,s=this.jAcc;this.jAcc=b((s+i)*r,0,this.jMax*Math.abs(r))/r,i=this.jAcc-s,e.w-=i*e.i_inv,t.w+=i*t.i_inv},ur.prototype.getImpulse=function(e){return Math.abs(e.jAcc)};var ar=e.GearJoint=function(e,t,n,r){Gn.call(this,e,t),this.phase=n,this.ratio=r,this.ratio_inv=1/r,this.jAcc=0,this.iSum=this.bias=this.jMax=0};ar.prototype=Object.create(Gn.prototype),ar.prototype.preStep=function(e){var t=this.a,n=this.b;this.iSum=1/(t.i_inv*this.ratio_inv+this.ratio*n.i_inv);var r=this.maxBias;this.bias=b(-Qn(this.errorBias,e)*(n.a*this.ratio-t.a-this.phase)/e,-r,r),this.jMax=this.maxForce*e},ar.prototype.applyCachedImpulse=function(e){var t=this.a,n=this.b,r=this.jAcc*e;t.w-=r*t.i_inv*this.ratio_inv,n.w+=r*n.i_inv},ar.prototype.applyImpulse=function(){var e=this.a,t=this.b,n=t.w*this.ratio-e.w,r=(this.bias-n)*this.iSum,i=this.jAcc;this.jAcc=b(i+r,-this.jMax,this.jMax),r=this.jAcc-i,e.w-=r*e.i_inv*this.ratio_inv,t.w+=r*t.i_inv},ar.prototype.getImpulse=function(){return Math.abs(this.jAcc)},ar.prototype.setRatio=function(e){this.ratio=e,this.ratio_inv=1/e,this.activateBodies()};var fr=e.SimpleMotor=function(e,t,n){Gn.call(this,e,t),this.rate=n,this.jAcc=0,this.iSum=this.jMax=0};fr.prototype=Object.create(Gn.prototype),fr.prototype.preStep=function(e){this.iSum=1/(this.a.i_inv+this.b.i_inv),this.jMax=this.maxForce*e},fr.prototype.applyCachedImpulse=function(e){var t=this.a,n=this.b,r=this.jAcc*e;t.w-=r*t.i_inv,n.w+=r*n.i_inv},fr.prototype.applyImpulse=function(){var e=this.a,t=this.b,n=t.w-e.w+this.rate,r=-n*this.iSum,i=this.jAcc;this.jAcc=b(i+r,-this.jMax,this.jMax),r=this.jAcc-i,e.w-=r*e.i_inv,t.w+=r*t.i_inv},fr.prototype.getImpulse=function(){return Math.abs(this.jAcc)}})();
View
6 lib/cpSpaceQuery.js
@@ -141,11 +141,11 @@ Space.prototype.shapeQuery = function(shape, func)
var contacts;
- // Shape 'a' should have the lower shape type. (required by cpCollideShapes() )
+ // Shape 'a' should have the lower shape type. (required by collideShapes() )
if(a.collisionCode <= b.collisionCode){
- contacts = cpCollideShapes(a, b);
+ contacts = collideShapes(a, b);
} else {
- contacts = cpCollideShapes(b, a);
+ contacts = collideShapes(b, a);
for(var i=0; i<contacts.length; i++) contacts[i].n = vneg(contacts[i].n);
}

0 comments on commit 46913e7

Please sign in to comment.
Something went wrong with that request. Please try again.