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.

170 lines
4.5KB

  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 PINBALL_H
  19. #define PINBALL_H
  20. /// This tests bullet collision and provides an example of a gameplay scenario.
  21. /// This also uses a loop shape.
  22. class Pinball : public Test
  23. {
  24. public:
  25. Pinball()
  26. {
  27. // Ground body
  28. b2Body* ground = NULL;
  29. {
  30. b2BodyDef bd;
  31. ground = m_world->CreateBody(&bd);
  32. b2Vec2 vs[5];
  33. vs[0].Set(0.0f, -2.0f);
  34. vs[1].Set(8.0f, 6.0f);
  35. vs[2].Set(8.0f, 20.0f);
  36. vs[3].Set(-8.0f, 20.0f);
  37. vs[4].Set(-8.0f, 6.0f);
  38. b2ChainShape loop;
  39. loop.CreateLoop(vs, 5);
  40. b2FixtureDef fd;
  41. fd.shape = &loop;
  42. fd.density = 0.0f;
  43. ground->CreateFixture(&fd);
  44. }
  45. // Flippers
  46. {
  47. b2Vec2 p1(-2.0f, 0.0f), p2(2.0f, 0.0f);
  48. b2BodyDef bd;
  49. bd.type = b2_dynamicBody;
  50. bd.position = p1;
  51. b2Body* leftFlipper = m_world->CreateBody(&bd);
  52. bd.position = p2;
  53. b2Body* rightFlipper = m_world->CreateBody(&bd);
  54. b2PolygonShape box;
  55. box.SetAsBox(1.75f, 0.1f);
  56. b2FixtureDef fd;
  57. fd.shape = &box;
  58. fd.density = 1.0f;
  59. leftFlipper->CreateFixture(&fd);
  60. rightFlipper->CreateFixture(&fd);
  61. b2RevoluteJointDef jd;
  62. jd.bodyA = ground;
  63. jd.localAnchorB.SetZero();
  64. jd.enableMotor = true;
  65. jd.maxMotorTorque = 1000.0f;
  66. jd.enableLimit = true;
  67. jd.motorSpeed = 0.0f;
  68. jd.localAnchorA = p1;
  69. jd.bodyB = leftFlipper;
  70. jd.lowerAngle = -30.0f * b2_pi / 180.0f;
  71. jd.upperAngle = 5.0f * b2_pi / 180.0f;
  72. m_leftJoint = (b2RevoluteJoint*)m_world->CreateJoint(&jd);
  73. jd.motorSpeed = 0.0f;
  74. jd.localAnchorA = p2;
  75. jd.bodyB = rightFlipper;
  76. jd.lowerAngle = -5.0f * b2_pi / 180.0f;
  77. jd.upperAngle = 30.0f * b2_pi / 180.0f;
  78. m_rightJoint = (b2RevoluteJoint*)m_world->CreateJoint(&jd);
  79. }
  80. // Circle character
  81. {
  82. b2BodyDef bd;
  83. bd.position.Set(1.0f, 15.0f);
  84. bd.type = b2_dynamicBody;
  85. bd.bullet = true;
  86. m_ball = m_world->CreateBody(&bd);
  87. b2CircleShape shape;
  88. shape.m_radius = 0.2f;
  89. b2FixtureDef fd;
  90. fd.shape = &shape;
  91. fd.density = 1.0f;
  92. m_ball->CreateFixture(&fd);
  93. }
  94. m_button = false;
  95. }
  96. void Step()
  97. {
  98. if (m_button)
  99. {
  100. m_leftJoint->SetMotorSpeed(20.0f);
  101. m_rightJoint->SetMotorSpeed(-20.0f);
  102. }
  103. else
  104. {
  105. m_leftJoint->SetMotorSpeed(-10.0f);
  106. m_rightJoint->SetMotorSpeed(10.0f);
  107. }
  108. // Test::Step(settings);
  109. //
  110. // m_debugDraw.DrawString(5, m_textLine, "Press 'a' to control the flippers");
  111. // m_textLine += 15;
  112. }
  113. void Keyboard(unsigned char key)
  114. {
  115. switch (key)
  116. {
  117. case 'a':
  118. case 'A':
  119. m_button = true;
  120. break;
  121. }
  122. }
  123. void KeyboardUp(unsigned char key)
  124. {
  125. switch (key)
  126. {
  127. case 'a':
  128. case 'A':
  129. m_button = false;
  130. break;
  131. }
  132. }
  133. static Test* Create()
  134. {
  135. return new Pinball;
  136. }
  137. b2RevoluteJoint* m_leftJoint;
  138. b2RevoluteJoint* m_rightJoint;
  139. b2Body* m_ball;
  140. bool m_button;
  141. };
  142. #endif