btPersistentManifold.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. #include "btPersistentManifold.h"
  14. #include "bullet/LinearMath/btTransform.h"
  15. btScalar gContactBreakingThreshold = btScalar(0.02);
  16. ContactDestroyedCallback gContactDestroyedCallback = 0;
  17. ContactProcessedCallback gContactProcessedCallback = 0;
  18. ///gContactCalcArea3Points will approximate the convex hull area using 3 points
  19. ///when setting it to false, it will use 4 points to compute the area: it is more accurate but slower
  20. bool gContactCalcArea3Points = true;
  21. btPersistentManifold::btPersistentManifold()
  22. :btTypedObject(BT_PERSISTENT_MANIFOLD_TYPE),
  23. m_body0(0),
  24. m_body1(0),
  25. m_cachedPoints (0),
  26. m_index1a(0)
  27. {
  28. }
  29. #ifdef DEBUG_PERSISTENCY
  30. #include <stdio.h>
  31. void btPersistentManifold::DebugPersistency()
  32. {
  33. int i;
  34. printf("DebugPersistency : numPoints %d\n",m_cachedPoints);
  35. for (i=0;i<m_cachedPoints;i++)
  36. {
  37. printf("m_pointCache[%d].m_userPersistentData = %x\n",i,m_pointCache[i].m_userPersistentData);
  38. }
  39. }
  40. #endif //DEBUG_PERSISTENCY
  41. void btPersistentManifold::clearUserCache(btManifoldPoint& pt)
  42. {
  43. void* oldPtr = pt.m_userPersistentData;
  44. if (oldPtr)
  45. {
  46. #ifdef DEBUG_PERSISTENCY
  47. int i;
  48. int occurance = 0;
  49. for (i=0;i<m_cachedPoints;i++)
  50. {
  51. if (m_pointCache[i].m_userPersistentData == oldPtr)
  52. {
  53. occurance++;
  54. if (occurance>1)
  55. printf("error in clearUserCache\n");
  56. }
  57. }
  58. btAssert(occurance<=0);
  59. #endif //DEBUG_PERSISTENCY
  60. if (pt.m_userPersistentData && gContactDestroyedCallback)
  61. {
  62. (*gContactDestroyedCallback)(pt.m_userPersistentData);
  63. pt.m_userPersistentData = 0;
  64. }
  65. #ifdef DEBUG_PERSISTENCY
  66. DebugPersistency();
  67. #endif
  68. }
  69. }
  70. static inline btScalar calcArea4Points(const btVector3 &p0,const btVector3 &p1,const btVector3 &p2,const btVector3 &p3)
  71. {
  72. // It calculates possible 3 area constructed from random 4 points and returns the biggest one.
  73. btVector3 a[3],b[3];
  74. a[0] = p0 - p1;
  75. a[1] = p0 - p2;
  76. a[2] = p0 - p3;
  77. b[0] = p2 - p3;
  78. b[1] = p1 - p3;
  79. b[2] = p1 - p2;
  80. //todo: Following 3 cross production can be easily optimized by SIMD.
  81. btVector3 tmp0 = a[0].cross(b[0]);
  82. btVector3 tmp1 = a[1].cross(b[1]);
  83. btVector3 tmp2 = a[2].cross(b[2]);
  84. return btMax(btMax(tmp0.length2(),tmp1.length2()),tmp2.length2());
  85. }
  86. int btPersistentManifold::sortCachedPoints(const btManifoldPoint& pt)
  87. {
  88. //calculate 4 possible cases areas, and take biggest area
  89. //also need to keep 'deepest'
  90. int maxPenetrationIndex = -1;
  91. #define KEEP_DEEPEST_POINT 1
  92. #ifdef KEEP_DEEPEST_POINT
  93. btScalar maxPenetration = pt.getDistance();
  94. for (int i=0;i<4;i++)
  95. {
  96. if (m_pointCache[i].getDistance() < maxPenetration)
  97. {
  98. maxPenetrationIndex = i;
  99. maxPenetration = m_pointCache[i].getDistance();
  100. }
  101. }
  102. #endif //KEEP_DEEPEST_POINT
  103. btScalar res0(btScalar(0.)),res1(btScalar(0.)),res2(btScalar(0.)),res3(btScalar(0.));
  104. if (gContactCalcArea3Points)
  105. {
  106. if (maxPenetrationIndex != 0)
  107. {
  108. btVector3 a0 = pt.m_localPointA-m_pointCache[1].m_localPointA;
  109. btVector3 b0 = m_pointCache[3].m_localPointA-m_pointCache[2].m_localPointA;
  110. btVector3 cross = a0.cross(b0);
  111. res0 = cross.length2();
  112. }
  113. if (maxPenetrationIndex != 1)
  114. {
  115. btVector3 a1 = pt.m_localPointA-m_pointCache[0].m_localPointA;
  116. btVector3 b1 = m_pointCache[3].m_localPointA-m_pointCache[2].m_localPointA;
  117. btVector3 cross = a1.cross(b1);
  118. res1 = cross.length2();
  119. }
  120. if (maxPenetrationIndex != 2)
  121. {
  122. btVector3 a2 = pt.m_localPointA-m_pointCache[0].m_localPointA;
  123. btVector3 b2 = m_pointCache[3].m_localPointA-m_pointCache[1].m_localPointA;
  124. btVector3 cross = a2.cross(b2);
  125. res2 = cross.length2();
  126. }
  127. if (maxPenetrationIndex != 3)
  128. {
  129. btVector3 a3 = pt.m_localPointA-m_pointCache[0].m_localPointA;
  130. btVector3 b3 = m_pointCache[2].m_localPointA-m_pointCache[1].m_localPointA;
  131. btVector3 cross = a3.cross(b3);
  132. res3 = cross.length2();
  133. }
  134. }
  135. else
  136. {
  137. if(maxPenetrationIndex != 0) {
  138. res0 = calcArea4Points(pt.m_localPointA,m_pointCache[1].m_localPointA,m_pointCache[2].m_localPointA,m_pointCache[3].m_localPointA);
  139. }
  140. if(maxPenetrationIndex != 1) {
  141. res1 = calcArea4Points(pt.m_localPointA,m_pointCache[0].m_localPointA,m_pointCache[2].m_localPointA,m_pointCache[3].m_localPointA);
  142. }
  143. if(maxPenetrationIndex != 2) {
  144. res2 = calcArea4Points(pt.m_localPointA,m_pointCache[0].m_localPointA,m_pointCache[1].m_localPointA,m_pointCache[3].m_localPointA);
  145. }
  146. if(maxPenetrationIndex != 3) {
  147. res3 = calcArea4Points(pt.m_localPointA,m_pointCache[0].m_localPointA,m_pointCache[1].m_localPointA,m_pointCache[2].m_localPointA);
  148. }
  149. }
  150. btVector4 maxvec(res0,res1,res2,res3);
  151. int biggestarea = maxvec.closestAxis4();
  152. return biggestarea;
  153. }
  154. int btPersistentManifold::getCacheEntry(const btManifoldPoint& newPoint) const
  155. {
  156. btScalar shortestDist = getContactBreakingThreshold() * getContactBreakingThreshold();
  157. int size = getNumContacts();
  158. int nearestPoint = -1;
  159. for( int i = 0; i < size; i++ )
  160. {
  161. const btManifoldPoint &mp = m_pointCache[i];
  162. btVector3 diffA = mp.m_localPointA- newPoint.m_localPointA;
  163. const btScalar distToManiPoint = diffA.dot(diffA);
  164. if( distToManiPoint < shortestDist )
  165. {
  166. shortestDist = distToManiPoint;
  167. nearestPoint = i;
  168. }
  169. }
  170. return nearestPoint;
  171. }
  172. int btPersistentManifold::addManifoldPoint(const btManifoldPoint& newPoint, bool isPredictive)
  173. {
  174. if (!isPredictive)
  175. {
  176. btAssert(validContactDistance(newPoint));
  177. }
  178. int insertIndex = getNumContacts();
  179. if (insertIndex == MANIFOLD_CACHE_SIZE)
  180. {
  181. #if MANIFOLD_CACHE_SIZE >= 4
  182. //sort cache so best points come first, based on area
  183. insertIndex = sortCachedPoints(newPoint);
  184. #else
  185. insertIndex = 0;
  186. #endif
  187. clearUserCache(m_pointCache[insertIndex]);
  188. } else
  189. {
  190. m_cachedPoints++;
  191. }
  192. if (insertIndex<0)
  193. insertIndex=0;
  194. btAssert(m_pointCache[insertIndex].m_userPersistentData==0);
  195. m_pointCache[insertIndex] = newPoint;
  196. return insertIndex;
  197. }
  198. btScalar btPersistentManifold::getContactBreakingThreshold() const
  199. {
  200. return m_contactBreakingThreshold;
  201. }
  202. void btPersistentManifold::refreshContactPoints(const btTransform& trA,const btTransform& trB)
  203. {
  204. int i;
  205. #ifdef DEBUG_PERSISTENCY
  206. printf("refreshContactPoints posA = (%f,%f,%f) posB = (%f,%f,%f)\n",
  207. trA.getOrigin().getX(),
  208. trA.getOrigin().getY(),
  209. trA.getOrigin().getZ(),
  210. trB.getOrigin().getX(),
  211. trB.getOrigin().getY(),
  212. trB.getOrigin().getZ());
  213. #endif //DEBUG_PERSISTENCY
  214. /// first refresh worldspace positions and distance
  215. for (i=getNumContacts()-1;i>=0;i--)
  216. {
  217. btManifoldPoint &manifoldPoint = m_pointCache[i];
  218. manifoldPoint.m_positionWorldOnA = trA( manifoldPoint.m_localPointA );
  219. manifoldPoint.m_positionWorldOnB = trB( manifoldPoint.m_localPointB );
  220. manifoldPoint.m_distance1 = (manifoldPoint.m_positionWorldOnA - manifoldPoint.m_positionWorldOnB).dot(manifoldPoint.m_normalWorldOnB);
  221. manifoldPoint.m_lifeTime++;
  222. }
  223. /// then
  224. btScalar distance2d;
  225. btVector3 projectedDifference,projectedPoint;
  226. for (i=getNumContacts()-1;i>=0;i--)
  227. {
  228. btManifoldPoint &manifoldPoint = m_pointCache[i];
  229. //contact becomes invalid when signed distance exceeds margin (projected on contactnormal direction)
  230. if (!validContactDistance(manifoldPoint))
  231. {
  232. removeContactPoint(i);
  233. } else
  234. {
  235. //contact also becomes invalid when relative movement orthogonal to normal exceeds margin
  236. projectedPoint = manifoldPoint.m_positionWorldOnA - manifoldPoint.m_normalWorldOnB * manifoldPoint.m_distance1;
  237. projectedDifference = manifoldPoint.m_positionWorldOnB - projectedPoint;
  238. distance2d = projectedDifference.dot(projectedDifference);
  239. if (distance2d > getContactBreakingThreshold()*getContactBreakingThreshold() )
  240. {
  241. removeContactPoint(i);
  242. } else
  243. {
  244. //contact point processed callback
  245. if (gContactProcessedCallback)
  246. (*gContactProcessedCallback)(manifoldPoint,(void*)m_body0,(void*)m_body1);
  247. }
  248. }
  249. }
  250. #ifdef DEBUG_PERSISTENCY
  251. DebugPersistency();
  252. #endif //
  253. }