car.bmx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. SuperStrict
  2. Framework Physics.Box2d
  3. Import SDL.SDLRenderMax2D
  4. Import "test.bmx"
  5. Graphics 800,600, 0
  6. SetBlend alphablend
  7. Run(New SliderCrank.Create(), New TSettings)
  8. Type SliderCrank Extends Test
  9. Field m_leftWheel:b2Body
  10. Field m_rightWheel:b2Body
  11. Field m_vehicle:b2Body
  12. Field m_leftJoint:b2RevoluteJoint
  13. Field m_rightJoint:b2RevoluteJoint
  14. Method Create:SliderCrank()
  15. Init(8, 8)
  16. ' car body
  17. Local poly1:b2PolygonDef = New b2PolygonDef
  18. Local poly2:b2PolygonDef = New b2PolygonDef
  19. ' bottom half
  20. Local vertices:b2Vec2[] = New b2Vec2[5]
  21. vertices[4] = Vec2(-2.2,-0.74)
  22. vertices[3] = Vec2(-2.2,0)
  23. vertices[2] = Vec2(1.0,0)
  24. vertices[1] = Vec2(2.2,-0.2)
  25. vertices[0] = Vec2(2.2,-0.74)
  26. poly1.SetVertices(vertices)
  27. poly1.SetFilterGroupIndex(-1)
  28. poly1.SetDensity(20.0)
  29. poly1.SetFriction(0.68)
  30. poly1.SetFilterGroupIndex(-1)
  31. ' top half
  32. vertices = New b2Vec2[4]
  33. vertices[3] = Vec2(-1.7,0)
  34. vertices[2] = Vec2(-1.3,0.7)
  35. vertices[1] = Vec2(0.5,0.74)
  36. vertices[0] = Vec2(1.0,0)
  37. poly2.SetVertices(vertices)
  38. poly2.SetFilterGroupIndex(-1)
  39. poly2.SetDensity(5.0)
  40. poly2.SetFriction(0.68)
  41. poly2.SetFilterGroupIndex(-1)
  42. Local bd:b2BodyDef = New b2BodyDef
  43. bd.SetPositionXY(-35.0, 2.8)
  44. m_vehicle = m_world.CreateBody(bd)
  45. m_vehicle.CreateShape(poly1)
  46. m_vehicle.CreateShape(poly2)
  47. m_vehicle.SetMassFromShapes()
  48. ' vehicle wheels
  49. Local circ:b2CircleDef = New b2CircleDef
  50. circ.SetDensity(40.0)
  51. circ.SetRadius(0.38608)
  52. circ.SetFriction(0.8)
  53. circ.SetFilterGroupIndex(-1)
  54. bd = New b2BodyDef
  55. bd.SetAllowSleep(False)
  56. bd.SetPositionXY(-33.8, 2.0)
  57. m_rightWheel = m_world.CreateBody(bd)
  58. m_rightWheel.CreateShape(circ)
  59. m_rightWheel.SetMassFromShapes()
  60. bd.SetPositionXY(-36.2, 2.0)
  61. m_leftWheel = m_world.CreateBody(bd)
  62. m_leftWheel.CreateShape(circ)
  63. m_leftWheel.SetMassFromShapes()
  64. ' join wheels to chassis
  65. Local anchor:b2Vec2 = New b2Vec2.Create()
  66. Local jd:b2RevoluteJointDef= New b2RevoluteJointDef
  67. jd.Initialize(m_vehicle, m_leftWheel, m_leftWheel.GetWorldCenter())
  68. jd.SetCollideConnected(False)
  69. jd.EnableMotor(True)
  70. jd.SetMaxMotorTorque(10.0)
  71. jd.SetMotorSpeed(0.0)
  72. m_leftJoint = b2RevoluteJoint(m_world.CreateJoint(jd))
  73. jd.Initialize(m_vehicle, m_rightWheel, m_rightWheel.GetWorldCenter())
  74. jd.SetCollideConnected(False)
  75. m_rightJoint = b2RevoluteJoint(m_world.CreateJoint(jd))
  76. ' ground
  77. Local box:b2PolygonDef = New b2PolygonDef
  78. box.SetAsBox(19.5, 0.5)
  79. box.SetFriction(0.62)
  80. bd = New b2BodyDef
  81. bd.SetPositionXY(-25.0, 1.0)
  82. Local ground:b2Body = m_world.CreateBody(bd)
  83. ground.CreateShape(box)
  84. ' more ground
  85. box = New b2PolygonDef
  86. bd = New b2BodyDef
  87. box.SetAsOrientedBox(9.5, 0.5, b2Vec2.ZERO, 18)
  88. box.SetFriction(0.62)
  89. bd.SetPositionXY(27.0 - 30.0, 3.1)
  90. ground = m_world.CreateBody(bd)
  91. ground.CreateShape(box)
  92. ' more ground
  93. box = New b2PolygonDef
  94. bd = New b2BodyDef
  95. box.SetAsOrientedBox(9.5, 0.5, b2Vec2.ZERO, -18)
  96. box.SetFriction(0.62)
  97. bd.SetPositionXY(55.0 - 30.0, 3.1)
  98. ground = m_world.CreateBody(bd)
  99. ground.CreateShape(box)
  100. ' more ground
  101. box = New b2PolygonDef
  102. bd = New b2BodyDef
  103. box.SetAsOrientedBox(9.5, 0.5, b2Vec2.ZERO, 5.4)
  104. box.SetFriction(0.62)
  105. bd.SetPositionXY(41.0, 2.0)
  106. ground = m_world.CreateBody(bd)
  107. ground.CreateShape(box)
  108. ' more ground
  109. box = New b2PolygonDef
  110. bd = New b2BodyDef
  111. box.SetAsOrientedBox(5.0, 0.5, b2Vec2.ZERO, 27)
  112. box.SetFriction(0.62)
  113. bd.SetPositionXY(50.0, 4.0)
  114. ground = m_world.CreateBody(bd)
  115. ground.CreateShape(box)
  116. ' more ground
  117. box = New b2PolygonDef
  118. bd = New b2BodyDef
  119. box.SetAsBox(20.0, 0.5)
  120. box.SetFriction(0.62)
  121. bd.SetPositionXY(85.0, 2.0)
  122. ground = m_world.CreateBody(bd)
  123. ground.CreateShape(box)
  124. Return Self
  125. End Method
  126. Method Keyboard()
  127. Super.Keyboard()
  128. If KeyHit(KEY_A) Then
  129. m_leftJoint.SetMaxMotorTorque(800.0)
  130. m_leftJoint.SetMotorSpeed(687.549354)
  131. End If
  132. If KeyHit(KEY_S) Then
  133. m_leftJoint.SetMaxMotorTorque(100.0)
  134. m_leftJoint.SetMotorSpeed(0.0)
  135. End If
  136. If KeyHit(KEY_D) Then
  137. m_leftJoint.SetMaxMotorTorque(1200.0)
  138. m_leftJoint.SetMotorSpeed(-2062.64806)
  139. End If
  140. End Method
  141. Method DoStep(settings:TSettings)
  142. Super.DoStep(settings)
  143. DrawString "Keys: left = a, brake = s, right = d", 5, m_textLine
  144. m_textLine :+ 12
  145. End Method
  146. End Type