btDeformableGravityForce.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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_GRAVITY_FORCE_H
  15. #define BT_DEFORMABLE_GRAVITY_FORCE_H
  16. #include "btDeformableLagrangianForce.h"
  17. class btDeformableGravityForce : public btDeformableLagrangianForce
  18. {
  19. public:
  20. typedef btAlignedObjectArray<btVector3> TVStack;
  21. btVector3 m_gravity;
  22. btDeformableGravityForce(const btVector3& g) : m_gravity(g)
  23. {
  24. }
  25. virtual void addScaledForces(btScalar scale, TVStack& force)
  26. {
  27. addScaledGravityForce(scale, force);
  28. }
  29. virtual void addScaledExplicitForce(btScalar scale, TVStack& force)
  30. {
  31. addScaledGravityForce(scale, force);
  32. }
  33. virtual void addScaledDampingForce(btScalar scale, TVStack& force)
  34. {
  35. }
  36. virtual void addScaledElasticForceDifferential(btScalar scale, const TVStack& dx, TVStack& df)
  37. {
  38. }
  39. virtual void addScaledDampingForceDifferential(btScalar scale, const TVStack& dv, TVStack& df)
  40. {
  41. }
  42. virtual void buildDampingForceDifferentialDiagonal(btScalar scale, TVStack& diagA) {}
  43. virtual void addScaledGravityForce(btScalar scale, TVStack& force)
  44. {
  45. int numNodes = getNumNodes();
  46. btAssert(numNodes <= force.size());
  47. for (int i = 0; i < m_softBodies.size(); ++i)
  48. {
  49. btSoftBody* psb = m_softBodies[i];
  50. if (!psb->isActive())
  51. {
  52. continue;
  53. }
  54. for (int j = 0; j < psb->m_nodes.size(); ++j)
  55. {
  56. btSoftBody::Node& n = psb->m_nodes[j];
  57. size_t id = n.index;
  58. btScalar mass = (n.m_im == 0) ? 0 : 1. / n.m_im;
  59. btVector3 scaled_force = scale * m_gravity * mass * m_softBodies[i]->m_gravityFactor;
  60. force[id] += scaled_force;
  61. }
  62. }
  63. }
  64. virtual btDeformableLagrangianForceType getForceType()
  65. {
  66. return BT_GRAVITY_FORCE;
  67. }
  68. // the gravitational potential energy
  69. virtual double totalEnergy(btScalar dt)
  70. {
  71. double e = 0;
  72. for (int i = 0; i < m_softBodies.size(); ++i)
  73. {
  74. btSoftBody* psb = m_softBodies[i];
  75. if (!psb->isActive())
  76. {
  77. continue;
  78. }
  79. for (int j = 0; j < psb->m_nodes.size(); ++j)
  80. {
  81. const btSoftBody::Node& node = psb->m_nodes[j];
  82. if (node.m_im > 0)
  83. {
  84. e -= m_gravity.dot(node.m_q) / node.m_im;
  85. }
  86. }
  87. }
  88. return e;
  89. }
  90. };
  91. #endif /* BT_DEFORMABLE_GRAVITY_FORCE_H */