btMLCPSolver.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-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. ///original version written by Erwin Coumans, October 2013
  14. #include "btMLCPSolver.h"
  15. #include "LinearMath/btMatrixX.h"
  16. #include "LinearMath/btQuickprof.h"
  17. #include "btSolveProjectedGaussSeidel.h"
  18. btMLCPSolver::btMLCPSolver(btMLCPSolverInterface* solver)
  19. : m_solver(solver),
  20. m_fallback(0)
  21. {
  22. }
  23. btMLCPSolver::~btMLCPSolver()
  24. {
  25. }
  26. bool gUseMatrixMultiply = false;
  27. bool interleaveContactAndFriction = false;
  28. btScalar btMLCPSolver::solveGroupCacheFriendlySetup(btCollisionObject** bodies, int numBodiesUnUsed, btPersistentManifold** manifoldPtr, int numManifolds, btTypedConstraint** constraints, int numConstraints, const btContactSolverInfo& infoGlobal, btIDebugDraw* debugDrawer)
  29. {
  30. btSequentialImpulseConstraintSolver::solveGroupCacheFriendlySetup(bodies, numBodiesUnUsed, manifoldPtr, numManifolds, constraints, numConstraints, infoGlobal, debugDrawer);
  31. {
  32. BT_PROFILE("gather constraint data");
  33. int numFrictionPerContact = m_tmpSolverContactConstraintPool.size() == m_tmpSolverContactFrictionConstraintPool.size() ? 1 : 2;
  34. // int numBodies = m_tmpSolverBodyPool.size();
  35. m_allConstraintPtrArray.resize(0);
  36. m_limitDependencies.resize(m_tmpSolverNonContactConstraintPool.size() + m_tmpSolverContactConstraintPool.size() + m_tmpSolverContactFrictionConstraintPool.size());
  37. btAssert(m_limitDependencies.size() == m_tmpSolverNonContactConstraintPool.size() + m_tmpSolverContactConstraintPool.size() + m_tmpSolverContactFrictionConstraintPool.size());
  38. // printf("m_limitDependencies.size() = %d\n",m_limitDependencies.size());
  39. int dindex = 0;
  40. for (int i = 0; i < m_tmpSolverNonContactConstraintPool.size(); i++)
  41. {
  42. m_allConstraintPtrArray.push_back(&m_tmpSolverNonContactConstraintPool[i]);
  43. m_limitDependencies[dindex++] = -1;
  44. }
  45. ///The btSequentialImpulseConstraintSolver moves all friction constraints at the very end, we can also interleave them instead
  46. int firstContactConstraintOffset = dindex;
  47. if (interleaveContactAndFriction)
  48. {
  49. for (int i = 0; i < m_tmpSolverContactConstraintPool.size(); i++)
  50. {
  51. m_allConstraintPtrArray.push_back(&m_tmpSolverContactConstraintPool[i]);
  52. m_limitDependencies[dindex++] = -1;
  53. m_allConstraintPtrArray.push_back(&m_tmpSolverContactFrictionConstraintPool[i * numFrictionPerContact]);
  54. int findex = (m_tmpSolverContactFrictionConstraintPool[i * numFrictionPerContact].m_frictionIndex * (1 + numFrictionPerContact));
  55. m_limitDependencies[dindex++] = findex + firstContactConstraintOffset;
  56. if (numFrictionPerContact == 2)
  57. {
  58. m_allConstraintPtrArray.push_back(&m_tmpSolverContactFrictionConstraintPool[i * numFrictionPerContact + 1]);
  59. m_limitDependencies[dindex++] = findex + firstContactConstraintOffset;
  60. }
  61. }
  62. }
  63. else
  64. {
  65. for (int i = 0; i < m_tmpSolverContactConstraintPool.size(); i++)
  66. {
  67. m_allConstraintPtrArray.push_back(&m_tmpSolverContactConstraintPool[i]);
  68. m_limitDependencies[dindex++] = -1;
  69. }
  70. for (int i = 0; i < m_tmpSolverContactFrictionConstraintPool.size(); i++)
  71. {
  72. m_allConstraintPtrArray.push_back(&m_tmpSolverContactFrictionConstraintPool[i]);
  73. m_limitDependencies[dindex++] = m_tmpSolverContactFrictionConstraintPool[i].m_frictionIndex + firstContactConstraintOffset;
  74. }
  75. }
  76. if (!m_allConstraintPtrArray.size())
  77. {
  78. m_A.resize(0, 0);
  79. m_b.resize(0);
  80. m_x.resize(0);
  81. m_lo.resize(0);
  82. m_hi.resize(0);
  83. return 0.f;
  84. }
  85. }
  86. if (gUseMatrixMultiply)
  87. {
  88. BT_PROFILE("createMLCP");
  89. createMLCP(infoGlobal);
  90. }
  91. else
  92. {
  93. BT_PROFILE("createMLCPFast");
  94. createMLCPFast(infoGlobal);
  95. }
  96. return 0.f;
  97. }
  98. bool btMLCPSolver::solveMLCP(const btContactSolverInfo& infoGlobal)
  99. {
  100. bool result = true;
  101. if (m_A.rows() == 0)
  102. return true;
  103. //if using split impulse, we solve 2 separate (M)LCPs
  104. if (infoGlobal.m_splitImpulse)
  105. {
  106. btMatrixXu Acopy = m_A;
  107. btAlignedObjectArray<int> limitDependenciesCopy = m_limitDependencies;
  108. // printf("solve first LCP\n");
  109. result = m_solver->solveMLCP(m_A, m_b, m_x, m_lo, m_hi, m_limitDependencies, infoGlobal.m_numIterations);
  110. if (result)
  111. result = m_solver->solveMLCP(Acopy, m_bSplit, m_xSplit, m_lo, m_hi, limitDependenciesCopy, infoGlobal.m_numIterations);
  112. }
  113. else
  114. {
  115. result = m_solver->solveMLCP(m_A, m_b, m_x, m_lo, m_hi, m_limitDependencies, infoGlobal.m_numIterations);
  116. }
  117. return result;
  118. }
  119. struct btJointNode
  120. {
  121. int jointIndex; // pointer to enclosing dxJoint object
  122. int otherBodyIndex; // *other* body this joint is connected to
  123. int nextJointNodeIndex; //-1 for null
  124. int constraintRowIndex;
  125. };
  126. void btMLCPSolver::createMLCPFast(const btContactSolverInfo& infoGlobal)
  127. {
  128. int numContactRows = interleaveContactAndFriction ? 3 : 1;
  129. int numConstraintRows = m_allConstraintPtrArray.size();
  130. int n = numConstraintRows;
  131. {
  132. BT_PROFILE("init b (rhs)");
  133. m_b.resize(numConstraintRows);
  134. m_bSplit.resize(numConstraintRows);
  135. m_b.setZero();
  136. m_bSplit.setZero();
  137. for (int i = 0; i < numConstraintRows; i++)
  138. {
  139. btScalar jacDiag = m_allConstraintPtrArray[i]->m_jacDiagABInv;
  140. if (!btFuzzyZero(jacDiag))
  141. {
  142. btScalar rhs = m_allConstraintPtrArray[i]->m_rhs;
  143. btScalar rhsPenetration = m_allConstraintPtrArray[i]->m_rhsPenetration;
  144. m_b[i] = rhs / jacDiag;
  145. m_bSplit[i] = rhsPenetration / jacDiag;
  146. }
  147. }
  148. }
  149. // btScalar* w = 0;
  150. // int nub = 0;
  151. m_lo.resize(numConstraintRows);
  152. m_hi.resize(numConstraintRows);
  153. {
  154. BT_PROFILE("init lo/ho");
  155. for (int i = 0; i < numConstraintRows; i++)
  156. {
  157. if (0) //m_limitDependencies[i]>=0)
  158. {
  159. m_lo[i] = -BT_INFINITY;
  160. m_hi[i] = BT_INFINITY;
  161. }
  162. else
  163. {
  164. m_lo[i] = m_allConstraintPtrArray[i]->m_lowerLimit;
  165. m_hi[i] = m_allConstraintPtrArray[i]->m_upperLimit;
  166. }
  167. }
  168. }
  169. //
  170. int m = m_allConstraintPtrArray.size();
  171. int numBodies = m_tmpSolverBodyPool.size();
  172. btAlignedObjectArray<int> bodyJointNodeArray;
  173. {
  174. BT_PROFILE("bodyJointNodeArray.resize");
  175. bodyJointNodeArray.resize(numBodies, -1);
  176. }
  177. btAlignedObjectArray<btJointNode> jointNodeArray;
  178. {
  179. BT_PROFILE("jointNodeArray.reserve");
  180. jointNodeArray.reserve(2 * m_allConstraintPtrArray.size());
  181. }
  182. btMatrixXu& J3 = m_scratchJ3;
  183. {
  184. BT_PROFILE("J3.resize");
  185. J3.resize(2 * m, 8);
  186. }
  187. btMatrixXu& JinvM3 = m_scratchJInvM3;
  188. {
  189. BT_PROFILE("JinvM3.resize/setZero");
  190. JinvM3.resize(2 * m, 8);
  191. JinvM3.setZero();
  192. J3.setZero();
  193. }
  194. int cur = 0;
  195. int rowOffset = 0;
  196. btAlignedObjectArray<int>& ofs = m_scratchOfs;
  197. {
  198. BT_PROFILE("ofs resize");
  199. ofs.resize(0);
  200. ofs.resizeNoInitialize(m_allConstraintPtrArray.size());
  201. }
  202. {
  203. BT_PROFILE("Compute J and JinvM");
  204. int c = 0;
  205. int numRows = 0;
  206. for (int i = 0; i < m_allConstraintPtrArray.size(); i += numRows, c++)
  207. {
  208. ofs[c] = rowOffset;
  209. int sbA = m_allConstraintPtrArray[i]->m_solverBodyIdA;
  210. int sbB = m_allConstraintPtrArray[i]->m_solverBodyIdB;
  211. btRigidBody* orgBodyA = m_tmpSolverBodyPool[sbA].m_originalBody;
  212. btRigidBody* orgBodyB = m_tmpSolverBodyPool[sbB].m_originalBody;
  213. numRows = i < m_tmpSolverNonContactConstraintPool.size() ? m_tmpConstraintSizesPool[c].m_numConstraintRows : numContactRows;
  214. if (orgBodyA)
  215. {
  216. {
  217. int slotA = -1;
  218. //find free jointNode slot for sbA
  219. slotA = jointNodeArray.size();
  220. jointNodeArray.expand(); //NonInitializing();
  221. int prevSlot = bodyJointNodeArray[sbA];
  222. bodyJointNodeArray[sbA] = slotA;
  223. jointNodeArray[slotA].nextJointNodeIndex = prevSlot;
  224. jointNodeArray[slotA].jointIndex = c;
  225. jointNodeArray[slotA].constraintRowIndex = i;
  226. jointNodeArray[slotA].otherBodyIndex = orgBodyB ? sbB : -1;
  227. }
  228. for (int row = 0; row < numRows; row++, cur++)
  229. {
  230. btVector3 normalInvMass = m_allConstraintPtrArray[i + row]->m_contactNormal1 * orgBodyA->getInvMass();
  231. btVector3 relPosCrossNormalInvInertia = m_allConstraintPtrArray[i + row]->m_relpos1CrossNormal * orgBodyA->getInvInertiaTensorWorld();
  232. for (int r = 0; r < 3; r++)
  233. {
  234. J3.setElem(cur, r, m_allConstraintPtrArray[i + row]->m_contactNormal1[r]);
  235. J3.setElem(cur, r + 4, m_allConstraintPtrArray[i + row]->m_relpos1CrossNormal[r]);
  236. JinvM3.setElem(cur, r, normalInvMass[r]);
  237. JinvM3.setElem(cur, r + 4, relPosCrossNormalInvInertia[r]);
  238. }
  239. J3.setElem(cur, 3, 0);
  240. JinvM3.setElem(cur, 3, 0);
  241. J3.setElem(cur, 7, 0);
  242. JinvM3.setElem(cur, 7, 0);
  243. }
  244. }
  245. else
  246. {
  247. cur += numRows;
  248. }
  249. if (orgBodyB)
  250. {
  251. {
  252. int slotB = -1;
  253. //find free jointNode slot for sbA
  254. slotB = jointNodeArray.size();
  255. jointNodeArray.expand(); //NonInitializing();
  256. int prevSlot = bodyJointNodeArray[sbB];
  257. bodyJointNodeArray[sbB] = slotB;
  258. jointNodeArray[slotB].nextJointNodeIndex = prevSlot;
  259. jointNodeArray[slotB].jointIndex = c;
  260. jointNodeArray[slotB].otherBodyIndex = orgBodyA ? sbA : -1;
  261. jointNodeArray[slotB].constraintRowIndex = i;
  262. }
  263. for (int row = 0; row < numRows; row++, cur++)
  264. {
  265. btVector3 normalInvMassB = m_allConstraintPtrArray[i + row]->m_contactNormal2 * orgBodyB->getInvMass();
  266. btVector3 relPosInvInertiaB = m_allConstraintPtrArray[i + row]->m_relpos2CrossNormal * orgBodyB->getInvInertiaTensorWorld();
  267. for (int r = 0; r < 3; r++)
  268. {
  269. J3.setElem(cur, r, m_allConstraintPtrArray[i + row]->m_contactNormal2[r]);
  270. J3.setElem(cur, r + 4, m_allConstraintPtrArray[i + row]->m_relpos2CrossNormal[r]);
  271. JinvM3.setElem(cur, r, normalInvMassB[r]);
  272. JinvM3.setElem(cur, r + 4, relPosInvInertiaB[r]);
  273. }
  274. J3.setElem(cur, 3, 0);
  275. JinvM3.setElem(cur, 3, 0);
  276. J3.setElem(cur, 7, 0);
  277. JinvM3.setElem(cur, 7, 0);
  278. }
  279. }
  280. else
  281. {
  282. cur += numRows;
  283. }
  284. rowOffset += numRows;
  285. }
  286. }
  287. //compute JinvM = J*invM.
  288. const btScalar* JinvM = JinvM3.getBufferPointer();
  289. const btScalar* Jptr = J3.getBufferPointer();
  290. {
  291. BT_PROFILE("m_A.resize");
  292. m_A.resize(n, n);
  293. }
  294. {
  295. BT_PROFILE("m_A.setZero");
  296. m_A.setZero();
  297. }
  298. int c = 0;
  299. {
  300. int numRows = 0;
  301. BT_PROFILE("Compute A");
  302. for (int i = 0; i < m_allConstraintPtrArray.size(); i += numRows, c++)
  303. {
  304. int row__ = ofs[c];
  305. int sbA = m_allConstraintPtrArray[i]->m_solverBodyIdA;
  306. int sbB = m_allConstraintPtrArray[i]->m_solverBodyIdB;
  307. // btRigidBody* orgBodyA = m_tmpSolverBodyPool[sbA].m_originalBody;
  308. // btRigidBody* orgBodyB = m_tmpSolverBodyPool[sbB].m_originalBody;
  309. numRows = i < m_tmpSolverNonContactConstraintPool.size() ? m_tmpConstraintSizesPool[c].m_numConstraintRows : numContactRows;
  310. const btScalar* JinvMrow = JinvM + 2 * 8 * (size_t)row__;
  311. {
  312. int startJointNodeA = bodyJointNodeArray[sbA];
  313. while (startJointNodeA >= 0)
  314. {
  315. int j0 = jointNodeArray[startJointNodeA].jointIndex;
  316. int cr0 = jointNodeArray[startJointNodeA].constraintRowIndex;
  317. if (j0 < c)
  318. {
  319. int numRowsOther = cr0 < m_tmpSolverNonContactConstraintPool.size() ? m_tmpConstraintSizesPool[j0].m_numConstraintRows : numContactRows;
  320. size_t ofsother = (m_allConstraintPtrArray[cr0]->m_solverBodyIdB == sbA) ? 8 * numRowsOther : 0;
  321. //printf("%d joint i %d and j0: %d: ",count++,i,j0);
  322. m_A.multiplyAdd2_p8r(JinvMrow,
  323. Jptr + 2 * 8 * (size_t)ofs[j0] + ofsother, numRows, numRowsOther, row__, ofs[j0]);
  324. }
  325. startJointNodeA = jointNodeArray[startJointNodeA].nextJointNodeIndex;
  326. }
  327. }
  328. {
  329. int startJointNodeB = bodyJointNodeArray[sbB];
  330. while (startJointNodeB >= 0)
  331. {
  332. int j1 = jointNodeArray[startJointNodeB].jointIndex;
  333. int cj1 = jointNodeArray[startJointNodeB].constraintRowIndex;
  334. if (j1 < c)
  335. {
  336. int numRowsOther = cj1 < m_tmpSolverNonContactConstraintPool.size() ? m_tmpConstraintSizesPool[j1].m_numConstraintRows : numContactRows;
  337. size_t ofsother = (m_allConstraintPtrArray[cj1]->m_solverBodyIdB == sbB) ? 8 * numRowsOther : 0;
  338. m_A.multiplyAdd2_p8r(JinvMrow + 8 * (size_t)numRows,
  339. Jptr + 2 * 8 * (size_t)ofs[j1] + ofsother, numRows, numRowsOther, row__, ofs[j1]);
  340. }
  341. startJointNodeB = jointNodeArray[startJointNodeB].nextJointNodeIndex;
  342. }
  343. }
  344. }
  345. {
  346. BT_PROFILE("compute diagonal");
  347. // compute diagonal blocks of m_A
  348. int row__ = 0;
  349. int numJointRows = m_allConstraintPtrArray.size();
  350. int jj = 0;
  351. for (; row__ < numJointRows;)
  352. {
  353. //int sbA = m_allConstraintPtrArray[row__]->m_solverBodyIdA;
  354. int sbB = m_allConstraintPtrArray[row__]->m_solverBodyIdB;
  355. // btRigidBody* orgBodyA = m_tmpSolverBodyPool[sbA].m_originalBody;
  356. btRigidBody* orgBodyB = m_tmpSolverBodyPool[sbB].m_originalBody;
  357. const unsigned int infom = row__ < m_tmpSolverNonContactConstraintPool.size() ? m_tmpConstraintSizesPool[jj].m_numConstraintRows : numContactRows;
  358. const btScalar* JinvMrow = JinvM + 2 * 8 * (size_t)row__;
  359. const btScalar* Jrow = Jptr + 2 * 8 * (size_t)row__;
  360. m_A.multiply2_p8r(JinvMrow, Jrow, infom, infom, row__, row__);
  361. if (orgBodyB)
  362. {
  363. m_A.multiplyAdd2_p8r(JinvMrow + 8 * (size_t)infom, Jrow + 8 * (size_t)infom, infom, infom, row__, row__);
  364. }
  365. row__ += infom;
  366. jj++;
  367. }
  368. }
  369. }
  370. if (1)
  371. {
  372. // add cfm to the diagonal of m_A
  373. for (int i = 0; i < m_A.rows(); ++i)
  374. {
  375. m_A.setElem(i, i, m_A(i, i) + infoGlobal.m_globalCfm / infoGlobal.m_timeStep);
  376. }
  377. }
  378. ///fill the upper triangle of the matrix, to make it symmetric
  379. {
  380. BT_PROFILE("fill the upper triangle ");
  381. m_A.copyLowerToUpperTriangle();
  382. }
  383. {
  384. BT_PROFILE("resize/init x");
  385. m_x.resize(numConstraintRows);
  386. m_xSplit.resize(numConstraintRows);
  387. if (infoGlobal.m_solverMode & SOLVER_USE_WARMSTARTING)
  388. {
  389. for (int i = 0; i < m_allConstraintPtrArray.size(); i++)
  390. {
  391. const btSolverConstraint& c = *m_allConstraintPtrArray[i];
  392. m_x[i] = c.m_appliedImpulse;
  393. m_xSplit[i] = c.m_appliedPushImpulse;
  394. }
  395. }
  396. else
  397. {
  398. m_x.setZero();
  399. m_xSplit.setZero();
  400. }
  401. }
  402. }
  403. void btMLCPSolver::createMLCP(const btContactSolverInfo& infoGlobal)
  404. {
  405. int numBodies = this->m_tmpSolverBodyPool.size();
  406. int numConstraintRows = m_allConstraintPtrArray.size();
  407. m_b.resize(numConstraintRows);
  408. if (infoGlobal.m_splitImpulse)
  409. m_bSplit.resize(numConstraintRows);
  410. m_bSplit.setZero();
  411. m_b.setZero();
  412. for (int i = 0; i < numConstraintRows; i++)
  413. {
  414. if (m_allConstraintPtrArray[i]->m_jacDiagABInv)
  415. {
  416. m_b[i] = m_allConstraintPtrArray[i]->m_rhs / m_allConstraintPtrArray[i]->m_jacDiagABInv;
  417. if (infoGlobal.m_splitImpulse)
  418. m_bSplit[i] = m_allConstraintPtrArray[i]->m_rhsPenetration / m_allConstraintPtrArray[i]->m_jacDiagABInv;
  419. }
  420. }
  421. btMatrixXu& Minv = m_scratchMInv;
  422. Minv.resize(6 * numBodies, 6 * numBodies);
  423. Minv.setZero();
  424. for (int i = 0; i < numBodies; i++)
  425. {
  426. const btSolverBody& rb = m_tmpSolverBodyPool[i];
  427. const btVector3& invMass = rb.m_invMass;
  428. setElem(Minv, i * 6 + 0, i * 6 + 0, invMass[0]);
  429. setElem(Minv, i * 6 + 1, i * 6 + 1, invMass[1]);
  430. setElem(Minv, i * 6 + 2, i * 6 + 2, invMass[2]);
  431. btRigidBody* orgBody = m_tmpSolverBodyPool[i].m_originalBody;
  432. for (int r = 0; r < 3; r++)
  433. for (int c = 0; c < 3; c++)
  434. setElem(Minv, i * 6 + 3 + r, i * 6 + 3 + c, orgBody ? orgBody->getInvInertiaTensorWorld()[r][c] : 0);
  435. }
  436. btMatrixXu& J = m_scratchJ;
  437. J.resize(numConstraintRows, 6 * numBodies);
  438. J.setZero();
  439. m_lo.resize(numConstraintRows);
  440. m_hi.resize(numConstraintRows);
  441. for (int i = 0; i < numConstraintRows; i++)
  442. {
  443. m_lo[i] = m_allConstraintPtrArray[i]->m_lowerLimit;
  444. m_hi[i] = m_allConstraintPtrArray[i]->m_upperLimit;
  445. int bodyIndex0 = m_allConstraintPtrArray[i]->m_solverBodyIdA;
  446. int bodyIndex1 = m_allConstraintPtrArray[i]->m_solverBodyIdB;
  447. if (m_tmpSolverBodyPool[bodyIndex0].m_originalBody)
  448. {
  449. setElem(J, i, 6 * bodyIndex0 + 0, m_allConstraintPtrArray[i]->m_contactNormal1[0]);
  450. setElem(J, i, 6 * bodyIndex0 + 1, m_allConstraintPtrArray[i]->m_contactNormal1[1]);
  451. setElem(J, i, 6 * bodyIndex0 + 2, m_allConstraintPtrArray[i]->m_contactNormal1[2]);
  452. setElem(J, i, 6 * bodyIndex0 + 3, m_allConstraintPtrArray[i]->m_relpos1CrossNormal[0]);
  453. setElem(J, i, 6 * bodyIndex0 + 4, m_allConstraintPtrArray[i]->m_relpos1CrossNormal[1]);
  454. setElem(J, i, 6 * bodyIndex0 + 5, m_allConstraintPtrArray[i]->m_relpos1CrossNormal[2]);
  455. }
  456. if (m_tmpSolverBodyPool[bodyIndex1].m_originalBody)
  457. {
  458. setElem(J, i, 6 * bodyIndex1 + 0, m_allConstraintPtrArray[i]->m_contactNormal2[0]);
  459. setElem(J, i, 6 * bodyIndex1 + 1, m_allConstraintPtrArray[i]->m_contactNormal2[1]);
  460. setElem(J, i, 6 * bodyIndex1 + 2, m_allConstraintPtrArray[i]->m_contactNormal2[2]);
  461. setElem(J, i, 6 * bodyIndex1 + 3, m_allConstraintPtrArray[i]->m_relpos2CrossNormal[0]);
  462. setElem(J, i, 6 * bodyIndex1 + 4, m_allConstraintPtrArray[i]->m_relpos2CrossNormal[1]);
  463. setElem(J, i, 6 * bodyIndex1 + 5, m_allConstraintPtrArray[i]->m_relpos2CrossNormal[2]);
  464. }
  465. }
  466. btMatrixXu& J_transpose = m_scratchJTranspose;
  467. J_transpose = J.transpose();
  468. btMatrixXu& tmp = m_scratchTmp;
  469. //Minv.printMatrix("Minv=");
  470. {
  471. {
  472. BT_PROFILE("J*Minv");
  473. tmp = J * Minv;
  474. }
  475. {
  476. BT_PROFILE("J*tmp");
  477. m_A = tmp * J_transpose;
  478. }
  479. }
  480. //J.printMatrix("J");
  481. if (1)
  482. {
  483. // add cfm to the diagonal of m_A
  484. for (int i = 0; i < m_A.rows(); ++i)
  485. {
  486. m_A.setElem(i, i, m_A(i, i) + infoGlobal.m_globalCfm / infoGlobal.m_timeStep);
  487. }
  488. }
  489. m_x.resize(numConstraintRows);
  490. if (infoGlobal.m_splitImpulse)
  491. m_xSplit.resize(numConstraintRows);
  492. // m_x.setZero();
  493. for (int i = 0; i < m_allConstraintPtrArray.size(); i++)
  494. {
  495. const btSolverConstraint& c = *m_allConstraintPtrArray[i];
  496. m_x[i] = c.m_appliedImpulse;
  497. if (infoGlobal.m_splitImpulse)
  498. m_xSplit[i] = c.m_appliedPushImpulse;
  499. }
  500. }
  501. btScalar btMLCPSolver::solveGroupCacheFriendlyIterations(btCollisionObject** bodies, int numBodies, btPersistentManifold** manifoldPtr, int numManifolds, btTypedConstraint** constraints, int numConstraints, const btContactSolverInfo& infoGlobal, btIDebugDraw* debugDrawer)
  502. {
  503. bool result = true;
  504. {
  505. BT_PROFILE("solveMLCP");
  506. // printf("m_A(%d,%d)\n", m_A.rows(),m_A.cols());
  507. result = solveMLCP(infoGlobal);
  508. }
  509. //check if solution is valid, and otherwise fallback to btSequentialImpulseConstraintSolver::solveGroupCacheFriendlyIterations
  510. if (result)
  511. {
  512. BT_PROFILE("process MLCP results");
  513. for (int i = 0; i < m_allConstraintPtrArray.size(); i++)
  514. {
  515. {
  516. btSolverConstraint& c = *m_allConstraintPtrArray[i];
  517. int sbA = c.m_solverBodyIdA;
  518. int sbB = c.m_solverBodyIdB;
  519. //btRigidBody* orgBodyA = m_tmpSolverBodyPool[sbA].m_originalBody;
  520. // btRigidBody* orgBodyB = m_tmpSolverBodyPool[sbB].m_originalBody;
  521. btSolverBody& solverBodyA = m_tmpSolverBodyPool[sbA];
  522. btSolverBody& solverBodyB = m_tmpSolverBodyPool[sbB];
  523. {
  524. btScalar deltaImpulse = m_x[i] - c.m_appliedImpulse;
  525. c.m_appliedImpulse = m_x[i];
  526. solverBodyA.internalApplyImpulse(c.m_contactNormal1 * solverBodyA.internalGetInvMass(), c.m_angularComponentA, deltaImpulse);
  527. solverBodyB.internalApplyImpulse(c.m_contactNormal2 * solverBodyB.internalGetInvMass(), c.m_angularComponentB, deltaImpulse);
  528. }
  529. if (infoGlobal.m_splitImpulse)
  530. {
  531. btScalar deltaImpulse = m_xSplit[i] - c.m_appliedPushImpulse;
  532. solverBodyA.internalApplyPushImpulse(c.m_contactNormal1 * solverBodyA.internalGetInvMass(), c.m_angularComponentA, deltaImpulse);
  533. solverBodyB.internalApplyPushImpulse(c.m_contactNormal2 * solverBodyB.internalGetInvMass(), c.m_angularComponentB, deltaImpulse);
  534. c.m_appliedPushImpulse = m_xSplit[i];
  535. }
  536. }
  537. }
  538. }
  539. else
  540. {
  541. // printf("m_fallback = %d\n",m_fallback);
  542. m_fallback++;
  543. btSequentialImpulseConstraintSolver::solveGroupCacheFriendlyIterations(bodies, numBodies, manifoldPtr, numManifolds, constraints, numConstraints, infoGlobal, debugDrawer);
  544. }
  545. return 0.f;
  546. }