btSimpleBroadphase.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2006 Erwin Coumans https://bulletphysics.org
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. #include "btSimpleBroadphase.h"
  14. #include "BulletCollision/BroadphaseCollision/btDispatcher.h"
  15. #include "BulletCollision/BroadphaseCollision/btCollisionAlgorithm.h"
  16. #include "LinearMath/btVector3.h"
  17. #include "LinearMath/btTransform.h"
  18. #include "LinearMath/btMatrix3x3.h"
  19. #include "LinearMath/btAabbUtil2.h"
  20. #include <new>
  21. void btSimpleBroadphase::validate()
  22. {
  23. for (int i = 0; i < m_numHandles; i++)
  24. {
  25. for (int j = i + 1; j < m_numHandles; j++)
  26. {
  27. btAssert(&m_pHandles[i] != &m_pHandles[j]);
  28. }
  29. }
  30. }
  31. btSimpleBroadphase::btSimpleBroadphase(int maxProxies, btOverlappingPairCache* overlappingPairCache)
  32. : m_pairCache(overlappingPairCache),
  33. m_ownsPairCache(false),
  34. m_invalidPair(0)
  35. {
  36. if (!overlappingPairCache)
  37. {
  38. void* mem = btAlignedAlloc(sizeof(btHashedOverlappingPairCache), 16);
  39. m_pairCache = new (mem) btHashedOverlappingPairCache();
  40. m_ownsPairCache = true;
  41. }
  42. // allocate handles buffer and put all handles on free list
  43. m_pHandlesRawPtr = btAlignedAlloc(sizeof(btSimpleBroadphaseProxy) * maxProxies, 16);
  44. m_pHandles = new (m_pHandlesRawPtr) btSimpleBroadphaseProxy[maxProxies];
  45. m_maxHandles = maxProxies;
  46. m_numHandles = 0;
  47. m_firstFreeHandle = 0;
  48. m_LastHandleIndex = -1;
  49. {
  50. for (int i = m_firstFreeHandle; i < maxProxies; i++)
  51. {
  52. m_pHandles[i].SetNextFree(i + 1);
  53. m_pHandles[i].m_uniqueId = i + 2; //any UID will do, we just avoid too trivial values (0,1) for debugging purposes
  54. }
  55. m_pHandles[maxProxies - 1].SetNextFree(0);
  56. }
  57. }
  58. btSimpleBroadphase::~btSimpleBroadphase()
  59. {
  60. btAlignedFree(m_pHandlesRawPtr);
  61. if (m_ownsPairCache)
  62. {
  63. m_pairCache->~btOverlappingPairCache();
  64. btAlignedFree(m_pairCache);
  65. }
  66. }
  67. btBroadphaseProxy* btSimpleBroadphase::createProxy(const btVector3& aabbMin, const btVector3& aabbMax, int shapeType, void* userPtr, int collisionFilterGroup, int collisionFilterMask, btDispatcher* /*dispatcher*/)
  68. {
  69. if (m_numHandles >= m_maxHandles)
  70. {
  71. btAssert(0);
  72. return 0; //should never happen, but don't let the game crash ;-)
  73. }
  74. btAssert(aabbMin[0] <= aabbMax[0] && aabbMin[1] <= aabbMax[1] && aabbMin[2] <= aabbMax[2]);
  75. int newHandleIndex = allocHandle();
  76. btSimpleBroadphaseProxy* proxy = new (&m_pHandles[newHandleIndex]) btSimpleBroadphaseProxy(aabbMin, aabbMax, shapeType, userPtr, collisionFilterGroup, collisionFilterMask);
  77. return proxy;
  78. }
  79. class RemovingOverlapCallback : public btOverlapCallback
  80. {
  81. protected:
  82. virtual bool processOverlap(btBroadphasePair& pair)
  83. {
  84. (void)pair;
  85. btAssert(0);
  86. return false;
  87. }
  88. };
  89. class RemovePairContainingProxy
  90. {
  91. btBroadphaseProxy* m_targetProxy;
  92. public:
  93. virtual ~RemovePairContainingProxy()
  94. {
  95. }
  96. protected:
  97. virtual bool processOverlap(btBroadphasePair& pair)
  98. {
  99. btSimpleBroadphaseProxy* proxy0 = static_cast<btSimpleBroadphaseProxy*>(pair.m_pProxy0);
  100. btSimpleBroadphaseProxy* proxy1 = static_cast<btSimpleBroadphaseProxy*>(pair.m_pProxy1);
  101. return ((m_targetProxy == proxy0 || m_targetProxy == proxy1));
  102. };
  103. };
  104. void btSimpleBroadphase::destroyProxy(btBroadphaseProxy* proxyOrg, btDispatcher* dispatcher)
  105. {
  106. m_pairCache->removeOverlappingPairsContainingProxy(proxyOrg, dispatcher);
  107. btSimpleBroadphaseProxy* proxy0 = static_cast<btSimpleBroadphaseProxy*>(proxyOrg);
  108. freeHandle(proxy0);
  109. //validate();
  110. }
  111. void btSimpleBroadphase::getAabb(btBroadphaseProxy* proxy, btVector3& aabbMin, btVector3& aabbMax) const
  112. {
  113. const btSimpleBroadphaseProxy* sbp = getSimpleProxyFromProxy(proxy);
  114. aabbMin = sbp->m_aabbMin;
  115. aabbMax = sbp->m_aabbMax;
  116. }
  117. void btSimpleBroadphase::setAabb(btBroadphaseProxy* proxy, const btVector3& aabbMin, const btVector3& aabbMax, btDispatcher* /*dispatcher*/)
  118. {
  119. btSimpleBroadphaseProxy* sbp = getSimpleProxyFromProxy(proxy);
  120. sbp->m_aabbMin = aabbMin;
  121. sbp->m_aabbMax = aabbMax;
  122. }
  123. void btSimpleBroadphase::rayTest(const btVector3& rayFrom, const btVector3& rayTo, btBroadphaseRayCallback& rayCallback, const btVector3& aabbMin, const btVector3& aabbMax)
  124. {
  125. for (int i = 0; i <= m_LastHandleIndex; i++)
  126. {
  127. btSimpleBroadphaseProxy* proxy = &m_pHandles[i];
  128. if (!proxy->m_clientObject)
  129. {
  130. continue;
  131. }
  132. rayCallback.process(proxy);
  133. }
  134. }
  135. void btSimpleBroadphase::aabbTest(const btVector3& aabbMin, const btVector3& aabbMax, btBroadphaseAabbCallback& callback)
  136. {
  137. for (int i = 0; i <= m_LastHandleIndex; i++)
  138. {
  139. btSimpleBroadphaseProxy* proxy = &m_pHandles[i];
  140. if (!proxy->m_clientObject)
  141. {
  142. continue;
  143. }
  144. if (TestAabbAgainstAabb2(aabbMin, aabbMax, proxy->m_aabbMin, proxy->m_aabbMax))
  145. {
  146. callback.process(proxy);
  147. }
  148. }
  149. }
  150. bool btSimpleBroadphase::aabbOverlap(btSimpleBroadphaseProxy* proxy0, btSimpleBroadphaseProxy* proxy1)
  151. {
  152. return proxy0->m_aabbMin[0] <= proxy1->m_aabbMax[0] && proxy1->m_aabbMin[0] <= proxy0->m_aabbMax[0] &&
  153. proxy0->m_aabbMin[1] <= proxy1->m_aabbMax[1] && proxy1->m_aabbMin[1] <= proxy0->m_aabbMax[1] &&
  154. proxy0->m_aabbMin[2] <= proxy1->m_aabbMax[2] && proxy1->m_aabbMin[2] <= proxy0->m_aabbMax[2];
  155. }
  156. //then remove non-overlapping ones
  157. class CheckOverlapCallback : public btOverlapCallback
  158. {
  159. public:
  160. virtual bool processOverlap(btBroadphasePair& pair)
  161. {
  162. return (!btSimpleBroadphase::aabbOverlap(static_cast<btSimpleBroadphaseProxy*>(pair.m_pProxy0), static_cast<btSimpleBroadphaseProxy*>(pair.m_pProxy1)));
  163. }
  164. };
  165. void btSimpleBroadphase::calculateOverlappingPairs(btDispatcher* dispatcher)
  166. {
  167. //first check for new overlapping pairs
  168. int i, j;
  169. if (m_numHandles >= 0)
  170. {
  171. int new_largest_index = -1;
  172. for (i = 0; i <= m_LastHandleIndex; i++)
  173. {
  174. btSimpleBroadphaseProxy* proxy0 = &m_pHandles[i];
  175. if (!proxy0->m_clientObject)
  176. {
  177. continue;
  178. }
  179. new_largest_index = i;
  180. for (j = i + 1; j <= m_LastHandleIndex; j++)
  181. {
  182. btSimpleBroadphaseProxy* proxy1 = &m_pHandles[j];
  183. btAssert(proxy0 != proxy1);
  184. if (!proxy1->m_clientObject)
  185. {
  186. continue;
  187. }
  188. btSimpleBroadphaseProxy* p0 = getSimpleProxyFromProxy(proxy0);
  189. btSimpleBroadphaseProxy* p1 = getSimpleProxyFromProxy(proxy1);
  190. if (aabbOverlap(p0, p1))
  191. {
  192. if (!m_pairCache->findPair(proxy0, proxy1))
  193. {
  194. m_pairCache->addOverlappingPair(proxy0, proxy1);
  195. }
  196. }
  197. else
  198. {
  199. if (!m_pairCache->hasDeferredRemoval())
  200. {
  201. if (m_pairCache->findPair(proxy0, proxy1))
  202. {
  203. m_pairCache->removeOverlappingPair(proxy0, proxy1, dispatcher);
  204. }
  205. }
  206. }
  207. }
  208. }
  209. m_LastHandleIndex = new_largest_index;
  210. if (m_ownsPairCache && m_pairCache->hasDeferredRemoval())
  211. {
  212. btBroadphasePairArray& overlappingPairArray = m_pairCache->getOverlappingPairArray();
  213. //perform a sort, to find duplicates and to sort 'invalid' pairs to the end
  214. overlappingPairArray.quickSort(btBroadphasePairSortPredicate());
  215. overlappingPairArray.resize(overlappingPairArray.size() - m_invalidPair);
  216. m_invalidPair = 0;
  217. btBroadphasePair previousPair;
  218. previousPair.m_pProxy0 = 0;
  219. previousPair.m_pProxy1 = 0;
  220. previousPair.m_algorithm = 0;
  221. for (i = 0; i < overlappingPairArray.size(); i++)
  222. {
  223. btBroadphasePair& pair = overlappingPairArray[i];
  224. bool isDuplicate = (pair == previousPair);
  225. previousPair = pair;
  226. bool needsRemoval = false;
  227. if (!isDuplicate)
  228. {
  229. bool hasOverlap = testAabbOverlap(pair.m_pProxy0, pair.m_pProxy1);
  230. if (hasOverlap)
  231. {
  232. needsRemoval = false; //callback->processOverlap(pair);
  233. }
  234. else
  235. {
  236. needsRemoval = true;
  237. }
  238. }
  239. else
  240. {
  241. //remove duplicate
  242. needsRemoval = true;
  243. //should have no algorithm
  244. btAssert(!pair.m_algorithm);
  245. }
  246. if (needsRemoval)
  247. {
  248. m_pairCache->cleanOverlappingPair(pair, dispatcher);
  249. // m_overlappingPairArray.swap(i,m_overlappingPairArray.size()-1);
  250. // m_overlappingPairArray.pop_back();
  251. pair.m_pProxy0 = 0;
  252. pair.m_pProxy1 = 0;
  253. m_invalidPair++;
  254. }
  255. }
  256. ///if you don't like to skip the invalid pairs in the array, execute following code:
  257. #define CLEAN_INVALID_PAIRS 1
  258. #ifdef CLEAN_INVALID_PAIRS
  259. //perform a sort, to sort 'invalid' pairs to the end
  260. overlappingPairArray.quickSort(btBroadphasePairSortPredicate());
  261. overlappingPairArray.resize(overlappingPairArray.size() - m_invalidPair);
  262. m_invalidPair = 0;
  263. #endif //CLEAN_INVALID_PAIRS
  264. }
  265. }
  266. }
  267. bool btSimpleBroadphase::testAabbOverlap(btBroadphaseProxy* proxy0, btBroadphaseProxy* proxy1)
  268. {
  269. btSimpleBroadphaseProxy* p0 = getSimpleProxyFromProxy(proxy0);
  270. btSimpleBroadphaseProxy* p1 = getSimpleProxyFromProxy(proxy1);
  271. return aabbOverlap(p0, p1);
  272. }
  273. void btSimpleBroadphase::resetPool(btDispatcher* dispatcher)
  274. {
  275. //not yet
  276. }