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.

167 lines
5.1KB

  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 REVOLUTE_H
  19. #define REVOLUTE_H
  20. class Revolute : public Test
  21. {
  22. public:
  23. Revolute()
  24. {
  25. b2Body* ground = NULL;
  26. {
  27. b2BodyDef bd;
  28. ground = m_world->CreateBody(&bd);
  29. b2EdgeShape shape;
  30. shape.Set(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f));
  31. b2FixtureDef fd;
  32. fd.shape = &shape;
  33. //fd.filter.categoryBits = 2;
  34. ground->CreateFixture(&fd);
  35. }
  36. {
  37. b2CircleShape shape;
  38. shape.m_radius = 0.5f;
  39. b2BodyDef bd;
  40. bd.type = b2_dynamicBody;
  41. b2RevoluteJointDef rjd;
  42. bd.position.Set(-10.0f, 20.0f);
  43. b2Body* body = m_world->CreateBody(&bd);
  44. body->CreateFixture(&shape, 5.0f);
  45. float32 w = 100.0f;
  46. body->SetAngularVelocity(w);
  47. body->SetLinearVelocity(b2Vec2(-8.0f * w, 0.0f));
  48. rjd.Initialize(ground, body, b2Vec2(-10.0f, 12.0f));
  49. rjd.motorSpeed = 1.0f * b2_pi;
  50. rjd.maxMotorTorque = 10000.0f;
  51. rjd.enableMotor = false;
  52. rjd.lowerAngle = -0.25f * b2_pi;
  53. rjd.upperAngle = 0.5f * b2_pi;
  54. rjd.enableLimit = true;
  55. rjd.collideConnected = true;
  56. m_joint = (b2RevoluteJoint*)m_world->CreateJoint(&rjd);
  57. }
  58. {
  59. b2CircleShape circle_shape;
  60. circle_shape.m_radius = 3.0f;
  61. b2BodyDef circle_bd;
  62. circle_bd.type = b2_dynamicBody;
  63. circle_bd.position.Set(5.0f, 30.0f);
  64. b2FixtureDef fd;
  65. fd.density = 5.0f;
  66. fd.filter.maskBits = 1;
  67. fd.shape = &circle_shape;
  68. m_ball = m_world->CreateBody(&circle_bd);
  69. m_ball->CreateFixture(&fd);
  70. b2PolygonShape polygon_shape;
  71. polygon_shape.SetAsBox(10.0f, 0.2f, b2Vec2 (-10.0f, 0.0f), 0.0f);
  72. b2BodyDef polygon_bd;
  73. polygon_bd.position.Set(20.0f, 10.0f);
  74. polygon_bd.type = b2_dynamicBody;
  75. polygon_bd.bullet = true;
  76. b2Body* polygon_body = m_world->CreateBody(&polygon_bd);
  77. polygon_body->CreateFixture(&polygon_shape, 2.0f);
  78. b2RevoluteJointDef rjd;
  79. rjd.Initialize(ground, polygon_body, b2Vec2(20.0f, 10.0f));
  80. rjd.lowerAngle = -0.25f * b2_pi;
  81. rjd.upperAngle = 0.0f * b2_pi;
  82. rjd.enableLimit = true;
  83. m_world->CreateJoint(&rjd);
  84. }
  85. // Tests mass computation of a small object far from the origin
  86. {
  87. b2BodyDef bodyDef;
  88. bodyDef.type = b2_dynamicBody;
  89. b2Body* body = m_world->CreateBody(&bodyDef);
  90. b2PolygonShape polyShape;
  91. b2Vec2 verts[3];
  92. verts[0].Set( 17.63f, 36.31f );
  93. verts[1].Set( 17.52f, 36.69f );
  94. verts[2].Set( 17.19f, 36.36f );
  95. polyShape.Set(verts, 3);
  96. b2FixtureDef polyFixtureDef;
  97. polyFixtureDef.shape = &polyShape;
  98. polyFixtureDef.density = 1;
  99. body->CreateFixture(&polyFixtureDef); //assertion hits inside here
  100. }
  101. }
  102. void Keyboard(unsigned char key)
  103. {
  104. switch (key)
  105. {
  106. case 'l':
  107. m_joint->EnableLimit(!m_joint->IsLimitEnabled());
  108. break;
  109. case 'm':
  110. m_joint->EnableMotor(!m_joint->IsMotorEnabled());
  111. break;
  112. }
  113. }
  114. void Step(Settings* settings)
  115. {
  116. Test::Step(settings);
  117. m_debugDraw.DrawString(5, m_textLine, "Keys: (l) limits, (m) motor");
  118. m_textLine += 15;
  119. //if (m_stepCount == 360)
  120. //{
  121. // m_ball->SetTransform(b2Vec2(0.0f, 0.5f), 0.0f);
  122. //}
  123. //float32 torque1 = m_joint1->GetMotorTorque();
  124. //m_debugDraw.DrawString(5, m_textLine, "Motor Torque = %4.0f, %4.0f : Motor Force = %4.0f", (float) torque1, (float) torque2, (float) force3);
  125. //m_textLine += 15;
  126. }
  127. static Test* Create()
  128. {
  129. return new Revolute;
  130. }
  131. b2Body* m_ball;
  132. b2RevoluteJoint* m_joint;
  133. };
  134. #endif