b2Rope.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (c) 2011 Erin Catto http://www.box2d.org
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * 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
  9. * freely, subject to the following restrictions:
  10. * 1. The origin of this software must not be misrepresented; you must not
  11. * claim that you wrote the original software. If you use this software
  12. * in a product, an acknowledgment in the product documentation would be
  13. * appreciated but is not required.
  14. * 2. Altered source versions must be plainly marked as such, and must not be
  15. * misrepresented as being the original software.
  16. * 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #ifndef B2_ROPE_H
  19. #define B2_ROPE_H
  20. #include <Box2D/Common/b2Math.h>
  21. class b2Draw;
  22. ///
  23. struct b2RopeDef
  24. {
  25. b2RopeDef()
  26. {
  27. vertices = NULL;
  28. count = 0;
  29. masses = NULL;
  30. gravity.SetZero();
  31. damping = 0.1f;
  32. k2 = 0.9f;
  33. k3 = 0.1f;
  34. }
  35. ///
  36. b2Vec2* vertices;
  37. ///
  38. int32 count;
  39. ///
  40. float32* masses;
  41. ///
  42. b2Vec2 gravity;
  43. ///
  44. float32 damping;
  45. /// Stretching stiffness
  46. float32 k2;
  47. /// Bending stiffness. Values above 0.5 can make the simulation blow up.
  48. float32 k3;
  49. };
  50. ///
  51. class b2Rope
  52. {
  53. public:
  54. b2Rope();
  55. ~b2Rope();
  56. ///
  57. void Initialize(const b2RopeDef* def);
  58. ///
  59. void Step(float32 timeStep, int32 iterations);
  60. ///
  61. int32 GetVertexCount() const
  62. {
  63. return m_count;
  64. }
  65. ///
  66. const b2Vec2* GetVertices() const
  67. {
  68. return m_ps;
  69. }
  70. ///
  71. void Draw(b2Draw* draw) const;
  72. ///
  73. void SetAngle(float32 angle);
  74. private:
  75. void SolveC2();
  76. void SolveC3();
  77. int32 m_count;
  78. b2Vec2* m_ps;
  79. b2Vec2* m_p0s;
  80. b2Vec2* m_vs;
  81. float32* m_ims;
  82. float32* m_Ls;
  83. float32* m_as;
  84. b2Vec2 m_gravity;
  85. float32 m_damping;
  86. float32 m_k2;
  87. float32 m_k3;
  88. };
  89. #endif