btSoftBodyConcaveCollisionAlgorithm.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 "btSoftBodyConcaveCollisionAlgorithm.h"
  14. #include "BulletCollision/CollisionDispatch/btCollisionObject.h"
  15. #include "BulletCollision/CollisionShapes/btMultiSphereShape.h"
  16. #include "BulletCollision/BroadphaseCollision/btBroadphaseProxy.h"
  17. #include "BulletCollision/CollisionShapes/btConcaveShape.h"
  18. #include "BulletCollision/CollisionDispatch/btManifoldResult.h"
  19. #include "BulletCollision/NarrowPhaseCollision/btRaycastCallback.h"
  20. #include "BulletCollision/CollisionShapes/btTriangleShape.h"
  21. #include "BulletCollision/CollisionShapes/btSphereShape.h"
  22. #include "BulletCollision/CollisionShapes/btTetrahedronShape.h"
  23. #include "BulletCollision/CollisionShapes/btConvexHullShape.h"
  24. #include "BulletCollision/CollisionDispatch/btCollisionObjectWrapper.h"
  25. #include "LinearMath/btIDebugDraw.h"
  26. #include "BulletCollision/NarrowPhaseCollision/btSubSimplexConvexCast.h"
  27. #include "BulletSoftBody/btSoftBody.h"
  28. #define BT_SOFTBODY_TRIANGLE_EXTRUSION btScalar(0.06)//make this configurable
  29. btSoftBodyConcaveCollisionAlgorithm::btSoftBodyConcaveCollisionAlgorithm( const btCollisionAlgorithmConstructionInfo& ci, const btCollisionObjectWrapper* body0Wrap,const btCollisionObjectWrapper* body1Wrap,bool isSwapped)
  30. : btCollisionAlgorithm(ci),
  31. m_isSwapped(isSwapped),
  32. m_btSoftBodyTriangleCallback(ci.m_dispatcher1,body0Wrap,body1Wrap,isSwapped)
  33. {
  34. }
  35. btSoftBodyConcaveCollisionAlgorithm::~btSoftBodyConcaveCollisionAlgorithm()
  36. {
  37. }
  38. btSoftBodyTriangleCallback::btSoftBodyTriangleCallback(btDispatcher* dispatcher,const btCollisionObjectWrapper* body0Wrap,const btCollisionObjectWrapper* body1Wrap,bool isSwapped):
  39. m_dispatcher(dispatcher),
  40. m_dispatchInfoPtr(0)
  41. {
  42. m_softBody = (isSwapped? (btSoftBody*)body1Wrap->getCollisionObject():(btSoftBody*)body0Wrap->getCollisionObject());
  43. m_triBody = isSwapped? body0Wrap->getCollisionObject():body1Wrap->getCollisionObject();
  44. //
  45. // create the manifold from the dispatcher 'manifold pool'
  46. //
  47. // m_manifoldPtr = m_dispatcher->getNewManifold(m_convexBody,m_triBody);
  48. clearCache();
  49. }
  50. btSoftBodyTriangleCallback::~btSoftBodyTriangleCallback()
  51. {
  52. clearCache();
  53. // m_dispatcher->releaseManifold( m_manifoldPtr );
  54. }
  55. void btSoftBodyTriangleCallback::clearCache()
  56. {
  57. for (int i=0;i<m_shapeCache.size();i++)
  58. {
  59. btTriIndex* tmp = m_shapeCache.getAtIndex(i);
  60. btAssert(tmp);
  61. btAssert(tmp->m_childShape);
  62. m_softBody->getWorldInfo()->m_sparsesdf.RemoveReferences(tmp->m_childShape);//necessary?
  63. delete tmp->m_childShape;
  64. }
  65. m_shapeCache.clear();
  66. }
  67. void btSoftBodyTriangleCallback::processTriangle(btVector3* triangle,int partId, int triangleIndex)
  68. {
  69. //just for debugging purposes
  70. //printf("triangle %d",m_triangleCount++);
  71. btCollisionAlgorithmConstructionInfo ci;
  72. ci.m_dispatcher1 = m_dispatcher;
  73. ///debug drawing of the overlapping triangles
  74. if (m_dispatchInfoPtr && m_dispatchInfoPtr->m_debugDraw && (m_dispatchInfoPtr->m_debugDraw->getDebugMode() &btIDebugDraw::DBG_DrawWireframe))
  75. {
  76. btVector3 color(1,1,0);
  77. const btTransform& tr = m_triBody->getWorldTransform();
  78. m_dispatchInfoPtr->m_debugDraw->drawLine(tr(triangle[0]),tr(triangle[1]),color);
  79. m_dispatchInfoPtr->m_debugDraw->drawLine(tr(triangle[1]),tr(triangle[2]),color);
  80. m_dispatchInfoPtr->m_debugDraw->drawLine(tr(triangle[2]),tr(triangle[0]),color);
  81. }
  82. btTriIndex triIndex(partId,triangleIndex,0);
  83. btHashKey<btTriIndex> triKey(triIndex.getUid());
  84. btTriIndex* shapeIndex = m_shapeCache[triKey];
  85. if (shapeIndex)
  86. {
  87. btCollisionShape* tm = shapeIndex->m_childShape;
  88. btAssert(tm);
  89. //copy over user pointers to temporary shape
  90. tm->setUserPointer(m_triBody->getCollisionShape()->getUserPointer());
  91. btCollisionObjectWrapper softBody(0,m_softBody->getCollisionShape(),m_softBody,m_softBody->getWorldTransform(),-1,-1);
  92. //btCollisionObjectWrapper triBody(0,tm, ob, btTransform::getIdentity());//ob->getWorldTransform());//??
  93. btCollisionObjectWrapper triBody(0,tm, m_triBody, m_triBody->getWorldTransform(),partId, triangleIndex);
  94. ebtDispatcherQueryType algoType = m_resultOut->m_closestPointDistanceThreshold > 0 ? BT_CLOSEST_POINT_ALGORITHMS : BT_CONTACT_POINT_ALGORITHMS;
  95. btCollisionAlgorithm* colAlgo = ci.m_dispatcher1->findAlgorithm(&softBody,&triBody,0, algoType);//m_manifoldPtr);
  96. colAlgo->processCollision(&softBody,&triBody,*m_dispatchInfoPtr,m_resultOut);
  97. colAlgo->~btCollisionAlgorithm();
  98. ci.m_dispatcher1->freeCollisionAlgorithm(colAlgo);
  99. return;
  100. }
  101. //aabb filter is already applied!
  102. //btCollisionObject* colObj = static_cast<btCollisionObject*>(m_convexProxy->m_clientObject);
  103. // if (m_softBody->getCollisionShape()->getShapeType()==
  104. {
  105. // btVector3 other;
  106. btVector3 normal = (triangle[1]-triangle[0]).cross(triangle[2]-triangle[0]);
  107. normal.normalize();
  108. normal*= BT_SOFTBODY_TRIANGLE_EXTRUSION;
  109. // other=(triangle[0]+triangle[1]+triangle[2])*0.333333f;
  110. // other+=normal*22.f;
  111. btVector3 pts[6] = {triangle[0]+normal,
  112. triangle[1]+normal,
  113. triangle[2]+normal,
  114. triangle[0]-normal,
  115. triangle[1]-normal,
  116. triangle[2]-normal};
  117. btConvexHullShape* tm = new btConvexHullShape(&pts[0].getX(),6);
  118. // btBU_Simplex1to4 tm(triangle[0],triangle[1],triangle[2],other);
  119. //btTriangleShape tm(triangle[0],triangle[1],triangle[2]);
  120. // tm.setMargin(m_collisionMarginTriangle);
  121. //copy over user pointers to temporary shape
  122. tm->setUserPointer(m_triBody->getCollisionShape()->getUserPointer());
  123. btCollisionObjectWrapper softBody(0,m_softBody->getCollisionShape(),m_softBody,m_softBody->getWorldTransform(),-1,-1);
  124. btCollisionObjectWrapper triBody(0,tm, m_triBody, m_triBody->getWorldTransform(),partId, triangleIndex);//btTransform::getIdentity());//??
  125. ebtDispatcherQueryType algoType = m_resultOut->m_closestPointDistanceThreshold > 0 ? BT_CLOSEST_POINT_ALGORITHMS : BT_CONTACT_POINT_ALGORITHMS;
  126. btCollisionAlgorithm* colAlgo = ci.m_dispatcher1->findAlgorithm(&softBody,&triBody,0, algoType);//m_manifoldPtr);
  127. colAlgo->processCollision(&softBody,&triBody,*m_dispatchInfoPtr,m_resultOut);
  128. colAlgo->~btCollisionAlgorithm();
  129. ci.m_dispatcher1->freeCollisionAlgorithm(colAlgo);
  130. triIndex.m_childShape = tm;
  131. m_shapeCache.insert(triKey,triIndex);
  132. }
  133. }
  134. void btSoftBodyTriangleCallback::setTimeStepAndCounters(btScalar collisionMarginTriangle,const btCollisionObjectWrapper* triBodyWrap, const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut)
  135. {
  136. m_dispatchInfoPtr = &dispatchInfo;
  137. m_collisionMarginTriangle = collisionMarginTriangle+btScalar(BT_SOFTBODY_TRIANGLE_EXTRUSION);
  138. m_resultOut = resultOut;
  139. btVector3 aabbWorldSpaceMin,aabbWorldSpaceMax;
  140. m_softBody->getAabb(aabbWorldSpaceMin,aabbWorldSpaceMax);
  141. btVector3 halfExtents = (aabbWorldSpaceMax-aabbWorldSpaceMin)*btScalar(0.5);
  142. btVector3 softBodyCenter = (aabbWorldSpaceMax+aabbWorldSpaceMin)*btScalar(0.5);
  143. btTransform softTransform;
  144. softTransform.setIdentity();
  145. softTransform.setOrigin(softBodyCenter);
  146. btTransform convexInTriangleSpace;
  147. convexInTriangleSpace = triBodyWrap->getWorldTransform().inverse() * softTransform;
  148. btTransformAabb(halfExtents,m_collisionMarginTriangle,convexInTriangleSpace,m_aabbMin,m_aabbMax);
  149. }
  150. void btSoftBodyConcaveCollisionAlgorithm::clearCache()
  151. {
  152. m_btSoftBodyTriangleCallback.clearCache();
  153. }
  154. void btSoftBodyConcaveCollisionAlgorithm::processCollision (const btCollisionObjectWrapper* body0Wrap,const btCollisionObjectWrapper* body1Wrap,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut)
  155. {
  156. //btCollisionObject* convexBody = m_isSwapped ? body1 : body0;
  157. const btCollisionObjectWrapper* triBody = m_isSwapped ? body0Wrap : body1Wrap;
  158. if (triBody->getCollisionShape()->isConcave())
  159. {
  160. const btCollisionObject* triOb = triBody->getCollisionObject();
  161. const btConcaveShape* concaveShape = static_cast<const btConcaveShape*>( triOb->getCollisionShape());
  162. // if (convexBody->getCollisionShape()->isConvex())
  163. {
  164. btScalar collisionMarginTriangle = concaveShape->getMargin();
  165. // resultOut->setPersistentManifold(m_btSoftBodyTriangleCallback.m_manifoldPtr);
  166. m_btSoftBodyTriangleCallback.setTimeStepAndCounters(collisionMarginTriangle,triBody,dispatchInfo,resultOut);
  167. concaveShape->processAllTriangles( &m_btSoftBodyTriangleCallback,m_btSoftBodyTriangleCallback.getAabbMin(),m_btSoftBodyTriangleCallback.getAabbMax());
  168. // resultOut->refreshContactPoints();
  169. }
  170. }
  171. }
  172. btScalar btSoftBodyConcaveCollisionAlgorithm::calculateTimeOfImpact(btCollisionObject* body0,btCollisionObject* body1,const btDispatcherInfo& dispatchInfo,btManifoldResult* resultOut)
  173. {
  174. (void)resultOut;
  175. (void)dispatchInfo;
  176. btCollisionObject* convexbody = m_isSwapped ? body1 : body0;
  177. btCollisionObject* triBody = m_isSwapped ? body0 : body1;
  178. //quick approximation using raycast, todo: hook up to the continuous collision detection (one of the btConvexCast)
  179. //only perform CCD above a certain threshold, this prevents blocking on the long run
  180. //because object in a blocked ccd state (hitfraction<1) get their linear velocity halved each frame...
  181. btScalar squareMot0 = (convexbody->getInterpolationWorldTransform().getOrigin() - convexbody->getWorldTransform().getOrigin()).length2();
  182. if (squareMot0 < convexbody->getCcdSquareMotionThreshold())
  183. {
  184. return btScalar(1.);
  185. }
  186. //const btVector3& from = convexbody->m_worldTransform.getOrigin();
  187. //btVector3 to = convexbody->m_interpolationWorldTransform.getOrigin();
  188. //todo: only do if the motion exceeds the 'radius'
  189. btTransform triInv = triBody->getWorldTransform().inverse();
  190. btTransform convexFromLocal = triInv * convexbody->getWorldTransform();
  191. btTransform convexToLocal = triInv * convexbody->getInterpolationWorldTransform();
  192. struct LocalTriangleSphereCastCallback : public btTriangleCallback
  193. {
  194. btTransform m_ccdSphereFromTrans;
  195. btTransform m_ccdSphereToTrans;
  196. btTransform m_meshTransform;
  197. btScalar m_ccdSphereRadius;
  198. btScalar m_hitFraction;
  199. LocalTriangleSphereCastCallback(const btTransform& from,const btTransform& to,btScalar ccdSphereRadius,btScalar hitFraction)
  200. :m_ccdSphereFromTrans(from),
  201. m_ccdSphereToTrans(to),
  202. m_ccdSphereRadius(ccdSphereRadius),
  203. m_hitFraction(hitFraction)
  204. {
  205. }
  206. virtual void processTriangle(btVector3* triangle, int partId, int triangleIndex)
  207. {
  208. (void)partId;
  209. (void)triangleIndex;
  210. //do a swept sphere for now
  211. btTransform ident;
  212. ident.setIdentity();
  213. btConvexCast::CastResult castResult;
  214. castResult.m_fraction = m_hitFraction;
  215. btSphereShape pointShape(m_ccdSphereRadius);
  216. btTriangleShape triShape(triangle[0],triangle[1],triangle[2]);
  217. btVoronoiSimplexSolver simplexSolver;
  218. btSubsimplexConvexCast convexCaster(&pointShape,&triShape,&simplexSolver);
  219. //GjkConvexCast convexCaster(&pointShape,convexShape,&simplexSolver);
  220. //ContinuousConvexCollision convexCaster(&pointShape,convexShape,&simplexSolver,0);
  221. //local space?
  222. if (convexCaster.calcTimeOfImpact(m_ccdSphereFromTrans,m_ccdSphereToTrans,
  223. ident,ident,castResult))
  224. {
  225. if (m_hitFraction > castResult.m_fraction)
  226. m_hitFraction = castResult.m_fraction;
  227. }
  228. }
  229. };
  230. if (triBody->getCollisionShape()->isConcave())
  231. {
  232. btVector3 rayAabbMin = convexFromLocal.getOrigin();
  233. rayAabbMin.setMin(convexToLocal.getOrigin());
  234. btVector3 rayAabbMax = convexFromLocal.getOrigin();
  235. rayAabbMax.setMax(convexToLocal.getOrigin());
  236. btScalar ccdRadius0 = convexbody->getCcdSweptSphereRadius();
  237. rayAabbMin -= btVector3(ccdRadius0,ccdRadius0,ccdRadius0);
  238. rayAabbMax += btVector3(ccdRadius0,ccdRadius0,ccdRadius0);
  239. btScalar curHitFraction = btScalar(1.); //is this available?
  240. LocalTriangleSphereCastCallback raycastCallback(convexFromLocal,convexToLocal,
  241. convexbody->getCcdSweptSphereRadius(),curHitFraction);
  242. raycastCallback.m_hitFraction = convexbody->getHitFraction();
  243. btCollisionObject* concavebody = triBody;
  244. btConcaveShape* triangleMesh = (btConcaveShape*) concavebody->getCollisionShape();
  245. if (triangleMesh)
  246. {
  247. triangleMesh->processAllTriangles(&raycastCallback,rayAabbMin,rayAabbMax);
  248. }
  249. if (raycastCallback.m_hitFraction < convexbody->getHitFraction())
  250. {
  251. convexbody->setHitFraction( raycastCallback.m_hitFraction);
  252. return raycastCallback.m_hitFraction;
  253. }
  254. }
  255. return btScalar(1.);
  256. }