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 }
bool QueryCallback(int32 proxyId)
void * b2Alloc(int32 size)
Implement this function to use your own memory allocator.
Definition: b2_settings.h:100
T b2Min(T a, T b)
Definition: b2_math.h:626
int32 m_queryProxyId
bool MoveProxy(int32 proxyId, const b2AABB &aabb1, const b2Vec2 &displacement)
b2Pair * m_pairBuffer
void UnBufferMove(int32 proxyId)
int32 proxyIdB
A 2D column vector.
Definition: b2_math.h:41
signed int int32
Definition: b2_types.h:28
void DestroyProxy(int32 proxyId)
Destroy a proxy. This asserts if the id is invalid.
bool WasMoved(int32 proxyId) const
int32 CreateProxy(const b2AABB &aabb, void *userData)
T b2Max(T a, T b)
Definition: b2_math.h:637
void DestroyProxy(int32 proxyId)
Destroy a proxy. It is up to the client to remove any pairs.
int32 CreateProxy(const b2AABB &aabb, void *userData)
Create a proxy. Provide a tight fitting AABB and a userData pointer.
void BufferMove(int32 proxyId)
An axis aligned bounding box.
Definition: b2_collision.h:168
void b2Free(void *mem)
If you implement b2Alloc, you should also implement this function.
Definition: b2_settings.h:106
void TouchProxy(int32 proxyId)
Call to trigger a re-processing of it&#39;s pairs on the next call to UpdatePairs.
int32 m_moveCapacity
void MoveProxy(int32 proxyId, const b2AABB &aabb, const b2Vec2 &displacement)
int32 * m_moveBuffer
b2DynamicTree m_tree
int32 m_pairCapacity
int32 proxyIdA


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