diff --git a/cp.js b/cp.js index e4d30fd..3bc6299 100644 --- a/cp.js +++ b/cp.js @@ -2514,18 +2514,16 @@ BBTree.prototype.optimize = function() var nodeRender = function(node, depth) { if(!node.isLeaf && depth <= 10){ - nodeRender(node.a, depth + 1); - nodeRender(node.b, depth + 1); + nodeRender(node.A, depth + 1); + nodeRender(node.B, depth + 1); } -// var bb = node.bb; - var str = ''; for(var i = 0; i < depth; i++) { str += ' '; } -// console.log(str + bb.b + ' ' + bb.t); + console.log(str + node.bb_b + ' ' + node.bb_t); }; BBTree.prototype.log = function(){ diff --git a/cp.min.js b/cp.min.js index b1fb882..82c74e8 100644 --- a/cp.min.js +++ b/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 et?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 et*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)=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=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.x0?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.t0)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>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;la)continue;var f=k(t,u),l=(n[o].d-a)/(f-a);if(l<0||10)return!1}return!0},Lt.prototype.containsVertPartial=function(e,t,n){var r=this.tAxes;for(var i=0;i0)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;t0){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=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;hl-a,d=new Array(r*2);if(p)for(var h=n;h=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;i0)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>1)))}var l=t.tVerts;for(var o=0;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=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;c0)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;s0)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(ga?0:s.nodeIdleTime+e}}var f=this.arbiters;for(var i=0,l=f.length;i=0,"Internal Error: Space lock underflow.");if(!this.locked&&e){var n=this.rousedBodies;for(var r=0;rn.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;r0,"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=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:r0?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)}})(); \ No newline at end of file +(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 et?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 et*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)=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=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.x0?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.t0)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>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;la)continue;var f=k(t,u),l=(n[o].d-a)/(f-a);if(l<0||10)return!1}return!0},Lt.prototype.containsVertPartial=function(e,t,n){var r=this.tAxes;for(var i=0;i0)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;t0){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=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;hl-a,d=new Array(r*2);if(p)for(var h=n;h=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;i0)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>1)))}var l=t.tVerts;for(var o=0;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=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;c0)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;s0)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(ga?0:s.nodeIdleTime+e}}var f=this.arbiters;for(var i=0,l=f.length;i=0,"Internal Error: Space lock underflow.");if(!this.locked&&e){var n=this.rousedBodies;for(var r=0;rn.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;r0,"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=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:r0?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)}})(); \ No newline at end of file diff --git a/lib/cpBBTree.js b/lib/cpBBTree.js index 1118ece..d58bd84 100644 --- a/lib/cpBBTree.js +++ b/lib/cpBBTree.js @@ -759,18 +759,16 @@ BBTree.prototype.optimize = function() var nodeRender = function(node, depth) { if(!node.isLeaf && depth <= 10){ - nodeRender(node.a, depth + 1); - nodeRender(node.b, depth + 1); + nodeRender(node.A, depth + 1); + nodeRender(node.B, depth + 1); } -// var bb = node.bb; - var str = ''; for(var i = 0; i < depth; i++) { str += ' '; } -// console.log(str + bb.b + ' ' + bb.t); + console.log(str + node.bb_b + ' ' + node.bb_t); }; BBTree.prototype.log = function(){