01_basics.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #!/usr/bin/env python
  2. #from panda3d.core import load_prc_file_data
  3. #load_prc_file_data('', 'bullet-additional-damping true')
  4. #load_prc_file_data('', 'bullet-additional-damping-linear-factor 0.005')
  5. #load_prc_file_data('', 'bullet-additional-damping-angular-factor 0.01')
  6. #load_prc_file_data('', 'bullet-additional-damping-linear-threshold 0.01')
  7. #load_prc_file_data('', 'bullet-additional-damping-angular-threshold 0.01')
  8. import sys
  9. from direct.showbase.ShowBase import ShowBase
  10. from direct.showbase.InputStateGlobal import inputState
  11. from panda3d.core import AmbientLight
  12. from panda3d.core import DirectionalLight
  13. from panda3d.core import LVector3
  14. from panda3d.core import LPoint3
  15. from panda3d.core import TransformState
  16. from panda3d.core import BitMask32
  17. from panda3d.bullet import BulletWorld
  18. from panda3d.bullet import BulletPlaneShape
  19. from panda3d.bullet import BulletBoxShape
  20. from panda3d.bullet import BulletRigidBodyNode
  21. from panda3d.bullet import BulletDebugNode
  22. class Game(ShowBase):
  23. def __init__(self):
  24. ShowBase.__init__(self)
  25. base.set_background_color(0.1, 0.1, 0.8, 1)
  26. base.set_frame_rate_meter(True)
  27. base.cam.set_pos(0, -20, 4)
  28. base.cam.look_at(0, 0, 0)
  29. # Light
  30. alight = AmbientLight('ambientLight')
  31. alight.set_color((0.5, 0.5, 0.5, 1))
  32. alightNP = render.attach_new_node(alight)
  33. dlight = DirectionalLight('directionalLight')
  34. dlight.set_direction((1, 1, -1))
  35. dlight.set_color((0.7, 0.7, 0.7, 1))
  36. dlightNP = render.attach_new_node(dlight)
  37. render.clear_light()
  38. render.set_light(alightNP)
  39. render.set_light(dlightNP)
  40. # Input
  41. self.accept('escape', self.do_exit)
  42. self.accept('r', self.do_reset)
  43. self.accept('f1', base.toggle_wireframe)
  44. self.accept('f2', base.toggle_texture)
  45. self.accept('f3', self.toggle_debug)
  46. self.accept('f5', self.do_screenshot)
  47. inputState.watchWithModifiers('forward', 'w')
  48. inputState.watchWithModifiers('left', 'a')
  49. inputState.watchWithModifiers('reverse', 's')
  50. inputState.watchWithModifiers('right', 'd')
  51. inputState.watchWithModifiers('turnLeft', 'q')
  52. inputState.watchWithModifiers('turnRight', 'e')
  53. # Task
  54. taskMgr.add(self.update, 'updateWorld')
  55. # Physics
  56. self.setup()
  57. def do_exit(self):
  58. self.cleanup()
  59. sys.exit(1)
  60. def do_reset(self):
  61. self.cleanup()
  62. self.setup()
  63. def toggle_debug(self):
  64. if self.debugNP.is_hidden():
  65. self.debugNP.show()
  66. else:
  67. self.debugNP.hide()
  68. def do_screenshot(self):
  69. base.screenshot('Bullet')
  70. def process_input(self, dt):
  71. force = LVector3(0, 0, 0)
  72. torque = LVector3(0, 0, 0)
  73. if inputState.isSet('forward'): force.y = 1.0
  74. if inputState.isSet('reverse'): force.y = -1.0
  75. if inputState.isSet('left'): force.x = -1.0
  76. if inputState.isSet('right'): force.x = 1.0
  77. if inputState.isSet('turnLeft'): torque.z = 1.0
  78. if inputState.isSet('turnRight'): torque.z = -1.0
  79. force *= 30.0
  80. torque *= 10.0
  81. force = render.get_relative_vector(self.boxNP, force)
  82. torque = render.get_relative_vector(self.boxNP, torque)
  83. self.boxNP.node().set_active(True)
  84. self.boxNP.node().apply_central_force(force)
  85. self.boxNP.node().apply_torque(torque)
  86. def update(self, task):
  87. dt = globalClock.get_dt()
  88. self.process_input(dt)
  89. #self.world.doPhysics(dt)
  90. self.world.do_physics(dt, 5, 1.0/180.0)
  91. return task.cont
  92. def cleanup(self):
  93. self.world.remove(self.groundNP.node())
  94. self.world.remove(self.boxNP.node())
  95. self.world = None
  96. self.debugNP = None
  97. self.groundNP = None
  98. self.boxNP = None
  99. self.worldNP.remove_node()
  100. def setup(self):
  101. self.worldNP = render.attach_new_node('World')
  102. # World
  103. self.debugNP = self.worldNP.attach_new_node(BulletDebugNode('Debug'))
  104. self.debugNP.show()
  105. self.debugNP.node().show_wireframe(True)
  106. self.debugNP.node().show_constraints(True)
  107. self.debugNP.node().show_bounding_boxes(False)
  108. self.debugNP.node().show_normals(True)
  109. #self.debugNP.show_tight_bounds()
  110. #self.debugNP.show_bounds()
  111. self.world = BulletWorld()
  112. self.world.set_gravity(0, 0, -9.81)
  113. self.world.set_debug_node(self.debugNP.node())
  114. # Ground (static)
  115. shape = BulletPlaneShape((0, 0, 1), 1)
  116. self.groundNP = self.worldNP.attach_new_node(BulletRigidBodyNode('Ground'))
  117. self.groundNP.node().add_shape(shape)
  118. self.groundNP.set_pos(0, 0, -2)
  119. self.groundNP.set_collide_mask(BitMask32.all_on())
  120. self.world.attach(self.groundNP.node())
  121. # Box (dynamic)
  122. shape = BulletBoxShape((0.5, 0.5, 0.5))
  123. self.boxNP = self.worldNP.attach_new_node(BulletRigidBodyNode('Box'))
  124. self.boxNP.node().set_mass(1.0)
  125. self.boxNP.node().add_shape(shape)
  126. self.boxNP.set_pos(0, 0, 2)
  127. #self.boxNP.set_scale(2, 1, 0.5)
  128. self.boxNP.set_collide_mask(BitMask32.all_on())
  129. #self.boxNP.node().set_deactivation_enabled(False)
  130. self.world.attach(self.boxNP.node())
  131. visualNP = loader.load_model('models/box.egg')
  132. visualNP.clear_model_nodes()
  133. visualNP.reparent_to(self.boxNP)
  134. # Bullet nodes should survive a flatten operation!
  135. #self.worldNP.flatten_strong()
  136. #render.ls()
  137. game = Game()
  138. game.run()