btDeformableContactConstraint.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. /*
  2. Written by Xuchen Han <xuchenhan2015@u.northwestern.edu>
  3. Bullet Continuous Collision Detection and Physics Library
  4. Copyright (c) 2019 Google Inc. http://bulletphysics.org
  5. This software is provided 'as-is', without any express or implied warranty.
  6. In no event will the authors be held liable for any damages arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it freely,
  9. subject to the following restrictions:
  10. 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.
  11. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  12. 3. This notice may not be removed or altered from any source distribution.
  13. */
  14. #include "btDeformableContactConstraint.h"
  15. /* ================ Deformable Node Anchor =================== */
  16. btDeformableNodeAnchorConstraint::btDeformableNodeAnchorConstraint(const btSoftBody::DeformableNodeRigidAnchor& a, const btContactSolverInfo& infoGlobal)
  17. : m_anchor(&a), btDeformableContactConstraint(a.m_cti.m_normal, infoGlobal)
  18. {
  19. }
  20. btDeformableNodeAnchorConstraint::btDeformableNodeAnchorConstraint(const btDeformableNodeAnchorConstraint& other)
  21. : m_anchor(other.m_anchor), btDeformableContactConstraint(other)
  22. {
  23. }
  24. btVector3 btDeformableNodeAnchorConstraint::getVa() const
  25. {
  26. const btSoftBody::sCti& cti = m_anchor->m_cti;
  27. btVector3 va(0, 0, 0);
  28. if (cti.m_colObj->hasContactResponse())
  29. {
  30. btRigidBody* rigidCol = 0;
  31. btMultiBodyLinkCollider* multibodyLinkCol = 0;
  32. // grab the velocity of the rigid body
  33. if (cti.m_colObj->getInternalType() == btCollisionObject::CO_RIGID_BODY)
  34. {
  35. rigidCol = (btRigidBody*)btRigidBody::upcast(cti.m_colObj);
  36. va = rigidCol ? (rigidCol->getVelocityInLocalPoint(m_anchor->m_c1)) : btVector3(0, 0, 0);
  37. }
  38. else if (cti.m_colObj->getInternalType() == btCollisionObject::CO_FEATHERSTONE_LINK)
  39. {
  40. multibodyLinkCol = (btMultiBodyLinkCollider*)btMultiBodyLinkCollider::upcast(cti.m_colObj);
  41. if (multibodyLinkCol)
  42. {
  43. const int ndof = multibodyLinkCol->m_multiBody->getNumDofs() + 6;
  44. const btScalar* J_n = &m_anchor->jacobianData_normal.m_jacobians[0];
  45. const btScalar* J_t1 = &m_anchor->jacobianData_t1.m_jacobians[0];
  46. const btScalar* J_t2 = &m_anchor->jacobianData_t2.m_jacobians[0];
  47. const btScalar* local_v = multibodyLinkCol->m_multiBody->getVelocityVector();
  48. const btScalar* local_dv = multibodyLinkCol->m_multiBody->getDeltaVelocityVector();
  49. // add in the normal component of the va
  50. btScalar vel = 0.0;
  51. for (int k = 0; k < ndof; ++k)
  52. {
  53. vel += (local_v[k] + local_dv[k]) * J_n[k];
  54. }
  55. va = cti.m_normal * vel;
  56. // add in the tangential components of the va
  57. vel = 0.0;
  58. for (int k = 0; k < ndof; ++k)
  59. {
  60. vel += (local_v[k] + local_dv[k]) * J_t1[k];
  61. }
  62. va += m_anchor->t1 * vel;
  63. vel = 0.0;
  64. for (int k = 0; k < ndof; ++k)
  65. {
  66. vel += (local_v[k] + local_dv[k]) * J_t2[k];
  67. }
  68. va += m_anchor->t2 * vel;
  69. }
  70. }
  71. }
  72. return va;
  73. }
  74. btScalar btDeformableNodeAnchorConstraint::solveConstraint(const btContactSolverInfo& infoGlobal)
  75. {
  76. const btSoftBody::sCti& cti = m_anchor->m_cti;
  77. btVector3 va = getVa();
  78. btVector3 vb = getVb();
  79. btVector3 vr = (vb - va);
  80. // + (m_anchor->m_node->m_x - cti.m_colObj->getWorldTransform() * m_anchor->m_local) * 10.0
  81. const btScalar dn = btDot(vr, vr);
  82. // dn is the normal component of velocity diffrerence. Approximates the residual. // todo xuchenhan@: this prob needs to be scaled by dt
  83. btScalar residualSquare = dn * dn;
  84. btVector3 impulse = m_anchor->m_c0 * vr;
  85. // apply impulse to deformable nodes involved and change their velocities
  86. applyImpulse(impulse);
  87. // apply impulse to the rigid/multibodies involved and change their velocities
  88. if (cti.m_colObj->getInternalType() == btCollisionObject::CO_RIGID_BODY)
  89. {
  90. btRigidBody* rigidCol = 0;
  91. rigidCol = (btRigidBody*)btRigidBody::upcast(cti.m_colObj);
  92. if (rigidCol)
  93. {
  94. rigidCol->applyImpulse(impulse, m_anchor->m_c1);
  95. }
  96. }
  97. else if (cti.m_colObj->getInternalType() == btCollisionObject::CO_FEATHERSTONE_LINK)
  98. {
  99. btMultiBodyLinkCollider* multibodyLinkCol = 0;
  100. multibodyLinkCol = (btMultiBodyLinkCollider*)btMultiBodyLinkCollider::upcast(cti.m_colObj);
  101. if (multibodyLinkCol)
  102. {
  103. const btScalar* deltaV_normal = &m_anchor->jacobianData_normal.m_deltaVelocitiesUnitImpulse[0];
  104. // apply normal component of the impulse
  105. multibodyLinkCol->m_multiBody->applyDeltaVeeMultiDof2(deltaV_normal, impulse.dot(cti.m_normal));
  106. // apply tangential component of the impulse
  107. const btScalar* deltaV_t1 = &m_anchor->jacobianData_t1.m_deltaVelocitiesUnitImpulse[0];
  108. multibodyLinkCol->m_multiBody->applyDeltaVeeMultiDof2(deltaV_t1, impulse.dot(m_anchor->t1));
  109. const btScalar* deltaV_t2 = &m_anchor->jacobianData_t2.m_deltaVelocitiesUnitImpulse[0];
  110. multibodyLinkCol->m_multiBody->applyDeltaVeeMultiDof2(deltaV_t2, impulse.dot(m_anchor->t2));
  111. }
  112. }
  113. return residualSquare;
  114. }
  115. btVector3 btDeformableNodeAnchorConstraint::getVb() const
  116. {
  117. return m_anchor->m_node->m_v;
  118. }
  119. void btDeformableNodeAnchorConstraint::applyImpulse(const btVector3& impulse)
  120. {
  121. btVector3 dv = impulse * m_anchor->m_c2;
  122. m_anchor->m_node->m_v -= dv;
  123. }
  124. /* ================ Deformable vs. Rigid =================== */
  125. btDeformableRigidContactConstraint::btDeformableRigidContactConstraint(const btSoftBody::DeformableRigidContact& c, const btContactSolverInfo& infoGlobal)
  126. : m_contact(&c), btDeformableContactConstraint(c.m_cti.m_normal, infoGlobal)
  127. {
  128. m_total_normal_dv.setZero();
  129. m_total_tangent_dv.setZero();
  130. // The magnitude of penetration is the depth of penetration.
  131. m_penetration = c.m_cti.m_offset;
  132. m_total_split_impulse = 0;
  133. m_binding = false;
  134. }
  135. btDeformableRigidContactConstraint::btDeformableRigidContactConstraint(const btDeformableRigidContactConstraint& other)
  136. : m_contact(other.m_contact), btDeformableContactConstraint(other), m_penetration(other.m_penetration), m_total_split_impulse(other.m_total_split_impulse), m_binding(other.m_binding)
  137. {
  138. m_total_normal_dv = other.m_total_normal_dv;
  139. m_total_tangent_dv = other.m_total_tangent_dv;
  140. }
  141. btVector3 btDeformableRigidContactConstraint::getVa() const
  142. {
  143. const btSoftBody::sCti& cti = m_contact->m_cti;
  144. btVector3 va(0, 0, 0);
  145. if (cti.m_colObj->hasContactResponse())
  146. {
  147. btRigidBody* rigidCol = 0;
  148. btMultiBodyLinkCollider* multibodyLinkCol = 0;
  149. // grab the velocity of the rigid body
  150. if (cti.m_colObj->getInternalType() == btCollisionObject::CO_RIGID_BODY)
  151. {
  152. rigidCol = (btRigidBody*)btRigidBody::upcast(cti.m_colObj);
  153. va = rigidCol ? (rigidCol->getVelocityInLocalPoint(m_contact->m_c1)) : btVector3(0, 0, 0);
  154. }
  155. else if (cti.m_colObj->getInternalType() == btCollisionObject::CO_FEATHERSTONE_LINK)
  156. {
  157. multibodyLinkCol = (btMultiBodyLinkCollider*)btMultiBodyLinkCollider::upcast(cti.m_colObj);
  158. if (multibodyLinkCol)
  159. {
  160. const int ndof = multibodyLinkCol->m_multiBody->getNumDofs() + 6;
  161. const btScalar* J_n = &m_contact->jacobianData_normal.m_jacobians[0];
  162. const btScalar* J_t1 = &m_contact->jacobianData_t1.m_jacobians[0];
  163. const btScalar* J_t2 = &m_contact->jacobianData_t2.m_jacobians[0];
  164. const btScalar* local_v = multibodyLinkCol->m_multiBody->getVelocityVector();
  165. const btScalar* local_dv = multibodyLinkCol->m_multiBody->getDeltaVelocityVector();
  166. // add in the normal component of the va
  167. btScalar vel = 0.0;
  168. for (int k = 0; k < ndof; ++k)
  169. {
  170. vel += (local_v[k] + local_dv[k]) * J_n[k];
  171. }
  172. va = cti.m_normal * vel;
  173. // add in the tangential components of the va
  174. vel = 0.0;
  175. for (int k = 0; k < ndof; ++k)
  176. {
  177. vel += (local_v[k] + local_dv[k]) * J_t1[k];
  178. }
  179. va += m_contact->t1 * vel;
  180. vel = 0.0;
  181. for (int k = 0; k < ndof; ++k)
  182. {
  183. vel += (local_v[k] + local_dv[k]) * J_t2[k];
  184. }
  185. va += m_contact->t2 * vel;
  186. }
  187. }
  188. }
  189. return va;
  190. }
  191. btVector3 btDeformableRigidContactConstraint::getSplitVa() const
  192. {
  193. const btSoftBody::sCti& cti = m_contact->m_cti;
  194. btVector3 va(0, 0, 0);
  195. if (cti.m_colObj->hasContactResponse())
  196. {
  197. btRigidBody* rigidCol = 0;
  198. btMultiBodyLinkCollider* multibodyLinkCol = 0;
  199. // grab the velocity of the rigid body
  200. if (cti.m_colObj->getInternalType() == btCollisionObject::CO_RIGID_BODY)
  201. {
  202. rigidCol = (btRigidBody*)btRigidBody::upcast(cti.m_colObj);
  203. va = rigidCol ? (rigidCol->getPushVelocityInLocalPoint(m_contact->m_c1)) : btVector3(0, 0, 0);
  204. }
  205. else if (cti.m_colObj->getInternalType() == btCollisionObject::CO_FEATHERSTONE_LINK)
  206. {
  207. multibodyLinkCol = (btMultiBodyLinkCollider*)btMultiBodyLinkCollider::upcast(cti.m_colObj);
  208. if (multibodyLinkCol)
  209. {
  210. const int ndof = multibodyLinkCol->m_multiBody->getNumDofs() + 6;
  211. const btScalar* J_n = &m_contact->jacobianData_normal.m_jacobians[0];
  212. const btScalar* J_t1 = &m_contact->jacobianData_t1.m_jacobians[0];
  213. const btScalar* J_t2 = &m_contact->jacobianData_t2.m_jacobians[0];
  214. const btScalar* local_split_v = multibodyLinkCol->m_multiBody->getSplitVelocityVector();
  215. // add in the normal component of the va
  216. btScalar vel = 0.0;
  217. for (int k = 0; k < ndof; ++k)
  218. {
  219. vel += local_split_v[k] * J_n[k];
  220. }
  221. va = cti.m_normal * vel;
  222. // add in the tangential components of the va
  223. vel = 0.0;
  224. for (int k = 0; k < ndof; ++k)
  225. {
  226. vel += local_split_v[k] * J_t1[k];
  227. }
  228. va += m_contact->t1 * vel;
  229. vel = 0.0;
  230. for (int k = 0; k < ndof; ++k)
  231. {
  232. vel += local_split_v[k] * J_t2[k];
  233. }
  234. va += m_contact->t2 * vel;
  235. }
  236. }
  237. }
  238. return va;
  239. }
  240. btScalar btDeformableRigidContactConstraint::solveConstraint(const btContactSolverInfo& infoGlobal)
  241. {
  242. const btSoftBody::sCti& cti = m_contact->m_cti;
  243. btVector3 va = getVa();
  244. btVector3 vb = getVb();
  245. btVector3 vr = vb - va;
  246. btScalar dn = btDot(vr, cti.m_normal) + m_total_normal_dv.dot(cti.m_normal) * infoGlobal.m_deformable_cfm;
  247. if (m_penetration > 0)
  248. {
  249. dn += m_penetration / infoGlobal.m_timeStep;
  250. }
  251. if (!infoGlobal.m_splitImpulse)
  252. {
  253. dn += m_penetration * infoGlobal.m_deformable_erp / infoGlobal.m_timeStep;
  254. }
  255. // dn is the normal component of velocity diffrerence. Approximates the residual. // todo xuchenhan@: this prob needs to be scaled by dt
  256. btVector3 impulse = m_contact->m_c0 * (vr + m_total_normal_dv * infoGlobal.m_deformable_cfm + ((m_penetration > 0) ? m_penetration / infoGlobal.m_timeStep * cti.m_normal : btVector3(0, 0, 0)));
  257. if (!infoGlobal.m_splitImpulse)
  258. {
  259. impulse += m_contact->m_c0 * (m_penetration * infoGlobal.m_deformable_erp / infoGlobal.m_timeStep * cti.m_normal);
  260. }
  261. btVector3 impulse_normal = m_contact->m_c0 * (cti.m_normal * dn);
  262. btVector3 impulse_tangent = impulse - impulse_normal;
  263. if (dn > 0)
  264. {
  265. return 0;
  266. }
  267. m_binding = true;
  268. btScalar residualSquare = dn * dn;
  269. btVector3 old_total_tangent_dv = m_total_tangent_dv;
  270. // m_c5 is the inverse mass of the deformable node/face
  271. m_total_normal_dv -= m_contact->m_c5 * impulse_normal;
  272. m_total_tangent_dv -= m_contact->m_c5 * impulse_tangent;
  273. if (m_total_normal_dv.dot(cti.m_normal) < 0)
  274. {
  275. // separating in the normal direction
  276. m_binding = false;
  277. m_static = false;
  278. impulse_tangent.setZero();
  279. }
  280. else
  281. {
  282. if (m_total_normal_dv.norm() * m_contact->m_c3 < m_total_tangent_dv.norm())
  283. {
  284. // dynamic friction
  285. // with dynamic friction, the impulse are still applied to the two objects colliding, however, it does not pose a constraint in the cg solve, hence the change to dv merely serves to update velocity in the contact iterations.
  286. m_static = false;
  287. if (m_total_tangent_dv.safeNorm() < SIMD_EPSILON)
  288. {
  289. m_total_tangent_dv = btVector3(0, 0, 0);
  290. }
  291. else
  292. {
  293. m_total_tangent_dv = m_total_tangent_dv.normalized() * m_total_normal_dv.safeNorm() * m_contact->m_c3;
  294. }
  295. // impulse_tangent = -btScalar(1)/m_contact->m_c2 * (m_total_tangent_dv - old_total_tangent_dv);
  296. impulse_tangent = m_contact->m_c5.inverse() * (old_total_tangent_dv - m_total_tangent_dv);
  297. }
  298. else
  299. {
  300. // static friction
  301. m_static = true;
  302. }
  303. }
  304. impulse = impulse_normal + impulse_tangent;
  305. // apply impulse to deformable nodes involved and change their velocities
  306. applyImpulse(impulse);
  307. // apply impulse to the rigid/multibodies involved and change their velocities
  308. if (cti.m_colObj->getInternalType() == btCollisionObject::CO_RIGID_BODY)
  309. {
  310. btRigidBody* rigidCol = 0;
  311. rigidCol = (btRigidBody*)btRigidBody::upcast(cti.m_colObj);
  312. if (rigidCol)
  313. {
  314. rigidCol->applyImpulse(impulse, m_contact->m_c1);
  315. }
  316. }
  317. else if (cti.m_colObj->getInternalType() == btCollisionObject::CO_FEATHERSTONE_LINK)
  318. {
  319. btMultiBodyLinkCollider* multibodyLinkCol = 0;
  320. multibodyLinkCol = (btMultiBodyLinkCollider*)btMultiBodyLinkCollider::upcast(cti.m_colObj);
  321. if (multibodyLinkCol)
  322. {
  323. const btScalar* deltaV_normal = &m_contact->jacobianData_normal.m_deltaVelocitiesUnitImpulse[0];
  324. // apply normal component of the impulse
  325. multibodyLinkCol->m_multiBody->applyDeltaVeeMultiDof2(deltaV_normal, impulse.dot(cti.m_normal));
  326. if (impulse_tangent.norm() > SIMD_EPSILON)
  327. {
  328. // apply tangential component of the impulse
  329. const btScalar* deltaV_t1 = &m_contact->jacobianData_t1.m_deltaVelocitiesUnitImpulse[0];
  330. multibodyLinkCol->m_multiBody->applyDeltaVeeMultiDof2(deltaV_t1, impulse.dot(m_contact->t1));
  331. const btScalar* deltaV_t2 = &m_contact->jacobianData_t2.m_deltaVelocitiesUnitImpulse[0];
  332. multibodyLinkCol->m_multiBody->applyDeltaVeeMultiDof2(deltaV_t2, impulse.dot(m_contact->t2));
  333. }
  334. }
  335. }
  336. return residualSquare;
  337. }
  338. btScalar btDeformableRigidContactConstraint::solveSplitImpulse(const btContactSolverInfo& infoGlobal)
  339. {
  340. btScalar MAX_PENETRATION_CORRECTION = infoGlobal.m_deformable_maxErrorReduction;
  341. const btSoftBody::sCti& cti = m_contact->m_cti;
  342. btVector3 vb = getSplitVb();
  343. btVector3 va = getSplitVa();
  344. btScalar p = m_penetration;
  345. if (p > 0)
  346. {
  347. return 0;
  348. }
  349. btVector3 vr = vb - va;
  350. btScalar dn = btDot(vr, cti.m_normal) + p * infoGlobal.m_deformable_erp / infoGlobal.m_timeStep;
  351. if (dn > 0)
  352. {
  353. return 0;
  354. }
  355. if (m_total_split_impulse + dn > MAX_PENETRATION_CORRECTION)
  356. {
  357. dn = MAX_PENETRATION_CORRECTION - m_total_split_impulse;
  358. }
  359. if (m_total_split_impulse + dn < -MAX_PENETRATION_CORRECTION)
  360. {
  361. dn = -MAX_PENETRATION_CORRECTION - m_total_split_impulse;
  362. }
  363. m_total_split_impulse += dn;
  364. btScalar residualSquare = dn * dn;
  365. const btVector3 impulse = m_contact->m_c0 * (cti.m_normal * dn);
  366. applySplitImpulse(impulse);
  367. // apply split impulse to the rigid/multibodies involved and change their velocities
  368. if (cti.m_colObj->getInternalType() == btCollisionObject::CO_RIGID_BODY)
  369. {
  370. btRigidBody* rigidCol = 0;
  371. rigidCol = (btRigidBody*)btRigidBody::upcast(cti.m_colObj);
  372. if (rigidCol)
  373. {
  374. rigidCol->applyPushImpulse(impulse, m_contact->m_c1);
  375. }
  376. }
  377. else if (cti.m_colObj->getInternalType() == btCollisionObject::CO_FEATHERSTONE_LINK)
  378. {
  379. btMultiBodyLinkCollider* multibodyLinkCol = 0;
  380. multibodyLinkCol = (btMultiBodyLinkCollider*)btMultiBodyLinkCollider::upcast(cti.m_colObj);
  381. if (multibodyLinkCol)
  382. {
  383. const btScalar* deltaV_normal = &m_contact->jacobianData_normal.m_deltaVelocitiesUnitImpulse[0];
  384. // apply normal component of the impulse
  385. multibodyLinkCol->m_multiBody->applyDeltaSplitVeeMultiDof(deltaV_normal, impulse.dot(cti.m_normal));
  386. }
  387. }
  388. return residualSquare;
  389. }
  390. /* ================ Node vs. Rigid =================== */
  391. btDeformableNodeRigidContactConstraint::btDeformableNodeRigidContactConstraint(const btSoftBody::DeformableNodeRigidContact& contact, const btContactSolverInfo& infoGlobal)
  392. : m_node(contact.m_node), btDeformableRigidContactConstraint(contact, infoGlobal)
  393. {
  394. }
  395. btDeformableNodeRigidContactConstraint::btDeformableNodeRigidContactConstraint(const btDeformableNodeRigidContactConstraint& other)
  396. : m_node(other.m_node), btDeformableRigidContactConstraint(other)
  397. {
  398. }
  399. btVector3 btDeformableNodeRigidContactConstraint::getVb() const
  400. {
  401. return m_node->m_v;
  402. }
  403. btVector3 btDeformableNodeRigidContactConstraint::getSplitVb() const
  404. {
  405. return m_node->m_splitv;
  406. }
  407. btVector3 btDeformableNodeRigidContactConstraint::getDv(const btSoftBody::Node* node) const
  408. {
  409. return m_total_normal_dv + m_total_tangent_dv;
  410. }
  411. void btDeformableNodeRigidContactConstraint::applyImpulse(const btVector3& impulse)
  412. {
  413. const btSoftBody::DeformableNodeRigidContact* contact = getContact();
  414. btVector3 dv = contact->m_c5 * impulse;
  415. contact->m_node->m_v -= dv;
  416. }
  417. void btDeformableNodeRigidContactConstraint::applySplitImpulse(const btVector3& impulse)
  418. {
  419. const btSoftBody::DeformableNodeRigidContact* contact = getContact();
  420. btVector3 dv = contact->m_c5 * impulse;
  421. contact->m_node->m_splitv -= dv;
  422. }
  423. /* ================ Face vs. Rigid =================== */
  424. btDeformableFaceRigidContactConstraint::btDeformableFaceRigidContactConstraint(const btSoftBody::DeformableFaceRigidContact& contact, const btContactSolverInfo& infoGlobal, bool useStrainLimiting)
  425. : m_face(contact.m_face), m_useStrainLimiting(useStrainLimiting), btDeformableRigidContactConstraint(contact, infoGlobal)
  426. {
  427. }
  428. btDeformableFaceRigidContactConstraint::btDeformableFaceRigidContactConstraint(const btDeformableFaceRigidContactConstraint& other)
  429. : m_face(other.m_face), m_useStrainLimiting(other.m_useStrainLimiting), btDeformableRigidContactConstraint(other)
  430. {
  431. }
  432. btVector3 btDeformableFaceRigidContactConstraint::getVb() const
  433. {
  434. const btSoftBody::DeformableFaceRigidContact* contact = getContact();
  435. btVector3 vb = m_face->m_n[0]->m_v * contact->m_bary[0] + m_face->m_n[1]->m_v * contact->m_bary[1] + m_face->m_n[2]->m_v * contact->m_bary[2];
  436. return vb;
  437. }
  438. btVector3 btDeformableFaceRigidContactConstraint::getDv(const btSoftBody::Node* node) const
  439. {
  440. btVector3 face_dv = m_total_normal_dv + m_total_tangent_dv;
  441. const btSoftBody::DeformableFaceRigidContact* contact = getContact();
  442. if (m_face->m_n[0] == node)
  443. {
  444. return face_dv * contact->m_weights[0];
  445. }
  446. if (m_face->m_n[1] == node)
  447. {
  448. return face_dv * contact->m_weights[1];
  449. }
  450. btAssert(node == m_face->m_n[2]);
  451. return face_dv * contact->m_weights[2];
  452. }
  453. void btDeformableFaceRigidContactConstraint::applyImpulse(const btVector3& impulse)
  454. {
  455. const btSoftBody::DeformableFaceRigidContact* contact = getContact();
  456. btVector3 dv = impulse * contact->m_c2;
  457. btSoftBody::Face* face = contact->m_face;
  458. btVector3& v0 = face->m_n[0]->m_v;
  459. btVector3& v1 = face->m_n[1]->m_v;
  460. btVector3& v2 = face->m_n[2]->m_v;
  461. const btScalar& im0 = face->m_n[0]->m_im;
  462. const btScalar& im1 = face->m_n[1]->m_im;
  463. const btScalar& im2 = face->m_n[2]->m_im;
  464. if (im0 > 0)
  465. v0 -= dv * contact->m_weights[0];
  466. if (im1 > 0)
  467. v1 -= dv * contact->m_weights[1];
  468. if (im2 > 0)
  469. v2 -= dv * contact->m_weights[2];
  470. if (m_useStrainLimiting)
  471. {
  472. btScalar relaxation = 1. / btScalar(m_infoGlobal->m_numIterations);
  473. btScalar m01 = (relaxation / (im0 + im1));
  474. btScalar m02 = (relaxation / (im0 + im2));
  475. btScalar m12 = (relaxation / (im1 + im2));
  476. #ifdef USE_STRAIN_RATE_LIMITING
  477. // apply strain limiting to prevent the new velocity to change the current length of the edge by more than 1%.
  478. btScalar p = 0.01;
  479. btVector3& x0 = face->m_n[0]->m_x;
  480. btVector3& x1 = face->m_n[1]->m_x;
  481. btVector3& x2 = face->m_n[2]->m_x;
  482. const btVector3 x_diff[3] = {x1 - x0, x2 - x0, x2 - x1};
  483. const btVector3 v_diff[3] = {v1 - v0, v2 - v0, v2 - v1};
  484. btVector3 u[3];
  485. btScalar x_diff_dot_u, dn[3];
  486. btScalar dt = m_infoGlobal->m_timeStep;
  487. for (int i = 0; i < 3; ++i)
  488. {
  489. btScalar x_diff_norm = x_diff[i].safeNorm();
  490. btScalar x_diff_norm_new = (x_diff[i] + v_diff[i] * dt).safeNorm();
  491. btScalar strainRate = x_diff_norm_new / x_diff_norm;
  492. u[i] = v_diff[i];
  493. u[i].safeNormalize();
  494. if (x_diff_norm == 0 || (1 - p <= strainRate && strainRate <= 1 + p))
  495. {
  496. dn[i] = 0;
  497. continue;
  498. }
  499. x_diff_dot_u = btDot(x_diff[i], u[i]);
  500. btScalar s;
  501. if (1 - p > strainRate)
  502. {
  503. s = 1 / dt * (-x_diff_dot_u - btSqrt(x_diff_dot_u * x_diff_dot_u + (p * p - 2 * p) * x_diff_norm * x_diff_norm));
  504. }
  505. else
  506. {
  507. s = 1 / dt * (-x_diff_dot_u + btSqrt(x_diff_dot_u * x_diff_dot_u + (p * p + 2 * p) * x_diff_norm * x_diff_norm));
  508. }
  509. // x_diff_norm_new = (x_diff[i] + s * u[i] * dt).safeNorm();
  510. // strainRate = x_diff_norm_new/x_diff_norm;
  511. dn[i] = s - v_diff[i].safeNorm();
  512. }
  513. btVector3 dv0 = im0 * (m01 * u[0] * (-dn[0]) + m02 * u[1] * -(dn[1]));
  514. btVector3 dv1 = im1 * (m01 * u[0] * (dn[0]) + m12 * u[2] * (-dn[2]));
  515. btVector3 dv2 = im2 * (m12 * u[2] * (dn[2]) + m02 * u[1] * (dn[1]));
  516. #else
  517. // apply strain limiting to prevent undamped modes
  518. btVector3 dv0 = im0 * (m01 * (v1 - v0) + m02 * (v2 - v0));
  519. btVector3 dv1 = im1 * (m01 * (v0 - v1) + m12 * (v2 - v1));
  520. btVector3 dv2 = im2 * (m12 * (v1 - v2) + m02 * (v0 - v2));
  521. #endif
  522. v0 += dv0;
  523. v1 += dv1;
  524. v2 += dv2;
  525. }
  526. }
  527. btVector3 btDeformableFaceRigidContactConstraint::getSplitVb() const
  528. {
  529. const btSoftBody::DeformableFaceRigidContact* contact = getContact();
  530. btVector3 vb = (m_face->m_n[0]->m_splitv) * contact->m_bary[0] + (m_face->m_n[1]->m_splitv) * contact->m_bary[1] + (m_face->m_n[2]->m_splitv) * contact->m_bary[2];
  531. return vb;
  532. }
  533. void btDeformableFaceRigidContactConstraint::applySplitImpulse(const btVector3& impulse)
  534. {
  535. const btSoftBody::DeformableFaceRigidContact* contact = getContact();
  536. btVector3 dv = impulse * contact->m_c2;
  537. btSoftBody::Face* face = contact->m_face;
  538. btVector3& v0 = face->m_n[0]->m_splitv;
  539. btVector3& v1 = face->m_n[1]->m_splitv;
  540. btVector3& v2 = face->m_n[2]->m_splitv;
  541. const btScalar& im0 = face->m_n[0]->m_im;
  542. const btScalar& im1 = face->m_n[1]->m_im;
  543. const btScalar& im2 = face->m_n[2]->m_im;
  544. if (im0 > 0)
  545. {
  546. v0 -= dv * contact->m_weights[0];
  547. }
  548. if (im1 > 0)
  549. {
  550. v1 -= dv * contact->m_weights[1];
  551. }
  552. if (im2 > 0)
  553. {
  554. v2 -= dv * contact->m_weights[2];
  555. }
  556. }
  557. /* ================ Face vs. Node =================== */
  558. btDeformableFaceNodeContactConstraint::btDeformableFaceNodeContactConstraint(const btSoftBody::DeformableFaceNodeContact& contact, const btContactSolverInfo& infoGlobal)
  559. : m_node(contact.m_node), m_face(contact.m_face), m_contact(&contact), btDeformableContactConstraint(contact.m_normal, infoGlobal)
  560. {
  561. m_total_normal_dv.setZero();
  562. m_total_tangent_dv.setZero();
  563. }
  564. btVector3 btDeformableFaceNodeContactConstraint::getVa() const
  565. {
  566. return m_node->m_v;
  567. }
  568. btVector3 btDeformableFaceNodeContactConstraint::getVb() const
  569. {
  570. const btSoftBody::DeformableFaceNodeContact* contact = getContact();
  571. btVector3 vb = m_face->m_n[0]->m_v * contact->m_bary[0] + m_face->m_n[1]->m_v * contact->m_bary[1] + m_face->m_n[2]->m_v * contact->m_bary[2];
  572. return vb;
  573. }
  574. btVector3 btDeformableFaceNodeContactConstraint::getDv(const btSoftBody::Node* n) const
  575. {
  576. btVector3 dv = m_total_normal_dv + m_total_tangent_dv;
  577. if (n == m_node)
  578. return dv;
  579. const btSoftBody::DeformableFaceNodeContact* contact = getContact();
  580. if (m_face->m_n[0] == n)
  581. {
  582. return dv * contact->m_weights[0];
  583. }
  584. if (m_face->m_n[1] == n)
  585. {
  586. return dv * contact->m_weights[1];
  587. }
  588. btAssert(n == m_face->m_n[2]);
  589. return dv * contact->m_weights[2];
  590. }
  591. btScalar btDeformableFaceNodeContactConstraint::solveConstraint(const btContactSolverInfo& infoGlobal)
  592. {
  593. btVector3 va = getVa();
  594. btVector3 vb = getVb();
  595. btVector3 vr = vb - va;
  596. const btScalar dn = btDot(vr, m_contact->m_normal);
  597. // dn is the normal component of velocity diffrerence. Approximates the residual. // todo xuchenhan@: this prob needs to be scaled by dt
  598. btScalar residualSquare = dn * dn;
  599. btVector3 impulse = m_contact->m_c0 * vr;
  600. const btVector3 impulse_normal = m_contact->m_c0 * (m_contact->m_normal * dn);
  601. btVector3 impulse_tangent = impulse - impulse_normal;
  602. btVector3 old_total_tangent_dv = m_total_tangent_dv;
  603. // m_c2 is the inverse mass of the deformable node/face
  604. if (m_node->m_im > 0)
  605. {
  606. m_total_normal_dv -= impulse_normal * m_node->m_im;
  607. m_total_tangent_dv -= impulse_tangent * m_node->m_im;
  608. }
  609. else
  610. {
  611. m_total_normal_dv -= impulse_normal * m_contact->m_imf;
  612. m_total_tangent_dv -= impulse_tangent * m_contact->m_imf;
  613. }
  614. if (m_total_normal_dv.dot(m_contact->m_normal) > 0)
  615. {
  616. // separating in the normal direction
  617. m_static = false;
  618. m_total_tangent_dv = btVector3(0, 0, 0);
  619. impulse_tangent.setZero();
  620. }
  621. else
  622. {
  623. if (m_total_normal_dv.norm() * m_contact->m_friction < m_total_tangent_dv.norm())
  624. {
  625. // dynamic friction
  626. // with dynamic friction, the impulse are still applied to the two objects colliding, however, it does not pose a constraint in the cg solve, hence the change to dv merely serves to update velocity in the contact iterations.
  627. m_static = false;
  628. if (m_total_tangent_dv.safeNorm() < SIMD_EPSILON)
  629. {
  630. m_total_tangent_dv = btVector3(0, 0, 0);
  631. }
  632. else
  633. {
  634. m_total_tangent_dv = m_total_tangent_dv.normalized() * m_total_normal_dv.safeNorm() * m_contact->m_friction;
  635. }
  636. impulse_tangent = -btScalar(1) / m_node->m_im * (m_total_tangent_dv - old_total_tangent_dv);
  637. }
  638. else
  639. {
  640. // static friction
  641. m_static = true;
  642. }
  643. }
  644. impulse = impulse_normal + impulse_tangent;
  645. // apply impulse to deformable nodes involved and change their velocities
  646. applyImpulse(impulse);
  647. return residualSquare;
  648. }
  649. void btDeformableFaceNodeContactConstraint::applyImpulse(const btVector3& impulse)
  650. {
  651. const btSoftBody::DeformableFaceNodeContact* contact = getContact();
  652. btVector3 dva = impulse * contact->m_node->m_im;
  653. btVector3 dvb = impulse * contact->m_imf;
  654. if (contact->m_node->m_im > 0)
  655. {
  656. contact->m_node->m_v += dva;
  657. }
  658. btSoftBody::Face* face = contact->m_face;
  659. btVector3& v0 = face->m_n[0]->m_v;
  660. btVector3& v1 = face->m_n[1]->m_v;
  661. btVector3& v2 = face->m_n[2]->m_v;
  662. const btScalar& im0 = face->m_n[0]->m_im;
  663. const btScalar& im1 = face->m_n[1]->m_im;
  664. const btScalar& im2 = face->m_n[2]->m_im;
  665. if (im0 > 0)
  666. {
  667. v0 -= dvb * contact->m_weights[0];
  668. }
  669. if (im1 > 0)
  670. {
  671. v1 -= dvb * contact->m_weights[1];
  672. }
  673. if (im2 > 0)
  674. {
  675. v2 -= dvb * contact->m_weights[2];
  676. }
  677. }