btGImpactCollisionAlgorithm.h

Go to the documentation of this file.
00001 
00004 /*
00005 This source file is part of GIMPACT Library.
00006 
00007 For the latest info, see http://gimpact.sourceforge.net/
00008 
00009 Copyright (c) 2007 Francisco Leon Najera. C.C. 80087371.
00010 email: projectileman@yahoo.com
00011 
00012 
00013 This software is provided 'as-is', without any express or implied warranty.
00014 In no event will the authors be held liable for any damages arising from the use of this software.
00015 Permission is granted to anyone to use this software for any purpose,
00016 including commercial applications, and to alter it and redistribute it freely,
00017 subject to the following restrictions:
00018 
00019 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.
00020 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
00021 3. This notice may not be removed or altered from any source distribution.
00022 */
00023 
00024 #ifndef BT_GIMPACT_BVH_CONCAVE_COLLISION_ALGORITHM_H
00025 #define BT_GIMPACT_BVH_CONCAVE_COLLISION_ALGORITHM_H
00026 
00027 #include "BulletCollision/CollisionDispatch/btActivatingCollisionAlgorithm.h"
00028 #include "BulletCollision/BroadphaseCollision/btDispatcher.h"
00029 #include "BulletCollision/BroadphaseCollision/btBroadphaseInterface.h"
00030 #include "BulletCollision/NarrowPhaseCollision/btPersistentManifold.h"
00031 class btDispatcher;
00032 #include "BulletCollision/BroadphaseCollision/btBroadphaseProxy.h"
00033 #include "BulletCollision/CollisionDispatch/btCollisionCreateFunc.h"
00034 #include "BulletCollision/CollisionDispatch/btCollisionDispatcher.h"
00035 
00036 #include "LinearMath/btAlignedObjectArray.h"
00037 
00038 #include "btGImpactShape.h"
00039 #include "BulletCollision/CollisionShapes/btStaticPlaneShape.h"
00040 #include "BulletCollision/CollisionShapes/btCompoundShape.h"
00041 #include "BulletCollision/CollisionDispatch/btConvexConvexAlgorithm.h"
00042 #include "LinearMath/btIDebugDraw.h"
00043 #include "BulletCollision/CollisionDispatch/btCollisionObjectWrapper.h"
00044 
00045 
00047 
00054 class btGImpactCollisionAlgorithm : public btActivatingCollisionAlgorithm
00055 {
00056 protected:
00057         btCollisionAlgorithm * m_convex_algorithm;
00058     btPersistentManifold * m_manifoldPtr;
00059         btManifoldResult* m_resultOut;
00060         const btDispatcherInfo * m_dispatchInfo;
00061         int m_triface0;
00062         int m_part0;
00063         int m_triface1;
00064         int m_part1;
00065 
00066 
00068         SIMD_FORCE_INLINE btPersistentManifold* newContactManifold(const btCollisionObject* body0,const btCollisionObject* body1)
00069         {
00070                 m_manifoldPtr = m_dispatcher->getNewManifold(body0,body1);
00071                 return m_manifoldPtr;
00072         }
00073 
00074         SIMD_FORCE_INLINE void destroyConvexAlgorithm()
00075         {
00076                 if(m_convex_algorithm)
00077                 {
00078                         m_convex_algorithm->~btCollisionAlgorithm();
00079                         m_dispatcher->freeCollisionAlgorithm( m_convex_algorithm);
00080                         m_convex_algorithm = NULL;
00081                 }
00082         }
00083 
00084         SIMD_FORCE_INLINE void destroyContactManifolds()
00085         {
00086                 if(m_manifoldPtr == NULL) return;
00087                 m_dispatcher->releaseManifold(m_manifoldPtr);
00088                 m_manifoldPtr = NULL;
00089         }
00090 
00091         SIMD_FORCE_INLINE void clearCache()
00092         {
00093                 destroyContactManifolds();
00094                 destroyConvexAlgorithm();
00095 
00096                 m_triface0 = -1;
00097                 m_part0 = -1;
00098                 m_triface1 = -1;
00099                 m_part1 = -1;
00100         }
00101 
00102         SIMD_FORCE_INLINE btPersistentManifold* getLastManifold()
00103         {
00104                 return m_manifoldPtr;
00105         }
00106 
00107 
00108         // Call before process collision
00109         SIMD_FORCE_INLINE void checkManifold(const btCollisionObjectWrapper* body0Wrap,const btCollisionObjectWrapper* body1Wrap)
00110         {
00111                 if(getLastManifold() == 0)
00112                 {
00113                         newContactManifold(body0Wrap->getCollisionObject(),body1Wrap->getCollisionObject());
00114                 }
00115 
00116                 m_resultOut->setPersistentManifold(getLastManifold());
00117         }
00118 
00119         // Call before process collision
00120         SIMD_FORCE_INLINE btCollisionAlgorithm * newAlgorithm(const btCollisionObjectWrapper* body0Wrap,const btCollisionObjectWrapper* body1Wrap)
00121         {
00122                 checkManifold(body0Wrap,body1Wrap);
00123 
00124                 btCollisionAlgorithm * convex_algorithm = m_dispatcher->findAlgorithm(
00125                                 body0Wrap,body1Wrap,getLastManifold());
00126                 return convex_algorithm ;
00127         }
00128 
00129         // Call before process collision
00130         SIMD_FORCE_INLINE void checkConvexAlgorithm(const btCollisionObjectWrapper* body0Wrap,const btCollisionObjectWrapper* body1Wrap)
00131         {
00132                 if(m_convex_algorithm) return;
00133                 m_convex_algorithm = newAlgorithm(body0Wrap,body1Wrap);
00134         }
00135 
00136 
00137 
00138 
00139         void addContactPoint(const btCollisionObjectWrapper * body0Wrap,
00140                                         const btCollisionObjectWrapper * body1Wrap,
00141                                         const btVector3 & point,
00142                                         const btVector3 & normal,
00143                                         btScalar distance);
00144 
00147 
00148         void collide_gjk_triangles(const btCollisionObjectWrapper* body0Wrap,
00149                                   const btCollisionObjectWrapper* body1Wrap,
00150                                   const btGImpactMeshShapePart * shape0,
00151                                   const btGImpactMeshShapePart * shape1,
00152                                   const int * pairs, int pair_count);
00153 
00154         void collide_sat_triangles(const btCollisionObjectWrapper* body0Wrap,
00155                                           const btCollisionObjectWrapper* body1Wrap,
00156                                           const btGImpactMeshShapePart * shape0,
00157                                           const btGImpactMeshShapePart * shape1,
00158                                           const int * pairs, int pair_count);
00159 
00160 
00161 
00162 
00163         void shape_vs_shape_collision(
00164                                           const btCollisionObjectWrapper* body0,
00165                                           const btCollisionObjectWrapper* body1,
00166                                           const btCollisionShape * shape0,
00167                                           const btCollisionShape * shape1);
00168 
00169         void convex_vs_convex_collision(const btCollisionObjectWrapper* body0Wrap,
00170                                           const btCollisionObjectWrapper* body1Wrap,
00171                                           const btCollisionShape* shape0,
00172                                           const btCollisionShape* shape1);
00173 
00174 
00175 
00176         void gimpact_vs_gimpact_find_pairs(
00177                                           const btTransform & trans0,
00178                                           const btTransform & trans1,
00179                                           const btGImpactShapeInterface * shape0,
00180                                           const btGImpactShapeInterface * shape1,btPairSet & pairset);
00181 
00182         void gimpact_vs_shape_find_pairs(
00183                                           const btTransform & trans0,
00184                                           const btTransform & trans1,
00185                                           const btGImpactShapeInterface * shape0,
00186                                           const btCollisionShape * shape1,
00187                                           btAlignedObjectArray<int> & collided_primitives);
00188 
00189 
00190         void gimpacttrimeshpart_vs_plane_collision(
00191                                           const btCollisionObjectWrapper * body0Wrap,
00192                                           const btCollisionObjectWrapper * body1Wrap,
00193                                           const btGImpactMeshShapePart * shape0,
00194                                           const btStaticPlaneShape * shape1,bool swapped);
00195 
00196 
00197 public:
00198 
00199         btGImpactCollisionAlgorithm( const btCollisionAlgorithmConstructionInfo& ci,const btCollisionObjectWrapper* body0Wrap,const btCollisionObjectWrapper* body1Wrap);
00200 
00201         virtual ~btGImpactCollisionAlgorithm();
00202 
00203         virtual void processCollision (const btCollisionObjectWrapper* body0Wrap,const btCollisionObjectWrapper* body1Wrap,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut);
00204 
00205         btScalar        calculateTimeOfImpact(btCollisionObject* body0,btCollisionObject* body1,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut);
00206 
00207         virtual void    getAllContactManifolds(btManifoldArray& manifoldArray)
00208         {
00209                 if (m_manifoldPtr)
00210                         manifoldArray.push_back(m_manifoldPtr);
00211         }
00212 
00213         btManifoldResult*       internalGetResultOut()
00214         {
00215                 return m_resultOut;
00216         }
00217 
00218         struct CreateFunc :public       btCollisionAlgorithmCreateFunc
00219         {
00220                 virtual btCollisionAlgorithm* CreateCollisionAlgorithm(btCollisionAlgorithmConstructionInfo& ci, const btCollisionObjectWrapper* body0Wrap,const btCollisionObjectWrapper* body1Wrap)
00221                 {
00222                         void* mem = ci.m_dispatcher1->allocateCollisionAlgorithm(sizeof(btGImpactCollisionAlgorithm));
00223                         return new(mem) btGImpactCollisionAlgorithm(ci,body0Wrap,body1Wrap);
00224                 }
00225         };
00226 
00228         static void registerAlgorithm(btCollisionDispatcher * dispatcher);
00229 #ifdef TRI_COLLISION_PROFILING
00230 
00231         static float getAverageTreeCollisionTime();
00232 
00234         static float getAverageTriangleCollisionTime();
00235 #endif //TRI_COLLISION_PROFILING
00236 
00238 
00243         void gimpact_vs_gimpact(const btCollisionObjectWrapper* body0Wrap,
00244                                           const btCollisionObjectWrapper * body1Wrap,
00245                                           const btGImpactShapeInterface * shape0,
00246                                           const btGImpactShapeInterface * shape1);
00247 
00248         void gimpact_vs_shape(const btCollisionObjectWrapper* body0Wrap,
00249                                           const btCollisionObjectWrapper* body1Wrap,
00250                                           const btGImpactShapeInterface * shape0,
00251                                           const btCollisionShape * shape1,bool swapped);
00252 
00253         void gimpact_vs_compoundshape(const btCollisionObjectWrapper * body0Wrap,
00254                                           const btCollisionObjectWrapper * body1Wrap,
00255                                           const btGImpactShapeInterface * shape0,
00256                                           const btCompoundShape * shape1,bool swapped);
00257 
00258         void gimpact_vs_concave(
00259                                           const btCollisionObjectWrapper * body0Wrap,
00260                                           const btCollisionObjectWrapper * body1Wrap,
00261                                           const btGImpactShapeInterface * shape0,
00262                                           const btConcaveShape * shape1,bool swapped);
00263 
00264 
00265 
00266 
00268     void        setFace0(int value) 
00269     { 
00270         m_triface0 = value; 
00271     }
00272     int getFace0() 
00273     { 
00274         return m_triface0; 
00275     }
00276     void setFace1(int value) 
00277     { 
00278         m_triface1 = value; 
00279     }
00280     int getFace1() 
00281     { 
00282         return m_triface1; 
00283     }
00284     void setPart0(int value) 
00285     { 
00286         m_part0 = value; 
00287     }
00288     int getPart0() 
00289     { 
00290         return m_part0; 
00291     }
00292     void setPart1(int value) 
00293     { 
00294         m_part1 = value; 
00295                 }
00296     int getPart1() 
00297     { 
00298         return m_part1; 
00299     }
00300 
00301 };
00302 
00303 
00304 //algorithm details
00305 //#define BULLET_TRIANGLE_COLLISION 1
00306 #define GIMPACT_VS_PLANE_COLLISION 1
00307 
00308 
00309 
00310 #endif //BT_GIMPACT_BVH_CONCAVE_COLLISION_ALGORITHM_H