btMultiBodyDynamicsWorld.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2013 Erwin Coumans http://bulletphysics.org
  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 "btMultiBodyDynamicsWorld.h"
  14. #include "btMultiBodyConstraintSolver.h"
  15. #include "btMultiBody.h"
  16. #include "btMultiBodyLinkCollider.h"
  17. #include "BulletCollision/CollisionDispatch/btSimulationIslandManager.h"
  18. #include "LinearMath/btQuickprof.h"
  19. #include "btMultiBodyConstraint.h"
  20. #include "LinearMath/btIDebugDraw.h"
  21. #include "LinearMath/btSerializer.h"
  22. void btMultiBodyDynamicsWorld::addMultiBody(btMultiBody* body, int group, int mask)
  23. {
  24. m_multiBodies.push_back(body);
  25. }
  26. void btMultiBodyDynamicsWorld::removeMultiBody(btMultiBody* body)
  27. {
  28. m_multiBodies.remove(body);
  29. }
  30. void btMultiBodyDynamicsWorld::calculateSimulationIslands()
  31. {
  32. BT_PROFILE("calculateSimulationIslands");
  33. getSimulationIslandManager()->updateActivationState(getCollisionWorld(),getCollisionWorld()->getDispatcher());
  34. {
  35. //merge islands based on speculative contact manifolds too
  36. for (int i=0;i<this->m_predictiveManifolds.size();i++)
  37. {
  38. btPersistentManifold* manifold = m_predictiveManifolds[i];
  39. const btCollisionObject* colObj0 = manifold->getBody0();
  40. const btCollisionObject* colObj1 = manifold->getBody1();
  41. if (((colObj0) && (!(colObj0)->isStaticOrKinematicObject())) &&
  42. ((colObj1) && (!(colObj1)->isStaticOrKinematicObject())))
  43. {
  44. getSimulationIslandManager()->getUnionFind().unite((colObj0)->getIslandTag(),(colObj1)->getIslandTag());
  45. }
  46. }
  47. }
  48. {
  49. int i;
  50. int numConstraints = int(m_constraints.size());
  51. for (i=0;i< numConstraints ; i++ )
  52. {
  53. btTypedConstraint* constraint = m_constraints[i];
  54. if (constraint->isEnabled())
  55. {
  56. const btRigidBody* colObj0 = &constraint->getRigidBodyA();
  57. const btRigidBody* colObj1 = &constraint->getRigidBodyB();
  58. if (((colObj0) && (!(colObj0)->isStaticOrKinematicObject())) &&
  59. ((colObj1) && (!(colObj1)->isStaticOrKinematicObject())))
  60. {
  61. getSimulationIslandManager()->getUnionFind().unite((colObj0)->getIslandTag(),(colObj1)->getIslandTag());
  62. }
  63. }
  64. }
  65. }
  66. //merge islands linked by Featherstone link colliders
  67. for (int i=0;i<m_multiBodies.size();i++)
  68. {
  69. btMultiBody* body = m_multiBodies[i];
  70. {
  71. btMultiBodyLinkCollider* prev = body->getBaseCollider();
  72. for (int b=0;b<body->getNumLinks();b++)
  73. {
  74. btMultiBodyLinkCollider* cur = body->getLink(b).m_collider;
  75. if (((cur) && (!(cur)->isStaticOrKinematicObject())) &&
  76. ((prev) && (!(prev)->isStaticOrKinematicObject())))
  77. {
  78. int tagPrev = prev->getIslandTag();
  79. int tagCur = cur->getIslandTag();
  80. getSimulationIslandManager()->getUnionFind().unite(tagPrev, tagCur);
  81. }
  82. if (cur && !cur->isStaticOrKinematicObject())
  83. prev = cur;
  84. }
  85. }
  86. }
  87. //merge islands linked by multibody constraints
  88. {
  89. for (int i=0;i<this->m_multiBodyConstraints.size();i++)
  90. {
  91. btMultiBodyConstraint* c = m_multiBodyConstraints[i];
  92. int tagA = c->getIslandIdA();
  93. int tagB = c->getIslandIdB();
  94. if (tagA>=0 && tagB>=0)
  95. getSimulationIslandManager()->getUnionFind().unite(tagA, tagB);
  96. }
  97. }
  98. //Store the island id in each body
  99. getSimulationIslandManager()->storeIslandActivationState(getCollisionWorld());
  100. }
  101. void btMultiBodyDynamicsWorld::updateActivationState(btScalar timeStep)
  102. {
  103. BT_PROFILE("btMultiBodyDynamicsWorld::updateActivationState");
  104. for ( int i=0;i<m_multiBodies.size();i++)
  105. {
  106. btMultiBody* body = m_multiBodies[i];
  107. if (body)
  108. {
  109. body->checkMotionAndSleepIfRequired(timeStep);
  110. if (!body->isAwake())
  111. {
  112. btMultiBodyLinkCollider* col = body->getBaseCollider();
  113. if (col && col->getActivationState() == ACTIVE_TAG)
  114. {
  115. col->setActivationState( WANTS_DEACTIVATION);
  116. col->setDeactivationTime(0.f);
  117. }
  118. for (int b=0;b<body->getNumLinks();b++)
  119. {
  120. btMultiBodyLinkCollider* col = body->getLink(b).m_collider;
  121. if (col && col->getActivationState() == ACTIVE_TAG)
  122. {
  123. col->setActivationState( WANTS_DEACTIVATION);
  124. col->setDeactivationTime(0.f);
  125. }
  126. }
  127. } else
  128. {
  129. btMultiBodyLinkCollider* col = body->getBaseCollider();
  130. if (col && col->getActivationState() != DISABLE_DEACTIVATION)
  131. col->setActivationState( ACTIVE_TAG );
  132. for (int b=0;b<body->getNumLinks();b++)
  133. {
  134. btMultiBodyLinkCollider* col = body->getLink(b).m_collider;
  135. if (col && col->getActivationState() != DISABLE_DEACTIVATION)
  136. col->setActivationState( ACTIVE_TAG );
  137. }
  138. }
  139. }
  140. }
  141. btDiscreteDynamicsWorld::updateActivationState(timeStep);
  142. }
  143. SIMD_FORCE_INLINE int btGetConstraintIslandId2(const btTypedConstraint* lhs)
  144. {
  145. int islandId;
  146. const btCollisionObject& rcolObj0 = lhs->getRigidBodyA();
  147. const btCollisionObject& rcolObj1 = lhs->getRigidBodyB();
  148. islandId= rcolObj0.getIslandTag()>=0?rcolObj0.getIslandTag():rcolObj1.getIslandTag();
  149. return islandId;
  150. }
  151. class btSortConstraintOnIslandPredicate2
  152. {
  153. public:
  154. bool operator() ( const btTypedConstraint* lhs, const btTypedConstraint* rhs ) const
  155. {
  156. int rIslandId0,lIslandId0;
  157. rIslandId0 = btGetConstraintIslandId2(rhs);
  158. lIslandId0 = btGetConstraintIslandId2(lhs);
  159. return lIslandId0 < rIslandId0;
  160. }
  161. };
  162. SIMD_FORCE_INLINE int btGetMultiBodyConstraintIslandId(const btMultiBodyConstraint* lhs)
  163. {
  164. int islandId;
  165. int islandTagA = lhs->getIslandIdA();
  166. int islandTagB = lhs->getIslandIdB();
  167. islandId= islandTagA>=0?islandTagA:islandTagB;
  168. return islandId;
  169. }
  170. class btSortMultiBodyConstraintOnIslandPredicate
  171. {
  172. public:
  173. bool operator() ( const btMultiBodyConstraint* lhs, const btMultiBodyConstraint* rhs ) const
  174. {
  175. int rIslandId0,lIslandId0;
  176. rIslandId0 = btGetMultiBodyConstraintIslandId(rhs);
  177. lIslandId0 = btGetMultiBodyConstraintIslandId(lhs);
  178. return lIslandId0 < rIslandId0;
  179. }
  180. };
  181. struct MultiBodyInplaceSolverIslandCallback : public btSimulationIslandManager::IslandCallback
  182. {
  183. btContactSolverInfo* m_solverInfo;
  184. btMultiBodyConstraintSolver* m_solver;
  185. btMultiBodyConstraint** m_multiBodySortedConstraints;
  186. int m_numMultiBodyConstraints;
  187. btTypedConstraint** m_sortedConstraints;
  188. int m_numConstraints;
  189. btIDebugDraw* m_debugDrawer;
  190. btDispatcher* m_dispatcher;
  191. btAlignedObjectArray<btCollisionObject*> m_bodies;
  192. btAlignedObjectArray<btPersistentManifold*> m_manifolds;
  193. btAlignedObjectArray<btTypedConstraint*> m_constraints;
  194. btAlignedObjectArray<btMultiBodyConstraint*> m_multiBodyConstraints;
  195. MultiBodyInplaceSolverIslandCallback( btMultiBodyConstraintSolver* solver,
  196. btDispatcher* dispatcher)
  197. :m_solverInfo(NULL),
  198. m_solver(solver),
  199. m_multiBodySortedConstraints(NULL),
  200. m_numConstraints(0),
  201. m_debugDrawer(NULL),
  202. m_dispatcher(dispatcher)
  203. {
  204. }
  205. MultiBodyInplaceSolverIslandCallback& operator=(MultiBodyInplaceSolverIslandCallback& other)
  206. {
  207. btAssert(0);
  208. (void)other;
  209. return *this;
  210. }
  211. SIMD_FORCE_INLINE void setup ( btContactSolverInfo* solverInfo, btTypedConstraint** sortedConstraints, int numConstraints, btMultiBodyConstraint** sortedMultiBodyConstraints, int numMultiBodyConstraints, btIDebugDraw* debugDrawer)
  212. {
  213. btAssert(solverInfo);
  214. m_solverInfo = solverInfo;
  215. m_multiBodySortedConstraints = sortedMultiBodyConstraints;
  216. m_numMultiBodyConstraints = numMultiBodyConstraints;
  217. m_sortedConstraints = sortedConstraints;
  218. m_numConstraints = numConstraints;
  219. m_debugDrawer = debugDrawer;
  220. m_bodies.resize (0);
  221. m_manifolds.resize (0);
  222. m_constraints.resize (0);
  223. m_multiBodyConstraints.resize(0);
  224. }
  225. void setMultiBodyConstraintSolver(btMultiBodyConstraintSolver* solver)
  226. {
  227. m_solver = solver;
  228. }
  229. virtual void processIsland(btCollisionObject** bodies,int numBodies,btPersistentManifold** manifolds,int numManifolds, int islandId)
  230. {
  231. if (islandId<0)
  232. {
  233. ///we don't split islands, so all constraints/contact manifolds/bodies are passed into the solver regardless the island id
  234. m_solver->solveMultiBodyGroup( bodies,numBodies,manifolds, numManifolds,m_sortedConstraints, m_numConstraints, &m_multiBodySortedConstraints[0],m_numConstraints,*m_solverInfo,m_debugDrawer,m_dispatcher);
  235. } else
  236. {
  237. //also add all non-contact constraints/joints for this island
  238. btTypedConstraint** startConstraint = 0;
  239. btMultiBodyConstraint** startMultiBodyConstraint = 0;
  240. int numCurConstraints = 0;
  241. int numCurMultiBodyConstraints = 0;
  242. int i;
  243. //find the first constraint for this island
  244. for (i=0;i<m_numConstraints;i++)
  245. {
  246. if (btGetConstraintIslandId2(m_sortedConstraints[i]) == islandId)
  247. {
  248. startConstraint = &m_sortedConstraints[i];
  249. break;
  250. }
  251. }
  252. //count the number of constraints in this island
  253. for (;i<m_numConstraints;i++)
  254. {
  255. if (btGetConstraintIslandId2(m_sortedConstraints[i]) == islandId)
  256. {
  257. numCurConstraints++;
  258. }
  259. }
  260. for (i=0;i<m_numMultiBodyConstraints;i++)
  261. {
  262. if (btGetMultiBodyConstraintIslandId(m_multiBodySortedConstraints[i]) == islandId)
  263. {
  264. startMultiBodyConstraint = &m_multiBodySortedConstraints[i];
  265. break;
  266. }
  267. }
  268. //count the number of multi body constraints in this island
  269. for (;i<m_numMultiBodyConstraints;i++)
  270. {
  271. if (btGetMultiBodyConstraintIslandId(m_multiBodySortedConstraints[i]) == islandId)
  272. {
  273. numCurMultiBodyConstraints++;
  274. }
  275. }
  276. //if (m_solverInfo->m_minimumSolverBatchSize<=1)
  277. //{
  278. // m_solver->solveGroup( bodies,numBodies,manifolds, numManifolds,startConstraint,numCurConstraints,*m_solverInfo,m_debugDrawer,m_dispatcher);
  279. //} else
  280. {
  281. for (i=0;i<numBodies;i++)
  282. m_bodies.push_back(bodies[i]);
  283. for (i=0;i<numManifolds;i++)
  284. m_manifolds.push_back(manifolds[i]);
  285. for (i=0;i<numCurConstraints;i++)
  286. m_constraints.push_back(startConstraint[i]);
  287. for (i=0;i<numCurMultiBodyConstraints;i++)
  288. m_multiBodyConstraints.push_back(startMultiBodyConstraint[i]);
  289. if ((m_multiBodyConstraints.size()+m_constraints.size()+m_manifolds.size())>m_solverInfo->m_minimumSolverBatchSize)
  290. {
  291. processConstraints();
  292. } else
  293. {
  294. //printf("deferred\n");
  295. }
  296. }
  297. }
  298. }
  299. void processConstraints()
  300. {
  301. btCollisionObject** bodies = m_bodies.size()? &m_bodies[0]:0;
  302. btPersistentManifold** manifold = m_manifolds.size()?&m_manifolds[0]:0;
  303. btTypedConstraint** constraints = m_constraints.size()?&m_constraints[0]:0;
  304. btMultiBodyConstraint** multiBodyConstraints = m_multiBodyConstraints.size() ? &m_multiBodyConstraints[0] : 0;
  305. //printf("mb contacts = %d, mb constraints = %d\n", mbContacts, m_multiBodyConstraints.size());
  306. m_solver->solveMultiBodyGroup( bodies,m_bodies.size(),manifold, m_manifolds.size(),constraints, m_constraints.size() ,multiBodyConstraints, m_multiBodyConstraints.size(), *m_solverInfo,m_debugDrawer,m_dispatcher);
  307. m_bodies.resize(0);
  308. m_manifolds.resize(0);
  309. m_constraints.resize(0);
  310. m_multiBodyConstraints.resize(0);
  311. }
  312. };
  313. btMultiBodyDynamicsWorld::btMultiBodyDynamicsWorld(btDispatcher* dispatcher,btBroadphaseInterface* pairCache,btMultiBodyConstraintSolver* constraintSolver,btCollisionConfiguration* collisionConfiguration)
  314. :btDiscreteDynamicsWorld(dispatcher,pairCache,constraintSolver,collisionConfiguration),
  315. m_multiBodyConstraintSolver(constraintSolver)
  316. {
  317. //split impulse is not yet supported for Featherstone hierarchies
  318. // getSolverInfo().m_splitImpulse = false;
  319. getSolverInfo().m_solverMode |=SOLVER_USE_2_FRICTION_DIRECTIONS;
  320. m_solverMultiBodyIslandCallback = new MultiBodyInplaceSolverIslandCallback(constraintSolver,dispatcher);
  321. }
  322. btMultiBodyDynamicsWorld::~btMultiBodyDynamicsWorld ()
  323. {
  324. delete m_solverMultiBodyIslandCallback;
  325. }
  326. void btMultiBodyDynamicsWorld::setMultiBodyConstraintSolver(btMultiBodyConstraintSolver* solver)
  327. {
  328. m_multiBodyConstraintSolver = solver;
  329. m_solverMultiBodyIslandCallback->setMultiBodyConstraintSolver(solver);
  330. btDiscreteDynamicsWorld::setConstraintSolver(solver);
  331. }
  332. void btMultiBodyDynamicsWorld::setConstraintSolver(btConstraintSolver* solver)
  333. {
  334. if (solver->getSolverType()==BT_MULTIBODY_SOLVER)
  335. {
  336. m_multiBodyConstraintSolver = (btMultiBodyConstraintSolver*)solver;
  337. }
  338. btDiscreteDynamicsWorld::setConstraintSolver(solver);
  339. }
  340. void btMultiBodyDynamicsWorld::forwardKinematics()
  341. {
  342. for (int b=0;b<m_multiBodies.size();b++)
  343. {
  344. btMultiBody* bod = m_multiBodies[b];
  345. bod->forwardKinematics(m_scratch_world_to_local,m_scratch_local_origin);
  346. }
  347. }
  348. void btMultiBodyDynamicsWorld::solveConstraints(btContactSolverInfo& solverInfo)
  349. {
  350. forwardKinematics();
  351. BT_PROFILE("solveConstraints");
  352. clearMultiBodyConstraintForces();
  353. m_sortedConstraints.resize( m_constraints.size());
  354. int i;
  355. for (i=0;i<getNumConstraints();i++)
  356. {
  357. m_sortedConstraints[i] = m_constraints[i];
  358. }
  359. m_sortedConstraints.quickSort(btSortConstraintOnIslandPredicate2());
  360. btTypedConstraint** constraintsPtr = getNumConstraints() ? &m_sortedConstraints[0] : 0;
  361. m_sortedMultiBodyConstraints.resize(m_multiBodyConstraints.size());
  362. for (i=0;i<m_multiBodyConstraints.size();i++)
  363. {
  364. m_sortedMultiBodyConstraints[i] = m_multiBodyConstraints[i];
  365. }
  366. m_sortedMultiBodyConstraints.quickSort(btSortMultiBodyConstraintOnIslandPredicate());
  367. btMultiBodyConstraint** sortedMultiBodyConstraints = m_sortedMultiBodyConstraints.size() ? &m_sortedMultiBodyConstraints[0] : 0;
  368. m_solverMultiBodyIslandCallback->setup(&solverInfo,constraintsPtr,m_sortedConstraints.size(),sortedMultiBodyConstraints,m_sortedMultiBodyConstraints.size(), getDebugDrawer());
  369. m_constraintSolver->prepareSolve(getCollisionWorld()->getNumCollisionObjects(), getCollisionWorld()->getDispatcher()->getNumManifolds());
  370. #ifndef BT_USE_VIRTUAL_CLEARFORCES_AND_GRAVITY
  371. {
  372. BT_PROFILE("btMultiBody addForce");
  373. for (int i=0;i<this->m_multiBodies.size();i++)
  374. {
  375. btMultiBody* bod = m_multiBodies[i];
  376. bool isSleeping = false;
  377. if (bod->getBaseCollider() && bod->getBaseCollider()->getActivationState() == ISLAND_SLEEPING)
  378. {
  379. isSleeping = true;
  380. }
  381. for (int b=0;b<bod->getNumLinks();b++)
  382. {
  383. if (bod->getLink(b).m_collider && bod->getLink(b).m_collider->getActivationState()==ISLAND_SLEEPING)
  384. isSleeping = true;
  385. }
  386. if (!isSleeping)
  387. {
  388. //useless? they get resized in stepVelocities once again (AND DIFFERENTLY)
  389. m_scratch_r.resize(bod->getNumLinks()+1); //multidof? ("Y"s use it and it is used to store qdd)
  390. m_scratch_v.resize(bod->getNumLinks()+1);
  391. m_scratch_m.resize(bod->getNumLinks()+1);
  392. bod->addBaseForce(m_gravity * bod->getBaseMass());
  393. for (int j = 0; j < bod->getNumLinks(); ++j)
  394. {
  395. bod->addLinkForce(j, m_gravity * bod->getLinkMass(j));
  396. }
  397. }//if (!isSleeping)
  398. }
  399. }
  400. #endif //BT_USE_VIRTUAL_CLEARFORCES_AND_GRAVITY
  401. {
  402. BT_PROFILE("btMultiBody stepVelocities");
  403. for (int i=0;i<this->m_multiBodies.size();i++)
  404. {
  405. btMultiBody* bod = m_multiBodies[i];
  406. bool isSleeping = false;
  407. if (bod->getBaseCollider() && bod->getBaseCollider()->getActivationState() == ISLAND_SLEEPING)
  408. {
  409. isSleeping = true;
  410. }
  411. for (int b=0;b<bod->getNumLinks();b++)
  412. {
  413. if (bod->getLink(b).m_collider && bod->getLink(b).m_collider->getActivationState()==ISLAND_SLEEPING)
  414. isSleeping = true;
  415. }
  416. if (!isSleeping)
  417. {
  418. //useless? they get resized in stepVelocities once again (AND DIFFERENTLY)
  419. m_scratch_r.resize(bod->getNumLinks()+1); //multidof? ("Y"s use it and it is used to store qdd)
  420. m_scratch_v.resize(bod->getNumLinks()+1);
  421. m_scratch_m.resize(bod->getNumLinks()+1);
  422. bool doNotUpdatePos = false;
  423. {
  424. if(!bod->isUsingRK4Integration())
  425. {
  426. bod->computeAccelerationsArticulatedBodyAlgorithmMultiDof(solverInfo.m_timeStep, m_scratch_r, m_scratch_v, m_scratch_m);
  427. }
  428. else
  429. {
  430. //
  431. int numDofs = bod->getNumDofs() + 6;
  432. int numPosVars = bod->getNumPosVars() + 7;
  433. btAlignedObjectArray<btScalar> scratch_r2; scratch_r2.resize(2*numPosVars + 8*numDofs);
  434. //convenience
  435. btScalar *pMem = &scratch_r2[0];
  436. btScalar *scratch_q0 = pMem; pMem += numPosVars;
  437. btScalar *scratch_qx = pMem; pMem += numPosVars;
  438. btScalar *scratch_qd0 = pMem; pMem += numDofs;
  439. btScalar *scratch_qd1 = pMem; pMem += numDofs;
  440. btScalar *scratch_qd2 = pMem; pMem += numDofs;
  441. btScalar *scratch_qd3 = pMem; pMem += numDofs;
  442. btScalar *scratch_qdd0 = pMem; pMem += numDofs;
  443. btScalar *scratch_qdd1 = pMem; pMem += numDofs;
  444. btScalar *scratch_qdd2 = pMem; pMem += numDofs;
  445. btScalar *scratch_qdd3 = pMem; pMem += numDofs;
  446. btAssert((pMem - (2*numPosVars + 8*numDofs)) == &scratch_r2[0]);
  447. /////
  448. //copy q0 to scratch_q0 and qd0 to scratch_qd0
  449. scratch_q0[0] = bod->getWorldToBaseRot().x();
  450. scratch_q0[1] = bod->getWorldToBaseRot().y();
  451. scratch_q0[2] = bod->getWorldToBaseRot().z();
  452. scratch_q0[3] = bod->getWorldToBaseRot().w();
  453. scratch_q0[4] = bod->getBasePos().x();
  454. scratch_q0[5] = bod->getBasePos().y();
  455. scratch_q0[6] = bod->getBasePos().z();
  456. //
  457. for(int link = 0; link < bod->getNumLinks(); ++link)
  458. {
  459. for(int dof = 0; dof < bod->getLink(link).m_posVarCount; ++dof)
  460. scratch_q0[7 + bod->getLink(link).m_cfgOffset + dof] = bod->getLink(link).m_jointPos[dof];
  461. }
  462. //
  463. for(int dof = 0; dof < numDofs; ++dof)
  464. scratch_qd0[dof] = bod->getVelocityVector()[dof];
  465. ////
  466. struct
  467. {
  468. btMultiBody *bod;
  469. btScalar *scratch_qx, *scratch_q0;
  470. void operator()()
  471. {
  472. for(int dof = 0; dof < bod->getNumPosVars() + 7; ++dof)
  473. scratch_qx[dof] = scratch_q0[dof];
  474. }
  475. } pResetQx = {bod, scratch_qx, scratch_q0};
  476. //
  477. struct
  478. {
  479. void operator()(btScalar dt, const btScalar *pDer, const btScalar *pCurVal, btScalar *pVal, int size)
  480. {
  481. for(int i = 0; i < size; ++i)
  482. pVal[i] = pCurVal[i] + dt * pDer[i];
  483. }
  484. } pEulerIntegrate;
  485. //
  486. struct
  487. {
  488. void operator()(btMultiBody *pBody, const btScalar *pData)
  489. {
  490. btScalar *pVel = const_cast<btScalar*>(pBody->getVelocityVector());
  491. for(int i = 0; i < pBody->getNumDofs() + 6; ++i)
  492. pVel[i] = pData[i];
  493. }
  494. } pCopyToVelocityVector;
  495. //
  496. struct
  497. {
  498. void operator()(const btScalar *pSrc, btScalar *pDst, int start, int size)
  499. {
  500. for(int i = 0; i < size; ++i)
  501. pDst[i] = pSrc[start + i];
  502. }
  503. } pCopy;
  504. //
  505. btScalar h = solverInfo.m_timeStep;
  506. #define output &m_scratch_r[bod->getNumDofs()]
  507. //calc qdd0 from: q0 & qd0
  508. bod->computeAccelerationsArticulatedBodyAlgorithmMultiDof(0., m_scratch_r, m_scratch_v, m_scratch_m);
  509. pCopy(output, scratch_qdd0, 0, numDofs);
  510. //calc q1 = q0 + h/2 * qd0
  511. pResetQx();
  512. bod->stepPositionsMultiDof(btScalar(.5)*h, scratch_qx, scratch_qd0);
  513. //calc qd1 = qd0 + h/2 * qdd0
  514. pEulerIntegrate(btScalar(.5)*h, scratch_qdd0, scratch_qd0, scratch_qd1, numDofs);
  515. //
  516. //calc qdd1 from: q1 & qd1
  517. pCopyToVelocityVector(bod, scratch_qd1);
  518. bod->computeAccelerationsArticulatedBodyAlgorithmMultiDof(0., m_scratch_r, m_scratch_v, m_scratch_m);
  519. pCopy(output, scratch_qdd1, 0, numDofs);
  520. //calc q2 = q0 + h/2 * qd1
  521. pResetQx();
  522. bod->stepPositionsMultiDof(btScalar(.5)*h, scratch_qx, scratch_qd1);
  523. //calc qd2 = qd0 + h/2 * qdd1
  524. pEulerIntegrate(btScalar(.5)*h, scratch_qdd1, scratch_qd0, scratch_qd2, numDofs);
  525. //
  526. //calc qdd2 from: q2 & qd2
  527. pCopyToVelocityVector(bod, scratch_qd2);
  528. bod->computeAccelerationsArticulatedBodyAlgorithmMultiDof(0., m_scratch_r, m_scratch_v, m_scratch_m);
  529. pCopy(output, scratch_qdd2, 0, numDofs);
  530. //calc q3 = q0 + h * qd2
  531. pResetQx();
  532. bod->stepPositionsMultiDof(h, scratch_qx, scratch_qd2);
  533. //calc qd3 = qd0 + h * qdd2
  534. pEulerIntegrate(h, scratch_qdd2, scratch_qd0, scratch_qd3, numDofs);
  535. //
  536. //calc qdd3 from: q3 & qd3
  537. pCopyToVelocityVector(bod, scratch_qd3);
  538. bod->computeAccelerationsArticulatedBodyAlgorithmMultiDof(0., m_scratch_r, m_scratch_v, m_scratch_m);
  539. pCopy(output, scratch_qdd3, 0, numDofs);
  540. //
  541. //calc q = q0 + h/6(qd0 + 2*(qd1 + qd2) + qd3)
  542. //calc qd = qd0 + h/6(qdd0 + 2*(qdd1 + qdd2) + qdd3)
  543. btAlignedObjectArray<btScalar> delta_q; delta_q.resize(numDofs);
  544. btAlignedObjectArray<btScalar> delta_qd; delta_qd.resize(numDofs);
  545. for(int i = 0; i < numDofs; ++i)
  546. {
  547. delta_q[i] = h/btScalar(6.)*(scratch_qd0[i] + 2*scratch_qd1[i] + 2*scratch_qd2[i] + scratch_qd3[i]);
  548. delta_qd[i] = h/btScalar(6.)*(scratch_qdd0[i] + 2*scratch_qdd1[i] + 2*scratch_qdd2[i] + scratch_qdd3[i]);
  549. //delta_q[i] = h*scratch_qd0[i];
  550. //delta_qd[i] = h*scratch_qdd0[i];
  551. }
  552. //
  553. pCopyToVelocityVector(bod, scratch_qd0);
  554. bod->applyDeltaVeeMultiDof(&delta_qd[0], 1);
  555. //
  556. if(!doNotUpdatePos)
  557. {
  558. btScalar *pRealBuf = const_cast<btScalar *>(bod->getVelocityVector());
  559. pRealBuf += 6 + bod->getNumDofs() + bod->getNumDofs()*bod->getNumDofs();
  560. for(int i = 0; i < numDofs; ++i)
  561. pRealBuf[i] = delta_q[i];
  562. //bod->stepPositionsMultiDof(1, 0, &delta_q[0]);
  563. bod->setPosUpdated(true);
  564. }
  565. //ugly hack which resets the cached data to t0 (needed for constraint solver)
  566. {
  567. for(int link = 0; link < bod->getNumLinks(); ++link)
  568. bod->getLink(link).updateCacheMultiDof();
  569. bod->computeAccelerationsArticulatedBodyAlgorithmMultiDof(0, m_scratch_r, m_scratch_v, m_scratch_m);
  570. }
  571. }
  572. }
  573. #ifndef BT_USE_VIRTUAL_CLEARFORCES_AND_GRAVITY
  574. bod->clearForcesAndTorques();
  575. #endif //BT_USE_VIRTUAL_CLEARFORCES_AND_GRAVITY
  576. }//if (!isSleeping)
  577. }
  578. }
  579. /// solve all the constraints for this island
  580. m_islandManager->buildAndProcessIslands(getCollisionWorld()->getDispatcher(), getCollisionWorld(), m_solverMultiBodyIslandCallback);
  581. m_solverMultiBodyIslandCallback->processConstraints();
  582. m_constraintSolver->allSolved(solverInfo, m_debugDrawer);
  583. {
  584. BT_PROFILE("btMultiBody stepVelocities");
  585. for (int i=0;i<this->m_multiBodies.size();i++)
  586. {
  587. btMultiBody* bod = m_multiBodies[i];
  588. bool isSleeping = false;
  589. if (bod->getBaseCollider() && bod->getBaseCollider()->getActivationState() == ISLAND_SLEEPING)
  590. {
  591. isSleeping = true;
  592. }
  593. for (int b=0;b<bod->getNumLinks();b++)
  594. {
  595. if (bod->getLink(b).m_collider && bod->getLink(b).m_collider->getActivationState()==ISLAND_SLEEPING)
  596. isSleeping = true;
  597. }
  598. if (!isSleeping)
  599. {
  600. //useless? they get resized in stepVelocities once again (AND DIFFERENTLY)
  601. m_scratch_r.resize(bod->getNumLinks()+1); //multidof? ("Y"s use it and it is used to store qdd)
  602. m_scratch_v.resize(bod->getNumLinks()+1);
  603. m_scratch_m.resize(bod->getNumLinks()+1);
  604. {
  605. if(!bod->isUsingRK4Integration())
  606. {
  607. bool isConstraintPass = true;
  608. bod->computeAccelerationsArticulatedBodyAlgorithmMultiDof(solverInfo.m_timeStep, m_scratch_r, m_scratch_v, m_scratch_m, isConstraintPass);
  609. }
  610. }
  611. }
  612. }
  613. }
  614. for (int i=0;i<this->m_multiBodies.size();i++)
  615. {
  616. btMultiBody* bod = m_multiBodies[i];
  617. bod->processDeltaVeeMultiDof2();
  618. }
  619. }
  620. void btMultiBodyDynamicsWorld::integrateTransforms(btScalar timeStep)
  621. {
  622. btDiscreteDynamicsWorld::integrateTransforms(timeStep);
  623. {
  624. BT_PROFILE("btMultiBody stepPositions");
  625. //integrate and update the Featherstone hierarchies
  626. for (int b=0;b<m_multiBodies.size();b++)
  627. {
  628. btMultiBody* bod = m_multiBodies[b];
  629. bool isSleeping = false;
  630. if (bod->getBaseCollider() && bod->getBaseCollider()->getActivationState() == ISLAND_SLEEPING)
  631. {
  632. isSleeping = true;
  633. }
  634. for (int b=0;b<bod->getNumLinks();b++)
  635. {
  636. if (bod->getLink(b).m_collider && bod->getLink(b).m_collider->getActivationState()==ISLAND_SLEEPING)
  637. isSleeping = true;
  638. }
  639. if (!isSleeping)
  640. {
  641. int nLinks = bod->getNumLinks();
  642. ///base + num m_links
  643. {
  644. if(!bod->isPosUpdated())
  645. bod->stepPositionsMultiDof(timeStep);
  646. else
  647. {
  648. btScalar *pRealBuf = const_cast<btScalar *>(bod->getVelocityVector());
  649. pRealBuf += 6 + bod->getNumDofs() + bod->getNumDofs()*bod->getNumDofs();
  650. bod->stepPositionsMultiDof(1, 0, pRealBuf);
  651. bod->setPosUpdated(false);
  652. }
  653. }
  654. m_scratch_world_to_local.resize(nLinks+1);
  655. m_scratch_local_origin.resize(nLinks+1);
  656. bod->updateCollisionObjectWorldTransforms(m_scratch_world_to_local,m_scratch_local_origin);
  657. } else
  658. {
  659. bod->clearVelocities();
  660. }
  661. }
  662. }
  663. }
  664. void btMultiBodyDynamicsWorld::addMultiBodyConstraint( btMultiBodyConstraint* constraint)
  665. {
  666. m_multiBodyConstraints.push_back(constraint);
  667. }
  668. void btMultiBodyDynamicsWorld::removeMultiBodyConstraint( btMultiBodyConstraint* constraint)
  669. {
  670. m_multiBodyConstraints.remove(constraint);
  671. }
  672. void btMultiBodyDynamicsWorld::debugDrawMultiBodyConstraint(btMultiBodyConstraint* constraint)
  673. {
  674. constraint->debugDraw(getDebugDrawer());
  675. }
  676. void btMultiBodyDynamicsWorld::debugDrawWorld()
  677. {
  678. BT_PROFILE("btMultiBodyDynamicsWorld debugDrawWorld");
  679. btDiscreteDynamicsWorld::debugDrawWorld();
  680. bool drawConstraints = false;
  681. if (getDebugDrawer())
  682. {
  683. int mode = getDebugDrawer()->getDebugMode();
  684. if (mode & (btIDebugDraw::DBG_DrawConstraints | btIDebugDraw::DBG_DrawConstraintLimits))
  685. {
  686. drawConstraints = true;
  687. }
  688. if (drawConstraints)
  689. {
  690. BT_PROFILE("btMultiBody debugDrawWorld");
  691. for (int c=0;c<m_multiBodyConstraints.size();c++)
  692. {
  693. btMultiBodyConstraint* constraint = m_multiBodyConstraints[c];
  694. debugDrawMultiBodyConstraint(constraint);
  695. }
  696. for (int b = 0; b<m_multiBodies.size(); b++)
  697. {
  698. btMultiBody* bod = m_multiBodies[b];
  699. bod->forwardKinematics(m_scratch_world_to_local1,m_scratch_local_origin1);
  700. if (mode & btIDebugDraw::DBG_DrawFrames)
  701. {
  702. getDebugDrawer()->drawTransform(bod->getBaseWorldTransform(), 0.1);
  703. }
  704. for (int m = 0; m<bod->getNumLinks(); m++)
  705. {
  706. const btTransform& tr = bod->getLink(m).m_cachedWorldTransform;
  707. if (mode & btIDebugDraw::DBG_DrawFrames)
  708. {
  709. getDebugDrawer()->drawTransform(tr, 0.1);
  710. }
  711. //draw the joint axis
  712. if (bod->getLink(m).m_jointType==btMultibodyLink::eRevolute)
  713. {
  714. btVector3 vec = quatRotate(tr.getRotation(),bod->getLink(m).m_axes[0].m_topVec)*0.1;
  715. btVector4 color(0,0,0,1);//1,1,1);
  716. btVector3 from = vec+tr.getOrigin()-quatRotate(tr.getRotation(),bod->getLink(m).m_dVector);
  717. btVector3 to = tr.getOrigin()-quatRotate(tr.getRotation(),bod->getLink(m).m_dVector);
  718. getDebugDrawer()->drawLine(from,to,color);
  719. }
  720. if (bod->getLink(m).m_jointType==btMultibodyLink::eFixed)
  721. {
  722. btVector3 vec = quatRotate(tr.getRotation(),bod->getLink(m).m_axes[0].m_bottomVec)*0.1;
  723. btVector4 color(0,0,0,1);//1,1,1);
  724. btVector3 from = vec+tr.getOrigin()-quatRotate(tr.getRotation(),bod->getLink(m).m_dVector);
  725. btVector3 to = tr.getOrigin()-quatRotate(tr.getRotation(),bod->getLink(m).m_dVector);
  726. getDebugDrawer()->drawLine(from,to,color);
  727. }
  728. if (bod->getLink(m).m_jointType==btMultibodyLink::ePrismatic)
  729. {
  730. btVector3 vec = quatRotate(tr.getRotation(),bod->getLink(m).m_axes[0].m_bottomVec)*0.1;
  731. btVector4 color(0,0,0,1);//1,1,1);
  732. btVector3 from = vec+tr.getOrigin()-quatRotate(tr.getRotation(),bod->getLink(m).m_dVector);
  733. btVector3 to = tr.getOrigin()-quatRotate(tr.getRotation(),bod->getLink(m).m_dVector);
  734. getDebugDrawer()->drawLine(from,to,color);
  735. }
  736. }
  737. }
  738. }
  739. }
  740. }
  741. void btMultiBodyDynamicsWorld::applyGravity()
  742. {
  743. btDiscreteDynamicsWorld::applyGravity();
  744. #ifdef BT_USE_VIRTUAL_CLEARFORCES_AND_GRAVITY
  745. BT_PROFILE("btMultiBody addGravity");
  746. for (int i=0;i<this->m_multiBodies.size();i++)
  747. {
  748. btMultiBody* bod = m_multiBodies[i];
  749. bool isSleeping = false;
  750. if (bod->getBaseCollider() && bod->getBaseCollider()->getActivationState() == ISLAND_SLEEPING)
  751. {
  752. isSleeping = true;
  753. }
  754. for (int b=0;b<bod->getNumLinks();b++)
  755. {
  756. if (bod->getLink(b).m_collider && bod->getLink(b).m_collider->getActivationState()==ISLAND_SLEEPING)
  757. isSleeping = true;
  758. }
  759. if (!isSleeping)
  760. {
  761. bod->addBaseForce(m_gravity * bod->getBaseMass());
  762. for (int j = 0; j < bod->getNumLinks(); ++j)
  763. {
  764. bod->addLinkForce(j, m_gravity * bod->getLinkMass(j));
  765. }
  766. }//if (!isSleeping)
  767. }
  768. #endif //BT_USE_VIRTUAL_CLEARFORCES_AND_GRAVITY
  769. }
  770. void btMultiBodyDynamicsWorld::clearMultiBodyConstraintForces()
  771. {
  772. for (int i=0;i<this->m_multiBodies.size();i++)
  773. {
  774. btMultiBody* bod = m_multiBodies[i];
  775. bod->clearConstraintForces();
  776. }
  777. }
  778. void btMultiBodyDynamicsWorld::clearMultiBodyForces()
  779. {
  780. {
  781. // BT_PROFILE("clearMultiBodyForces");
  782. for (int i=0;i<this->m_multiBodies.size();i++)
  783. {
  784. btMultiBody* bod = m_multiBodies[i];
  785. bool isSleeping = false;
  786. if (bod->getBaseCollider() && bod->getBaseCollider()->getActivationState() == ISLAND_SLEEPING)
  787. {
  788. isSleeping = true;
  789. }
  790. for (int b=0;b<bod->getNumLinks();b++)
  791. {
  792. if (bod->getLink(b).m_collider && bod->getLink(b).m_collider->getActivationState()==ISLAND_SLEEPING)
  793. isSleeping = true;
  794. }
  795. if (!isSleeping)
  796. {
  797. btMultiBody* bod = m_multiBodies[i];
  798. bod->clearForcesAndTorques();
  799. }
  800. }
  801. }
  802. }
  803. void btMultiBodyDynamicsWorld::clearForces()
  804. {
  805. btDiscreteDynamicsWorld::clearForces();
  806. #ifdef BT_USE_VIRTUAL_CLEARFORCES_AND_GRAVITY
  807. clearMultiBodyForces();
  808. #endif
  809. }
  810. void btMultiBodyDynamicsWorld::serialize(btSerializer* serializer)
  811. {
  812. serializer->startSerialization();
  813. serializeDynamicsWorldInfo( serializer);
  814. serializeMultiBodies(serializer);
  815. serializeRigidBodies(serializer);
  816. serializeCollisionObjects(serializer);
  817. serializeContactManifolds(serializer);
  818. serializer->finishSerialization();
  819. }
  820. void btMultiBodyDynamicsWorld::serializeMultiBodies(btSerializer* serializer)
  821. {
  822. int i;
  823. //serialize all collision objects
  824. for (i=0;i<m_multiBodies.size();i++)
  825. {
  826. btMultiBody* mb = m_multiBodies[i];
  827. {
  828. int len = mb->calculateSerializeBufferSize();
  829. btChunk* chunk = serializer->allocate(len,1);
  830. const char* structType = mb->serialize(chunk->m_oldPtr, serializer);
  831. serializer->finalizeChunk(chunk,structType,BT_MULTIBODY_CODE,mb);
  832. }
  833. }
  834. //serialize all multibody links (collision objects)
  835. for (i=0;i<m_collisionObjects.size();i++)
  836. {
  837. btCollisionObject* colObj = m_collisionObjects[i];
  838. if (colObj->getInternalType() == btCollisionObject::CO_FEATHERSTONE_LINK)
  839. {
  840. int len = colObj->calculateSerializeBufferSize();
  841. btChunk* chunk = serializer->allocate(len,1);
  842. const char* structType = colObj->serialize(chunk->m_oldPtr, serializer);
  843. serializer->finalizeChunk(chunk,structType,BT_MB_LINKCOLLIDER_CODE,colObj);
  844. }
  845. }
  846. }