b2_broad_phase.cpp 3.4 KB

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