1 line
158 KiB
JavaScript
1 line
158 KiB
JavaScript
var b2Settings=Class.create();b2Settings.prototype={initialize:function(){}},b2Settings.USHRT_MAX=65535,b2Settings.b2_pi=Math.PI,b2Settings.b2_massUnitsPerKilogram=1,b2Settings.b2_timeUnitsPerSecond=1,b2Settings.b2_lengthUnitsPerMeter=30,b2Settings.b2_maxManifoldPoints=2,b2Settings.b2_maxShapesPerBody=64,b2Settings.b2_maxPolyVertices=8,b2Settings.b2_maxProxies=1024,b2Settings.b2_maxPairs=8*b2Settings.b2_maxProxies,b2Settings.b2_linearSlop=.005*b2Settings.b2_lengthUnitsPerMeter,b2Settings.b2_angularSlop=2/180*b2Settings.b2_pi,b2Settings.b2_velocityThreshold=1*b2Settings.b2_lengthUnitsPerMeter/b2Settings.b2_timeUnitsPerSecond,b2Settings.b2_maxLinearCorrection=.2*b2Settings.b2_lengthUnitsPerMeter,b2Settings.b2_maxAngularCorrection=8/180*b2Settings.b2_pi,b2Settings.b2_contactBaumgarte=.2,b2Settings.b2_timeToSleep=.5*b2Settings.b2_timeUnitsPerSecond,b2Settings.b2_linearSleepTolerance=.01*b2Settings.b2_lengthUnitsPerMeter/b2Settings.b2_timeUnitsPerSecond,b2Settings.b2_angularSleepTolerance=2/180/b2Settings.b2_timeUnitsPerSecond,b2Settings.b2Assert=function(a){a||undefined.x++};var b2Vec2=Class.create();b2Vec2.prototype={initialize:function(x_,y_){this.x=x_,this.y=y_},SetZero:function(){this.x=0,this.y=0},Set:function(x_,y_){this.x=x_,this.y=y_},SetV:function(v){this.x=v.x,this.y=v.y},Negative:function(){return new b2Vec2(-this.x,-this.y)},Copy:function(){return new b2Vec2(this.x,this.y)},Add:function(v){this.x+=v.x,this.y+=v.y},Subtract:function(v){this.x-=v.x,this.y-=v.y},Multiply:function(a){this.x*=a,this.y*=a},MulM:function(A){var tX=this.x;this.x=A.col1.x*tX+A.col2.x*this.y,this.y=A.col1.y*tX+A.col2.y*this.y},MulTM:function(A){var tX=b2Math.b2Dot(this,A.col1);this.y=b2Math.b2Dot(this,A.col2),this.x=tX},CrossVF:function(s){var tX=this.x;this.x=s*this.y,this.y=-s*tX},CrossFV:function(s){var tX=this.x;this.x=-s*this.y,this.y=s*tX},MinV:function(b){this.x=this.x<b.x?this.x:b.x,this.y=this.y<b.y?this.y:b.y},MaxV:function(b){this.x=this.x>b.x?this.x:b.x,this.y=this.y>b.y?this.y:b.y},Abs:function(){this.x=Math.abs(this.x),this.y=Math.abs(this.y)},Length:function(){return Math.sqrt(this.x*this.x+this.y*this.y)},Normalize:function(){var length=this.Length();if(length<Number.MIN_VALUE)return 0;var invLength=1/length;return this.x*=invLength,this.y*=invLength,length},IsValid:function(){return b2Math.b2IsValid(this.x)&&b2Math.b2IsValid(this.y)},x:null,y:null},b2Vec2.Make=function(x_,y_){return new b2Vec2(x_,y_)};var b2Mat22=Class.create();b2Mat22.prototype={initialize:function(angle,c1,c2){if(null==angle&&(angle=0),this.col1=new b2Vec2,this.col2=new b2Vec2,null!=c1&&null!=c2)this.col1.SetV(c1),this.col2.SetV(c2);else{var c=Math.cos(angle),s=Math.sin(angle);this.col1.x=c,this.col2.x=-s,this.col1.y=s,this.col2.y=c}},Set:function(angle){var c=Math.cos(angle),s=Math.sin(angle);this.col1.x=c,this.col2.x=-s,this.col1.y=s,this.col2.y=c},SetVV:function(c1,c2){this.col1.SetV(c1),this.col2.SetV(c2)},Copy:function(){return new b2Mat22(0,this.col1,this.col2)},SetM:function(m){this.col1.SetV(m.col1),this.col2.SetV(m.col2)},AddM:function(m){this.col1.x+=m.col1.x,this.col1.y+=m.col1.y,this.col2.x+=m.col2.x,this.col2.y+=m.col2.y},SetIdentity:function(){this.col1.x=1,this.col2.x=0,this.col1.y=0,this.col2.y=1},SetZero:function(){this.col1.x=0,this.col2.x=0,this.col1.y=0,this.col2.y=0},Invert:function(out){var a=this.col1.x,b=this.col2.x,c=this.col1.y,d=this.col2.y,det=a*d-b*c;return det=1/det,out.col1.x=det*d,out.col2.x=-det*b,out.col1.y=-det*c,out.col2.y=det*a,out},Solve:function(out,bX,bY){var a11=this.col1.x,a12=this.col2.x,a21=this.col1.y,a22=this.col2.y,det=a11*a22-a12*a21;return det=1/det,out.x=det*(a22*bX-a12*bY),out.y=det*(a11*bY-a21*bX),out},Abs:function(){this.col1.Abs(),this.col2.Abs()},col1:new b2Vec2,col2:new b2Vec2};var b2Math=Class.create();b2Math.prototype={initialize:function(){}},b2Math.b2IsValid=function(x){return isFinite(x)},b2Math.b2Dot=function(a,b){return a.x*b.x+a.y*b.y},b2Math.b2CrossVV=function(a,b){return a.x*b.y-a.y*b.x},b2Math.b2CrossVF=function(a,s){return new b2Vec2(s*a.y,-s*a.x)},b2Math.b2CrossFV=function(s,a){return new b2Vec2(-s*a.y,s*a.x)},b2Math.b2MulMV=function(A,v){return new b2Vec2(A.col1.x*v.x+A.col2.x*v.y,A.col1.y*v.x+A.col2.y*v.y)},b2Math.b2MulTMV=function(A,v){return new b2Vec2(b2Math.b2Dot(v,A.col1),b2Math.b2Dot(v,A.col2))},b2Math.AddVV=function(a,b){return new b2Vec2(a.x+b.x,a.y+b.y)},b2Math.SubtractVV=function(a,b){return new b2Vec2(a.x-b.x,a.y-b.y)},b2Math.MulFV=function(s,a){return new b2Vec2(s*a.x,s*a.y)},b2Math.AddMM=function(A,B){return new b2Mat22(0,b2Math.AddVV(A.col1,B.col1),b2Math.AddVV(A.col2,B.col2))},b2Math.b2MulMM=function(A,B){return new b2Mat22(0,b2Math.b2MulMV(A,B.col1),b2Math.b2MulMV(A,B.col2))},b2Math.b2MulTMM=function(A,B){var c1=new b2Vec2(b2Math.b2Dot(A.col1,B.col1),b2Math.b2Dot(A.col2,B.col1)),c2=new b2Vec2(b2Math.b2Dot(A.col1,B.col2),b2Math.b2Dot(A.col2,B.col2));return new b2Mat22(0,c1,c2)},b2Math.b2Abs=function(a){return a>0?a:-a},b2Math.b2AbsV=function(a){return new b2Vec2(b2Math.b2Abs(a.x),b2Math.b2Abs(a.y))},b2Math.b2AbsM=function(A){return new b2Mat22(0,b2Math.b2AbsV(A.col1),b2Math.b2AbsV(A.col2))},b2Math.b2Min=function(a,b){return a<b?a:b},b2Math.b2MinV=function(a,b){return new b2Vec2(b2Math.b2Min(a.x,b.x),b2Math.b2Min(a.y,b.y))},b2Math.b2Max=function(a,b){return a>b?a:b},b2Math.b2MaxV=function(a,b){return new b2Vec2(b2Math.b2Max(a.x,b.x),b2Math.b2Max(a.y,b.y))},b2Math.b2Clamp=function(a,low,high){return b2Math.b2Max(low,b2Math.b2Min(a,high))},b2Math.b2ClampV=function(a,low,high){return b2Math.b2MaxV(low,b2Math.b2MinV(a,high))},b2Math.b2Swap=function(a,b){var tmp=a[0];a[0]=b[0],b[0]=tmp},b2Math.b2Random=function(){return 2*Math.random()-1},b2Math.b2NextPowerOfTwo=function(x){return x|=x>>1&2147483647,x|=x>>2&1073741823,x|=x>>4&268435455,x|=x>>8&16777215,(x|=x>>16&65535)+1},b2Math.b2IsPowerOfTwo=function(x){return x>0&&0==(x&x-1)},b2Math.tempVec2=new b2Vec2,b2Math.tempVec3=new b2Vec2,b2Math.tempVec4=new b2Vec2,b2Math.tempVec5=new b2Vec2,b2Math.tempMat=new b2Mat22;var b2AABB=Class.create();b2AABB.prototype={IsValid:function(){var dX=this.maxVertex.x,dY=this.maxVertex.y;dX=this.maxVertex.x,dY=this.maxVertex.y,dX-=this.minVertex.x,dY-=this.minVertex.y;var valid=dX>=0&&dY>=0;return valid=valid&&this.minVertex.IsValid()&&this.maxVertex.IsValid()},minVertex:new b2Vec2,maxVertex:new b2Vec2,initialize:function(){this.minVertex=new b2Vec2,this.maxVertex=new b2Vec2}};var b2Bound=Class.create();b2Bound.prototype={IsLower:function(){return 0==(1&this.value)},IsUpper:function(){return 1==(1&this.value)},Swap:function(b){var tempValue=this.value,tempProxyId=this.proxyId,tempStabbingCount=this.stabbingCount;this.value=b.value,this.proxyId=b.proxyId,this.stabbingCount=b.stabbingCount,b.value=tempValue,b.proxyId=tempProxyId,b.stabbingCount=tempStabbingCount},value:0,proxyId:0,stabbingCount:0,initialize:function(){}};var b2BoundValues=Class.create();b2BoundValues.prototype={lowerValues:[0,0],upperValues:[0,0],initialize:function(){this.lowerValues=[0,0],this.upperValues=[0,0]}};var b2Pair=Class.create();b2Pair.prototype={SetBuffered:function(){this.status|=b2Pair.e_pairBuffered},ClearBuffered:function(){this.status&=~b2Pair.e_pairBuffered},IsBuffered:function(){return(this.status&b2Pair.e_pairBuffered)==b2Pair.e_pairBuffered},SetRemoved:function(){this.status|=b2Pair.e_pairRemoved},ClearRemoved:function(){this.status&=~b2Pair.e_pairRemoved},IsRemoved:function(){return(this.status&b2Pair.e_pairRemoved)==b2Pair.e_pairRemoved},SetFinal:function(){this.status|=b2Pair.e_pairFinal},IsFinal:function(){return(this.status&b2Pair.e_pairFinal)==b2Pair.e_pairFinal},userData:null,proxyId1:0,proxyId2:0,next:0,status:0,initialize:function(){}},b2Pair.b2_nullPair=b2Settings.USHRT_MAX,b2Pair.b2_nullProxy=b2Settings.USHRT_MAX,b2Pair.b2_tableCapacity=b2Settings.b2_maxPairs,b2Pair.b2_tableMask=b2Pair.b2_tableCapacity-1,b2Pair.e_pairBuffered=1,b2Pair.e_pairRemoved=2,b2Pair.e_pairFinal=4;var b2PairCallback=Class.create();b2PairCallback.prototype={PairAdded:function(proxyUserData1,proxyUserData2){return null},PairRemoved:function(proxyUserData1,proxyUserData2,pairUserData){},initialize:function(){}};var b2BufferedPair=Class.create();b2BufferedPair.prototype={proxyId1:0,proxyId2:0,initialize:function(){}};var b2PairManager=Class.create();b2PairManager.prototype={initialize:function(){var i=0;for(this.m_hashTable=new Array(b2Pair.b2_tableCapacity),i=0;i<b2Pair.b2_tableCapacity;++i)this.m_hashTable[i]=b2Pair.b2_nullPair;for(this.m_pairs=new Array(b2Settings.b2_maxPairs),i=0;i<b2Settings.b2_maxPairs;++i)this.m_pairs[i]=new b2Pair;for(this.m_pairBuffer=new Array(b2Settings.b2_maxPairs),i=0;i<b2Settings.b2_maxPairs;++i)this.m_pairBuffer[i]=new b2BufferedPair;for(i=0;i<b2Settings.b2_maxPairs;++i)this.m_pairs[i].proxyId1=b2Pair.b2_nullProxy,this.m_pairs[i].proxyId2=b2Pair.b2_nullProxy,this.m_pairs[i].userData=null,this.m_pairs[i].status=0,this.m_pairs[i].next=i+1;this.m_pairs[b2Settings.b2_maxPairs-1].next=b2Pair.b2_nullPair,this.m_pairCount=0},Initialize:function(broadPhase,callback){this.m_broadPhase=broadPhase,this.m_callback=callback},AddBufferedPair:function(proxyId1,proxyId2){var pair=this.AddPair(proxyId1,proxyId2);0==pair.IsBuffered()&&(pair.SetBuffered(),this.m_pairBuffer[this.m_pairBufferCount].proxyId1=pair.proxyId1,this.m_pairBuffer[this.m_pairBufferCount].proxyId2=pair.proxyId2,++this.m_pairBufferCount),pair.ClearRemoved(),b2BroadPhase.s_validate&&this.ValidateBuffer()},RemoveBufferedPair:function(proxyId1,proxyId2){var pair=this.Find(proxyId1,proxyId2);null!=pair&&(0==pair.IsBuffered()&&(pair.SetBuffered(),this.m_pairBuffer[this.m_pairBufferCount].proxyId1=pair.proxyId1,this.m_pairBuffer[this.m_pairBufferCount].proxyId2=pair.proxyId2,++this.m_pairBufferCount),pair.SetRemoved(),b2BroadPhase.s_validate&&this.ValidateBuffer())},Commit:function(){var i=0,removeCount=0,proxies=this.m_broadPhase.m_proxyPool;for(i=0;i<this.m_pairBufferCount;++i){var pair=this.Find(this.m_pairBuffer[i].proxyId1,this.m_pairBuffer[i].proxyId2);pair.ClearBuffered();var proxy1=proxies[pair.proxyId1],proxy2=proxies[pair.proxyId2];pair.IsRemoved()?(1==pair.IsFinal()&&this.m_callback.PairRemoved(proxy1.userData,proxy2.userData,pair.userData),this.m_pairBuffer[removeCount].proxyId1=pair.proxyId1,this.m_pairBuffer[removeCount].proxyId2=pair.proxyId2,++removeCount):0==pair.IsFinal()&&(pair.userData=this.m_callback.PairAdded(proxy1.userData,proxy2.userData),pair.SetFinal())}for(i=0;i<removeCount;++i)this.RemovePair(this.m_pairBuffer[i].proxyId1,this.m_pairBuffer[i].proxyId2);this.m_pairBufferCount=0,b2BroadPhase.s_validate&&this.ValidateTable()},AddPair:function(proxyId1,proxyId2){if(proxyId1>proxyId2){var temp=proxyId1;proxyId1=proxyId2,proxyId2=temp}var hash=b2PairManager.Hash(proxyId1,proxyId2)&b2Pair.b2_tableMask,pair=pair=this.FindHash(proxyId1,proxyId2,hash);if(null!=pair)return pair;var pIndex=this.m_freePair;return pair=this.m_pairs[pIndex],this.m_freePair=pair.next,pair.proxyId1=proxyId1,pair.proxyId2=proxyId2,pair.status=0,pair.userData=null,pair.next=this.m_hashTable[hash],this.m_hashTable[hash]=pIndex,++this.m_pairCount,pair},RemovePair:function(proxyId1,proxyId2){if(proxyId1>proxyId2){var temp=proxyId1;proxyId1=proxyId2,proxyId2=temp}for(var hash=b2PairManager.Hash(proxyId1,proxyId2)&b2Pair.b2_tableMask,node=this.m_hashTable[hash],pNode=null;node!=b2Pair.b2_nullPair;){if(b2PairManager.Equals(this.m_pairs[node],proxyId1,proxyId2)){var index=node;pNode?pNode.next=this.m_pairs[node].next:this.m_hashTable[hash]=this.m_pairs[node].next;var pair=this.m_pairs[index],userData=pair.userData;return pair.next=this.m_freePair,pair.proxyId1=b2Pair.b2_nullProxy,pair.proxyId2=b2Pair.b2_nullProxy,pair.userData=null,pair.status=0,this.m_freePair=index,--this.m_pairCount,userData}node=(pNode=this.m_pairs[node]).next}return null},Find:function(proxyId1,proxyId2){if(proxyId1>proxyId2){var temp=proxyId1;proxyId1=proxyId2,proxyId2=temp}var hash=b2PairManager.Hash(proxyId1,proxyId2)&b2Pair.b2_tableMask;return this.FindHash(proxyId1,proxyId2,hash)},FindHash:function(proxyId1,proxyId2,hash){for(var index=this.m_hashTable[hash];index!=b2Pair.b2_nullPair&&0==b2PairManager.Equals(this.m_pairs[index],proxyId1,proxyId2);)index=this.m_pairs[index].next;return index==b2Pair.b2_nullPair?null:this.m_pairs[index]},ValidateBuffer:function(){},ValidateTable:function(){},m_broadPhase:null,m_callback:null,m_pairs:null,m_freePair:0,m_pairCount:0,m_pairBuffer:null,m_pairBufferCount:0,m_hashTable:null},b2PairManager.Hash=function(proxyId1,proxyId2){var key=proxyId2<<16&4294901760|proxyId1;return key=~key+(key<<15&4294934528),key^=key>>12&1048575,key+=key<<2&4294967292,key^=key>>4&268435455,key*=2057,key^=key>>16&65535},b2PairManager.Equals=function(pair,proxyId1,proxyId2){return pair.proxyId1==proxyId1&&pair.proxyId2==proxyId2},b2PairManager.EqualsPair=function(pair1,pair2){return pair1.proxyId1==pair2.proxyId1&&pair1.proxyId2==pair2.proxyId2};var b2BroadPhase=Class.create();b2BroadPhase.prototype={initialize:function(worldAABB,callback){this.m_pairManager=new b2PairManager,this.m_proxyPool=new Array(b2Settings.b2_maxPairs),this.m_bounds=new Array(2*b2Settings.b2_maxProxies),this.m_queryResults=new Array(b2Settings.b2_maxProxies),this.m_quantizationFactor=new b2Vec2;var i=0;for(this.m_pairManager.Initialize(this,callback),this.m_worldAABB=worldAABB,this.m_proxyCount=0,i=0;i<b2Settings.b2_maxProxies;i++)this.m_queryResults[i]=0;for(this.m_bounds=new Array(2),i=0;i<2;i++){this.m_bounds[i]=new Array(2*b2Settings.b2_maxProxies);for(var j=0;j<2*b2Settings.b2_maxProxies;j++)this.m_bounds[i][j]=new b2Bound}var tProxy,dX=worldAABB.maxVertex.x,dY=worldAABB.maxVertex.y;for(dX-=worldAABB.minVertex.x,dY-=worldAABB.minVertex.y,this.m_quantizationFactor.x=b2Settings.USHRT_MAX/dX,this.m_quantizationFactor.y=b2Settings.USHRT_MAX/dY,i=0;i<b2Settings.b2_maxProxies-1;++i)tProxy=new b2Proxy,this.m_proxyPool[i]=tProxy,tProxy.SetNext(i+1),tProxy.timeStamp=0,tProxy.overlapCount=b2BroadPhase.b2_invalid,tProxy.userData=null;tProxy=new b2Proxy,this.m_proxyPool[b2Settings.b2_maxProxies-1]=tProxy,tProxy.SetNext(b2Pair.b2_nullProxy),tProxy.timeStamp=0,tProxy.overlapCount=b2BroadPhase.b2_invalid,tProxy.userData=null,this.m_freeProxy=0,this.m_timeStamp=1,this.m_queryResultCount=0},InRange:function(aabb){var dX,dY,d2X,d2Y;return dX=aabb.minVertex.x,dY=aabb.minVertex.y,dX-=this.m_worldAABB.maxVertex.x,dY-=this.m_worldAABB.maxVertex.y,d2X=this.m_worldAABB.minVertex.x,d2Y=this.m_worldAABB.minVertex.y,d2X-=aabb.maxVertex.x,d2Y-=aabb.maxVertex.y,dX=b2Math.b2Max(dX,d2X),dY=b2Math.b2Max(dY,d2Y),b2Math.b2Max(dX,dY)<0},GetProxy:function(proxyId){return proxyId==b2Pair.b2_nullProxy||0==this.m_proxyPool[proxyId].IsValid()?null:this.m_proxyPool[proxyId]},CreateProxy:function(aabb,userData){var proxy,index=0,proxyId=this.m_freeProxy;proxy=this.m_proxyPool[proxyId],this.m_freeProxy=proxy.GetNext(),proxy.overlapCount=0,proxy.userData=userData;var boundCount=2*this.m_proxyCount,lowerValues=new Array,upperValues=new Array;this.ComputeBounds(lowerValues,upperValues,aabb);for(var axis=0;axis<2;++axis){var bounds=this.m_bounds[axis],lowerIndex=0,upperIndex=0,lowerIndexOut=[lowerIndex],upperIndexOut=[upperIndex];this.Query(lowerIndexOut,upperIndexOut,lowerValues[axis],upperValues[axis],bounds,boundCount,axis),lowerIndex=lowerIndexOut[0],upperIndex=upperIndexOut[0];var tBound1,tBound2,tArr=new Array,j=0,tEnd=boundCount-upperIndex;for(j=0;j<tEnd;j++)tArr[j]=new b2Bound,tBound1=tArr[j],tBound2=bounds[upperIndex+j],tBound1.value=tBound2.value,tBound1.proxyId=tBound2.proxyId,tBound1.stabbingCount=tBound2.stabbingCount;tEnd=tArr.length;var tIndex=upperIndex+2;for(j=0;j<tEnd;j++)tBound2=tArr[j],(tBound1=bounds[tIndex+j]).value=tBound2.value,tBound1.proxyId=tBound2.proxyId,tBound1.stabbingCount=tBound2.stabbingCount;for(tArr=new Array,tEnd=upperIndex-lowerIndex,j=0;j<tEnd;j++)tArr[j]=new b2Bound,tBound1=tArr[j],tBound2=bounds[lowerIndex+j],tBound1.value=tBound2.value,tBound1.proxyId=tBound2.proxyId,tBound1.stabbingCount=tBound2.stabbingCount;for(tEnd=tArr.length,tIndex=lowerIndex+1,j=0;j<tEnd;j++)tBound2=tArr[j],(tBound1=bounds[tIndex+j]).value=tBound2.value,tBound1.proxyId=tBound2.proxyId,tBound1.stabbingCount=tBound2.stabbingCount;for(++upperIndex,bounds[lowerIndex].value=lowerValues[axis],bounds[lowerIndex].proxyId=proxyId,bounds[upperIndex].value=upperValues[axis],bounds[upperIndex].proxyId=proxyId,bounds[lowerIndex].stabbingCount=0==lowerIndex?0:bounds[lowerIndex-1].stabbingCount,bounds[upperIndex].stabbingCount=bounds[upperIndex-1].stabbingCount,index=lowerIndex;index<upperIndex;++index)bounds[index].stabbingCount++;for(index=lowerIndex;index<boundCount+2;++index){var proxy2=this.m_proxyPool[bounds[index].proxyId];bounds[index].IsLower()?proxy2.lowerBounds[axis]=index:proxy2.upperBounds[axis]=index}}++this.m_proxyCount;for(var i=0;i<this.m_queryResultCount;++i)this.m_pairManager.AddBufferedPair(proxyId,this.m_queryResults[i]);return this.m_pairManager.Commit(),this.m_queryResultCount=0,this.IncrementTimeStamp(),proxyId},DestroyProxy:function(proxyId){for(var proxy=this.m_proxyPool[proxyId],boundCount=2*this.m_proxyCount,axis=0;axis<2;++axis){var tBound1,tBound2,bounds=this.m_bounds[axis],lowerIndex=proxy.lowerBounds[axis],upperIndex=proxy.upperBounds[axis],lowerValue=bounds[lowerIndex].value,upperValue=bounds[upperIndex].value,tArr=new Array,j=0,tEnd=upperIndex-lowerIndex-1;for(j=0;j<tEnd;j++)tArr[j]=new b2Bound,tBound1=tArr[j],tBound2=bounds[lowerIndex+1+j],tBound1.value=tBound2.value,tBound1.proxyId=tBound2.proxyId,tBound1.stabbingCount=tBound2.stabbingCount;tEnd=tArr.length;var tIndex=lowerIndex;for(j=0;j<tEnd;j++)tBound2=tArr[j],(tBound1=bounds[tIndex+j]).value=tBound2.value,tBound1.proxyId=tBound2.proxyId,tBound1.stabbingCount=tBound2.stabbingCount;for(tArr=new Array,tEnd=boundCount-upperIndex-1,j=0;j<tEnd;j++)tArr[j]=new b2Bound,tBound1=tArr[j],tBound2=bounds[upperIndex+1+j],tBound1.value=tBound2.value,tBound1.proxyId=tBound2.proxyId,tBound1.stabbingCount=tBound2.stabbingCount;for(tEnd=tArr.length,tIndex=upperIndex-1,j=0;j<tEnd;j++)tBound2=tArr[j],(tBound1=bounds[tIndex+j]).value=tBound2.value,tBound1.proxyId=tBound2.proxyId,tBound1.stabbingCount=tBound2.stabbingCount;tEnd=boundCount-2;for(var index=lowerIndex;index<tEnd;++index){var proxy2=this.m_proxyPool[bounds[index].proxyId];bounds[index].IsLower()?proxy2.lowerBounds[axis]=index:proxy2.upperBounds[axis]=index}tEnd=upperIndex-1;for(var index2=lowerIndex;index2<tEnd;++index2)bounds[index2].stabbingCount--;this.Query([0],[0],lowerValue,upperValue,bounds,boundCount-2,axis)}for(var i=0;i<this.m_queryResultCount;++i)this.m_pairManager.RemoveBufferedPair(proxyId,this.m_queryResults[i]);this.m_pairManager.Commit(),this.m_queryResultCount=0,this.IncrementTimeStamp(),proxy.userData=null,proxy.overlapCount=b2BroadPhase.b2_invalid,proxy.lowerBounds[0]=b2BroadPhase.b2_invalid,proxy.lowerBounds[1]=b2BroadPhase.b2_invalid,proxy.upperBounds[0]=b2BroadPhase.b2_invalid,proxy.upperBounds[1]=b2BroadPhase.b2_invalid,proxy.SetNext(this.m_freeProxy),this.m_freeProxy=proxyId,--this.m_proxyCount},MoveProxy:function(proxyId,aabb){var bound,prevBound,nextBound,nextProxy,axis=0,index=0,nextProxyId=0;if(!(proxyId==b2Pair.b2_nullProxy||b2Settings.b2_maxProxies<=proxyId)&&0!=aabb.IsValid()){var boundCount=2*this.m_proxyCount,proxy=this.m_proxyPool[proxyId],newValues=new b2BoundValues;this.ComputeBounds(newValues.lowerValues,newValues.upperValues,aabb);var oldValues=new b2BoundValues;for(axis=0;axis<2;++axis)oldValues.lowerValues[axis]=this.m_bounds[axis][proxy.lowerBounds[axis]].value,oldValues.upperValues[axis]=this.m_bounds[axis][proxy.upperBounds[axis]].value;for(axis=0;axis<2;++axis){var bounds=this.m_bounds[axis],lowerIndex=proxy.lowerBounds[axis],upperIndex=proxy.upperBounds[axis],lowerValue=newValues.lowerValues[axis],upperValue=newValues.upperValues[axis],deltaLower=lowerValue-bounds[lowerIndex].value,deltaUpper=upperValue-bounds[upperIndex].value;if(bounds[lowerIndex].value=lowerValue,bounds[upperIndex].value=upperValue,deltaLower<0)for(index=lowerIndex;index>0&&lowerValue<bounds[index-1].value;){bound=bounds[index];var prevProxyId=(prevBound=bounds[index-1]).proxyId,prevProxy=this.m_proxyPool[prevBound.proxyId];prevBound.stabbingCount++,1==prevBound.IsUpper()?(this.TestOverlap(newValues,prevProxy)&&this.m_pairManager.AddBufferedPair(proxyId,prevProxyId),prevProxy.upperBounds[axis]++,bound.stabbingCount++):(prevProxy.lowerBounds[axis]++,bound.stabbingCount--),proxy.lowerBounds[axis]--,bound.Swap(prevBound),--index}if(deltaUpper>0)for(index=upperIndex;index<boundCount-1&&bounds[index+1].value<=upperValue;)bound=bounds[index],nextProxyId=(nextBound=bounds[index+1]).proxyId,nextProxy=this.m_proxyPool[nextProxyId],nextBound.stabbingCount++,1==nextBound.IsLower()?(this.TestOverlap(newValues,nextProxy)&&this.m_pairManager.AddBufferedPair(proxyId,nextProxyId),nextProxy.lowerBounds[axis]--,bound.stabbingCount++):(nextProxy.upperBounds[axis]--,bound.stabbingCount--),proxy.upperBounds[axis]++,bound.Swap(nextBound),index++;if(deltaLower>0)for(index=lowerIndex;index<boundCount-1&&bounds[index+1].value<=lowerValue;)bound=bounds[index],nextProxyId=(nextBound=bounds[index+1]).proxyId,nextProxy=this.m_proxyPool[nextProxyId],nextBound.stabbingCount--,nextBound.IsUpper()?(this.TestOverlap(oldValues,nextProxy)&&this.m_pairManager.RemoveBufferedPair(proxyId,nextProxyId),nextProxy.upperBounds[axis]--,bound.stabbingCount--):(nextProxy.lowerBounds[axis]--,bound.stabbingCount++),proxy.lowerBounds[axis]++,bound.Swap(nextBound),index++;if(deltaUpper<0)for(index=upperIndex;index>0&&upperValue<bounds[index-1].value;)bound=bounds[index],prevProxyId=(prevBound=bounds[index-1]).proxyId,prevProxy=this.m_proxyPool[prevProxyId],prevBound.stabbingCount--,1==prevBound.IsLower()?(this.TestOverlap(oldValues,prevProxy)&&this.m_pairManager.RemoveBufferedPair(proxyId,prevProxyId),prevProxy.lowerBounds[axis]++,bound.stabbingCount--):(prevProxy.upperBounds[axis]++,bound.stabbingCount++),proxy.upperBounds[axis]--,bound.Swap(prevBound),index--}}},Commit:function(){this.m_pairManager.Commit()},QueryAABB:function(aabb,userData,maxCount){var lowerValues=new Array,upperValues=new Array;this.ComputeBounds(lowerValues,upperValues,aabb);var lowerIndexOut=[0],upperIndexOut=[0];this.Query(lowerIndexOut,upperIndexOut,lowerValues[0],upperValues[0],this.m_bounds[0],2*this.m_proxyCount,0),this.Query(lowerIndexOut,upperIndexOut,lowerValues[1],upperValues[1],this.m_bounds[1],2*this.m_proxyCount,1);for(var count=0,i=0;i<this.m_queryResultCount&&count<maxCount;++i,++count){var proxy=this.m_proxyPool[this.m_queryResults[i]];userData[i]=proxy.userData}return this.m_queryResultCount=0,this.IncrementTimeStamp(),count},Validate:function(){for(var axis=0;axis<2;++axis)for(var bounds=this.m_bounds[axis],boundCount=2*this.m_proxyCount,i=0;i<boundCount;++i){1==bounds[i].IsLower()?0:0}},ComputeBounds:function(lowerValues,upperValues,aabb){var minVertexX=aabb.minVertex.x,minVertexY=aabb.minVertex.y;minVertexX=b2Math.b2Min(minVertexX,this.m_worldAABB.maxVertex.x),minVertexY=b2Math.b2Min(minVertexY,this.m_worldAABB.maxVertex.y),minVertexX=b2Math.b2Max(minVertexX,this.m_worldAABB.minVertex.x),minVertexY=b2Math.b2Max(minVertexY,this.m_worldAABB.minVertex.y);var maxVertexX=aabb.maxVertex.x,maxVertexY=aabb.maxVertex.y;maxVertexX=b2Math.b2Min(maxVertexX,this.m_worldAABB.maxVertex.x),maxVertexY=b2Math.b2Min(maxVertexY,this.m_worldAABB.maxVertex.y),maxVertexX=b2Math.b2Max(maxVertexX,this.m_worldAABB.minVertex.x),maxVertexY=b2Math.b2Max(maxVertexY,this.m_worldAABB.minVertex.y),lowerValues[0]=this.m_quantizationFactor.x*(minVertexX-this.m_worldAABB.minVertex.x)&b2Settings.USHRT_MAX-1,upperValues[0]=this.m_quantizationFactor.x*(maxVertexX-this.m_worldAABB.minVertex.x)&65535|1,lowerValues[1]=this.m_quantizationFactor.y*(minVertexY-this.m_worldAABB.minVertex.y)&b2Settings.USHRT_MAX-1,upperValues[1]=this.m_quantizationFactor.y*(maxVertexY-this.m_worldAABB.minVertex.y)&65535|1},TestOverlapValidate:function(p1,p2){for(var axis=0;axis<2;++axis){var bounds=this.m_bounds[axis];if(bounds[p1.lowerBounds[axis]].value>bounds[p2.upperBounds[axis]].value)return!1;if(bounds[p1.upperBounds[axis]].value<bounds[p2.lowerBounds[axis]].value)return!1}return!0},TestOverlap:function(b,p){for(var axis=0;axis<2;++axis){var bounds=this.m_bounds[axis];if(b.lowerValues[axis]>bounds[p.upperBounds[axis]].value)return!1;if(b.upperValues[axis]<bounds[p.lowerBounds[axis]].value)return!1}return!0},Query:function(lowerQueryOut,upperQueryOut,lowerValue,upperValue,bounds,boundCount,axis){for(var lowerQuery=b2BroadPhase.BinarySearch(bounds,boundCount,lowerValue),upperQuery=b2BroadPhase.BinarySearch(bounds,boundCount,upperValue),j=lowerQuery;j<upperQuery;++j)bounds[j].IsLower()&&this.IncrementOverlapCount(bounds[j].proxyId);if(lowerQuery>0)for(var i=lowerQuery-1,s=bounds[i].stabbingCount;s;){if(bounds[i].IsLower())lowerQuery<=this.m_proxyPool[bounds[i].proxyId].upperBounds[axis]&&(this.IncrementOverlapCount(bounds[i].proxyId),--s);--i}lowerQueryOut[0]=lowerQuery,upperQueryOut[0]=upperQuery},IncrementOverlapCount:function(proxyId){var proxy=this.m_proxyPool[proxyId];proxy.timeStamp<this.m_timeStamp?(proxy.timeStamp=this.m_timeStamp,proxy.overlapCount=1):(proxy.overlapCount=2,this.m_queryResults[this.m_queryResultCount]=proxyId,++this.m_queryResultCount)},IncrementTimeStamp:function(){if(this.m_timeStamp==b2Settings.USHRT_MAX){for(var i=0;i<b2Settings.b2_maxProxies;++i)this.m_proxyPool[i].timeStamp=0;this.m_timeStamp=1}else++this.m_timeStamp},m_pairManager:new b2PairManager,m_proxyPool:new Array(b2Settings.b2_maxPairs),m_freeProxy:0,m_bounds:new Array(2*b2Settings.b2_maxProxies),m_queryResults:new Array(b2Settings.b2_maxProxies),m_queryResultCount:0,m_worldAABB:null,m_quantizationFactor:new b2Vec2,m_proxyCount:0,m_timeStamp:0},b2BroadPhase.s_validate=!1,b2BroadPhase.b2_invalid=b2Settings.USHRT_MAX,b2BroadPhase.b2_nullEdge=b2Settings.USHRT_MAX,b2BroadPhase.BinarySearch=function(bounds,count,value){for(var low=0,high=count-1;low<=high;){var mid=Math.floor((low+high)/2);if(bounds[mid].value>value)high=mid-1;else{if(!(bounds[mid].value<value))return mid;low=mid+1}}return low};var b2Collision=Class.create();b2Collision.prototype={initialize:function(){}},b2Collision.b2_nullFeature=255,b2Collision.ClipSegmentToLine=function(vOut,vIn,normal,offset){var numOut=0,vIn0=vIn[0].v,vIn1=vIn[1].v,distance0=b2Math.b2Dot(normal,vIn[0].v)-offset,distance1=b2Math.b2Dot(normal,vIn[1].v)-offset;if(distance0<=0&&(vOut[numOut++]=vIn[0]),distance1<=0&&(vOut[numOut++]=vIn[1]),distance0*distance1<0){var interp=distance0/(distance0-distance1),tVec=vOut[numOut].v;tVec.x=vIn0.x+interp*(vIn1.x-vIn0.x),tVec.y=vIn0.y+interp*(vIn1.y-vIn0.y),vOut[numOut].id=distance0>0?vIn[0].id:vIn[1].id,++numOut}return numOut},b2Collision.EdgeSeparation=function(poly1,edge1,poly2){var vert1s=poly1.m_vertices,count2=poly2.m_vertexCount,vert2s=poly2.m_vertices,normalX=poly1.m_normals[edge1].x,normalY=poly1.m_normals[edge1].y,tX=normalX,tMat=poly1.m_R,normalLocal2X=normalX=tMat.col1.x*tX+tMat.col2.x*normalY,normalLocal2Y=normalY=tMat.col1.y*tX+tMat.col2.y*normalY;tX=normalLocal2X*(tMat=poly2.m_R).col1.x+normalLocal2Y*tMat.col1.y,normalLocal2Y=normalLocal2X*tMat.col2.x+normalLocal2Y*tMat.col2.y,normalLocal2X=tX;for(var vertexIndex2=0,minDot=Number.MAX_VALUE,i=0;i<count2;++i){var tVec=vert2s[i],dot=tVec.x*normalLocal2X+tVec.y*normalLocal2Y;dot<minDot&&(minDot=dot,vertexIndex2=i)}tMat=poly1.m_R;var v1X=poly1.m_position.x+(tMat.col1.x*vert1s[edge1].x+tMat.col2.x*vert1s[edge1].y),v1Y=poly1.m_position.y+(tMat.col1.y*vert1s[edge1].x+tMat.col2.y*vert1s[edge1].y);tMat=poly2.m_R;var v2X=poly2.m_position.x+(tMat.col1.x*vert2s[vertexIndex2].x+tMat.col2.x*vert2s[vertexIndex2].y),v2Y=poly2.m_position.y+(tMat.col1.y*vert2s[vertexIndex2].x+tMat.col2.y*vert2s[vertexIndex2].y);return(v2X-=v1X)*normalX+(v2Y-=v1Y)*normalY},b2Collision.FindMaxSeparation=function(edgeIndex,poly1,poly2,conservative){for(var count1=poly1.m_vertexCount,dX=poly2.m_position.x-poly1.m_position.x,dY=poly2.m_position.y-poly1.m_position.y,dLocal1X=dX*poly1.m_R.col1.x+dY*poly1.m_R.col1.y,dLocal1Y=dX*poly1.m_R.col2.x+dY*poly1.m_R.col2.y,edge=0,maxDot=-Number.MAX_VALUE,i=0;i<count1;++i){var dot=poly1.m_normals[i].x*dLocal1X+poly1.m_normals[i].y*dLocal1Y;dot>maxDot&&(maxDot=dot,edge=i)}var s=b2Collision.EdgeSeparation(poly1,edge,poly2);if(s>0&&0==conservative)return s;var prevEdge=edge-1>=0?edge-1:count1-1,sPrev=b2Collision.EdgeSeparation(poly1,prevEdge,poly2);if(sPrev>0&&0==conservative)return sPrev;var nextEdge=edge+1<count1?edge+1:0,sNext=b2Collision.EdgeSeparation(poly1,nextEdge,poly2);if(sNext>0&&0==conservative)return sNext;var bestSeparation,bestEdge=0,increment=0;if(sPrev>s&&sPrev>sNext)increment=-1,bestEdge=prevEdge,bestSeparation=sPrev;else{if(!(sNext>s))return edgeIndex[0]=edge,s;increment=1,bestEdge=nextEdge,bestSeparation=sNext}for(;;){if(edge=-1==increment?bestEdge-1>=0?bestEdge-1:count1-1:bestEdge+1<count1?bestEdge+1:0,(s=b2Collision.EdgeSeparation(poly1,edge,poly2))>0&&0==conservative)return s;if(!(s>bestSeparation))break;bestEdge=edge,bestSeparation=s}return edgeIndex[0]=bestEdge,bestSeparation},b2Collision.FindIncidentEdge=function(c,poly1,edge1,poly2){var count1=poly1.m_vertexCount,vert1s=poly1.m_vertices,count2=poly2.m_vertexCount,vert2s=poly2.m_vertices,vertex11=edge1,tVec=vert1s[edge1+1==count1?0:edge1+1],normal1Local1X=tVec.x,normal1Local1Y=tVec.y,tX=normal1Local1X-=(tVec=vert1s[vertex11]).x;normal1Local1X=normal1Local1Y-=tVec.y,normal1Local1Y=-tX;var invLength=1/Math.sqrt(normal1Local1X*normal1Local1X+normal1Local1Y*normal1Local1Y),normal1X=normal1Local1X*=invLength,normal1Y=normal1Local1Y*=invLength;tX=normal1X;var tMat=poly1.m_R,normal1Local2X=normal1X=tMat.col1.x*tX+tMat.col2.x*normal1Y,normal1Local2Y=normal1Y=tMat.col1.y*tX+tMat.col2.y*normal1Y;tX=normal1Local2X*(tMat=poly2.m_R).col1.x+normal1Local2Y*tMat.col1.y,normal1Local2Y=normal1Local2X*tMat.col2.x+normal1Local2Y*tMat.col2.y,normal1Local2X=tX;for(var tClip,vertex21=0,vertex22=0,minDot=Number.MAX_VALUE,i=0;i<count2;++i){var i1=i,i2=i+1<count2?i+1:0,normal2Local2X=(tVec=vert2s[i2]).x,normal2Local2Y=tVec.y;tX=normal2Local2X-=(tVec=vert2s[i1]).x,normal2Local2X=normal2Local2Y-=tVec.y,normal2Local2Y=-tX;var dot=(normal2Local2X*=invLength=1/Math.sqrt(normal2Local2X*normal2Local2X+normal2Local2Y*normal2Local2Y))*normal1Local2X+(normal2Local2Y*=invLength)*normal1Local2Y;dot<minDot&&(minDot=dot,vertex21=i1,vertex22=i2)}(tVec=(tClip=c[0]).v).SetV(vert2s[vertex21]),tVec.MulM(poly2.m_R),tVec.Add(poly2.m_position),tClip.id.features.referenceFace=edge1,tClip.id.features.incidentEdge=vertex21,tClip.id.features.incidentVertex=vertex21,(tVec=(tClip=c[1]).v).SetV(vert2s[vertex22]),tVec.MulM(poly2.m_R),tVec.Add(poly2.m_position),tClip.id.features.referenceFace=edge1,tClip.id.features.incidentEdge=vertex21,tClip.id.features.incidentVertex=vertex22},b2Collision.b2CollidePolyTempVec=new b2Vec2,b2Collision.b2CollidePoly=function(manifold,polyA,polyB,conservative){manifold.pointCount=0;var edgeA=0,edgeAOut=[edgeA],separationA=b2Collision.FindMaxSeparation(edgeAOut,polyA,polyB,conservative);if(edgeA=edgeAOut[0],!(separationA>0&&0==conservative)){var edgeB=0,edgeBOut=[edgeB],separationB=b2Collision.FindMaxSeparation(edgeBOut,polyB,polyA,conservative);if(edgeB=edgeBOut[0],!(separationB>0&&0==conservative)){var poly1,poly2,edge1=0,flip=0;separationB>.98*separationA+.001?(poly1=polyB,poly2=polyA,edge1=edgeB,flip=1):(poly1=polyA,poly2=polyB,edge1=edgeA,flip=0);var incidentEdge=[new ClipVertex,new ClipVertex];b2Collision.FindIncidentEdge(incidentEdge,poly1,edge1,poly2);var count1=poly1.m_vertexCount,vert1s=poly1.m_vertices,v11=vert1s[edge1],v12=edge1+1<count1?vert1s[edge1+1]:vert1s[0],sideNormalX=(v12.x,v11.x,v12.y,v11.y,v12.x-v11.x),sideNormalY=v12.y-v11.y,tX=sideNormalX,tMat=poly1.m_R;sideNormalX=tMat.col1.x*tX+tMat.col2.x*sideNormalY,sideNormalY=tMat.col1.y*tX+tMat.col2.y*sideNormalY;var invLength=1/Math.sqrt(sideNormalX*sideNormalX+sideNormalY*sideNormalY),frontNormalX=sideNormalX*=invLength,frontNormalY=sideNormalY*=invLength;tX=frontNormalX,frontNormalX=frontNormalY,frontNormalY=-tX;var v11X=v11.x,v11Y=v11.y;tX=v11X,v11X=(tMat=poly1.m_R).col1.x*tX+tMat.col2.x*v11Y,v11Y=tMat.col1.y*tX+tMat.col2.y*v11Y,v11X+=poly1.m_position.x,v11Y+=poly1.m_position.y;var v12X=v12.x,v12Y=v12.y;tX=v12X,v12X=(tMat=poly1.m_R).col1.x*tX+tMat.col2.x*v12Y,v12Y=tMat.col1.y*tX+tMat.col2.y*v12Y;var frontOffset=frontNormalX*v11X+frontNormalY*v11Y,sideOffset1=-(sideNormalX*v11X+sideNormalY*v11Y),sideOffset2=sideNormalX*(v12X+=poly1.m_position.x)+sideNormalY*(v12Y+=poly1.m_position.y),clipPoints1=[new ClipVertex,new ClipVertex],clipPoints2=[new ClipVertex,new ClipVertex];if(b2Collision.b2CollidePolyTempVec.Set(-sideNormalX,-sideNormalY),!(b2Collision.ClipSegmentToLine(clipPoints1,incidentEdge,b2Collision.b2CollidePolyTempVec,sideOffset1)<2||(b2Collision.b2CollidePolyTempVec.Set(sideNormalX,sideNormalY),b2Collision.ClipSegmentToLine(clipPoints2,clipPoints1,b2Collision.b2CollidePolyTempVec,sideOffset2)<2))){flip?manifold.normal.Set(-frontNormalX,-frontNormalY):manifold.normal.Set(frontNormalX,frontNormalY);for(var pointCount=0,i=0;i<b2Settings.b2_maxManifoldPoints;++i){var tVec=clipPoints2[i].v,separation=frontNormalX*tVec.x+frontNormalY*tVec.y-frontOffset;if(separation<=0||1==conservative){var cp=manifold.points[pointCount];cp.separation=separation,cp.position.SetV(clipPoints2[i].v),cp.id.Set(clipPoints2[i].id),cp.id.features.flip=flip,++pointCount}}manifold.pointCount=pointCount}}}},b2Collision.b2CollideCircle=function(manifold,circle1,circle2,conservative){manifold.pointCount=0;var dX=circle2.m_position.x-circle1.m_position.x,dY=circle2.m_position.y-circle1.m_position.y,distSqr=dX*dX+dY*dY,radiusSum=circle1.m_radius+circle2.m_radius;if(!(distSqr>radiusSum*radiusSum&&0==conservative)){var separation;if(distSqr<Number.MIN_VALUE)separation=-radiusSum,manifold.normal.Set(0,1);else{var dist=Math.sqrt(distSqr);separation=dist-radiusSum;var a=1/dist;manifold.normal.x=a*dX,manifold.normal.y=a*dY}manifold.pointCount=1;var tPoint=manifold.points[0];tPoint.id.set_key(0),tPoint.separation=separation,tPoint.position.x=circle2.m_position.x-circle2.m_radius*manifold.normal.x,tPoint.position.y=circle2.m_position.y-circle2.m_radius*manifold.normal.y}},b2Collision.b2CollidePolyAndCircle=function(manifold,poly,circle,conservative){var tPoint,dX,dY;manifold.pointCount=0;var dist,xLocalX=circle.m_position.x-poly.m_position.x,xLocalY=circle.m_position.y-poly.m_position.y,tMat=poly.m_R,tX=xLocalX*tMat.col1.x+xLocalY*tMat.col1.y;xLocalY=xLocalX*tMat.col2.x+xLocalY*tMat.col2.y,xLocalX=tX;for(var normalIndex=0,separation=-Number.MAX_VALUE,radius=circle.m_radius,i=0;i<poly.m_vertexCount;++i){var s=poly.m_normals[i].x*(xLocalX-poly.m_vertices[i].x)+poly.m_normals[i].y*(xLocalY-poly.m_vertices[i].y);if(s>radius)return;s>separation&&(separation=s,normalIndex=i)}if(separation<Number.MIN_VALUE){manifold.pointCount=1;var tVec=poly.m_normals[normalIndex];return manifold.normal.x=tMat.col1.x*tVec.x+tMat.col2.x*tVec.y,manifold.normal.y=tMat.col1.y*tVec.x+tMat.col2.y*tVec.y,(tPoint=manifold.points[0]).id.features.incidentEdge=normalIndex,tPoint.id.features.incidentVertex=b2Collision.b2_nullFeature,tPoint.id.features.referenceFace=b2Collision.b2_nullFeature,tPoint.id.features.flip=0,tPoint.position.x=circle.m_position.x-radius*manifold.normal.x,tPoint.position.y=circle.m_position.y-radius*manifold.normal.y,void(tPoint.separation=separation-radius)}var vertIndex1=normalIndex,vertIndex2=vertIndex1+1<poly.m_vertexCount?vertIndex1+1:0,eX=poly.m_vertices[vertIndex2].x-poly.m_vertices[vertIndex1].x,eY=poly.m_vertices[vertIndex2].y-poly.m_vertices[vertIndex1].y,length=Math.sqrt(eX*eX+eY*eY);if(eX/=length,eY/=length,length<Number.MIN_VALUE){if(dX=xLocalX-poly.m_vertices[vertIndex1].x,dY=xLocalY-poly.m_vertices[vertIndex1].y,dX/=dist=Math.sqrt(dX*dX+dY*dY),dY/=dist,dist>radius)return;return manifold.pointCount=1,manifold.normal.Set(tMat.col1.x*dX+tMat.col2.x*dY,tMat.col1.y*dX+tMat.col2.y*dY),(tPoint=manifold.points[0]).id.features.incidentEdge=b2Collision.b2_nullFeature,tPoint.id.features.incidentVertex=vertIndex1,tPoint.id.features.referenceFace=b2Collision.b2_nullFeature,tPoint.id.features.flip=0,tPoint.position.x=circle.m_position.x-radius*manifold.normal.x,tPoint.position.y=circle.m_position.y-radius*manifold.normal.y,void(tPoint.separation=dist-radius)}var pX,pY,u=(xLocalX-poly.m_vertices[vertIndex1].x)*eX+(xLocalY-poly.m_vertices[vertIndex1].y)*eY;(tPoint=manifold.points[0]).id.features.incidentEdge=b2Collision.b2_nullFeature,tPoint.id.features.incidentVertex=b2Collision.b2_nullFeature,tPoint.id.features.referenceFace=b2Collision.b2_nullFeature,tPoint.id.features.flip=0,u<=0?(pX=poly.m_vertices[vertIndex1].x,pY=poly.m_vertices[vertIndex1].y,tPoint.id.features.incidentVertex=vertIndex1):u>=length?(pX=poly.m_vertices[vertIndex2].x,pY=poly.m_vertices[vertIndex2].y,tPoint.id.features.incidentVertex=vertIndex2):(pX=eX*u+poly.m_vertices[vertIndex1].x,pY=eY*u+poly.m_vertices[vertIndex1].y,tPoint.id.features.incidentEdge=vertIndex1),dX=xLocalX-pX,dY=xLocalY-pY,dX/=dist=Math.sqrt(dX*dX+dY*dY),dY/=dist,dist>radius||(manifold.pointCount=1,manifold.normal.Set(tMat.col1.x*dX+tMat.col2.x*dY,tMat.col1.y*dX+tMat.col2.y*dY),tPoint.position.x=circle.m_position.x-radius*manifold.normal.x,tPoint.position.y=circle.m_position.y-radius*manifold.normal.y,tPoint.separation=dist-radius)},b2Collision.b2TestOverlap=function(a,b){var t1=b.minVertex,t2=a.maxVertex,d1X=t1.x-t2.x,d1Y=t1.y-t2.y;t1=a.minVertex,t2=b.maxVertex;var d2X=t1.x-t2.x,d2Y=t1.y-t2.y;return!(d1X>0||d1Y>0)&&!(d2X>0||d2Y>0)};var Features=Class.create();Features.prototype={set_referenceFace:function(value){this._referenceFace=value,this._m_id._key=4294967040&this._m_id._key|255&this._referenceFace},get_referenceFace:function(){return this._referenceFace},_referenceFace:0,set_incidentEdge:function(value){this._incidentEdge=value,this._m_id._key=4294902015&this._m_id._key|this._incidentEdge<<8&65280},get_incidentEdge:function(){return this._incidentEdge},_incidentEdge:0,set_incidentVertex:function(value){this._incidentVertex=value,this._m_id._key=4278255615&this._m_id._key|this._incidentVertex<<16&16711680},get_incidentVertex:function(){return this._incidentVertex},_incidentVertex:0,set_flip:function(value){this._flip=value,this._m_id._key=16777215&this._m_id._key|this._flip<<24&4278190080},get_flip:function(){return this._flip},_flip:0,_m_id:null,initialize:function(){}};var b2ContactID=Class.create();b2ContactID.prototype={initialize:function(){this.features=new Features,this.features._m_id=this},Set:function(id){this.set_key(id._key)},Copy:function(){var id=new b2ContactID;return id.set_key(this._key),id},get_key:function(){return this._key},set_key:function(value){this._key=value,this.features._referenceFace=255&this._key,this.features._incidentEdge=(65280&this._key)>>8&255,this.features._incidentVertex=(16711680&this._key)>>16&255,this.features._flip=(4278190080&this._key)>>24&255},features:new Features,_key:0};var b2ContactPoint=Class.create();b2ContactPoint.prototype={position:new b2Vec2,separation:null,normalImpulse:null,tangentImpulse:null,id:new b2ContactID,initialize:function(){this.position=new b2Vec2,this.id=new b2ContactID}};var b2Distance=Class.create();b2Distance.prototype={initialize:function(){}},b2Distance.ProcessTwo=function(p1Out,p2Out,p1s,p2s,points){var rX=-points[1].x,rY=-points[1].y,dX=points[0].x-points[1].x,dY=points[0].y-points[1].y,length=Math.sqrt(dX*dX+dY*dY),lambda=rX*(dX/=length)+rY*(dY/=length);return lambda<=0||length<Number.MIN_VALUE?(p1Out.SetV(p1s[1]),p2Out.SetV(p2s[1]),p1s[0].SetV(p1s[1]),p2s[0].SetV(p2s[1]),points[0].SetV(points[1]),1):(lambda/=length,p1Out.x=p1s[1].x+lambda*(p1s[0].x-p1s[1].x),p1Out.y=p1s[1].y+lambda*(p1s[0].y-p1s[1].y),p2Out.x=p2s[1].x+lambda*(p2s[0].x-p2s[1].x),p2Out.y=p2s[1].y+lambda*(p2s[0].y-p2s[1].y),2)},b2Distance.ProcessThree=function(p1Out,p2Out,p1s,p2s,points){var aX=points[0].x,aY=points[0].y,bX=points[1].x,bY=points[1].y,cX=points[2].x,cY=points[2].y,abX=bX-aX,abY=bY-aY,acX=cX-aX,acY=cY-aY,bcX=cX-bX,bcY=cY-bY,tn=-(aX*acX+aY*acY),td=cX*acX+cY*acY,un=-(bX*bcX+bY*bcY),ud=cX*bcX+cY*bcY;if(td<=0&&ud<=0)return p1Out.SetV(p1s[2]),p2Out.SetV(p2s[2]),p1s[0].SetV(p1s[2]),p2s[0].SetV(p2s[2]),points[0].SetV(points[2]),1;var n=abX*acY-abY*acX,vc=n*(aX*bY-aY*bX),va=n*(bX*cY-bY*cX);if(va<=0&&un>=0&&ud>=0){var lambda=un/(un+ud);return p1Out.x=p1s[1].x+lambda*(p1s[2].x-p1s[1].x),p1Out.y=p1s[1].y+lambda*(p1s[2].y-p1s[1].y),p2Out.x=p2s[1].x+lambda*(p2s[2].x-p2s[1].x),p2Out.y=p2s[1].y+lambda*(p2s[2].y-p2s[1].y),p1s[0].SetV(p1s[2]),p2s[0].SetV(p2s[2]),points[0].SetV(points[2]),2}var vb=n*(cX*aY-cY*aX);if(vb<=0&&tn>=0&&td>=0){lambda=tn/(tn+td);return p1Out.x=p1s[0].x+lambda*(p1s[2].x-p1s[0].x),p1Out.y=p1s[0].y+lambda*(p1s[2].y-p1s[0].y),p2Out.x=p2s[0].x+lambda*(p2s[2].x-p2s[0].x),p2Out.y=p2s[0].y+lambda*(p2s[2].y-p2s[0].y),p1s[1].SetV(p1s[2]),p2s[1].SetV(p2s[2]),points[1].SetV(points[2]),2}var denom=va+vb+vc,u=va*(denom=1/denom),v=vb*denom,w=1-u-v;return p1Out.x=u*p1s[0].x+v*p1s[1].x+w*p1s[2].x,p1Out.y=u*p1s[0].y+v*p1s[1].y+w*p1s[2].y,p2Out.x=u*p2s[0].x+v*p2s[1].x+w*p2s[2].x,p2Out.y=u*p2s[0].y+v*p2s[1].y+w*p2s[2].y,3},b2Distance.InPoinsts=function(w,points,pointCount){for(var i=0;i<pointCount;++i)if(w.x==points[i].x&&w.y==points[i].y)return!0;return!1},b2Distance.Distance=function(p1Out,p2Out,shape1,shape2){var p1s=new Array(3),p2s=new Array(3),points=new Array(3),pointCount=0;p1Out.SetV(shape1.m_position),p2Out.SetV(shape2.m_position);for(var vSqr=0,iter=0;iter<20;++iter){var vX=p2Out.x-p1Out.x,vY=p2Out.y-p1Out.y,w1=shape1.Support(vX,vY),w2=shape2.Support(-vX,-vY);vSqr=vX*vX+vY*vY;var wX=w2.x-w1.x,wY=w2.y-w1.y;if(vSqr-b2Dot(vX*wX+vY*wY)<=.01*vSqr)return 0==pointCount&&(p1Out.SetV(w1),p2Out.SetV(w2)),b2Distance.g_GJK_Iterations=iter,Math.sqrt(vSqr);switch(pointCount){case 0:p1s[0].SetV(w1),p2s[0].SetV(w2),points[0]=w,p1Out.SetV(p1s[0]),p2Out.SetV(p2s[0]),++pointCount;break;case 1:p1s[1].SetV(w1),p2s[1].SetV(w2),points[1].x=wX,points[1].y=wY,pointCount=b2Distance.ProcessTwo(p1Out,p2Out,p1s,p2s,points);break;case 2:p1s[2].SetV(w1),p2s[2].SetV(w2),points[2].x=wX,points[2].y=wY,pointCount=b2Distance.ProcessThree(p1Out,p2Out,p1s,p2s,points)}if(3==pointCount)return b2Distance.g_GJK_Iterations=iter,0;for(var maxSqr=-Number.MAX_VALUE,i=0;i<pointCount;++i)maxSqr=b2Math.b2Max(maxSqr,points[i].x*points[i].x+points[i].y*points[i].y);if(3==pointCount||vSqr<=100*Number.MIN_VALUE*maxSqr)return b2Distance.g_GJK_Iterations=iter,Math.sqrt(vSqr)}return b2Distance.g_GJK_Iterations=20,Math.sqrt(vSqr)},b2Distance.g_GJK_Iterations=0;var b2Manifold=Class.create();b2Manifold.prototype={initialize:function(){this.points=new Array(b2Settings.b2_maxManifoldPoints);for(var i=0;i<b2Settings.b2_maxManifoldPoints;i++)this.points[i]=new b2ContactPoint;this.normal=new b2Vec2},points:null,normal:null,pointCount:0};var b2OBB=Class.create();b2OBB.prototype={R:new b2Mat22,center:new b2Vec2,extents:new b2Vec2,initialize:function(){this.R=new b2Mat22,this.center=new b2Vec2,this.extents=new b2Vec2}};var b2Proxy=Class.create();b2Proxy.prototype={GetNext:function(){return this.lowerBounds[0]},SetNext:function(next){this.lowerBounds[0]=next},IsValid:function(){return this.overlapCount!=b2BroadPhase.b2_invalid},lowerBounds:[0,0],upperBounds:[0,0],overlapCount:0,timeStamp:0,userData:null,initialize:function(){this.lowerBounds=[0,0],this.upperBounds=[0,0]}};var ClipVertex=Class.create();ClipVertex.prototype={v:new b2Vec2,id:new b2ContactID,initialize:function(){this.v=new b2Vec2,this.id=new b2ContactID}};var b2Shape=Class.create();b2Shape.prototype={TestPoint:function(p){return!1},GetUserData:function(){return this.m_userData},GetType:function(){return this.m_type},GetBody:function(){return this.m_body},GetPosition:function(){return this.m_position},GetRotationMatrix:function(){return this.m_R},ResetProxy:function(broadPhase){},GetNext:function(){return this.m_next},initialize:function(def,body){this.m_R=new b2Mat22,this.m_position=new b2Vec2,this.m_userData=def.userData,this.m_friction=def.friction,this.m_restitution=def.restitution,this.m_body=body,this.m_proxyId=b2Pair.b2_nullProxy,this.m_maxRadius=0,this.m_categoryBits=def.categoryBits,this.m_maskBits=def.maskBits,this.m_groupIndex=def.groupIndex},DestroyProxy:function(){this.m_proxyId!=b2Pair.b2_nullProxy&&(this.m_body.m_world.m_broadPhase.DestroyProxy(this.m_proxyId),this.m_proxyId=b2Pair.b2_nullProxy)},Synchronize:function(position1,R1,position2,R2){},QuickSync:function(position,R){},Support:function(dX,dY,out){},GetMaxRadius:function(){return this.m_maxRadius},m_next:null,m_R:new b2Mat22,m_position:new b2Vec2,m_type:0,m_userData:null,m_body:null,m_friction:null,m_restitution:null,m_maxRadius:null,m_proxyId:0,m_categoryBits:0,m_maskBits:0,m_groupIndex:0},b2Shape.Create=function(def,body,center){switch(def.type){case b2Shape.e_circleShape:return new b2CircleShape(def,body,center);case b2Shape.e_boxShape:case b2Shape.e_polyShape:return new b2PolyShape(def,body,center)}return null},b2Shape.Destroy=function(shape){shape.m_proxyId!=b2Pair.b2_nullProxy&&shape.m_body.m_world.m_broadPhase.DestroyProxy(shape.m_proxyId)},b2Shape.e_unknownShape=-1,b2Shape.e_circleShape=0,b2Shape.e_boxShape=1,b2Shape.e_polyShape=2,b2Shape.e_meshShape=3,b2Shape.e_shapeTypeCount=4,b2Shape.PolyMass=function(massData,vs,count,rho){var center=new b2Vec2;center.SetZero();for(var area=0,I=0,pRef=new b2Vec2(0,0),inv3=1/3,i=0;i<count;++i){var p1=pRef,p2=vs[i],p3=i+1<count?vs[i+1]:vs[0],e1=b2Math.SubtractVV(p2,p1),e2=b2Math.SubtractVV(p3,p1),D=b2Math.b2CrossVV(e1,e2),triangleArea=.5*D;area+=triangleArea;var tVec=new b2Vec2;tVec.SetV(p1),tVec.Add(p2),tVec.Add(p3),tVec.Multiply(inv3*triangleArea),center.Add(tVec);var px=p1.x,py=p1.y,ex1=e1.x,ey1=e1.y,ex2=e2.x,ey2=e2.y;I+=D*(inv3*(.25*(ex1*ex1+ex2*ex1+ex2*ex2)+(px*ex1+px*ex2))+.5*px*px+(inv3*(.25*(ey1*ey1+ey2*ey1+ey2*ey2)+(py*ey1+py*ey2))+.5*py*py))}massData.mass=rho*area,center.Multiply(1/area),massData.center=center,I=rho*(I-area*b2Math.b2Dot(center,center)),massData.I=I},b2Shape.PolyCentroid=function(vs,count,out){for(var cX=0,cY=0,area=0,inv3=1/3,i=0;i<count;++i){var p2X=vs[i].x,p2Y=vs[i].y,p3X=i+1<count?vs[i+1].x:vs[0].x,p3Y=i+1<count?vs[i+1].y:vs[0].y,triangleArea=.5*((p2X-0)*(p3Y-0)-(p2Y-0)*(p3X-0));area+=triangleArea,cX+=triangleArea*inv3*(0+p2X+p3X),cY+=triangleArea*inv3*(0+p2Y+p3Y)}cX*=1/area,cY*=1/area,out.Set(cX,cY)};var b2ShapeDef=Class.create();b2ShapeDef.prototype={initialize:function(){this.type=b2Shape.e_unknownShape,this.userData=null,this.localPosition=new b2Vec2(0,0),this.localRotation=0,this.friction=.2,this.restitution=0,this.density=0,this.categoryBits=1,this.maskBits=65535,this.groupIndex=0},ComputeMass:function(massData){switch(massData.center=new b2Vec2(0,0),0==this.density&&(massData.mass=0,massData.center.Set(0,0),massData.I=0),this.type){case b2Shape.e_circleShape:massData.mass=this.density*b2Settings.b2_pi*this.radius*this.radius,massData.center.Set(0,0),massData.I=.5*massData.mass*this.radius*this.radius;break;case b2Shape.e_boxShape:massData.mass=4*this.density*this.extents.x*this.extents.y,massData.center.Set(0,0),massData.I=massData.mass/3*b2Math.b2Dot(this.extents,this.extents);break;case b2Shape.e_polyShape:b2Shape.PolyMass(massData,this.vertices,this.vertexCount,this.density);break;default:massData.mass=0,massData.center.Set(0,0),massData.I=0}},type:0,userData:null,localPosition:null,localRotation:null,friction:null,restitution:null,density:null,categoryBits:0,maskBits:0,groupIndex:0};var b2BoxDef=Class.create();Object.extend(b2BoxDef.prototype,b2ShapeDef.prototype),Object.extend(b2BoxDef.prototype,{initialize:function(){this.type=b2Shape.e_unknownShape,this.userData=null,this.localPosition=new b2Vec2(0,0),this.localRotation=0,this.friction=.2,this.restitution=0,this.density=0,this.categoryBits=1,this.maskBits=65535,this.groupIndex=0,this.type=b2Shape.e_boxShape,this.extents=new b2Vec2(1,1)},extents:null});var b2CircleDef=Class.create();Object.extend(b2CircleDef.prototype,b2ShapeDef.prototype),Object.extend(b2CircleDef.prototype,{initialize:function(){this.type=b2Shape.e_unknownShape,this.userData=null,this.localPosition=new b2Vec2(0,0),this.localRotation=0,this.friction=.2,this.restitution=0,this.density=0,this.categoryBits=1,this.maskBits=65535,this.groupIndex=0,this.type=b2Shape.e_circleShape,this.radius=1},radius:null});var b2CircleShape=Class.create();Object.extend(b2CircleShape.prototype,b2Shape.prototype),Object.extend(b2CircleShape.prototype,{TestPoint:function(p){var d=new b2Vec2;return d.SetV(p),d.Subtract(this.m_position),b2Math.b2Dot(d,d)<=this.m_radius*this.m_radius},initialize:function(def,body,localCenter){this.m_R=new b2Mat22,this.m_position=new b2Vec2,this.m_userData=def.userData,this.m_friction=def.friction,this.m_restitution=def.restitution,this.m_body=body,this.m_proxyId=b2Pair.b2_nullProxy,this.m_maxRadius=0,this.m_categoryBits=def.categoryBits,this.m_maskBits=def.maskBits,this.m_groupIndex=def.groupIndex,this.m_localPosition=new b2Vec2;var circle=def;this.m_localPosition.Set(def.localPosition.x-localCenter.x,def.localPosition.y-localCenter.y),this.m_type=b2Shape.e_circleShape,this.m_radius=circle.radius,this.m_R.SetM(this.m_body.m_R);var rX=this.m_R.col1.x*this.m_localPosition.x+this.m_R.col2.x*this.m_localPosition.y,rY=this.m_R.col1.y*this.m_localPosition.x+this.m_R.col2.y*this.m_localPosition.y;this.m_position.x=this.m_body.m_position.x+rX,this.m_position.y=this.m_body.m_position.y+rY,this.m_maxRadius=Math.sqrt(rX*rX+rY*rY)+this.m_radius;var aabb=new b2AABB;aabb.minVertex.Set(this.m_position.x-this.m_radius,this.m_position.y-this.m_radius),aabb.maxVertex.Set(this.m_position.x+this.m_radius,this.m_position.y+this.m_radius);var broadPhase=this.m_body.m_world.m_broadPhase;broadPhase.InRange(aabb)?this.m_proxyId=broadPhase.CreateProxy(aabb,this):this.m_proxyId=b2Pair.b2_nullProxy,this.m_proxyId==b2Pair.b2_nullProxy&&this.m_body.Freeze()},Synchronize:function(position1,R1,position2,R2){if(this.m_R.SetM(R2),this.m_position.x=R2.col1.x*this.m_localPosition.x+R2.col2.x*this.m_localPosition.y+position2.x,this.m_position.y=R2.col1.y*this.m_localPosition.x+R2.col2.y*this.m_localPosition.y+position2.y,this.m_proxyId!=b2Pair.b2_nullProxy){var p1X=position1.x+(R1.col1.x*this.m_localPosition.x+R1.col2.x*this.m_localPosition.y),p1Y=position1.y+(R1.col1.y*this.m_localPosition.x+R1.col2.y*this.m_localPosition.y),lowerX=Math.min(p1X,this.m_position.x),lowerY=Math.min(p1Y,this.m_position.y),upperX=Math.max(p1X,this.m_position.x),upperY=Math.max(p1Y,this.m_position.y),aabb=new b2AABB;aabb.minVertex.Set(lowerX-this.m_radius,lowerY-this.m_radius),aabb.maxVertex.Set(upperX+this.m_radius,upperY+this.m_radius);var broadPhase=this.m_body.m_world.m_broadPhase;broadPhase.InRange(aabb)?broadPhase.MoveProxy(this.m_proxyId,aabb):this.m_body.Freeze()}},QuickSync:function(position,R){this.m_R.SetM(R),this.m_position.x=R.col1.x*this.m_localPosition.x+R.col2.x*this.m_localPosition.y+position.x,this.m_position.y=R.col1.y*this.m_localPosition.x+R.col2.y*this.m_localPosition.y+position.y},ResetProxy:function(broadPhase){if(this.m_proxyId!=b2Pair.b2_nullProxy){broadPhase.GetProxy(this.m_proxyId);broadPhase.DestroyProxy(this.m_proxyId),null;var aabb=new b2AABB;aabb.minVertex.Set(this.m_position.x-this.m_radius,this.m_position.y-this.m_radius),aabb.maxVertex.Set(this.m_position.x+this.m_radius,this.m_position.y+this.m_radius),broadPhase.InRange(aabb)?this.m_proxyId=broadPhase.CreateProxy(aabb,this):this.m_proxyId=b2Pair.b2_nullProxy,this.m_proxyId==b2Pair.b2_nullProxy&&this.m_body.Freeze()}},Support:function(dX,dY,out){var len=Math.sqrt(dX*dX+dY*dY);dX/=len,dY/=len,out.Set(this.m_position.x+this.m_radius*dX,this.m_position.y+this.m_radius*dY)},m_localPosition:new b2Vec2,m_radius:null});var b2MassData=Class.create();b2MassData.prototype={mass:0,center:new b2Vec2(0,0),I:0,initialize:function(){this.center=new b2Vec2(0,0)}};var b2PolyDef=Class.create();Object.extend(b2PolyDef.prototype,b2ShapeDef.prototype),Object.extend(b2PolyDef.prototype,{initialize:function(){this.type=b2Shape.e_unknownShape,this.userData=null,this.localPosition=new b2Vec2(0,0),this.localRotation=0,this.friction=.2,this.restitution=0,this.density=0,this.categoryBits=1,this.maskBits=65535,this.groupIndex=0,this.vertices=new Array(b2Settings.b2_maxPolyVertices),this.type=b2Shape.e_polyShape,this.vertexCount=0;for(var i=0;i<b2Settings.b2_maxPolyVertices;i++)this.vertices[i]=new b2Vec2},vertices:new Array(b2Settings.b2_maxPolyVertices),vertexCount:0});var b2PolyShape=Class.create();Object.extend(b2PolyShape.prototype,b2Shape.prototype),Object.extend(b2PolyShape.prototype,{TestPoint:function(p){var pLocal=new b2Vec2;pLocal.SetV(p),pLocal.Subtract(this.m_position),pLocal.MulTM(this.m_R);for(var i=0;i<this.m_vertexCount;++i){var tVec=new b2Vec2;if(tVec.SetV(pLocal),tVec.Subtract(this.m_vertices[i]),b2Math.b2Dot(this.m_normals[i],tVec)>0)return!1}return!0},initialize:function(def,body,newOrigin){this.m_R=new b2Mat22,this.m_position=new b2Vec2,this.m_userData=def.userData,this.m_friction=def.friction,this.m_restitution=def.restitution,this.m_body=body,this.m_proxyId=b2Pair.b2_nullProxy,this.m_maxRadius=0,this.m_categoryBits=def.categoryBits,this.m_maskBits=def.maskBits,this.m_groupIndex=def.groupIndex,this.syncAABB=new b2AABB,this.syncMat=new b2Mat22,this.m_localCentroid=new b2Vec2,this.m_localOBB=new b2OBB;var hX,hY,tVec,i=0,aabb=new b2AABB;this.m_vertices=new Array(b2Settings.b2_maxPolyVertices),this.m_coreVertices=new Array(b2Settings.b2_maxPolyVertices),this.m_normals=new Array(b2Settings.b2_maxPolyVertices),this.m_type=b2Shape.e_polyShape;var localR=new b2Mat22(def.localRotation);if(def.type==b2Shape.e_boxShape){this.m_localCentroid.x=def.localPosition.x-newOrigin.x,this.m_localCentroid.y=def.localPosition.y-newOrigin.y;var box=def;this.m_vertexCount=4,hX=box.extents.x,hY=box.extents.y;var hcX=Math.max(0,hX-2*b2Settings.b2_linearSlop),hcY=Math.max(0,hY-2*b2Settings.b2_linearSlop);(tVec=this.m_vertices[0]=new b2Vec2).x=localR.col1.x*hX+localR.col2.x*hY,tVec.y=localR.col1.y*hX+localR.col2.y*hY,(tVec=this.m_vertices[1]=new b2Vec2).x=localR.col1.x*-hX+localR.col2.x*hY,tVec.y=localR.col1.y*-hX+localR.col2.y*hY,(tVec=this.m_vertices[2]=new b2Vec2).x=localR.col1.x*-hX+localR.col2.x*-hY,tVec.y=localR.col1.y*-hX+localR.col2.y*-hY,(tVec=this.m_vertices[3]=new b2Vec2).x=localR.col1.x*hX+localR.col2.x*-hY,tVec.y=localR.col1.y*hX+localR.col2.y*-hY,(tVec=this.m_coreVertices[0]=new b2Vec2).x=localR.col1.x*hcX+localR.col2.x*hcY,tVec.y=localR.col1.y*hcX+localR.col2.y*hcY,(tVec=this.m_coreVertices[1]=new b2Vec2).x=localR.col1.x*-hcX+localR.col2.x*hcY,tVec.y=localR.col1.y*-hcX+localR.col2.y*hcY,(tVec=this.m_coreVertices[2]=new b2Vec2).x=localR.col1.x*-hcX+localR.col2.x*-hcY,tVec.y=localR.col1.y*-hcX+localR.col2.y*-hcY,(tVec=this.m_coreVertices[3]=new b2Vec2).x=localR.col1.x*hcX+localR.col2.x*-hcY,tVec.y=localR.col1.y*hcX+localR.col2.y*-hcY}else{var poly=def;this.m_vertexCount=poly.vertexCount,b2Shape.PolyCentroid(poly.vertices,poly.vertexCount,b2PolyShape.tempVec);var centroidX=b2PolyShape.tempVec.x,centroidY=b2PolyShape.tempVec.y;for(this.m_localCentroid.x=def.localPosition.x+(localR.col1.x*centroidX+localR.col2.x*centroidY)-newOrigin.x,this.m_localCentroid.y=def.localPosition.y+(localR.col1.y*centroidX+localR.col2.y*centroidY)-newOrigin.y,i=0;i<this.m_vertexCount;++i){this.m_vertices[i]=new b2Vec2,this.m_coreVertices[i]=new b2Vec2,hX=poly.vertices[i].x-centroidX,hY=poly.vertices[i].y-centroidY,this.m_vertices[i].x=localR.col1.x*hX+localR.col2.x*hY,this.m_vertices[i].y=localR.col1.y*hX+localR.col2.y*hY;var uX=this.m_vertices[i].x,uY=this.m_vertices[i].y,length=Math.sqrt(uX*uX+uY*uY);length>Number.MIN_VALUE&&(uX*=1/length,uY*=1/length),this.m_coreVertices[i].x=this.m_vertices[i].x-2*b2Settings.b2_linearSlop*uX,this.m_coreVertices[i].y=this.m_vertices[i].y-2*b2Settings.b2_linearSlop*uY}}var minVertexX=Number.MAX_VALUE,minVertexY=Number.MAX_VALUE,maxVertexX=-Number.MAX_VALUE,maxVertexY=-Number.MAX_VALUE;for(this.m_maxRadius=0,i=0;i<this.m_vertexCount;++i){var v=this.m_vertices[i];minVertexX=Math.min(minVertexX,v.x),minVertexY=Math.min(minVertexY,v.y),maxVertexX=Math.max(maxVertexX,v.x),maxVertexY=Math.max(maxVertexY,v.y),this.m_maxRadius=Math.max(this.m_maxRadius,v.Length())}this.m_localOBB.R.SetIdentity(),this.m_localOBB.center.Set(.5*(minVertexX+maxVertexX),.5*(minVertexY+maxVertexY)),this.m_localOBB.extents.Set(.5*(maxVertexX-minVertexX),.5*(maxVertexY-minVertexY));var i1=0,i2=0;for(i=0;i<this.m_vertexCount;++i)this.m_normals[i]=new b2Vec2,i1=i,i2=i+1<this.m_vertexCount?i+1:0,this.m_normals[i].x=this.m_vertices[i2].y-this.m_vertices[i1].y,this.m_normals[i].y=-(this.m_vertices[i2].x-this.m_vertices[i1].x),this.m_normals[i].Normalize();for(i=0;i<this.m_vertexCount;++i)i1=i,i2=i+1<this.m_vertexCount?i+1:0;this.m_R.SetM(this.m_body.m_R),this.m_position.x=this.m_body.m_position.x+(this.m_R.col1.x*this.m_localCentroid.x+this.m_R.col2.x*this.m_localCentroid.y),this.m_position.y=this.m_body.m_position.y+(this.m_R.col1.y*this.m_localCentroid.x+this.m_R.col2.y*this.m_localCentroid.y),b2PolyShape.tAbsR.col1.x=this.m_R.col1.x*this.m_localOBB.R.col1.x+this.m_R.col2.x*this.m_localOBB.R.col1.y,b2PolyShape.tAbsR.col1.y=this.m_R.col1.y*this.m_localOBB.R.col1.x+this.m_R.col2.y*this.m_localOBB.R.col1.y,b2PolyShape.tAbsR.col2.x=this.m_R.col1.x*this.m_localOBB.R.col2.x+this.m_R.col2.x*this.m_localOBB.R.col2.y,b2PolyShape.tAbsR.col2.y=this.m_R.col1.y*this.m_localOBB.R.col2.x+this.m_R.col2.y*this.m_localOBB.R.col2.y,b2PolyShape.tAbsR.Abs(),hX=b2PolyShape.tAbsR.col1.x*this.m_localOBB.extents.x+b2PolyShape.tAbsR.col2.x*this.m_localOBB.extents.y,hY=b2PolyShape.tAbsR.col1.y*this.m_localOBB.extents.x+b2PolyShape.tAbsR.col2.y*this.m_localOBB.extents.y;var positionX=this.m_position.x+(this.m_R.col1.x*this.m_localOBB.center.x+this.m_R.col2.x*this.m_localOBB.center.y),positionY=this.m_position.y+(this.m_R.col1.y*this.m_localOBB.center.x+this.m_R.col2.y*this.m_localOBB.center.y);aabb.minVertex.x=positionX-hX,aabb.minVertex.y=positionY-hY,aabb.maxVertex.x=positionX+hX,aabb.maxVertex.y=positionY+hY;var broadPhase=this.m_body.m_world.m_broadPhase;broadPhase.InRange(aabb)?this.m_proxyId=broadPhase.CreateProxy(aabb,this):this.m_proxyId=b2Pair.b2_nullProxy,this.m_proxyId==b2Pair.b2_nullProxy&&this.m_body.Freeze()},syncAABB:new b2AABB,syncMat:new b2Mat22,Synchronize:function(position1,R1,position2,R2){if(this.m_R.SetM(R2),this.m_position.x=this.m_body.m_position.x+(R2.col1.x*this.m_localCentroid.x+R2.col2.x*this.m_localCentroid.y),this.m_position.y=this.m_body.m_position.y+(R2.col1.y*this.m_localCentroid.x+R2.col2.y*this.m_localCentroid.y),this.m_proxyId!=b2Pair.b2_nullProxy){var hX,hY,v1=R1.col1,v2=R1.col2,v3=this.m_localOBB.R.col1,v4=this.m_localOBB.R.col2;this.syncMat.col1.x=v1.x*v3.x+v2.x*v3.y,this.syncMat.col1.y=v1.y*v3.x+v2.y*v3.y,this.syncMat.col2.x=v1.x*v4.x+v2.x*v4.y,this.syncMat.col2.y=v1.y*v4.x+v2.y*v4.y,this.syncMat.Abs(),hX=this.m_localCentroid.x+this.m_localOBB.center.x,hY=this.m_localCentroid.y+this.m_localOBB.center.y;var centerX=position1.x+(R1.col1.x*hX+R1.col2.x*hY),centerY=position1.y+(R1.col1.y*hX+R1.col2.y*hY);hX=this.syncMat.col1.x*this.m_localOBB.extents.x+this.syncMat.col2.x*this.m_localOBB.extents.y,hY=this.syncMat.col1.y*this.m_localOBB.extents.x+this.syncMat.col2.y*this.m_localOBB.extents.y,this.syncAABB.minVertex.x=centerX-hX,this.syncAABB.minVertex.y=centerY-hY,this.syncAABB.maxVertex.x=centerX+hX,this.syncAABB.maxVertex.y=centerY+hY,v1=R2.col1,v2=R2.col2,v3=this.m_localOBB.R.col1,v4=this.m_localOBB.R.col2,this.syncMat.col1.x=v1.x*v3.x+v2.x*v3.y,this.syncMat.col1.y=v1.y*v3.x+v2.y*v3.y,this.syncMat.col2.x=v1.x*v4.x+v2.x*v4.y,this.syncMat.col2.y=v1.y*v4.x+v2.y*v4.y,this.syncMat.Abs(),hX=this.m_localCentroid.x+this.m_localOBB.center.x,hY=this.m_localCentroid.y+this.m_localOBB.center.y,centerX=position2.x+(R2.col1.x*hX+R2.col2.x*hY),centerY=position2.y+(R2.col1.y*hX+R2.col2.y*hY),hX=this.syncMat.col1.x*this.m_localOBB.extents.x+this.syncMat.col2.x*this.m_localOBB.extents.y,hY=this.syncMat.col1.y*this.m_localOBB.extents.x+this.syncMat.col2.y*this.m_localOBB.extents.y,this.syncAABB.minVertex.x=Math.min(this.syncAABB.minVertex.x,centerX-hX),this.syncAABB.minVertex.y=Math.min(this.syncAABB.minVertex.y,centerY-hY),this.syncAABB.maxVertex.x=Math.max(this.syncAABB.maxVertex.x,centerX+hX),this.syncAABB.maxVertex.y=Math.max(this.syncAABB.maxVertex.y,centerY+hY);var broadPhase=this.m_body.m_world.m_broadPhase;broadPhase.InRange(this.syncAABB)?broadPhase.MoveProxy(this.m_proxyId,this.syncAABB):this.m_body.Freeze()}},QuickSync:function(position,R){this.m_R.SetM(R),this.m_position.x=position.x+(R.col1.x*this.m_localCentroid.x+R.col2.x*this.m_localCentroid.y),this.m_position.y=position.y+(R.col1.y*this.m_localCentroid.x+R.col2.y*this.m_localCentroid.y)},ResetProxy:function(broadPhase){if(this.m_proxyId!=b2Pair.b2_nullProxy){broadPhase.GetProxy(this.m_proxyId);broadPhase.DestroyProxy(this.m_proxyId),null;var R=b2Math.b2MulMM(this.m_R,this.m_localOBB.R),absR=b2Math.b2AbsM(R),h=b2Math.b2MulMV(absR,this.m_localOBB.extents),position=b2Math.b2MulMV(this.m_R,this.m_localOBB.center);position.Add(this.m_position);var aabb=new b2AABB;aabb.minVertex.SetV(position),aabb.minVertex.Subtract(h),aabb.maxVertex.SetV(position),aabb.maxVertex.Add(h),broadPhase.InRange(aabb)?this.m_proxyId=broadPhase.CreateProxy(aabb,this):this.m_proxyId=b2Pair.b2_nullProxy,this.m_proxyId==b2Pair.b2_nullProxy&&this.m_body.Freeze()}},Support:function(dX,dY,out){for(var dLocalX=dX*this.m_R.col1.x+dY*this.m_R.col1.y,dLocalY=dX*this.m_R.col2.x+dY*this.m_R.col2.y,bestIndex=0,bestValue=this.m_coreVertices[0].x*dLocalX+this.m_coreVertices[0].y*dLocalY,i=1;i<this.m_vertexCount;++i){var value=this.m_coreVertices[i].x*dLocalX+this.m_coreVertices[i].y*dLocalY;value>bestValue&&(bestIndex=i,bestValue=value)}out.Set(this.m_position.x+(this.m_R.col1.x*this.m_coreVertices[bestIndex].x+this.m_R.col2.x*this.m_coreVertices[bestIndex].y),this.m_position.y+(this.m_R.col1.y*this.m_coreVertices[bestIndex].x+this.m_R.col2.y*this.m_coreVertices[bestIndex].y))},m_localCentroid:new b2Vec2,m_localOBB:new b2OBB,m_vertices:null,m_coreVertices:null,m_vertexCount:0,m_normals:null}),b2PolyShape.tempVec=new b2Vec2,b2PolyShape.tAbsR=new b2Mat22;var b2Body=Class.create();b2Body.prototype={SetOriginPosition:function(position,rotation){if(!this.IsFrozen()){this.m_rotation=rotation,this.m_R.Set(this.m_rotation),this.m_position=b2Math.AddVV(position,b2Math.b2MulMV(this.m_R,this.m_center)),this.m_position0.SetV(this.m_position),this.m_rotation0=this.m_rotation;for(var s=this.m_shapeList;null!=s;s=s.m_next)s.Synchronize(this.m_position,this.m_R,this.m_position,this.m_R);this.m_world.m_broadPhase.Commit()}},GetOriginPosition:function(){return b2Math.SubtractVV(this.m_position,b2Math.b2MulMV(this.m_R,this.m_center))},SetCenterPosition:function(position,rotation){if(!this.IsFrozen()){this.m_rotation=rotation,this.m_R.Set(this.m_rotation),this.m_position.SetV(position),this.m_position0.SetV(this.m_position),this.m_rotation0=this.m_rotation;for(var s=this.m_shapeList;null!=s;s=s.m_next)s.Synchronize(this.m_position,this.m_R,this.m_position,this.m_R);this.m_world.m_broadPhase.Commit()}},GetCenterPosition:function(){return this.m_position},GetRotation:function(){return this.m_rotation},GetRotationMatrix:function(){return this.m_R},SetLinearVelocity:function(v){this.m_linearVelocity.SetV(v)},GetLinearVelocity:function(){return this.m_linearVelocity},SetAngularVelocity:function(w){this.m_angularVelocity=w},GetAngularVelocity:function(){return this.m_angularVelocity},ApplyForce:function(force,point){0==this.IsSleeping()&&(this.m_force.Add(force),this.m_torque+=b2Math.b2CrossVV(b2Math.SubtractVV(point,this.m_position),force))},ApplyTorque:function(torque){0==this.IsSleeping()&&(this.m_torque+=torque)},ApplyImpulse:function(impulse,point){0==this.IsSleeping()&&(this.m_linearVelocity.Add(b2Math.MulFV(this.m_invMass,impulse)),this.m_angularVelocity+=this.m_invI*b2Math.b2CrossVV(b2Math.SubtractVV(point,this.m_position),impulse))},GetMass:function(){return this.m_mass},GetInertia:function(){return this.m_I},GetWorldPoint:function(localPoint){return b2Math.AddVV(this.m_position,b2Math.b2MulMV(this.m_R,localPoint))},GetWorldVector:function(localVector){return b2Math.b2MulMV(this.m_R,localVector)},GetLocalPoint:function(worldPoint){return b2Math.b2MulTMV(this.m_R,b2Math.SubtractVV(worldPoint,this.m_position))},GetLocalVector:function(worldVector){return b2Math.b2MulTMV(this.m_R,worldVector)},IsStatic:function(){return(this.m_flags&b2Body.e_staticFlag)==b2Body.e_staticFlag},IsFrozen:function(){return(this.m_flags&b2Body.e_frozenFlag)==b2Body.e_frozenFlag},IsSleeping:function(){return(this.m_flags&b2Body.e_sleepFlag)==b2Body.e_sleepFlag},AllowSleeping:function(flag){flag?this.m_flags|=b2Body.e_allowSleepFlag:(this.m_flags&=~b2Body.e_allowSleepFlag,this.WakeUp())},WakeUp:function(){this.m_flags&=~b2Body.e_sleepFlag,this.m_sleepTime=0},GetShapeList:function(){return this.m_shapeList},GetContactList:function(){return this.m_contactList},GetJointList:function(){return this.m_jointList},GetNext:function(){return this.m_next},GetUserData:function(){return this.m_userData},initialize:function(bd,world){this.sMat0=new b2Mat22,this.m_position=new b2Vec2,this.m_R=new b2Mat22(0),this.m_position0=new b2Vec2;var sd,massData,i=0;this.m_flags=0,this.m_position.SetV(bd.position),this.m_rotation=bd.rotation,this.m_R.Set(this.m_rotation),this.m_position0.SetV(this.m_position),this.m_rotation0=this.m_rotation,this.m_world=world,this.m_linearDamping=b2Math.b2Clamp(1-bd.linearDamping,0,1),this.m_angularDamping=b2Math.b2Clamp(1-bd.angularDamping,0,1),this.m_force=new b2Vec2(0,0),this.m_torque=0,this.m_mass=0;var massDatas=new Array(b2Settings.b2_maxShapesPerBody);for(i=0;i<b2Settings.b2_maxShapesPerBody;i++)massDatas[i]=new b2MassData;for(this.m_shapeCount=0,this.m_center=new b2Vec2(0,0),i=0;i<b2Settings.b2_maxShapesPerBody&&null!=(sd=bd.shapes[i]);++i)massData=massDatas[i],sd.ComputeMass(massData),this.m_mass+=massData.mass,this.m_center.x+=massData.mass*(sd.localPosition.x+massData.center.x),this.m_center.y+=massData.mass*(sd.localPosition.y+massData.center.y),++this.m_shapeCount;for(this.m_mass>0?(this.m_center.Multiply(1/this.m_mass),this.m_position.Add(b2Math.b2MulMV(this.m_R,this.m_center))):this.m_flags|=b2Body.e_staticFlag,this.m_I=0,i=0;i<this.m_shapeCount;++i){sd=bd.shapes[i],massData=massDatas[i],this.m_I+=massData.I;var r=b2Math.SubtractVV(b2Math.AddVV(sd.localPosition,massData.center),this.m_center);this.m_I+=massData.mass*b2Math.b2Dot(r,r)}for(this.m_mass>0?this.m_invMass=1/this.m_mass:this.m_invMass=0,this.m_I>0&&0==bd.preventRotation?this.m_invI=1/this.m_I:(this.m_I=0,this.m_invI=0),this.m_linearVelocity=b2Math.AddVV(bd.linearVelocity,b2Math.b2CrossFV(bd.angularVelocity,this.m_center)),this.m_angularVelocity=bd.angularVelocity,this.m_jointList=null,this.m_contactList=null,this.m_prev=null,this.m_next=null,this.m_shapeList=null,i=0;i<this.m_shapeCount;++i){sd=bd.shapes[i];var shape=b2Shape.Create(sd,this,this.m_center);shape.m_next=this.m_shapeList,this.m_shapeList=shape}this.m_sleepTime=0,bd.allowSleep&&(this.m_flags|=b2Body.e_allowSleepFlag),bd.isSleeping&&(this.m_flags|=b2Body.e_sleepFlag),(this.m_flags&b2Body.e_sleepFlag||0==this.m_invMass)&&(this.m_linearVelocity.Set(0,0),this.m_angularVelocity=0),this.m_userData=bd.userData},Destroy:function(){for(var s=this.m_shapeList;s;){var s0=s;s=s.m_next,b2Shape.Destroy(s0)}},sMat0:new b2Mat22,SynchronizeShapes:function(){this.sMat0.Set(this.m_rotation0);for(var s=this.m_shapeList;null!=s;s=s.m_next)s.Synchronize(this.m_position0,this.sMat0,this.m_position,this.m_R)},QuickSyncShapes:function(){for(var s=this.m_shapeList;null!=s;s=s.m_next)s.QuickSync(this.m_position,this.m_R)},IsConnected:function(other){for(var jn=this.m_jointList;null!=jn;jn=jn.next)if(jn.other==other)return 0==jn.joint.m_collideConnected;return!1},Freeze:function(){this.m_flags|=b2Body.e_frozenFlag,this.m_linearVelocity.SetZero(),this.m_angularVelocity=0;for(var s=this.m_shapeList;null!=s;s=s.m_next)s.DestroyProxy()},m_flags:0,m_position:new b2Vec2,m_rotation:null,m_R:new b2Mat22(0),m_position0:new b2Vec2,m_rotation0:null,m_linearVelocity:null,m_angularVelocity:null,m_force:null,m_torque:null,m_center:null,m_world:null,m_prev:null,m_next:null,m_shapeList:null,m_shapeCount:0,m_jointList:null,m_contactList:null,m_mass:null,m_invMass:null,m_I:null,m_invI:null,m_linearDamping:null,m_angularDamping:null,m_sleepTime:null,m_userData:null},b2Body.e_staticFlag=1,b2Body.e_frozenFlag=2,b2Body.e_islandFlag=4,b2Body.e_sleepFlag=8,b2Body.e_allowSleepFlag=16,b2Body.e_destroyFlag=32;var b2BodyDef=Class.create();b2BodyDef.prototype={initialize:function(){this.shapes=new Array,this.userData=null;for(var i=0;i<b2Settings.b2_maxShapesPerBody;i++)this.shapes[i]=null;this.position=new b2Vec2(0,0),this.rotation=0,this.linearVelocity=new b2Vec2(0,0),this.angularVelocity=0,this.linearDamping=0,this.angularDamping=0,this.allowSleep=!0,this.isSleeping=!1,this.preventRotation=!1},userData:null,shapes:new Array,position:null,rotation:null,linearVelocity:null,angularVelocity:null,linearDamping:null,angularDamping:null,allowSleep:null,isSleeping:null,preventRotation:null,AddShape:function(shape){for(var i=0;i<b2Settings.b2_maxShapesPerBody;++i)if(null==this.shapes[i]){this.shapes[i]=shape;break}}};var b2CollisionFilter=Class.create();b2CollisionFilter.prototype={ShouldCollide:function(shape1,shape2){return shape1.m_groupIndex==shape2.m_groupIndex&&0!=shape1.m_groupIndex?shape1.m_groupIndex>0:0!=(shape1.m_maskBits&shape2.m_categoryBits)&&0!=(shape1.m_categoryBits&shape2.m_maskBits)},initialize:function(){}},b2CollisionFilter.b2_defaultFilter=new b2CollisionFilter;var b2Island=Class.create();b2Island.prototype={initialize:function(bodyCapacity,contactCapacity,jointCapacity,allocator){var i=0;for(this.m_bodyCapacity=bodyCapacity,this.m_contactCapacity=contactCapacity,this.m_jointCapacity=jointCapacity,this.m_bodyCount=0,this.m_contactCount=0,this.m_jointCount=0,this.m_bodies=new Array(bodyCapacity),i=0;i<bodyCapacity;i++)this.m_bodies[i]=null;for(this.m_contacts=new Array(contactCapacity),i=0;i<contactCapacity;i++)this.m_contacts[i]=null;for(this.m_joints=new Array(jointCapacity),i=0;i<jointCapacity;i++)this.m_joints[i]=null;this.m_allocator=allocator},Clear:function(){this.m_bodyCount=0,this.m_contactCount=0,this.m_jointCount=0},Solve:function(step,gravity){var b,i=0;for(i=0;i<this.m_bodyCount;++i)0!=(b=this.m_bodies[i]).m_invMass&&(b.m_linearVelocity.Add(b2Math.MulFV(step.dt,b2Math.AddVV(gravity,b2Math.MulFV(b.m_invMass,b.m_force)))),b.m_angularVelocity+=step.dt*b.m_invI*b.m_torque,b.m_linearVelocity.Multiply(b.m_linearDamping),b.m_angularVelocity*=b.m_angularDamping,b.m_position0.SetV(b.m_position),b.m_rotation0=b.m_rotation);var contactSolver=new b2ContactSolver(this.m_contacts,this.m_contactCount,this.m_allocator);for(contactSolver.PreSolve(),i=0;i<this.m_jointCount;++i)this.m_joints[i].PrepareVelocitySolver();for(i=0;i<step.iterations;++i){contactSolver.SolveVelocityConstraints();for(var j=0;j<this.m_jointCount;++j)this.m_joints[j].SolveVelocityConstraints(step)}for(i=0;i<this.m_bodyCount;++i)0!=(b=this.m_bodies[i]).m_invMass&&(b.m_position.x+=step.dt*b.m_linearVelocity.x,b.m_position.y+=step.dt*b.m_linearVelocity.y,b.m_rotation+=step.dt*b.m_angularVelocity,b.m_R.Set(b.m_rotation));for(i=0;i<this.m_jointCount;++i)this.m_joints[i].PreparePositionSolver();if(b2World.s_enablePositionCorrection)for(b2Island.m_positionIterationCount=0;b2Island.m_positionIterationCount<step.iterations;++b2Island.m_positionIterationCount){var contactsOkay=contactSolver.SolvePositionConstraints(b2Settings.b2_contactBaumgarte),jointsOkay=!0;for(i=0;i<this.m_jointCount;++i){var jointOkay=this.m_joints[i].SolvePositionConstraints();jointsOkay=jointsOkay&&jointOkay}if(contactsOkay&&jointsOkay)break}for(contactSolver.PostSolve(),i=0;i<this.m_bodyCount;++i)0!=(b=this.m_bodies[i]).m_invMass&&(b.m_R.Set(b.m_rotation),b.SynchronizeShapes(),b.m_force.Set(0,0),b.m_torque=0)},UpdateSleep:function(dt){var b,i=0,minSleepTime=Number.MAX_VALUE,linTolSqr=b2Settings.b2_linearSleepTolerance*b2Settings.b2_linearSleepTolerance,angTolSqr=b2Settings.b2_angularSleepTolerance*b2Settings.b2_angularSleepTolerance;for(i=0;i<this.m_bodyCount;++i)0!=(b=this.m_bodies[i]).m_invMass&&(0==(b.m_flags&b2Body.e_allowSleepFlag)&&(b.m_sleepTime=0,minSleepTime=0),0==(b.m_flags&b2Body.e_allowSleepFlag)||b.m_angularVelocity*b.m_angularVelocity>angTolSqr||b2Math.b2Dot(b.m_linearVelocity,b.m_linearVelocity)>linTolSqr?(b.m_sleepTime=0,minSleepTime=0):(b.m_sleepTime+=dt,minSleepTime=b2Math.b2Min(minSleepTime,b.m_sleepTime)));if(minSleepTime>=b2Settings.b2_timeToSleep)for(i=0;i<this.m_bodyCount;++i)(b=this.m_bodies[i]).m_flags|=b2Body.e_sleepFlag},AddBody:function(body){this.m_bodies[this.m_bodyCount++]=body},AddContact:function(contact){this.m_contacts[this.m_contactCount++]=contact},AddJoint:function(joint){this.m_joints[this.m_jointCount++]=joint},m_allocator:null,m_bodies:null,m_contacts:null,m_joints:null,m_bodyCount:0,m_jointCount:0,m_contactCount:0,m_bodyCapacity:0,m_contactCapacity:0,m_jointCapacity:0,m_positionError:null},b2Island.m_positionIterationCount=0;var b2TimeStep=Class.create();b2TimeStep.prototype={dt:null,inv_dt:null,iterations:0,initialize:function(){}};var b2ContactNode=Class.create();b2ContactNode.prototype={other:null,contact:null,prev:null,next:null,initialize:function(){}};var b2Contact=Class.create();b2Contact.prototype={GetManifolds:function(){return null},GetManifoldCount:function(){return this.m_manifoldCount},GetNext:function(){return this.m_next},GetShape1:function(){return this.m_shape1},GetShape2:function(){return this.m_shape2},initialize:function(s1,s2){if(this.m_node1=new b2ContactNode,this.m_node2=new b2ContactNode,this.m_flags=0,!s1||!s2)return this.m_shape1=null,void(this.m_shape2=null);this.m_shape1=s1,this.m_shape2=s2,this.m_manifoldCount=0,this.m_friction=Math.sqrt(this.m_shape1.m_friction*this.m_shape2.m_friction),this.m_restitution=b2Math.b2Max(this.m_shape1.m_restitution,this.m_shape2.m_restitution),this.m_prev=null,this.m_next=null,this.m_node1.contact=null,this.m_node1.prev=null,this.m_node1.next=null,this.m_node1.other=null,this.m_node2.contact=null,this.m_node2.prev=null,this.m_node2.next=null,this.m_node2.other=null},Evaluate:function(){},m_flags:0,m_prev:null,m_next:null,m_node1:new b2ContactNode,m_node2:new b2ContactNode,m_shape1:null,m_shape2:null,m_manifoldCount:0,m_friction:null,m_restitution:null},b2Contact.e_islandFlag=1,b2Contact.e_destroyFlag=2,b2Contact.AddType=function(createFcn,destroyFcn,type1,type2){b2Contact.s_registers[type1][type2].createFcn=createFcn,b2Contact.s_registers[type1][type2].destroyFcn=destroyFcn,b2Contact.s_registers[type1][type2].primary=!0,type1!=type2&&(b2Contact.s_registers[type2][type1].createFcn=createFcn,b2Contact.s_registers[type2][type1].destroyFcn=destroyFcn,b2Contact.s_registers[type2][type1].primary=!1)},b2Contact.InitializeRegisters=function(){b2Contact.s_registers=new Array(b2Shape.e_shapeTypeCount);for(var i=0;i<b2Shape.e_shapeTypeCount;i++){b2Contact.s_registers[i]=new Array(b2Shape.e_shapeTypeCount);for(var j=0;j<b2Shape.e_shapeTypeCount;j++)b2Contact.s_registers[i][j]=new b2ContactRegister}b2Contact.AddType(b2CircleContact.Create,b2CircleContact.Destroy,b2Shape.e_circleShape,b2Shape.e_circleShape),b2Contact.AddType(b2PolyAndCircleContact.Create,b2PolyAndCircleContact.Destroy,b2Shape.e_polyShape,b2Shape.e_circleShape),b2Contact.AddType(b2PolyContact.Create,b2PolyContact.Destroy,b2Shape.e_polyShape,b2Shape.e_polyShape)},b2Contact.Create=function(shape1,shape2,allocator){0==b2Contact.s_initialized&&(b2Contact.InitializeRegisters(),b2Contact.s_initialized=!0);var type1=shape1.m_type,type2=shape2.m_type,createFcn=b2Contact.s_registers[type1][type2].createFcn;if(createFcn){if(b2Contact.s_registers[type1][type2].primary)return createFcn(shape1,shape2,allocator);for(var c=createFcn(shape2,shape1,allocator),i=0;i<c.GetManifoldCount();++i){var m=c.GetManifolds()[i];m.normal=m.normal.Negative()}return c}return null},b2Contact.Destroy=function(contact,allocator){contact.GetManifoldCount()>0&&(contact.m_shape1.m_body.WakeUp(),contact.m_shape2.m_body.WakeUp());var type1=contact.m_shape1.m_type,type2=contact.m_shape2.m_type;(0,b2Contact.s_registers[type1][type2].destroyFcn)(contact,allocator)},b2Contact.s_registers=null,b2Contact.s_initialized=!1;var b2ContactConstraint=Class.create();b2ContactConstraint.prototype={initialize:function(){this.normal=new b2Vec2,this.points=new Array(b2Settings.b2_maxManifoldPoints);for(var i=0;i<b2Settings.b2_maxManifoldPoints;i++)this.points[i]=new b2ContactConstraintPoint},points:null,normal:new b2Vec2,manifold:null,body1:null,body2:null,friction:null,restitution:null,pointCount:0};var b2ContactConstraintPoint=Class.create();b2ContactConstraintPoint.prototype={localAnchor1:new b2Vec2,localAnchor2:new b2Vec2,normalImpulse:null,tangentImpulse:null,positionImpulse:null,normalMass:null,tangentMass:null,separation:null,velocityBias:null,initialize:function(){this.localAnchor1=new b2Vec2,this.localAnchor2=new b2Vec2}};var b2ContactRegister=Class.create();b2ContactRegister.prototype={createFcn:null,destroyFcn:null,primary:null,initialize:function(){}};var b2ContactSolver=Class.create();b2ContactSolver.prototype={initialize:function(contacts,contactCount,allocator){this.m_constraints=new Array,this.m_allocator=allocator;var tVec,tMat,i=0;for(this.m_constraintCount=0,i=0;i<contactCount;++i)this.m_constraintCount+=contacts[i].GetManifoldCount();for(i=0;i<this.m_constraintCount;i++)this.m_constraints[i]=new b2ContactConstraint;var count=0;for(i=0;i<contactCount;++i)for(var contact=contacts[i],b1=contact.m_shape1.m_body,b2=contact.m_shape2.m_body,manifoldCount=contact.GetManifoldCount(),manifolds=contact.GetManifolds(),friction=contact.m_friction,restitution=contact.m_restitution,v1X=b1.m_linearVelocity.x,v1Y=b1.m_linearVelocity.y,v2X=b2.m_linearVelocity.x,v2Y=b2.m_linearVelocity.y,w1=b1.m_angularVelocity,w2=b2.m_angularVelocity,j=0;j<manifoldCount;++j){var manifold=manifolds[j],normalX=manifold.normal.x,normalY=manifold.normal.y,c=this.m_constraints[count];c.body1=b1,c.body2=b2,c.manifold=manifold,c.normal.x=normalX,c.normal.y=normalY,c.pointCount=manifold.pointCount,c.friction=friction,c.restitution=restitution;for(var k=0;k<c.pointCount;++k){var cp=manifold.points[k],ccp=c.points[k];ccp.normalImpulse=cp.normalImpulse,ccp.tangentImpulse=cp.tangentImpulse,ccp.separation=cp.separation;var r1X=cp.position.x-b1.m_position.x,r1Y=cp.position.y-b1.m_position.y,r2X=cp.position.x-b2.m_position.x,r2Y=cp.position.y-b2.m_position.y;tVec=ccp.localAnchor1,tMat=b1.m_R,tVec.x=r1X*tMat.col1.x+r1Y*tMat.col1.y,tVec.y=r1X*tMat.col2.x+r1Y*tMat.col2.y,tVec=ccp.localAnchor2,tMat=b2.m_R,tVec.x=r2X*tMat.col1.x+r2Y*tMat.col1.y,tVec.y=r2X*tMat.col2.x+r2Y*tMat.col2.y;var r1Sqr=r1X*r1X+r1Y*r1Y,r2Sqr=r2X*r2X+r2Y*r2Y,rn1=r1X*normalX+r1Y*normalY,rn2=r2X*normalX+r2Y*normalY,kNormal=b1.m_invMass+b2.m_invMass;kNormal+=b1.m_invI*(r1Sqr-rn1*rn1)+b2.m_invI*(r2Sqr-rn2*rn2),ccp.normalMass=1/kNormal;var tangentY=-normalX,rt1=r1X*normalY+r1Y*tangentY,rt2=r2X*normalY+r2Y*tangentY,kTangent=b1.m_invMass+b2.m_invMass;kTangent+=b1.m_invI*(r1Sqr-rt1*rt1)+b2.m_invI*(r2Sqr-rt2*rt2),ccp.tangentMass=1/kTangent,ccp.velocityBias=0,ccp.separation>0&&(ccp.velocityBias=-60*ccp.separation);var tX=v2X+-w2*r2Y-v1X- -w1*r1Y,tY=v2Y+w2*r2X-v1Y-w1*r1X,vRel=c.normal.x*tX+c.normal.y*tY;vRel<-b2Settings.b2_velocityThreshold&&(ccp.velocityBias+=-c.restitution*vRel)}++count}},PreSolve:function(){for(var tVec,tMat,i=0;i<this.m_constraintCount;++i){var c=this.m_constraints[i],b1=c.body1,b2=c.body2,invMass1=b1.m_invMass,invI1=b1.m_invI,invMass2=b2.m_invMass,invI2=b2.m_invI,normalX=c.normal.x,normalY=c.normal.y,tangentX=normalY,tangentY=-normalX,j=0,tCount=0;if(b2World.s_enableWarmStarting)for(tCount=c.pointCount,j=0;j<tCount;++j){var ccp=c.points[j],PX=ccp.normalImpulse*normalX+ccp.tangentImpulse*tangentX,PY=ccp.normalImpulse*normalY+ccp.tangentImpulse*tangentY;tMat=b1.m_R,tVec=ccp.localAnchor1;var r1X=tMat.col1.x*tVec.x+tMat.col2.x*tVec.y,r1Y=tMat.col1.y*tVec.x+tMat.col2.y*tVec.y;tMat=b2.m_R,tVec=ccp.localAnchor2;var r2X=tMat.col1.x*tVec.x+tMat.col2.x*tVec.y,r2Y=tMat.col1.y*tVec.x+tMat.col2.y*tVec.y;b1.m_angularVelocity-=invI1*(r1X*PY-r1Y*PX),b1.m_linearVelocity.x-=invMass1*PX,b1.m_linearVelocity.y-=invMass1*PY,b2.m_angularVelocity+=invI2*(r2X*PY-r2Y*PX),b2.m_linearVelocity.x+=invMass2*PX,b2.m_linearVelocity.y+=invMass2*PY,ccp.positionImpulse=0}else for(tCount=c.pointCount,j=0;j<tCount;++j){var ccp2=c.points[j];ccp2.normalImpulse=0,ccp2.tangentImpulse=0,ccp2.positionImpulse=0}}},SolveVelocityConstraints:function(){for(var ccp,r1X,r1Y,r2X,r2Y,lambda,newImpulse,PX,PY,tMat,tVec,j=0,i=0;i<this.m_constraintCount;++i){var c=this.m_constraints[i],b1=c.body1,b2=c.body2,b1_angularVelocity=b1.m_angularVelocity,b1_linearVelocity=b1.m_linearVelocity,b2_angularVelocity=b2.m_angularVelocity,b2_linearVelocity=b2.m_linearVelocity,invMass1=b1.m_invMass,invI1=b1.m_invI,invMass2=b2.m_invMass,invI2=b2.m_invI,normalX=c.normal.x,normalY=c.normal.y,tangentX=normalY,tangentY=-normalX,tCount=c.pointCount;for(j=0;j<tCount;++j){ccp=c.points[j],tMat=b1.m_R,tVec=ccp.localAnchor1,r1X=tMat.col1.x*tVec.x+tMat.col2.x*tVec.y,r1Y=tMat.col1.y*tVec.x+tMat.col2.y*tVec.y,tMat=b2.m_R,tVec=ccp.localAnchor2,r2X=tMat.col1.x*tVec.x+tMat.col2.x*tVec.y,r2Y=tMat.col1.y*tVec.x+tMat.col2.y*tVec.y;var vn=(b2_linearVelocity.x+-b2_angularVelocity*r2Y-b1_linearVelocity.x- -b1_angularVelocity*r1Y)*normalX+(b2_linearVelocity.y+b2_angularVelocity*r2X-b1_linearVelocity.y-b1_angularVelocity*r1X)*normalY;lambda=-ccp.normalMass*(vn-ccp.velocityBias),PX=(lambda=(newImpulse=b2Math.b2Max(ccp.normalImpulse+lambda,0))-ccp.normalImpulse)*normalX,PY=lambda*normalY,b1_linearVelocity.x-=invMass1*PX,b1_linearVelocity.y-=invMass1*PY,b1_angularVelocity-=invI1*(r1X*PY-r1Y*PX),b2_linearVelocity.x+=invMass2*PX,b2_linearVelocity.y+=invMass2*PY,b2_angularVelocity+=invI2*(r2X*PY-r2Y*PX),ccp.normalImpulse=newImpulse;var vt=(b2_linearVelocity.x+-b2_angularVelocity*r2Y-b1_linearVelocity.x- -b1_angularVelocity*r1Y)*tangentX+(b2_linearVelocity.y+b2_angularVelocity*r2X-b1_linearVelocity.y-b1_angularVelocity*r1X)*tangentY;lambda=ccp.tangentMass*-vt;var maxFriction=c.friction*ccp.normalImpulse;PX=(lambda=(newImpulse=b2Math.b2Clamp(ccp.tangentImpulse+lambda,-maxFriction,maxFriction))-ccp.tangentImpulse)*tangentX,PY=lambda*tangentY,b1_linearVelocity.x-=invMass1*PX,b1_linearVelocity.y-=invMass1*PY,b1_angularVelocity-=invI1*(r1X*PY-r1Y*PX),b2_linearVelocity.x+=invMass2*PX,b2_linearVelocity.y+=invMass2*PY,b2_angularVelocity+=invI2*(r2X*PY-r2Y*PX),ccp.tangentImpulse=newImpulse}b1.m_angularVelocity=b1_angularVelocity,b2.m_angularVelocity=b2_angularVelocity}},SolvePositionConstraints:function(beta){for(var tMat,tVec,minSeparation=0,i=0;i<this.m_constraintCount;++i){for(var c=this.m_constraints[i],b1=c.body1,b2=c.body2,b1_position=b1.m_position,b1_rotation=b1.m_rotation,b2_position=b2.m_position,b2_rotation=b2.m_rotation,invMass1=b1.m_invMass,invI1=b1.m_invI,invMass2=b2.m_invMass,invI2=b2.m_invI,normalX=c.normal.x,normalY=c.normal.y,tCount=c.pointCount,j=0;j<tCount;++j){var ccp=c.points[j];tMat=b1.m_R,tVec=ccp.localAnchor1;var r1X=tMat.col1.x*tVec.x+tMat.col2.x*tVec.y,r1Y=tMat.col1.y*tVec.x+tMat.col2.y*tVec.y;tMat=b2.m_R,tVec=ccp.localAnchor2;var r2X=tMat.col1.x*tVec.x+tMat.col2.x*tVec.y,r2Y=tMat.col1.y*tVec.x+tMat.col2.y*tVec.y,p1X=b1_position.x+r1X,p1Y=b1_position.y+r1Y,separation=(b2_position.x+r2X-p1X)*normalX+(b2_position.y+r2Y-p1Y)*normalY+ccp.separation;minSeparation=b2Math.b2Min(minSeparation,separation);var C=beta*b2Math.b2Clamp(separation+b2Settings.b2_linearSlop,-b2Settings.b2_maxLinearCorrection,0),dImpulse=-ccp.normalMass*C,impulse0=ccp.positionImpulse;ccp.positionImpulse=b2Math.b2Max(impulse0+dImpulse,0);var impulseX=(dImpulse=ccp.positionImpulse-impulse0)*normalX,impulseY=dImpulse*normalY;b1_position.x-=invMass1*impulseX,b1_position.y-=invMass1*impulseY,b1_rotation-=invI1*(r1X*impulseY-r1Y*impulseX),b1.m_R.Set(b1_rotation),b2_position.x+=invMass2*impulseX,b2_position.y+=invMass2*impulseY,b2_rotation+=invI2*(r2X*impulseY-r2Y*impulseX),b2.m_R.Set(b2_rotation)}b1.m_rotation=b1_rotation,b2.m_rotation=b2_rotation}return minSeparation>=-b2Settings.b2_linearSlop},PostSolve:function(){for(var i=0;i<this.m_constraintCount;++i)for(var c=this.m_constraints[i],m=c.manifold,j=0;j<c.pointCount;++j){var mPoint=m.points[j],cPoint=c.points[j];mPoint.normalImpulse=cPoint.normalImpulse,mPoint.tangentImpulse=cPoint.tangentImpulse}},m_allocator:null,m_constraints:new Array,m_constraintCount:0};var b2CircleContact=Class.create();Object.extend(b2CircleContact.prototype,b2Contact.prototype),Object.extend(b2CircleContact.prototype,{initialize:function(s1,s2){if(this.m_node1=new b2ContactNode,this.m_node2=new b2ContactNode,this.m_flags=0,!s1||!s2)return this.m_shape1=null,void(this.m_shape2=null);this.m_shape1=s1,this.m_shape2=s2,this.m_manifoldCount=0,this.m_friction=Math.sqrt(this.m_shape1.m_friction*this.m_shape2.m_friction),this.m_restitution=b2Math.b2Max(this.m_shape1.m_restitution,this.m_shape2.m_restitution),this.m_prev=null,this.m_next=null,this.m_node1.contact=null,this.m_node1.prev=null,this.m_node1.next=null,this.m_node1.other=null,this.m_node2.contact=null,this.m_node2.prev=null,this.m_node2.next=null,this.m_node2.other=null,this.m_manifold=[new b2Manifold],this.m_manifold[0].pointCount=0,this.m_manifold[0].points[0].normalImpulse=0,this.m_manifold[0].points[0].tangentImpulse=0},Evaluate:function(){b2Collision.b2CollideCircle(this.m_manifold[0],this.m_shape1,this.m_shape2,!1),this.m_manifold[0].pointCount>0?this.m_manifoldCount=1:this.m_manifoldCount=0},GetManifolds:function(){return this.m_manifold},m_manifold:[new b2Manifold]}),b2CircleContact.Create=function(shape1,shape2,allocator){return new b2CircleContact(shape1,shape2)},b2CircleContact.Destroy=function(contact,allocator){};var b2Conservative=Class.create();b2Conservative.prototype={initialize:function(){}},b2Conservative.R1=new b2Mat22,b2Conservative.R2=new b2Mat22,b2Conservative.x1=new b2Vec2,b2Conservative.x2=new b2Vec2,b2Conservative.Conservative=function(shape1,shape2){var body1=shape1.GetBody(),body2=shape2.GetBody(),v1X=body1.m_position.x-body1.m_position0.x,v1Y=body1.m_position.y-body1.m_position0.y,omega1=body1.m_rotation-body1.m_rotation0,v2X=body2.m_position.x-body2.m_position0.x,v2Y=body2.m_position.y-body2.m_position0.y,omega2=body2.m_rotation-body2.m_rotation0,r1=shape1.GetMaxRadius(),r2=shape2.GetMaxRadius(),p1StartX=body1.m_position0.x,p1StartY=body1.m_position0.y,a1Start=body1.m_rotation0,p2StartX=body2.m_position0.x,p2StartY=body2.m_position0.y,a2Start=body2.m_rotation0,p1X=p1StartX,p1Y=p1StartY,a1=a1Start,p2X=p2StartX,p2Y=p2StartY,a2=a2Start;b2Conservative.R1.Set(a1),b2Conservative.R2.Set(a2),shape1.QuickSync(p1,b2Conservative.R1),shape2.QuickSync(p2,b2Conservative.R2);for(var dX,dY,s1=0,invRelativeVelocity=0,hit=!0,iter=0;iter<10;++iter){var distance=b2Distance.Distance(b2Conservative.x1,b2Conservative.x2,shape1,shape2);if(distance<b2Settings.b2_linearSlop){hit=0!=iter;break}if(0==iter){dX=b2Conservative.x2.x-b2Conservative.x1.x,dY=b2Conservative.x2.y-b2Conservative.x1.y;Math.sqrt(dX*dX+dY*dY);var relativeVelocity=dX*(v1X-v2X)+dY*(v1Y-v2Y)+Math.abs(omega1)*r1+Math.abs(omega2)*r2;if(Math.abs(relativeVelocity)<Number.MIN_VALUE){hit=!1;break}invRelativeVelocity=1/relativeVelocity}var s2=s1+distance*invRelativeVelocity;if(s2<0||1<s2){hit=!1;break}if(s2<(1+100*Number.MIN_VALUE)*s1){hit=!0;break}p1X=p1StartX+(s1=s2)*v1.x,p1Y=p1StartY+s1*v1.y,a1=a1Start+s1*omega1,p2X=p2StartX+s1*v2.x,p2Y=p2StartY+s1*v2.y,a2=a2Start+s1*omega2,b2Conservative.R1.Set(a1),b2Conservative.R2.Set(a2),shape1.QuickSync(p1,b2Conservative.R1),shape2.QuickSync(p2,b2Conservative.R2)}if(hit){dX=b2Conservative.x2.x-b2Conservative.x1.x,dY=b2Conservative.x2.y-b2Conservative.x1.y;var length=Math.sqrt(dX*dX+dY*dY);return length>FLT_EPSILON&&(d*=b2_linearSlop/length),body1.IsStatic()?(body1.m_position.x=p1X,body1.m_position.y=p1Y):(body1.m_position.x=p1X-dX,body1.m_position.y=p1Y-dY),body1.m_rotation=a1,body1.m_R.Set(a1),body1.QuickSyncShapes(),body2.IsStatic()?(body2.m_position.x=p2X,body2.m_position.y=p2Y):(body2.m_position.x=p2X+dX,body2.m_position.y=p2Y+dY),body2.m_position.x=p2X+dX,body2.m_position.y=p2Y+dY,body2.m_rotation=a2,body2.m_R.Set(a2),body2.QuickSyncShapes(),!0}return shape1.QuickSync(body1.m_position,body1.m_R),shape2.QuickSync(body2.m_position,body2.m_R),!1};var b2NullContact=Class.create();Object.extend(b2NullContact.prototype,b2Contact.prototype),Object.extend(b2NullContact.prototype,{initialize:function(s1,s2){if(this.m_node1=new b2ContactNode,this.m_node2=new b2ContactNode,this.m_flags=0,!s1||!s2)return this.m_shape1=null,void(this.m_shape2=null);this.m_shape1=s1,this.m_shape2=s2,this.m_manifoldCount=0,this.m_friction=Math.sqrt(this.m_shape1.m_friction*this.m_shape2.m_friction),this.m_restitution=b2Math.b2Max(this.m_shape1.m_restitution,this.m_shape2.m_restitution),this.m_prev=null,this.m_next=null,this.m_node1.contact=null,this.m_node1.prev=null,this.m_node1.next=null,this.m_node1.other=null,this.m_node2.contact=null,this.m_node2.prev=null,this.m_node2.next=null,this.m_node2.other=null},Evaluate:function(){},GetManifolds:function(){return null}});var b2PolyAndCircleContact=Class.create();Object.extend(b2PolyAndCircleContact.prototype,b2Contact.prototype),Object.extend(b2PolyAndCircleContact.prototype,{initialize:function(s1,s2){if(this.m_node1=new b2ContactNode,this.m_node2=new b2ContactNode,this.m_flags=0,!s1||!s2)return this.m_shape1=null,void(this.m_shape2=null);this.m_shape1=s1,this.m_shape2=s2,this.m_manifoldCount=0,this.m_friction=Math.sqrt(this.m_shape1.m_friction*this.m_shape2.m_friction),this.m_restitution=b2Math.b2Max(this.m_shape1.m_restitution,this.m_shape2.m_restitution),this.m_prev=null,this.m_next=null,this.m_node1.contact=null,this.m_node1.prev=null,this.m_node1.next=null,this.m_node1.other=null,this.m_node2.contact=null,this.m_node2.prev=null,this.m_node2.next=null,this.m_node2.other=null,this.m_manifold=[new b2Manifold],b2Settings.b2Assert(this.m_shape1.m_type==b2Shape.e_polyShape),b2Settings.b2Assert(this.m_shape2.m_type==b2Shape.e_circleShape),this.m_manifold[0].pointCount=0,this.m_manifold[0].points[0].normalImpulse=0,this.m_manifold[0].points[0].tangentImpulse=0},Evaluate:function(){b2Collision.b2CollidePolyAndCircle(this.m_manifold[0],this.m_shape1,this.m_shape2,!1),this.m_manifold[0].pointCount>0?this.m_manifoldCount=1:this.m_manifoldCount=0},GetManifolds:function(){return this.m_manifold},m_manifold:[new b2Manifold]}),b2PolyAndCircleContact.Create=function(shape1,shape2,allocator){return new b2PolyAndCircleContact(shape1,shape2)},b2PolyAndCircleContact.Destroy=function(contact,allocator){};var b2PolyContact=Class.create();Object.extend(b2PolyContact.prototype,b2Contact.prototype),Object.extend(b2PolyContact.prototype,{initialize:function(s1,s2){if(this.m_node1=new b2ContactNode,this.m_node2=new b2ContactNode,this.m_flags=0,!s1||!s2)return this.m_shape1=null,void(this.m_shape2=null);this.m_shape1=s1,this.m_shape2=s2,this.m_manifoldCount=0,this.m_friction=Math.sqrt(this.m_shape1.m_friction*this.m_shape2.m_friction),this.m_restitution=b2Math.b2Max(this.m_shape1.m_restitution,this.m_shape2.m_restitution),this.m_prev=null,this.m_next=null,this.m_node1.contact=null,this.m_node1.prev=null,this.m_node1.next=null,this.m_node1.other=null,this.m_node2.contact=null,this.m_node2.prev=null,this.m_node2.next=null,this.m_node2.other=null,this.m0=new b2Manifold,this.m_manifold=[new b2Manifold],this.m_manifold[0].pointCount=0},m0:new b2Manifold,Evaluate:function(){for(var tMani=this.m_manifold[0],tPoints=this.m0.points,k=0;k<tMani.pointCount;k++){var tPoint=tPoints[k],tPoint0=tMani.points[k];tPoint.normalImpulse=tPoint0.normalImpulse,tPoint.tangentImpulse=tPoint0.tangentImpulse,tPoint.id=tPoint0.id.Copy()}if(this.m0.pointCount=tMani.pointCount,b2Collision.b2CollidePoly(tMani,this.m_shape1,this.m_shape2,!1),tMani.pointCount>0){for(var match=[!1,!1],i=0;i<tMani.pointCount;++i){var cp=tMani.points[i];cp.normalImpulse=0,cp.tangentImpulse=0;for(var idKey=cp.id.key,j=0;j<this.m0.pointCount;++j)if(1!=match[j]){var cp0=this.m0.points[j];if(cp0.id.key==idKey){match[j]=!0,cp.normalImpulse=cp0.normalImpulse,cp.tangentImpulse=cp0.tangentImpulse;break}}}this.m_manifoldCount=1}else this.m_manifoldCount=0},GetManifolds:function(){return this.m_manifold},m_manifold:[new b2Manifold]}),b2PolyContact.Create=function(shape1,shape2,allocator){return new b2PolyContact(shape1,shape2)},b2PolyContact.Destroy=function(contact,allocator){};var b2ContactManager=Class.create();Object.extend(b2ContactManager.prototype,b2PairCallback.prototype),Object.extend(b2ContactManager.prototype,{initialize:function(){this.m_nullContact=new b2NullContact,this.m_world=null,this.m_destroyImmediate=!1},PairAdded:function(proxyUserData1,proxyUserData2){var shape1=proxyUserData1,shape2=proxyUserData2,body1=shape1.m_body,body2=shape2.m_body;if(body1.IsStatic()&&body2.IsStatic())return this.m_nullContact;if(shape1.m_body==shape2.m_body)return this.m_nullContact;if(body2.IsConnected(body1))return this.m_nullContact;if(null!=this.m_world.m_filter&&0==this.m_world.m_filter.ShouldCollide(shape1,shape2))return this.m_nullContact;if(0==body2.m_invMass){var tempShape=shape1;shape1=shape2,shape2=tempShape;var tempBody=body1;body1=body2,body2=tempBody}var contact=b2Contact.Create(shape1,shape2,this.m_world.m_blockAllocator);return null==contact?this.m_nullContact:(contact.m_prev=null,contact.m_next=this.m_world.m_contactList,null!=this.m_world.m_contactList&&(this.m_world.m_contactList.m_prev=contact),this.m_world.m_contactList=contact,this.m_world.m_contactCount++,contact)},PairRemoved:function(proxyUserData1,proxyUserData2,pairUserData){if(null!=pairUserData){var c=pairUserData;c!=this.m_nullContact&&(1==this.m_destroyImmediate?(this.DestroyContact(c),c=null):c.m_flags|=b2Contact.e_destroyFlag)}},DestroyContact:function(c){if(c.m_prev&&(c.m_prev.m_next=c.m_next),c.m_next&&(c.m_next.m_prev=c.m_prev),c==this.m_world.m_contactList&&(this.m_world.m_contactList=c.m_next),c.GetManifoldCount()>0){var body1=c.m_shape1.m_body,body2=c.m_shape2.m_body,node1=c.m_node1,node2=c.m_node2;body1.WakeUp(),body2.WakeUp(),node1.prev&&(node1.prev.next=node1.next),node1.next&&(node1.next.prev=node1.prev),node1==body1.m_contactList&&(body1.m_contactList=node1.next),node1.prev=null,node1.next=null,node2.prev&&(node2.prev.next=node2.next),node2.next&&(node2.next.prev=node2.prev),node2==body2.m_contactList&&(body2.m_contactList=node2.next),node2.prev=null,node2.next=null}b2Contact.Destroy(c,this.m_world.m_blockAllocator),--this.m_world.m_contactCount},CleanContactList:function(){for(var c=this.m_world.m_contactList;null!=c;){var c0=c;c=c.m_next,c0.m_flags&b2Contact.e_destroyFlag&&(this.DestroyContact(c0),c0=null)}},Collide:function(){for(var body1,body2,node1,node2,c=this.m_world.m_contactList;null!=c;c=c.m_next)if(!c.m_shape1.m_body.IsSleeping()||!c.m_shape2.m_body.IsSleeping()){var oldCount=c.GetManifoldCount();c.Evaluate();var newCount=c.GetManifoldCount();0==oldCount&&newCount>0?(body1=c.m_shape1.m_body,body2=c.m_shape2.m_body,node1=c.m_node1,node2=c.m_node2,node1.contact=c,node1.other=body2,node1.prev=null,node1.next=body1.m_contactList,null!=node1.next&&(node1.next.prev=c.m_node1),body1.m_contactList=c.m_node1,node2.contact=c,node2.other=body1,node2.prev=null,node2.next=body2.m_contactList,null!=node2.next&&(node2.next.prev=node2),body2.m_contactList=node2):oldCount>0&&0==newCount&&(body1=c.m_shape1.m_body,body2=c.m_shape2.m_body,node1=c.m_node1,node2=c.m_node2,node1.prev&&(node1.prev.next=node1.next),node1.next&&(node1.next.prev=node1.prev),node1==body1.m_contactList&&(body1.m_contactList=node1.next),node1.prev=null,node1.next=null,node2.prev&&(node2.prev.next=node2.next),node2.next&&(node2.next.prev=node2.prev),node2==body2.m_contactList&&(body2.m_contactList=node2.next),node2.prev=null,node2.next=null)}},m_world:null,m_nullContact:new b2NullContact,m_destroyImmediate:null});var b2World=Class.create();b2World.prototype={initialize:function(worldAABB,gravity,doSleep){this.step=new b2TimeStep,this.m_contactManager=new b2ContactManager,this.m_listener=null,this.m_filter=b2CollisionFilter.b2_defaultFilter,this.m_bodyList=null,this.m_contactList=null,this.m_jointList=null,this.m_bodyCount=0,this.m_contactCount=0,this.m_jointCount=0,this.m_bodyDestroyList=null,this.m_allowSleep=doSleep,this.m_gravity=gravity,this.m_contactManager.m_world=this,this.m_broadPhase=new b2BroadPhase(worldAABB,this.m_contactManager);var bd=new b2BodyDef;this.m_groundBody=this.CreateBody(bd)},SetListener:function(listener){this.m_listener=listener},SetFilter:function(filter){this.m_filter=filter},CreateBody:function(def){var b=new b2Body(def,this);return b.m_prev=null,b.m_next=this.m_bodyList,this.m_bodyList&&(this.m_bodyList.m_prev=b),this.m_bodyList=b,++this.m_bodyCount,b},DestroyBody:function(b){b.m_flags&b2Body.e_destroyFlag||(b.m_prev&&(b.m_prev.m_next=b.m_next),b.m_next&&(b.m_next.m_prev=b.m_prev),b==this.m_bodyList&&(this.m_bodyList=b.m_next),b.m_flags|=b2Body.e_destroyFlag,--this.m_bodyCount,b.m_prev=null,b.m_next=this.m_bodyDestroyList,this.m_bodyDestroyList=b)},CleanBodyList:function(){this.m_contactManager.m_destroyImmediate=!0;for(var b=this.m_bodyDestroyList;b;){var b0=b;b=b.m_next;for(var jn=b0.m_jointList;jn;){var jn0=jn;jn=jn.next,this.m_listener&&this.m_listener.NotifyJointDestroyed(jn0.joint),this.DestroyJoint(jn0.joint)}b0.Destroy()}this.m_bodyDestroyList=null,this.m_contactManager.m_destroyImmediate=!1},CreateJoint:function(def){var j=b2Joint.Create(def,this.m_blockAllocator);if(j.m_prev=null,j.m_next=this.m_jointList,this.m_jointList&&(this.m_jointList.m_prev=j),this.m_jointList=j,++this.m_jointCount,j.m_node1.joint=j,j.m_node1.other=j.m_body2,j.m_node1.prev=null,j.m_node1.next=j.m_body1.m_jointList,j.m_body1.m_jointList&&(j.m_body1.m_jointList.prev=j.m_node1),j.m_body1.m_jointList=j.m_node1,j.m_node2.joint=j,j.m_node2.other=j.m_body1,j.m_node2.prev=null,j.m_node2.next=j.m_body2.m_jointList,j.m_body2.m_jointList&&(j.m_body2.m_jointList.prev=j.m_node2),j.m_body2.m_jointList=j.m_node2,0==def.collideConnected)for(var s=(def.body1.m_shapeCount<def.body2.m_shapeCount?def.body1:def.body2).m_shapeList;s;s=s.m_next)s.ResetProxy(this.m_broadPhase);return j},DestroyJoint:function(j){var collideConnected=j.m_collideConnected;j.m_prev&&(j.m_prev.m_next=j.m_next),j.m_next&&(j.m_next.m_prev=j.m_prev),j==this.m_jointList&&(this.m_jointList=j.m_next);var body1=j.m_body1,body2=j.m_body2;if(body1.WakeUp(),body2.WakeUp(),j.m_node1.prev&&(j.m_node1.prev.next=j.m_node1.next),j.m_node1.next&&(j.m_node1.next.prev=j.m_node1.prev),j.m_node1==body1.m_jointList&&(body1.m_jointList=j.m_node1.next),j.m_node1.prev=null,j.m_node1.next=null,j.m_node2.prev&&(j.m_node2.prev.next=j.m_node2.next),j.m_node2.next&&(j.m_node2.next.prev=j.m_node2.prev),j.m_node2==body2.m_jointList&&(body2.m_jointList=j.m_node2.next),j.m_node2.prev=null,j.m_node2.next=null,b2Joint.Destroy(j,this.m_blockAllocator),--this.m_jointCount,0==collideConnected)for(var s=(body1.m_shapeCount<body2.m_shapeCount?body1:body2).m_shapeList;s;s=s.m_next)s.ResetProxy(this.m_broadPhase)},GetGroundBody:function(){return this.m_groundBody},step:new b2TimeStep,Step:function(dt,iterations){var b,other;this.step.dt=dt,this.step.iterations=iterations,this.step.inv_dt=dt>0?1/dt:0,this.m_positionIterationCount=0,this.m_contactManager.CleanContactList(),this.CleanBodyList(),this.m_contactManager.Collide();var island=new b2Island(this.m_bodyCount,this.m_contactCount,this.m_jointCount,this.m_stackAllocator);for(b=this.m_bodyList;null!=b;b=b.m_next)b.m_flags&=~b2Body.e_islandFlag;for(var c=this.m_contactList;null!=c;c=c.m_next)c.m_flags&=~b2Contact.e_islandFlag;for(var j=this.m_jointList;null!=j;j=j.m_next)j.m_islandFlag=!1;this.m_bodyCount;for(var stack=new Array(this.m_bodyCount),k=0;k<this.m_bodyCount;k++)stack[k]=null;for(var seed=this.m_bodyList;null!=seed;seed=seed.m_next)if(!(seed.m_flags&(b2Body.e_staticFlag|b2Body.e_islandFlag|b2Body.e_sleepFlag|b2Body.e_frozenFlag))){island.Clear();var stackCount=0;for(stack[stackCount++]=seed,seed.m_flags|=b2Body.e_islandFlag;stackCount>0;)if(b=stack[--stackCount],island.AddBody(b),b.m_flags&=~b2Body.e_sleepFlag,!(b.m_flags&b2Body.e_staticFlag)){for(var cn=b.m_contactList;null!=cn;cn=cn.next)cn.contact.m_flags&b2Contact.e_islandFlag||(island.AddContact(cn.contact),cn.contact.m_flags|=b2Contact.e_islandFlag,(other=cn.other).m_flags&b2Body.e_islandFlag||(stack[stackCount++]=other,other.m_flags|=b2Body.e_islandFlag));for(var jn=b.m_jointList;null!=jn;jn=jn.next)1!=jn.joint.m_islandFlag&&(island.AddJoint(jn.joint),jn.joint.m_islandFlag=!0,(other=jn.other).m_flags&b2Body.e_islandFlag||(stack[stackCount++]=other,other.m_flags|=b2Body.e_islandFlag))}island.Solve(this.step,this.m_gravity),this.m_positionIterationCount=b2Math.b2Max(this.m_positionIterationCount,b2Island.m_positionIterationCount),this.m_allowSleep&&island.UpdateSleep(dt);for(var i=0;i<island.m_bodyCount;++i){if((b=island.m_bodies[i]).m_flags&b2Body.e_staticFlag&&(b.m_flags&=~b2Body.e_islandFlag),b.IsFrozen()&&this.m_listener)this.m_listener.NotifyBoundaryViolated(b)==b2WorldListener.b2_destroyBody&&(this.DestroyBody(b),b=null,island.m_bodies[i]=null)}}this.m_broadPhase.Commit()},Query:function(aabb,shapes,maxCount){for(var results=new Array,count=this.m_broadPhase.QueryAABB(aabb,results,maxCount),i=0;i<count;++i)shapes[i]=results[i];return count},GetBodyList:function(){return this.m_bodyList},GetJointList:function(){return this.m_jointList},GetContactList:function(){return this.m_contactList},m_blockAllocator:null,m_stackAllocator:null,m_broadPhase:null,m_contactManager:new b2ContactManager,m_bodyList:null,m_contactList:null,m_jointList:null,m_bodyCount:0,m_contactCount:0,m_jointCount:0,m_bodyDestroyList:null,m_gravity:null,m_allowSleep:null,m_groundBody:null,m_listener:null,m_filter:null,m_positionIterationCount:0},b2World.s_enablePositionCorrection=1,b2World.s_enableWarmStarting=1;var b2WorldListener=Class.create();b2WorldListener.prototype={NotifyJointDestroyed:function(joint){},NotifyBoundaryViolated:function(body){return b2WorldListener.b2_freezeBody},initialize:function(){}},b2WorldListener.b2_freezeBody=0,b2WorldListener.b2_destroyBody=1;var b2JointNode=Class.create();b2JointNode.prototype={other:null,joint:null,prev:null,next:null,initialize:function(){}};var b2Joint=Class.create();b2Joint.prototype={GetType:function(){return this.m_type},GetAnchor1:function(){return null},GetAnchor2:function(){return null},GetReactionForce:function(invTimeStep){return null},GetReactionTorque:function(invTimeStep){return 0},GetBody1:function(){return this.m_body1},GetBody2:function(){return this.m_body2},GetNext:function(){return this.m_next},GetUserData:function(){return this.m_userData},initialize:function(def){this.m_node1=new b2JointNode,this.m_node2=new b2JointNode,this.m_type=def.type,this.m_prev=null,this.m_next=null,this.m_body1=def.body1,this.m_body2=def.body2,this.m_collideConnected=def.collideConnected,this.m_islandFlag=!1,this.m_userData=def.userData},PrepareVelocitySolver:function(){},SolveVelocityConstraints:function(step){},PreparePositionSolver:function(){},SolvePositionConstraints:function(){return!1},m_type:0,m_prev:null,m_next:null,m_node1:new b2JointNode,m_node2:new b2JointNode,m_body1:null,m_body2:null,m_islandFlag:null,m_collideConnected:null,m_userData:null},b2Joint.Create=function(def,allocator){var joint=null;switch(def.type){case b2Joint.e_distanceJoint:joint=new b2DistanceJoint(def);break;case b2Joint.e_mouseJoint:joint=new b2MouseJoint(def);break;case b2Joint.e_prismaticJoint:joint=new b2PrismaticJoint(def);break;case b2Joint.e_revoluteJoint:joint=new b2RevoluteJoint(def);break;case b2Joint.e_pulleyJoint:joint=new b2PulleyJoint(def);break;case b2Joint.e_gearJoint:joint=new b2GearJoint(def)}return joint},b2Joint.Destroy=function(joint,allocator){},b2Joint.e_unknownJoint=0,b2Joint.e_revoluteJoint=1,b2Joint.e_prismaticJoint=2,b2Joint.e_distanceJoint=3,b2Joint.e_pulleyJoint=4,b2Joint.e_mouseJoint=5,b2Joint.e_gearJoint=6,b2Joint.e_inactiveLimit=0,b2Joint.e_atLowerLimit=1,b2Joint.e_atUpperLimit=2,b2Joint.e_equalLimits=3;var b2JointDef=Class.create();b2JointDef.prototype={initialize:function(){this.type=b2Joint.e_unknownJoint,this.userData=null,this.body1=null,this.body2=null,this.collideConnected=!1},type:0,userData:null,body1:null,body2:null,collideConnected:null};var b2DistanceJoint=Class.create();Object.extend(b2DistanceJoint.prototype,b2Joint.prototype),Object.extend(b2DistanceJoint.prototype,{initialize:function(def){var tMat,tX,tY;this.m_node1=new b2JointNode,this.m_node2=new b2JointNode,this.m_type=def.type,this.m_prev=null,this.m_next=null,this.m_body1=def.body1,this.m_body2=def.body2,this.m_collideConnected=def.collideConnected,this.m_islandFlag=!1,this.m_userData=def.userData,this.m_localAnchor1=new b2Vec2,this.m_localAnchor2=new b2Vec2,this.m_u=new b2Vec2,tMat=this.m_body1.m_R,tX=def.anchorPoint1.x-this.m_body1.m_position.x,tY=def.anchorPoint1.y-this.m_body1.m_position.y,this.m_localAnchor1.x=tX*tMat.col1.x+tY*tMat.col1.y,this.m_localAnchor1.y=tX*tMat.col2.x+tY*tMat.col2.y,tMat=this.m_body2.m_R,tX=def.anchorPoint2.x-this.m_body2.m_position.x,tY=def.anchorPoint2.y-this.m_body2.m_position.y,this.m_localAnchor2.x=tX*tMat.col1.x+tY*tMat.col1.y,this.m_localAnchor2.y=tX*tMat.col2.x+tY*tMat.col2.y,tX=def.anchorPoint2.x-def.anchorPoint1.x,tY=def.anchorPoint2.y-def.anchorPoint1.y,this.m_length=Math.sqrt(tX*tX+tY*tY),this.m_impulse=0},PrepareVelocitySolver:function(){var tMat,r1X=(tMat=this.m_body1.m_R).col1.x*this.m_localAnchor1.x+tMat.col2.x*this.m_localAnchor1.y,r1Y=tMat.col1.y*this.m_localAnchor1.x+tMat.col2.y*this.m_localAnchor1.y,r2X=(tMat=this.m_body2.m_R).col1.x*this.m_localAnchor2.x+tMat.col2.x*this.m_localAnchor2.y,r2Y=tMat.col1.y*this.m_localAnchor2.x+tMat.col2.y*this.m_localAnchor2.y;this.m_u.x=this.m_body2.m_position.x+r2X-this.m_body1.m_position.x-r1X,this.m_u.y=this.m_body2.m_position.y+r2Y-this.m_body1.m_position.y-r1Y;var length=Math.sqrt(this.m_u.x*this.m_u.x+this.m_u.y*this.m_u.y);length>b2Settings.b2_linearSlop?this.m_u.Multiply(1/length):this.m_u.SetZero();var cr1u=r1X*this.m_u.y-r1Y*this.m_u.x,cr2u=r2X*this.m_u.y-r2Y*this.m_u.x;if(this.m_mass=this.m_body1.m_invMass+this.m_body1.m_invI*cr1u*cr1u+this.m_body2.m_invMass+this.m_body2.m_invI*cr2u*cr2u,this.m_mass=1/this.m_mass,b2World.s_enableWarmStarting){var PX=this.m_impulse*this.m_u.x,PY=this.m_impulse*this.m_u.y;this.m_body1.m_linearVelocity.x-=this.m_body1.m_invMass*PX,this.m_body1.m_linearVelocity.y-=this.m_body1.m_invMass*PY,this.m_body1.m_angularVelocity-=this.m_body1.m_invI*(r1X*PY-r1Y*PX),this.m_body2.m_linearVelocity.x+=this.m_body2.m_invMass*PX,this.m_body2.m_linearVelocity.y+=this.m_body2.m_invMass*PY,this.m_body2.m_angularVelocity+=this.m_body2.m_invI*(r2X*PY-r2Y*PX)}else this.m_impulse=0},SolveVelocityConstraints:function(step){var tMat,r1X=(tMat=this.m_body1.m_R).col1.x*this.m_localAnchor1.x+tMat.col2.x*this.m_localAnchor1.y,r1Y=tMat.col1.y*this.m_localAnchor1.x+tMat.col2.y*this.m_localAnchor1.y,r2X=(tMat=this.m_body2.m_R).col1.x*this.m_localAnchor2.x+tMat.col2.x*this.m_localAnchor2.y,r2Y=tMat.col1.y*this.m_localAnchor2.x+tMat.col2.y*this.m_localAnchor2.y,v1X=this.m_body1.m_linearVelocity.x+-this.m_body1.m_angularVelocity*r1Y,v1Y=this.m_body1.m_linearVelocity.y+this.m_body1.m_angularVelocity*r1X,v2X=this.m_body2.m_linearVelocity.x+-this.m_body2.m_angularVelocity*r2Y,v2Y=this.m_body2.m_linearVelocity.y+this.m_body2.m_angularVelocity*r2X,Cdot=this.m_u.x*(v2X-v1X)+this.m_u.y*(v2Y-v1Y),impulse=-this.m_mass*Cdot;this.m_impulse+=impulse;var PX=impulse*this.m_u.x,PY=impulse*this.m_u.y;this.m_body1.m_linearVelocity.x-=this.m_body1.m_invMass*PX,this.m_body1.m_linearVelocity.y-=this.m_body1.m_invMass*PY,this.m_body1.m_angularVelocity-=this.m_body1.m_invI*(r1X*PY-r1Y*PX),this.m_body2.m_linearVelocity.x+=this.m_body2.m_invMass*PX,this.m_body2.m_linearVelocity.y+=this.m_body2.m_invMass*PY,this.m_body2.m_angularVelocity+=this.m_body2.m_invI*(r2X*PY-r2Y*PX)},SolvePositionConstraints:function(){var tMat,r1X=(tMat=this.m_body1.m_R).col1.x*this.m_localAnchor1.x+tMat.col2.x*this.m_localAnchor1.y,r1Y=tMat.col1.y*this.m_localAnchor1.x+tMat.col2.y*this.m_localAnchor1.y,r2X=(tMat=this.m_body2.m_R).col1.x*this.m_localAnchor2.x+tMat.col2.x*this.m_localAnchor2.y,r2Y=tMat.col1.y*this.m_localAnchor2.x+tMat.col2.y*this.m_localAnchor2.y,dX=this.m_body2.m_position.x+r2X-this.m_body1.m_position.x-r1X,dY=this.m_body2.m_position.y+r2Y-this.m_body1.m_position.y-r1Y,length=Math.sqrt(dX*dX+dY*dY);dX/=length,dY/=length;var C=length-this.m_length;C=b2Math.b2Clamp(C,-b2Settings.b2_maxLinearCorrection,b2Settings.b2_maxLinearCorrection);var impulse=-this.m_mass*C;this.m_u.Set(dX,dY);var PX=impulse*this.m_u.x,PY=impulse*this.m_u.y;return this.m_body1.m_position.x-=this.m_body1.m_invMass*PX,this.m_body1.m_position.y-=this.m_body1.m_invMass*PY,this.m_body1.m_rotation-=this.m_body1.m_invI*(r1X*PY-r1Y*PX),this.m_body2.m_position.x+=this.m_body2.m_invMass*PX,this.m_body2.m_position.y+=this.m_body2.m_invMass*PY,this.m_body2.m_rotation+=this.m_body2.m_invI*(r2X*PY-r2Y*PX),this.m_body1.m_R.Set(this.m_body1.m_rotation),this.m_body2.m_R.Set(this.m_body2.m_rotation),b2Math.b2Abs(C)<b2Settings.b2_linearSlop},GetAnchor1:function(){return b2Math.AddVV(this.m_body1.m_position,b2Math.b2MulMV(this.m_body1.m_R,this.m_localAnchor1))},GetAnchor2:function(){return b2Math.AddVV(this.m_body2.m_position,b2Math.b2MulMV(this.m_body2.m_R,this.m_localAnchor2))},GetReactionForce:function(invTimeStep){var F=new b2Vec2;return F.SetV(this.m_u),F.Multiply(this.m_impulse*invTimeStep),F},GetReactionTorque:function(invTimeStep){return 0},m_localAnchor1:new b2Vec2,m_localAnchor2:new b2Vec2,m_u:new b2Vec2,m_impulse:null,m_mass:null,m_length:null});var b2DistanceJointDef=Class.create();Object.extend(b2DistanceJointDef.prototype,b2JointDef.prototype),Object.extend(b2DistanceJointDef.prototype,{initialize:function(){this.type=b2Joint.e_unknownJoint,this.userData=null,this.body1=null,this.body2=null,this.collideConnected=!1,this.anchorPoint1=new b2Vec2,this.anchorPoint2=new b2Vec2,this.type=b2Joint.e_distanceJoint},anchorPoint1:new b2Vec2,anchorPoint2:new b2Vec2});var b2Jacobian=Class.create();b2Jacobian.prototype={linear1:new b2Vec2,angular1:null,linear2:new b2Vec2,angular2:null,SetZero:function(){this.linear1.SetZero(),this.angular1=0,this.linear2.SetZero(),this.angular2=0},Set:function(x1,a1,x2,a2){this.linear1.SetV(x1),this.angular1=a1,this.linear2.SetV(x2),this.angular2=a2},Compute:function(x1,a1,x2,a2){return this.linear1.x*x1.x+this.linear1.y*x1.y+this.angular1*a1+(this.linear2.x*x2.x+this.linear2.y*x2.y)+this.angular2*a2},initialize:function(){this.linear1=new b2Vec2,this.linear2=new b2Vec2}};var b2GearJoint=Class.create();Object.extend(b2GearJoint.prototype,b2Joint.prototype),Object.extend(b2GearJoint.prototype,{GetAnchor1:function(){var tMat=this.m_body1.m_R;return new b2Vec2(this.m_body1.m_position.x+(tMat.col1.x*this.m_localAnchor1.x+tMat.col2.x*this.m_localAnchor1.y),this.m_body1.m_position.y+(tMat.col1.y*this.m_localAnchor1.x+tMat.col2.y*this.m_localAnchor1.y))},GetAnchor2:function(){var tMat=this.m_body2.m_R;return new b2Vec2(this.m_body2.m_position.x+(tMat.col1.x*this.m_localAnchor2.x+tMat.col2.x*this.m_localAnchor2.y),this.m_body2.m_position.y+(tMat.col1.y*this.m_localAnchor2.x+tMat.col2.y*this.m_localAnchor2.y))},GetReactionForce:function(invTimeStep){return new b2Vec2},GetReactionTorque:function(invTimeStep){return 0},GetRatio:function(){return this.m_ratio},initialize:function(def){var coordinate1,coordinate2;this.m_node1=new b2JointNode,this.m_node2=new b2JointNode,this.m_type=def.type,this.m_prev=null,this.m_next=null,this.m_body1=def.body1,this.m_body2=def.body2,this.m_collideConnected=def.collideConnected,this.m_islandFlag=!1,this.m_userData=def.userData,this.m_groundAnchor1=new b2Vec2,this.m_groundAnchor2=new b2Vec2,this.m_localAnchor1=new b2Vec2,this.m_localAnchor2=new b2Vec2,this.m_J=new b2Jacobian,this.m_revolute1=null,this.m_prismatic1=null,this.m_revolute2=null,this.m_prismatic2=null,this.m_ground1=def.joint1.m_body1,this.m_body1=def.joint1.m_body2,def.joint1.m_type==b2Joint.e_revoluteJoint?(this.m_revolute1=def.joint1,this.m_groundAnchor1.SetV(this.m_revolute1.m_localAnchor1),this.m_localAnchor1.SetV(this.m_revolute1.m_localAnchor2),coordinate1=this.m_revolute1.GetJointAngle()):(this.m_prismatic1=def.joint1,this.m_groundAnchor1.SetV(this.m_prismatic1.m_localAnchor1),this.m_localAnchor1.SetV(this.m_prismatic1.m_localAnchor2),coordinate1=this.m_prismatic1.GetJointTranslation()),this.m_ground2=def.joint2.m_body1,this.m_body2=def.joint2.m_body2,def.joint2.m_type==b2Joint.e_revoluteJoint?(this.m_revolute2=def.joint2,this.m_groundAnchor2.SetV(this.m_revolute2.m_localAnchor1),this.m_localAnchor2.SetV(this.m_revolute2.m_localAnchor2),coordinate2=this.m_revolute2.GetJointAngle()):(this.m_prismatic2=def.joint2,this.m_groundAnchor2.SetV(this.m_prismatic2.m_localAnchor1),this.m_localAnchor2.SetV(this.m_prismatic2.m_localAnchor2),coordinate2=this.m_prismatic2.GetJointTranslation()),this.m_ratio=def.ratio,this.m_constant=coordinate1+this.m_ratio*coordinate2,this.m_impulse=0},PrepareVelocitySolver:function(){var ugX,ugY,tMat,tVec,crug,g1=this.m_ground1,g2=this.m_ground2,b1=this.m_body1,b2=this.m_body2,K=0;this.m_J.SetZero(),this.m_revolute1?(this.m_J.angular1=-1,K+=b1.m_invI):(tMat=g1.m_R,tVec=this.m_prismatic1.m_localXAxis1,ugX=tMat.col1.x*tVec.x+tMat.col2.x*tVec.y,ugY=tMat.col1.y*tVec.x+tMat.col2.y*tVec.y,crug=((tMat=b1.m_R).col1.x*this.m_localAnchor1.x+tMat.col2.x*this.m_localAnchor1.y)*ugY-(tMat.col1.y*this.m_localAnchor1.x+tMat.col2.y*this.m_localAnchor1.y)*ugX,this.m_J.linear1.Set(-ugX,-ugY),this.m_J.angular1=-crug,K+=b1.m_invMass+b1.m_invI*crug*crug),this.m_revolute2?(this.m_J.angular2=-this.m_ratio,K+=this.m_ratio*this.m_ratio*b2.m_invI):(tMat=g2.m_R,tVec=this.m_prismatic2.m_localXAxis1,ugX=tMat.col1.x*tVec.x+tMat.col2.x*tVec.y,ugY=tMat.col1.y*tVec.x+tMat.col2.y*tVec.y,crug=((tMat=b2.m_R).col1.x*this.m_localAnchor2.x+tMat.col2.x*this.m_localAnchor2.y)*ugY-(tMat.col1.y*this.m_localAnchor2.x+tMat.col2.y*this.m_localAnchor2.y)*ugX,this.m_J.linear2.Set(-this.m_ratio*ugX,-this.m_ratio*ugY),this.m_J.angular2=-this.m_ratio*crug,K+=this.m_ratio*this.m_ratio*(b2.m_invMass+b2.m_invI*crug*crug)),this.m_mass=1/K,b1.m_linearVelocity.x+=b1.m_invMass*this.m_impulse*this.m_J.linear1.x,b1.m_linearVelocity.y+=b1.m_invMass*this.m_impulse*this.m_J.linear1.y,b1.m_angularVelocity+=b1.m_invI*this.m_impulse*this.m_J.angular1,b2.m_linearVelocity.x+=b2.m_invMass*this.m_impulse*this.m_J.linear2.x,b2.m_linearVelocity.y+=b2.m_invMass*this.m_impulse*this.m_J.linear2.y,b2.m_angularVelocity+=b2.m_invI*this.m_impulse*this.m_J.angular2},SolveVelocityConstraints:function(step){var b1=this.m_body1,b2=this.m_body2,Cdot=this.m_J.Compute(b1.m_linearVelocity,b1.m_angularVelocity,b2.m_linearVelocity,b2.m_angularVelocity),impulse=-this.m_mass*Cdot;this.m_impulse+=impulse,b1.m_linearVelocity.x+=b1.m_invMass*impulse*this.m_J.linear1.x,b1.m_linearVelocity.y+=b1.m_invMass*impulse*this.m_J.linear1.y,b1.m_angularVelocity+=b1.m_invI*impulse*this.m_J.angular1,b2.m_linearVelocity.x+=b2.m_invMass*impulse*this.m_J.linear2.x,b2.m_linearVelocity.y+=b2.m_invMass*impulse*this.m_J.linear2.y,b2.m_angularVelocity+=b2.m_invI*impulse*this.m_J.angular2},SolvePositionConstraints:function(){var coordinate1,coordinate2,b1=this.m_body1,b2=this.m_body2;coordinate1=this.m_revolute1?this.m_revolute1.GetJointAngle():this.m_prismatic1.GetJointTranslation(),coordinate2=this.m_revolute2?this.m_revolute2.GetJointAngle():this.m_prismatic2.GetJointTranslation();var C=this.m_constant-(coordinate1+this.m_ratio*coordinate2),impulse=-this.m_mass*C;return b1.m_position.x+=b1.m_invMass*impulse*this.m_J.linear1.x,b1.m_position.y+=b1.m_invMass*impulse*this.m_J.linear1.y,b1.m_rotation+=b1.m_invI*impulse*this.m_J.angular1,b2.m_position.x+=b2.m_invMass*impulse*this.m_J.linear2.x,b2.m_position.y+=b2.m_invMass*impulse*this.m_J.linear2.y,b2.m_rotation+=b2.m_invI*impulse*this.m_J.angular2,b1.m_R.Set(b1.m_rotation),b2.m_R.Set(b2.m_rotation),0<b2Settings.b2_linearSlop},m_ground1:null,m_ground2:null,m_revolute1:null,m_prismatic1:null,m_revolute2:null,m_prismatic2:null,m_groundAnchor1:new b2Vec2,m_groundAnchor2:new b2Vec2,m_localAnchor1:new b2Vec2,m_localAnchor2:new b2Vec2,m_J:new b2Jacobian,m_constant:null,m_ratio:null,m_mass:null,m_impulse:null});var b2GearJointDef=Class.create();Object.extend(b2GearJointDef.prototype,b2JointDef.prototype),Object.extend(b2GearJointDef.prototype,{initialize:function(){this.type=b2Joint.e_gearJoint,this.joint1=null,this.joint2=null,this.ratio=1},joint1:null,joint2:null,ratio:null});var b2MouseJoint=Class.create();Object.extend(b2MouseJoint.prototype,b2Joint.prototype),Object.extend(b2MouseJoint.prototype,{GetAnchor1:function(){return this.m_target},GetAnchor2:function(){var tVec=b2Math.b2MulMV(this.m_body2.m_R,this.m_localAnchor);return tVec.Add(this.m_body2.m_position),tVec},GetReactionForce:function(invTimeStep){var F=new b2Vec2;return F.SetV(this.m_impulse),F.Multiply(invTimeStep),F},GetReactionTorque:function(invTimeStep){return 0},SetTarget:function(target){this.m_body2.WakeUp(),this.m_target=target},initialize:function(def){this.m_node1=new b2JointNode,this.m_node2=new b2JointNode,this.m_type=def.type,this.m_prev=null,this.m_next=null,this.m_body1=def.body1,this.m_body2=def.body2,this.m_collideConnected=def.collideConnected,this.m_islandFlag=!1,this.m_userData=def.userData,this.K=new b2Mat22,this.K1=new b2Mat22,this.K2=new b2Mat22,this.m_localAnchor=new b2Vec2,this.m_target=new b2Vec2,this.m_impulse=new b2Vec2,this.m_ptpMass=new b2Mat22,this.m_C=new b2Vec2,this.m_target.SetV(def.target);var tX=this.m_target.x-this.m_body2.m_position.x,tY=this.m_target.y-this.m_body2.m_position.y;this.m_localAnchor.x=tX*this.m_body2.m_R.col1.x+tY*this.m_body2.m_R.col1.y,this.m_localAnchor.y=tX*this.m_body2.m_R.col2.x+tY*this.m_body2.m_R.col2.y,this.m_maxForce=def.maxForce,this.m_impulse.SetZero();var mass=this.m_body2.m_mass,omega=2*b2Settings.b2_pi*def.frequencyHz,d=2*mass*def.dampingRatio*omega,k=mass*omega*omega;this.m_gamma=1/(d+def.timeStep*k),this.m_beta=def.timeStep*k/(d+def.timeStep*k)},K:new b2Mat22,K1:new b2Mat22,K2:new b2Mat22,PrepareVelocitySolver:function(){var tMat,b=this.m_body2,rX=(tMat=b.m_R).col1.x*this.m_localAnchor.x+tMat.col2.x*this.m_localAnchor.y,rY=tMat.col1.y*this.m_localAnchor.x+tMat.col2.y*this.m_localAnchor.y,invMass=b.m_invMass,invI=b.m_invI;this.K1.col1.x=invMass,this.K1.col2.x=0,this.K1.col1.y=0,this.K1.col2.y=invMass,this.K2.col1.x=invI*rY*rY,this.K2.col2.x=-invI*rX*rY,this.K2.col1.y=-invI*rX*rY,this.K2.col2.y=invI*rX*rX,this.K.SetM(this.K1),this.K.AddM(this.K2),this.K.col1.x+=this.m_gamma,this.K.col2.y+=this.m_gamma,this.K.Invert(this.m_ptpMass),this.m_C.x=b.m_position.x+rX-this.m_target.x,this.m_C.y=b.m_position.y+rY-this.m_target.y,b.m_angularVelocity*=.98;var PX=this.m_impulse.x,PY=this.m_impulse.y;b.m_linearVelocity.x+=invMass*PX,b.m_linearVelocity.y+=invMass*PY,b.m_angularVelocity+=invI*(rX*PY-rY*PX)},SolveVelocityConstraints:function(step){var tMat,body=this.m_body2,rX=(tMat=body.m_R).col1.x*this.m_localAnchor.x+tMat.col2.x*this.m_localAnchor.y,rY=tMat.col1.y*this.m_localAnchor.x+tMat.col2.y*this.m_localAnchor.y,CdotX=body.m_linearVelocity.x+-body.m_angularVelocity*rY,CdotY=body.m_linearVelocity.y+body.m_angularVelocity*rX;tMat=this.m_ptpMass;var tX=CdotX+this.m_beta*step.inv_dt*this.m_C.x+this.m_gamma*this.m_impulse.x,tY=CdotY+this.m_beta*step.inv_dt*this.m_C.y+this.m_gamma*this.m_impulse.y,impulseX=-(tMat.col1.x*tX+tMat.col2.x*tY),impulseY=-(tMat.col1.y*tX+tMat.col2.y*tY),oldImpulseX=this.m_impulse.x,oldImpulseY=this.m_impulse.y;this.m_impulse.x+=impulseX,this.m_impulse.y+=impulseY;var length=this.m_impulse.Length();length>step.dt*this.m_maxForce&&this.m_impulse.Multiply(step.dt*this.m_maxForce/length),impulseX=this.m_impulse.x-oldImpulseX,impulseY=this.m_impulse.y-oldImpulseY,body.m_linearVelocity.x+=body.m_invMass*impulseX,body.m_linearVelocity.y+=body.m_invMass*impulseY,body.m_angularVelocity+=body.m_invI*(rX*impulseY-rY*impulseX)},SolvePositionConstraints:function(){return!0},m_localAnchor:new b2Vec2,m_target:new b2Vec2,m_impulse:new b2Vec2,m_ptpMass:new b2Mat22,m_C:new b2Vec2,m_maxForce:null,m_beta:null,m_gamma:null});var b2MouseJointDef=Class.create();Object.extend(b2MouseJointDef.prototype,b2JointDef.prototype),Object.extend(b2MouseJointDef.prototype,{initialize:function(){this.type=b2Joint.e_unknownJoint,this.userData=null,this.body1=null,this.body2=null,this.collideConnected=!1,this.target=new b2Vec2,this.type=b2Joint.e_mouseJoint,this.maxForce=0,this.frequencyHz=5,this.dampingRatio=.7,this.timeStep=1/60},target:new b2Vec2,maxForce:null,frequencyHz:null,dampingRatio:null,timeStep:null});var b2PrismaticJoint=Class.create();Object.extend(b2PrismaticJoint.prototype,b2Joint.prototype),Object.extend(b2PrismaticJoint.prototype,{GetAnchor1:function(){var b1=this.m_body1,tVec=new b2Vec2;return tVec.SetV(this.m_localAnchor1),tVec.MulM(b1.m_R),tVec.Add(b1.m_position),tVec},GetAnchor2:function(){var b2=this.m_body2,tVec=new b2Vec2;return tVec.SetV(this.m_localAnchor2),tVec.MulM(b2.m_R),tVec.Add(b2.m_position),tVec},GetJointTranslation:function(){var tMat,b1=this.m_body1,b2=this.m_body2,r1X=(tMat=b1.m_R).col1.x*this.m_localAnchor1.x+tMat.col2.x*this.m_localAnchor1.y,r1Y=tMat.col1.y*this.m_localAnchor1.x+tMat.col2.y*this.m_localAnchor1.y,r2X=(tMat=b2.m_R).col1.x*this.m_localAnchor2.x+tMat.col2.x*this.m_localAnchor2.y,r2Y=tMat.col1.y*this.m_localAnchor2.x+tMat.col2.y*this.m_localAnchor2.y,p1X=b1.m_position.x+r1X,p1Y=b1.m_position.y+r1Y,dX=b2.m_position.x+r2X-p1X,dY=b2.m_position.y+r2Y-p1Y;return((tMat=b1.m_R).col1.x*this.m_localXAxis1.x+tMat.col2.x*this.m_localXAxis1.y)*dX+(tMat.col1.y*this.m_localXAxis1.x+tMat.col2.y*this.m_localXAxis1.y)*dY},GetJointSpeed:function(){var tMat,b1=this.m_body1,b2=this.m_body2,r1X=(tMat=b1.m_R).col1.x*this.m_localAnchor1.x+tMat.col2.x*this.m_localAnchor1.y,r1Y=tMat.col1.y*this.m_localAnchor1.x+tMat.col2.y*this.m_localAnchor1.y,r2X=(tMat=b2.m_R).col1.x*this.m_localAnchor2.x+tMat.col2.x*this.m_localAnchor2.y,r2Y=tMat.col1.y*this.m_localAnchor2.x+tMat.col2.y*this.m_localAnchor2.y,p1X=b1.m_position.x+r1X,p1Y=b1.m_position.y+r1Y,dX=b2.m_position.x+r2X-p1X,dY=b2.m_position.y+r2Y-p1Y,ax1X=(tMat=b1.m_R).col1.x*this.m_localXAxis1.x+tMat.col2.x*this.m_localXAxis1.y,ax1Y=tMat.col1.y*this.m_localXAxis1.x+tMat.col2.y*this.m_localXAxis1.y,v1=b1.m_linearVelocity,v2=b2.m_linearVelocity,w1=b1.m_angularVelocity,w2=b2.m_angularVelocity;return dX*(-w1*ax1Y)+dY*(w1*ax1X)+(ax1X*(v2.x+-w2*r2Y-v1.x- -w1*r1Y)+ax1Y*(v2.y+w2*r2X-v1.y-w1*r1X))},GetMotorForce:function(invTimeStep){return invTimeStep*this.m_motorImpulse},SetMotorSpeed:function(speed){this.m_motorSpeed=speed},SetMotorForce:function(force){this.m_maxMotorForce=force},GetReactionForce:function(invTimeStep){var tMat,tImp=invTimeStep*this.m_limitImpulse,ax1X=tImp*((tMat=this.m_body1.m_R).col1.x*this.m_localXAxis1.x+tMat.col2.x*this.m_localXAxis1.y),ax1Y=tImp*(tMat.col1.y*this.m_localXAxis1.x+tMat.col2.y*this.m_localXAxis1.y),ay1X=tImp*(tMat.col1.x*this.m_localYAxis1.x+tMat.col2.x*this.m_localYAxis1.y),ay1Y=tImp*(tMat.col1.y*this.m_localYAxis1.x+tMat.col2.y*this.m_localYAxis1.y);return new b2Vec2(ax1X+ay1X,ax1Y+ay1Y)},GetReactionTorque:function(invTimeStep){return invTimeStep*this.m_angularImpulse},initialize:function(def){var tMat,tX,tY;this.m_node1=new b2JointNode,this.m_node2=new b2JointNode,this.m_type=def.type,this.m_prev=null,this.m_next=null,this.m_body1=def.body1,this.m_body2=def.body2,this.m_collideConnected=def.collideConnected,this.m_islandFlag=!1,this.m_userData=def.userData,this.m_localAnchor1=new b2Vec2,this.m_localAnchor2=new b2Vec2,this.m_localXAxis1=new b2Vec2,this.m_localYAxis1=new b2Vec2,this.m_linearJacobian=new b2Jacobian,this.m_motorJacobian=new b2Jacobian,tMat=this.m_body1.m_R,tX=def.anchorPoint.x-this.m_body1.m_position.x,tY=def.anchorPoint.y-this.m_body1.m_position.y,this.m_localAnchor1.Set(tX*tMat.col1.x+tY*tMat.col1.y,tX*tMat.col2.x+tY*tMat.col2.y),tMat=this.m_body2.m_R,tX=def.anchorPoint.x-this.m_body2.m_position.x,tY=def.anchorPoint.y-this.m_body2.m_position.y,this.m_localAnchor2.Set(tX*tMat.col1.x+tY*tMat.col1.y,tX*tMat.col2.x+tY*tMat.col2.y),tMat=this.m_body1.m_R,tX=def.axis.x,tY=def.axis.y,this.m_localXAxis1.Set(tX*tMat.col1.x+tY*tMat.col1.y,tX*tMat.col2.x+tY*tMat.col2.y),this.m_localYAxis1.x=-this.m_localXAxis1.y,this.m_localYAxis1.y=this.m_localXAxis1.x,this.m_initialAngle=this.m_body2.m_rotation-this.m_body1.m_rotation,this.m_linearJacobian.SetZero(),this.m_linearMass=0,this.m_linearImpulse=0,this.m_angularMass=0,this.m_angularImpulse=0,this.m_motorJacobian.SetZero(),this.m_motorMass=0,this.m_motorImpulse=0,this.m_limitImpulse=0,this.m_limitPositionImpulse=0,this.m_lowerTranslation=def.lowerTranslation,this.m_upperTranslation=def.upperTranslation,this.m_maxMotorForce=def.motorForce,this.m_motorSpeed=def.motorSpeed,this.m_enableLimit=def.enableLimit,this.m_enableMotor=def.enableMotor},PrepareVelocitySolver:function(){var tMat,b1=this.m_body1,b2=this.m_body2,r1X=(tMat=b1.m_R).col1.x*this.m_localAnchor1.x+tMat.col2.x*this.m_localAnchor1.y,r1Y=tMat.col1.y*this.m_localAnchor1.x+tMat.col2.y*this.m_localAnchor1.y,r2X=(tMat=b2.m_R).col1.x*this.m_localAnchor2.x+tMat.col2.x*this.m_localAnchor2.y,r2Y=tMat.col1.y*this.m_localAnchor2.x+tMat.col2.y*this.m_localAnchor2.y,invMass1=b1.m_invMass,invMass2=b2.m_invMass,invI1=b1.m_invI,invI2=b2.m_invI,ay1X=(tMat=b1.m_R).col1.x*this.m_localYAxis1.x+tMat.col2.x*this.m_localYAxis1.y,ay1Y=tMat.col1.y*this.m_localYAxis1.x+tMat.col2.y*this.m_localYAxis1.y,eX=b2.m_position.x+r2X-b1.m_position.x,eY=b2.m_position.y+r2Y-b1.m_position.y;if(this.m_linearJacobian.linear1.x=-ay1X,this.m_linearJacobian.linear1.y=-ay1Y,this.m_linearJacobian.linear2.x=ay1X,this.m_linearJacobian.linear2.y=ay1Y,this.m_linearJacobian.angular1=-(eX*ay1Y-eY*ay1X),this.m_linearJacobian.angular2=r2X*ay1Y-r2Y*ay1X,this.m_linearMass=invMass1+invI1*this.m_linearJacobian.angular1*this.m_linearJacobian.angular1+invMass2+invI2*this.m_linearJacobian.angular2*this.m_linearJacobian.angular2,this.m_linearMass=1/this.m_linearMass,this.m_angularMass=1/(invI1+invI2),this.m_enableLimit||this.m_enableMotor){var ax1X=(tMat=b1.m_R).col1.x*this.m_localXAxis1.x+tMat.col2.x*this.m_localXAxis1.y,ax1Y=tMat.col1.y*this.m_localXAxis1.x+tMat.col2.y*this.m_localXAxis1.y;if(this.m_motorJacobian.linear1.x=-ax1X,this.m_motorJacobian.linear1.y=-ax1Y,this.m_motorJacobian.linear2.x=ax1X,this.m_motorJacobian.linear2.y=ax1Y,this.m_motorJacobian.angular1=-(eX*ax1Y-eY*ax1X),this.m_motorJacobian.angular2=r2X*ax1Y-r2Y*ax1X,this.m_motorMass=invMass1+invI1*this.m_motorJacobian.angular1*this.m_motorJacobian.angular1+invMass2+invI2*this.m_motorJacobian.angular2*this.m_motorJacobian.angular2,this.m_motorMass=1/this.m_motorMass,this.m_enableLimit){var jointTranslation=ax1X*(eX-r1X)+ax1Y*(eY-r1Y);b2Math.b2Abs(this.m_upperTranslation-this.m_lowerTranslation)<2*b2Settings.b2_linearSlop?this.m_limitState=b2Joint.e_equalLimits:jointTranslation<=this.m_lowerTranslation?(this.m_limitState!=b2Joint.e_atLowerLimit&&(this.m_limitImpulse=0),this.m_limitState=b2Joint.e_atLowerLimit):jointTranslation>=this.m_upperTranslation?(this.m_limitState!=b2Joint.e_atUpperLimit&&(this.m_limitImpulse=0),this.m_limitState=b2Joint.e_atUpperLimit):(this.m_limitState=b2Joint.e_inactiveLimit,this.m_limitImpulse=0)}}if(0==this.m_enableMotor&&(this.m_motorImpulse=0),0==this.m_enableLimit&&(this.m_limitImpulse=0),b2World.s_enableWarmStarting){var P1X=this.m_linearImpulse*this.m_linearJacobian.linear1.x+(this.m_motorImpulse+this.m_limitImpulse)*this.m_motorJacobian.linear1.x,P1Y=this.m_linearImpulse*this.m_linearJacobian.linear1.y+(this.m_motorImpulse+this.m_limitImpulse)*this.m_motorJacobian.linear1.y,P2X=this.m_linearImpulse*this.m_linearJacobian.linear2.x+(this.m_motorImpulse+this.m_limitImpulse)*this.m_motorJacobian.linear2.x,P2Y=this.m_linearImpulse*this.m_linearJacobian.linear2.y+(this.m_motorImpulse+this.m_limitImpulse)*this.m_motorJacobian.linear2.y,L1=this.m_linearImpulse*this.m_linearJacobian.angular1-this.m_angularImpulse+(this.m_motorImpulse+this.m_limitImpulse)*this.m_motorJacobian.angular1,L2=this.m_linearImpulse*this.m_linearJacobian.angular2+this.m_angularImpulse+(this.m_motorImpulse+this.m_limitImpulse)*this.m_motorJacobian.angular2;b1.m_linearVelocity.x+=invMass1*P1X,b1.m_linearVelocity.y+=invMass1*P1Y,b1.m_angularVelocity+=invI1*L1,b2.m_linearVelocity.x+=invMass2*P2X,b2.m_linearVelocity.y+=invMass2*P2Y,b2.m_angularVelocity+=invI2*L2}else this.m_linearImpulse=0,this.m_angularImpulse=0,this.m_limitImpulse=0,this.m_motorImpulse=0;this.m_limitPositionImpulse=0},SolveVelocityConstraints:function(step){var oldLimitImpulse,b1=this.m_body1,b2=this.m_body2,invMass1=b1.m_invMass,invMass2=b2.m_invMass,invI1=b1.m_invI,invI2=b2.m_invI,linearCdot=this.m_linearJacobian.Compute(b1.m_linearVelocity,b1.m_angularVelocity,b2.m_linearVelocity,b2.m_angularVelocity),linearImpulse=-this.m_linearMass*linearCdot;this.m_linearImpulse+=linearImpulse,b1.m_linearVelocity.x+=invMass1*linearImpulse*this.m_linearJacobian.linear1.x,b1.m_linearVelocity.y+=invMass1*linearImpulse*this.m_linearJacobian.linear1.y,b1.m_angularVelocity+=invI1*linearImpulse*this.m_linearJacobian.angular1,b2.m_linearVelocity.x+=invMass2*linearImpulse*this.m_linearJacobian.linear2.x,b2.m_linearVelocity.y+=invMass2*linearImpulse*this.m_linearJacobian.linear2.y,b2.m_angularVelocity+=invI2*linearImpulse*this.m_linearJacobian.angular2;var angularCdot=b2.m_angularVelocity-b1.m_angularVelocity,angularImpulse=-this.m_angularMass*angularCdot;if(this.m_angularImpulse+=angularImpulse,b1.m_angularVelocity-=invI1*angularImpulse,b2.m_angularVelocity+=invI2*angularImpulse,this.m_enableMotor&&this.m_limitState!=b2Joint.e_equalLimits){var motorCdot=this.m_motorJacobian.Compute(b1.m_linearVelocity,b1.m_angularVelocity,b2.m_linearVelocity,b2.m_angularVelocity)-this.m_motorSpeed,motorImpulse=-this.m_motorMass*motorCdot,oldMotorImpulse=this.m_motorImpulse;this.m_motorImpulse=b2Math.b2Clamp(this.m_motorImpulse+motorImpulse,-step.dt*this.m_maxMotorForce,step.dt*this.m_maxMotorForce),motorImpulse=this.m_motorImpulse-oldMotorImpulse,b1.m_linearVelocity.x+=invMass1*motorImpulse*this.m_motorJacobian.linear1.x,b1.m_linearVelocity.y+=invMass1*motorImpulse*this.m_motorJacobian.linear1.y,b1.m_angularVelocity+=invI1*motorImpulse*this.m_motorJacobian.angular1,b2.m_linearVelocity.x+=invMass2*motorImpulse*this.m_motorJacobian.linear2.x,b2.m_linearVelocity.y+=invMass2*motorImpulse*this.m_motorJacobian.linear2.y,b2.m_angularVelocity+=invI2*motorImpulse*this.m_motorJacobian.angular2}if(this.m_enableLimit&&this.m_limitState!=b2Joint.e_inactiveLimit){var limitCdot=this.m_motorJacobian.Compute(b1.m_linearVelocity,b1.m_angularVelocity,b2.m_linearVelocity,b2.m_angularVelocity),limitImpulse=-this.m_motorMass*limitCdot;this.m_limitState==b2Joint.e_equalLimits?this.m_limitImpulse+=limitImpulse:this.m_limitState==b2Joint.e_atLowerLimit?(oldLimitImpulse=this.m_limitImpulse,this.m_limitImpulse=b2Math.b2Max(this.m_limitImpulse+limitImpulse,0),limitImpulse=this.m_limitImpulse-oldLimitImpulse):this.m_limitState==b2Joint.e_atUpperLimit&&(oldLimitImpulse=this.m_limitImpulse,this.m_limitImpulse=b2Math.b2Min(this.m_limitImpulse+limitImpulse,0),limitImpulse=this.m_limitImpulse-oldLimitImpulse),b1.m_linearVelocity.x+=invMass1*limitImpulse*this.m_motorJacobian.linear1.x,b1.m_linearVelocity.y+=invMass1*limitImpulse*this.m_motorJacobian.linear1.y,b1.m_angularVelocity+=invI1*limitImpulse*this.m_motorJacobian.angular1,b2.m_linearVelocity.x+=invMass2*limitImpulse*this.m_motorJacobian.linear2.x,b2.m_linearVelocity.y+=invMass2*limitImpulse*this.m_motorJacobian.linear2.y,b2.m_angularVelocity+=invI2*limitImpulse*this.m_motorJacobian.angular2}},SolvePositionConstraints:function(){var limitC,oldLimitImpulse,tMat,b1=this.m_body1,b2=this.m_body2,invMass1=b1.m_invMass,invMass2=b2.m_invMass,invI1=b1.m_invI,invI2=b2.m_invI,r1X=(tMat=b1.m_R).col1.x*this.m_localAnchor1.x+tMat.col2.x*this.m_localAnchor1.y,r1Y=tMat.col1.y*this.m_localAnchor1.x+tMat.col2.y*this.m_localAnchor1.y,r2X=(tMat=b2.m_R).col1.x*this.m_localAnchor2.x+tMat.col2.x*this.m_localAnchor2.y,r2Y=tMat.col1.y*this.m_localAnchor2.x+tMat.col2.y*this.m_localAnchor2.y,p1X=b1.m_position.x+r1X,p1Y=b1.m_position.y+r1Y,p2X=b2.m_position.x+r2X,p2Y=b2.m_position.y+r2Y,dX=p2X-p1X,dY=p2Y-p1Y,linearC=((tMat=b1.m_R).col1.x*this.m_localYAxis1.x+tMat.col2.x*this.m_localYAxis1.y)*dX+(tMat.col1.y*this.m_localYAxis1.x+tMat.col2.y*this.m_localYAxis1.y)*dY;linearC=b2Math.b2Clamp(linearC,-b2Settings.b2_maxLinearCorrection,b2Settings.b2_maxLinearCorrection);var linearImpulse=-this.m_linearMass*linearC;b1.m_position.x+=invMass1*linearImpulse*this.m_linearJacobian.linear1.x,b1.m_position.y+=invMass1*linearImpulse*this.m_linearJacobian.linear1.y,b1.m_rotation+=invI1*linearImpulse*this.m_linearJacobian.angular1,b2.m_position.x+=invMass2*linearImpulse*this.m_linearJacobian.linear2.x,b2.m_position.y+=invMass2*linearImpulse*this.m_linearJacobian.linear2.y,b2.m_rotation+=invI2*linearImpulse*this.m_linearJacobian.angular2;var positionError=b2Math.b2Abs(linearC),angularC=b2.m_rotation-b1.m_rotation-this.m_initialAngle;angularC=b2Math.b2Clamp(angularC,-b2Settings.b2_maxAngularCorrection,b2Settings.b2_maxAngularCorrection);var angularImpulse=-this.m_angularMass*angularC;b1.m_rotation-=b1.m_invI*angularImpulse,b1.m_R.Set(b1.m_rotation),b2.m_rotation+=b2.m_invI*angularImpulse,b2.m_R.Set(b2.m_rotation);var angularError=b2Math.b2Abs(angularC);if(this.m_enableLimit&&this.m_limitState!=b2Joint.e_inactiveLimit){r1X=(tMat=b1.m_R).col1.x*this.m_localAnchor1.x+tMat.col2.x*this.m_localAnchor1.y,r1Y=tMat.col1.y*this.m_localAnchor1.x+tMat.col2.y*this.m_localAnchor1.y,r2X=(tMat=b2.m_R).col1.x*this.m_localAnchor2.x+tMat.col2.x*this.m_localAnchor2.y,r2Y=tMat.col1.y*this.m_localAnchor2.x+tMat.col2.y*this.m_localAnchor2.y,p1X=b1.m_position.x+r1X,p1Y=b1.m_position.y+r1Y,dX=(p2X=b2.m_position.x+r2X)-p1X,dY=(p2Y=b2.m_position.y+r2Y)-p1Y;var translation=((tMat=b1.m_R).col1.x*this.m_localXAxis1.x+tMat.col2.x*this.m_localXAxis1.y)*dX+(tMat.col1.y*this.m_localXAxis1.x+tMat.col2.y*this.m_localXAxis1.y)*dY,limitImpulse=0;this.m_limitState==b2Joint.e_equalLimits?(limitC=b2Math.b2Clamp(translation,-b2Settings.b2_maxLinearCorrection,b2Settings.b2_maxLinearCorrection),limitImpulse=-this.m_motorMass*limitC,positionError=b2Math.b2Max(positionError,b2Math.b2Abs(angularC))):this.m_limitState==b2Joint.e_atLowerLimit?(limitC=translation-this.m_lowerTranslation,positionError=b2Math.b2Max(positionError,-limitC),limitC=b2Math.b2Clamp(limitC+b2Settings.b2_linearSlop,-b2Settings.b2_maxLinearCorrection,0),limitImpulse=-this.m_motorMass*limitC,oldLimitImpulse=this.m_limitPositionImpulse,this.m_limitPositionImpulse=b2Math.b2Max(this.m_limitPositionImpulse+limitImpulse,0),limitImpulse=this.m_limitPositionImpulse-oldLimitImpulse):this.m_limitState==b2Joint.e_atUpperLimit&&(limitC=translation-this.m_upperTranslation,positionError=b2Math.b2Max(positionError,limitC),limitC=b2Math.b2Clamp(limitC-b2Settings.b2_linearSlop,0,b2Settings.b2_maxLinearCorrection),limitImpulse=-this.m_motorMass*limitC,oldLimitImpulse=this.m_limitPositionImpulse,this.m_limitPositionImpulse=b2Math.b2Min(this.m_limitPositionImpulse+limitImpulse,0),limitImpulse=this.m_limitPositionImpulse-oldLimitImpulse),b1.m_position.x+=invMass1*limitImpulse*this.m_motorJacobian.linear1.x,b1.m_position.y+=invMass1*limitImpulse*this.m_motorJacobian.linear1.y,b1.m_rotation+=invI1*limitImpulse*this.m_motorJacobian.angular1,b1.m_R.Set(b1.m_rotation),b2.m_position.x+=invMass2*limitImpulse*this.m_motorJacobian.linear2.x,b2.m_position.y+=invMass2*limitImpulse*this.m_motorJacobian.linear2.y,b2.m_rotation+=invI2*limitImpulse*this.m_motorJacobian.angular2,b2.m_R.Set(b2.m_rotation)}return positionError<=b2Settings.b2_linearSlop&&angularError<=b2Settings.b2_angularSlop},m_localAnchor1:new b2Vec2,m_localAnchor2:new b2Vec2,m_localXAxis1:new b2Vec2,m_localYAxis1:new b2Vec2,m_initialAngle:null,m_linearJacobian:new b2Jacobian,m_linearMass:null,m_linearImpulse:null,m_angularMass:null,m_angularImpulse:null,m_motorJacobian:new b2Jacobian,m_motorMass:null,m_motorImpulse:null,m_limitImpulse:null,m_limitPositionImpulse:null,m_lowerTranslation:null,m_upperTranslation:null,m_maxMotorForce:null,m_motorSpeed:null,m_enableLimit:null,m_enableMotor:null,m_limitState:0});var b2PrismaticJointDef=Class.create();Object.extend(b2PrismaticJointDef.prototype,b2JointDef.prototype),Object.extend(b2PrismaticJointDef.prototype,{initialize:function(){this.type=b2Joint.e_unknownJoint,this.userData=null,this.body1=null,this.body2=null,this.collideConnected=!1,this.type=b2Joint.e_prismaticJoint,this.anchorPoint=new b2Vec2(0,0),this.axis=new b2Vec2(0,0),this.lowerTranslation=0,this.upperTranslation=0,this.motorForce=0,this.motorSpeed=0,this.enableLimit=!1,this.enableMotor=!1},anchorPoint:null,axis:null,lowerTranslation:null,upperTranslation:null,motorForce:null,motorSpeed:null,enableLimit:null,enableMotor:null});var b2PulleyJoint=Class.create();Object.extend(b2PulleyJoint.prototype,b2Joint.prototype),Object.extend(b2PulleyJoint.prototype,{GetAnchor1:function(){var tMat=this.m_body1.m_R;return new b2Vec2(this.m_body1.m_position.x+(tMat.col1.x*this.m_localAnchor1.x+tMat.col2.x*this.m_localAnchor1.y),this.m_body1.m_position.y+(tMat.col1.y*this.m_localAnchor1.x+tMat.col2.y*this.m_localAnchor1.y))},GetAnchor2:function(){var tMat=this.m_body2.m_R;return new b2Vec2(this.m_body2.m_position.x+(tMat.col1.x*this.m_localAnchor2.x+tMat.col2.x*this.m_localAnchor2.y),this.m_body2.m_position.y+(tMat.col1.y*this.m_localAnchor2.x+tMat.col2.y*this.m_localAnchor2.y))},GetGroundPoint1:function(){return new b2Vec2(this.m_ground.m_position.x+this.m_groundAnchor1.x,this.m_ground.m_position.y+this.m_groundAnchor1.y)},GetGroundPoint2:function(){return new b2Vec2(this.m_ground.m_position.x+this.m_groundAnchor2.x,this.m_ground.m_position.y+this.m_groundAnchor2.y)},GetReactionForce:function(invTimeStep){return new b2Vec2},GetReactionTorque:function(invTimeStep){return 0},GetLength1:function(){var tMat;tMat=this.m_body1.m_R;var pX=this.m_body1.m_position.x+(tMat.col1.x*this.m_localAnchor1.x+tMat.col2.x*this.m_localAnchor1.y),pY=this.m_body1.m_position.y+(tMat.col1.y*this.m_localAnchor1.x+tMat.col2.y*this.m_localAnchor1.y),dX=pX-(this.m_ground.m_position.x+this.m_groundAnchor1.x),dY=pY-(this.m_ground.m_position.y+this.m_groundAnchor1.y);return Math.sqrt(dX*dX+dY*dY)},GetLength2:function(){var tMat;tMat=this.m_body2.m_R;var pX=this.m_body2.m_position.x+(tMat.col1.x*this.m_localAnchor2.x+tMat.col2.x*this.m_localAnchor2.y),pY=this.m_body2.m_position.y+(tMat.col1.y*this.m_localAnchor2.x+tMat.col2.y*this.m_localAnchor2.y),dX=pX-(this.m_ground.m_position.x+this.m_groundAnchor2.x),dY=pY-(this.m_ground.m_position.y+this.m_groundAnchor2.y);return Math.sqrt(dX*dX+dY*dY)},GetRatio:function(){return this.m_ratio},initialize:function(def){var tMat,tX,tY;this.m_node1=new b2JointNode,this.m_node2=new b2JointNode,this.m_type=def.type,this.m_prev=null,this.m_next=null,this.m_body1=def.body1,this.m_body2=def.body2,this.m_collideConnected=def.collideConnected,this.m_islandFlag=!1,this.m_userData=def.userData,this.m_groundAnchor1=new b2Vec2,this.m_groundAnchor2=new b2Vec2,this.m_localAnchor1=new b2Vec2,this.m_localAnchor2=new b2Vec2,this.m_u1=new b2Vec2,this.m_u2=new b2Vec2,this.m_ground=this.m_body1.m_world.m_groundBody,this.m_groundAnchor1.x=def.groundPoint1.x-this.m_ground.m_position.x,this.m_groundAnchor1.y=def.groundPoint1.y-this.m_ground.m_position.y,this.m_groundAnchor2.x=def.groundPoint2.x-this.m_ground.m_position.x,this.m_groundAnchor2.y=def.groundPoint2.y-this.m_ground.m_position.y,tMat=this.m_body1.m_R,tX=def.anchorPoint1.x-this.m_body1.m_position.x,tY=def.anchorPoint1.y-this.m_body1.m_position.y,this.m_localAnchor1.x=tX*tMat.col1.x+tY*tMat.col1.y,this.m_localAnchor1.y=tX*tMat.col2.x+tY*tMat.col2.y,tMat=this.m_body2.m_R,tX=def.anchorPoint2.x-this.m_body2.m_position.x,tY=def.anchorPoint2.y-this.m_body2.m_position.y,this.m_localAnchor2.x=tX*tMat.col1.x+tY*tMat.col1.y,this.m_localAnchor2.y=tX*tMat.col2.x+tY*tMat.col2.y,this.m_ratio=def.ratio,tX=def.groundPoint1.x-def.anchorPoint1.x,tY=def.groundPoint1.y-def.anchorPoint1.y;var d1Len=Math.sqrt(tX*tX+tY*tY);tX=def.groundPoint2.x-def.anchorPoint2.x,tY=def.groundPoint2.y-def.anchorPoint2.y;var d2Len=Math.sqrt(tX*tX+tY*tY),length1=b2Math.b2Max(.5*b2PulleyJoint.b2_minPulleyLength,d1Len),length2=b2Math.b2Max(.5*b2PulleyJoint.b2_minPulleyLength,d2Len);this.m_constant=length1+this.m_ratio*length2,this.m_maxLength1=b2Math.b2Clamp(def.maxLength1,length1,this.m_constant-this.m_ratio*b2PulleyJoint.b2_minPulleyLength),this.m_maxLength2=b2Math.b2Clamp(def.maxLength2,length2,(this.m_constant-b2PulleyJoint.b2_minPulleyLength)/this.m_ratio),this.m_pulleyImpulse=0,this.m_limitImpulse1=0,this.m_limitImpulse2=0},PrepareVelocitySolver:function(){var tMat,b1=this.m_body1,b2=this.m_body2,r1X=(tMat=b1.m_R).col1.x*this.m_localAnchor1.x+tMat.col2.x*this.m_localAnchor1.y,r1Y=tMat.col1.y*this.m_localAnchor1.x+tMat.col2.y*this.m_localAnchor1.y,r2X=(tMat=b2.m_R).col1.x*this.m_localAnchor2.x+tMat.col2.x*this.m_localAnchor2.y,r2Y=tMat.col1.y*this.m_localAnchor2.x+tMat.col2.y*this.m_localAnchor2.y,p1X=b1.m_position.x+r1X,p1Y=b1.m_position.y+r1Y,p2X=b2.m_position.x+r2X,p2Y=b2.m_position.y+r2Y,s1X=this.m_ground.m_position.x+this.m_groundAnchor1.x,s1Y=this.m_ground.m_position.y+this.m_groundAnchor1.y,s2X=this.m_ground.m_position.x+this.m_groundAnchor2.x,s2Y=this.m_ground.m_position.y+this.m_groundAnchor2.y;this.m_u1.Set(p1X-s1X,p1Y-s1Y),this.m_u2.Set(p2X-s2X,p2Y-s2Y);var length1=this.m_u1.Length(),length2=this.m_u2.Length();length1>b2Settings.b2_linearSlop?this.m_u1.Multiply(1/length1):this.m_u1.SetZero(),length2>b2Settings.b2_linearSlop?this.m_u2.Multiply(1/length2):this.m_u2.SetZero(),length1<this.m_maxLength1?(this.m_limitState1=b2Joint.e_inactiveLimit,this.m_limitImpulse1=0):(this.m_limitState1=b2Joint.e_atUpperLimit,this.m_limitPositionImpulse1=0),length2<this.m_maxLength2?(this.m_limitState2=b2Joint.e_inactiveLimit,this.m_limitImpulse2=0):(this.m_limitState2=b2Joint.e_atUpperLimit,this.m_limitPositionImpulse2=0);var cr1u1=r1X*this.m_u1.y-r1Y*this.m_u1.x,cr2u2=r2X*this.m_u2.y-r2Y*this.m_u2.x;this.m_limitMass1=b1.m_invMass+b1.m_invI*cr1u1*cr1u1,this.m_limitMass2=b2.m_invMass+b2.m_invI*cr2u2*cr2u2,this.m_pulleyMass=this.m_limitMass1+this.m_ratio*this.m_ratio*this.m_limitMass2,this.m_limitMass1=1/this.m_limitMass1,this.m_limitMass2=1/this.m_limitMass2,this.m_pulleyMass=1/this.m_pulleyMass;var P1X=(-this.m_pulleyImpulse-this.m_limitImpulse1)*this.m_u1.x,P1Y=(-this.m_pulleyImpulse-this.m_limitImpulse1)*this.m_u1.y,P2X=(-this.m_ratio*this.m_pulleyImpulse-this.m_limitImpulse2)*this.m_u2.x,P2Y=(-this.m_ratio*this.m_pulleyImpulse-this.m_limitImpulse2)*this.m_u2.y;b1.m_linearVelocity.x+=b1.m_invMass*P1X,b1.m_linearVelocity.y+=b1.m_invMass*P1Y,b1.m_angularVelocity+=b1.m_invI*(r1X*P1Y-r1Y*P1X),b2.m_linearVelocity.x+=b2.m_invMass*P2X,b2.m_linearVelocity.y+=b2.m_invMass*P2Y,b2.m_angularVelocity+=b2.m_invI*(r2X*P2Y-r2Y*P2X)},SolveVelocityConstraints:function(step){var tMat,v1X,v1Y,v2X,v2Y,P1X,P1Y,P2X,P2Y,Cdot,impulse,oldLimitImpulse,b1=this.m_body1,b2=this.m_body2,r1X=(tMat=b1.m_R).col1.x*this.m_localAnchor1.x+tMat.col2.x*this.m_localAnchor1.y,r1Y=tMat.col1.y*this.m_localAnchor1.x+tMat.col2.y*this.m_localAnchor1.y,r2X=(tMat=b2.m_R).col1.x*this.m_localAnchor2.x+tMat.col2.x*this.m_localAnchor2.y,r2Y=tMat.col1.y*this.m_localAnchor2.x+tMat.col2.y*this.m_localAnchor2.y;v1X=b1.m_linearVelocity.x+-b1.m_angularVelocity*r1Y,v1Y=b1.m_linearVelocity.y+b1.m_angularVelocity*r1X,v2X=b2.m_linearVelocity.x+-b2.m_angularVelocity*r2Y,v2Y=b2.m_linearVelocity.y+b2.m_angularVelocity*r2X,Cdot=-(this.m_u1.x*v1X+this.m_u1.y*v1Y)-this.m_ratio*(this.m_u2.x*v2X+this.m_u2.y*v2Y),impulse=-this.m_pulleyMass*Cdot,this.m_pulleyImpulse+=impulse,P1X=-impulse*this.m_u1.x,P1Y=-impulse*this.m_u1.y,P2X=-this.m_ratio*impulse*this.m_u2.x,P2Y=-this.m_ratio*impulse*this.m_u2.y,b1.m_linearVelocity.x+=b1.m_invMass*P1X,b1.m_linearVelocity.y+=b1.m_invMass*P1Y,b1.m_angularVelocity+=b1.m_invI*(r1X*P1Y-r1Y*P1X),b2.m_linearVelocity.x+=b2.m_invMass*P2X,b2.m_linearVelocity.y+=b2.m_invMass*P2Y,b2.m_angularVelocity+=b2.m_invI*(r2X*P2Y-r2Y*P2X),this.m_limitState1==b2Joint.e_atUpperLimit&&(v1X=b1.m_linearVelocity.x+-b1.m_angularVelocity*r1Y,v1Y=b1.m_linearVelocity.y+b1.m_angularVelocity*r1X,Cdot=-(this.m_u1.x*v1X+this.m_u1.y*v1Y),impulse=-this.m_limitMass1*Cdot,oldLimitImpulse=this.m_limitImpulse1,this.m_limitImpulse1=b2Math.b2Max(0,this.m_limitImpulse1+impulse),P1X=-(impulse=this.m_limitImpulse1-oldLimitImpulse)*this.m_u1.x,P1Y=-impulse*this.m_u1.y,b1.m_linearVelocity.x+=b1.m_invMass*P1X,b1.m_linearVelocity.y+=b1.m_invMass*P1Y,b1.m_angularVelocity+=b1.m_invI*(r1X*P1Y-r1Y*P1X)),this.m_limitState2==b2Joint.e_atUpperLimit&&(v2X=b2.m_linearVelocity.x+-b2.m_angularVelocity*r2Y,v2Y=b2.m_linearVelocity.y+b2.m_angularVelocity*r2X,Cdot=-(this.m_u2.x*v2X+this.m_u2.y*v2Y),impulse=-this.m_limitMass2*Cdot,oldLimitImpulse=this.m_limitImpulse2,this.m_limitImpulse2=b2Math.b2Max(0,this.m_limitImpulse2+impulse),P2X=-(impulse=this.m_limitImpulse2-oldLimitImpulse)*this.m_u2.x,P2Y=-impulse*this.m_u2.y,b2.m_linearVelocity.x+=b2.m_invMass*P2X,b2.m_linearVelocity.y+=b2.m_invMass*P2Y,b2.m_angularVelocity+=b2.m_invI*(r2X*P2Y-r2Y*P2X))},SolvePositionConstraints:function(){var tMat,r1X,r1Y,r2X,r2Y,p1X,p1Y,p2X,p2Y,length1,length2,C,impulse,oldLimitPositionImpulse,b1=this.m_body1,b2=this.m_body2,s1X=this.m_ground.m_position.x+this.m_groundAnchor1.x,s1Y=this.m_ground.m_position.y+this.m_groundAnchor1.y,s2X=this.m_ground.m_position.x+this.m_groundAnchor2.x,s2Y=this.m_ground.m_position.y+this.m_groundAnchor2.y,linearError=0;return r1X=(tMat=b1.m_R).col1.x*this.m_localAnchor1.x+tMat.col2.x*this.m_localAnchor1.y,r1Y=tMat.col1.y*this.m_localAnchor1.x+tMat.col2.y*this.m_localAnchor1.y,r2X=(tMat=b2.m_R).col1.x*this.m_localAnchor2.x+tMat.col2.x*this.m_localAnchor2.y,r2Y=tMat.col1.y*this.m_localAnchor2.x+tMat.col2.y*this.m_localAnchor2.y,p1X=b1.m_position.x+r1X,p1Y=b1.m_position.y+r1Y,p2X=b2.m_position.x+r2X,p2Y=b2.m_position.y+r2Y,this.m_u1.Set(p1X-s1X,p1Y-s1Y),this.m_u2.Set(p2X-s2X,p2Y-s2Y),length1=this.m_u1.Length(),length2=this.m_u2.Length(),length1>b2Settings.b2_linearSlop?this.m_u1.Multiply(1/length1):this.m_u1.SetZero(),length2>b2Settings.b2_linearSlop?this.m_u2.Multiply(1/length2):this.m_u2.SetZero(),C=this.m_constant-length1-this.m_ratio*length2,linearError=b2Math.b2Max(linearError,Math.abs(C)),C=b2Math.b2Clamp(C,-b2Settings.b2_maxLinearCorrection,b2Settings.b2_maxLinearCorrection),p1X=-(impulse=-this.m_pulleyMass*C)*this.m_u1.x,p1Y=-impulse*this.m_u1.y,p2X=-this.m_ratio*impulse*this.m_u2.x,p2Y=-this.m_ratio*impulse*this.m_u2.y,b1.m_position.x+=b1.m_invMass*p1X,b1.m_position.y+=b1.m_invMass*p1Y,b1.m_rotation+=b1.m_invI*(r1X*p1Y-r1Y*p1X),b2.m_position.x+=b2.m_invMass*p2X,b2.m_position.y+=b2.m_invMass*p2Y,b2.m_rotation+=b2.m_invI*(r2X*p2Y-r2Y*p2X),b1.m_R.Set(b1.m_rotation),b2.m_R.Set(b2.m_rotation),this.m_limitState1==b2Joint.e_atUpperLimit&&(r1X=(tMat=b1.m_R).col1.x*this.m_localAnchor1.x+tMat.col2.x*this.m_localAnchor1.y,r1Y=tMat.col1.y*this.m_localAnchor1.x+tMat.col2.y*this.m_localAnchor1.y,p1X=b1.m_position.x+r1X,p1Y=b1.m_position.y+r1Y,this.m_u1.Set(p1X-s1X,p1Y-s1Y),(length1=this.m_u1.Length())>b2Settings.b2_linearSlop?(this.m_u1.x*=1/length1,this.m_u1.y*=1/length1):this.m_u1.SetZero(),C=this.m_maxLength1-length1,linearError=b2Math.b2Max(linearError,-C),C=b2Math.b2Clamp(C+b2Settings.b2_linearSlop,-b2Settings.b2_maxLinearCorrection,0),impulse=-this.m_limitMass1*C,oldLimitPositionImpulse=this.m_limitPositionImpulse1,this.m_limitPositionImpulse1=b2Math.b2Max(0,this.m_limitPositionImpulse1+impulse),p1X=-(impulse=this.m_limitPositionImpulse1-oldLimitPositionImpulse)*this.m_u1.x,p1Y=-impulse*this.m_u1.y,b1.m_position.x+=b1.m_invMass*p1X,b1.m_position.y+=b1.m_invMass*p1Y,b1.m_rotation+=b1.m_invI*(r1X*p1Y-r1Y*p1X),b1.m_R.Set(b1.m_rotation)),this.m_limitState2==b2Joint.e_atUpperLimit&&(r2X=(tMat=b2.m_R).col1.x*this.m_localAnchor2.x+tMat.col2.x*this.m_localAnchor2.y,r2Y=tMat.col1.y*this.m_localAnchor2.x+tMat.col2.y*this.m_localAnchor2.y,p2X=b2.m_position.x+r2X,p2Y=b2.m_position.y+r2Y,this.m_u2.Set(p2X-s2X,p2Y-s2Y),(length2=this.m_u2.Length())>b2Settings.b2_linearSlop?(this.m_u2.x*=1/length2,this.m_u2.y*=1/length2):this.m_u2.SetZero(),C=this.m_maxLength2-length2,linearError=b2Math.b2Max(linearError,-C),C=b2Math.b2Clamp(C+b2Settings.b2_linearSlop,-b2Settings.b2_maxLinearCorrection,0),impulse=-this.m_limitMass2*C,oldLimitPositionImpulse=this.m_limitPositionImpulse2,this.m_limitPositionImpulse2=b2Math.b2Max(0,this.m_limitPositionImpulse2+impulse),p2X=-(impulse=this.m_limitPositionImpulse2-oldLimitPositionImpulse)*this.m_u2.x,p2Y=-impulse*this.m_u2.y,b2.m_position.x+=b2.m_invMass*p2X,b2.m_position.y+=b2.m_invMass*p2Y,b2.m_rotation+=b2.m_invI*(r2X*p2Y-r2Y*p2X),b2.m_R.Set(b2.m_rotation)),linearError<b2Settings.b2_linearSlop},m_ground:null,m_groundAnchor1:new b2Vec2,m_groundAnchor2:new b2Vec2,m_localAnchor1:new b2Vec2,m_localAnchor2:new b2Vec2,m_u1:new b2Vec2,m_u2:new b2Vec2,m_constant:null,m_ratio:null,m_maxLength1:null,m_maxLength2:null,m_pulleyMass:null,m_limitMass1:null,m_limitMass2:null,m_pulleyImpulse:null,m_limitImpulse1:null,m_limitImpulse2:null,m_limitPositionImpulse1:null,m_limitPositionImpulse2:null,m_limitState1:0,m_limitState2:0}),b2PulleyJoint.b2_minPulleyLength=b2Settings.b2_lengthUnitsPerMeter;var b2PulleyJointDef=Class.create();Object.extend(b2PulleyJointDef.prototype,b2JointDef.prototype),Object.extend(b2PulleyJointDef.prototype,{initialize:function(){this.type=b2Joint.e_unknownJoint,this.userData=null,this.body1=null,this.body2=null,this.collideConnected=!1,this.groundPoint1=new b2Vec2,this.groundPoint2=new b2Vec2,this.anchorPoint1=new b2Vec2,this.anchorPoint2=new b2Vec2,this.type=b2Joint.e_pulleyJoint,this.groundPoint1.Set(-1,1),this.groundPoint2.Set(1,1),this.anchorPoint1.Set(-1,0),this.anchorPoint2.Set(1,0),this.maxLength1=.5*b2PulleyJoint.b2_minPulleyLength,this.maxLength2=.5*b2PulleyJoint.b2_minPulleyLength,this.ratio=1,this.collideConnected=!0},groundPoint1:new b2Vec2,groundPoint2:new b2Vec2,anchorPoint1:new b2Vec2,anchorPoint2:new b2Vec2,maxLength1:null,maxLength2:null,ratio:null});var b2RevoluteJoint=Class.create();Object.extend(b2RevoluteJoint.prototype,b2Joint.prototype),Object.extend(b2RevoluteJoint.prototype,{GetAnchor1:function(){var tMat=this.m_body1.m_R;return new b2Vec2(this.m_body1.m_position.x+(tMat.col1.x*this.m_localAnchor1.x+tMat.col2.x*this.m_localAnchor1.y),this.m_body1.m_position.y+(tMat.col1.y*this.m_localAnchor1.x+tMat.col2.y*this.m_localAnchor1.y))},GetAnchor2:function(){var tMat=this.m_body2.m_R;return new b2Vec2(this.m_body2.m_position.x+(tMat.col1.x*this.m_localAnchor2.x+tMat.col2.x*this.m_localAnchor2.y),this.m_body2.m_position.y+(tMat.col1.y*this.m_localAnchor2.x+tMat.col2.y*this.m_localAnchor2.y))},GetJointAngle:function(){return this.m_body2.m_rotation-this.m_body1.m_rotation},GetJointSpeed:function(){return this.m_body2.m_angularVelocity-this.m_body1.m_angularVelocity},GetMotorTorque:function(invTimeStep){return invTimeStep*this.m_motorImpulse},SetMotorSpeed:function(speed){this.m_motorSpeed=speed},SetMotorTorque:function(torque){this.m_maxMotorTorque=torque},GetReactionForce:function(invTimeStep){var tVec=this.m_ptpImpulse.Copy();return tVec.Multiply(invTimeStep),tVec},GetReactionTorque:function(invTimeStep){return invTimeStep*this.m_limitImpulse},initialize:function(def){var tMat,tX,tY;this.m_node1=new b2JointNode,this.m_node2=new b2JointNode,this.m_type=def.type,this.m_prev=null,this.m_next=null,this.m_body1=def.body1,this.m_body2=def.body2,this.m_collideConnected=def.collideConnected,this.m_islandFlag=!1,this.m_userData=def.userData,this.K=new b2Mat22,this.K1=new b2Mat22,this.K2=new b2Mat22,this.K3=new b2Mat22,this.m_localAnchor1=new b2Vec2,this.m_localAnchor2=new b2Vec2,this.m_ptpImpulse=new b2Vec2,this.m_ptpMass=new b2Mat22,tMat=this.m_body1.m_R,tX=def.anchorPoint.x-this.m_body1.m_position.x,tY=def.anchorPoint.y-this.m_body1.m_position.y,this.m_localAnchor1.x=tX*tMat.col1.x+tY*tMat.col1.y,this.m_localAnchor1.y=tX*tMat.col2.x+tY*tMat.col2.y,tMat=this.m_body2.m_R,tX=def.anchorPoint.x-this.m_body2.m_position.x,tY=def.anchorPoint.y-this.m_body2.m_position.y,this.m_localAnchor2.x=tX*tMat.col1.x+tY*tMat.col1.y,this.m_localAnchor2.y=tX*tMat.col2.x+tY*tMat.col2.y,this.m_intialAngle=this.m_body2.m_rotation-this.m_body1.m_rotation,this.m_ptpImpulse.Set(0,0),this.m_motorImpulse=0,this.m_limitImpulse=0,this.m_limitPositionImpulse=0,this.m_lowerAngle=def.lowerAngle,this.m_upperAngle=def.upperAngle,this.m_maxMotorTorque=def.motorTorque,this.m_motorSpeed=def.motorSpeed,this.m_enableLimit=def.enableLimit,this.m_enableMotor=def.enableMotor},K:new b2Mat22,K1:new b2Mat22,K2:new b2Mat22,K3:new b2Mat22,PrepareVelocitySolver:function(){var tMat,b1=this.m_body1,b2=this.m_body2,r1X=(tMat=b1.m_R).col1.x*this.m_localAnchor1.x+tMat.col2.x*this.m_localAnchor1.y,r1Y=tMat.col1.y*this.m_localAnchor1.x+tMat.col2.y*this.m_localAnchor1.y,r2X=(tMat=b2.m_R).col1.x*this.m_localAnchor2.x+tMat.col2.x*this.m_localAnchor2.y,r2Y=tMat.col1.y*this.m_localAnchor2.x+tMat.col2.y*this.m_localAnchor2.y,invMass1=b1.m_invMass,invMass2=b2.m_invMass,invI1=b1.m_invI,invI2=b2.m_invI;if(this.K1.col1.x=invMass1+invMass2,this.K1.col2.x=0,this.K1.col1.y=0,this.K1.col2.y=invMass1+invMass2,this.K2.col1.x=invI1*r1Y*r1Y,this.K2.col2.x=-invI1*r1X*r1Y,this.K2.col1.y=-invI1*r1X*r1Y,this.K2.col2.y=invI1*r1X*r1X,this.K3.col1.x=invI2*r2Y*r2Y,this.K3.col2.x=-invI2*r2X*r2Y,this.K3.col1.y=-invI2*r2X*r2Y,this.K3.col2.y=invI2*r2X*r2X,this.K.SetM(this.K1),this.K.AddM(this.K2),this.K.AddM(this.K3),this.K.Invert(this.m_ptpMass),this.m_motorMass=1/(invI1+invI2),0==this.m_enableMotor&&(this.m_motorImpulse=0),this.m_enableLimit){var jointAngle=b2.m_rotation-b1.m_rotation-this.m_intialAngle;b2Math.b2Abs(this.m_upperAngle-this.m_lowerAngle)<2*b2Settings.b2_angularSlop?this.m_limitState=b2Joint.e_equalLimits:jointAngle<=this.m_lowerAngle?(this.m_limitState!=b2Joint.e_atLowerLimit&&(this.m_limitImpulse=0),this.m_limitState=b2Joint.e_atLowerLimit):jointAngle>=this.m_upperAngle?(this.m_limitState!=b2Joint.e_atUpperLimit&&(this.m_limitImpulse=0),this.m_limitState=b2Joint.e_atUpperLimit):(this.m_limitState=b2Joint.e_inactiveLimit,this.m_limitImpulse=0)}else this.m_limitImpulse=0;b2World.s_enableWarmStarting?(b1.m_linearVelocity.x-=invMass1*this.m_ptpImpulse.x,b1.m_linearVelocity.y-=invMass1*this.m_ptpImpulse.y,b1.m_angularVelocity-=invI1*(r1X*this.m_ptpImpulse.y-r1Y*this.m_ptpImpulse.x+this.m_motorImpulse+this.m_limitImpulse),b2.m_linearVelocity.x+=invMass2*this.m_ptpImpulse.x,b2.m_linearVelocity.y+=invMass2*this.m_ptpImpulse.y,b2.m_angularVelocity+=invI2*(r2X*this.m_ptpImpulse.y-r2Y*this.m_ptpImpulse.x+this.m_motorImpulse+this.m_limitImpulse)):(this.m_ptpImpulse.SetZero(),this.m_motorImpulse=0,this.m_limitImpulse=0),this.m_limitPositionImpulse=0},SolveVelocityConstraints:function(step){var tMat,oldLimitImpulse,b1=this.m_body1,b2=this.m_body2,r1X=(tMat=b1.m_R).col1.x*this.m_localAnchor1.x+tMat.col2.x*this.m_localAnchor1.y,r1Y=tMat.col1.y*this.m_localAnchor1.x+tMat.col2.y*this.m_localAnchor1.y,r2X=(tMat=b2.m_R).col1.x*this.m_localAnchor2.x+tMat.col2.x*this.m_localAnchor2.y,r2Y=tMat.col1.y*this.m_localAnchor2.x+tMat.col2.y*this.m_localAnchor2.y,ptpCdotX=b2.m_linearVelocity.x+-b2.m_angularVelocity*r2Y-b1.m_linearVelocity.x- -b1.m_angularVelocity*r1Y,ptpCdotY=b2.m_linearVelocity.y+b2.m_angularVelocity*r2X-b1.m_linearVelocity.y-b1.m_angularVelocity*r1X,ptpImpulseX=-(this.m_ptpMass.col1.x*ptpCdotX+this.m_ptpMass.col2.x*ptpCdotY),ptpImpulseY=-(this.m_ptpMass.col1.y*ptpCdotX+this.m_ptpMass.col2.y*ptpCdotY);if(this.m_ptpImpulse.x+=ptpImpulseX,this.m_ptpImpulse.y+=ptpImpulseY,b1.m_linearVelocity.x-=b1.m_invMass*ptpImpulseX,b1.m_linearVelocity.y-=b1.m_invMass*ptpImpulseY,b1.m_angularVelocity-=b1.m_invI*(r1X*ptpImpulseY-r1Y*ptpImpulseX),b2.m_linearVelocity.x+=b2.m_invMass*ptpImpulseX,b2.m_linearVelocity.y+=b2.m_invMass*ptpImpulseY,b2.m_angularVelocity+=b2.m_invI*(r2X*ptpImpulseY-r2Y*ptpImpulseX),this.m_enableMotor&&this.m_limitState!=b2Joint.e_equalLimits){var motorCdot=b2.m_angularVelocity-b1.m_angularVelocity-this.m_motorSpeed,motorImpulse=-this.m_motorMass*motorCdot,oldMotorImpulse=this.m_motorImpulse;this.m_motorImpulse=b2Math.b2Clamp(this.m_motorImpulse+motorImpulse,-step.dt*this.m_maxMotorTorque,step.dt*this.m_maxMotorTorque),motorImpulse=this.m_motorImpulse-oldMotorImpulse,b1.m_angularVelocity-=b1.m_invI*motorImpulse,b2.m_angularVelocity+=b2.m_invI*motorImpulse}if(this.m_enableLimit&&this.m_limitState!=b2Joint.e_inactiveLimit){var limitCdot=b2.m_angularVelocity-b1.m_angularVelocity,limitImpulse=-this.m_motorMass*limitCdot;this.m_limitState==b2Joint.e_equalLimits?this.m_limitImpulse+=limitImpulse:this.m_limitState==b2Joint.e_atLowerLimit?(oldLimitImpulse=this.m_limitImpulse,this.m_limitImpulse=b2Math.b2Max(this.m_limitImpulse+limitImpulse,0),limitImpulse=this.m_limitImpulse-oldLimitImpulse):this.m_limitState==b2Joint.e_atUpperLimit&&(oldLimitImpulse=this.m_limitImpulse,this.m_limitImpulse=b2Math.b2Min(this.m_limitImpulse+limitImpulse,0),limitImpulse=this.m_limitImpulse-oldLimitImpulse),b1.m_angularVelocity-=b1.m_invI*limitImpulse,b2.m_angularVelocity+=b2.m_invI*limitImpulse}},SolvePositionConstraints:function(){var oldLimitImpulse,limitC,positionError,tMat,b1=this.m_body1,b2=this.m_body2,r1X=(tMat=b1.m_R).col1.x*this.m_localAnchor1.x+tMat.col2.x*this.m_localAnchor1.y,r1Y=tMat.col1.y*this.m_localAnchor1.x+tMat.col2.y*this.m_localAnchor1.y,r2X=(tMat=b2.m_R).col1.x*this.m_localAnchor2.x+tMat.col2.x*this.m_localAnchor2.y,r2Y=tMat.col1.y*this.m_localAnchor2.x+tMat.col2.y*this.m_localAnchor2.y,p1X=b1.m_position.x+r1X,p1Y=b1.m_position.y+r1Y,ptpCX=b2.m_position.x+r2X-p1X,ptpCY=b2.m_position.y+r2Y-p1Y;positionError=Math.sqrt(ptpCX*ptpCX+ptpCY*ptpCY);var invMass1=b1.m_invMass,invMass2=b2.m_invMass,invI1=b1.m_invI,invI2=b2.m_invI;this.K1.col1.x=invMass1+invMass2,this.K1.col2.x=0,this.K1.col1.y=0,this.K1.col2.y=invMass1+invMass2,this.K2.col1.x=invI1*r1Y*r1Y,this.K2.col2.x=-invI1*r1X*r1Y,this.K2.col1.y=-invI1*r1X*r1Y,this.K2.col2.y=invI1*r1X*r1X,this.K3.col1.x=invI2*r2Y*r2Y,this.K3.col2.x=-invI2*r2X*r2Y,this.K3.col1.y=-invI2*r2X*r2Y,this.K3.col2.y=invI2*r2X*r2X,this.K.SetM(this.K1),this.K.AddM(this.K2),this.K.AddM(this.K3),this.K.Solve(b2RevoluteJoint.tImpulse,-ptpCX,-ptpCY);var impulseX=b2RevoluteJoint.tImpulse.x,impulseY=b2RevoluteJoint.tImpulse.y;b1.m_position.x-=b1.m_invMass*impulseX,b1.m_position.y-=b1.m_invMass*impulseY,b1.m_rotation-=b1.m_invI*(r1X*impulseY-r1Y*impulseX),b1.m_R.Set(b1.m_rotation),b2.m_position.x+=b2.m_invMass*impulseX,b2.m_position.y+=b2.m_invMass*impulseY,b2.m_rotation+=b2.m_invI*(r2X*impulseY-r2Y*impulseX),b2.m_R.Set(b2.m_rotation);var angularError=0;if(this.m_enableLimit&&this.m_limitState!=b2Joint.e_inactiveLimit){var angle=b2.m_rotation-b1.m_rotation-this.m_intialAngle,limitImpulse=0;this.m_limitState==b2Joint.e_equalLimits?(limitC=b2Math.b2Clamp(angle,-b2Settings.b2_maxAngularCorrection,b2Settings.b2_maxAngularCorrection),limitImpulse=-this.m_motorMass*limitC,angularError=b2Math.b2Abs(limitC)):this.m_limitState==b2Joint.e_atLowerLimit?(limitC=angle-this.m_lowerAngle,angularError=b2Math.b2Max(0,-limitC),limitC=b2Math.b2Clamp(limitC+b2Settings.b2_angularSlop,-b2Settings.b2_maxAngularCorrection,0),limitImpulse=-this.m_motorMass*limitC,oldLimitImpulse=this.m_limitPositionImpulse,this.m_limitPositionImpulse=b2Math.b2Max(this.m_limitPositionImpulse+limitImpulse,0),limitImpulse=this.m_limitPositionImpulse-oldLimitImpulse):this.m_limitState==b2Joint.e_atUpperLimit&&(limitC=angle-this.m_upperAngle,angularError=b2Math.b2Max(0,limitC),limitC=b2Math.b2Clamp(limitC-b2Settings.b2_angularSlop,0,b2Settings.b2_maxAngularCorrection),limitImpulse=-this.m_motorMass*limitC,oldLimitImpulse=this.m_limitPositionImpulse,this.m_limitPositionImpulse=b2Math.b2Min(this.m_limitPositionImpulse+limitImpulse,0),limitImpulse=this.m_limitPositionImpulse-oldLimitImpulse),b1.m_rotation-=b1.m_invI*limitImpulse,b1.m_R.Set(b1.m_rotation),b2.m_rotation+=b2.m_invI*limitImpulse,b2.m_R.Set(b2.m_rotation)}return positionError<=b2Settings.b2_linearSlop&&angularError<=b2Settings.b2_angularSlop},m_localAnchor1:new b2Vec2,m_localAnchor2:new b2Vec2,m_ptpImpulse:new b2Vec2,m_motorImpulse:null,m_limitImpulse:null,m_limitPositionImpulse:null,m_ptpMass:new b2Mat22,m_motorMass:null,m_intialAngle:null,m_lowerAngle:null,m_upperAngle:null,m_maxMotorTorque:null,m_motorSpeed:null,m_enableLimit:null,m_enableMotor:null,m_limitState:0}),b2RevoluteJoint.tImpulse=new b2Vec2;var b2RevoluteJointDef=Class.create();Object.extend(b2RevoluteJointDef.prototype,b2JointDef.prototype),Object.extend(b2RevoluteJointDef.prototype,{initialize:function(){this.type=b2Joint.e_unknownJoint,this.userData=null,this.body1=null,this.body2=null,this.collideConnected=!1,this.type=b2Joint.e_revoluteJoint,this.anchorPoint=new b2Vec2(0,0),this.lowerAngle=0,this.upperAngle=0,this.motorTorque=0,this.motorSpeed=0,this.enableLimit=!1,this.enableMotor=!1},anchorPoint:null,lowerAngle:null,upperAngle:null,motorTorque:null,motorSpeed:null,enableLimit:null,enableMotor:null}); |