Back to index

supertuxkart  0.5+dfsg1
btCollisionWorld.h
Go to the documentation of this file.
00001 /*
00002 Bullet Continuous Collision Detection and Physics Library
00003 Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/
00004 
00005 This software is provided 'as-is', without any express or implied warranty.
00006 In no event will the authors be held liable for any damages arising from the use of this software.
00007 Permission is granted to anyone to use this software for any purpose, 
00008 including commercial applications, and to alter it and redistribute it freely, 
00009 subject to the following restrictions:
00010 
00011 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.
00012 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
00013 3. This notice may not be removed or altered from any source distribution.
00014 */
00015 
00016 
00064 #ifndef COLLISION_WORLD_H
00065 #define COLLISION_WORLD_H
00066 
00067 class btStackAlloc;
00068 class btCollisionShape;
00069 class btConvexShape;
00070 class btBroadphaseInterface;
00071 #include "LinearMath/btVector3.h"
00072 #include "LinearMath/btTransform.h"
00073 #include "btCollisionObject.h"
00074 #include "btCollisionDispatcher.h" //for definition of btCollisionObjectArray
00075 #include "BulletCollision/BroadphaseCollision/btOverlappingPairCache.h"
00076 #include "LinearMath/btAlignedObjectArray.h"
00077 
00079 class btCollisionWorld
00080 {
00081 
00082        
00083 protected:
00084 
00085        btAlignedObjectArray<btCollisionObject*>  m_collisionObjects;
00086        
00087        btDispatcher* m_dispatcher1;
00088 
00089        btDispatcherInfo     m_dispatchInfo;
00090 
00091        btStackAlloc* m_stackAlloc;
00092 
00093        btBroadphaseInterface*      m_broadphasePairCache;
00094 
00095        btIDebugDraw* m_debugDrawer;
00096 
00097        
00098 public:
00099 
00100        //this constructor doesn't own the dispatcher and paircache/broadphase
00101        btCollisionWorld(btDispatcher* dispatcher,btBroadphaseInterface* broadphasePairCache, btCollisionConfiguration* collisionConfiguration);
00102 
00103        virtual ~btCollisionWorld();
00104 
00105 
00106        btBroadphaseInterface*      getBroadphase()
00107        {
00108               return m_broadphasePairCache;
00109        }
00110 
00111        btOverlappingPairCache*     getPairCache()
00112        {
00113               return m_broadphasePairCache->getOverlappingPairCache();
00114        }
00115 
00116 
00117        btDispatcher* getDispatcher()
00118        {
00119               return m_dispatcher1;
00120        }
00121 
00122        virtual void  updateAabbs();
00123 
00124        virtual void  setDebugDrawer(btIDebugDraw*       debugDrawer)
00125        {
00126                      m_debugDrawer = debugDrawer;
00127        }
00128 
00129        virtual btIDebugDraw*       getDebugDrawer()
00130        {
00131               return m_debugDrawer;
00132        }
00133 
00134 
00137        struct LocalShapeInfo
00138        {
00139               int    m_shapePart;
00140               int    m_triangleIndex;
00141               
00142               //const btCollisionShape*   m_shapeTemp;
00143               //const btTransform* m_shapeLocalTransform;
00144        };
00145 
00146        struct LocalRayResult
00147        {
00148               LocalRayResult(btCollisionObject*  collisionObject, 
00149                      LocalShapeInfo*      localShapeInfo,
00150                      const btVector3&            hitNormalLocal,
00151                      btScalar hitFraction)
00152               :m_collisionObject(collisionObject),
00153               m_localShapeInfo(localShapeInfo),
00154               m_hitNormalLocal(hitNormalLocal),
00155               m_hitFraction(hitFraction)
00156               {
00157               }
00158 
00159               btCollisionObject*          m_collisionObject;
00160               LocalShapeInfo*                    m_localShapeInfo;
00161               btVector3                          m_hitNormalLocal;
00162               btScalar                           m_hitFraction;
00163 
00164        };
00165 
00167        struct RayResultCallback
00168        {
00169               btScalar      m_closestHitFraction;
00170               btCollisionObject*          m_collisionObject;
00171 
00172               virtual ~RayResultCallback()
00173               {
00174               }
00175               bool   HasHit()
00176               {
00177                      return (m_collisionObject != 0);
00178               }
00179 
00180               RayResultCallback()
00181                      :m_closestHitFraction(btScalar(1.)),
00182                      m_collisionObject(0)
00183               {
00184               }
00185               virtual       btScalar      AddSingleResult(LocalRayResult& rayResult,bool normalInWorldSpace) = 0;
00186        };
00187 
00188        struct ClosestRayResultCallback : public RayResultCallback
00189        {
00190               ClosestRayResultCallback(const btVector3& rayFromWorld,const btVector3&      rayToWorld)
00191               :m_rayFromWorld(rayFromWorld),
00192               m_rayToWorld(rayToWorld)
00193               {
00194               }
00195 
00196               btVector3     m_rayFromWorld;//used to calculate hitPointWorld from hitFraction
00197               btVector3     m_rayToWorld;
00198 
00199               btVector3     m_hitNormalWorld;
00200               btVector3     m_hitPointWorld;
00201                      
00202               virtual       btScalar      AddSingleResult(LocalRayResult& rayResult,bool normalInWorldSpace)
00203               {
00204                      //caller already does the filter on the m_closestHitFraction
00205                      btAssert(rayResult.m_hitFraction <= m_closestHitFraction);
00206                      
00207                      m_closestHitFraction = rayResult.m_hitFraction;
00208                      m_collisionObject = rayResult.m_collisionObject;
00209                      if (normalInWorldSpace)
00210                      {
00211                             m_hitNormalWorld = rayResult.m_hitNormalLocal;
00212                      } else
00213                      {
00215                             m_hitNormalWorld = m_collisionObject->getWorldTransform().getBasis()*rayResult.m_hitNormalLocal;
00216                      }
00217                      m_hitPointWorld.setInterpolate3(m_rayFromWorld,m_rayToWorld,rayResult.m_hitFraction);
00218                      return rayResult.m_hitFraction;
00219               }
00220        };
00221 
00222 
00223        struct LocalConvexResult
00224        {
00225               LocalConvexResult(btCollisionObject*      hitCollisionObject, 
00226                      LocalShapeInfo*      localShapeInfo,
00227                      const btVector3&            hitNormalLocal,
00228                      const btVector3&            hitPointLocal,
00229                      btScalar hitFraction
00230                      )
00231               :m_hitCollisionObject(hitCollisionObject),
00232               m_localShapeInfo(localShapeInfo),
00233               m_hitNormalLocal(hitNormalLocal),
00234               m_hitPointLocal(hitPointLocal),
00235               m_hitFraction(hitFraction)
00236               {
00237               }
00238 
00239               btCollisionObject*          m_hitCollisionObject;
00240               LocalShapeInfo*                    m_localShapeInfo;
00241               btVector3                          m_hitNormalLocal;
00242               btVector3                          m_hitPointLocal;
00243               btScalar                           m_hitFraction;
00244        };
00245 
00247        struct ConvexResultCallback
00248        {
00249               virtual ~ConvexResultCallback()
00250               {
00251               }
00252               btScalar      m_closestHitFraction;
00253               bool   HasHit()
00254               {
00255                      return (m_closestHitFraction < btScalar(1.));
00256               }
00257 
00258               ConvexResultCallback()
00259                      :m_closestHitFraction(btScalar(1.))
00260               {
00261               }
00262               virtual       btScalar      AddSingleResult(LocalConvexResult& convexResult,bool normalInWorldSpace) = 0;
00263        };
00264 
00265        struct ClosestConvexResultCallback : public ConvexResultCallback
00266        {
00267               ClosestConvexResultCallback(const btVector3&     convexFromWorld,const btVector3&   convexToWorld)
00268               :m_convexFromWorld(convexFromWorld),
00269               m_convexToWorld(convexToWorld),
00270               m_hitCollisionObject(0)
00271               {
00272               }
00273 
00274               btVector3     m_convexFromWorld;//used to calculate hitPointWorld from hitFraction
00275               btVector3     m_convexToWorld;
00276 
00277               btVector3     m_hitNormalWorld;
00278               btVector3     m_hitPointWorld;
00279               btCollisionObject*   m_hitCollisionObject;
00280               
00281               virtual       btScalar      AddSingleResult(LocalConvexResult& convexResult,bool normalInWorldSpace)
00282               {
00283 //caller already does the filter on the m_closestHitFraction
00284                      btAssert(convexResult.m_hitFraction <= m_closestHitFraction);
00285                                           
00286                      m_closestHitFraction = convexResult.m_hitFraction;
00287                      m_hitCollisionObject = convexResult.m_hitCollisionObject;
00288                      if (normalInWorldSpace)
00289                      {
00290                             m_hitNormalWorld = convexResult.m_hitNormalLocal;
00291                      } else
00292                      {
00294                             m_hitNormalWorld = m_hitCollisionObject->getWorldTransform().getBasis()*convexResult.m_hitNormalLocal;
00295                      }
00296                      m_hitPointWorld = convexResult.m_hitPointLocal;
00297                      return convexResult.m_hitFraction;
00298               }
00299        };
00300 
00301        int    getNumCollisionObjects() const
00302        {
00303               return int(m_collisionObjects.size());
00304        }
00305 
00308        void   rayTest(const btVector3& rayFromWorld, const btVector3& rayToWorld, RayResultCallback& resultCallback, short int collisionFilterMask=-1);
00309 
00310        // convexTest performs a swept convex cast on all objects in the btCollisionWorld, and calls the resultCallback
00311        // This allows for several queries: first hit, all hits, any hit, dependent on the value return by the callback.
00312        void    convexSweepTest (const btConvexShape* castShape, const btTransform& from, const btTransform& to, ConvexResultCallback& resultCallback, short int collisionFilterMask=-1);
00313 
00314 
00318        static void   rayTestSingle(const btTransform& rayFromTrans,const btTransform& rayToTrans,
00319                                      btCollisionObject* collisionObject,
00320                                      const btCollisionShape* collisionShape,
00321                                      const btTransform& colObjWorldTransform,
00322                                      RayResultCallback& resultCallback, short int collisionFilterMask=-1);
00323 
00325        static void   objectQuerySingle(const btConvexShape* castShape, const btTransform& rayFromTrans,const btTransform& rayToTrans,
00326                                      btCollisionObject* collisionObject,
00327                                      const btCollisionShape* collisionShape,
00328                                      const btTransform& colObjWorldTransform,
00329                                      ConvexResultCallback& resultCallback, short int collisionFilterMask=-1);
00330 
00331        void   addCollisionObject(btCollisionObject* collisionObject,short int collisionFilterGroup=1,short int collisionFilterMask=1);
00332 
00333        btCollisionObjectArray& getCollisionObjectArray()
00334        {
00335               return m_collisionObjects;
00336        }
00337 
00338        const btCollisionObjectArray& getCollisionObjectArray() const
00339        {
00340               return m_collisionObjects;
00341        }
00342 
00343 
00344        void   removeCollisionObject(btCollisionObject* collisionObject);
00345 
00346        virtual void  performDiscreteCollisionDetection();
00347 
00348        btDispatcherInfo& getDispatchInfo()
00349        {
00350               return m_dispatchInfo;
00351        }
00352 
00353 };
00354 
00355 
00356 #endif //COLLISION_WORLD_H