diff --git a/build/physics/physics.js b/build/physics/physics.js index 9720bb32..76b60d19 100644 --- a/build/physics/physics.js +++ b/build/physics/physics.js @@ -6214,7 +6214,9 @@ SimpleMotor.prototype.getImpulse = function() * @mixin PhysicsViewMixin */ var PhysicsViewMixin = { + space:null, body:null, + shape:null, /** * 施加冲量 * @memberOf PhysicsViewMixin @@ -6243,6 +6245,9 @@ SimpleMotor.prototype.getImpulse = function() */ setPosition:function(x, y){ this.body.setPos(new cp.Vect(x, y)); + if(this.body.isStaticBody){ + this.space.needReindexStatic = true; + } }, /** * 设置角度 @@ -6251,6 +6256,9 @@ SimpleMotor.prototype.getImpulse = function() */ setRotation:function(rotation){ this.body.setAngle(rotation * DEG2RAD); + if(this.body.isStaticBody){ + this.space.needReindexStatic = true; + } }, /** * 重写render @@ -6301,20 +6309,20 @@ SimpleMotor.prototype.getImpulse = function() * @private */ _init:function(gravity, cfg){ - var world = new cp.Space(); - world.iterations = 20; - world.gravity = new cp.Vect(gravity.x, gravity.y); - world.collisionSlop = 0.5; - world.sleepTimeThreshold = 0.5; + var space = new cp.Space(); + space.iterations = 20; + space.gravity = new cp.Vect(gravity.x, gravity.y); + space.collisionSlop = 0.5; + space.sleepTimeThreshold = 0.5; if(cfg){ for(var i in cfg){ - world[i] = cfg[i]; + space[i] = cfg[i]; } } - this.space = world; - this.staticBody = world.staticBody; + this.space = space; + this.staticBody = space.staticBody; this._deleteBodies = []; }, @@ -6324,19 +6332,23 @@ SimpleMotor.prototype.getImpulse = function() * @param {Number} dt 间隔 */ tick:function(dt){ - var world = this.space; + var space = this.space; dt = dt > 32?16:dt; - world.step(dt * .001); + if(space.needReindexStatic){ + space.reindexStatic(); + space.needReindexStatic = false; + } + space.step(dt * .001); //delete bodies and shapes for(var i = this._deleteBodies.length - 1;i >= 0;i --){ var body = this._deleteBodies[i]; var shapeList = body.shapeList; for(var j = shapeList.length - 1;j >= 0;j --){ - world.removeShape(shapeList[j]); + space.removeShape(shapeList[j]); } - world.removeBody(body); + space.removeBody(body); } }, /** @@ -6346,7 +6358,11 @@ SimpleMotor.prototype.getImpulse = function() * @param {String} cfg.type 形状类型,SHAPE_RECT|SHAPE_CIRCLE|SHAPE_POLYGEN , 默认矩形 * @param {Number} cfg.restitution 弹力,默认0.4 * @param {Number} cfg.friction 摩擦力,默认1 + * @param {Number} cfg.mass 质量,默认1 * @param {Number} cfg.collisionType 碰撞类型,默认1 + * @param {Uint} cfg.group 碰撞组标识,默认为0,零组与任何组都碰撞,相同的非零组之间不会互相碰撞 + * @param {Uint} cfg.layers 碰撞层的掩码,默认为~0,两个层的按位与不为0时(a.layers & b.layers != 0)会发生碰撞 + * @param {Boolean} cfg.isStatic 是否静态刚体,默认false * @param {Number} cfg.width 宽,type为SHAPE_RECT时有效,默认为view宽 * @param {Number} cfg.height 高,type为SHAPE_RECT时有效,默认为view高 * @param {Number} cfg.radius 半径,type为SHAPE_CIRCLE时有效,默认为view宽的一半 @@ -6360,6 +6376,8 @@ SimpleMotor.prototype.getImpulse = function() var cfg = cfg||{}; var mass = cfg.mass || 1; var type = cfg.type || Physics.SHAPE_RECT; + var group = cfg.group === undefined?0:cfg.group; + var layers = cfg.layers === undefined?~0:cfg.layers; var width = view.width * view.scaleX; var height = view.height * view.scaleY; @@ -6372,12 +6390,12 @@ SimpleMotor.prototype.getImpulse = function() case Physics.SHAPE_RECT: width = cfg.width||width; height = cfg.height||height; - body = new cp.Body(mass, cp.momentForBox(mass, width, height)); + body = cfg.isStatic?this._createStaticBody():new cp.Body(mass, cp.momentForBox(mass, width, height)); shape = new cp.BoxShape(body, width, height); break; case Physics.SHAPE_CIRCLE: radius = cfg.radius||width*.5; - body = new cp.Body(mass, cp.momentForCircle(mass, 0, radius, new cp.Vect(0, 0))); + body = cfg.isStatic?this._createStaticBody():new cp.Body(mass, cp.momentForCircle(mass, 0, radius, new cp.Vect(0, 0))); shape = new cp.CircleShape(body, radius, new cp.Vect(0, 0)); break; case Physics.SHAPE_POLYGEN: @@ -6388,7 +6406,7 @@ SimpleMotor.prototype.getImpulse = function() verts.push(point.y); }); view.boundsArea = boundsArea; - body = new cp.Body(mass, cp.momentForPoly(mass, verts, new cp.Vect(0, 0))); + body = cfg.isStatic?this._createStaticBody():new cp.Body(mass, cp.momentForPoly(mass, verts, new cp.Vect(0, 0))); shape = new cp.PolyShape(body, verts, new cp.Vect(0, 0)); break; default: @@ -6401,6 +6419,8 @@ SimpleMotor.prototype.getImpulse = function() shape.setElasticity(cfg.restitution||.4); shape.setFriction(cfg.friction||1); shape.setCollisionType(cfg.collisionType||1); + shape.layers = layers; + shape.group = group; view._viewRender = view.render; Class.mix(view, PhysicsViewMixin); @@ -6408,14 +6428,20 @@ SimpleMotor.prototype.getImpulse = function() view.body = body; view.shape = shape; + view.space = this.space; body.view = view; //物理对象中心点必须在中心 view.pivotX = view.width * .5; view.pivotY = view.height * .5; - this.space.addBody(body); - this.space.addShape(shape); + if(cfg.isStatic){ + this.space.addShape(shape); + } + else{ + this.space.addBody(body); + this.space.addShape(shape); + } view._physicsRender(); @@ -6509,6 +6535,12 @@ SimpleMotor.prototype.getImpulse = function() floor.setElasticity(1); floor.setFriction(1); }, + _createStaticBody:function(){ + var body = new cp.Body(Infinity, Infinity); + body.nodeIdleTime = Infinity; + body.isStaticBody = true; + return body; + } }); /** diff --git a/build/physics/physics.min.js b/build/physics/physics.min.js index 58c54584..24a1f415 100644 --- a/build/physics/physics.min.js +++ b/build/physics/physics.min.js @@ -3,6 +3,6 @@ * 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){},n=function(t,i){},r=function(t,i){return i>t?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 i>t?t+" "+i:i+" "+t},h=function(t,i){for(var e=0;eo;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;s>e;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;n>s;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,B(S(r,o),a))}return B(e,1/(3*i))},t.recenterPoly=function(i){for(var e=t.centroidForPoly(i),s=0;s>1,h=1;a>h;h++){var c=t[2*h],p=t[2*h+1];i>c||c==i&&e>p?(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;l>=p;){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(0>s)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),v=new f(i[2*(e+h)],i[2*(e+h)+1]);return p+b(t,i,e+h+1,u-1,r,v,o,a+p)};t.convexHull=function(t,i,e){if(i)for(var s=0;s>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 v=function(t,s,n){return i(e(t,s),n)},d=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 j=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 B=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 B(i,x(t,i)/V(i))});f.prototype.project=function(t){return this.mult(x(this,t)/V(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)},V=t.v.lengthsq=function(t){return x(t,t)},T=t.v.lengthsq2=function(t,i){return t*t+i*i},F=t.v.lerp=function(t,i,e){return S(B(t,1-e),B(i,e))},R=t.v.normalize=function(t){return B(t,1/m(t))},N=t.v.normalize_safe=function(t){return 0===t.x&&0===t.y?_:R(t)},E=t.v.clamp=function(t,i){return x(t,t)>i*i?B(R(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 V(A(t,i))},O=(t.v.near=function(t,i,e){return H(t,i)=0){var p=(-a-Math.sqrt(c))/(2*o);if(p>=0&&1>=p)return new X(t,p,R(F(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(R(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.x0?j(e):e,o=A(B(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(0>l*u)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.te;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;e>r;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=R(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 W(-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;p0&&(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);r>y&&(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;n>o;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(!(0>p||p>1)){var l=F(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(u>=y&&b>=u)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;r0)return!1}return!0},it.prototype.containsVertPartial=function(t,i,e){for(var s=this.tPlanes,n=0;n0)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=v(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),B(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;e>i;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){if(t.count>0){var e=t.query;this.each(function(t){e(t,new W(t.bb_l,t.bb_b,t.bb_r,t.bb_t),i)})}};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=B(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 vt=0,dt=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):(vt++,new dt(t,i,e,s))},dt.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)),r>o?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(c>=y&&b>=h){var v=e(h,y),d=i(c,b);if(d>=0&&1>=v)return e(v,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 a>o?(n>o&&(n=i(n,At(t.A,e,s,n,r))),n>a&&(n=i(n,At(t.B,e,s,n,r)))):(n>a&&(n=i(n,At(t.B,e,s,n,r))),n>o&&(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 jt=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},Bt=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){Bt(t,this)&&(i?_t(t,this,e):(this.stamp=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)?!1:(t.getBB(this.obj,this),i=jt(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=jt(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 W(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;l>u;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;l>u;u++)b[2*u+0]=s[u].bb_l,b[2*u+1]=s[u].bb_r;else for(var u=n;l>u;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 v=.5*(b[r-1]+b[r]),d=a,f=h,_=c,x=p,g=a,m=h,w=c,S=p;y?_=g=v:x=m=v;for(var A=l,j=n;A>j;){var o=s[j];kt(o,g,m,w,S)u;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&&10>=i&&(Pt(t.A,i+1),Pt(t.B,i+1));for(var e="",s=0;i>s;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;s>e;e++){var n=t[e];i.add(B(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;s>e;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;n>s;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 Vt=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=n*n)){var a=Math.sqrt(o);return new Et(S(t,B(r,.5+(e-.5*n)/(a?a:1/0))),a?B(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=d(x(r,A(n,e))/V(r)),a=S(e,B(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},Wt=0,Dt=function(t,i){var e=0,s=t.valueOnAxis(i[0].n,i[0].d);if(s>0)return-1;for(var n=1;n0)return-1;r>s&&(s=r,e=n)}return Wt=s,e},Gt=function(t,i,e,s){for(var n=[],r=t.tVerts,o=0;o>1)))}for(var p=i.tVerts,o=0;o>1)))}return n.length?n:Gt(t,i,e,s)},Yt=function(t,i){var e=Dt(i,t.tPlanes);if(-1==e)return Qt;var s=Wt,n=Dt(t,i.tPlanes);if(-1==n)return Qt;var r=Wt;return s>r?Jt(t,i,t.tPlanes[e].n,s):Jt(t,i,j(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=B(i.tn,n),c=e.tVerts,p=0;p=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(j(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;n>l;l++){var u=Ut(t,s[l].n,s[l].d);if(u>0)return Qt;u>p&&(p=u,c=l)}var y=j(s[c].n),b=S(t.ta,B(y,t.r)),v=S(t.tb,B(y,t.r));if(i.containsVert(b.x,b.y)&&e.push(new Et(b,y,p,a(t.hashid,0))),i.containsVert(v.x,v.y)&&e.push(new Et(v,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 d,_=2*c,g=i.tVerts,m=new f(g[_],g[_+1]);if(d=Ht(t.ta,m,t.r,0,e))return[d];if(d=Ht(t.tb,m,t.r,0,e))return[d];var w=2*n,A=new f(g[(_+2)%w],g[(_+3)%w]);if(d=Ht(t.ta,A,t.r,0,e))return[d];if(d=Ht(t.tb,A,t.r,0,e))return[d]}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;r0)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],v=k(a.x,a.y,l,u),d=k(a.x,a.y,y,b),_=C(a,t.tc);if(d>_){var g=Ht(t.tc,new f(y,b),t.r,0,g);return g?[g]:Qt}if(v>_)return[new Et(A(t.tc,B(a,t.r+n/2)),j(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;eh?0:r.nodeIdleTime+t}for(var c=this.arbiters,s=0,p=c.length;p>s;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=0,"Internal Error: Space lock underflow."),0===this.locked&&t){for(var i=this.rousedBodies,e=0;ee.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())?!0:(i>=1&&"cached"!=t.state&&(t.callSeparate(this),t.state="cached"),i>=this.collisionPersistence?(t.contacts=null,!1):!0)};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;e0,"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=B(s,1/(n?n:1/0)),this.nMass=1/bi(i,e,this.r1,this.r2,this.n);var r=this.maxBias;this.bias=v(-fi(this.errorBias,t)*(n-this.dist)/t,-r,r),this.jnMax=this.maxForce*t},xi.prototype.applyCachedImpulse=function(t){var i=B(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=v(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=C(n,r)?(this.clamp=-1,this.r1=A(n,i.p)):(this.clamp=0,this.r1=A(S(B(M(r),-a),B(r,o)),i.p)),vi(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(B(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=di(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(R(A(this.grv_b,t))),this.activateBodies()},wi.prototype.setGrooveB=function(t){this.grv_b=t,this.grv_n=M(R(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=B(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 ji=function(t,i){return(i-t.restAngle)*t.stiffness},Bi=t.DampedRotarySpring=function(t,i,e,s,n){_i.call(this,t,i),this.restAngle=e,this.stiffness=s,this.damping=n,this.springTorqueFunc=ji,this.target_wrn=0,this.w_coef=0,this.iSum=0};Bi.prototype=Object.create(_i.prototype),Bi.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},Bi.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:s0?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=v(-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=v((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=v(-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=v(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=v(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={body: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))},setRotation:function(t){this.body.setAngle(t*s)},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=[]},tick:function(t){var i=this.space;t=t>32?16:t,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)}},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=i.width*i.scaleX,l=i.height*i.scaleY;switch(c!==o.SHAPE_POLYGEN||i.boundsArea||e.boundsArea||(c=o.SHAPE_RECT),c){case o.SHAPE_RECT:p=e.width||p,l=e.height||l,n=new cp.Body(h,cp.momentForBox(h,p,l)),a=new cp.BoxShape(n,p,l);break;case o.SHAPE_CIRCLE:radius=e.radius||.5*p,n=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 u=e.boundsArea||i.boundsArea;verts=[],u.forEach(function(t){verts.push(t.x),verts.push(t.y)}),i.boundsArea=u,n=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),i._viewRender=i.render,t.mix(i,r),i.body=n,i.shape=a,n.view=i,i.pivotX=.5*i.width,i.pivotY=.5*i.height,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;e&&(t.body=null,e.view=null,this._deleteBodies.indexOf(e)<0&&this._deleteBodies.push(e));for(var s in r)t[s]=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)}}),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;cn;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;100>a;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}(); \ No newline at end of file +!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){},n=function(t,i){},r=function(t,i){return i>t?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 i>t?t+" "+i:i+" "+t},h=function(t,i){for(var e=0;eo;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;s>e;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;n>s;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>1,h=1;a>h;h++){var c=t[2*h],p=t[2*h+1];i>c||c==i&&e>p?(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;l>=p;){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(0>s)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>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)},V=t.v.lengthsq2=function(t,i){return t*t+i*i},R=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)=0){var p=(-a-Math.sqrt(c))/(2*o);if(p>=0&&1>=p)return new X(t,p,F(R(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.x0?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(0>l*u)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.te;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;e>r;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 W(-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;p0&&(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);r>y&&(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;n>o;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(!(0>p||p>1)){var l=R(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(u>=y&&b>=u)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;r0)return!1}return!0},it.prototype.containsVertPartial=function(t,i,e){for(var s=this.tPlanes,n=0;n0)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;e>i;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){if(t.count>0){var e=t.query;this.each(function(t){e(t,new W(t.bb_l,t.bb_b,t.bb_r,t.bb_t),i)})}};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)),r>o?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(c>=y&&b>=h){var d=e(h,y),v=i(c,b);if(v>=0&&1>=d)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 a>o?(n>o&&(n=i(n,At(t.A,e,s,n,r))),n>a&&(n=i(n,At(t.B,e,s,n,r)))):(n>a&&(n=i(n,At(t.B,e,s,n,r))),n>o&&(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.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)?!1:(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 W(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;l>u;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;l>u;u++)b[2*u+0]=s[u].bb_l,b[2*u+1]=s[u].bb_r;else for(var u=n;l>u;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;A>B;){var o=s[B];kt(o,g,m,w,S)u;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&&10>=i&&(Pt(t.A,i+1),Pt(t.B,i+1));for(var e="",s=0;i>s;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;s>e;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;s>e;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;n>s;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=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},Wt=0,Dt=function(t,i){var e=0,s=t.valueOnAxis(i[0].n,i[0].d);if(s>0)return-1;for(var n=1;n0)return-1;r>s&&(s=r,e=n)}return Wt=s,e},Gt=function(t,i,e,s){for(var n=[],r=t.tVerts,o=0;o>1)))}for(var p=i.tVerts,o=0;o>1)))}return n.length?n:Gt(t,i,e,s)},Yt=function(t,i){var e=Dt(i,t.tPlanes);if(-1==e)return Qt;var s=Wt,n=Dt(t,i.tPlanes);if(-1==n)return Qt;var r=Wt;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=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;n>l;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;r0)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;eh?0:r.nodeIdleTime+t}for(var c=this.arbiters,s=0,p=c.length;p>s;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=0,"Internal Error: Space lock underflow."),0===this.locked&&t){for(var i=this.rousedBodies,e=0;ee.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())?!0:(i>=1&&"cached"!=t.state&&(t.callSeparate(this),t.state="cached"),i>=this.collisionPersistence?(t.contacts=null,!1):!0)};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;e0,"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=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:s0?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=[]},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)}},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;e&&(t.body=null,e.view=null,this._deleteBodies.indexOf(e)<0&&this._deleteBodies.push(e));for(var s in r)t[s]=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;cn;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;100>a;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}(); \ No newline at end of file diff --git a/src/extensions/physics/README.md b/src/extensions/physics/README.md index 59d09496..b513e718 100644 --- a/src/extensions/physics/README.md +++ b/src/extensions/physics/README.md @@ -2,8 +2,9 @@ 对chipmunk二次封装,简化使用方法 ## example -* [demo](http://groups.demo.taobao.net/Hilo/hilo/src/extensions/physics/demo/) -* [joints](http://groups.demo.taobao.net/Hilo/hilo/src/extensions/physics/demo/joints.html) +* [demo](http://hiloteam.github.io/Hilo/src/extensions/physics/demo/index.html) +* [joints](http://hiloteam.github.io/Hilo/src/extensions/physics/demo/joints.html) +* [collision](http://hiloteam.github.io/Hilo/src/extensions/physics/demo/collision.html) ## 使用说明 @@ -27,7 +28,9 @@ ``` world.bindView(view, { type:Hilo.Physics.SHAPE_CIRCLE, - restitution:.9 + restitution:0.9, + group:0, + layers:~0 }); ``` 1. 操作物理对象,施加力, 冲量,改变位置,角度 [详细api](./api.md#PhysicsViewMixin) @@ -78,4 +81,10 @@ ## 详细文档 * [完整api](./api.md) -* [chipmunk中文手册](https://github.com/iTyran/ChipmunkDocsCN/blob/master/Chipmunk2D.md) \ No newline at end of file +* [chipmunk中文手册](https://github.com/iTyran/ChipmunkDocsCN/blob/master/Chipmunk2D.md) + +## build api doc +``` +npm install -g jsdoc-to-markdown +jsdoc2md src/extensions/physics/physics.js > src/extensions/physics/api.md +``` \ No newline at end of file diff --git a/src/extensions/physics/api.md b/src/extensions/physics/api.md index 76396174..8c929e84 100644 --- a/src/extensions/physics/api.md +++ b/src/extensions/physics/api.md @@ -1,33 +1,39 @@ ## Classes +
Physics
PhysicsDebugView
+ ## Mixins +
PhysicsViewMixin

PhysicsViewMixin是一个包含物理相关功能的mixin。可以通过 Class.mix(target, PhysicsViewMixin) 来为target增加物理功能。

+ + ## Physics **Kind**: global class * [Physics](#Physics) - * [new Physics(gravity, cfg)](#new_Physics_new) - * _instance_ - * [.bindView(view, cfg)](#Physics#bindView) - * [.addConstraint(joint)](#Physics#addConstraint) ⇒ Joint - * [.removeConstraint(joint)](#Physics#removeConstraint) ⇒ Joint - * [.unbindView(view, isDelView)](#Physics#unbindView) - * [.addCollisionListener(typeA, typeB, listenerConfig)](#Physics#addCollisionListener) - * [.createBounds(width, height)](#Physics#createBounds) - * _inner_ - * [~collisionCallback](#Physics..collisionCallback) : function + * [new Physics(gravity, cfg)](#new_Physics_new) + * _instance_ + * [.bindView(view, cfg)](#Physics+bindView) + * [.addConstraint(joint)](#Physics+addConstraint) ⇒ Joint + * [.removeConstraint(joint)](#Physics+removeConstraint) ⇒ Joint + * [.unbindView(view, isDelView)](#Physics+unbindView) + * [.addCollisionListener(typeA, typeB, listenerConfig)](#Physics+addCollisionListener) + * [.createBounds(width, height)](#Physics+createBounds) + * _inner_ + * [~collisionCallback](#Physics..collisionCallback) : function + ### new Physics(gravity, cfg) 物理世界 @@ -39,7 +45,8 @@ | gravity.y | Number | 重力加速度y | | cfg | Object | 世界属性配置 | - + + ### physics.bindView(view, cfg) 绑定物理刚体 @@ -52,13 +59,18 @@ | cfg.type | String | 形状类型,SHAPE_RECT|SHAPE_CIRCLE|SHAPE_POLYGEN , 默认矩形 | | cfg.restitution | Number | 弹力,默认0.4 | | cfg.friction | Number | 摩擦力,默认1 | +| cfg.mass | Number | 质量,默认1 | | cfg.collisionType | Number | 碰撞类型,默认1 | +| cfg.group | Uint | 碰撞组标识,默认为0,零组与任何组都碰撞,相同的非零组之间不会互相碰撞 | +| cfg.layers | Uint | 碰撞层的掩码,默认为~0,两个层的按位与不为0时(a.layers & b.layers != 0)会发生碰撞 | +| cfg.isStatic | Boolean | 是否静态刚体,默认false | | cfg.width | Number | 宽,type为SHAPE_RECT时有效,默认为view宽 | | cfg.height | Number | 高,type为SHAPE_RECT时有效,默认为view高 | | cfg.radius | Number | 半径,type为SHAPE_CIRCLE时有效,默认为view宽的一半 | | cfg.boundsArea | Array | 顶点数组,type为SHAPE_POLYGEN时有效, 顶点顺序必须逆时针,[{x:0, y:0}, {x:100, y:0}, {x:50, y:50}] | - + + ### physics.addConstraint(joint) ⇒ Joint 增加关节 @@ -68,7 +80,8 @@ | --- | --- | --- | | joint | Joint | 关节 | - + + ### physics.removeConstraint(joint) ⇒ Joint 移除关节 @@ -78,7 +91,8 @@ | --- | --- | --- | | joint | Joint | 关节 | - + + ### physics.unbindView(view, isDelView) 解绑物理刚体 @@ -89,7 +103,8 @@ | view | View | 要解绑的view | | isDelView | Boolean | 是否删除view,默认不删除 | - + + ### physics.addCollisionListener(typeA, typeB, listenerConfig) 添加碰撞监听 @@ -105,7 +120,8 @@ | listenerConfig.postSolve | [collisionCallback](#Physics..collisionCallback) | 处理后碰撞回调 | | listenerConfig.separate | [collisionCallback](#Physics..collisionCallback) | 分离回调 | - + + ### physics.createBounds(width, height) 添加边框 @@ -117,6 +133,7 @@ | height | Number | 高 | + ### Physics~collisionCallback : function 碰撞回调函数格式 @@ -129,9 +146,11 @@ | arbiter.b | Shape | 碰撞b形状 | + ## PhysicsDebugView **Kind**: global class + ### new PhysicsDebugView(properties) 调试显示对象 @@ -144,6 +163,7 @@ | properties.showConstraints | Boolean | 是否显示constraint,默认true | + ## PhysicsViewMixin PhysicsViewMixin是一个包含物理相关功能的mixin。可以通过 Class.mix(target, PhysicsViewMixin) 来为target增加物理功能。 @@ -156,12 +176,13 @@ PhysicsViewMixin是一个包含物理相关功能的mixin。可以通过 Class.m * [PhysicsViewMixin](#PhysicsViewMixin) - * [.applyImpulse(impulse, pos)](#PhysicsViewMixin.applyImpulse) - * [.applyForce(force, pos)](#PhysicsViewMixin.applyForce) - * [.setPosition(x, y)](#PhysicsViewMixin.setPosition) - * [.setRotation(rotation)](#PhysicsViewMixin.setRotation) + * [.applyImpulse(impulse, pos)](#PhysicsViewMixin.applyImpulse) + * [.applyForce(force, pos)](#PhysicsViewMixin.applyForce) + * [.setPosition(x, y)](#PhysicsViewMixin.setPosition) + * [.setRotation(rotation)](#PhysicsViewMixin.setRotation) + ### PhysicsViewMixin.applyImpulse(impulse, pos) 施加冲量 @@ -173,6 +194,7 @@ PhysicsViewMixin是一个包含物理相关功能的mixin。可以通过 Class.m | pos | Object | 施力位置离重心相对偏移量,默认0,格式:{x:0, y:0} | + ### PhysicsViewMixin.applyForce(force, pos) 施加力 @@ -184,6 +206,7 @@ PhysicsViewMixin是一个包含物理相关功能的mixin。可以通过 Class.m | pos | Object | 施力位置离重心相对偏移量,默认0,格式:{x:0, y:0} | + ### PhysicsViewMixin.setPosition(x, y) 设置位置 @@ -195,6 +218,7 @@ PhysicsViewMixin是一个包含物理相关功能的mixin。可以通过 Class.m | y | Number | + ### PhysicsViewMixin.setRotation(rotation) 设置角度 diff --git a/src/extensions/physics/demo/collision.html b/src/extensions/physics/demo/collision.html new file mode 100644 index 00000000..be963e20 --- /dev/null +++ b/src/extensions/physics/demo/collision.html @@ -0,0 +1,115 @@ + + + + + + + physics collision demo + + + + + +
+
+

鼠标hover在方块上可对方块施力

+

绿色(layers:4|8, group:1):其他颜色碰撞,与自己不碰撞

+

蓝色(layers:4, group:0):与绿色碰撞,与自己碰撞

+

红色(layers:8, group:0):与绿色碰撞,与自己碰撞

+
+ + + \ No newline at end of file diff --git a/src/extensions/physics/physics.js b/src/extensions/physics/physics.js index c61b85e0..e13952c9 100644 --- a/src/extensions/physics/physics.js +++ b/src/extensions/physics/physics.js @@ -158,6 +158,8 @@ * @param {Number} cfg.friction 摩擦力,默认1 * @param {Number} cfg.mass 质量,默认1 * @param {Number} cfg.collisionType 碰撞类型,默认1 + * @param {Uint} cfg.group 碰撞组标识,默认为0,零组与任何组都碰撞,相同的非零组之间不会互相碰撞 + * @param {Uint} cfg.layers 碰撞层的掩码,默认为~0,两个层的按位与不为0时(a.layers & b.layers != 0)会发生碰撞 * @param {Boolean} cfg.isStatic 是否静态刚体,默认false * @param {Number} cfg.width 宽,type为SHAPE_RECT时有效,默认为view宽 * @param {Number} cfg.height 高,type为SHAPE_RECT时有效,默认为view高 @@ -172,6 +174,8 @@ var cfg = cfg||{}; var mass = cfg.mass || 1; var type = cfg.type || Physics.SHAPE_RECT; + var group = cfg.group === undefined?0:cfg.group; + var layers = cfg.layers === undefined?~0:cfg.layers; var width = view.width * view.scaleX; var height = view.height * view.scaleY; @@ -213,6 +217,8 @@ shape.setElasticity(cfg.restitution||.4); shape.setFriction(cfg.friction||1); shape.setCollisionType(cfg.collisionType||1); + shape.layers = layers; + shape.group = group; view._viewRender = view.render; Class.mix(view, PhysicsViewMixin);