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_COLLISION__DISPATCHER_H
17 #define BT_COLLISION__DISPATCHER_H
18 
19 #include "BulletCollision/BroadphaseCollision/btDispatcher.h"
20 #include "BulletCollision/NarrowPhaseCollision/btPersistentManifold.h"
21 
22 #include "BulletCollision/CollisionDispatch/btManifoldResult.h"
23 
24 #include "BulletCollision/BroadphaseCollision/btBroadphaseProxy.h"
25 #include "LinearMath/btAlignedObjectArray.h"
26 
27 class btIDebugDraw;
28 class btOverlappingPairCache;
29 class btPoolAllocator;
30 class btCollisionConfiguration;
31 
32 #include "btCollisionCreateFunc.h"
33 
34 #define USE_DISPATCH_REGISTRY_ARRAY 1
35 
36 class btCollisionDispatcher;
37 ///user can override this nearcallback for collision filtering and more finegrained control over collision detection
38 typedef void (*btNearCallback)(btBroadphasePair& collisionPair, btCollisionDispatcher& dispatcher, const btDispatcherInfo& dispatchInfo);
39 
40 ///btCollisionDispatcher supports algorithms that handle ConvexConvex and ConvexConcave collision pairs.
41 ///Time of Impact, Closest Points and Penetration Depth.
42 class btCollisionDispatcher : public btDispatcher
43 {
44 protected:
45 	int m_dispatcherFlags;
46 
47 	btAlignedObjectArray<btPersistentManifold*> m_manifoldsPtr;
48 
49 	btManifoldResult m_defaultManifoldResult;
50 
51 	btNearCallback m_nearCallback;
52 
53 	btPoolAllocator* m_collisionAlgorithmPoolAllocator;
54 
55 	btPoolAllocator* m_persistentManifoldPoolAllocator;
56 
57 	btCollisionAlgorithmCreateFunc* m_doubleDispatchContactPoints[MAX_BROADPHASE_COLLISION_TYPES][MAX_BROADPHASE_COLLISION_TYPES];
58 
59 	btCollisionAlgorithmCreateFunc* m_doubleDispatchClosestPoints[MAX_BROADPHASE_COLLISION_TYPES][MAX_BROADPHASE_COLLISION_TYPES];
60 
61 	btCollisionConfiguration* m_collisionConfiguration;
62 
63 public:
64 	enum DispatcherFlags
65 	{
66 		CD_STATIC_STATIC_REPORTED = 1,
67 		CD_USE_RELATIVE_CONTACT_BREAKING_THRESHOLD = 2,
68 		CD_DISABLE_CONTACTPOOL_DYNAMIC_ALLOCATION = 4
69 	};
70 
getDispatcherFlags()71 	int getDispatcherFlags() const
72 	{
73 		return m_dispatcherFlags;
74 	}
75 
setDispatcherFlags(int flags)76 	void setDispatcherFlags(int flags)
77 	{
78 		m_dispatcherFlags = flags;
79 	}
80 
81 	///registerCollisionCreateFunc allows registration of custom/alternative collision create functions
82 	void registerCollisionCreateFunc(int proxyType0, int proxyType1, btCollisionAlgorithmCreateFunc* createFunc);
83 
84 	void registerClosestPointsCreateFunc(int proxyType0, int proxyType1, btCollisionAlgorithmCreateFunc* createFunc);
85 
getNumManifolds()86 	int getNumManifolds() const
87 	{
88 		return int(m_manifoldsPtr.size());
89 	}
90 
getInternalManifoldPointer()91 	btPersistentManifold** getInternalManifoldPointer()
92 	{
93 		return m_manifoldsPtr.size() ? &m_manifoldsPtr[0] : 0;
94 	}
95 
getManifoldByIndexInternal(int index)96 	btPersistentManifold* getManifoldByIndexInternal(int index)
97 	{
98 		return m_manifoldsPtr[index];
99 	}
100 
getManifoldByIndexInternal(int index)101 	const btPersistentManifold* getManifoldByIndexInternal(int index) const
102 	{
103 		return m_manifoldsPtr[index];
104 	}
105 
106 	btCollisionDispatcher(btCollisionConfiguration* collisionConfiguration);
107 
108 	virtual ~btCollisionDispatcher();
109 
110 	virtual btPersistentManifold* getNewManifold(const btCollisionObject* b0, const btCollisionObject* b1);
111 
112 	virtual void releaseManifold(btPersistentManifold* manifold);
113 
114 	virtual void clearManifold(btPersistentManifold* manifold);
115 
116 	btCollisionAlgorithm* findAlgorithm(const btCollisionObjectWrapper* body0Wrap, const btCollisionObjectWrapper* body1Wrap, btPersistentManifold* sharedManifold, ebtDispatcherQueryType queryType);
117 
118 	virtual bool needsCollision(const btCollisionObject* body0, const btCollisionObject* body1);
119 
120 	virtual bool needsResponse(const btCollisionObject* body0, const btCollisionObject* body1);
121 
122 	virtual void dispatchAllCollisionPairs(btOverlappingPairCache* pairCache, const btDispatcherInfo& dispatchInfo, btDispatcher* dispatcher);
123 
setNearCallback(btNearCallback nearCallback)124 	void setNearCallback(btNearCallback nearCallback)
125 	{
126 		m_nearCallback = nearCallback;
127 	}
128 
getNearCallback()129 	btNearCallback getNearCallback() const
130 	{
131 		return m_nearCallback;
132 	}
133 
134 	//by default, Bullet will use this near callback
135 	static void defaultNearCallback(btBroadphasePair& collisionPair, btCollisionDispatcher& dispatcher, const btDispatcherInfo& dispatchInfo);
136 
137 	virtual void* allocateCollisionAlgorithm(int size);
138 
139 	virtual void freeCollisionAlgorithm(void* ptr);
140 
getCollisionConfiguration()141 	btCollisionConfiguration* getCollisionConfiguration()
142 	{
143 		return m_collisionConfiguration;
144 	}
145 
getCollisionConfiguration()146 	const btCollisionConfiguration* getCollisionConfiguration() const
147 	{
148 		return m_collisionConfiguration;
149 	}
150 
setCollisionConfiguration(btCollisionConfiguration * config)151 	void setCollisionConfiguration(btCollisionConfiguration* config)
152 	{
153 		m_collisionConfiguration = config;
154 	}
155 
getInternalManifoldPool()156 	virtual btPoolAllocator* getInternalManifoldPool()
157 	{
158 		return m_persistentManifoldPoolAllocator;
159 	}
160 
getInternalManifoldPool()161 	virtual const btPoolAllocator* getInternalManifoldPool() const
162 	{
163 		return m_persistentManifoldPoolAllocator;
164 	}
165 };
166 
167 #endif  //BT_COLLISION__DISPATCHER_H
168