Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <Box2D/Collision/b2BroadPhase.h>
00020
00021 b2BroadPhase::b2BroadPhase()
00022 {
00023 m_proxyCount = 0;
00024
00025 m_pairCapacity = 16;
00026 m_pairCount = 0;
00027 m_pairBuffer = (b2Pair*)b2Alloc(m_pairCapacity * sizeof(b2Pair));
00028
00029 m_moveCapacity = 16;
00030 m_moveCount = 0;
00031 m_moveBuffer = (int32*)b2Alloc(m_moveCapacity * sizeof(int32));
00032 }
00033
00034 b2BroadPhase::~b2BroadPhase()
00035 {
00036 b2Free(m_moveBuffer);
00037 b2Free(m_pairBuffer);
00038 }
00039
00040 int32 b2BroadPhase::CreateProxy(const b2AABB& aabb, void* userData)
00041 {
00042 int32 proxyId = m_tree.CreateProxy(aabb, userData);
00043 ++m_proxyCount;
00044 BufferMove(proxyId);
00045 return proxyId;
00046 }
00047
00048 void b2BroadPhase::DestroyProxy(int32 proxyId)
00049 {
00050 UnBufferMove(proxyId);
00051 --m_proxyCount;
00052 m_tree.DestroyProxy(proxyId);
00053 }
00054
00055 void b2BroadPhase::MoveProxy(int32 proxyId, const b2AABB& aabb, const b2Vec2& displacement)
00056 {
00057 bool buffer = m_tree.MoveProxy(proxyId, aabb, displacement);
00058 if (buffer)
00059 {
00060 BufferMove(proxyId);
00061 }
00062 }
00063
00064 void b2BroadPhase::TouchProxy(int32 proxyId)
00065 {
00066 BufferMove(proxyId);
00067 }
00068
00069 void b2BroadPhase::BufferMove(int32 proxyId)
00070 {
00071 if (m_moveCount == m_moveCapacity)
00072 {
00073 int32* oldBuffer = m_moveBuffer;
00074 m_moveCapacity *= 2;
00075 m_moveBuffer = (int32*)b2Alloc(m_moveCapacity * sizeof(int32));
00076 memcpy(m_moveBuffer, oldBuffer, m_moveCount * sizeof(int32));
00077 b2Free(oldBuffer);
00078 }
00079
00080 m_moveBuffer[m_moveCount] = proxyId;
00081 ++m_moveCount;
00082 }
00083
00084 void b2BroadPhase::UnBufferMove(int32 proxyId)
00085 {
00086 for (int32 i = 0; i < m_moveCount; ++i)
00087 {
00088 if (m_moveBuffer[i] == proxyId)
00089 {
00090 m_moveBuffer[i] = e_nullProxy;
00091 }
00092 }
00093 }
00094
00095
00096 bool b2BroadPhase::QueryCallback(int32 proxyId)
00097 {
00098
00099 if (proxyId == m_queryProxyId)
00100 {
00101 return true;
00102 }
00103
00104
00105 if (m_pairCount == m_pairCapacity)
00106 {
00107 b2Pair* oldBuffer = m_pairBuffer;
00108 m_pairCapacity *= 2;
00109 m_pairBuffer = (b2Pair*)b2Alloc(m_pairCapacity * sizeof(b2Pair));
00110 memcpy(m_pairBuffer, oldBuffer, m_pairCount * sizeof(b2Pair));
00111 b2Free(oldBuffer);
00112 }
00113
00114 m_pairBuffer[m_pairCount].proxyIdA = b2Min(proxyId, m_queryProxyId);
00115 m_pairBuffer[m_pairCount].proxyIdB = b2Max(proxyId, m_queryProxyId);
00116 ++m_pairCount;
00117
00118 return true;
00119 }