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.

146 lines
4.4KB

  1. /*
  2. * Copyright (c) 2006-2010 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 ROPE_JOINT_H
  19. #define ROPE_JOINT_H
  20. /// This test shows how a rope joint can be used to stabilize a chain of
  21. /// bodies with a heavy payload. Notice that the rope joint just prevents
  22. /// excessive stretching and has no other effect.
  23. /// By disabling the rope joint you can see that the Box2D solver has trouble
  24. /// supporting heavy bodies with light bodies. Try playing around with the
  25. /// densities, time step, and iterations to see how they affect stability.
  26. /// This test also shows how to use contact filtering. Filtering is configured
  27. /// so that the payload does not collide with the chain.
  28. class RopeJoint : public Test
  29. {
  30. public:
  31. RopeJoint()
  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. fd.friction = 0.2f;
  48. fd.filter.categoryBits = 0x0001;
  49. fd.filter.maskBits = 0xFFFF & ~0x0002;
  50. b2RevoluteJointDef jd;
  51. jd.collideConnected = false;
  52. const int32 N = 10;
  53. const float32 y = 15.0f;
  54. m_ropeDef.localAnchorA.Set(0.0f, y);
  55. b2Body* prevBody = ground;
  56. for (int32 i = 0; i < N; ++i)
  57. {
  58. b2BodyDef bd;
  59. bd.type = b2_dynamicBody;
  60. bd.position.Set(0.5f + 1.0f * i, y);
  61. if (i == N - 1)
  62. {
  63. shape.SetAsBox(1.5f, 1.5f);
  64. fd.density = 100.0f;
  65. fd.filter.categoryBits = 0x0002;
  66. bd.position.Set(1.0f * i, y);
  67. bd.angularDamping = 0.4f;
  68. }
  69. b2Body* body = m_world->CreateBody(&bd);
  70. body->CreateFixture(&fd);
  71. b2Vec2 anchor(float32(i), y);
  72. jd.Initialize(prevBody, body, anchor);
  73. m_world->CreateJoint(&jd);
  74. prevBody = body;
  75. }
  76. m_ropeDef.localAnchorB.SetZero();
  77. float32 extraLength = 0.01f;
  78. m_ropeDef.maxLength = N - 1.0f + extraLength;
  79. m_ropeDef.bodyB = prevBody;
  80. }
  81. {
  82. m_ropeDef.bodyA = ground;
  83. m_rope = m_world->CreateJoint(&m_ropeDef);
  84. }
  85. }
  86. void Keyboard(unsigned char key)
  87. {
  88. switch (key)
  89. {
  90. case 'j':
  91. if (m_rope)
  92. {
  93. m_world->DestroyJoint(m_rope);
  94. m_rope = NULL;
  95. }
  96. else
  97. {
  98. m_rope = m_world->CreateJoint(&m_ropeDef);
  99. }
  100. break;
  101. }
  102. }
  103. void Step(Settings* settings)
  104. {
  105. Test::Step(settings);
  106. m_debugDraw.DrawString(5, m_textLine, "Press (j) to toggle the rope joint.");
  107. m_textLine += 15;
  108. if (m_rope)
  109. {
  110. m_debugDraw.DrawString(5, m_textLine, "Rope ON");
  111. }
  112. else
  113. {
  114. m_debugDraw.DrawString(5, m_textLine, "Rope OFF");
  115. }
  116. m_textLine += 15;
  117. }
  118. static Test* Create()
  119. {
  120. return new RopeJoint;
  121. }
  122. b2RopeJointDef m_ropeDef;
  123. b2Joint* m_rope;
  124. };
  125. #endif