b2_broad_phase.h
Go to the documentation of this file.
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 {
35 };
36 
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 
121 
123 
127 
131 
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 
153 {
154  return m_proxyCount;
155 }
156 
158 {
159  return m_tree.GetHeight();
160 }
161 
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
float GetTreeQuality() const
Get the quality metric of the embedded tree.
int32 m_queryProxyId
#define B2_API
Definition: b2_api.h:49
b2Pair * m_pairBuffer
Ray-cast input data. The ray extends from p1 to p1 + maxFraction * (p2 - p1).
Definition: b2_collision.h:153
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.
void RayCast(T *callback, const b2RayCastInput &input) const
int32 proxyIdB
A 2D column vector.
Definition: b2_math.h:41
signed int int32
Definition: b2_types.h:28
bool TestOverlap(int32 proxyIdA, int32 proxyIdB) const
Test overlap of fat AABBs.
int32 GetProxyCount() const
Get the number of proxies.
int32 GetTreeHeight() const
Get the height of the embedded tree.
void UpdatePairs(T *callback)
Update the pairs. This results in pair callbacks. This can only add pairs.
int32 GetTreeBalance() const
Get the balance of the embedded tree.
void * GetUserData(int32 proxyId) const
Get user data from a proxy. Returns nullptr if the id is invalid.
void ShiftOrigin(const b2Vec2 &newOrigin)
const b2AABB & GetFatAABB(int32 proxyId) const
Get the fat AABB for a proxy.
An axis aligned bounding box.
Definition: b2_collision.h:168
int32 m_moveCapacity
int32 * m_moveBuffer
b2DynamicTree m_tree
int32 m_pairCapacity
void Query(T *callback, const b2AABB &aabb) const
int32 proxyIdA


mvsim
Author(s):
autogenerated on Tue Jul 4 2023 03:08:19