b2_rope.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // MIT License
  2. // Copyright (c) 2019 Erin Catto
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. // The above copyright notice and this permission notice shall be included in all
  10. // copies or substantial portions of the Software.
  11. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. // SOFTWARE.
  18. #ifndef B2_ROPE_H
  19. #define B2_ROPE_H
  20. #include "b2_api.h"
  21. #include "b2_math.h"
  22. class b2Draw;
  23. struct b2RopeStretch;
  24. struct b2RopeBend;
  25. enum b2StretchingModel
  26. {
  27. b2_pbdStretchingModel,
  28. b2_xpbdStretchingModel
  29. };
  30. enum b2BendingModel
  31. {
  32. b2_springAngleBendingModel = 0,
  33. b2_pbdAngleBendingModel,
  34. b2_xpbdAngleBendingModel,
  35. b2_pbdDistanceBendingModel,
  36. b2_pbdHeightBendingModel,
  37. b2_pbdTriangleBendingModel
  38. };
  39. ///
  40. struct B2_API b2RopeTuning
  41. {
  42. b2RopeTuning()
  43. {
  44. stretchingModel = b2_pbdStretchingModel;
  45. bendingModel = b2_pbdAngleBendingModel;
  46. damping = 0.0f;
  47. stretchStiffness = 1.0f;
  48. stretchHertz = 1.0f;
  49. stretchDamping = 0.0f;
  50. bendStiffness = 0.5f;
  51. bendHertz = 1.0f;
  52. bendDamping = 0.0f;
  53. isometric = false;
  54. fixedEffectiveMass = false;
  55. warmStart = false;
  56. }
  57. b2StretchingModel stretchingModel;
  58. b2BendingModel bendingModel;
  59. float damping;
  60. float stretchStiffness;
  61. float stretchHertz;
  62. float stretchDamping;
  63. float bendStiffness;
  64. float bendHertz;
  65. float bendDamping;
  66. bool isometric;
  67. bool fixedEffectiveMass;
  68. bool warmStart;
  69. };
  70. ///
  71. struct B2_API b2RopeDef
  72. {
  73. b2RopeDef()
  74. {
  75. position.SetZero();
  76. vertices = nullptr;
  77. count = 0;
  78. masses = nullptr;
  79. gravity.SetZero();
  80. }
  81. b2Vec2 position;
  82. b2Vec2* vertices;
  83. int32 count;
  84. float* masses;
  85. b2Vec2 gravity;
  86. b2RopeTuning tuning;
  87. };
  88. ///
  89. class B2_API b2Rope
  90. {
  91. public:
  92. b2Rope();
  93. ~b2Rope();
  94. ///
  95. void Create(const b2RopeDef& def);
  96. ///
  97. void SetTuning(const b2RopeTuning& tuning);
  98. ///
  99. void Step(float timeStep, int32 iterations, const b2Vec2& position);
  100. ///
  101. void Reset(const b2Vec2& position);
  102. ///
  103. void Draw(b2Draw* draw) const;
  104. private:
  105. void SolveStretch_PBD();
  106. void SolveStretch_XPBD(float dt);
  107. void SolveBend_PBD_Angle();
  108. void SolveBend_XPBD_Angle(float dt);
  109. void SolveBend_PBD_Distance();
  110. void SolveBend_PBD_Height();
  111. void SolveBend_PBD_Triangle();
  112. void ApplyBendForces(float dt);
  113. b2Vec2 m_position;
  114. int32 m_count;
  115. int32 m_stretchCount;
  116. int32 m_bendCount;
  117. b2RopeStretch* m_stretchConstraints;
  118. b2RopeBend* m_bendConstraints;
  119. b2Vec2* m_bindPositions;
  120. b2Vec2* m_ps;
  121. b2Vec2* m_p0s;
  122. b2Vec2* m_vs;
  123. float* m_invMasses;
  124. b2Vec2 m_gravity;
  125. b2RopeTuning m_tuning;
  126. };
  127. #endif