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