Warning, file /education/gcompris/external/qml-box2d/Box2D/Collision/b2BroadPhase.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* 0002 * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org 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 0021 b2BroadPhase::b2BroadPhase() 0022 { 0023 m_proxyCount = 0; 0024 0025 m_pairCapacity = 16; 0026 m_pairCount = 0; 0027 m_pairBuffer = (b2Pair*)b2Alloc(m_pairCapacity * sizeof(b2Pair)); 0028 0029 m_moveCapacity = 16; 0030 m_moveCount = 0; 0031 m_moveBuffer = (int32*)b2Alloc(m_moveCapacity * sizeof(int32)); 0032 } 0033 0034 b2BroadPhase::~b2BroadPhase() 0035 { 0036 b2Free(m_moveBuffer); 0037 b2Free(m_pairBuffer); 0038 } 0039 0040 int32 b2BroadPhase::CreateProxy(const b2AABB& aabb, void* userData) 0041 { 0042 int32 proxyId = m_tree.CreateProxy(aabb, userData); 0043 ++m_proxyCount; 0044 BufferMove(proxyId); 0045 return proxyId; 0046 } 0047 0048 void b2BroadPhase::DestroyProxy(int32 proxyId) 0049 { 0050 UnBufferMove(proxyId); 0051 --m_proxyCount; 0052 m_tree.DestroyProxy(proxyId); 0053 } 0054 0055 void b2BroadPhase::MoveProxy(int32 proxyId, const b2AABB& aabb, const b2Vec2& displacement) 0056 { 0057 bool buffer = m_tree.MoveProxy(proxyId, aabb, displacement); 0058 if (buffer) 0059 { 0060 BufferMove(proxyId); 0061 } 0062 } 0063 0064 void b2BroadPhase::TouchProxy(int32 proxyId) 0065 { 0066 BufferMove(proxyId); 0067 } 0068 0069 void b2BroadPhase::BufferMove(int32 proxyId) 0070 { 0071 if (m_moveCount == m_moveCapacity) 0072 { 0073 int32* oldBuffer = m_moveBuffer; 0074 m_moveCapacity *= 2; 0075 m_moveBuffer = (int32*)b2Alloc(m_moveCapacity * sizeof(int32)); 0076 memcpy(m_moveBuffer, oldBuffer, m_moveCount * sizeof(int32)); 0077 b2Free(oldBuffer); 0078 } 0079 0080 m_moveBuffer[m_moveCount] = proxyId; 0081 ++m_moveCount; 0082 } 0083 0084 void b2BroadPhase::UnBufferMove(int32 proxyId) 0085 { 0086 for (int32 i = 0; i < m_moveCount; ++i) 0087 { 0088 if (m_moveBuffer[i] == proxyId) 0089 { 0090 m_moveBuffer[i] = e_nullProxy; 0091 } 0092 } 0093 } 0094 0095 // This is called from b2DynamicTree::Query when we are gathering pairs. 0096 bool b2BroadPhase::QueryCallback(int32 proxyId) 0097 { 0098 // A proxy cannot form a pair with itself. 0099 if (proxyId == m_queryProxyId) 0100 { 0101 return true; 0102 } 0103 0104 // Grow the pair buffer as needed. 0105 if (m_pairCount == m_pairCapacity) 0106 { 0107 b2Pair* oldBuffer = m_pairBuffer; 0108 m_pairCapacity *= 2; 0109 m_pairBuffer = (b2Pair*)b2Alloc(m_pairCapacity * sizeof(b2Pair)); 0110 memcpy(m_pairBuffer, oldBuffer, m_pairCount * sizeof(b2Pair)); 0111 b2Free(oldBuffer); 0112 } 0113 0114 m_pairBuffer[m_pairCount].proxyIdA = b2Min(proxyId, m_queryProxyId); 0115 m_pairBuffer[m_pairCount].proxyIdB = b2Max(proxyId, m_queryProxyId); 0116 ++m_pairCount; 0117 0118 return true; 0119 }