btMultiBodyConstraintSolver.cpp 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627
  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 "btMultiBodyConstraintSolver.h"
  14. #include "BulletCollision/NarrowPhaseCollision/btPersistentManifold.h"
  15. #include "btMultiBodyLinkCollider.h"
  16. #include "BulletDynamics/ConstraintSolver/btSolverBody.h"
  17. #include "btMultiBodyConstraint.h"
  18. #include "BulletDynamics/ConstraintSolver/btContactSolverInfo.h"
  19. #include "LinearMath/btQuickprof.h"
  20. btScalar btMultiBodyConstraintSolver::solveSingleIteration(int iteration, btCollisionObject** bodies, int numBodies, btPersistentManifold** manifoldPtr, int numManifolds, btTypedConstraint** constraints, int numConstraints, const btContactSolverInfo& infoGlobal, btIDebugDraw* debugDrawer)
  21. {
  22. btScalar leastSquaredResidual = btSequentialImpulseConstraintSolver::solveSingleIteration(iteration, bodies, numBodies, manifoldPtr, numManifolds, constraints, numConstraints, infoGlobal, debugDrawer);
  23. //solve featherstone non-contact constraints
  24. //printf("m_multiBodyNonContactConstraints = %d\n",m_multiBodyNonContactConstraints.size());
  25. for (int j = 0; j < m_multiBodyNonContactConstraints.size(); j++)
  26. {
  27. int index = iteration & 1 ? j : m_multiBodyNonContactConstraints.size() - 1 - j;
  28. btMultiBodySolverConstraint& constraint = m_multiBodyNonContactConstraints[index];
  29. btScalar residual = resolveSingleConstraintRowGeneric(constraint);
  30. leastSquaredResidual = btMax(leastSquaredResidual, residual * residual);
  31. if (constraint.m_multiBodyA)
  32. constraint.m_multiBodyA->setPosUpdated(false);
  33. if (constraint.m_multiBodyB)
  34. constraint.m_multiBodyB->setPosUpdated(false);
  35. }
  36. //solve featherstone normal contact
  37. for (int j0 = 0; j0 < m_multiBodyNormalContactConstraints.size(); j0++)
  38. {
  39. int index = j0; //iteration&1? j0 : m_multiBodyNormalContactConstraints.size()-1-j0;
  40. btMultiBodySolverConstraint& constraint = m_multiBodyNormalContactConstraints[index];
  41. btScalar residual = 0.f;
  42. if (iteration < infoGlobal.m_numIterations)
  43. {
  44. residual = resolveSingleConstraintRowGeneric(constraint);
  45. }
  46. leastSquaredResidual = btMax(leastSquaredResidual, residual * residual);
  47. if (constraint.m_multiBodyA)
  48. constraint.m_multiBodyA->setPosUpdated(false);
  49. if (constraint.m_multiBodyB)
  50. constraint.m_multiBodyB->setPosUpdated(false);
  51. }
  52. //solve featherstone frictional contact
  53. if (infoGlobal.m_solverMode & SOLVER_USE_2_FRICTION_DIRECTIONS && ((infoGlobal.m_solverMode & SOLVER_DISABLE_IMPLICIT_CONE_FRICTION) == 0))
  54. {
  55. for (int j1 = 0; j1 < this->m_multiBodyTorsionalFrictionContactConstraints.size(); j1++)
  56. {
  57. if (iteration < infoGlobal.m_numIterations)
  58. {
  59. int index = j1; //iteration&1? j1 : m_multiBodyTorsionalFrictionContactConstraints.size()-1-j1;
  60. btMultiBodySolverConstraint& frictionConstraint = m_multiBodyTorsionalFrictionContactConstraints[index];
  61. btScalar totalImpulse = m_multiBodyNormalContactConstraints[frictionConstraint.m_frictionIndex].m_appliedImpulse;
  62. //adjust friction limits here
  63. if (totalImpulse > btScalar(0))
  64. {
  65. frictionConstraint.m_lowerLimit = -(frictionConstraint.m_friction * totalImpulse);
  66. frictionConstraint.m_upperLimit = frictionConstraint.m_friction * totalImpulse;
  67. btScalar residual = resolveSingleConstraintRowGeneric(frictionConstraint);
  68. leastSquaredResidual = btMax(leastSquaredResidual, residual * residual);
  69. if (frictionConstraint.m_multiBodyA)
  70. frictionConstraint.m_multiBodyA->setPosUpdated(false);
  71. if (frictionConstraint.m_multiBodyB)
  72. frictionConstraint.m_multiBodyB->setPosUpdated(false);
  73. }
  74. }
  75. }
  76. for (int j1 = 0; j1 < this->m_multiBodyFrictionContactConstraints.size(); j1++)
  77. {
  78. if (iteration < infoGlobal.m_numIterations)
  79. {
  80. int index = j1; //iteration&1? j1 : m_multiBodyFrictionContactConstraints.size()-1-j1;
  81. btMultiBodySolverConstraint& frictionConstraint = m_multiBodyFrictionContactConstraints[index];
  82. btScalar totalImpulse = m_multiBodyNormalContactConstraints[frictionConstraint.m_frictionIndex].m_appliedImpulse;
  83. j1++;
  84. int index2 = j1; //iteration&1? j1 : m_multiBodyFrictionContactConstraints.size()-1-j1;
  85. btMultiBodySolverConstraint& frictionConstraintB = m_multiBodyFrictionContactConstraints[index2];
  86. btAssert(frictionConstraint.m_frictionIndex == frictionConstraintB.m_frictionIndex);
  87. if (frictionConstraint.m_frictionIndex == frictionConstraintB.m_frictionIndex)
  88. {
  89. frictionConstraint.m_lowerLimit = -(frictionConstraint.m_friction * totalImpulse);
  90. frictionConstraint.m_upperLimit = frictionConstraint.m_friction * totalImpulse;
  91. frictionConstraintB.m_lowerLimit = -(frictionConstraintB.m_friction * totalImpulse);
  92. frictionConstraintB.m_upperLimit = frictionConstraintB.m_friction * totalImpulse;
  93. btScalar residual = resolveConeFrictionConstraintRows(frictionConstraint, frictionConstraintB);
  94. leastSquaredResidual = btMax(leastSquaredResidual, residual * residual);
  95. if (frictionConstraintB.m_multiBodyA)
  96. frictionConstraintB.m_multiBodyA->setPosUpdated(false);
  97. if (frictionConstraintB.m_multiBodyB)
  98. frictionConstraintB.m_multiBodyB->setPosUpdated(false);
  99. if (frictionConstraint.m_multiBodyA)
  100. frictionConstraint.m_multiBodyA->setPosUpdated(false);
  101. if (frictionConstraint.m_multiBodyB)
  102. frictionConstraint.m_multiBodyB->setPosUpdated(false);
  103. }
  104. }
  105. }
  106. }
  107. else
  108. {
  109. for (int j1 = 0; j1 < this->m_multiBodyFrictionContactConstraints.size(); j1++)
  110. {
  111. if (iteration < infoGlobal.m_numIterations)
  112. {
  113. int index = j1; //iteration&1? j1 : m_multiBodyFrictionContactConstraints.size()-1-j1;
  114. btMultiBodySolverConstraint& frictionConstraint = m_multiBodyFrictionContactConstraints[index];
  115. btScalar totalImpulse = m_multiBodyNormalContactConstraints[frictionConstraint.m_frictionIndex].m_appliedImpulse;
  116. //adjust friction limits here
  117. if (totalImpulse > btScalar(0))
  118. {
  119. frictionConstraint.m_lowerLimit = -(frictionConstraint.m_friction * totalImpulse);
  120. frictionConstraint.m_upperLimit = frictionConstraint.m_friction * totalImpulse;
  121. btScalar residual = resolveSingleConstraintRowGeneric(frictionConstraint);
  122. leastSquaredResidual = btMax(leastSquaredResidual, residual * residual);
  123. if (frictionConstraint.m_multiBodyA)
  124. frictionConstraint.m_multiBodyA->setPosUpdated(false);
  125. if (frictionConstraint.m_multiBodyB)
  126. frictionConstraint.m_multiBodyB->setPosUpdated(false);
  127. }
  128. }
  129. }
  130. }
  131. return leastSquaredResidual;
  132. }
  133. btScalar btMultiBodyConstraintSolver::solveGroupCacheFriendlySetup(btCollisionObject** bodies, int numBodies, btPersistentManifold** manifoldPtr, int numManifolds, btTypedConstraint** constraints, int numConstraints, const btContactSolverInfo& infoGlobal, btIDebugDraw* debugDrawer)
  134. {
  135. m_multiBodyNonContactConstraints.resize(0);
  136. m_multiBodyNormalContactConstraints.resize(0);
  137. m_multiBodyFrictionContactConstraints.resize(0);
  138. m_multiBodyTorsionalFrictionContactConstraints.resize(0);
  139. m_data.m_jacobians.resize(0);
  140. m_data.m_deltaVelocitiesUnitImpulse.resize(0);
  141. m_data.m_deltaVelocities.resize(0);
  142. for (int i = 0; i < numBodies; i++)
  143. {
  144. const btMultiBodyLinkCollider* fcA = btMultiBodyLinkCollider::upcast(bodies[i]);
  145. if (fcA)
  146. {
  147. fcA->m_multiBody->setCompanionId(-1);
  148. }
  149. }
  150. btScalar val = btSequentialImpulseConstraintSolver::solveGroupCacheFriendlySetup(bodies, numBodies, manifoldPtr, numManifolds, constraints, numConstraints, infoGlobal, debugDrawer);
  151. return val;
  152. }
  153. void btMultiBodyConstraintSolver::applyDeltaVee(btScalar* delta_vee, btScalar impulse, int velocityIndex, int ndof)
  154. {
  155. for (int i = 0; i < ndof; ++i)
  156. m_data.m_deltaVelocities[velocityIndex + i] += delta_vee[i] * impulse;
  157. }
  158. btScalar btMultiBodyConstraintSolver::resolveSingleConstraintRowGeneric(const btMultiBodySolverConstraint& c)
  159. {
  160. btScalar deltaImpulse = c.m_rhs - btScalar(c.m_appliedImpulse) * c.m_cfm;
  161. btScalar deltaVelADotn = 0;
  162. btScalar deltaVelBDotn = 0;
  163. btSolverBody* bodyA = 0;
  164. btSolverBody* bodyB = 0;
  165. int ndofA = 0;
  166. int ndofB = 0;
  167. if (c.m_multiBodyA)
  168. {
  169. ndofA = c.m_multiBodyA->getNumDofs() + 6;
  170. for (int i = 0; i < ndofA; ++i)
  171. deltaVelADotn += m_data.m_jacobians[c.m_jacAindex + i] * m_data.m_deltaVelocities[c.m_deltaVelAindex + i];
  172. }
  173. else if (c.m_solverBodyIdA >= 0)
  174. {
  175. bodyA = &m_tmpSolverBodyPool[c.m_solverBodyIdA];
  176. deltaVelADotn += c.m_contactNormal1.dot(bodyA->internalGetDeltaLinearVelocity()) + c.m_relpos1CrossNormal.dot(bodyA->internalGetDeltaAngularVelocity());
  177. }
  178. if (c.m_multiBodyB)
  179. {
  180. ndofB = c.m_multiBodyB->getNumDofs() + 6;
  181. for (int i = 0; i < ndofB; ++i)
  182. deltaVelBDotn += m_data.m_jacobians[c.m_jacBindex + i] * m_data.m_deltaVelocities[c.m_deltaVelBindex + i];
  183. }
  184. else if (c.m_solverBodyIdB >= 0)
  185. {
  186. bodyB = &m_tmpSolverBodyPool[c.m_solverBodyIdB];
  187. deltaVelBDotn += c.m_contactNormal2.dot(bodyB->internalGetDeltaLinearVelocity()) + c.m_relpos2CrossNormal.dot(bodyB->internalGetDeltaAngularVelocity());
  188. }
  189. deltaImpulse -= deltaVelADotn * c.m_jacDiagABInv; //m_jacDiagABInv = 1./denom
  190. deltaImpulse -= deltaVelBDotn * c.m_jacDiagABInv;
  191. const btScalar sum = btScalar(c.m_appliedImpulse) + deltaImpulse;
  192. if (sum < c.m_lowerLimit)
  193. {
  194. deltaImpulse = c.m_lowerLimit - c.m_appliedImpulse;
  195. c.m_appliedImpulse = c.m_lowerLimit;
  196. }
  197. else if (sum > c.m_upperLimit)
  198. {
  199. deltaImpulse = c.m_upperLimit - c.m_appliedImpulse;
  200. c.m_appliedImpulse = c.m_upperLimit;
  201. }
  202. else
  203. {
  204. c.m_appliedImpulse = sum;
  205. }
  206. if (c.m_multiBodyA)
  207. {
  208. applyDeltaVee(&m_data.m_deltaVelocitiesUnitImpulse[c.m_jacAindex], deltaImpulse, c.m_deltaVelAindex, ndofA);
  209. #ifdef DIRECTLY_UPDATE_VELOCITY_DURING_SOLVER_ITERATIONS
  210. //note: update of the actual velocities (below) in the multibody does not have to happen now since m_deltaVelocities can be applied after all iterations
  211. //it would make the multibody solver more like the regular one with m_deltaVelocities being equivalent to btSolverBody::m_deltaLinearVelocity/m_deltaAngularVelocity
  212. c.m_multiBodyA->applyDeltaVeeMultiDof2(&m_data.m_deltaVelocitiesUnitImpulse[c.m_jacAindex], deltaImpulse);
  213. #endif //DIRECTLY_UPDATE_VELOCITY_DURING_SOLVER_ITERATIONS
  214. }
  215. else if (c.m_solverBodyIdA >= 0)
  216. {
  217. bodyA->internalApplyImpulse(c.m_contactNormal1 * bodyA->internalGetInvMass(), c.m_angularComponentA, deltaImpulse);
  218. }
  219. if (c.m_multiBodyB)
  220. {
  221. applyDeltaVee(&m_data.m_deltaVelocitiesUnitImpulse[c.m_jacBindex], deltaImpulse, c.m_deltaVelBindex, ndofB);
  222. #ifdef DIRECTLY_UPDATE_VELOCITY_DURING_SOLVER_ITERATIONS
  223. //note: update of the actual velocities (below) in the multibody does not have to happen now since m_deltaVelocities can be applied after all iterations
  224. //it would make the multibody solver more like the regular one with m_deltaVelocities being equivalent to btSolverBody::m_deltaLinearVelocity/m_deltaAngularVelocity
  225. c.m_multiBodyB->applyDeltaVeeMultiDof2(&m_data.m_deltaVelocitiesUnitImpulse[c.m_jacBindex], deltaImpulse);
  226. #endif //DIRECTLY_UPDATE_VELOCITY_DURING_SOLVER_ITERATIONS
  227. }
  228. else if (c.m_solverBodyIdB >= 0)
  229. {
  230. bodyB->internalApplyImpulse(c.m_contactNormal2 * bodyB->internalGetInvMass(), c.m_angularComponentB, deltaImpulse);
  231. }
  232. btScalar deltaVel = deltaImpulse / c.m_jacDiagABInv;
  233. return deltaVel;
  234. }
  235. btScalar btMultiBodyConstraintSolver::resolveConeFrictionConstraintRows(const btMultiBodySolverConstraint& cA1, const btMultiBodySolverConstraint& cB)
  236. {
  237. int ndofA = 0;
  238. int ndofB = 0;
  239. btSolverBody* bodyA = 0;
  240. btSolverBody* bodyB = 0;
  241. btScalar deltaImpulseB = 0.f;
  242. btScalar sumB = 0.f;
  243. {
  244. deltaImpulseB = cB.m_rhs - btScalar(cB.m_appliedImpulse) * cB.m_cfm;
  245. btScalar deltaVelADotn = 0;
  246. btScalar deltaVelBDotn = 0;
  247. if (cB.m_multiBodyA)
  248. {
  249. ndofA = cB.m_multiBodyA->getNumDofs() + 6;
  250. for (int i = 0; i < ndofA; ++i)
  251. deltaVelADotn += m_data.m_jacobians[cB.m_jacAindex + i] * m_data.m_deltaVelocities[cB.m_deltaVelAindex + i];
  252. }
  253. else if (cB.m_solverBodyIdA >= 0)
  254. {
  255. bodyA = &m_tmpSolverBodyPool[cB.m_solverBodyIdA];
  256. deltaVelADotn += cB.m_contactNormal1.dot(bodyA->internalGetDeltaLinearVelocity()) + cB.m_relpos1CrossNormal.dot(bodyA->internalGetDeltaAngularVelocity());
  257. }
  258. if (cB.m_multiBodyB)
  259. {
  260. ndofB = cB.m_multiBodyB->getNumDofs() + 6;
  261. for (int i = 0; i < ndofB; ++i)
  262. deltaVelBDotn += m_data.m_jacobians[cB.m_jacBindex + i] * m_data.m_deltaVelocities[cB.m_deltaVelBindex + i];
  263. }
  264. else if (cB.m_solverBodyIdB >= 0)
  265. {
  266. bodyB = &m_tmpSolverBodyPool[cB.m_solverBodyIdB];
  267. deltaVelBDotn += cB.m_contactNormal2.dot(bodyB->internalGetDeltaLinearVelocity()) + cB.m_relpos2CrossNormal.dot(bodyB->internalGetDeltaAngularVelocity());
  268. }
  269. deltaImpulseB -= deltaVelADotn * cB.m_jacDiagABInv; //m_jacDiagABInv = 1./denom
  270. deltaImpulseB -= deltaVelBDotn * cB.m_jacDiagABInv;
  271. sumB = btScalar(cB.m_appliedImpulse) + deltaImpulseB;
  272. }
  273. btScalar deltaImpulseA = 0.f;
  274. btScalar sumA = 0.f;
  275. const btMultiBodySolverConstraint& cA = cA1;
  276. {
  277. {
  278. deltaImpulseA = cA.m_rhs - btScalar(cA.m_appliedImpulse) * cA.m_cfm;
  279. btScalar deltaVelADotn = 0;
  280. btScalar deltaVelBDotn = 0;
  281. if (cA.m_multiBodyA)
  282. {
  283. ndofA = cA.m_multiBodyA->getNumDofs() + 6;
  284. for (int i = 0; i < ndofA; ++i)
  285. deltaVelADotn += m_data.m_jacobians[cA.m_jacAindex + i] * m_data.m_deltaVelocities[cA.m_deltaVelAindex + i];
  286. }
  287. else if (cA.m_solverBodyIdA >= 0)
  288. {
  289. bodyA = &m_tmpSolverBodyPool[cA.m_solverBodyIdA];
  290. deltaVelADotn += cA.m_contactNormal1.dot(bodyA->internalGetDeltaLinearVelocity()) + cA.m_relpos1CrossNormal.dot(bodyA->internalGetDeltaAngularVelocity());
  291. }
  292. if (cA.m_multiBodyB)
  293. {
  294. ndofB = cA.m_multiBodyB->getNumDofs() + 6;
  295. for (int i = 0; i < ndofB; ++i)
  296. deltaVelBDotn += m_data.m_jacobians[cA.m_jacBindex + i] * m_data.m_deltaVelocities[cA.m_deltaVelBindex + i];
  297. }
  298. else if (cA.m_solverBodyIdB >= 0)
  299. {
  300. bodyB = &m_tmpSolverBodyPool[cA.m_solverBodyIdB];
  301. deltaVelBDotn += cA.m_contactNormal2.dot(bodyB->internalGetDeltaLinearVelocity()) + cA.m_relpos2CrossNormal.dot(bodyB->internalGetDeltaAngularVelocity());
  302. }
  303. deltaImpulseA -= deltaVelADotn * cA.m_jacDiagABInv; //m_jacDiagABInv = 1./denom
  304. deltaImpulseA -= deltaVelBDotn * cA.m_jacDiagABInv;
  305. sumA = btScalar(cA.m_appliedImpulse) + deltaImpulseA;
  306. }
  307. }
  308. if (sumA * sumA + sumB * sumB >= cA.m_lowerLimit * cB.m_lowerLimit)
  309. {
  310. btScalar angle = btAtan2(sumA, sumB);
  311. btScalar sumAclipped = btFabs(cA.m_lowerLimit * btSin(angle));
  312. btScalar sumBclipped = btFabs(cB.m_lowerLimit * btCos(angle));
  313. if (sumA < -sumAclipped)
  314. {
  315. deltaImpulseA = -sumAclipped - cA.m_appliedImpulse;
  316. cA.m_appliedImpulse = -sumAclipped;
  317. }
  318. else if (sumA > sumAclipped)
  319. {
  320. deltaImpulseA = sumAclipped - cA.m_appliedImpulse;
  321. cA.m_appliedImpulse = sumAclipped;
  322. }
  323. else
  324. {
  325. cA.m_appliedImpulse = sumA;
  326. }
  327. if (sumB < -sumBclipped)
  328. {
  329. deltaImpulseB = -sumBclipped - cB.m_appliedImpulse;
  330. cB.m_appliedImpulse = -sumBclipped;
  331. }
  332. else if (sumB > sumBclipped)
  333. {
  334. deltaImpulseB = sumBclipped - cB.m_appliedImpulse;
  335. cB.m_appliedImpulse = sumBclipped;
  336. }
  337. else
  338. {
  339. cB.m_appliedImpulse = sumB;
  340. }
  341. //deltaImpulseA = sumAclipped-cA.m_appliedImpulse;
  342. //cA.m_appliedImpulse = sumAclipped;
  343. //deltaImpulseB = sumBclipped-cB.m_appliedImpulse;
  344. //cB.m_appliedImpulse = sumBclipped;
  345. }
  346. else
  347. {
  348. cA.m_appliedImpulse = sumA;
  349. cB.m_appliedImpulse = sumB;
  350. }
  351. if (cA.m_multiBodyA)
  352. {
  353. applyDeltaVee(&m_data.m_deltaVelocitiesUnitImpulse[cA.m_jacAindex], deltaImpulseA, cA.m_deltaVelAindex, ndofA);
  354. #ifdef DIRECTLY_UPDATE_VELOCITY_DURING_SOLVER_ITERATIONS
  355. //note: update of the actual velocities (below) in the multibody does not have to happen now since m_deltaVelocities can be applied after all iterations
  356. //it would make the multibody solver more like the regular one with m_deltaVelocities being equivalent to btSolverBody::m_deltaLinearVelocity/m_deltaAngularVelocity
  357. cA.m_multiBodyA->applyDeltaVeeMultiDof2(&m_data.m_deltaVelocitiesUnitImpulse[cA.m_jacAindex], deltaImpulseA);
  358. #endif //DIRECTLY_UPDATE_VELOCITY_DURING_SOLVER_ITERATIONS
  359. }
  360. else if (cA.m_solverBodyIdA >= 0)
  361. {
  362. bodyA->internalApplyImpulse(cA.m_contactNormal1 * bodyA->internalGetInvMass(), cA.m_angularComponentA, deltaImpulseA);
  363. }
  364. if (cA.m_multiBodyB)
  365. {
  366. applyDeltaVee(&m_data.m_deltaVelocitiesUnitImpulse[cA.m_jacBindex], deltaImpulseA, cA.m_deltaVelBindex, ndofB);
  367. #ifdef DIRECTLY_UPDATE_VELOCITY_DURING_SOLVER_ITERATIONS
  368. //note: update of the actual velocities (below) in the multibody does not have to happen now since m_deltaVelocities can be applied after all iterations
  369. //it would make the multibody solver more like the regular one with m_deltaVelocities being equivalent to btSolverBody::m_deltaLinearVelocity/m_deltaAngularVelocity
  370. cA.m_multiBodyB->applyDeltaVeeMultiDof2(&m_data.m_deltaVelocitiesUnitImpulse[cA.m_jacBindex], deltaImpulseA);
  371. #endif //DIRECTLY_UPDATE_VELOCITY_DURING_SOLVER_ITERATIONS
  372. }
  373. else if (cA.m_solverBodyIdB >= 0)
  374. {
  375. bodyB->internalApplyImpulse(cA.m_contactNormal2 * bodyB->internalGetInvMass(), cA.m_angularComponentB, deltaImpulseA);
  376. }
  377. if (cB.m_multiBodyA)
  378. {
  379. applyDeltaVee(&m_data.m_deltaVelocitiesUnitImpulse[cB.m_jacAindex], deltaImpulseB, cB.m_deltaVelAindex, ndofA);
  380. #ifdef DIRECTLY_UPDATE_VELOCITY_DURING_SOLVER_ITERATIONS
  381. //note: update of the actual velocities (below) in the multibody does not have to happen now since m_deltaVelocities can be applied after all iterations
  382. //it would make the multibody solver more like the regular one with m_deltaVelocities being equivalent to btSolverBody::m_deltaLinearVelocity/m_deltaAngularVelocity
  383. cB.m_multiBodyA->applyDeltaVeeMultiDof2(&m_data.m_deltaVelocitiesUnitImpulse[cB.m_jacAindex], deltaImpulseB);
  384. #endif //DIRECTLY_UPDATE_VELOCITY_DURING_SOLVER_ITERATIONS
  385. }
  386. else if (cB.m_solverBodyIdA >= 0)
  387. {
  388. bodyA->internalApplyImpulse(cB.m_contactNormal1 * bodyA->internalGetInvMass(), cB.m_angularComponentA, deltaImpulseB);
  389. }
  390. if (cB.m_multiBodyB)
  391. {
  392. applyDeltaVee(&m_data.m_deltaVelocitiesUnitImpulse[cB.m_jacBindex], deltaImpulseB, cB.m_deltaVelBindex, ndofB);
  393. #ifdef DIRECTLY_UPDATE_VELOCITY_DURING_SOLVER_ITERATIONS
  394. //note: update of the actual velocities (below) in the multibody does not have to happen now since m_deltaVelocities can be applied after all iterations
  395. //it would make the multibody solver more like the regular one with m_deltaVelocities being equivalent to btSolverBody::m_deltaLinearVelocity/m_deltaAngularVelocity
  396. cB.m_multiBodyB->applyDeltaVeeMultiDof2(&m_data.m_deltaVelocitiesUnitImpulse[cB.m_jacBindex], deltaImpulseB);
  397. #endif //DIRECTLY_UPDATE_VELOCITY_DURING_SOLVER_ITERATIONS
  398. }
  399. else if (cB.m_solverBodyIdB >= 0)
  400. {
  401. bodyB->internalApplyImpulse(cB.m_contactNormal2 * bodyB->internalGetInvMass(), cB.m_angularComponentB, deltaImpulseB);
  402. }
  403. btScalar deltaVel = deltaImpulseA / cA.m_jacDiagABInv + deltaImpulseB / cB.m_jacDiagABInv;
  404. return deltaVel;
  405. }
  406. void btMultiBodyConstraintSolver::setupMultiBodyContactConstraint(btMultiBodySolverConstraint& solverConstraint,
  407. const btVector3& contactNormal,
  408. btManifoldPoint& cp, const btContactSolverInfo& infoGlobal,
  409. btScalar& relaxation,
  410. bool isFriction, btScalar desiredVelocity, btScalar cfmSlip)
  411. {
  412. BT_PROFILE("setupMultiBodyContactConstraint");
  413. btVector3 rel_pos1;
  414. btVector3 rel_pos2;
  415. btMultiBody* multiBodyA = solverConstraint.m_multiBodyA;
  416. btMultiBody* multiBodyB = solverConstraint.m_multiBodyB;
  417. const btVector3& pos1 = cp.getPositionWorldOnA();
  418. const btVector3& pos2 = cp.getPositionWorldOnB();
  419. btSolverBody* bodyA = multiBodyA ? 0 : &m_tmpSolverBodyPool[solverConstraint.m_solverBodyIdA];
  420. btSolverBody* bodyB = multiBodyB ? 0 : &m_tmpSolverBodyPool[solverConstraint.m_solverBodyIdB];
  421. btRigidBody* rb0 = multiBodyA ? 0 : bodyA->m_originalBody;
  422. btRigidBody* rb1 = multiBodyB ? 0 : bodyB->m_originalBody;
  423. if (bodyA)
  424. rel_pos1 = pos1 - bodyA->getWorldTransform().getOrigin();
  425. if (bodyB)
  426. rel_pos2 = pos2 - bodyB->getWorldTransform().getOrigin();
  427. relaxation = infoGlobal.m_sor;
  428. btScalar invTimeStep = btScalar(1) / infoGlobal.m_timeStep;
  429. //cfm = 1 / ( dt * kp + kd )
  430. //erp = dt * kp / ( dt * kp + kd )
  431. btScalar cfm;
  432. btScalar erp;
  433. if (isFriction)
  434. {
  435. cfm = infoGlobal.m_frictionCFM;
  436. erp = infoGlobal.m_frictionERP;
  437. }
  438. else
  439. {
  440. cfm = infoGlobal.m_globalCfm;
  441. erp = infoGlobal.m_erp2;
  442. if ((cp.m_contactPointFlags & BT_CONTACT_FLAG_HAS_CONTACT_CFM) || (cp.m_contactPointFlags & BT_CONTACT_FLAG_HAS_CONTACT_ERP))
  443. {
  444. if (cp.m_contactPointFlags & BT_CONTACT_FLAG_HAS_CONTACT_CFM)
  445. cfm = cp.m_contactCFM;
  446. if (cp.m_contactPointFlags & BT_CONTACT_FLAG_HAS_CONTACT_ERP)
  447. erp = cp.m_contactERP;
  448. }
  449. else
  450. {
  451. if (cp.m_contactPointFlags & BT_CONTACT_FLAG_CONTACT_STIFFNESS_DAMPING)
  452. {
  453. btScalar denom = (infoGlobal.m_timeStep * cp.m_combinedContactStiffness1 + cp.m_combinedContactDamping1);
  454. if (denom < SIMD_EPSILON)
  455. {
  456. denom = SIMD_EPSILON;
  457. }
  458. cfm = btScalar(1) / denom;
  459. erp = (infoGlobal.m_timeStep * cp.m_combinedContactStiffness1) / denom;
  460. }
  461. }
  462. }
  463. cfm *= invTimeStep;
  464. if (multiBodyA)
  465. {
  466. if (solverConstraint.m_linkA < 0)
  467. {
  468. rel_pos1 = pos1 - multiBodyA->getBasePos();
  469. }
  470. else
  471. {
  472. rel_pos1 = pos1 - multiBodyA->getLink(solverConstraint.m_linkA).m_cachedWorldTransform.getOrigin();
  473. }
  474. const int ndofA = multiBodyA->getNumDofs() + 6;
  475. solverConstraint.m_deltaVelAindex = multiBodyA->getCompanionId();
  476. if (solverConstraint.m_deltaVelAindex < 0)
  477. {
  478. solverConstraint.m_deltaVelAindex = m_data.m_deltaVelocities.size();
  479. multiBodyA->setCompanionId(solverConstraint.m_deltaVelAindex);
  480. m_data.m_deltaVelocities.resize(m_data.m_deltaVelocities.size() + ndofA);
  481. }
  482. else
  483. {
  484. btAssert(m_data.m_deltaVelocities.size() >= solverConstraint.m_deltaVelAindex + ndofA);
  485. }
  486. solverConstraint.m_jacAindex = m_data.m_jacobians.size();
  487. m_data.m_jacobians.resize(m_data.m_jacobians.size() + ndofA);
  488. m_data.m_deltaVelocitiesUnitImpulse.resize(m_data.m_deltaVelocitiesUnitImpulse.size() + ndofA);
  489. btAssert(m_data.m_jacobians.size() == m_data.m_deltaVelocitiesUnitImpulse.size());
  490. btScalar* jac1 = &m_data.m_jacobians[solverConstraint.m_jacAindex];
  491. multiBodyA->fillContactJacobianMultiDof(solverConstraint.m_linkA, cp.getPositionWorldOnA(), contactNormal, jac1, m_data.scratch_r, m_data.scratch_v, m_data.scratch_m);
  492. btScalar* delta = &m_data.m_deltaVelocitiesUnitImpulse[solverConstraint.m_jacAindex];
  493. multiBodyA->calcAccelerationDeltasMultiDof(&m_data.m_jacobians[solverConstraint.m_jacAindex], delta, m_data.scratch_r, m_data.scratch_v);
  494. btVector3 torqueAxis0 = rel_pos1.cross(contactNormal);
  495. solverConstraint.m_relpos1CrossNormal = torqueAxis0;
  496. solverConstraint.m_contactNormal1 = contactNormal;
  497. }
  498. else
  499. {
  500. btVector3 torqueAxis0 = rel_pos1.cross(contactNormal);
  501. solverConstraint.m_relpos1CrossNormal = torqueAxis0;
  502. solverConstraint.m_contactNormal1 = contactNormal;
  503. solverConstraint.m_angularComponentA = rb0 ? rb0->getInvInertiaTensorWorld() * torqueAxis0 * rb0->getAngularFactor() : btVector3(0, 0, 0);
  504. }
  505. if (multiBodyB)
  506. {
  507. if (solverConstraint.m_linkB < 0)
  508. {
  509. rel_pos2 = pos2 - multiBodyB->getBasePos();
  510. }
  511. else
  512. {
  513. rel_pos2 = pos2 - multiBodyB->getLink(solverConstraint.m_linkB).m_cachedWorldTransform.getOrigin();
  514. }
  515. const int ndofB = multiBodyB->getNumDofs() + 6;
  516. solverConstraint.m_deltaVelBindex = multiBodyB->getCompanionId();
  517. if (solverConstraint.m_deltaVelBindex < 0)
  518. {
  519. solverConstraint.m_deltaVelBindex = m_data.m_deltaVelocities.size();
  520. multiBodyB->setCompanionId(solverConstraint.m_deltaVelBindex);
  521. m_data.m_deltaVelocities.resize(m_data.m_deltaVelocities.size() + ndofB);
  522. }
  523. solverConstraint.m_jacBindex = m_data.m_jacobians.size();
  524. m_data.m_jacobians.resize(m_data.m_jacobians.size() + ndofB);
  525. m_data.m_deltaVelocitiesUnitImpulse.resize(m_data.m_deltaVelocitiesUnitImpulse.size() + ndofB);
  526. btAssert(m_data.m_jacobians.size() == m_data.m_deltaVelocitiesUnitImpulse.size());
  527. multiBodyB->fillContactJacobianMultiDof(solverConstraint.m_linkB, cp.getPositionWorldOnB(), -contactNormal, &m_data.m_jacobians[solverConstraint.m_jacBindex], m_data.scratch_r, m_data.scratch_v, m_data.scratch_m);
  528. multiBodyB->calcAccelerationDeltasMultiDof(&m_data.m_jacobians[solverConstraint.m_jacBindex], &m_data.m_deltaVelocitiesUnitImpulse[solverConstraint.m_jacBindex], m_data.scratch_r, m_data.scratch_v);
  529. btVector3 torqueAxis1 = rel_pos2.cross(contactNormal);
  530. solverConstraint.m_relpos2CrossNormal = -torqueAxis1;
  531. solverConstraint.m_contactNormal2 = -contactNormal;
  532. }
  533. else
  534. {
  535. btVector3 torqueAxis1 = rel_pos2.cross(contactNormal);
  536. solverConstraint.m_relpos2CrossNormal = -torqueAxis1;
  537. solverConstraint.m_contactNormal2 = -contactNormal;
  538. solverConstraint.m_angularComponentB = rb1 ? rb1->getInvInertiaTensorWorld() * -torqueAxis1 * rb1->getAngularFactor() : btVector3(0, 0, 0);
  539. }
  540. {
  541. btVector3 vec;
  542. btScalar denom0 = 0.f;
  543. btScalar denom1 = 0.f;
  544. btScalar* jacB = 0;
  545. btScalar* jacA = 0;
  546. btScalar* lambdaA = 0;
  547. btScalar* lambdaB = 0;
  548. int ndofA = 0;
  549. if (multiBodyA)
  550. {
  551. ndofA = multiBodyA->getNumDofs() + 6;
  552. jacA = &m_data.m_jacobians[solverConstraint.m_jacAindex];
  553. lambdaA = &m_data.m_deltaVelocitiesUnitImpulse[solverConstraint.m_jacAindex];
  554. for (int i = 0; i < ndofA; ++i)
  555. {
  556. btScalar j = jacA[i];
  557. btScalar l = lambdaA[i];
  558. denom0 += j * l;
  559. }
  560. }
  561. else
  562. {
  563. if (rb0)
  564. {
  565. vec = (solverConstraint.m_angularComponentA).cross(rel_pos1);
  566. denom0 = rb0->getInvMass() + contactNormal.dot(vec);
  567. }
  568. }
  569. if (multiBodyB)
  570. {
  571. const int ndofB = multiBodyB->getNumDofs() + 6;
  572. jacB = &m_data.m_jacobians[solverConstraint.m_jacBindex];
  573. lambdaB = &m_data.m_deltaVelocitiesUnitImpulse[solverConstraint.m_jacBindex];
  574. for (int i = 0; i < ndofB; ++i)
  575. {
  576. btScalar j = jacB[i];
  577. btScalar l = lambdaB[i];
  578. denom1 += j * l;
  579. }
  580. }
  581. else
  582. {
  583. if (rb1)
  584. {
  585. vec = (-solverConstraint.m_angularComponentB).cross(rel_pos2);
  586. denom1 = rb1->getInvMass() + contactNormal.dot(vec);
  587. }
  588. }
  589. btScalar d = denom0 + denom1 + cfm;
  590. if (d > SIMD_EPSILON)
  591. {
  592. solverConstraint.m_jacDiagABInv = relaxation / (d);
  593. }
  594. else
  595. {
  596. //disable the constraint row to handle singularity/redundant constraint
  597. solverConstraint.m_jacDiagABInv = 0.f;
  598. }
  599. }
  600. //compute rhs and remaining solverConstraint fields
  601. btScalar restitution = 0.f;
  602. btScalar distance = 0;
  603. if (!isFriction)
  604. {
  605. distance = cp.getDistance() + infoGlobal.m_linearSlop;
  606. }
  607. else
  608. {
  609. if (cp.m_contactPointFlags & BT_CONTACT_FLAG_FRICTION_ANCHOR)
  610. {
  611. distance = (cp.getPositionWorldOnA() - cp.getPositionWorldOnB()).dot(contactNormal);
  612. }
  613. }
  614. btScalar rel_vel = 0.f;
  615. int ndofA = 0;
  616. int ndofB = 0;
  617. {
  618. btVector3 vel1, vel2;
  619. if (multiBodyA)
  620. {
  621. ndofA = multiBodyA->getNumDofs() + 6;
  622. btScalar* jacA = &m_data.m_jacobians[solverConstraint.m_jacAindex];
  623. for (int i = 0; i < ndofA; ++i)
  624. rel_vel += multiBodyA->getVelocityVector()[i] * jacA[i];
  625. }
  626. else
  627. {
  628. if (rb0)
  629. {
  630. rel_vel += (rb0->getVelocityInLocalPoint(rel_pos1) +
  631. (rb0->getTotalTorque() * rb0->getInvInertiaTensorWorld() * infoGlobal.m_timeStep).cross(rel_pos1) +
  632. rb0->getTotalForce() * rb0->getInvMass() * infoGlobal.m_timeStep)
  633. .dot(solverConstraint.m_contactNormal1);
  634. }
  635. }
  636. if (multiBodyB)
  637. {
  638. ndofB = multiBodyB->getNumDofs() + 6;
  639. btScalar* jacB = &m_data.m_jacobians[solverConstraint.m_jacBindex];
  640. for (int i = 0; i < ndofB; ++i)
  641. rel_vel += multiBodyB->getVelocityVector()[i] * jacB[i];
  642. }
  643. else
  644. {
  645. if (rb1)
  646. {
  647. rel_vel += (rb1->getVelocityInLocalPoint(rel_pos2) +
  648. (rb1->getTotalTorque() * rb1->getInvInertiaTensorWorld() * infoGlobal.m_timeStep).cross(rel_pos2) +
  649. rb1->getTotalForce() * rb1->getInvMass() * infoGlobal.m_timeStep)
  650. .dot(solverConstraint.m_contactNormal2);
  651. }
  652. }
  653. solverConstraint.m_friction = cp.m_combinedFriction;
  654. if (!isFriction)
  655. {
  656. restitution = restitutionCurve(rel_vel, cp.m_combinedRestitution, infoGlobal.m_restitutionVelocityThreshold);
  657. if (restitution <= btScalar(0.))
  658. {
  659. restitution = 0.f;
  660. }
  661. }
  662. }
  663. ///warm starting (or zero if disabled)
  664. //disable warmstarting for btMultiBody, it has issues gaining energy (==explosion)
  665. if (0) //infoGlobal.m_solverMode & SOLVER_USE_WARMSTARTING)
  666. {
  667. solverConstraint.m_appliedImpulse = isFriction ? 0 : cp.m_appliedImpulse * infoGlobal.m_warmstartingFactor;
  668. if (solverConstraint.m_appliedImpulse)
  669. {
  670. if (multiBodyA)
  671. {
  672. btScalar impulse = solverConstraint.m_appliedImpulse;
  673. btScalar* deltaV = &m_data.m_deltaVelocitiesUnitImpulse[solverConstraint.m_jacAindex];
  674. multiBodyA->applyDeltaVeeMultiDof(deltaV, impulse);
  675. applyDeltaVee(deltaV, impulse, solverConstraint.m_deltaVelAindex, ndofA);
  676. }
  677. else
  678. {
  679. if (rb0)
  680. bodyA->internalApplyImpulse(solverConstraint.m_contactNormal1 * bodyA->internalGetInvMass() * rb0->getLinearFactor(), solverConstraint.m_angularComponentA, solverConstraint.m_appliedImpulse);
  681. }
  682. if (multiBodyB)
  683. {
  684. btScalar impulse = solverConstraint.m_appliedImpulse;
  685. btScalar* deltaV = &m_data.m_deltaVelocitiesUnitImpulse[solverConstraint.m_jacBindex];
  686. multiBodyB->applyDeltaVeeMultiDof(deltaV, impulse);
  687. applyDeltaVee(deltaV, impulse, solverConstraint.m_deltaVelBindex, ndofB);
  688. }
  689. else
  690. {
  691. if (rb1)
  692. bodyB->internalApplyImpulse(-solverConstraint.m_contactNormal2 * bodyB->internalGetInvMass() * rb1->getLinearFactor(), -solverConstraint.m_angularComponentB, -(btScalar)solverConstraint.m_appliedImpulse);
  693. }
  694. }
  695. }
  696. else
  697. {
  698. solverConstraint.m_appliedImpulse = 0.f;
  699. }
  700. solverConstraint.m_appliedPushImpulse = 0.f;
  701. {
  702. btScalar positionalError = 0.f;
  703. btScalar velocityError = restitution - rel_vel; // * damping; //note for friction restitution is always set to 0 (check above) so it is acutally velocityError = -rel_vel for friction
  704. if (isFriction)
  705. {
  706. positionalError = -distance * erp / infoGlobal.m_timeStep;
  707. }
  708. else
  709. {
  710. if (distance > 0)
  711. {
  712. positionalError = 0;
  713. velocityError -= distance / infoGlobal.m_timeStep;
  714. }
  715. else
  716. {
  717. positionalError = -distance * erp / infoGlobal.m_timeStep;
  718. }
  719. }
  720. btScalar penetrationImpulse = positionalError * solverConstraint.m_jacDiagABInv;
  721. btScalar velocityImpulse = velocityError * solverConstraint.m_jacDiagABInv;
  722. if (!isFriction)
  723. {
  724. // if (!infoGlobal.m_splitImpulse || (penetration > infoGlobal.m_splitImpulsePenetrationThreshold))
  725. {
  726. //combine position and velocity into rhs
  727. solverConstraint.m_rhs = penetrationImpulse + velocityImpulse;
  728. solverConstraint.m_rhsPenetration = 0.f;
  729. }
  730. /*else
  731. {
  732. //split position and velocity into rhs and m_rhsPenetration
  733. solverConstraint.m_rhs = velocityImpulse;
  734. solverConstraint.m_rhsPenetration = penetrationImpulse;
  735. }
  736. */
  737. solverConstraint.m_lowerLimit = 0;
  738. solverConstraint.m_upperLimit = 1e10f;
  739. }
  740. else
  741. {
  742. solverConstraint.m_rhs = penetrationImpulse + velocityImpulse;
  743. solverConstraint.m_rhsPenetration = 0.f;
  744. solverConstraint.m_lowerLimit = -solverConstraint.m_friction;
  745. solverConstraint.m_upperLimit = solverConstraint.m_friction;
  746. }
  747. solverConstraint.m_cfm = cfm * solverConstraint.m_jacDiagABInv;
  748. }
  749. }
  750. void btMultiBodyConstraintSolver::setupMultiBodyTorsionalFrictionConstraint(btMultiBodySolverConstraint& solverConstraint,
  751. const btVector3& constraintNormal,
  752. btManifoldPoint& cp,
  753. btScalar combinedTorsionalFriction,
  754. const btContactSolverInfo& infoGlobal,
  755. btScalar& relaxation,
  756. bool isFriction, btScalar desiredVelocity, btScalar cfmSlip)
  757. {
  758. BT_PROFILE("setupMultiBodyRollingFrictionConstraint");
  759. btVector3 rel_pos1;
  760. btVector3 rel_pos2;
  761. btMultiBody* multiBodyA = solverConstraint.m_multiBodyA;
  762. btMultiBody* multiBodyB = solverConstraint.m_multiBodyB;
  763. const btVector3& pos1 = cp.getPositionWorldOnA();
  764. const btVector3& pos2 = cp.getPositionWorldOnB();
  765. btSolverBody* bodyA = multiBodyA ? 0 : &m_tmpSolverBodyPool[solverConstraint.m_solverBodyIdA];
  766. btSolverBody* bodyB = multiBodyB ? 0 : &m_tmpSolverBodyPool[solverConstraint.m_solverBodyIdB];
  767. btRigidBody* rb0 = multiBodyA ? 0 : bodyA->m_originalBody;
  768. btRigidBody* rb1 = multiBodyB ? 0 : bodyB->m_originalBody;
  769. if (bodyA)
  770. rel_pos1 = pos1 - bodyA->getWorldTransform().getOrigin();
  771. if (bodyB)
  772. rel_pos2 = pos2 - bodyB->getWorldTransform().getOrigin();
  773. relaxation = infoGlobal.m_sor;
  774. // btScalar invTimeStep = btScalar(1)/infoGlobal.m_timeStep;
  775. if (multiBodyA)
  776. {
  777. if (solverConstraint.m_linkA < 0)
  778. {
  779. rel_pos1 = pos1 - multiBodyA->getBasePos();
  780. }
  781. else
  782. {
  783. rel_pos1 = pos1 - multiBodyA->getLink(solverConstraint.m_linkA).m_cachedWorldTransform.getOrigin();
  784. }
  785. const int ndofA = multiBodyA->getNumDofs() + 6;
  786. solverConstraint.m_deltaVelAindex = multiBodyA->getCompanionId();
  787. if (solverConstraint.m_deltaVelAindex < 0)
  788. {
  789. solverConstraint.m_deltaVelAindex = m_data.m_deltaVelocities.size();
  790. multiBodyA->setCompanionId(solverConstraint.m_deltaVelAindex);
  791. m_data.m_deltaVelocities.resize(m_data.m_deltaVelocities.size() + ndofA);
  792. }
  793. else
  794. {
  795. btAssert(m_data.m_deltaVelocities.size() >= solverConstraint.m_deltaVelAindex + ndofA);
  796. }
  797. solverConstraint.m_jacAindex = m_data.m_jacobians.size();
  798. m_data.m_jacobians.resize(m_data.m_jacobians.size() + ndofA);
  799. m_data.m_deltaVelocitiesUnitImpulse.resize(m_data.m_deltaVelocitiesUnitImpulse.size() + ndofA);
  800. btAssert(m_data.m_jacobians.size() == m_data.m_deltaVelocitiesUnitImpulse.size());
  801. btScalar* jac1 = &m_data.m_jacobians[solverConstraint.m_jacAindex];
  802. multiBodyA->fillConstraintJacobianMultiDof(solverConstraint.m_linkA, cp.getPositionWorldOnA(), constraintNormal, btVector3(0, 0, 0), jac1, m_data.scratch_r, m_data.scratch_v, m_data.scratch_m);
  803. btScalar* delta = &m_data.m_deltaVelocitiesUnitImpulse[solverConstraint.m_jacAindex];
  804. multiBodyA->calcAccelerationDeltasMultiDof(&m_data.m_jacobians[solverConstraint.m_jacAindex], delta, m_data.scratch_r, m_data.scratch_v);
  805. btVector3 torqueAxis0 = -constraintNormal;
  806. solverConstraint.m_relpos1CrossNormal = torqueAxis0;
  807. solverConstraint.m_contactNormal1 = btVector3(0, 0, 0);
  808. }
  809. else
  810. {
  811. btVector3 torqueAxis0 = -constraintNormal;
  812. solverConstraint.m_relpos1CrossNormal = torqueAxis0;
  813. solverConstraint.m_contactNormal1 = btVector3(0, 0, 0);
  814. solverConstraint.m_angularComponentA = rb0 ? rb0->getInvInertiaTensorWorld() * torqueAxis0 * rb0->getAngularFactor() : btVector3(0, 0, 0);
  815. }
  816. if (multiBodyB)
  817. {
  818. if (solverConstraint.m_linkB < 0)
  819. {
  820. rel_pos2 = pos2 - multiBodyB->getBasePos();
  821. }
  822. else
  823. {
  824. rel_pos2 = pos2 - multiBodyB->getLink(solverConstraint.m_linkB).m_cachedWorldTransform.getOrigin();
  825. }
  826. const int ndofB = multiBodyB->getNumDofs() + 6;
  827. solverConstraint.m_deltaVelBindex = multiBodyB->getCompanionId();
  828. if (solverConstraint.m_deltaVelBindex < 0)
  829. {
  830. solverConstraint.m_deltaVelBindex = m_data.m_deltaVelocities.size();
  831. multiBodyB->setCompanionId(solverConstraint.m_deltaVelBindex);
  832. m_data.m_deltaVelocities.resize(m_data.m_deltaVelocities.size() + ndofB);
  833. }
  834. solverConstraint.m_jacBindex = m_data.m_jacobians.size();
  835. m_data.m_jacobians.resize(m_data.m_jacobians.size() + ndofB);
  836. m_data.m_deltaVelocitiesUnitImpulse.resize(m_data.m_deltaVelocitiesUnitImpulse.size() + ndofB);
  837. btAssert(m_data.m_jacobians.size() == m_data.m_deltaVelocitiesUnitImpulse.size());
  838. multiBodyB->fillConstraintJacobianMultiDof(solverConstraint.m_linkB, cp.getPositionWorldOnB(), -constraintNormal, btVector3(0, 0, 0), &m_data.m_jacobians[solverConstraint.m_jacBindex], m_data.scratch_r, m_data.scratch_v, m_data.scratch_m);
  839. multiBodyB->calcAccelerationDeltasMultiDof(&m_data.m_jacobians[solverConstraint.m_jacBindex], &m_data.m_deltaVelocitiesUnitImpulse[solverConstraint.m_jacBindex], m_data.scratch_r, m_data.scratch_v);
  840. btVector3 torqueAxis1 = constraintNormal;
  841. solverConstraint.m_relpos2CrossNormal = torqueAxis1;
  842. solverConstraint.m_contactNormal2 = -btVector3(0, 0, 0);
  843. }
  844. else
  845. {
  846. btVector3 torqueAxis1 = constraintNormal;
  847. solverConstraint.m_relpos2CrossNormal = torqueAxis1;
  848. solverConstraint.m_contactNormal2 = -btVector3(0, 0, 0);
  849. solverConstraint.m_angularComponentB = rb1 ? rb1->getInvInertiaTensorWorld() * torqueAxis1 * rb1->getAngularFactor() : btVector3(0, 0, 0);
  850. }
  851. {
  852. btScalar denom0 = 0.f;
  853. btScalar denom1 = 0.f;
  854. btScalar* jacB = 0;
  855. btScalar* jacA = 0;
  856. btScalar* lambdaA = 0;
  857. btScalar* lambdaB = 0;
  858. int ndofA = 0;
  859. if (multiBodyA)
  860. {
  861. ndofA = multiBodyA->getNumDofs() + 6;
  862. jacA = &m_data.m_jacobians[solverConstraint.m_jacAindex];
  863. lambdaA = &m_data.m_deltaVelocitiesUnitImpulse[solverConstraint.m_jacAindex];
  864. for (int i = 0; i < ndofA; ++i)
  865. {
  866. btScalar j = jacA[i];
  867. btScalar l = lambdaA[i];
  868. denom0 += j * l;
  869. }
  870. }
  871. else
  872. {
  873. if (rb0)
  874. {
  875. btVector3 iMJaA = rb0 ? rb0->getInvInertiaTensorWorld() * solverConstraint.m_relpos1CrossNormal : btVector3(0, 0, 0);
  876. denom0 = iMJaA.dot(solverConstraint.m_relpos1CrossNormal);
  877. }
  878. }
  879. if (multiBodyB)
  880. {
  881. const int ndofB = multiBodyB->getNumDofs() + 6;
  882. jacB = &m_data.m_jacobians[solverConstraint.m_jacBindex];
  883. lambdaB = &m_data.m_deltaVelocitiesUnitImpulse[solverConstraint.m_jacBindex];
  884. for (int i = 0; i < ndofB; ++i)
  885. {
  886. btScalar j = jacB[i];
  887. btScalar l = lambdaB[i];
  888. denom1 += j * l;
  889. }
  890. }
  891. else
  892. {
  893. if (rb1)
  894. {
  895. btVector3 iMJaB = rb1 ? rb1->getInvInertiaTensorWorld() * solverConstraint.m_relpos2CrossNormal : btVector3(0, 0, 0);
  896. denom1 = iMJaB.dot(solverConstraint.m_relpos2CrossNormal);
  897. }
  898. }
  899. btScalar d = denom0 + denom1 + infoGlobal.m_globalCfm;
  900. if (d > SIMD_EPSILON)
  901. {
  902. solverConstraint.m_jacDiagABInv = relaxation / (d);
  903. }
  904. else
  905. {
  906. //disable the constraint row to handle singularity/redundant constraint
  907. solverConstraint.m_jacDiagABInv = 0.f;
  908. }
  909. }
  910. //compute rhs and remaining solverConstraint fields
  911. btScalar restitution = 0.f;
  912. btScalar penetration = isFriction ? 0 : cp.getDistance();
  913. btScalar rel_vel = 0.f;
  914. int ndofA = 0;
  915. int ndofB = 0;
  916. {
  917. btVector3 vel1, vel2;
  918. if (multiBodyA)
  919. {
  920. ndofA = multiBodyA->getNumDofs() + 6;
  921. btScalar* jacA = &m_data.m_jacobians[solverConstraint.m_jacAindex];
  922. for (int i = 0; i < ndofA; ++i)
  923. rel_vel += multiBodyA->getVelocityVector()[i] * jacA[i];
  924. }
  925. else
  926. {
  927. if (rb0)
  928. {
  929. btSolverBody* solverBodyA = &m_tmpSolverBodyPool[solverConstraint.m_solverBodyIdA];
  930. rel_vel += solverConstraint.m_contactNormal1.dot(rb0 ? solverBodyA->m_linearVelocity + solverBodyA->m_externalForceImpulse : btVector3(0, 0, 0)) + solverConstraint.m_relpos1CrossNormal.dot(rb0 ? solverBodyA->m_angularVelocity : btVector3(0, 0, 0));
  931. }
  932. }
  933. if (multiBodyB)
  934. {
  935. ndofB = multiBodyB->getNumDofs() + 6;
  936. btScalar* jacB = &m_data.m_jacobians[solverConstraint.m_jacBindex];
  937. for (int i = 0; i < ndofB; ++i)
  938. rel_vel += multiBodyB->getVelocityVector()[i] * jacB[i];
  939. }
  940. else
  941. {
  942. if (rb1)
  943. {
  944. btSolverBody* solverBodyB = &m_tmpSolverBodyPool[solverConstraint.m_solverBodyIdB];
  945. rel_vel += solverConstraint.m_contactNormal2.dot(rb1 ? solverBodyB->m_linearVelocity + solverBodyB->m_externalForceImpulse : btVector3(0, 0, 0)) + solverConstraint.m_relpos2CrossNormal.dot(rb1 ? solverBodyB->m_angularVelocity : btVector3(0, 0, 0));
  946. }
  947. }
  948. solverConstraint.m_friction = combinedTorsionalFriction;
  949. if (!isFriction)
  950. {
  951. restitution = restitutionCurve(rel_vel, cp.m_combinedRestitution, infoGlobal.m_restitutionVelocityThreshold);
  952. if (restitution <= btScalar(0.))
  953. {
  954. restitution = 0.f;
  955. }
  956. }
  957. }
  958. solverConstraint.m_appliedImpulse = 0.f;
  959. solverConstraint.m_appliedPushImpulse = 0.f;
  960. {
  961. btScalar velocityError = 0 - rel_vel; // * damping; //note for friction restitution is always set to 0 (check above) so it is acutally velocityError = -rel_vel for friction
  962. btScalar velocityImpulse = velocityError * solverConstraint.m_jacDiagABInv;
  963. solverConstraint.m_rhs = velocityImpulse;
  964. solverConstraint.m_rhsPenetration = 0.f;
  965. solverConstraint.m_lowerLimit = -solverConstraint.m_friction;
  966. solverConstraint.m_upperLimit = solverConstraint.m_friction;
  967. solverConstraint.m_cfm = infoGlobal.m_globalCfm * solverConstraint.m_jacDiagABInv;
  968. }
  969. }
  970. btMultiBodySolverConstraint& btMultiBodyConstraintSolver::addMultiBodyFrictionConstraint(const btVector3& normalAxis, btPersistentManifold* manifold, int frictionIndex, btManifoldPoint& cp, btCollisionObject* colObj0, btCollisionObject* colObj1, btScalar relaxation, const btContactSolverInfo& infoGlobal, btScalar desiredVelocity, btScalar cfmSlip)
  971. {
  972. BT_PROFILE("addMultiBodyFrictionConstraint");
  973. btMultiBodySolverConstraint& solverConstraint = m_multiBodyFrictionContactConstraints.expandNonInitializing();
  974. solverConstraint.m_orgConstraint = 0;
  975. solverConstraint.m_orgDofIndex = -1;
  976. solverConstraint.m_frictionIndex = frictionIndex;
  977. bool isFriction = true;
  978. const btMultiBodyLinkCollider* fcA = btMultiBodyLinkCollider::upcast(manifold->getBody0());
  979. const btMultiBodyLinkCollider* fcB = btMultiBodyLinkCollider::upcast(manifold->getBody1());
  980. btMultiBody* mbA = fcA ? fcA->m_multiBody : 0;
  981. btMultiBody* mbB = fcB ? fcB->m_multiBody : 0;
  982. int solverBodyIdA = mbA ? -1 : getOrInitSolverBody(*colObj0, infoGlobal.m_timeStep);
  983. int solverBodyIdB = mbB ? -1 : getOrInitSolverBody(*colObj1, infoGlobal.m_timeStep);
  984. solverConstraint.m_solverBodyIdA = solverBodyIdA;
  985. solverConstraint.m_solverBodyIdB = solverBodyIdB;
  986. solverConstraint.m_multiBodyA = mbA;
  987. if (mbA)
  988. solverConstraint.m_linkA = fcA->m_link;
  989. solverConstraint.m_multiBodyB = mbB;
  990. if (mbB)
  991. solverConstraint.m_linkB = fcB->m_link;
  992. solverConstraint.m_originalContactPoint = &cp;
  993. setupMultiBodyContactConstraint(solverConstraint, normalAxis, cp, infoGlobal, relaxation, isFriction, desiredVelocity, cfmSlip);
  994. return solverConstraint;
  995. }
  996. btMultiBodySolverConstraint& btMultiBodyConstraintSolver::addMultiBodyTorsionalFrictionConstraint(const btVector3& normalAxis, btPersistentManifold* manifold, int frictionIndex, btManifoldPoint& cp,
  997. btScalar combinedTorsionalFriction,
  998. btCollisionObject* colObj0, btCollisionObject* colObj1, btScalar relaxation, const btContactSolverInfo& infoGlobal, btScalar desiredVelocity, btScalar cfmSlip)
  999. {
  1000. BT_PROFILE("addMultiBodyRollingFrictionConstraint");
  1001. bool useTorsionalAndConeFriction = (infoGlobal.m_solverMode & SOLVER_USE_2_FRICTION_DIRECTIONS && ((infoGlobal.m_solverMode & SOLVER_DISABLE_IMPLICIT_CONE_FRICTION) == 0));
  1002. btMultiBodySolverConstraint& solverConstraint = useTorsionalAndConeFriction ? m_multiBodyTorsionalFrictionContactConstraints.expandNonInitializing() : m_multiBodyFrictionContactConstraints.expandNonInitializing();
  1003. solverConstraint.m_orgConstraint = 0;
  1004. solverConstraint.m_orgDofIndex = -1;
  1005. solverConstraint.m_frictionIndex = frictionIndex;
  1006. bool isFriction = true;
  1007. const btMultiBodyLinkCollider* fcA = btMultiBodyLinkCollider::upcast(manifold->getBody0());
  1008. const btMultiBodyLinkCollider* fcB = btMultiBodyLinkCollider::upcast(manifold->getBody1());
  1009. btMultiBody* mbA = fcA ? fcA->m_multiBody : 0;
  1010. btMultiBody* mbB = fcB ? fcB->m_multiBody : 0;
  1011. int solverBodyIdA = mbA ? -1 : getOrInitSolverBody(*colObj0, infoGlobal.m_timeStep);
  1012. int solverBodyIdB = mbB ? -1 : getOrInitSolverBody(*colObj1, infoGlobal.m_timeStep);
  1013. solverConstraint.m_solverBodyIdA = solverBodyIdA;
  1014. solverConstraint.m_solverBodyIdB = solverBodyIdB;
  1015. solverConstraint.m_multiBodyA = mbA;
  1016. if (mbA)
  1017. solverConstraint.m_linkA = fcA->m_link;
  1018. solverConstraint.m_multiBodyB = mbB;
  1019. if (mbB)
  1020. solverConstraint.m_linkB = fcB->m_link;
  1021. solverConstraint.m_originalContactPoint = &cp;
  1022. setupMultiBodyTorsionalFrictionConstraint(solverConstraint, normalAxis, cp, combinedTorsionalFriction, infoGlobal, relaxation, isFriction, desiredVelocity, cfmSlip);
  1023. return solverConstraint;
  1024. }
  1025. void btMultiBodyConstraintSolver::convertMultiBodyContact(btPersistentManifold* manifold, const btContactSolverInfo& infoGlobal)
  1026. {
  1027. const btMultiBodyLinkCollider* fcA = btMultiBodyLinkCollider::upcast(manifold->getBody0());
  1028. const btMultiBodyLinkCollider* fcB = btMultiBodyLinkCollider::upcast(manifold->getBody1());
  1029. btMultiBody* mbA = fcA ? fcA->m_multiBody : 0;
  1030. btMultiBody* mbB = fcB ? fcB->m_multiBody : 0;
  1031. btCollisionObject *colObj0 = 0, *colObj1 = 0;
  1032. colObj0 = (btCollisionObject*)manifold->getBody0();
  1033. colObj1 = (btCollisionObject*)manifold->getBody1();
  1034. int solverBodyIdA = mbA ? -1 : getOrInitSolverBody(*colObj0, infoGlobal.m_timeStep);
  1035. int solverBodyIdB = mbB ? -1 : getOrInitSolverBody(*colObj1, infoGlobal.m_timeStep);
  1036. // btSolverBody* solverBodyA = mbA ? 0 : &m_tmpSolverBodyPool[solverBodyIdA];
  1037. // btSolverBody* solverBodyB = mbB ? 0 : &m_tmpSolverBodyPool[solverBodyIdB];
  1038. ///avoid collision response between two static objects
  1039. // if (!solverBodyA || (solverBodyA->m_invMass.isZero() && (!solverBodyB || solverBodyB->m_invMass.isZero())))
  1040. // return;
  1041. //only a single rollingFriction per manifold
  1042. int rollingFriction = 1;
  1043. for (int j = 0; j < manifold->getNumContacts(); j++)
  1044. {
  1045. btManifoldPoint& cp = manifold->getContactPoint(j);
  1046. if (cp.getDistance() <= manifold->getContactProcessingThreshold())
  1047. {
  1048. btScalar relaxation;
  1049. int frictionIndex = m_multiBodyNormalContactConstraints.size();
  1050. btMultiBodySolverConstraint& solverConstraint = m_multiBodyNormalContactConstraints.expandNonInitializing();
  1051. // btRigidBody* rb0 = btRigidBody::upcast(colObj0);
  1052. // btRigidBody* rb1 = btRigidBody::upcast(colObj1);
  1053. solverConstraint.m_orgConstraint = 0;
  1054. solverConstraint.m_orgDofIndex = -1;
  1055. solverConstraint.m_solverBodyIdA = solverBodyIdA;
  1056. solverConstraint.m_solverBodyIdB = solverBodyIdB;
  1057. solverConstraint.m_multiBodyA = mbA;
  1058. if (mbA)
  1059. solverConstraint.m_linkA = fcA->m_link;
  1060. solverConstraint.m_multiBodyB = mbB;
  1061. if (mbB)
  1062. solverConstraint.m_linkB = fcB->m_link;
  1063. solverConstraint.m_originalContactPoint = &cp;
  1064. bool isFriction = false;
  1065. setupMultiBodyContactConstraint(solverConstraint, cp.m_normalWorldOnB, cp, infoGlobal, relaxation, isFriction);
  1066. // const btVector3& pos1 = cp.getPositionWorldOnA();
  1067. // const btVector3& pos2 = cp.getPositionWorldOnB();
  1068. /////setup the friction constraints
  1069. #define ENABLE_FRICTION
  1070. #ifdef ENABLE_FRICTION
  1071. solverConstraint.m_frictionIndex = m_multiBodyFrictionContactConstraints.size();
  1072. ///Bullet has several options to set the friction directions
  1073. ///By default, each contact has only a single friction direction that is recomputed automatically every frame
  1074. ///based on the relative linear velocity.
  1075. ///If the relative velocity is zero, it will automatically compute a friction direction.
  1076. ///You can also enable two friction directions, using the SOLVER_USE_2_FRICTION_DIRECTIONS.
  1077. ///In that case, the second friction direction will be orthogonal to both contact normal and first friction direction.
  1078. ///
  1079. ///If you choose SOLVER_DISABLE_VELOCITY_DEPENDENT_FRICTION_DIRECTION, then the friction will be independent from the relative projected velocity.
  1080. ///
  1081. ///The user can manually override the friction directions for certain contacts using a contact callback,
  1082. ///and set the cp.m_lateralFrictionInitialized to true
  1083. ///In that case, you can set the target relative motion in each friction direction (cp.m_contactMotion1 and cp.m_contactMotion2)
  1084. ///this will give a conveyor belt effect
  1085. ///
  1086. btPlaneSpace1(cp.m_normalWorldOnB, cp.m_lateralFrictionDir1, cp.m_lateralFrictionDir2);
  1087. cp.m_lateralFrictionDir1.normalize();
  1088. cp.m_lateralFrictionDir2.normalize();
  1089. if (rollingFriction > 0)
  1090. {
  1091. if (cp.m_combinedSpinningFriction > 0)
  1092. {
  1093. addMultiBodyTorsionalFrictionConstraint(cp.m_normalWorldOnB, manifold, frictionIndex, cp, cp.m_combinedSpinningFriction, colObj0, colObj1, relaxation, infoGlobal);
  1094. }
  1095. if (cp.m_combinedRollingFriction > 0)
  1096. {
  1097. applyAnisotropicFriction(colObj0, cp.m_lateralFrictionDir1, btCollisionObject::CF_ANISOTROPIC_ROLLING_FRICTION);
  1098. applyAnisotropicFriction(colObj1, cp.m_lateralFrictionDir1, btCollisionObject::CF_ANISOTROPIC_ROLLING_FRICTION);
  1099. applyAnisotropicFriction(colObj0, cp.m_lateralFrictionDir2, btCollisionObject::CF_ANISOTROPIC_ROLLING_FRICTION);
  1100. applyAnisotropicFriction(colObj1, cp.m_lateralFrictionDir2, btCollisionObject::CF_ANISOTROPIC_ROLLING_FRICTION);
  1101. if (cp.m_lateralFrictionDir1.length() > 0.001)
  1102. addMultiBodyTorsionalFrictionConstraint(cp.m_lateralFrictionDir1, manifold, frictionIndex, cp, cp.m_combinedRollingFriction, colObj0, colObj1, relaxation, infoGlobal);
  1103. if (cp.m_lateralFrictionDir2.length() > 0.001)
  1104. addMultiBodyTorsionalFrictionConstraint(cp.m_lateralFrictionDir2, manifold, frictionIndex, cp, cp.m_combinedRollingFriction, colObj0, colObj1, relaxation, infoGlobal);
  1105. }
  1106. rollingFriction--;
  1107. }
  1108. if (!(infoGlobal.m_solverMode & SOLVER_ENABLE_FRICTION_DIRECTION_CACHING) || !(cp.m_contactPointFlags & BT_CONTACT_FLAG_LATERAL_FRICTION_INITIALIZED))
  1109. { /*
  1110. cp.m_lateralFrictionDir1 = vel - cp.m_normalWorldOnB * rel_vel;
  1111. btScalar lat_rel_vel = cp.m_lateralFrictionDir1.length2();
  1112. if (!(infoGlobal.m_solverMode & SOLVER_DISABLE_VELOCITY_DEPENDENT_FRICTION_DIRECTION) && lat_rel_vel > SIMD_EPSILON)
  1113. {
  1114. cp.m_lateralFrictionDir1 *= 1.f/btSqrt(lat_rel_vel);
  1115. if((infoGlobal.m_solverMode & SOLVER_USE_2_FRICTION_DIRECTIONS))
  1116. {
  1117. cp.m_lateralFrictionDir2 = cp.m_lateralFrictionDir1.cross(cp.m_normalWorldOnB);
  1118. cp.m_lateralFrictionDir2.normalize();//??
  1119. applyAnisotropicFriction(colObj0,cp.m_lateralFrictionDir2,btCollisionObject::CF_ANISOTROPIC_FRICTION);
  1120. applyAnisotropicFriction(colObj1,cp.m_lateralFrictionDir2,btCollisionObject::CF_ANISOTROPIC_FRICTION);
  1121. addMultiBodyFrictionConstraint(cp.m_lateralFrictionDir2,solverBodyIdA,solverBodyIdB,frictionIndex,cp,rel_pos1,rel_pos2,colObj0,colObj1, relaxation);
  1122. }
  1123. applyAnisotropicFriction(colObj0,cp.m_lateralFrictionDir1,btCollisionObject::CF_ANISOTROPIC_FRICTION);
  1124. applyAnisotropicFriction(colObj1,cp.m_lateralFrictionDir1,btCollisionObject::CF_ANISOTROPIC_FRICTION);
  1125. addMultiBodyFrictionConstraint(cp.m_lateralFrictionDir1,solverBodyIdA,solverBodyIdB,frictionIndex,cp,rel_pos1,rel_pos2,colObj0,colObj1, relaxation);
  1126. } else
  1127. */
  1128. {
  1129. applyAnisotropicFriction(colObj0, cp.m_lateralFrictionDir1, btCollisionObject::CF_ANISOTROPIC_FRICTION);
  1130. applyAnisotropicFriction(colObj1, cp.m_lateralFrictionDir1, btCollisionObject::CF_ANISOTROPIC_FRICTION);
  1131. addMultiBodyFrictionConstraint(cp.m_lateralFrictionDir1, manifold, frictionIndex, cp, colObj0, colObj1, relaxation, infoGlobal);
  1132. if ((infoGlobal.m_solverMode & SOLVER_USE_2_FRICTION_DIRECTIONS))
  1133. {
  1134. applyAnisotropicFriction(colObj0, cp.m_lateralFrictionDir2, btCollisionObject::CF_ANISOTROPIC_FRICTION);
  1135. applyAnisotropicFriction(colObj1, cp.m_lateralFrictionDir2, btCollisionObject::CF_ANISOTROPIC_FRICTION);
  1136. addMultiBodyFrictionConstraint(cp.m_lateralFrictionDir2, manifold, frictionIndex, cp, colObj0, colObj1, relaxation, infoGlobal);
  1137. }
  1138. if ((infoGlobal.m_solverMode & SOLVER_USE_2_FRICTION_DIRECTIONS) && (infoGlobal.m_solverMode & SOLVER_DISABLE_VELOCITY_DEPENDENT_FRICTION_DIRECTION))
  1139. {
  1140. cp.m_contactPointFlags |= BT_CONTACT_FLAG_LATERAL_FRICTION_INITIALIZED;
  1141. }
  1142. }
  1143. }
  1144. else
  1145. {
  1146. addMultiBodyFrictionConstraint(cp.m_lateralFrictionDir1, manifold, frictionIndex, cp, colObj0, colObj1, relaxation, infoGlobal, cp.m_contactMotion1, cp.m_frictionCFM);
  1147. if ((infoGlobal.m_solverMode & SOLVER_USE_2_FRICTION_DIRECTIONS))
  1148. addMultiBodyFrictionConstraint(cp.m_lateralFrictionDir2, manifold, frictionIndex, cp, colObj0, colObj1, relaxation, infoGlobal, cp.m_contactMotion2, cp.m_frictionCFM);
  1149. //setMultiBodyFrictionConstraintImpulse( solverConstraint, solverBodyIdA, solverBodyIdB, cp, infoGlobal);
  1150. //todo:
  1151. solverConstraint.m_appliedImpulse = 0.f;
  1152. solverConstraint.m_appliedPushImpulse = 0.f;
  1153. }
  1154. #endif //ENABLE_FRICTION
  1155. }
  1156. }
  1157. }
  1158. void btMultiBodyConstraintSolver::convertContacts(btPersistentManifold** manifoldPtr, int numManifolds, const btContactSolverInfo& infoGlobal)
  1159. {
  1160. //btPersistentManifold* manifold = 0;
  1161. for (int i = 0; i < numManifolds; i++)
  1162. {
  1163. btPersistentManifold* manifold = manifoldPtr[i];
  1164. const btMultiBodyLinkCollider* fcA = btMultiBodyLinkCollider::upcast(manifold->getBody0());
  1165. const btMultiBodyLinkCollider* fcB = btMultiBodyLinkCollider::upcast(manifold->getBody1());
  1166. if (!fcA && !fcB)
  1167. {
  1168. //the contact doesn't involve any Featherstone btMultiBody, so deal with the regular btRigidBody/btCollisionObject case
  1169. convertContact(manifold, infoGlobal);
  1170. }
  1171. else
  1172. {
  1173. convertMultiBodyContact(manifold, infoGlobal);
  1174. }
  1175. }
  1176. //also convert the multibody constraints, if any
  1177. for (int i = 0; i < m_tmpNumMultiBodyConstraints; i++)
  1178. {
  1179. btMultiBodyConstraint* c = m_tmpMultiBodyConstraints[i];
  1180. m_data.m_solverBodyPool = &m_tmpSolverBodyPool;
  1181. m_data.m_fixedBodyId = m_fixedBodyId;
  1182. c->createConstraintRows(m_multiBodyNonContactConstraints, m_data, infoGlobal);
  1183. }
  1184. }
  1185. btScalar btMultiBodyConstraintSolver::solveGroup(btCollisionObject** bodies, int numBodies, btPersistentManifold** manifold, int numManifolds, btTypedConstraint** constraints, int numConstraints, const btContactSolverInfo& info, btIDebugDraw* debugDrawer, btDispatcher* dispatcher)
  1186. {
  1187. //printf("btMultiBodyConstraintSolver::solveGroup: numBodies=%d, numConstraints=%d\n", numBodies, numConstraints);
  1188. return btSequentialImpulseConstraintSolver::solveGroup(bodies, numBodies, manifold, numManifolds, constraints, numConstraints, info, debugDrawer, dispatcher);
  1189. }
  1190. #if 0
  1191. static void applyJointFeedback(btMultiBodyJacobianData& data, const btMultiBodySolverConstraint& solverConstraint, int jacIndex, btMultiBody* mb, btScalar appliedImpulse)
  1192. {
  1193. if (appliedImpulse!=0 && mb->internalNeedsJointFeedback())
  1194. {
  1195. //todo: get rid of those temporary memory allocations for the joint feedback
  1196. btAlignedObjectArray<btScalar> forceVector;
  1197. int numDofsPlusBase = 6+mb->getNumDofs();
  1198. forceVector.resize(numDofsPlusBase);
  1199. for (int i=0;i<numDofsPlusBase;i++)
  1200. {
  1201. forceVector[i] = data.m_jacobians[jacIndex+i]*appliedImpulse;
  1202. }
  1203. btAlignedObjectArray<btScalar> output;
  1204. output.resize(numDofsPlusBase);
  1205. bool applyJointFeedback = true;
  1206. mb->calcAccelerationDeltasMultiDof(&forceVector[0],&output[0],data.scratch_r,data.scratch_v,applyJointFeedback);
  1207. }
  1208. }
  1209. #endif
  1210. void btMultiBodyConstraintSolver::writeBackSolverBodyToMultiBody(btMultiBodySolverConstraint& c, btScalar deltaTime)
  1211. {
  1212. #if 1
  1213. //bod->addBaseForce(m_gravity * bod->getBaseMass());
  1214. //bod->addLinkForce(j, m_gravity * bod->getLinkMass(j));
  1215. if (c.m_orgConstraint)
  1216. {
  1217. c.m_orgConstraint->internalSetAppliedImpulse(c.m_orgDofIndex, c.m_appliedImpulse);
  1218. }
  1219. if (c.m_multiBodyA)
  1220. {
  1221. c.m_multiBodyA->setCompanionId(-1);
  1222. btVector3 force = c.m_contactNormal1 * (c.m_appliedImpulse / deltaTime);
  1223. btVector3 torque = c.m_relpos1CrossNormal * (c.m_appliedImpulse / deltaTime);
  1224. if (c.m_linkA < 0)
  1225. {
  1226. c.m_multiBodyA->addBaseConstraintForce(force);
  1227. c.m_multiBodyA->addBaseConstraintTorque(torque);
  1228. }
  1229. else
  1230. {
  1231. c.m_multiBodyA->addLinkConstraintForce(c.m_linkA, force);
  1232. //b3Printf("force = %f,%f,%f\n",force[0],force[1],force[2]);//[0],torque[1],torque[2]);
  1233. c.m_multiBodyA->addLinkConstraintTorque(c.m_linkA, torque);
  1234. }
  1235. }
  1236. if (c.m_multiBodyB)
  1237. {
  1238. {
  1239. c.m_multiBodyB->setCompanionId(-1);
  1240. btVector3 force = c.m_contactNormal2 * (c.m_appliedImpulse / deltaTime);
  1241. btVector3 torque = c.m_relpos2CrossNormal * (c.m_appliedImpulse / deltaTime);
  1242. if (c.m_linkB < 0)
  1243. {
  1244. c.m_multiBodyB->addBaseConstraintForce(force);
  1245. c.m_multiBodyB->addBaseConstraintTorque(torque);
  1246. }
  1247. else
  1248. {
  1249. {
  1250. c.m_multiBodyB->addLinkConstraintForce(c.m_linkB, force);
  1251. //b3Printf("t = %f,%f,%f\n",force[0],force[1],force[2]);//[0],torque[1],torque[2]);
  1252. c.m_multiBodyB->addLinkConstraintTorque(c.m_linkB, torque);
  1253. }
  1254. }
  1255. }
  1256. }
  1257. #endif
  1258. #ifndef DIRECTLY_UPDATE_VELOCITY_DURING_SOLVER_ITERATIONS
  1259. if (c.m_multiBodyA)
  1260. {
  1261. c.m_multiBodyA->applyDeltaVeeMultiDof(&m_data.m_deltaVelocitiesUnitImpulse[c.m_jacAindex], c.m_appliedImpulse);
  1262. }
  1263. if (c.m_multiBodyB)
  1264. {
  1265. c.m_multiBodyB->applyDeltaVeeMultiDof(&m_data.m_deltaVelocitiesUnitImpulse[c.m_jacBindex], c.m_appliedImpulse);
  1266. }
  1267. #endif
  1268. }
  1269. btScalar btMultiBodyConstraintSolver::solveGroupCacheFriendlyFinish(btCollisionObject** bodies, int numBodies, const btContactSolverInfo& infoGlobal)
  1270. {
  1271. BT_PROFILE("btMultiBodyConstraintSolver::solveGroupCacheFriendlyFinish");
  1272. int numPoolConstraints = m_multiBodyNormalContactConstraints.size();
  1273. //write back the delta v to the multi bodies, either as applied impulse (direct velocity change)
  1274. //or as applied force, so we can measure the joint reaction forces easier
  1275. for (int i = 0; i < numPoolConstraints; i++)
  1276. {
  1277. btMultiBodySolverConstraint& solverConstraint = m_multiBodyNormalContactConstraints[i];
  1278. writeBackSolverBodyToMultiBody(solverConstraint, infoGlobal.m_timeStep);
  1279. writeBackSolverBodyToMultiBody(m_multiBodyFrictionContactConstraints[solverConstraint.m_frictionIndex], infoGlobal.m_timeStep);
  1280. if ((infoGlobal.m_solverMode & SOLVER_USE_2_FRICTION_DIRECTIONS))
  1281. {
  1282. writeBackSolverBodyToMultiBody(m_multiBodyFrictionContactConstraints[solverConstraint.m_frictionIndex + 1], infoGlobal.m_timeStep);
  1283. }
  1284. }
  1285. for (int i = 0; i < m_multiBodyNonContactConstraints.size(); i++)
  1286. {
  1287. btMultiBodySolverConstraint& solverConstraint = m_multiBodyNonContactConstraints[i];
  1288. writeBackSolverBodyToMultiBody(solverConstraint, infoGlobal.m_timeStep);
  1289. }
  1290. if (infoGlobal.m_solverMode & SOLVER_USE_WARMSTARTING)
  1291. {
  1292. BT_PROFILE("warm starting write back");
  1293. for (int j = 0; j < numPoolConstraints; j++)
  1294. {
  1295. const btMultiBodySolverConstraint& solverConstraint = m_multiBodyNormalContactConstraints[j];
  1296. btManifoldPoint* pt = (btManifoldPoint*)solverConstraint.m_originalContactPoint;
  1297. btAssert(pt);
  1298. pt->m_appliedImpulse = solverConstraint.m_appliedImpulse;
  1299. pt->m_appliedImpulseLateral1 = m_multiBodyFrictionContactConstraints[solverConstraint.m_frictionIndex].m_appliedImpulse;
  1300. //printf("pt->m_appliedImpulseLateral1 = %f\n", pt->m_appliedImpulseLateral1);
  1301. if ((infoGlobal.m_solverMode & SOLVER_USE_2_FRICTION_DIRECTIONS))
  1302. {
  1303. pt->m_appliedImpulseLateral2 = m_multiBodyFrictionContactConstraints[solverConstraint.m_frictionIndex + 1].m_appliedImpulse;
  1304. } else
  1305. {
  1306. pt->m_appliedImpulseLateral2 = 0;
  1307. }
  1308. }
  1309. //do a callback here?
  1310. }
  1311. #if 0
  1312. //multibody joint feedback
  1313. {
  1314. BT_PROFILE("multi body joint feedback");
  1315. for (int j=0;j<numPoolConstraints;j++)
  1316. {
  1317. const btMultiBodySolverConstraint& solverConstraint = m_multiBodyNormalContactConstraints[j];
  1318. //apply the joint feedback into all links of the btMultiBody
  1319. //todo: double-check the signs of the applied impulse
  1320. if(solverConstraint.m_multiBodyA && solverConstraint.m_multiBodyA->isMultiDof())
  1321. {
  1322. applyJointFeedback(m_data,solverConstraint, solverConstraint.m_jacAindex,solverConstraint.m_multiBodyA, solverConstraint.m_appliedImpulse*btSimdScalar(1./infoGlobal.m_timeStep));
  1323. }
  1324. if(solverConstraint.m_multiBodyB && solverConstraint.m_multiBodyB->isMultiDof())
  1325. {
  1326. applyJointFeedback(m_data,solverConstraint, solverConstraint.m_jacBindex,solverConstraint.m_multiBodyB,solverConstraint.m_appliedImpulse*btSimdScalar(-1./infoGlobal.m_timeStep));
  1327. }
  1328. #if 0
  1329. if (m_multiBodyFrictionContactConstraints[solverConstraint.m_frictionIndex].m_multiBodyA && m_multiBodyFrictionContactConstraints[solverConstraint.m_frictionIndex].m_multiBodyA->isMultiDof())
  1330. {
  1331. applyJointFeedback(m_data,m_multiBodyFrictionContactConstraints[solverConstraint.m_frictionIndex],
  1332. m_multiBodyFrictionContactConstraints[solverConstraint.m_frictionIndex].m_jacAindex,
  1333. m_multiBodyFrictionContactConstraints[solverConstraint.m_frictionIndex].m_multiBodyA,
  1334. m_multiBodyFrictionContactConstraints[solverConstraint.m_frictionIndex].m_appliedImpulse*btSimdScalar(1./infoGlobal.m_timeStep));
  1335. }
  1336. if (m_multiBodyFrictionContactConstraints[solverConstraint.m_frictionIndex].m_multiBodyB && m_multiBodyFrictionContactConstraints[solverConstraint.m_frictionIndex].m_multiBodyB->isMultiDof())
  1337. {
  1338. applyJointFeedback(m_data,m_multiBodyFrictionContactConstraints[solverConstraint.m_frictionIndex],
  1339. m_multiBodyFrictionContactConstraints[solverConstraint.m_frictionIndex].m_jacBindex,
  1340. m_multiBodyFrictionContactConstraints[solverConstraint.m_frictionIndex].m_multiBodyB,
  1341. m_multiBodyFrictionContactConstraints[solverConstraint.m_frictionIndex].m_appliedImpulse*btSimdScalar(-1./infoGlobal.m_timeStep));
  1342. }
  1343. if ((infoGlobal.m_solverMode & SOLVER_USE_2_FRICTION_DIRECTIONS))
  1344. {
  1345. if (m_multiBodyFrictionContactConstraints[solverConstraint.m_frictionIndex+1].m_multiBodyA && m_multiBodyFrictionContactConstraints[solverConstraint.m_frictionIndex+1].m_multiBodyA->isMultiDof())
  1346. {
  1347. applyJointFeedback(m_data,m_multiBodyFrictionContactConstraints[solverConstraint.m_frictionIndex+1],
  1348. m_multiBodyFrictionContactConstraints[solverConstraint.m_frictionIndex+1].m_jacAindex,
  1349. m_multiBodyFrictionContactConstraints[solverConstraint.m_frictionIndex+1].m_multiBodyA,
  1350. m_multiBodyFrictionContactConstraints[solverConstraint.m_frictionIndex+1].m_appliedImpulse*btSimdScalar(1./infoGlobal.m_timeStep));
  1351. }
  1352. if (m_multiBodyFrictionContactConstraints[solverConstraint.m_frictionIndex+1].m_multiBodyB && m_multiBodyFrictionContactConstraints[solverConstraint.m_frictionIndex+1].m_multiBodyB->isMultiDof())
  1353. {
  1354. applyJointFeedback(m_data,m_multiBodyFrictionContactConstraints[solverConstraint.m_frictionIndex+1],
  1355. m_multiBodyFrictionContactConstraints[solverConstraint.m_frictionIndex+1].m_jacBindex,
  1356. m_multiBodyFrictionContactConstraints[solverConstraint.m_frictionIndex+1].m_multiBodyB,
  1357. m_multiBodyFrictionContactConstraints[solverConstraint.m_frictionIndex+1].m_appliedImpulse*btSimdScalar(-1./infoGlobal.m_timeStep));
  1358. }
  1359. }
  1360. #endif
  1361. }
  1362. for (int i=0;i<m_multiBodyNonContactConstraints.size();i++)
  1363. {
  1364. const btMultiBodySolverConstraint& solverConstraint = m_multiBodyNonContactConstraints[i];
  1365. if(solverConstraint.m_multiBodyA && solverConstraint.m_multiBodyA->isMultiDof())
  1366. {
  1367. applyJointFeedback(m_data,solverConstraint, solverConstraint.m_jacAindex,solverConstraint.m_multiBodyA, solverConstraint.m_appliedImpulse*btSimdScalar(1./infoGlobal.m_timeStep));
  1368. }
  1369. if(solverConstraint.m_multiBodyB && solverConstraint.m_multiBodyB->isMultiDof())
  1370. {
  1371. applyJointFeedback(m_data,solverConstraint, solverConstraint.m_jacBindex,solverConstraint.m_multiBodyB,solverConstraint.m_appliedImpulse*btSimdScalar(1./infoGlobal.m_timeStep));
  1372. }
  1373. }
  1374. }
  1375. numPoolConstraints = m_multiBodyNonContactConstraints.size();
  1376. #if 0
  1377. //@todo: m_originalContactPoint is not initialized for btMultiBodySolverConstraint
  1378. for (int i=0;i<numPoolConstraints;i++)
  1379. {
  1380. const btMultiBodySolverConstraint& c = m_multiBodyNonContactConstraints[i];
  1381. btTypedConstraint* constr = (btTypedConstraint*)c.m_originalContactPoint;
  1382. btJointFeedback* fb = constr->getJointFeedback();
  1383. if (fb)
  1384. {
  1385. fb->m_appliedForceBodyA += c.m_contactNormal1*c.m_appliedImpulse*constr->getRigidBodyA().getLinearFactor()/infoGlobal.m_timeStep;
  1386. fb->m_appliedForceBodyB += c.m_contactNormal2*c.m_appliedImpulse*constr->getRigidBodyB().getLinearFactor()/infoGlobal.m_timeStep;
  1387. fb->m_appliedTorqueBodyA += c.m_relpos1CrossNormal* constr->getRigidBodyA().getAngularFactor()*c.m_appliedImpulse/infoGlobal.m_timeStep;
  1388. fb->m_appliedTorqueBodyB += c.m_relpos2CrossNormal* constr->getRigidBodyB().getAngularFactor()*c.m_appliedImpulse/infoGlobal.m_timeStep; /*RGM ???? */
  1389. }
  1390. constr->internalSetAppliedImpulse(c.m_appliedImpulse);
  1391. if (btFabs(c.m_appliedImpulse)>=constr->getBreakingImpulseThreshold())
  1392. {
  1393. constr->setEnabled(false);
  1394. }
  1395. }
  1396. #endif
  1397. #endif
  1398. return btSequentialImpulseConstraintSolver::solveGroupCacheFriendlyFinish(bodies, numBodies, infoGlobal);
  1399. }
  1400. void btMultiBodyConstraintSolver::solveMultiBodyGroup(btCollisionObject** bodies, int numBodies, btPersistentManifold** manifold, int numManifolds, btTypedConstraint** constraints, int numConstraints, btMultiBodyConstraint** multiBodyConstraints, int numMultiBodyConstraints, const btContactSolverInfo& info, btIDebugDraw* debugDrawer, btDispatcher* dispatcher)
  1401. {
  1402. //printf("solveMultiBodyGroup: numBodies=%d, numConstraints=%d, numManifolds=%d, numMultiBodyConstraints=%d\n", numBodies, numConstraints, numManifolds, numMultiBodyConstraints);
  1403. //printf("solveMultiBodyGroup start\n");
  1404. m_tmpMultiBodyConstraints = multiBodyConstraints;
  1405. m_tmpNumMultiBodyConstraints = numMultiBodyConstraints;
  1406. btSequentialImpulseConstraintSolver::solveGroup(bodies, numBodies, manifold, numManifolds, constraints, numConstraints, info, debugDrawer, dispatcher);
  1407. m_tmpMultiBodyConstraints = 0;
  1408. m_tmpNumMultiBodyConstraints = 0;
  1409. }