1 /*
2 * Copyright (c) 2006-2009 Erin Catto http://www.gphysics.com
3 *
4 * This software is provided 'as-is', without any express or implied
5 * warranty.  In no event will the authors be held liable for any damages
6 * arising from the use of this software.
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely, subject to the following restrictions:
10 * 1. The origin of this software must not be misrepresented; you must not
11 * claim that you wrote the original software. If you use this software
12 * in a product, an acknowledgment in the product documentation would be
13 * appreciated but is not required.
14 * 2. Altered source versions must be plainly marked as such, and must not be
15 * misrepresented as being the original software.
16 * 3. This notice may not be removed or altered from any source distribution.
17 */
18 
19 #include <Box2D/Collision/b2BroadPhase.h>
20 #include <cstring>
21 using namespace std;
22 
b2BroadPhase()23 b2BroadPhase::b2BroadPhase()
24 {
25 	m_proxyCount = 0;
26 
27 	m_pairCapacity = 16;
28 	m_pairCount = 0;
29 	m_pairBuffer = (b2Pair*)b2Alloc(m_pairCapacity * sizeof(b2Pair));
30 
31 	m_moveCapacity = 16;
32 	m_moveCount = 0;
33 	m_moveBuffer = (int32*)b2Alloc(m_moveCapacity * sizeof(int32));
34 }
35 
~b2BroadPhase()36 b2BroadPhase::~b2BroadPhase()
37 {
38 	b2Free(m_moveBuffer);
39 	b2Free(m_pairBuffer);
40 }
41 
CreateProxy(const b2AABB & aabb,void * userData)42 int32 b2BroadPhase::CreateProxy(const b2AABB& aabb, void* userData)
43 {
44 	int32 proxyId = m_tree.CreateProxy(aabb, userData);
45 	++m_proxyCount;
46 	BufferMove(proxyId);
47 	return proxyId;
48 }
49 
DestroyProxy(int32 proxyId)50 void b2BroadPhase::DestroyProxy(int32 proxyId)
51 {
52 	UnBufferMove(proxyId);
53 	--m_proxyCount;
54 	m_tree.DestroyProxy(proxyId);
55 }
56 
MoveProxy(int32 proxyId,const b2AABB & aabb,const b2Vec2 & displacement)57 void b2BroadPhase::MoveProxy(int32 proxyId, const b2AABB& aabb, const b2Vec2& displacement)
58 {
59 	bool buffer = m_tree.MoveProxy(proxyId, aabb, displacement);
60 	if (buffer)
61 	{
62 		BufferMove(proxyId);
63 	}
64 }
65 
BufferMove(int32 proxyId)66 void b2BroadPhase::BufferMove(int32 proxyId)
67 {
68 	if (m_moveCount == m_moveCapacity)
69 	{
70 		int32* oldBuffer = m_moveBuffer;
71 		m_moveCapacity *= 2;
72 		m_moveBuffer = (int32*)b2Alloc(m_moveCapacity * sizeof(int32));
73 		memcpy(m_moveBuffer, oldBuffer, m_moveCount * sizeof(int32));
74 		b2Free(oldBuffer);
75 	}
76 
77 	m_moveBuffer[m_moveCount] = proxyId;
78 	++m_moveCount;
79 }
80 
UnBufferMove(int32 proxyId)81 void b2BroadPhase::UnBufferMove(int32 proxyId)
82 {
83 	for (int32 i = 0; i < m_moveCount; ++i)
84 	{
85 		if (m_moveBuffer[i] == proxyId)
86 		{
87 			m_moveBuffer[i] = e_nullProxy;
88 			return;
89 		}
90 	}
91 }
92 
93 // This is called from b2DynamicTree::Query when we are gathering pairs.
QueryCallback(int32 proxyId)94 bool b2BroadPhase::QueryCallback(int32 proxyId)
95 {
96 	// A proxy cannot form a pair with itself.
97 	if (proxyId == m_queryProxyId)
98 	{
99 		return true;
100 	}
101 
102 	// Grow the pair buffer as needed.
103 	if (m_pairCount == m_pairCapacity)
104 	{
105 		b2Pair* oldBuffer = m_pairBuffer;
106 		m_pairCapacity *= 2;
107 		m_pairBuffer = (b2Pair*)b2Alloc(m_pairCapacity * sizeof(b2Pair));
108 		memcpy(m_pairBuffer, oldBuffer, m_pairCount * sizeof(b2Pair));
109 		b2Free(oldBuffer);
110 	}
111 
112 	m_pairBuffer[m_pairCount].proxyIdA = b2Min(proxyId, m_queryProxyId);
113 	m_pairBuffer[m_pairCount].proxyIdB = b2Max(proxyId, m_queryProxyId);
114 	++m_pairCount;
115 
116 	return true;
117 }
118