23_softbody_pressure.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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.core import GeomNode
  12. from panda3d.bullet import BulletWorld
  13. from panda3d.bullet import BulletHelper
  14. from panda3d.bullet import BulletPlaneShape
  15. from panda3d.bullet import BulletBoxShape
  16. from panda3d.bullet import BulletRigidBodyNode
  17. from panda3d.bullet import BulletDebugNode
  18. from panda3d.bullet import BulletTriangleMesh
  19. from panda3d.bullet import BulletTriangleMeshShape
  20. from panda3d.bullet import BulletSoftBodyNode
  21. from panda3d.bullet import BulletSoftBodyConfig
  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, -60, 20)
  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. # 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 update(self, task):
  65. dt = globalClock.get_dt()
  66. self.world.do_physics(dt, 10, 0.008)
  67. return task.cont
  68. def cleanup(self):
  69. self.world = None
  70. self.worldNP.remove_node()
  71. def setup(self):
  72. self.worldNP = render.attach_new_node('World')
  73. # World
  74. self.debugNP = self.worldNP.attach_new_node(BulletDebugNode('Debug'))
  75. self.debugNP.show()
  76. #self.debugNP.show_tight_bounds()
  77. #self.debugNP.show_bounds()
  78. self.world = BulletWorld()
  79. self.world.set_gravity((0, 0, -9.81))
  80. self.world.set_debug_node(self.debugNP.node())
  81. # Ground
  82. p0 = LPoint3(-20, -20, 0)
  83. p1 = LPoint3(-20, 20, 0)
  84. p2 = LPoint3(20, -20, 0)
  85. p3 = LPoint3(20, 20, 0)
  86. mesh = BulletTriangleMesh()
  87. mesh.add_triangle(p0, p1, p2)
  88. mesh.add_triangle(p1, p2, p3)
  89. shape = BulletTriangleMeshShape(mesh, dynamic=False)
  90. np = self.worldNP.attach_new_node(BulletRigidBodyNode('Mesh'))
  91. np.node().add_shape(shape)
  92. np.set_pos(0, 0, -2)
  93. np.set_collide_mask(BitMask32.all_on())
  94. self.world.attach(np.node())
  95. # Stair
  96. origin = LPoint3(0, 0, 0)
  97. size = LVector3(2, 10, 1)
  98. shape = BulletBoxShape(size * 0.5)
  99. for i in range(10):
  100. pos = origin + size * i
  101. pos.setY(0)
  102. np = self.worldNP.attach_new_node(
  103. BulletRigidBodyNode('Stair{}'.format(i)))
  104. np.node().add_shape(shape)
  105. np.set_pos(pos)
  106. np.set_collide_mask(BitMask32.all_on())
  107. npV = loader.load_model('models/box.egg')
  108. npV.reparent_to(np)
  109. npV.set_scale(size)
  110. self.world.attach(np.node())
  111. # Soft body world information
  112. info = self.world.get_world_info()
  113. info.set_air_density(1.2)
  114. info.set_water_density(0)
  115. info.set_water_offset(0)
  116. info.set_water_normal((0, 0, 0))
  117. # Softbody
  118. center = LPoint3(0, 0, 0)
  119. radius = LVector3(1, 1, 1) * 1.5
  120. node = BulletSoftBodyNode.make_ellipsoid(info, center, radius, 128)
  121. node.set_name('Ellipsoid')
  122. node.get_material(0).set_linear_stiffness(0.1)
  123. node.get_cfg().set_dynamic_friction_coefficient(1)
  124. node.get_cfg().set_damping_coefficient(0.001)
  125. node.get_cfg().set_pressure_coefficient(1500)
  126. node.set_total_mass(30, True)
  127. node.set_pose(True, False)
  128. np = self.worldNP.attach_new_node(node)
  129. np.set_pos(15, 0, 12)
  130. #np.setH(90.0)
  131. #np.show_bounds()
  132. #np.show_tight_bounds()
  133. self.world.attach(np.node())
  134. geom = BulletHelper.make_geom_from_faces(node)
  135. node.link_geom(geom)
  136. nodeV = GeomNode('EllipsoidVisual')
  137. nodeV.add_geom(geom)
  138. npV = np.attach_new_node(nodeV)
  139. #npV.show_bounds()
  140. game = Game()
  141. game.run()