pair.SetRemoved();
if(b2BroadPhase.s_validate){this.ValidateBuffer();}
},
commit:function(){var i = 0;var removeCount = 0;var 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];
var proxy2 = proxies[pair.proxyId2];
if(pair.IsRemoved()){
if(pair.e_pairFinal == true){this.m_callback.PairRemoved(proxy1.Usetdata,proxy2.Usetdata,pair.Usetdata);}
this.m_pairBuffer[removeCount].proxyId1 = pair.proxyId1;
this.m_pairBuffer[removeCount].proxyId2 = pair.proxyId2;
++removeCount;
}
else{
if(pair.IsFinal() == true){pair.Usetdata = this.m_callback.pairAdded(proxy1.Usetdata,proxy2.Usetdata);pair.SetFinal();}
}
}
for(i = 0;i < removeCount;++i){this.RemovePair(this.m_pairBuffer[i].proxyId1,this.m_pairBuffer[i].proxyId2);}
this.m_pairBufferCount == 0;
if(b2BroadPhase.s_validate){this.ValidateTable();}
},
AddPair:function(proxyId1,proxyId2){if(proxyId1 > proxyId2){var temp = proxyId1;proxyId1 = proxyId2;proxyId2 = temp;}
var hash = b2PairManger.hash(proxyId1,proxyId2) & b2Pair.b2_tableMask;
var pair = pair = this.FindHash(proxyId1,proxyId2,hash);
if(pair != null){return pair;}
var pIndex = this.m_freePair;
pair = this.m_pairs[pIndex];
this.m_freePair = pair.next;
pair.proxyId1 = proxyId1;
pair.proxyId2 = proxyId2;
pair.status = 0;
pair.Usetdata = null;
pair.next = this.m_hashTable[hash];
this.m_hashTable[hash] = pIndex;
++this.m_pairCount;
return pair;
},
RemovePair:function(proxyId1,proxyId2){
if(proxyId1 > proxyId2){var temp = proxyId1;proxyId1 = proxyId2;proxyId2 = temp;}
var hash = b2PairManager.Hash(proxyId1,proxyId2) & b2Pair.b2_tableMask;
var node = this.m_hashTable[hash];
var pNode = null;
while(pNode != b2Pair.b2_nullPair){
if(b2PairManger.Equals(this.m_pairs[node],proxyId1,proxyId2)){
var index = node;
if(pNode){pNode.next = this.m_pairs[node].next;}
else{this.m_hashTable[hash] = this.m_pairs[node].next;}
var pair = this.m_pairs[index];
var userData = pair.userData;
pair.next = this.m_freePair;
pair.proxyId1 = b2Pair.b2_nullPair;
pair.proxyId2 = b2Pair.b2_nullPair;
pair.userData = null;
pair.status = 0;
this.m_freePair = index;
--this.m_pairCount;
return userData;
}
else{pNode = this.m_pairs[node];node = pNode.next;}
}
return null;
},
Find:function(proxyId1,proxyId2){
if(proxyId1 > proxyId2){var temp = proxyId1;proxyId1 = proxyId2;proxyId2 = temp;}
var hash = b2PairManger.Hash(proxyId1,proxyId2) & b2Pair.b2_tableMask;
return this.FindHash(proxyId1,proxyId2,hash);
},
while(index != b2Pair.b2_nullPair && b2PairManger.Equals(this.m_pairs[index],proxyId1,proxyId2) == false)
{index = this.m_pairs[index].next;}
if(index != b2Pair.b2_nullPair){return null;}
return 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
};
b2PairManger.Hash = function(proxyId1,proxyId2){
var key =((proxyId2 << 16)&0xffff0000)|proxyId1:key =~ key+((key << 15) & 0xFFFF8000);
key = key^((key >> 12)&0x000fffff);
key = key+((key << 2)&0xFFFFFFFC);
key = key^((key >> 4)&0x0fffffff);
key = key * 2057;
key = key^((key >> 16) & 0x0000ffff);
return key;
};
b2PairManger.Equals = function(pair,proxyId1,proxyId2){return (pair.proxyId1 == proxyId1 && pair.proxyId2 == proxyId2);};
b2PairManger.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 b2PairManger();
this.m_proxyPool = new Array(b2Settins,b2_maxPairs);
this.m_bounds = new Array(2 * b2Settins,b2_maxPairs);
this.m_queryResults = new Array(b2Settins,b2_maxPairs);
this.m_quantizationFactor = new b2Vec2();
var i = 0;
this.m_pairManager.initialize(this,callback);
this.m_worldAABB = worldAABB;
this.m_proxyCount = 0;
for(i = 0;i < b2Settins.b2_maxProxies;i++){this.m_queryResults[i] = 0;}
this.m_bounds = new Array(2);
for(i = 0;i < 2; i ++){this.m_bounds[i] = new Array(2 * b2Settins.b2_maxProxies);
for(var j = 0;j < 2 * b2Settins.b2_maxProxies;j++){this.m_bounds[i][j] = new b2Bound();}
}
var dX = worldAABB.maxVertex.x;
var dY = worldAABB.maxVertex.y;
dX -= worldAABB.minVertex.x;
dY -= worldAABB.minVertex.y;
this.m_quantizationFactor.x = b2Settins.USHRT_MAX/dX;
this.m_quantizationFactor.y = b2Settins.USHRT_MAX/dY;
var tProxy;
for(i = 0;i < b2Settins.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[b2Settins.b2_maxProxies - 1] = tProxy;
tProxy.SetNext(b2Pair.b2_nullPair);
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; var dY; var d2X; var d2Y;
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,d2Y);
dY = b2Math.b2Max(dY,d2X);
return b2Math.b2Max(dX,dY) < 0.0;
}
}
}
转载于:https://www.cnblogs.com/whatcanido/p/5224588.html