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.

157 lines
4.9KB

  1. /*
  2. * Copyright (c) 2006-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 SLIDER_CRANK_H
  19. #define SLIDER_CRANK_H
  20. // A motor driven slider crank with joint friction.
  21. class SliderCrank : public Test
  22. {
  23. public:
  24. SliderCrank()
  25. {
  26. b2Body* ground = NULL;
  27. {
  28. b2BodyDef bd;
  29. ground = m_world->CreateBody(&bd);
  30. b2EdgeShape shape;
  31. shape.Set(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f));
  32. ground->CreateFixture(&shape, 0.0f);
  33. }
  34. {
  35. b2Body* prevBody = ground;
  36. // Define crank.
  37. {
  38. b2PolygonShape shape;
  39. shape.SetAsBox(0.5f, 2.0f);
  40. b2BodyDef bd;
  41. bd.type = b2_dynamicBody;
  42. bd.position.Set(0.0f, 7.0f);
  43. b2Body* body = m_world->CreateBody(&bd);
  44. body->CreateFixture(&shape, 2.0f);
  45. b2RevoluteJointDef rjd;
  46. rjd.Initialize(prevBody, body, b2Vec2(0.0f, 5.0f));
  47. rjd.motorSpeed = 1.0f * b2_pi;
  48. rjd.maxMotorTorque = 10000.0f;
  49. rjd.enableMotor = true;
  50. m_joint1 = (b2RevoluteJoint*)m_world->CreateJoint(&rjd);
  51. prevBody = body;
  52. }
  53. // Define follower.
  54. {
  55. b2PolygonShape shape;
  56. shape.SetAsBox(0.5f, 4.0f);
  57. b2BodyDef bd;
  58. bd.type = b2_dynamicBody;
  59. bd.position.Set(0.0f, 13.0f);
  60. b2Body* body = m_world->CreateBody(&bd);
  61. body->CreateFixture(&shape, 2.0f);
  62. b2RevoluteJointDef rjd;
  63. rjd.Initialize(prevBody, body, b2Vec2(0.0f, 9.0f));
  64. rjd.enableMotor = false;
  65. m_world->CreateJoint(&rjd);
  66. prevBody = body;
  67. }
  68. // Define piston
  69. {
  70. b2PolygonShape shape;
  71. shape.SetAsBox(1.5f, 1.5f);
  72. b2BodyDef bd;
  73. bd.type = b2_dynamicBody;
  74. bd.fixedRotation = true;
  75. bd.position.Set(0.0f, 17.0f);
  76. b2Body* body = m_world->CreateBody(&bd);
  77. body->CreateFixture(&shape, 2.0f);
  78. b2RevoluteJointDef rjd;
  79. rjd.Initialize(prevBody, body, b2Vec2(0.0f, 17.0f));
  80. m_world->CreateJoint(&rjd);
  81. b2PrismaticJointDef pjd;
  82. pjd.Initialize(ground, body, b2Vec2(0.0f, 17.0f), b2Vec2(0.0f, 1.0f));
  83. pjd.maxMotorForce = 1000.0f;
  84. pjd.enableMotor = true;
  85. m_joint2 = (b2PrismaticJoint*)m_world->CreateJoint(&pjd);
  86. }
  87. // Create a payload
  88. {
  89. b2PolygonShape shape;
  90. shape.SetAsBox(1.5f, 1.5f);
  91. b2BodyDef bd;
  92. bd.type = b2_dynamicBody;
  93. bd.position.Set(0.0f, 23.0f);
  94. b2Body* body = m_world->CreateBody(&bd);
  95. body->CreateFixture(&shape, 2.0f);
  96. }
  97. }
  98. }
  99. void Keyboard(unsigned char key)
  100. {
  101. switch (key)
  102. {
  103. case 'f':
  104. m_joint2->EnableMotor(!m_joint2->IsMotorEnabled());
  105. m_joint2->GetBodyB()->SetAwake(true);
  106. break;
  107. case 'm':
  108. m_joint1->EnableMotor(!m_joint1->IsMotorEnabled());
  109. m_joint1->GetBodyB()->SetAwake(true);
  110. break;
  111. }
  112. }
  113. void Step(Settings* settings)
  114. {
  115. Test::Step(settings);
  116. m_debugDraw.DrawString(5, m_textLine, "Keys: (f) toggle friction, (m) toggle motor");
  117. m_textLine += 15;
  118. float32 torque = m_joint1->GetMotorTorque(settings->hz);
  119. m_debugDraw.DrawString(5, m_textLine, "Motor Torque = %5.0f", (float) torque);
  120. m_textLine += 15;
  121. }
  122. static Test* Create()
  123. {
  124. return new SliderCrank;
  125. }
  126. b2RevoluteJoint* m_joint1;
  127. b2PrismaticJoint* m_joint2;
  128. };
  129. #endif