b2BroadPhase.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. * Permission is granted to anyone to use this software for any purpose,
  8. * including commercial applications, and to alter it and redistribute it
  9. * freely, subject to the following restrictions:
  10. * 1. The origin of this software must not be misrepresented; you must not
  11. * claim that you wrote the original software. If you use this software
  12. * in a product, an acknowledgment in the product documentation would be
  13. * appreciated but is not required.
  14. * 2. Altered source versions must be plainly marked as such, and must not be
  15. * misrepresented as being the original software.
  16. * 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include <Box2D/Collision/b2BroadPhase.h>
  19. b2BroadPhase::b2BroadPhase()
  20. {
  21. m_proxyCount = 0;
  22. m_pairCapacity = 16;
  23. m_pairCount = 0;
  24. m_pairBuffer = (b2Pair*)b2Alloc(m_pairCapacity * sizeof(b2Pair));
  25. m_moveCapacity = 16;
  26. m_moveCount = 0;
  27. m_moveBuffer = (int32*)b2Alloc(m_moveCapacity * sizeof(int32));
  28. }
  29. b2BroadPhase::~b2BroadPhase()
  30. {
  31. b2Free(m_moveBuffer);
  32. b2Free(m_pairBuffer);
  33. }
  34. int32 b2BroadPhase::CreateProxy(const b2AABB& aabb, void* userData)
  35. {
  36. int32 proxyId = m_tree.CreateProxy(aabb, userData);
  37. ++m_proxyCount;
  38. BufferMove(proxyId);
  39. return proxyId;
  40. }
  41. void b2BroadPhase::DestroyProxy(int32 proxyId)
  42. {
  43. UnBufferMove(proxyId);
  44. --m_proxyCount;
  45. m_tree.DestroyProxy(proxyId);
  46. }
  47. void b2BroadPhase::MoveProxy(int32 proxyId, const b2AABB& aabb, const b2Vec2& displacement)
  48. {
  49. bool buffer = m_tree.MoveProxy(proxyId, aabb, displacement);
  50. if (buffer)
  51. {
  52. BufferMove(proxyId);
  53. }
  54. }
  55. void b2BroadPhase::TouchProxy(int32 proxyId)
  56. {
  57. BufferMove(proxyId);
  58. }
  59. void b2BroadPhase::BufferMove(int32 proxyId)
  60. {
  61. if (m_moveCount == m_moveCapacity)
  62. {
  63. int32* oldBuffer = m_moveBuffer;
  64. m_moveCapacity *= 2;
  65. m_moveBuffer = (int32*)b2Alloc(m_moveCapacity * sizeof(int32));
  66. memcpy(m_moveBuffer, oldBuffer, m_moveCount * sizeof(int32));
  67. b2Free(oldBuffer);
  68. }
  69. m_moveBuffer[m_moveCount] = proxyId;
  70. ++m_moveCount;
  71. }
  72. void b2BroadPhase::UnBufferMove(int32 proxyId)
  73. {
  74. for (int32 i = 0; i < m_moveCount; ++i)
  75. {
  76. if (m_moveBuffer[i] == proxyId)
  77. {
  78. m_moveBuffer[i] = e_nullProxy;
  79. }
  80. }
  81. }
  82. // This is called from b2DynamicTree::Query when we are gathering pairs.
  83. bool b2BroadPhase::QueryCallback(int32 proxyId)
  84. {
  85. // A proxy cannot form a pair with itself.
  86. if (proxyId == m_queryProxyId)
  87. {
  88. return true;
  89. }
  90. // Grow the pair buffer as needed.
  91. if (m_pairCount == m_pairCapacity)
  92. {
  93. b2Pair* oldBuffer = m_pairBuffer;
  94. m_pairCapacity *= 2;
  95. m_pairBuffer = (b2Pair*)b2Alloc(m_pairCapacity * sizeof(b2Pair));
  96. memcpy(m_pairBuffer, oldBuffer, m_pairCount * sizeof(b2Pair));
  97. b2Free(oldBuffer);
  98. }
  99. m_pairBuffer[m_pairCount].proxyIdA = b2Min(proxyId, m_queryProxyId);
  100. m_pairBuffer[m_pairCount].proxyIdB = b2Max(proxyId, m_queryProxyId);
  101. ++m_pairCount;
  102. return true;
  103. }