1
0

btSimpleBroadphase.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
  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 "bullet/BulletCollision//BroadphaseCollision/btDispatcher.h"
  15. #include "bullet/BulletCollision//BroadphaseCollision/btCollisionAlgorithm.h"
  16. #include "bullet/LinearMath/btVector3.h"
  17. #include "bullet/LinearMath/btTransform.h"
  18. #include "bullet/LinearMath/btMatrix3x3.h"
  19. #include "bullet/LinearMath/btAabbUtil2.h"
  20. #include <new>
  21. extern int gOverlappingPairs;
  22. void btSimpleBroadphase::validate()
  23. {
  24. for (int i=0;i<m_numHandles;i++)
  25. {
  26. for (int j=i+1;j<m_numHandles;j++)
  27. {
  28. btAssert(&m_pHandles[i] != &m_pHandles[j]);
  29. }
  30. }
  31. }
  32. btSimpleBroadphase::btSimpleBroadphase(int maxProxies, btOverlappingPairCache* overlappingPairCache)
  33. :m_pairCache(overlappingPairCache),
  34. m_ownsPairCache(false),
  35. m_invalidPair(0)
  36. {
  37. if (!overlappingPairCache)
  38. {
  39. void* mem = btAlignedAlloc(sizeof(btHashedOverlappingPairCache),16);
  40. m_pairCache = new (mem)btHashedOverlappingPairCache();
  41. m_ownsPairCache = true;
  42. }
  43. // allocate handles buffer and put all handles on free list
  44. m_pHandlesRawPtr = btAlignedAlloc(sizeof(btSimpleBroadphaseProxy)*maxProxies,16);
  45. m_pHandles = new(m_pHandlesRawPtr) btSimpleBroadphaseProxy[maxProxies];
  46. m_maxHandles = maxProxies;
  47. m_numHandles = 0;
  48. m_firstFreeHandle = 0;
  49. m_LastHandleIndex = -1;
  50. {
  51. for (int i = m_firstFreeHandle; i < maxProxies; i++)
  52. {
  53. m_pHandles[i].SetNextFree(i + 1);
  54. m_pHandles[i].m_uniqueId = i+2;//any UID will do, we just avoid too trivial values (0,1) for debugging purposes
  55. }
  56. m_pHandles[maxProxies - 1].SetNextFree(0);
  57. }
  58. }
  59. btSimpleBroadphase::~btSimpleBroadphase()
  60. {
  61. btAlignedFree(m_pHandlesRawPtr);
  62. if (m_ownsPairCache)
  63. {
  64. m_pairCache->~btOverlappingPairCache();
  65. btAlignedFree(m_pairCache);
  66. }
  67. }
  68. btBroadphaseProxy* btSimpleBroadphase::createProxy( const btVector3& aabbMin, const btVector3& aabbMax,int shapeType,void* userPtr ,short int collisionFilterGroup,short int collisionFilterMask, btDispatcher* /*dispatcher*/,void* multiSapProxy)
  69. {
  70. if (m_numHandles >= m_maxHandles)
  71. {
  72. btAssert(0);
  73. return 0; //should never happen, but don't let the game crash ;-)
  74. }
  75. btAssert(aabbMin[0]<= aabbMax[0] && aabbMin[1]<= aabbMax[1] && aabbMin[2]<= aabbMax[2]);
  76. int newHandleIndex = allocHandle();
  77. btSimpleBroadphaseProxy* proxy = new (&m_pHandles[newHandleIndex])btSimpleBroadphaseProxy(aabbMin,aabbMax,shapeType,userPtr,collisionFilterGroup,collisionFilterMask,multiSapProxy);
  78. return proxy;
  79. }
  80. class RemovingOverlapCallback : public btOverlapCallback
  81. {
  82. protected:
  83. virtual bool processOverlap(btBroadphasePair& pair)
  84. {
  85. (void)pair;
  86. btAssert(0);
  87. return false;
  88. }
  89. };
  90. class RemovePairContainingProxy
  91. {
  92. btBroadphaseProxy* m_targetProxy;
  93. public:
  94. virtual ~RemovePairContainingProxy()
  95. {
  96. }
  97. protected:
  98. virtual bool processOverlap(btBroadphasePair& pair)
  99. {
  100. btSimpleBroadphaseProxy* proxy0 = static_cast<btSimpleBroadphaseProxy*>(pair.m_pProxy0);
  101. btSimpleBroadphaseProxy* proxy1 = static_cast<btSimpleBroadphaseProxy*>(pair.m_pProxy1);
  102. return ((m_targetProxy == proxy0 || m_targetProxy == proxy1));
  103. };
  104. };
  105. void btSimpleBroadphase::destroyProxy(btBroadphaseProxy* proxyOrg,btDispatcher* dispatcher)
  106. {
  107. btSimpleBroadphaseProxy* proxy0 = static_cast<btSimpleBroadphaseProxy*>(proxyOrg);
  108. freeHandle(proxy0);
  109. m_pairCache->removeOverlappingPairsContainingProxy(proxyOrg,dispatcher);
  110. //validate();
  111. }
  112. void btSimpleBroadphase::getAabb(btBroadphaseProxy* proxy,btVector3& aabbMin, btVector3& aabbMax ) const
  113. {
  114. const btSimpleBroadphaseProxy* sbp = getSimpleProxyFromProxy(proxy);
  115. aabbMin = sbp->m_aabbMin;
  116. aabbMax = sbp->m_aabbMax;
  117. }
  118. void btSimpleBroadphase::setAabb(btBroadphaseProxy* proxy,const btVector3& aabbMin,const btVector3& aabbMax, btDispatcher* /*dispatcher*/)
  119. {
  120. btSimpleBroadphaseProxy* sbp = getSimpleProxyFromProxy(proxy);
  121. sbp->m_aabbMin = aabbMin;
  122. sbp->m_aabbMax = aabbMax;
  123. }
  124. void btSimpleBroadphase::rayTest(const btVector3& rayFrom,const btVector3& rayTo, btBroadphaseRayCallback& rayCallback, const btVector3& aabbMin,const btVector3& aabbMax)
  125. {
  126. for (int i=0; i <= m_LastHandleIndex; i++)
  127. {
  128. btSimpleBroadphaseProxy* proxy = &m_pHandles[i];
  129. if(!proxy->m_clientObject)
  130. {
  131. continue;
  132. }
  133. rayCallback.process(proxy);
  134. }
  135. }
  136. void btSimpleBroadphase::aabbTest(const btVector3& aabbMin, const btVector3& aabbMax, btBroadphaseAabbCallback& callback)
  137. {
  138. for (int i=0; i <= m_LastHandleIndex; i++)
  139. {
  140. btSimpleBroadphaseProxy* proxy = &m_pHandles[i];
  141. if(!proxy->m_clientObject)
  142. {
  143. continue;
  144. }
  145. if (TestAabbAgainstAabb2(aabbMin,aabbMax,proxy->m_aabbMin,proxy->m_aabbMax))
  146. {
  147. callback.process(proxy);
  148. }
  149. }
  150. }
  151. bool btSimpleBroadphase::aabbOverlap(btSimpleBroadphaseProxy* proxy0,btSimpleBroadphaseProxy* proxy1)
  152. {
  153. return proxy0->m_aabbMin[0] <= proxy1->m_aabbMax[0] && proxy1->m_aabbMin[0] <= proxy0->m_aabbMax[0] &&
  154. proxy0->m_aabbMin[1] <= proxy1->m_aabbMax[1] && proxy1->m_aabbMin[1] <= proxy0->m_aabbMax[1] &&
  155. proxy0->m_aabbMin[2] <= proxy1->m_aabbMax[2] && proxy1->m_aabbMin[2] <= proxy0->m_aabbMax[2];
  156. }
  157. //then remove non-overlapping ones
  158. class CheckOverlapCallback : public btOverlapCallback
  159. {
  160. public:
  161. virtual bool processOverlap(btBroadphasePair& pair)
  162. {
  163. return (!btSimpleBroadphase::aabbOverlap(static_cast<btSimpleBroadphaseProxy*>(pair.m_pProxy0),static_cast<btSimpleBroadphaseProxy*>(pair.m_pProxy1)));
  164. }
  165. };
  166. void btSimpleBroadphase::calculateOverlappingPairs(btDispatcher* dispatcher)
  167. {
  168. //first check for new overlapping pairs
  169. int i,j;
  170. if (m_numHandles >= 0)
  171. {
  172. int new_largest_index = -1;
  173. for (i=0; i <= m_LastHandleIndex; i++)
  174. {
  175. btSimpleBroadphaseProxy* proxy0 = &m_pHandles[i];
  176. if(!proxy0->m_clientObject)
  177. {
  178. continue;
  179. }
  180. new_largest_index = i;
  181. for (j=i+1; j <= m_LastHandleIndex; j++)
  182. {
  183. btSimpleBroadphaseProxy* proxy1 = &m_pHandles[j];
  184. btAssert(proxy0 != proxy1);
  185. if(!proxy1->m_clientObject)
  186. {
  187. continue;
  188. }
  189. btSimpleBroadphaseProxy* p0 = getSimpleProxyFromProxy(proxy0);
  190. btSimpleBroadphaseProxy* p1 = getSimpleProxyFromProxy(proxy1);
  191. if (aabbOverlap(p0,p1))
  192. {
  193. if ( !m_pairCache->findPair(proxy0,proxy1))
  194. {
  195. m_pairCache->addOverlappingPair(proxy0,proxy1);
  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. } else
  234. {
  235. needsRemoval = true;
  236. }
  237. } else
  238. {
  239. //remove duplicate
  240. needsRemoval = true;
  241. //should have no algorithm
  242. btAssert(!pair.m_algorithm);
  243. }
  244. if (needsRemoval)
  245. {
  246. m_pairCache->cleanOverlappingPair(pair,dispatcher);
  247. // m_overlappingPairArray.swap(i,m_overlappingPairArray.size()-1);
  248. // m_overlappingPairArray.pop_back();
  249. pair.m_pProxy0 = 0;
  250. pair.m_pProxy1 = 0;
  251. m_invalidPair++;
  252. gOverlappingPairs--;
  253. }
  254. }
  255. ///if you don't like to skip the invalid pairs in the array, execute following code:
  256. #define CLEAN_INVALID_PAIRS 1
  257. #ifdef CLEAN_INVALID_PAIRS
  258. //perform a sort, to sort 'invalid' pairs to the end
  259. overlappingPairArray.quickSort(btBroadphasePairSortPredicate());
  260. overlappingPairArray.resize(overlappingPairArray.size() - m_invalidPair);
  261. m_invalidPair = 0;
  262. #endif//CLEAN_INVALID_PAIRS
  263. }
  264. }
  265. }
  266. bool btSimpleBroadphase::testAabbOverlap(btBroadphaseProxy* proxy0,btBroadphaseProxy* proxy1)
  267. {
  268. btSimpleBroadphaseProxy* p0 = getSimpleProxyFromProxy(proxy0);
  269. btSimpleBroadphaseProxy* p1 = getSimpleProxyFromProxy(proxy1);
  270. return aabbOverlap(p0,p1);
  271. }
  272. void btSimpleBroadphase::resetPool(btDispatcher* dispatcher)
  273. {
  274. //not yet
  275. }