Back to index

supertuxkart  0.5+dfsg1
btPersistentManifold.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 #ifndef PERSISTENT_MANIFOLD_H
00017 #define PERSISTENT_MANIFOLD_H
00018 
00019 
00020 #include "LinearMath/btVector3.h"
00021 #include "LinearMath/btTransform.h"
00022 #include "btManifoldPoint.h"
00023 #include "LinearMath/btAlignedAllocator.h"
00024 
00025 struct btCollisionResult;
00026 
00028 extern btScalar gContactBreakingThreshold;
00029 
00030 typedef bool (*ContactDestroyedCallback)(void* userPersistentData);
00031 typedef bool (*ContactProcessedCallback)(btManifoldPoint& cp,void* body0,void* body1);
00032 extern ContactDestroyedCallback    gContactDestroyedCallback;
00033 
00034 
00035 
00036 
00037 #define MANIFOLD_CACHE_SIZE 4
00038 
00046 ATTRIBUTE_ALIGNED16( class) btPersistentManifold 
00047 {
00048 
00049        btManifoldPoint m_pointCache[MANIFOLD_CACHE_SIZE];
00050 
00053        void* m_body0;
00054        void* m_body1;
00055        int    m_cachedPoints;
00056 
00057        
00059        int    sortCachedPoints(const btManifoldPoint& pt);
00060 
00061        int           findContactPoint(const btManifoldPoint* unUsed, int numUnused,const btManifoldPoint& pt);
00062 
00063 public:
00064 
00065        BT_DECLARE_ALIGNED_ALLOCATOR();
00066 
00067        int m_index1a;
00068 
00069        btPersistentManifold();
00070 
00071        btPersistentManifold(void* body0,void* body1,int bla)
00072               : m_body0(body0),m_body1(body1),m_cachedPoints(0)
00073        {
00074               (void)bla;
00075        }
00076 
00077        SIMD_FORCE_INLINE void* getBody0() { return m_body0;}
00078        SIMD_FORCE_INLINE void* getBody1() { return m_body1;}
00079 
00080        SIMD_FORCE_INLINE const void* getBody0() const { return m_body0;}
00081        SIMD_FORCE_INLINE const void* getBody1() const { return m_body1;}
00082 
00083        void   setBodies(void* body0,void* body1)
00084        {
00085               m_body0 = body0;
00086               m_body1 = body1;
00087        }
00088 
00089        void clearUserCache(btManifoldPoint& pt);
00090 
00091 #ifdef DEBUG_PERSISTENCY
00092        void   DebugPersistency();
00093 #endif //
00094        
00095        SIMD_FORCE_INLINE int       getNumContacts() const { return m_cachedPoints;}
00096 
00097        SIMD_FORCE_INLINE const btManifoldPoint& getContactPoint(int index) const
00098        {
00099               btAssert(index < m_cachedPoints);
00100               return m_pointCache[index];
00101        }
00102 
00103        SIMD_FORCE_INLINE btManifoldPoint& getContactPoint(int index)
00104        {
00105               btAssert(index < m_cachedPoints);
00106               return m_pointCache[index];
00107        }
00108 
00110        btScalar      getContactBreakingThreshold() const;
00111        
00112        int getCacheEntry(const btManifoldPoint& newPoint) const;
00113 
00114        void AddManifoldPoint( const btManifoldPoint& newPoint);
00115 
00116        void removeContactPoint (int index)
00117        {
00118               clearUserCache(m_pointCache[index]);
00119 
00120               int lastUsedIndex = getNumContacts() - 1;
00121 //            m_pointCache[index] = m_pointCache[lastUsedIndex];
00122               if(index != lastUsedIndex) 
00123               {
00124                      m_pointCache[index] = m_pointCache[lastUsedIndex]; 
00125                      //get rid of duplicated userPersistentData pointer
00126                      m_pointCache[lastUsedIndex].m_userPersistentData = 0;
00127                      m_pointCache[lastUsedIndex].m_appliedImpulse = 0.f;
00128                      m_pointCache[lastUsedIndex].m_lifeTime = 0;
00129               }
00130 
00131               btAssert(m_pointCache[lastUsedIndex].m_userPersistentData==0);
00132               m_cachedPoints--;
00133        }
00134        void replaceContactPoint(const btManifoldPoint& newPoint,int insertIndex)
00135        {
00136               btAssert(validContactDistance(newPoint));
00137 
00138 #define MAINTAIN_PERSISTENCY 1
00139 #ifdef MAINTAIN_PERSISTENCY
00140               int    lifeTime = m_pointCache[insertIndex].getLifeTime();
00141               btScalar      appliedImpulse = 0.f;//m_pointCache[insertIndex].m_appliedImpulse;
00142               btAssert(lifeTime>=0);
00143               void* cache = m_pointCache[insertIndex].m_userPersistentData;
00144               
00145               m_pointCache[insertIndex] = newPoint;
00146 
00147               m_pointCache[insertIndex].m_userPersistentData = cache;
00148               m_pointCache[insertIndex].m_appliedImpulse = appliedImpulse;
00149               m_pointCache[insertIndex].m_lifeTime = lifeTime;
00150 #else
00151               clearUserCache(m_pointCache[insertIndex]);
00152               m_pointCache[insertIndex] = newPoint;
00153        
00154 #endif
00155        }
00156 
00157        bool validContactDistance(const btManifoldPoint& pt) const
00158        {
00159               return pt.m_distance1 <= getContactBreakingThreshold();
00160        }
00162        void   refreshContactPoints(  const btTransform& trA,const btTransform& trB);
00163 
00164        
00165        SIMD_FORCE_INLINE    void   clearManifold()
00166        {
00167               int i;
00168               for (i=0;i<m_cachedPoints;i++)
00169               {
00170                      clearUserCache(m_pointCache[i]);
00171               }
00172               m_cachedPoints = 0;
00173        }
00174 
00175 
00176 
00177 }
00178 ;
00179 
00180 
00181 
00182 
00183 
00184 #endif //PERSISTENT_MANIFOLD_H