16_ghost_local.py 5.0 KB

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