btPersistentManifold.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 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.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. #ifndef BT_PERSISTENT_MANIFOLD_H
  14. #define BT_PERSISTENT_MANIFOLD_H
  15. #include "bullet/LinearMath/btVector3.h"
  16. #include "bullet/LinearMath/btTransform.h"
  17. #include "btManifoldPoint.h"
  18. class btCollisionObject;
  19. #include "bullet/LinearMath/btAlignedAllocator.h"
  20. struct btCollisionResult;
  21. ///maximum contact breaking and merging threshold
  22. extern btScalar gContactBreakingThreshold;
  23. typedef bool (*ContactDestroyedCallback)(void* userPersistentData);
  24. typedef bool (*ContactProcessedCallback)(btManifoldPoint& cp,void* body0,void* body1);
  25. extern ContactDestroyedCallback gContactDestroyedCallback;
  26. extern ContactProcessedCallback gContactProcessedCallback;
  27. //the enum starts at 1024 to avoid type conflicts with btTypedConstraint
  28. enum btContactManifoldTypes
  29. {
  30. MIN_CONTACT_MANIFOLD_TYPE = 1024,
  31. BT_PERSISTENT_MANIFOLD_TYPE
  32. };
  33. #define MANIFOLD_CACHE_SIZE 4
  34. ///btPersistentManifold is a contact point cache, it stays persistent as long as objects are overlapping in the broadphase.
  35. ///Those contact points are created by the collision narrow phase.
  36. ///The cache can be empty, or hold 1,2,3 or 4 points. Some collision algorithms (GJK) might only add one point at a time.
  37. ///updates/refreshes old contact points, and throw them away if necessary (distance becomes too large)
  38. ///reduces the cache to 4 points, when more then 4 points are added, using following rules:
  39. ///the contact point with deepest penetration is always kept, and it tries to maximuze the area covered by the points
  40. ///note that some pairs of objects might have more then one contact manifold.
  41. ATTRIBUTE_ALIGNED128( class) btPersistentManifold : public btTypedObject
  42. //ATTRIBUTE_ALIGNED16( class) btPersistentManifold : public btTypedObject
  43. {
  44. btManifoldPoint m_pointCache[MANIFOLD_CACHE_SIZE];
  45. /// this two body pointers can point to the physics rigidbody class.
  46. const btCollisionObject* m_body0;
  47. const btCollisionObject* m_body1;
  48. int m_cachedPoints;
  49. btScalar m_contactBreakingThreshold;
  50. btScalar m_contactProcessingThreshold;
  51. /// sort cached points so most isolated points come first
  52. int sortCachedPoints(const btManifoldPoint& pt);
  53. int findContactPoint(const btManifoldPoint* unUsed, int numUnused,const btManifoldPoint& pt);
  54. public:
  55. BT_DECLARE_ALIGNED_ALLOCATOR();
  56. int m_companionIdA;
  57. int m_companionIdB;
  58. int m_index1a;
  59. btPersistentManifold();
  60. btPersistentManifold(const btCollisionObject* body0,const btCollisionObject* body1,int , btScalar contactBreakingThreshold,btScalar contactProcessingThreshold)
  61. : btTypedObject(BT_PERSISTENT_MANIFOLD_TYPE),
  62. m_body0(body0),m_body1(body1),m_cachedPoints(0),
  63. m_contactBreakingThreshold(contactBreakingThreshold),
  64. m_contactProcessingThreshold(contactProcessingThreshold)
  65. {
  66. }
  67. SIMD_FORCE_INLINE const btCollisionObject* getBody0() const { return m_body0;}
  68. SIMD_FORCE_INLINE const btCollisionObject* getBody1() const { return m_body1;}
  69. void setBodies(const btCollisionObject* body0,const btCollisionObject* body1)
  70. {
  71. m_body0 = body0;
  72. m_body1 = body1;
  73. }
  74. void clearUserCache(btManifoldPoint& pt);
  75. #ifdef DEBUG_PERSISTENCY
  76. void DebugPersistency();
  77. #endif //
  78. SIMD_FORCE_INLINE int getNumContacts() const { return m_cachedPoints;}
  79. /// the setNumContacts API is usually not used, except when you gather/fill all contacts manually
  80. void setNumContacts(int cachedPoints)
  81. {
  82. m_cachedPoints = cachedPoints;
  83. }
  84. SIMD_FORCE_INLINE const btManifoldPoint& getContactPoint(int index) const
  85. {
  86. btAssert(index < m_cachedPoints);
  87. return m_pointCache[index];
  88. }
  89. SIMD_FORCE_INLINE btManifoldPoint& getContactPoint(int index)
  90. {
  91. btAssert(index < m_cachedPoints);
  92. return m_pointCache[index];
  93. }
  94. ///@todo: get this margin from the current physics / collision environment
  95. btScalar getContactBreakingThreshold() const;
  96. btScalar getContactProcessingThreshold() const
  97. {
  98. return m_contactProcessingThreshold;
  99. }
  100. void setContactBreakingThreshold(btScalar contactBreakingThreshold)
  101. {
  102. m_contactBreakingThreshold = contactBreakingThreshold;
  103. }
  104. void setContactProcessingThreshold(btScalar contactProcessingThreshold)
  105. {
  106. m_contactProcessingThreshold = contactProcessingThreshold;
  107. }
  108. int getCacheEntry(const btManifoldPoint& newPoint) const;
  109. int addManifoldPoint( const btManifoldPoint& newPoint, bool isPredictive=false);
  110. void removeContactPoint (int index)
  111. {
  112. clearUserCache(m_pointCache[index]);
  113. int lastUsedIndex = getNumContacts() - 1;
  114. // m_pointCache[index] = m_pointCache[lastUsedIndex];
  115. if(index != lastUsedIndex)
  116. {
  117. m_pointCache[index] = m_pointCache[lastUsedIndex];
  118. //get rid of duplicated userPersistentData pointer
  119. m_pointCache[lastUsedIndex].m_userPersistentData = 0;
  120. m_pointCache[lastUsedIndex].m_appliedImpulse = 0.f;
  121. m_pointCache[lastUsedIndex].m_lateralFrictionInitialized = false;
  122. m_pointCache[lastUsedIndex].m_appliedImpulseLateral1 = 0.f;
  123. m_pointCache[lastUsedIndex].m_appliedImpulseLateral2 = 0.f;
  124. m_pointCache[lastUsedIndex].m_lifeTime = 0;
  125. }
  126. btAssert(m_pointCache[lastUsedIndex].m_userPersistentData==0);
  127. m_cachedPoints--;
  128. }
  129. void replaceContactPoint(const btManifoldPoint& newPoint,int insertIndex)
  130. {
  131. btAssert(validContactDistance(newPoint));
  132. #define MAINTAIN_PERSISTENCY 1
  133. #ifdef MAINTAIN_PERSISTENCY
  134. int lifeTime = m_pointCache[insertIndex].getLifeTime();
  135. btScalar appliedImpulse = m_pointCache[insertIndex].m_appliedImpulse;
  136. btScalar appliedLateralImpulse1 = m_pointCache[insertIndex].m_appliedImpulseLateral1;
  137. btScalar appliedLateralImpulse2 = m_pointCache[insertIndex].m_appliedImpulseLateral2;
  138. // bool isLateralFrictionInitialized = m_pointCache[insertIndex].m_lateralFrictionInitialized;
  139. btAssert(lifeTime>=0);
  140. void* cache = m_pointCache[insertIndex].m_userPersistentData;
  141. m_pointCache[insertIndex] = newPoint;
  142. m_pointCache[insertIndex].m_userPersistentData = cache;
  143. m_pointCache[insertIndex].m_appliedImpulse = appliedImpulse;
  144. m_pointCache[insertIndex].m_appliedImpulseLateral1 = appliedLateralImpulse1;
  145. m_pointCache[insertIndex].m_appliedImpulseLateral2 = appliedLateralImpulse2;
  146. m_pointCache[insertIndex].m_appliedImpulse = appliedImpulse;
  147. m_pointCache[insertIndex].m_appliedImpulseLateral1 = appliedLateralImpulse1;
  148. m_pointCache[insertIndex].m_appliedImpulseLateral2 = appliedLateralImpulse2;
  149. m_pointCache[insertIndex].m_lifeTime = lifeTime;
  150. #else
  151. clearUserCache(m_pointCache[insertIndex]);
  152. m_pointCache[insertIndex] = newPoint;
  153. #endif
  154. }
  155. bool validContactDistance(const btManifoldPoint& pt) const
  156. {
  157. return pt.m_distance1 <= getContactBreakingThreshold();
  158. }
  159. /// calculated new worldspace coordinates and depth, and reject points that exceed the collision margin
  160. void refreshContactPoints( const btTransform& trA,const btTransform& trB);
  161. SIMD_FORCE_INLINE void clearManifold()
  162. {
  163. int i;
  164. for (i=0;i<m_cachedPoints;i++)
  165. {
  166. clearUserCache(m_pointCache[i]);
  167. }
  168. m_cachedPoints = 0;
  169. }
  170. }
  171. ;
  172. #endif //BT_PERSISTENT_MANIFOLD_H