b2BroadPhase.cpp
Go to the documentation of this file.
00001 /*
00002 * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
00003 *
00004 * This software is provided 'as-is', without any express or implied
00005 * warranty.  In no event will the authors be held liable for any damages
00006 * arising from the use of this software.
00007 * Permission is granted to anyone to use this software for any purpose,
00008 * including commercial applications, and to alter it and redistribute it
00009 * freely, subject to the following restrictions:
00010 * 1. The origin of this software must not be misrepresented; you must not
00011 * claim that you wrote the original software. If you use this software
00012 * in a product, an acknowledgment in the product documentation would be
00013 * appreciated but is not required.
00014 * 2. Altered source versions must be plainly marked as such, and must not be
00015 * misrepresented as being the original software.
00016 * 3. This notice may not be removed or altered from any source distribution.
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 // This is called from b2DynamicTree::Query when we are gathering pairs.
00096 bool b2BroadPhase::QueryCallback(int32 proxyId)
00097 {
00098         // A proxy cannot form a pair with itself.
00099         if (proxyId == m_queryProxyId)
00100         {
00101                 return true;
00102         }
00103 
00104         // Grow the pair buffer as needed.
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 }


mvsim
Author(s):
autogenerated on Thu Jun 6 2019 22:08:34