b2_broad_phase.cpp
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 #include "box2d/b2_broad_phase.h"
24 #include <string.h>
25 
27 {
28  m_proxyCount = 0;
29 
30  m_pairCapacity = 16;
31  m_pairCount = 0;
33 
34  m_moveCapacity = 16;
35  m_moveCount = 0;
37 }
38 
40 {
43 }
44 
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 
54 {
55  UnBufferMove(proxyId);
56  --m_proxyCount;
57  m_tree.DestroyProxy(proxyId);
58 }
59 
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 
70 {
71  BufferMove(proxyId);
72 }
73 
75 {
77  {
78  int32* oldBuffer = m_moveBuffer;
79  m_moveCapacity *= 2;
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 
90 {
91  for (int32 i = 0; i < m_moveCount; ++i)
92  {
93  if (m_moveBuffer[i] == proxyId)
94  {
96  }
97  }
98 }
99 
100 // This is called from b2DynamicTree::Query when we are gathering pairs.
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.
118  {
119  b2Pair* oldBuffer = m_pairBuffer;
122  memcpy(m_pairBuffer, oldBuffer, m_pairCount * sizeof(b2Pair));
123  b2Free(oldBuffer);
124  }
125 
128  ++m_pairCount;
129 
130  return true;
131 }
b2BroadPhase::m_pairCount
int32 m_pairCount
Definition: b2_broad_phase.h:130
b2BroadPhase::DestroyProxy
void DestroyProxy(int32 proxyId)
Destroy a proxy. It is up to the client to remove any pairs.
Definition: b2_broad_phase.cpp:53
b2BroadPhase::UnBufferMove
void UnBufferMove(int32 proxyId)
Definition: b2_broad_phase.cpp:89
b2BroadPhase::m_pairCapacity
int32 m_pairCapacity
Definition: b2_broad_phase.h:129
b2Alloc
void * b2Alloc(int32 size)
Implement this function to use your own memory allocator.
Definition: b2_settings.h:100
b2BroadPhase::e_nullProxy
@ e_nullProxy
Definition: b2_broad_phase.h:46
b2_broad_phase.h
b2Pair
Definition: b2_broad_phase.h:31
b2BroadPhase::BufferMove
void BufferMove(int32 proxyId)
Definition: b2_broad_phase.cpp:74
b2BroadPhase::QueryCallback
bool QueryCallback(int32 proxyId)
Definition: b2_broad_phase.cpp:101
b2BroadPhase::TouchProxy
void TouchProxy(int32 proxyId)
Call to trigger a re-processing of it's pairs on the next call to UpdatePairs.
Definition: b2_broad_phase.cpp:69
b2Min
T b2Min(T a, T b)
Definition: b2_math.h:626
b2DynamicTree::WasMoved
bool WasMoved(int32 proxyId) const
Definition: b2_dynamic_tree.h:169
b2BroadPhase::MoveProxy
void MoveProxy(int32 proxyId, const b2AABB &aabb, const b2Vec2 &displacement)
Definition: b2_broad_phase.cpp:60
b2Vec2
A 2D column vector.
Definition: b2_math.h:41
b2Max
T b2Max(T a, T b)
Definition: b2_math.h:637
b2BroadPhase::m_pairBuffer
b2Pair * m_pairBuffer
Definition: b2_broad_phase.h:128
b2BroadPhase::m_moveCapacity
int32 m_moveCapacity
Definition: b2_broad_phase.h:125
b2BroadPhase::b2BroadPhase
b2BroadPhase()
Definition: b2_broad_phase.cpp:26
b2BroadPhase::m_queryProxyId
int32 m_queryProxyId
Definition: b2_broad_phase.h:132
b2BroadPhase::m_moveBuffer
int32 * m_moveBuffer
Definition: b2_broad_phase.h:124
b2BroadPhase::m_moveCount
int32 m_moveCount
Definition: b2_broad_phase.h:126
b2AABB
An axis aligned bounding box.
Definition: b2_collision.h:168
b2BroadPhase::m_proxyCount
int32 m_proxyCount
Definition: b2_broad_phase.h:122
b2DynamicTree::DestroyProxy
void DestroyProxy(int32 proxyId)
Destroy a proxy. This asserts if the id is invalid.
Definition: b2_dynamic_tree.cpp:124
b2DynamicTree::CreateProxy
int32 CreateProxy(const b2AABB &aabb, void *userData)
Create a proxy. Provide a tight fitting AABB and a userData pointer.
Definition: b2_dynamic_tree.cpp:107
b2Pair::proxyIdA
int32 proxyIdA
Definition: b2_broad_phase.h:33
b2BroadPhase::m_tree
b2DynamicTree m_tree
Definition: b2_broad_phase.h:120
b2DynamicTree::MoveProxy
bool MoveProxy(int32 proxyId, const b2AABB &aabb1, const b2Vec2 &displacement)
Definition: b2_dynamic_tree.cpp:133
int32
signed int int32
Definition: b2_types.h:28
b2Free
void b2Free(void *mem)
If you implement b2Alloc, you should also implement this function.
Definition: b2_settings.h:106
b2Pair::proxyIdB
int32 proxyIdB
Definition: b2_broad_phase.h:34
b2BroadPhase::CreateProxy
int32 CreateProxy(const b2AABB &aabb, void *userData)
Definition: b2_broad_phase.cpp:45
b2BroadPhase::~b2BroadPhase
~b2BroadPhase()
Definition: b2_broad_phase.cpp:39


mvsim
Author(s):
autogenerated on Wed May 28 2025 02:13:06