20_bowl_and_eggs.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #!/usr/bin/env python
  2. import sys
  3. import random
  4. from direct.showbase.ShowBase import ShowBase
  5. from direct.showbase.InputStateGlobal import inputState
  6. from panda3d.core import AmbientLight
  7. from panda3d.core import DirectionalLight
  8. from panda3d.core import LVector3
  9. from panda3d.core import TransformState
  10. from panda3d.core import BitMask32
  11. from panda3d.bullet import BulletWorld
  12. from panda3d.bullet import BulletRigidBodyNode
  13. from panda3d.bullet import BulletDebugNode
  14. from panda3d.bullet import BulletPlaneShape
  15. from panda3d.bullet import BulletConvexHullShape
  16. from panda3d.bullet import BulletTriangleMesh
  17. from panda3d.bullet import BulletTriangleMeshShape
  18. from panda3d.bullet import ZUp
  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, -10, 5)
  25. base.cam.look_at(0, 0, 0.2)
  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('up', 'w')
  45. inputState.watchWithModifiers('left', 'a')
  46. inputState.watchWithModifiers('down', 's')
  47. inputState.watchWithModifiers('right', 'd')
  48. # Task
  49. taskMgr.add(self.update, 'updateWorld')
  50. # Physics
  51. self.setup()
  52. def do_exit(self):
  53. self.cleanup()
  54. sys.exit(1)
  55. def do_reset(self):
  56. self.cleanup()
  57. self.setup()
  58. def toggle_debug(self):
  59. if self.debugNP.is_hidden():
  60. self.debugNP.show()
  61. else:
  62. self.debugNP.hide()
  63. def do_screenshot(self):
  64. base.screenshot('Bullet')
  65. def process_input(self, dt):
  66. force = LVector3(0, 0, 0)
  67. if inputState.isSet('up'): force.y = 1.0
  68. if inputState.isSet('down'): force.y = -1.0
  69. if inputState.isSet('left'): force.x = -1.0
  70. if inputState.isSet('right'): force.x = 1.0
  71. force *= 300.0
  72. self.bowlNP.node().set_active(True)
  73. self.bowlNP.node().apply_central_force(force)
  74. def update(self, task):
  75. dt = globalClock.get_dt()
  76. self.process_input(dt)
  77. self.world.do_physics(dt)
  78. return task.cont
  79. def cleanup(self):
  80. self.world = None
  81. self.worldNP.remove_node()
  82. def setup(self):
  83. self.worldNP = render.attach_new_node('World')
  84. # World
  85. self.debugNP = self.worldNP.attach_new_node(BulletDebugNode('Debug'))
  86. self.debugNP.show()
  87. self.debugNP.node().show_wireframe(True)
  88. self.debugNP.node().show_constraints(True)
  89. self.debugNP.node().show_bounding_boxes(False)
  90. self.debugNP.node().show_normals(False)
  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), 0)
  96. body = BulletRigidBodyNode('Ground')
  97. bodyNP = self.worldNP.attach_new_node(body)
  98. bodyNP.node().add_shape(shape)
  99. bodyNP.set_pos(0, 0, 0)
  100. bodyNP.set_collide_mask(BitMask32.all_on())
  101. self.world.attach(bodyNP.node())
  102. # Bowl
  103. visNP = loader.load_model('models/bowl.egg')
  104. geom = (visNP.findAllMatches('**/+GeomNode')
  105. .get_path(0).node().get_geom(0))
  106. mesh = BulletTriangleMesh()
  107. mesh.addGeom(geom)
  108. shape = BulletTriangleMeshShape(mesh, dynamic=True)
  109. body = BulletRigidBodyNode('Bowl')
  110. bodyNP = self.worldNP.attach_new_node(body)
  111. bodyNP.node().add_shape(shape)
  112. bodyNP.node().set_mass(10.0)
  113. bodyNP.set_pos(0, 0, 0)
  114. bodyNP.set_collide_mask(BitMask32.all_on())
  115. self.world.attach(bodyNP.node())
  116. visNP.reparent_to(bodyNP)
  117. self.bowlNP = bodyNP
  118. self.bowlNP.set_scale(2)
  119. # Eggs
  120. self.eggNPs = []
  121. for i in range(5):
  122. x = random.gauss(0, 0.1)
  123. y = random.gauss(0, 0.1)
  124. z = random.gauss(0, 0.1) + 1
  125. h = random.random() * 360
  126. p = random.random() * 360
  127. r = random.random() * 360
  128. visNP = loader.load_model('models/egg.egg')
  129. geom = (visNP.find_all_matches('**/+GeomNode')
  130. .get_path(0).node().get_geom(0))
  131. shape = BulletConvexHullShape()
  132. shape.addGeom(geom)
  133. body = BulletRigidBodyNode('Egg-%i' % i)
  134. bodyNP = self.worldNP.attach_new_node(body)
  135. bodyNP.node().set_mass(1.0)
  136. bodyNP.node().add_shape(shape)
  137. bodyNP.node().set_deactivation_enabled(False)
  138. bodyNP.set_collide_mask(BitMask32.all_on())
  139. bodyNP.set_pos_hpr(x, y, z, h, p, r)
  140. #bodyNP.set_scale(1.5)
  141. self.world.attach(bodyNP.node())
  142. visNP.reparent_to(bodyNP)
  143. self.eggNPs.append(bodyNP)
  144. game = Game()
  145. game.run()