Box2D: /build/box2d/src/box2d-2.4.1/include/box2d/b2_broad_phase.h Source File

| Box2D 2.4.1 A 2D physics engine for games

b2_broad_phase.h
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 #ifndef B2_BROAD_PHASE_H
24 #define B2_BROAD_PHASE_H
25
26 #include "b2_api.h"
27 #include "b2_settings.h"
28 #include "b2_collision.h"
29 #include "b2_dynamic_tree.h"
30
31 struct B2_API b2Pair
32 {
33 int32 proxyIdA;
34 int32 proxyIdB;
35 };
36
40 class B2_API b2BroadPhase
41 {
42 public:
43
44 enum
45 {
46 e_nullProxy = -1
47 };
48
49 b2BroadPhase();
50 ~b2BroadPhase();
51
54 int32 CreateProxy(const b2AABB& aabb, void* userData);
55
57 void DestroyProxy(int32 proxyId);
58
61 void MoveProxy(int32 proxyId, const b2AABB& aabb, const b2Vec2& displacement);
62
64 void TouchProxy(int32 proxyId);
65
67 const b2AABB& GetFatAABB(int32 proxyId) const;
68
70 void* GetUserData(int32 proxyId) const;
71
73 bool TestOverlap(int32 proxyIdA, int32 proxyIdB) const;
74
76 int32 GetProxyCount() const;
77
79 template <typename T>
80 void UpdatePairs(T* callback);
81
84 template <typename T>
85 void Query(T* callback, const b2AABB& aabb) const;
86
94 template <typename T>
95 void RayCast(T* callback, const b2RayCastInput& input) const;
96
98 int32 GetTreeHeight() const;
99
101 int32 GetTreeBalance() const;
102
104 float GetTreeQuality() const;
105
109 void ShiftOrigin(const b2Vec2& newOrigin);
110
111 private:
112
113 friend class b2DynamicTree;
114
115 void BufferMove(int32 proxyId);
116 void UnBufferMove(int32 proxyId);
117
118 bool QueryCallback(int32 proxyId);
119
120 b2DynamicTree m_tree;
121
122 int32 m_proxyCount;
123
124 int32* m_moveBuffer;
125 int32 m_moveCapacity;
126 int32 m_moveCount;
127
128 b2Pair* m_pairBuffer;
129 int32 m_pairCapacity;
130 int32 m_pairCount;
131
132 int32 m_queryProxyId;
133 };
134
135 inline void* b2BroadPhase::GetUserData(int32 proxyId) const
136 {
137 return m_tree.GetUserData(proxyId);
138 }
139
140 inline bool b2BroadPhase::TestOverlap(int32 proxyIdA, int32 proxyIdB) const
141 {
142 const b2AABB& aabbA = m_tree.GetFatAABB(proxyIdA);
143 const b2AABB& aabbB = m_tree.GetFatAABB(proxyIdB);
144 return b2TestOverlap(aabbA, aabbB);
145 }
146
147 inline const b2AABB& b2BroadPhase::GetFatAABB(int32 proxyId) const
148 {
149 return m_tree.GetFatAABB(proxyId);
150 }
151
152 inline int32 b2BroadPhase::GetProxyCount() const
153 {
154 return m_proxyCount;
155 }
156
157 inline int32 b2BroadPhase::GetTreeHeight() const
158 {
159 return m_tree.GetHeight();
160 }
161
162 inline int32 b2BroadPhase::GetTreeBalance() const
163 {
164 return m_tree.GetMaxBalance();
165 }
166
167 inline float b2BroadPhase::GetTreeQuality() const
168 {
169 return m_tree.GetAreaRatio();
170 }
171
172 template <typename T>
173 void b2BroadPhase::UpdatePairs(T* callback)
174 {
175 // Reset pair buffer
176 m_pairCount = 0;
177
178 // Perform tree queries for all moving proxies.
179 for (int32 i = 0; i < m_moveCount; ++i)
180 {
181 m_queryProxyId = m_moveBuffer[i];
182 if (m_queryProxyId == e_nullProxy)
183 {
184 continue;
185 }
186
187 // We have to query the tree with the fat AABB so that
188 // we don't fail to create a pair that may touch later.
189 const b2AABB& fatAABB = m_tree.GetFatAABB(m_queryProxyId);
190
191 // Query tree, create pairs and add them pair buffer.
192 m_tree.Query(this, fatAABB);
193 }
194
195 // Send pairs to caller
196 for (int32 i = 0; i < m_pairCount; ++i)
197 {
198 b2Pair* primaryPair = m_pairBuffer + i;
199 void* userDataA = m_tree.GetUserData(primaryPair->proxyIdA);
200 void* userDataB = m_tree.GetUserData(primaryPair->proxyIdB);
201
202 callback->AddPair(userDataA, userDataB);
203 }
204
205 // Clear move flags
206 for (int32 i = 0; i < m_moveCount; ++i)
207 {
208 int32 proxyId = m_moveBuffer[i];
209 if (proxyId == e_nullProxy)
210 {
211 continue;
212 }
213
214 m_tree.ClearMoved(proxyId);
215 }
216
217 // Reset move buffer
218 m_moveCount = 0;
219 }
220
221 template <typename T>
222 inline void b2BroadPhase::Query(T* callback, const b2AABB& aabb) const
223 {
224 m_tree.Query(callback, aabb);
225 }
226
227 template <typename T>
228 inline void b2BroadPhase::RayCast(T* callback, const b2RayCastInput& input) const
229 {
230 m_tree.RayCast(callback, input);
231 }
232
233 inline void b2BroadPhase::ShiftOrigin(const b2Vec2& newOrigin)
234 {
235 m_tree.ShiftOrigin(newOrigin);
236 }
237
238 #endif
b2Vec2
A 2D column vector.
Definition: b2_math.h:42
b2DynamicTree
Definition: b2_dynamic_tree.h:69
b2BroadPhase::GetTreeHeight
int32 GetTreeHeight() const
Get the height of the embedded tree.
Definition: b2_broad_phase.h:157
b2BroadPhase::MoveProxy
void MoveProxy(int32 proxyId, const b2AABB &aabb, const b2Vec2 &displacement)
b2_settings.h
b2DynamicTree::GetFatAABB
const b2AABB & GetFatAABB(int32 proxyId) const
Get the fat AABB for a proxy.
Definition: b2_dynamic_tree.h:181
b2BroadPhase::GetFatAABB
const b2AABB & GetFatAABB(int32 proxyId) const
Get the fat AABB for a proxy.
Definition: b2_broad_phase.h:147
b2RayCastInput
Ray-cast input data. The ray extends from p1 to p1 + maxFraction * (p2 - p1).
Definition: b2_collision.h:154
b2DynamicTree::GetAreaRatio
float GetAreaRatio() const
Get the ratio of the sum of the node areas to the root area.
b2BroadPhase::TouchProxy
void TouchProxy(int32 proxyId)
Call to trigger a re-processing of it's pairs on the next call to UpdatePairs.
b2BroadPhase::GetTreeQuality
float GetTreeQuality() const
Get the quality metric of the embedded tree.
Definition: b2_broad_phase.h:167
b2BroadPhase::CreateProxy
int32 CreateProxy(const b2AABB &aabb, void *userData)
b2BroadPhase::TestOverlap
bool TestOverlap(int32 proxyIdA, int32 proxyIdB) const
Test overlap of fat AABBs.
Definition: b2_broad_phase.h:140
b2BroadPhase::DestroyProxy
void DestroyProxy(int32 proxyId)
Destroy a proxy. It is up to the client to remove any pairs.
b2DynamicTree::GetUserData
void * GetUserData(int32 proxyId) const
Definition: b2_dynamic_tree.h:163
b2DynamicTree::RayCast
void RayCast(T *callback, const b2RayCastInput &input) const
Definition: b2_dynamic_tree.h:223
b2BroadPhase::RayCast
void RayCast(T *callback, const b2RayCastInput &input) const
Definition: b2_broad_phase.h:228
b2BroadPhase::UpdatePairs
void UpdatePairs(T *callback)
Update the pairs. This results in pair callbacks. This can only add pairs.
Definition: b2_broad_phase.h:173
b2AABB
An axis aligned bounding box.
Definition: b2_collision.h:169
b2BroadPhase::GetUserData
void * GetUserData(int32 proxyId) const
Get user data from a proxy. Returns nullptr if the id is invalid.
Definition: b2_broad_phase.h:135
b2BroadPhase::ShiftOrigin
void ShiftOrigin(const b2Vec2 &newOrigin)
Definition: b2_broad_phase.h:233
b2TestOverlap
B2_API bool b2TestOverlap(const b2Shape *shapeA, int32 indexA, const b2Shape *shapeB, int32 indexB, const b2Transform &xfA, const b2Transform &xfB)
Determine if two generic shapes overlap.
b2_collision.h
b2BroadPhase
Definition: b2_broad_phase.h:41
b2Pair
Definition: b2_broad_phase.h:32
b2BroadPhase::GetProxyCount
int32 GetProxyCount() const
Get the number of proxies.
Definition: b2_broad_phase.h:152
b2DynamicTree::Query
void Query(T *callback, const b2AABB &aabb) const
Definition: b2_dynamic_tree.h:188
b2DynamicTree::GetHeight
int32 GetHeight() const
b2DynamicTree::GetMaxBalance
int32 GetMaxBalance() const
b2DynamicTree::ShiftOrigin
void ShiftOrigin(const b2Vec2 &newOrigin)
b2BroadPhase::Query
void Query(T *callback, const b2AABB &aabb) const
Definition: b2_broad_phase.h:222
b2BroadPhase::GetTreeBalance
int32 GetTreeBalance() const
Get the balance of the embedded tree.
Definition: b2_broad_phase.h:162