btMLCPSolver.cpp 19 KB

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