1 /* 2 Bullet Continuous Collision Detection and Physics Library 3 Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/ 4 5 This software is provided 'as-is', without any express or implied warranty. 6 In no event will the authors be held liable for any damages 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 freely, 9 subject to the following restrictions: 10 11 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. 12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 13 3. This notice may not be removed or altered from any source distribution. 14 */ 15 16 #ifndef BT_SIMPLE_BROADPHASE_H 17 #define BT_SIMPLE_BROADPHASE_H 18 19 #include "btOverlappingPairCache.h" 20 21 struct btSimpleBroadphaseProxy : public btBroadphaseProxy 22 { 23 int m_nextFree; 24 25 // int m_handleId; 26 btSimpleBroadphaseProxybtSimpleBroadphaseProxy27 btSimpleBroadphaseProxy(){}; 28 btSimpleBroadphaseProxybtSimpleBroadphaseProxy29 btSimpleBroadphaseProxy(const btVector3& minpt, const btVector3& maxpt, int shapeType, void* userPtr, int collisionFilterGroup, int collisionFilterMask) 30 : btBroadphaseProxy(minpt, maxpt, userPtr, collisionFilterGroup, collisionFilterMask) 31 { 32 (void)shapeType; 33 } 34 SetNextFreebtSimpleBroadphaseProxy35 SIMD_FORCE_INLINE void SetNextFree(int next) { m_nextFree = next; } GetNextFreebtSimpleBroadphaseProxy36 SIMD_FORCE_INLINE int GetNextFree() const { return m_nextFree; } 37 }; 38 39 ///The SimpleBroadphase is just a unit-test for btAxisSweep3, bt32BitAxisSweep3, or btDbvtBroadphase, so use those classes instead. 40 ///It is a brute force aabb culling broadphase based on O(n^2) aabb checks 41 class btSimpleBroadphase : public btBroadphaseInterface 42 { 43 protected: 44 int m_numHandles; // number of active handles 45 int m_maxHandles; // max number of handles 46 int m_LastHandleIndex; 47 48 btSimpleBroadphaseProxy* m_pHandles; // handles pool 49 50 void* m_pHandlesRawPtr; 51 int m_firstFreeHandle; // free handles list 52 allocHandle()53 int allocHandle() 54 { 55 btAssert(m_numHandles < m_maxHandles); 56 int freeHandle = m_firstFreeHandle; 57 m_firstFreeHandle = m_pHandles[freeHandle].GetNextFree(); 58 m_numHandles++; 59 if (freeHandle > m_LastHandleIndex) 60 { 61 m_LastHandleIndex = freeHandle; 62 } 63 return freeHandle; 64 } 65 freeHandle(btSimpleBroadphaseProxy * proxy)66 void freeHandle(btSimpleBroadphaseProxy* proxy) 67 { 68 int handle = int(proxy - m_pHandles); 69 btAssert(handle >= 0 && handle < m_maxHandles); 70 if (handle == m_LastHandleIndex) 71 { 72 m_LastHandleIndex--; 73 } 74 proxy->SetNextFree(m_firstFreeHandle); 75 m_firstFreeHandle = handle; 76 77 proxy->m_clientObject = 0; 78 79 m_numHandles--; 80 } 81 82 btOverlappingPairCache* m_pairCache; 83 bool m_ownsPairCache; 84 85 int m_invalidPair; 86 getSimpleProxyFromProxy(btBroadphaseProxy * proxy)87 inline btSimpleBroadphaseProxy* getSimpleProxyFromProxy(btBroadphaseProxy* proxy) 88 { 89 btSimpleBroadphaseProxy* proxy0 = static_cast<btSimpleBroadphaseProxy*>(proxy); 90 return proxy0; 91 } 92 getSimpleProxyFromProxy(btBroadphaseProxy * proxy)93 inline const btSimpleBroadphaseProxy* getSimpleProxyFromProxy(btBroadphaseProxy* proxy) const 94 { 95 const btSimpleBroadphaseProxy* proxy0 = static_cast<const btSimpleBroadphaseProxy*>(proxy); 96 return proxy0; 97 } 98 99 ///reset broadphase internal structures, to ensure determinism/reproducability 100 virtual void resetPool(btDispatcher* dispatcher); 101 102 void validate(); 103 104 protected: 105 public: 106 btSimpleBroadphase(int maxProxies = 16384, btOverlappingPairCache* overlappingPairCache = 0); 107 virtual ~btSimpleBroadphase(); 108 109 static bool aabbOverlap(btSimpleBroadphaseProxy* proxy0, btSimpleBroadphaseProxy* proxy1); 110 111 virtual btBroadphaseProxy* createProxy(const btVector3& aabbMin, const btVector3& aabbMax, int shapeType, void* userPtr, int collisionFilterGroup, int collisionFilterMask, btDispatcher* dispatcher); 112 113 virtual void calculateOverlappingPairs(btDispatcher* dispatcher); 114 115 virtual void destroyProxy(btBroadphaseProxy* proxy, btDispatcher* dispatcher); 116 virtual void setAabb(btBroadphaseProxy* proxy, const btVector3& aabbMin, const btVector3& aabbMax, btDispatcher* dispatcher); 117 virtual void getAabb(btBroadphaseProxy* proxy, btVector3& aabbMin, btVector3& aabbMax) const; 118 119 virtual void rayTest(const btVector3& rayFrom, const btVector3& rayTo, btBroadphaseRayCallback& rayCallback, const btVector3& aabbMin = btVector3(0, 0, 0), const btVector3& aabbMax = btVector3(0, 0, 0)); 120 virtual void aabbTest(const btVector3& aabbMin, const btVector3& aabbMax, btBroadphaseAabbCallback& callback); 121 getOverlappingPairCache()122 btOverlappingPairCache* getOverlappingPairCache() 123 { 124 return m_pairCache; 125 } getOverlappingPairCache()126 const btOverlappingPairCache* getOverlappingPairCache() const 127 { 128 return m_pairCache; 129 } 130 131 bool testAabbOverlap(btBroadphaseProxy* proxy0, btBroadphaseProxy* proxy1); 132 133 ///getAabb returns the axis aligned bounding box in the 'global' coordinate frame 134 ///will add some transform later getBroadphaseAabb(btVector3 & aabbMin,btVector3 & aabbMax)135 virtual void getBroadphaseAabb(btVector3& aabbMin, btVector3& aabbMax) const 136 { 137 aabbMin.setValue(-BT_LARGE_FLOAT, -BT_LARGE_FLOAT, -BT_LARGE_FLOAT); 138 aabbMax.setValue(BT_LARGE_FLOAT, BT_LARGE_FLOAT, BT_LARGE_FLOAT); 139 } 140 printStats()141 virtual void printStats() 142 { 143 // printf("btSimpleBroadphase.h\n"); 144 // printf("numHandles = %d, maxHandles = %d\n",m_numHandles,m_maxHandles); 145 } 146 }; 147 148 #endif //BT_SIMPLE_BROADPHASE_H 149