The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

212 lines
6.1KB

  1. /*
  2. * Copyright (c) 2006-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 CANTILEVER_H
  19. #define CANTILEVER_H
  20. // It is difficult to make a cantilever made of links completely rigid with weld joints.
  21. // You will have to use a high number of iterations to make them stiff.
  22. // So why not go ahead and use soft weld joints? They behave like a revolute
  23. // joint with a rotational spring.
  24. class Cantilever : public Test
  25. {
  26. public:
  27. enum
  28. {
  29. e_count = 8
  30. };
  31. Cantilever()
  32. {
  33. b2Body* ground = NULL;
  34. {
  35. b2BodyDef bd;
  36. ground = m_world->CreateBody(&bd);
  37. b2EdgeShape shape;
  38. shape.Set(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f));
  39. ground->CreateFixture(&shape, 0.0f);
  40. }
  41. {
  42. b2PolygonShape shape;
  43. shape.SetAsBox(0.5f, 0.125f);
  44. b2FixtureDef fd;
  45. fd.shape = &shape;
  46. fd.density = 20.0f;
  47. b2WeldJointDef jd;
  48. b2Body* prevBody = ground;
  49. for (int32 i = 0; i < e_count; ++i)
  50. {
  51. b2BodyDef bd;
  52. bd.type = b2_dynamicBody;
  53. bd.position.Set(-14.5f + 1.0f * i, 5.0f);
  54. b2Body* body = m_world->CreateBody(&bd);
  55. body->CreateFixture(&fd);
  56. b2Vec2 anchor(-15.0f + 1.0f * i, 5.0f);
  57. jd.Initialize(prevBody, body, anchor);
  58. m_world->CreateJoint(&jd);
  59. prevBody = body;
  60. }
  61. }
  62. {
  63. b2PolygonShape shape;
  64. shape.SetAsBox(1.0f, 0.125f);
  65. b2FixtureDef fd;
  66. fd.shape = &shape;
  67. fd.density = 20.0f;
  68. b2WeldJointDef jd;
  69. jd.frequencyHz = 5.0f;
  70. jd.dampingRatio = 0.7f;
  71. b2Body* prevBody = ground;
  72. for (int32 i = 0; i < 3; ++i)
  73. {
  74. b2BodyDef bd;
  75. bd.type = b2_dynamicBody;
  76. bd.position.Set(-14.0f + 2.0f * i, 15.0f);
  77. b2Body* body = m_world->CreateBody(&bd);
  78. body->CreateFixture(&fd);
  79. b2Vec2 anchor(-15.0f + 2.0f * i, 15.0f);
  80. jd.Initialize(prevBody, body, anchor);
  81. m_world->CreateJoint(&jd);
  82. prevBody = body;
  83. }
  84. }
  85. {
  86. b2PolygonShape shape;
  87. shape.SetAsBox(0.5f, 0.125f);
  88. b2FixtureDef fd;
  89. fd.shape = &shape;
  90. fd.density = 20.0f;
  91. b2WeldJointDef jd;
  92. b2Body* prevBody = ground;
  93. for (int32 i = 0; i < e_count; ++i)
  94. {
  95. b2BodyDef bd;
  96. bd.type = b2_dynamicBody;
  97. bd.position.Set(-4.5f + 1.0f * i, 5.0f);
  98. b2Body* body = m_world->CreateBody(&bd);
  99. body->CreateFixture(&fd);
  100. if (i > 0)
  101. {
  102. b2Vec2 anchor(-5.0f + 1.0f * i, 5.0f);
  103. jd.Initialize(prevBody, body, anchor);
  104. m_world->CreateJoint(&jd);
  105. }
  106. prevBody = body;
  107. }
  108. }
  109. {
  110. b2PolygonShape shape;
  111. shape.SetAsBox(0.5f, 0.125f);
  112. b2FixtureDef fd;
  113. fd.shape = &shape;
  114. fd.density = 20.0f;
  115. b2WeldJointDef jd;
  116. jd.frequencyHz = 8.0f;
  117. jd.dampingRatio = 0.7f;
  118. b2Body* prevBody = ground;
  119. for (int32 i = 0; i < e_count; ++i)
  120. {
  121. b2BodyDef bd;
  122. bd.type = b2_dynamicBody;
  123. bd.position.Set(5.5f + 1.0f * i, 10.0f);
  124. b2Body* body = m_world->CreateBody(&bd);
  125. body->CreateFixture(&fd);
  126. if (i > 0)
  127. {
  128. b2Vec2 anchor(5.0f + 1.0f * i, 10.0f);
  129. jd.Initialize(prevBody, body, anchor);
  130. m_world->CreateJoint(&jd);
  131. }
  132. prevBody = body;
  133. }
  134. }
  135. for (int32 i = 0; i < 2; ++i)
  136. {
  137. b2Vec2 vertices[3];
  138. vertices[0].Set(-0.5f, 0.0f);
  139. vertices[1].Set(0.5f, 0.0f);
  140. vertices[2].Set(0.0f, 1.5f);
  141. b2PolygonShape shape;
  142. shape.Set(vertices, 3);
  143. b2FixtureDef fd;
  144. fd.shape = &shape;
  145. fd.density = 1.0f;
  146. b2BodyDef bd;
  147. bd.type = b2_dynamicBody;
  148. bd.position.Set(-8.0f + 8.0f * i, 12.0f);
  149. b2Body* body = m_world->CreateBody(&bd);
  150. body->CreateFixture(&fd);
  151. }
  152. for (int32 i = 0; i < 2; ++i)
  153. {
  154. b2CircleShape shape;
  155. shape.m_radius = 0.5f;
  156. b2FixtureDef fd;
  157. fd.shape = &shape;
  158. fd.density = 1.0f;
  159. b2BodyDef bd;
  160. bd.type = b2_dynamicBody;
  161. bd.position.Set(-6.0f + 6.0f * i, 10.0f);
  162. b2Body* body = m_world->CreateBody(&bd);
  163. body->CreateFixture(&fd);
  164. }
  165. }
  166. static Test* Create()
  167. {
  168. return new Cantilever;
  169. }
  170. b2Body* m_middle;
  171. };
  172. #endif