btDeformableContactConstraint.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. #ifndef BT_DEFORMABLE_CONTACT_CONSTRAINT_H
  15. #define BT_DEFORMABLE_CONTACT_CONSTRAINT_H
  16. #include "btSoftBody.h"
  17. // btDeformableContactConstraint is an abstract class specifying the method that each type of contact constraint needs to implement
  18. class btDeformableContactConstraint
  19. {
  20. public:
  21. // True if the friction is static
  22. // False if the friction is dynamic
  23. bool m_static;
  24. const btContactSolverInfo* m_infoGlobal;
  25. // normal of the contact
  26. btVector3 m_normal;
  27. btDeformableContactConstraint(const btVector3& normal, const btContactSolverInfo& infoGlobal) : m_static(false), m_normal(normal), m_infoGlobal(&infoGlobal)
  28. {
  29. }
  30. btDeformableContactConstraint(bool isStatic, const btVector3& normal, const btContactSolverInfo& infoGlobal) : m_static(isStatic), m_normal(normal), m_infoGlobal(&infoGlobal)
  31. {
  32. }
  33. btDeformableContactConstraint() {}
  34. btDeformableContactConstraint(const btDeformableContactConstraint& other)
  35. : m_static(other.m_static), m_normal(other.m_normal), m_infoGlobal(other.m_infoGlobal)
  36. {
  37. }
  38. virtual ~btDeformableContactConstraint() {}
  39. // solve the constraint with inelastic impulse and return the error, which is the square of normal component of velocity diffrerence
  40. // the constraint is solved by calculating the impulse between object A and B in the contact and apply the impulse to both objects involved in the contact
  41. virtual btScalar solveConstraint(const btContactSolverInfo& infoGlobal) = 0;
  42. // get the velocity of the object A in the contact
  43. virtual btVector3 getVa() const = 0;
  44. // get the velocity of the object B in the contact
  45. virtual btVector3 getVb() const = 0;
  46. // get the velocity change of the soft body node in the constraint
  47. virtual btVector3 getDv(const btSoftBody::Node*) const = 0;
  48. // apply impulse to the soft body node and/or face involved
  49. virtual void applyImpulse(const btVector3& impulse) = 0;
  50. // scale the penetration depth by erp
  51. virtual void setPenetrationScale(btScalar scale) = 0;
  52. };
  53. //
  54. // Constraint that a certain node in the deformable objects cannot move
  55. class btDeformableStaticConstraint : public btDeformableContactConstraint
  56. {
  57. public:
  58. btSoftBody::Node* m_node;
  59. btDeformableStaticConstraint(btSoftBody::Node* node, const btContactSolverInfo& infoGlobal) : m_node(node), btDeformableContactConstraint(false, btVector3(0, 0, 0), infoGlobal)
  60. {
  61. }
  62. btDeformableStaticConstraint() {}
  63. btDeformableStaticConstraint(const btDeformableStaticConstraint& other)
  64. : m_node(other.m_node), btDeformableContactConstraint(other)
  65. {
  66. }
  67. virtual ~btDeformableStaticConstraint() {}
  68. virtual btScalar solveConstraint(const btContactSolverInfo& infoGlobal)
  69. {
  70. return 0;
  71. }
  72. virtual btVector3 getVa() const
  73. {
  74. return btVector3(0, 0, 0);
  75. }
  76. virtual btVector3 getVb() const
  77. {
  78. return btVector3(0, 0, 0);
  79. }
  80. virtual btVector3 getDv(const btSoftBody::Node* n) const
  81. {
  82. return btVector3(0, 0, 0);
  83. }
  84. virtual void applyImpulse(const btVector3& impulse) {}
  85. virtual void setPenetrationScale(btScalar scale) {}
  86. };
  87. //
  88. // Anchor Constraint between rigid and deformable node
  89. class btDeformableNodeAnchorConstraint : public btDeformableContactConstraint
  90. {
  91. public:
  92. const btSoftBody::DeformableNodeRigidAnchor* m_anchor;
  93. btDeformableNodeAnchorConstraint(const btSoftBody::DeformableNodeRigidAnchor& c, const btContactSolverInfo& infoGlobal);
  94. btDeformableNodeAnchorConstraint(const btDeformableNodeAnchorConstraint& other);
  95. btDeformableNodeAnchorConstraint() {}
  96. virtual ~btDeformableNodeAnchorConstraint()
  97. {
  98. }
  99. virtual btScalar solveConstraint(const btContactSolverInfo& infoGlobal);
  100. // object A is the rigid/multi body, and object B is the deformable node/face
  101. virtual btVector3 getVa() const;
  102. // get the velocity of the deformable node in contact
  103. virtual btVector3 getVb() const;
  104. virtual btVector3 getDv(const btSoftBody::Node* n) const
  105. {
  106. return btVector3(0, 0, 0);
  107. }
  108. virtual void applyImpulse(const btVector3& impulse);
  109. virtual void setPenetrationScale(btScalar scale) {}
  110. };
  111. //
  112. // Constraint between rigid/multi body and deformable objects
  113. class btDeformableRigidContactConstraint : public btDeformableContactConstraint
  114. {
  115. public:
  116. btVector3 m_total_normal_dv;
  117. btVector3 m_total_tangent_dv;
  118. btScalar m_penetration;
  119. btScalar m_total_split_impulse;
  120. bool m_binding;
  121. const btSoftBody::DeformableRigidContact* m_contact;
  122. btDeformableRigidContactConstraint(const btSoftBody::DeformableRigidContact& c, const btContactSolverInfo& infoGlobal);
  123. btDeformableRigidContactConstraint(const btDeformableRigidContactConstraint& other);
  124. btDeformableRigidContactConstraint() {}
  125. virtual ~btDeformableRigidContactConstraint()
  126. {
  127. }
  128. // object A is the rigid/multi body, and object B is the deformable node/face
  129. virtual btVector3 getVa() const;
  130. // get the split impulse velocity of the deformable face at the contact point
  131. virtual btVector3 getSplitVb() const = 0;
  132. // get the split impulse velocity of the rigid/multibdoy at the contaft
  133. virtual btVector3 getSplitVa() const;
  134. virtual btScalar solveConstraint(const btContactSolverInfo& infoGlobal);
  135. virtual void setPenetrationScale(btScalar scale)
  136. {
  137. m_penetration *= scale;
  138. }
  139. btScalar solveSplitImpulse(const btContactSolverInfo& infoGlobal);
  140. virtual void applySplitImpulse(const btVector3& impulse) = 0;
  141. };
  142. //
  143. // Constraint between rigid/multi body and deformable objects nodes
  144. class btDeformableNodeRigidContactConstraint : public btDeformableRigidContactConstraint
  145. {
  146. public:
  147. // the deformable node in contact
  148. btSoftBody::Node* m_node;
  149. btDeformableNodeRigidContactConstraint(const btSoftBody::DeformableNodeRigidContact& contact, const btContactSolverInfo& infoGlobal);
  150. btDeformableNodeRigidContactConstraint(const btDeformableNodeRigidContactConstraint& other);
  151. btDeformableNodeRigidContactConstraint() {}
  152. virtual ~btDeformableNodeRigidContactConstraint()
  153. {
  154. }
  155. // get the velocity of the deformable node in contact
  156. virtual btVector3 getVb() const;
  157. // get the split impulse velocity of the deformable face at the contact point
  158. virtual btVector3 getSplitVb() const;
  159. // get the velocity change of the input soft body node in the constraint
  160. virtual btVector3 getDv(const btSoftBody::Node*) const;
  161. // cast the contact to the desired type
  162. const btSoftBody::DeformableNodeRigidContact* getContact() const
  163. {
  164. return static_cast<const btSoftBody::DeformableNodeRigidContact*>(m_contact);
  165. }
  166. virtual void applyImpulse(const btVector3& impulse);
  167. virtual void applySplitImpulse(const btVector3& impulse);
  168. };
  169. //
  170. // Constraint between rigid/multi body and deformable objects faces
  171. class btDeformableFaceRigidContactConstraint : public btDeformableRigidContactConstraint
  172. {
  173. public:
  174. btSoftBody::Face* m_face;
  175. bool m_useStrainLimiting;
  176. btDeformableFaceRigidContactConstraint(const btSoftBody::DeformableFaceRigidContact& contact, const btContactSolverInfo& infoGlobal, bool useStrainLimiting);
  177. btDeformableFaceRigidContactConstraint(const btDeformableFaceRigidContactConstraint& other);
  178. btDeformableFaceRigidContactConstraint() : m_useStrainLimiting(false) {}
  179. virtual ~btDeformableFaceRigidContactConstraint()
  180. {
  181. }
  182. // get the velocity of the deformable face at the contact point
  183. virtual btVector3 getVb() const;
  184. // get the split impulse velocity of the deformable face at the contact point
  185. virtual btVector3 getSplitVb() const;
  186. // get the velocity change of the input soft body node in the constraint
  187. virtual btVector3 getDv(const btSoftBody::Node*) const;
  188. // cast the contact to the desired type
  189. const btSoftBody::DeformableFaceRigidContact* getContact() const
  190. {
  191. return static_cast<const btSoftBody::DeformableFaceRigidContact*>(m_contact);
  192. }
  193. virtual void applyImpulse(const btVector3& impulse);
  194. virtual void applySplitImpulse(const btVector3& impulse);
  195. };
  196. //
  197. // Constraint between deformable objects faces and deformable objects nodes
  198. class btDeformableFaceNodeContactConstraint : public btDeformableContactConstraint
  199. {
  200. public:
  201. btSoftBody::Node* m_node;
  202. btSoftBody::Face* m_face;
  203. const btSoftBody::DeformableFaceNodeContact* m_contact;
  204. btVector3 m_total_normal_dv;
  205. btVector3 m_total_tangent_dv;
  206. btDeformableFaceNodeContactConstraint(const btSoftBody::DeformableFaceNodeContact& contact, const btContactSolverInfo& infoGlobal);
  207. btDeformableFaceNodeContactConstraint() {}
  208. virtual ~btDeformableFaceNodeContactConstraint() {}
  209. virtual btScalar solveConstraint(const btContactSolverInfo& infoGlobal);
  210. // get the velocity of the object A in the contact
  211. virtual btVector3 getVa() const;
  212. // get the velocity of the object B in the contact
  213. virtual btVector3 getVb() const;
  214. // get the velocity change of the input soft body node in the constraint
  215. virtual btVector3 getDv(const btSoftBody::Node*) const;
  216. // cast the contact to the desired type
  217. const btSoftBody::DeformableFaceNodeContact* getContact() const
  218. {
  219. return static_cast<const btSoftBody::DeformableFaceNodeContact*>(m_contact);
  220. }
  221. virtual void applyImpulse(const btVector3& impulse);
  222. virtual void setPenetrationScale(btScalar scale) {}
  223. };
  224. #endif /* BT_DEFORMABLE_CONTACT_CONSTRAINT_H */