btSoftRigidDynamicsWorld.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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 "btSoftRigidDynamicsWorld.h"
  14. #include "LinearMath/btQuickprof.h"
  15. //softbody & helpers
  16. #include "btSoftBody.h"
  17. #include "btSoftBodyHelpers.h"
  18. #include "btSoftBodySolvers.h"
  19. #include "btDefaultSoftBodySolver.h"
  20. #include "LinearMath/btSerializer.h"
  21. btSoftRigidDynamicsWorld::btSoftRigidDynamicsWorld(
  22. btDispatcher* dispatcher,
  23. btBroadphaseInterface* pairCache,
  24. btConstraintSolver* constraintSolver,
  25. btCollisionConfiguration* collisionConfiguration,
  26. btSoftBodySolver *softBodySolver ) :
  27. btDiscreteDynamicsWorld(dispatcher,pairCache,constraintSolver,collisionConfiguration),
  28. m_softBodySolver( softBodySolver ),
  29. m_ownsSolver(false)
  30. {
  31. if( !m_softBodySolver )
  32. {
  33. void* ptr = btAlignedAlloc(sizeof(btDefaultSoftBodySolver),16);
  34. m_softBodySolver = new(ptr) btDefaultSoftBodySolver();
  35. m_ownsSolver = true;
  36. }
  37. m_drawFlags = fDrawFlags::Std;
  38. m_drawNodeTree = true;
  39. m_drawFaceTree = false;
  40. m_drawClusterTree = false;
  41. m_sbi.m_broadphase = pairCache;
  42. m_sbi.m_dispatcher = dispatcher;
  43. m_sbi.m_sparsesdf.Initialize();
  44. m_sbi.m_sparsesdf.Reset();
  45. m_sbi.air_density = (btScalar)1.2;
  46. m_sbi.water_density = 0;
  47. m_sbi.water_offset = 0;
  48. m_sbi.water_normal = btVector3(0,0,0);
  49. m_sbi.m_gravity.setValue(0,-10,0);
  50. m_sbi.m_sparsesdf.Initialize();
  51. }
  52. btSoftRigidDynamicsWorld::~btSoftRigidDynamicsWorld()
  53. {
  54. if (m_ownsSolver)
  55. {
  56. m_softBodySolver->~btSoftBodySolver();
  57. btAlignedFree(m_softBodySolver);
  58. }
  59. }
  60. void btSoftRigidDynamicsWorld::predictUnconstraintMotion(btScalar timeStep)
  61. {
  62. btDiscreteDynamicsWorld::predictUnconstraintMotion( timeStep );
  63. {
  64. BT_PROFILE("predictUnconstraintMotionSoftBody");
  65. m_softBodySolver->predictMotion( timeStep );
  66. }
  67. }
  68. void btSoftRigidDynamicsWorld::internalSingleStepSimulation( btScalar timeStep )
  69. {
  70. // Let the solver grab the soft bodies and if necessary optimize for it
  71. m_softBodySolver->optimize( getSoftBodyArray() );
  72. if( !m_softBodySolver->checkInitialized() )
  73. {
  74. btAssert( "Solver initialization failed\n" );
  75. }
  76. btDiscreteDynamicsWorld::internalSingleStepSimulation( timeStep );
  77. ///solve soft bodies constraints
  78. solveSoftBodiesConstraints( timeStep );
  79. //self collisions
  80. for ( int i=0;i<m_softBodies.size();i++)
  81. {
  82. btSoftBody* psb=(btSoftBody*)m_softBodies[i];
  83. psb->defaultCollisionHandler(psb);
  84. }
  85. ///update soft bodies
  86. m_softBodySolver->updateSoftBodies( );
  87. // End solver-wise simulation step
  88. // ///////////////////////////////
  89. }
  90. void btSoftRigidDynamicsWorld::solveSoftBodiesConstraints( btScalar timeStep )
  91. {
  92. BT_PROFILE("solveSoftConstraints");
  93. if(m_softBodies.size())
  94. {
  95. btSoftBody::solveClusters(m_softBodies);
  96. }
  97. // Solve constraints solver-wise
  98. m_softBodySolver->solveConstraints( timeStep * m_softBodySolver->getTimeScale() );
  99. }
  100. void btSoftRigidDynamicsWorld::addSoftBody(btSoftBody* body,short int collisionFilterGroup,short int collisionFilterMask)
  101. {
  102. m_softBodies.push_back(body);
  103. // Set the soft body solver that will deal with this body
  104. // to be the world's solver
  105. body->setSoftBodySolver( m_softBodySolver );
  106. btCollisionWorld::addCollisionObject(body,
  107. collisionFilterGroup,
  108. collisionFilterMask);
  109. }
  110. void btSoftRigidDynamicsWorld::removeSoftBody(btSoftBody* body)
  111. {
  112. m_softBodies.remove(body);
  113. btCollisionWorld::removeCollisionObject(body);
  114. }
  115. void btSoftRigidDynamicsWorld::removeCollisionObject(btCollisionObject* collisionObject)
  116. {
  117. btSoftBody* body = btSoftBody::upcast(collisionObject);
  118. if (body)
  119. removeSoftBody(body);
  120. else
  121. btDiscreteDynamicsWorld::removeCollisionObject(collisionObject);
  122. }
  123. void btSoftRigidDynamicsWorld::debugDrawWorld()
  124. {
  125. btDiscreteDynamicsWorld::debugDrawWorld();
  126. if (getDebugDrawer())
  127. {
  128. int i;
  129. for ( i=0;i<this->m_softBodies.size();i++)
  130. {
  131. btSoftBody* psb=(btSoftBody*)this->m_softBodies[i];
  132. if (getDebugDrawer() && (getDebugDrawer()->getDebugMode() & (btIDebugDraw::DBG_DrawWireframe)))
  133. {
  134. btSoftBodyHelpers::DrawFrame(psb,m_debugDrawer);
  135. btSoftBodyHelpers::Draw(psb,m_debugDrawer,m_drawFlags);
  136. }
  137. if (m_debugDrawer && (m_debugDrawer->getDebugMode() & btIDebugDraw::DBG_DrawAabb))
  138. {
  139. if(m_drawNodeTree) btSoftBodyHelpers::DrawNodeTree(psb,m_debugDrawer);
  140. if(m_drawFaceTree) btSoftBodyHelpers::DrawFaceTree(psb,m_debugDrawer);
  141. if(m_drawClusterTree) btSoftBodyHelpers::DrawClusterTree(psb,m_debugDrawer);
  142. }
  143. }
  144. }
  145. }
  146. struct btSoftSingleRayCallback : public btBroadphaseRayCallback
  147. {
  148. btVector3 m_rayFromWorld;
  149. btVector3 m_rayToWorld;
  150. btTransform m_rayFromTrans;
  151. btTransform m_rayToTrans;
  152. btVector3 m_hitNormal;
  153. const btSoftRigidDynamicsWorld* m_world;
  154. btCollisionWorld::RayResultCallback& m_resultCallback;
  155. btSoftSingleRayCallback(const btVector3& rayFromWorld,const btVector3& rayToWorld,const btSoftRigidDynamicsWorld* world,btCollisionWorld::RayResultCallback& resultCallback)
  156. :m_rayFromWorld(rayFromWorld),
  157. m_rayToWorld(rayToWorld),
  158. m_world(world),
  159. m_resultCallback(resultCallback)
  160. {
  161. m_rayFromTrans.setIdentity();
  162. m_rayFromTrans.setOrigin(m_rayFromWorld);
  163. m_rayToTrans.setIdentity();
  164. m_rayToTrans.setOrigin(m_rayToWorld);
  165. btVector3 rayDir = (rayToWorld-rayFromWorld);
  166. rayDir.normalize ();
  167. ///what about division by zero? --> just set rayDirection[i] to INF/1e30
  168. m_rayDirectionInverse[0] = rayDir[0] == btScalar(0.0) ? btScalar(1e30) : btScalar(1.0) / rayDir[0];
  169. m_rayDirectionInverse[1] = rayDir[1] == btScalar(0.0) ? btScalar(1e30) : btScalar(1.0) / rayDir[1];
  170. m_rayDirectionInverse[2] = rayDir[2] == btScalar(0.0) ? btScalar(1e30) : btScalar(1.0) / rayDir[2];
  171. m_signs[0] = m_rayDirectionInverse[0] < 0.0;
  172. m_signs[1] = m_rayDirectionInverse[1] < 0.0;
  173. m_signs[2] = m_rayDirectionInverse[2] < 0.0;
  174. m_lambda_max = rayDir.dot(m_rayToWorld-m_rayFromWorld);
  175. }
  176. virtual bool process(const btBroadphaseProxy* proxy)
  177. {
  178. ///terminate further ray tests, once the closestHitFraction reached zero
  179. if (m_resultCallback.m_closestHitFraction == btScalar(0.f))
  180. return false;
  181. btCollisionObject* collisionObject = (btCollisionObject*)proxy->m_clientObject;
  182. //only perform raycast if filterMask matches
  183. if(m_resultCallback.needsCollision(collisionObject->getBroadphaseHandle()))
  184. {
  185. //RigidcollisionObject* collisionObject = ctrl->GetRigidcollisionObject();
  186. //btVector3 collisionObjectAabbMin,collisionObjectAabbMax;
  187. #if 0
  188. #ifdef RECALCULATE_AABB
  189. btVector3 collisionObjectAabbMin,collisionObjectAabbMax;
  190. collisionObject->getCollisionShape()->getAabb(collisionObject->getWorldTransform(),collisionObjectAabbMin,collisionObjectAabbMax);
  191. #else
  192. //getBroadphase()->getAabb(collisionObject->getBroadphaseHandle(),collisionObjectAabbMin,collisionObjectAabbMax);
  193. const btVector3& collisionObjectAabbMin = collisionObject->getBroadphaseHandle()->m_aabbMin;
  194. const btVector3& collisionObjectAabbMax = collisionObject->getBroadphaseHandle()->m_aabbMax;
  195. #endif
  196. #endif
  197. //btScalar hitLambda = m_resultCallback.m_closestHitFraction;
  198. //culling already done by broadphase
  199. //if (btRayAabb(m_rayFromWorld,m_rayToWorld,collisionObjectAabbMin,collisionObjectAabbMax,hitLambda,m_hitNormal))
  200. {
  201. m_world->rayTestSingle(m_rayFromTrans,m_rayToTrans,
  202. collisionObject,
  203. collisionObject->getCollisionShape(),
  204. collisionObject->getWorldTransform(),
  205. m_resultCallback);
  206. }
  207. }
  208. return true;
  209. }
  210. };
  211. void btSoftRigidDynamicsWorld::rayTest(const btVector3& rayFromWorld, const btVector3& rayToWorld, RayResultCallback& resultCallback) const
  212. {
  213. BT_PROFILE("rayTest");
  214. /// use the broadphase to accelerate the search for objects, based on their aabb
  215. /// and for each object with ray-aabb overlap, perform an exact ray test
  216. btSoftSingleRayCallback rayCB(rayFromWorld,rayToWorld,this,resultCallback);
  217. #ifndef USE_BRUTEFORCE_RAYBROADPHASE
  218. m_broadphasePairCache->rayTest(rayFromWorld,rayToWorld,rayCB);
  219. #else
  220. for (int i=0;i<this->getNumCollisionObjects();i++)
  221. {
  222. rayCB.process(m_collisionObjects[i]->getBroadphaseHandle());
  223. }
  224. #endif //USE_BRUTEFORCE_RAYBROADPHASE
  225. }
  226. void btSoftRigidDynamicsWorld::rayTestSingle(const btTransform& rayFromTrans,const btTransform& rayToTrans,
  227. btCollisionObject* collisionObject,
  228. const btCollisionShape* collisionShape,
  229. const btTransform& colObjWorldTransform,
  230. RayResultCallback& resultCallback)
  231. {
  232. if (collisionShape->isSoftBody()) {
  233. btSoftBody* softBody = btSoftBody::upcast(collisionObject);
  234. if (softBody) {
  235. btSoftBody::sRayCast softResult;
  236. if (softBody->rayTest(rayFromTrans.getOrigin(), rayToTrans.getOrigin(), softResult))
  237. {
  238. if (softResult.fraction<= resultCallback.m_closestHitFraction)
  239. {
  240. btCollisionWorld::LocalShapeInfo shapeInfo;
  241. shapeInfo.m_shapePart = 0;
  242. shapeInfo.m_triangleIndex = softResult.index;
  243. // get the normal
  244. btVector3 rayDir = rayToTrans.getOrigin() - rayFromTrans.getOrigin();
  245. btVector3 normal=-rayDir;
  246. normal.normalize();
  247. if (softResult.feature == btSoftBody::eFeature::Face)
  248. {
  249. normal = softBody->m_faces[softResult.index].m_normal;
  250. if (normal.dot(rayDir) > 0) {
  251. // normal always point toward origin of the ray
  252. normal = -normal;
  253. }
  254. }
  255. btCollisionWorld::LocalRayResult rayResult
  256. (collisionObject,
  257. &shapeInfo,
  258. normal,
  259. softResult.fraction);
  260. bool normalInWorldSpace = true;
  261. resultCallback.addSingleResult(rayResult,normalInWorldSpace);
  262. }
  263. }
  264. }
  265. }
  266. else {
  267. btCollisionWorld::rayTestSingle(rayFromTrans,rayToTrans,collisionObject,collisionShape,colObjWorldTransform,resultCallback);
  268. }
  269. }
  270. void btSoftRigidDynamicsWorld::serializeSoftBodies(btSerializer* serializer)
  271. {
  272. int i;
  273. //serialize all collision objects
  274. for (i=0;i<m_collisionObjects.size();i++)
  275. {
  276. btCollisionObject* colObj = m_collisionObjects[i];
  277. if (colObj->getInternalType() & btCollisionObject::CO_SOFT_BODY)
  278. {
  279. int len = colObj->calculateSerializeBufferSize();
  280. btChunk* chunk = serializer->allocate(len,1);
  281. const char* structType = colObj->serialize(chunk->m_oldPtr, serializer);
  282. serializer->finalizeChunk(chunk,structType,BT_SOFTBODY_CODE,colObj);
  283. }
  284. }
  285. }
  286. void btSoftRigidDynamicsWorld::serialize(btSerializer* serializer)
  287. {
  288. serializer->startSerialization();
  289. serializeDynamicsWorldInfo( serializer);
  290. serializeSoftBodies(serializer);
  291. serializeRigidBodies(serializer);
  292. serializeCollisionObjects(serializer);
  293. serializer->finishSerialization();
  294. }