b2Rope.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * Copyright (c) 2011 Erin Catto http://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. #include <Box2D/Rope/b2Rope.h>
  19. #include <Box2D/Common/b2Draw.h>
  20. b2Rope::b2Rope()
  21. {
  22. m_count = 0;
  23. m_ps = NULL;
  24. m_p0s = NULL;
  25. m_vs = NULL;
  26. m_ims = NULL;
  27. m_Ls = NULL;
  28. m_as = NULL;
  29. m_gravity.SetZero();
  30. m_k2 = 1.0f;
  31. m_k3 = 0.1f;
  32. }
  33. b2Rope::~b2Rope()
  34. {
  35. b2Free(m_ps);
  36. b2Free(m_p0s);
  37. b2Free(m_vs);
  38. b2Free(m_ims);
  39. b2Free(m_Ls);
  40. b2Free(m_as);
  41. }
  42. void b2Rope::Initialize(const b2RopeDef* def)
  43. {
  44. b2Assert(def->count >= 3);
  45. m_count = def->count;
  46. m_ps = (b2Vec2*)b2Alloc(m_count * sizeof(b2Vec2));
  47. m_p0s = (b2Vec2*)b2Alloc(m_count * sizeof(b2Vec2));
  48. m_vs = (b2Vec2*)b2Alloc(m_count * sizeof(b2Vec2));
  49. m_ims = (float32*)b2Alloc(m_count * sizeof(float32));
  50. for (int32 i = 0; i < m_count; ++i)
  51. {
  52. m_ps[i] = def->vertices[i];
  53. m_p0s[i] = def->vertices[i];
  54. m_vs[i].SetZero();
  55. float32 m = def->masses[i];
  56. if (m > 0.0f)
  57. {
  58. m_ims[i] = 1.0f / m;
  59. }
  60. else
  61. {
  62. m_ims[i] = 0.0f;
  63. }
  64. }
  65. int32 count2 = m_count - 1;
  66. int32 count3 = m_count - 2;
  67. m_Ls = (float32*)b2Alloc(count2 * sizeof(float32));
  68. m_as = (float32*)b2Alloc(count3 * sizeof(float32));
  69. for (int32 i = 0; i < count2; ++i)
  70. {
  71. b2Vec2 p1 = m_ps[i];
  72. b2Vec2 p2 = m_ps[i+1];
  73. m_Ls[i] = b2Distance(p1, p2);
  74. }
  75. for (int32 i = 0; i < count3; ++i)
  76. {
  77. b2Vec2 p1 = m_ps[i];
  78. b2Vec2 p2 = m_ps[i + 1];
  79. b2Vec2 p3 = m_ps[i + 2];
  80. b2Vec2 d1 = p2 - p1;
  81. b2Vec2 d2 = p3 - p2;
  82. float32 a = b2Cross(d1, d2);
  83. float32 b = b2Dot(d1, d2);
  84. m_as[i] = b2Atan2(a, b);
  85. }
  86. m_gravity = def->gravity;
  87. m_damping = def->damping;
  88. m_k2 = def->k2;
  89. m_k3 = def->k3;
  90. }
  91. void b2Rope::Step(float32 h, int32 iterations)
  92. {
  93. if (h == 0.0)
  94. {
  95. return;
  96. }
  97. float32 d = expf(- h * m_damping);
  98. for (int32 i = 0; i < m_count; ++i)
  99. {
  100. m_p0s[i] = m_ps[i];
  101. if (m_ims[i] > 0.0f)
  102. {
  103. m_vs[i] += h * m_gravity;
  104. }
  105. m_vs[i] *= d;
  106. m_ps[i] += h * m_vs[i];
  107. }
  108. for (int32 i = 0; i < iterations; ++i)
  109. {
  110. SolveC2();
  111. SolveC3();
  112. SolveC2();
  113. }
  114. float32 inv_h = 1.0f / h;
  115. for (int32 i = 0; i < m_count; ++i)
  116. {
  117. m_vs[i] = inv_h * (m_ps[i] - m_p0s[i]);
  118. }
  119. }
  120. void b2Rope::SolveC2()
  121. {
  122. int32 count2 = m_count - 1;
  123. for (int32 i = 0; i < count2; ++i)
  124. {
  125. b2Vec2 p1 = m_ps[i];
  126. b2Vec2 p2 = m_ps[i + 1];
  127. b2Vec2 d = p2 - p1;
  128. float32 L = d.Normalize();
  129. float32 im1 = m_ims[i];
  130. float32 im2 = m_ims[i + 1];
  131. if (im1 + im2 == 0.0f)
  132. {
  133. continue;
  134. }
  135. float32 s1 = im1 / (im1 + im2);
  136. float32 s2 = im2 / (im1 + im2);
  137. p1 -= m_k2 * s1 * (m_Ls[i] - L) * d;
  138. p2 += m_k2 * s2 * (m_Ls[i] - L) * d;
  139. m_ps[i] = p1;
  140. m_ps[i + 1] = p2;
  141. }
  142. }
  143. void b2Rope::SetAngle(float32 angle)
  144. {
  145. int32 count3 = m_count - 2;
  146. for (int32 i = 0; i < count3; ++i)
  147. {
  148. m_as[i] = angle;
  149. }
  150. }
  151. void b2Rope::SolveC3()
  152. {
  153. int32 count3 = m_count - 2;
  154. for (int32 i = 0; i < count3; ++i)
  155. {
  156. b2Vec2 p1 = m_ps[i];
  157. b2Vec2 p2 = m_ps[i + 1];
  158. b2Vec2 p3 = m_ps[i + 2];
  159. float32 m1 = m_ims[i];
  160. float32 m2 = m_ims[i + 1];
  161. float32 m3 = m_ims[i + 2];
  162. b2Vec2 d1 = p2 - p1;
  163. b2Vec2 d2 = p3 - p2;
  164. float32 L1sqr = d1.LengthSquared();
  165. float32 L2sqr = d2.LengthSquared();
  166. if (L1sqr * L2sqr == 0.0f)
  167. {
  168. continue;
  169. }
  170. float32 a = b2Cross(d1, d2);
  171. float32 b = b2Dot(d1, d2);
  172. float32 angle = b2Atan2(a, b);
  173. b2Vec2 Jd1 = (-1.0f / L1sqr) * d1.Skew();
  174. b2Vec2 Jd2 = (1.0f / L2sqr) * d2.Skew();
  175. b2Vec2 J1 = -Jd1;
  176. b2Vec2 J2 = Jd1 - Jd2;
  177. b2Vec2 J3 = Jd2;
  178. float32 mass = m1 * b2Dot(J1, J1) + m2 * b2Dot(J2, J2) + m3 * b2Dot(J3, J3);
  179. if (mass == 0.0f)
  180. {
  181. continue;
  182. }
  183. mass = 1.0f / mass;
  184. float32 C = angle - m_as[i];
  185. while (C > b2_pi)
  186. {
  187. angle -= 2 * b2_pi;
  188. C = angle - m_as[i];
  189. }
  190. while (C < -b2_pi)
  191. {
  192. angle += 2.0f * b2_pi;
  193. C = angle - m_as[i];
  194. }
  195. float32 impulse = - m_k3 * mass * C;
  196. p1 += (m1 * impulse) * J1;
  197. p2 += (m2 * impulse) * J2;
  198. p3 += (m3 * impulse) * J3;
  199. m_ps[i] = p1;
  200. m_ps[i + 1] = p2;
  201. m_ps[i + 2] = p3;
  202. }
  203. }
  204. void b2Rope::Draw(b2Draw* draw) const
  205. {
  206. b2Color c(0.4f, 0.5f, 0.7f);
  207. for (int32 i = 0; i < m_count - 1; ++i)
  208. {
  209. draw->DrawSegment(m_ps[i], m_ps[i+1], c);
  210. }
  211. }