File indexing completed on 2025-08-03 03:49:55

0001 /*
0002 * Copyright (c) 2006-2009 Erin Catto http://www.gphysics.com
0003 *
0004 * This software is provided 'as-is', without any express or implied
0005 * warranty.  In no event will the authors be held liable for any damages
0006 * arising from the use of this software.
0007 * Permission is granted to anyone to use this software for any purpose,
0008 * including commercial applications, and to alter it and redistribute it
0009 * freely, subject to the following restrictions:
0010 * 1. The origin of this software must not be misrepresented; you must not
0011 * claim that you wrote the original software. If you use this software
0012 * in a product, an acknowledgment in the product documentation would be
0013 * appreciated but is not required.
0014 * 2. Altered source versions must be plainly marked as such, and must not be
0015 * misrepresented as being the original software.
0016 * 3. This notice may not be removed or altered from any source distribution.
0017 */
0018 
0019 #include <Box2D/Collision/b2BroadPhase.h>
0020 #include <cstring>
0021 using namespace std;
0022 
0023 b2BroadPhase::b2BroadPhase()
0024 {
0025     m_proxyCount = 0;
0026 
0027     m_pairCapacity = 16;
0028     m_pairCount = 0;
0029     m_pairBuffer = (b2Pair*)b2Alloc(m_pairCapacity * sizeof(b2Pair));
0030 
0031     m_moveCapacity = 16;
0032     m_moveCount = 0;
0033     m_moveBuffer = (int32*)b2Alloc(m_moveCapacity * sizeof(int32));
0034 }
0035 
0036 b2BroadPhase::~b2BroadPhase()
0037 {
0038     b2Free(m_moveBuffer);
0039     b2Free(m_pairBuffer);
0040 }
0041 
0042 int32 b2BroadPhase::CreateProxy(const b2AABB& aabb, void* userData)
0043 {
0044     int32 proxyId = m_tree.CreateProxy(aabb, userData);
0045     ++m_proxyCount;
0046     BufferMove(proxyId);
0047     return proxyId;
0048 }
0049 
0050 void b2BroadPhase::DestroyProxy(int32 proxyId)
0051 {
0052     UnBufferMove(proxyId);
0053     --m_proxyCount;
0054     m_tree.DestroyProxy(proxyId);
0055 }
0056 
0057 void b2BroadPhase::MoveProxy(int32 proxyId, const b2AABB& aabb, const b2Vec2& displacement)
0058 {
0059     bool buffer = m_tree.MoveProxy(proxyId, aabb, displacement);
0060     if (buffer)
0061     {
0062         BufferMove(proxyId);
0063     }
0064 }
0065 
0066 void b2BroadPhase::BufferMove(int32 proxyId)
0067 {
0068     if (m_moveCount == m_moveCapacity)
0069     {
0070         int32* oldBuffer = m_moveBuffer;
0071         m_moveCapacity *= 2;
0072         m_moveBuffer = (int32*)b2Alloc(m_moveCapacity * sizeof(int32));
0073         memcpy(m_moveBuffer, oldBuffer, m_moveCount * sizeof(int32));
0074         b2Free(oldBuffer);
0075     }
0076 
0077     m_moveBuffer[m_moveCount] = proxyId;
0078     ++m_moveCount;
0079 }
0080 
0081 void b2BroadPhase::UnBufferMove(int32 proxyId)
0082 {
0083     for (int32 i = 0; i < m_moveCount; ++i)
0084     {
0085         if (m_moveBuffer[i] == proxyId)
0086         {
0087             m_moveBuffer[i] = e_nullProxy;
0088             return;
0089         }
0090     }
0091 }
0092 
0093 // This is called from b2DynamicTree::Query when we are gathering pairs.
0094 bool b2BroadPhase::QueryCallback(int32 proxyId)
0095 {
0096     // A proxy cannot form a pair with itself.
0097     if (proxyId == m_queryProxyId)
0098     {
0099         return true;
0100     }
0101 
0102     // Grow the pair buffer as needed.
0103     if (m_pairCount == m_pairCapacity)
0104     {
0105         b2Pair* oldBuffer = m_pairBuffer;
0106         m_pairCapacity *= 2;
0107         m_pairBuffer = (b2Pair*)b2Alloc(m_pairCapacity * sizeof(b2Pair));
0108         memcpy(m_pairBuffer, oldBuffer, m_pairCount * sizeof(b2Pair));
0109         b2Free(oldBuffer);
0110     }
0111 
0112     m_pairBuffer[m_pairCount].proxyIdA = b2Min(proxyId, m_queryProxyId);
0113     m_pairBuffer[m_pairCount].proxyIdB = b2Max(proxyId, m_queryProxyId);
0114     ++m_pairCount;
0115 
0116     return true;
0117 }