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.

188 lines
6.0KB

  1. /*
  2. * Copyright (c) 2007-2009 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 GEARS_H
  19. #define GEARS_H
  20. class Gears : public Test
  21. {
  22. public:
  23. Gears()
  24. {
  25. b2Body* ground = NULL;
  26. {
  27. b2BodyDef bd;
  28. ground = m_world->CreateBody(&bd);
  29. b2EdgeShape shape;
  30. shape.Set(b2Vec2(50.0f, 0.0f), b2Vec2(-50.0f, 0.0f));
  31. ground->CreateFixture(&shape, 0.0f);
  32. }
  33. // Gears co
  34. {
  35. b2CircleShape circle1;
  36. circle1.m_radius = 1.0f;
  37. b2PolygonShape box;
  38. box.SetAsBox(0.5f, 5.0f);
  39. b2CircleShape circle2;
  40. circle2.m_radius = 2.0f;
  41. b2BodyDef bd1;
  42. bd1.type = b2_staticBody;
  43. bd1.position.Set(10.0f, 9.0f);
  44. b2Body* body1 = m_world->CreateBody(&bd1);
  45. body1->CreateFixture(&circle1, 0.0f);
  46. b2BodyDef bd2;
  47. bd2.type = b2_dynamicBody;
  48. bd2.position.Set(10.0f, 8.0f);
  49. b2Body* body2 = m_world->CreateBody(&bd2);
  50. body2->CreateFixture(&box, 5.0f);
  51. b2BodyDef bd3;
  52. bd3.type = b2_dynamicBody;
  53. bd3.position.Set(10.0f, 6.0f);
  54. b2Body* body3 = m_world->CreateBody(&bd3);
  55. body3->CreateFixture(&circle2, 5.0f);
  56. b2RevoluteJointDef jd1;
  57. jd1.Initialize(body2, body1, bd1.position);
  58. b2Joint* joint1 = m_world->CreateJoint(&jd1);
  59. b2RevoluteJointDef jd2;
  60. jd2.Initialize(body2, body3, bd3.position);
  61. b2Joint* joint2 = m_world->CreateJoint(&jd2);
  62. b2GearJointDef jd4;
  63. jd4.bodyA = body1;
  64. jd4.bodyB = body3;
  65. jd4.joint1 = joint1;
  66. jd4.joint2 = joint2;
  67. jd4.ratio = circle2.m_radius / circle1.m_radius;
  68. m_world->CreateJoint(&jd4);
  69. }
  70. {
  71. b2CircleShape circle1;
  72. circle1.m_radius = 1.0f;
  73. b2CircleShape circle2;
  74. circle2.m_radius = 2.0f;
  75. b2PolygonShape box;
  76. box.SetAsBox(0.5f, 5.0f);
  77. b2BodyDef bd1;
  78. bd1.type = b2_dynamicBody;
  79. bd1.position.Set(-3.0f, 12.0f);
  80. b2Body* body1 = m_world->CreateBody(&bd1);
  81. body1->CreateFixture(&circle1, 5.0f);
  82. b2RevoluteJointDef jd1;
  83. jd1.bodyA = ground;
  84. jd1.bodyB = body1;
  85. jd1.localAnchorA = ground->GetLocalPoint(bd1.position);
  86. jd1.localAnchorB = body1->GetLocalPoint(bd1.position);
  87. jd1.referenceAngle = body1->GetAngle() - ground->GetAngle();
  88. m_joint1 = (b2RevoluteJoint*)m_world->CreateJoint(&jd1);
  89. b2BodyDef bd2;
  90. bd2.type = b2_dynamicBody;
  91. bd2.position.Set(0.0f, 12.0f);
  92. b2Body* body2 = m_world->CreateBody(&bd2);
  93. body2->CreateFixture(&circle2, 5.0f);
  94. b2RevoluteJointDef jd2;
  95. jd2.Initialize(ground, body2, bd2.position);
  96. m_joint2 = (b2RevoluteJoint*)m_world->CreateJoint(&jd2);
  97. b2BodyDef bd3;
  98. bd3.type = b2_dynamicBody;
  99. bd3.position.Set(2.5f, 12.0f);
  100. b2Body* body3 = m_world->CreateBody(&bd3);
  101. body3->CreateFixture(&box, 5.0f);
  102. b2PrismaticJointDef jd3;
  103. jd3.Initialize(ground, body3, bd3.position, b2Vec2(0.0f, 1.0f));
  104. jd3.lowerTranslation = -5.0f;
  105. jd3.upperTranslation = 5.0f;
  106. jd3.enableLimit = true;
  107. m_joint3 = (b2PrismaticJoint*)m_world->CreateJoint(&jd3);
  108. b2GearJointDef jd4;
  109. jd4.bodyA = body1;
  110. jd4.bodyB = body2;
  111. jd4.joint1 = m_joint1;
  112. jd4.joint2 = m_joint2;
  113. jd4.ratio = circle2.m_radius / circle1.m_radius;
  114. m_joint4 = (b2GearJoint*)m_world->CreateJoint(&jd4);
  115. b2GearJointDef jd5;
  116. jd5.bodyA = body2;
  117. jd5.bodyB = body3;
  118. jd5.joint1 = m_joint2;
  119. jd5.joint2 = m_joint3;
  120. jd5.ratio = -1.0f / circle2.m_radius;
  121. m_joint5 = (b2GearJoint*)m_world->CreateJoint(&jd5);
  122. }
  123. }
  124. void Keyboard(unsigned char key)
  125. {
  126. switch (key)
  127. {
  128. case 0:
  129. break;
  130. }
  131. }
  132. void Step(Settings* settings)
  133. {
  134. Test::Step(settings);
  135. float32 ratio, value;
  136. ratio = m_joint4->GetRatio();
  137. value = m_joint1->GetJointAngle() + ratio * m_joint2->GetJointAngle();
  138. m_debugDraw.DrawString(5, m_textLine, "theta1 + %4.2f * theta2 = %4.2f", (float) ratio, (float) value);
  139. m_textLine += 15;
  140. ratio = m_joint5->GetRatio();
  141. value = m_joint2->GetJointAngle() + ratio * m_joint3->GetJointTranslation();
  142. m_debugDraw.DrawString(5, m_textLine, "theta2 + %4.2f * delta = %4.2f", (float) ratio, (float) value);
  143. m_textLine += 15;
  144. }
  145. static Test* Create()
  146. {
  147. return new Gears;
  148. }
  149. b2RevoluteJoint* m_joint1;
  150. b2RevoluteJoint* m_joint2;
  151. b2PrismaticJoint* m_joint3;
  152. b2GearJoint* m_joint4;
  153. b2GearJoint* m_joint5;
  154. };
  155. #endif