1 // MIT License
2 
3 // Copyright (c) 2019 Erin Catto
4 
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 
23 #include "box2d/b2_broad_phase.h"
24 #include <string.h>
25 
b2BroadPhase()26 b2BroadPhase::b2BroadPhase()
27 {
28 	m_proxyCount = 0;
29 
30 	m_pairCapacity = 16;
31 	m_pairCount = 0;
32 	m_pairBuffer = (b2Pair*)b2Alloc(m_pairCapacity * sizeof(b2Pair));
33 
34 	m_moveCapacity = 16;
35 	m_moveCount = 0;
36 	m_moveBuffer = (int32*)b2Alloc(m_moveCapacity * sizeof(int32));
37 }
38 
~b2BroadPhase()39 b2BroadPhase::~b2BroadPhase()
40 {
41 	b2Free(m_moveBuffer);
42 	b2Free(m_pairBuffer);
43 }
44 
CreateProxy(const b2AABB & aabb,void * userData)45 int32 b2BroadPhase::CreateProxy(const b2AABB& aabb, void* userData)
46 {
47 	int32 proxyId = m_tree.CreateProxy(aabb, userData);
48 	++m_proxyCount;
49 	BufferMove(proxyId);
50 	return proxyId;
51 }
52 
DestroyProxy(int32 proxyId)53 void b2BroadPhase::DestroyProxy(int32 proxyId)
54 {
55 	UnBufferMove(proxyId);
56 	--m_proxyCount;
57 	m_tree.DestroyProxy(proxyId);
58 }
59 
MoveProxy(int32 proxyId,const b2AABB & aabb,const b2Vec2 & displacement)60 void b2BroadPhase::MoveProxy(int32 proxyId, const b2AABB& aabb, const b2Vec2& displacement)
61 {
62 	bool buffer = m_tree.MoveProxy(proxyId, aabb, displacement);
63 	if (buffer)
64 	{
65 		BufferMove(proxyId);
66 	}
67 }
68 
TouchProxy(int32 proxyId)69 void b2BroadPhase::TouchProxy(int32 proxyId)
70 {
71 	BufferMove(proxyId);
72 }
73 
BufferMove(int32 proxyId)74 void b2BroadPhase::BufferMove(int32 proxyId)
75 {
76 	if (m_moveCount == m_moveCapacity)
77 	{
78 		int32* oldBuffer = m_moveBuffer;
79 		m_moveCapacity *= 2;
80 		m_moveBuffer = (int32*)b2Alloc(m_moveCapacity * sizeof(int32));
81 		memcpy(m_moveBuffer, oldBuffer, m_moveCount * sizeof(int32));
82 		b2Free(oldBuffer);
83 	}
84 
85 	m_moveBuffer[m_moveCount] = proxyId;
86 	++m_moveCount;
87 }
88 
UnBufferMove(int32 proxyId)89 void b2BroadPhase::UnBufferMove(int32 proxyId)
90 {
91 	for (int32 i = 0; i < m_moveCount; ++i)
92 	{
93 		if (m_moveBuffer[i] == proxyId)
94 		{
95 			m_moveBuffer[i] = e_nullProxy;
96 		}
97 	}
98 }
99 
100 // This is called from b2DynamicTree::Query when we are gathering pairs.
QueryCallback(int32 proxyId)101 bool b2BroadPhase::QueryCallback(int32 proxyId)
102 {
103 	// A proxy cannot form a pair with itself.
104 	if (proxyId == m_queryProxyId)
105 	{
106 		return true;
107 	}
108 
109 	const bool moved = m_tree.WasMoved(proxyId);
110 	if (moved && proxyId > m_queryProxyId)
111 	{
112 		// Both proxies are moving. Avoid duplicate pairs.
113 		return true;
114 	}
115 
116 	// Grow the pair buffer as needed.
117 	if (m_pairCount == m_pairCapacity)
118 	{
119 		b2Pair* oldBuffer = m_pairBuffer;
120 		m_pairCapacity = m_pairCapacity + (m_pairCapacity >> 1);
121 		m_pairBuffer = (b2Pair*)b2Alloc(m_pairCapacity * sizeof(b2Pair));
122 		memcpy(m_pairBuffer, oldBuffer, m_pairCount * sizeof(b2Pair));
123 		b2Free(oldBuffer);
124 	}
125 
126 	m_pairBuffer[m_pairCount].proxyIdA = b2Min(proxyId, m_queryProxyId);
127 	m_pairBuffer[m_pairCount].proxyIdB = b2Max(proxyId, m_queryProxyId);
128 	++m_pairCount;
129 
130 	return true;
131 }
132