04_filtering.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 LPoint3
  9. from panda3d.core import TransformState
  10. from panda3d.core import BitMask32
  11. from panda3d.bullet import BulletWorld
  12. from panda3d.bullet import BulletPlaneShape
  13. from panda3d.bullet import BulletBoxShape
  14. from panda3d.bullet import BulletRigidBodyNode
  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. return task.cont
  83. def cleanup(self):
  84. self.world = None
  85. self.worldNP.remove_node()
  86. def setup(self):
  87. self.worldNP = render.attach_new_node('World')
  88. # World
  89. self.debugNP = self.worldNP.attach_new_node(BulletDebugNode('Debug'))
  90. self.debugNP.show()
  91. self.world = BulletWorld()
  92. self.world.set_gravity((0, 0, -9.81))
  93. self.world.set_debug_node(self.debugNP.node())
  94. # Ground
  95. shape = BulletPlaneShape((0, 0, 1), -1)
  96. mask = BitMask32(0x0f)
  97. print('ground: ', mask)
  98. np = self.worldNP.attach_new_node(BulletRigidBodyNode('Ground'))
  99. np.node().add_shape(shape)
  100. np.set_pos(0, 0, -1)
  101. np.set_collide_mask(mask)
  102. self.world.attach(np.node())
  103. ## Box 1
  104. #shape = BulletBoxShape((0.5, 0.5, 0.5))
  105. #mask = BitMask32.allOff()
  106. #print('box-1: ', mask)
  107. #np = self.worldNP.attach_new_node(BulletRigidBodyNode('Box-1'))
  108. #np.node().set_mass(1.0)
  109. #np.node().add_shape(shape)
  110. #np.set_pos(-6, 0, 4)
  111. #np.set_collide_mask(mask)
  112. #self.world.attach(np.node())
  113. ## Box 2
  114. #shape = BulletBoxShape((0.5, 0.5, 0.5))
  115. #mask = BitMask32.bit(0)
  116. #print('box-2: ', mask)
  117. #np = self.worldNP.attach_new_node(BulletRigidBodyNode('Box-2'))
  118. #np.node().set_mass(1.0)
  119. #np.node().add_shape(shape)
  120. #np.set_pos(-2, 0, 4)
  121. #np.set_collide_mask(mask)
  122. #self.world.attach(np.node())
  123. ## Box 3
  124. #shape = BulletBoxShape((0.5, 0.5, 0.5))
  125. #mask = BitMask32.bit(2)
  126. #print('box-3: ', mask)
  127. #np = self.worldNP.attach_new_node(BulletRigidBodyNode('Box-3'))
  128. #np.node().set_mass(1.0)
  129. #np.node().add_shape(shape)
  130. #np.set_pos(2, 0, 4)
  131. #np.set_collide_mask(mask)
  132. #self.world.attach(np.node())
  133. # Box 4
  134. shape = BulletBoxShape((0.5, 0.5, 0.5))
  135. mask = BitMask32(0x3)
  136. print('box-4: ', mask)
  137. np = self.worldNP.attach_new_node(BulletRigidBodyNode('Box-4'))
  138. np.node().set_mass(1.0)
  139. np.node().add_shape(shape)
  140. np.set_pos(6, 0, 4)
  141. np.set_collide_mask(mask)
  142. self.world.attach(np.node())
  143. self.boxNP = np
  144. visualNP = loader.load_model('models/box.egg')
  145. visualNP.reparent_to(self.boxNP)
  146. #self.boxNP.set_collide_mask(BitMask32.allOff())
  147. #self.world.remove(self.boxNP.node())
  148. #self.world.attach(self.boxNP.node())
  149. game = Game()
  150. game.run()