1 /*! \file btGImpactShape.h
2 \author Francisco Leon Najera
5 This source file is part of GIMPACT Library.
7 For the latest info, see http://gimpact.sourceforge.net/
9 Copyright (c) 2007 Francisco Leon Najera. C.C. 80087371.
10 email: projectileman@yahoo.com
13 This software is provided 'as-is', without any express or implied warranty.
14 In no event will the authors be held liable for any damages arising from the use of this software.
15 Permission is granted to anyone to use this software for any purpose,
16 including commercial applications, and to alter it and redistribute it freely,
17 subject to the following restrictions:
19 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.
20 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
21 3. This notice may not be removed or altered from any source distribution.
24 #ifndef BT_GIMPACT_BVH_CONCAVE_COLLISION_ALGORITHM_H
25 #define BT_GIMPACT_BVH_CONCAVE_COLLISION_ALGORITHM_H
27 #include "BulletCollision/CollisionDispatch/btActivatingCollisionAlgorithm.h"
28 #include "BulletCollision/BroadphaseCollision/btDispatcher.h"
29 #include "BulletCollision/BroadphaseCollision/btBroadphaseInterface.h"
30 #include "BulletCollision/NarrowPhaseCollision/btPersistentManifold.h"
32 #include "BulletCollision/BroadphaseCollision/btBroadphaseProxy.h"
33 #include "BulletCollision/CollisionDispatch/btCollisionCreateFunc.h"
34 #include "BulletCollision/CollisionDispatch/btCollisionDispatcher.h"
36 #include "LinearMath/btAlignedObjectArray.h"
38 #include "btGImpactShape.h"
39 #include "BulletCollision/CollisionShapes/btStaticPlaneShape.h"
40 #include "BulletCollision/CollisionShapes/btCompoundShape.h"
41 #include "BulletCollision/CollisionDispatch/btConvexConvexAlgorithm.h"
42 #include "LinearMath/btIDebugDraw.h"
43 #include "BulletCollision/CollisionDispatch/btCollisionObjectWrapper.h"
45 //! Collision Algorithm for GImpact Shapes
47 For register this algorithm in Bullet, proceed as following:
49 btCollisionDispatcher * dispatcher = static_cast<btCollisionDispatcher *>(m_dynamicsWorld ->getDispatcher());
50 btGImpactCollisionAlgorithm::registerAlgorithm(dispatcher);
53 class btGImpactCollisionAlgorithm : public btActivatingCollisionAlgorithm
56 btCollisionAlgorithm* m_convex_algorithm;
57 btPersistentManifold* m_manifoldPtr;
58 btManifoldResult* m_resultOut;
59 const btDispatcherInfo* m_dispatchInfo;
65 //! Creates a new contact point
66 SIMD_FORCE_INLINE btPersistentManifold* newContactManifold(const btCollisionObject* body0, const btCollisionObject* body1)
68 m_manifoldPtr = m_dispatcher->getNewManifold(body0, body1);
72 SIMD_FORCE_INLINE void destroyConvexAlgorithm()
74 if (m_convex_algorithm)
76 m_convex_algorithm->~btCollisionAlgorithm();
77 m_dispatcher->freeCollisionAlgorithm(m_convex_algorithm);
78 m_convex_algorithm = NULL;
82 SIMD_FORCE_INLINE void destroyContactManifolds()
84 if (m_manifoldPtr == NULL) return;
85 m_dispatcher->releaseManifold(m_manifoldPtr);
89 SIMD_FORCE_INLINE void clearCache()
91 destroyContactManifolds();
92 destroyConvexAlgorithm();
100 SIMD_FORCE_INLINE btPersistentManifold* getLastManifold()
102 return m_manifoldPtr;
105 // Call before process collision
106 SIMD_FORCE_INLINE void checkManifold(const btCollisionObjectWrapper* body0Wrap, const btCollisionObjectWrapper* body1Wrap)
108 if (getLastManifold() == 0)
110 newContactManifold(body0Wrap->getCollisionObject(), body1Wrap->getCollisionObject());
113 m_resultOut->setPersistentManifold(getLastManifold());
116 // Call before process collision
117 SIMD_FORCE_INLINE btCollisionAlgorithm* newAlgorithm(const btCollisionObjectWrapper* body0Wrap, const btCollisionObjectWrapper* body1Wrap)
119 checkManifold(body0Wrap, body1Wrap);
121 btCollisionAlgorithm* convex_algorithm = m_dispatcher->findAlgorithm(
122 body0Wrap, body1Wrap, getLastManifold(), BT_CONTACT_POINT_ALGORITHMS);
123 return convex_algorithm;
126 // Call before process collision
127 SIMD_FORCE_INLINE void checkConvexAlgorithm(const btCollisionObjectWrapper* body0Wrap, const btCollisionObjectWrapper* body1Wrap)
129 if (m_convex_algorithm) return;
130 m_convex_algorithm = newAlgorithm(body0Wrap, body1Wrap);
133 void addContactPoint(const btCollisionObjectWrapper* body0Wrap,
134 const btCollisionObjectWrapper* body1Wrap,
135 const btVector3& point,
136 const btVector3& normal,
139 //! Collision routines
142 void collide_gjk_triangles(const btCollisionObjectWrapper* body0Wrap,
143 const btCollisionObjectWrapper* body1Wrap,
144 const btGImpactMeshShapePart* shape0,
145 const btGImpactMeshShapePart* shape1,
146 const int* pairs, int pair_count);
148 void collide_sat_triangles(const btCollisionObjectWrapper* body0Wrap,
149 const btCollisionObjectWrapper* body1Wrap,
150 const btGImpactMeshShapePart* shape0,
151 const btGImpactMeshShapePart* shape1,
152 const int* pairs, int pair_count);
154 void shape_vs_shape_collision(
155 const btCollisionObjectWrapper* body0,
156 const btCollisionObjectWrapper* body1,
157 const btCollisionShape* shape0,
158 const btCollisionShape* shape1);
160 void convex_vs_convex_collision(const btCollisionObjectWrapper* body0Wrap,
161 const btCollisionObjectWrapper* body1Wrap,
162 const btCollisionShape* shape0,
163 const btCollisionShape* shape1);
165 void gimpact_vs_gimpact_find_pairs(
166 const btTransform& trans0,
167 const btTransform& trans1,
168 const btGImpactShapeInterface* shape0,
169 const btGImpactShapeInterface* shape1, btPairSet& pairset);
171 void gimpact_vs_shape_find_pairs(
172 const btTransform& trans0,
173 const btTransform& trans1,
174 const btGImpactShapeInterface* shape0,
175 const btCollisionShape* shape1,
176 btAlignedObjectArray<int>& collided_primitives);
178 void gimpacttrimeshpart_vs_plane_collision(
179 const btCollisionObjectWrapper* body0Wrap,
180 const btCollisionObjectWrapper* body1Wrap,
181 const btGImpactMeshShapePart* shape0,
182 const btStaticPlaneShape* shape1, bool swapped);
185 btGImpactCollisionAlgorithm(const btCollisionAlgorithmConstructionInfo& ci, const btCollisionObjectWrapper* body0Wrap, const btCollisionObjectWrapper* body1Wrap);
187 virtual ~btGImpactCollisionAlgorithm();
189 virtual void processCollision(const btCollisionObjectWrapper* body0Wrap, const btCollisionObjectWrapper* body1Wrap, const btDispatcherInfo& dispatchInfo, btManifoldResult* resultOut);
191 btScalar calculateTimeOfImpact(btCollisionObject* body0, btCollisionObject* body1, const btDispatcherInfo& dispatchInfo, btManifoldResult* resultOut);
193 virtual void getAllContactManifolds(btManifoldArray& manifoldArray)
196 manifoldArray.push_back(m_manifoldPtr);
199 btManifoldResult* internalGetResultOut()
204 struct CreateFunc : public btCollisionAlgorithmCreateFunc
206 virtual btCollisionAlgorithm* CreateCollisionAlgorithm(btCollisionAlgorithmConstructionInfo& ci, const btCollisionObjectWrapper* body0Wrap, const btCollisionObjectWrapper* body1Wrap)
208 void* mem = ci.m_dispatcher1->allocateCollisionAlgorithm(sizeof(btGImpactCollisionAlgorithm));
209 return new (mem) btGImpactCollisionAlgorithm(ci, body0Wrap, body1Wrap);
213 //! Use this function for register the algorithm externally
214 static void registerAlgorithm(btCollisionDispatcher* dispatcher);
215 #ifdef TRI_COLLISION_PROFILING
216 //! Gets the average time in miliseconds of tree collisions
217 static float getAverageTreeCollisionTime();
219 //! Gets the average time in miliseconds of triangle collisions
220 static float getAverageTriangleCollisionTime();
221 #endif //TRI_COLLISION_PROFILING
223 //! Collides two gimpact shapes
225 \pre shape0 and shape1 couldn't be btGImpactMeshShape objects
228 void gimpact_vs_gimpact(const btCollisionObjectWrapper* body0Wrap,
229 const btCollisionObjectWrapper* body1Wrap,
230 const btGImpactShapeInterface* shape0,
231 const btGImpactShapeInterface* shape1);
233 void gimpact_vs_shape(const btCollisionObjectWrapper* body0Wrap,
234 const btCollisionObjectWrapper* body1Wrap,
235 const btGImpactShapeInterface* shape0,
236 const btCollisionShape* shape1, bool swapped);
238 void gimpact_vs_compoundshape(const btCollisionObjectWrapper* body0Wrap,
239 const btCollisionObjectWrapper* body1Wrap,
240 const btGImpactShapeInterface* shape0,
241 const btCompoundShape* shape1, bool swapped);
243 void gimpact_vs_concave(
244 const btCollisionObjectWrapper* body0Wrap,
245 const btCollisionObjectWrapper* body1Wrap,
246 const btGImpactShapeInterface* shape0,
247 const btConcaveShape* shape1, bool swapped);
249 /// Accessor/Mutator pairs for Part and triangleID
250 void setFace0(int value)
258 void setFace1(int value)
266 void setPart0(int value)
274 void setPart1(int value)
285 //#define BULLET_TRIANGLE_COLLISION 1
286 #define GIMPACT_VS_PLANE_COLLISION 1
288 #endif //BT_GIMPACT_BVH_CONCAVE_COLLISION_ALGORITHM_H