btDeformableMousePickingForce.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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_MOUSE_PICKING_FORCE_H
  15. #define BT_MOUSE_PICKING_FORCE_H
  16. #include "btDeformableLagrangianForce.h"
  17. class btDeformableMousePickingForce : public btDeformableLagrangianForce
  18. {
  19. // If true, the damping force will be in the direction of the spring
  20. // If false, the damping force will be in the direction of the velocity
  21. btScalar m_elasticStiffness, m_dampingStiffness;
  22. const btSoftBody::Face& m_face;
  23. btVector3 m_mouse_pos;
  24. btScalar m_maxForce;
  25. public:
  26. typedef btAlignedObjectArray<btVector3> TVStack;
  27. btDeformableMousePickingForce(btScalar k, btScalar d, const btSoftBody::Face& face, const btVector3& mouse_pos, btScalar maxForce = 0.3) : m_elasticStiffness(k), m_dampingStiffness(d), m_face(face), m_mouse_pos(mouse_pos), m_maxForce(maxForce)
  28. {
  29. }
  30. virtual void addScaledForces(btScalar scale, TVStack& force)
  31. {
  32. addScaledDampingForce(scale, force);
  33. addScaledElasticForce(scale, force);
  34. }
  35. virtual void addScaledExplicitForce(btScalar scale, TVStack& force)
  36. {
  37. addScaledElasticForce(scale, force);
  38. }
  39. virtual void addScaledDampingForce(btScalar scale, TVStack& force)
  40. {
  41. for (int i = 0; i < 3; ++i)
  42. {
  43. btVector3 v_diff = m_face.m_n[i]->m_v;
  44. btVector3 scaled_force = scale * m_dampingStiffness * v_diff;
  45. if ((m_face.m_n[i]->m_x - m_mouse_pos).norm() > SIMD_EPSILON)
  46. {
  47. btVector3 dir = (m_face.m_n[i]->m_x - m_mouse_pos).normalized();
  48. scaled_force = scale * m_dampingStiffness * v_diff.dot(dir) * dir;
  49. }
  50. force[m_face.m_n[i]->index] -= scaled_force;
  51. }
  52. }
  53. virtual void addScaledElasticForce(btScalar scale, TVStack& force)
  54. {
  55. btScalar scaled_stiffness = scale * m_elasticStiffness;
  56. for (int i = 0; i < 3; ++i)
  57. {
  58. btVector3 dir = (m_face.m_n[i]->m_q - m_mouse_pos);
  59. btVector3 scaled_force = scaled_stiffness * dir;
  60. if (scaled_force.safeNorm() > m_maxForce)
  61. {
  62. scaled_force.safeNormalize();
  63. scaled_force *= m_maxForce;
  64. }
  65. force[m_face.m_n[i]->index] -= scaled_force;
  66. }
  67. }
  68. virtual void addScaledDampingForceDifferential(btScalar scale, const TVStack& dv, TVStack& df)
  69. {
  70. btScalar scaled_k_damp = m_dampingStiffness * scale;
  71. for (int i = 0; i < 3; ++i)
  72. {
  73. btVector3 local_scaled_df = scaled_k_damp * dv[m_face.m_n[i]->index];
  74. if ((m_face.m_n[i]->m_x - m_mouse_pos).norm() > SIMD_EPSILON)
  75. {
  76. btVector3 dir = (m_face.m_n[i]->m_x - m_mouse_pos).normalized();
  77. local_scaled_df = scaled_k_damp * dv[m_face.m_n[i]->index].dot(dir) * dir;
  78. }
  79. df[m_face.m_n[i]->index] -= local_scaled_df;
  80. }
  81. }
  82. virtual void buildDampingForceDifferentialDiagonal(btScalar scale, TVStack& diagA) {}
  83. virtual double totalElasticEnergy(btScalar dt)
  84. {
  85. double energy = 0;
  86. for (int i = 0; i < 3; ++i)
  87. {
  88. btVector3 dir = (m_face.m_n[i]->m_q - m_mouse_pos);
  89. btVector3 scaled_force = m_elasticStiffness * dir;
  90. if (scaled_force.safeNorm() > m_maxForce)
  91. {
  92. scaled_force.safeNormalize();
  93. scaled_force *= m_maxForce;
  94. }
  95. energy += 0.5 * scaled_force.dot(dir);
  96. }
  97. return energy;
  98. }
  99. virtual double totalDampingEnergy(btScalar dt)
  100. {
  101. double energy = 0;
  102. for (int i = 0; i < 3; ++i)
  103. {
  104. btVector3 v_diff = m_face.m_n[i]->m_v;
  105. btVector3 scaled_force = m_dampingStiffness * v_diff;
  106. if ((m_face.m_n[i]->m_x - m_mouse_pos).norm() > SIMD_EPSILON)
  107. {
  108. btVector3 dir = (m_face.m_n[i]->m_x - m_mouse_pos).normalized();
  109. scaled_force = m_dampingStiffness * v_diff.dot(dir) * dir;
  110. }
  111. energy -= scaled_force.dot(m_face.m_n[i]->m_v) / dt;
  112. }
  113. return energy;
  114. }
  115. virtual void addScaledElasticForceDifferential(btScalar scale, const TVStack& dx, TVStack& df)
  116. {
  117. btScalar scaled_stiffness = scale * m_elasticStiffness;
  118. for (int i = 0; i < 3; ++i)
  119. {
  120. btVector3 dir = (m_face.m_n[i]->m_q - m_mouse_pos);
  121. btScalar dir_norm = dir.norm();
  122. btVector3 dir_normalized = (dir_norm > SIMD_EPSILON) ? dir.normalized() : btVector3(0, 0, 0);
  123. int id = m_face.m_n[i]->index;
  124. btVector3 dx_diff = dx[id];
  125. btScalar r = 0; // rest length is 0 for picking spring
  126. btVector3 scaled_df = btVector3(0, 0, 0);
  127. if (dir_norm > SIMD_EPSILON)
  128. {
  129. scaled_df -= scaled_stiffness * dir_normalized.dot(dx_diff) * dir_normalized;
  130. scaled_df += scaled_stiffness * dir_normalized.dot(dx_diff) * ((dir_norm - r) / dir_norm) * dir_normalized;
  131. scaled_df -= scaled_stiffness * ((dir_norm - r) / dir_norm) * dx_diff;
  132. }
  133. df[id] += scaled_df;
  134. }
  135. }
  136. void setMousePos(const btVector3& p)
  137. {
  138. m_mouse_pos = p;
  139. }
  140. virtual btDeformableLagrangianForceType getForceType()
  141. {
  142. return BT_MOUSE_PICKING_FORCE;
  143. }
  144. };
  145. #endif /* btMassSpring_h */