2
0

15_ghost.py 4.7 KB

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