22_softbody_cloth.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 LPoint3
  8. from panda3d.core import TransformState
  9. from panda3d.core import BitMask32
  10. from panda3d.core import GeomNode
  11. from panda3d.core import GeomVertexFormat
  12. from panda3d.core import GeomVertexRewriter
  13. from panda3d.core import InternalName
  14. from panda3d.core import NurbsSurfaceEvaluator
  15. from panda3d.core import SheetNode
  16. from panda3d.bullet import BulletWorld
  17. from panda3d.bullet import BulletHelper
  18. from panda3d.bullet import BulletPlaneShape
  19. from panda3d.bullet import BulletBoxShape
  20. from panda3d.bullet import BulletRigidBodyNode
  21. from panda3d.bullet import BulletDebugNode
  22. from panda3d.bullet import BulletTriangleMesh
  23. from panda3d.bullet import BulletTriangleMeshShape
  24. from panda3d.bullet import BulletSoftBodyNode
  25. from panda3d.bullet import BulletSoftBodyConfig
  26. class Game(ShowBase):
  27. def __init__(self):
  28. ShowBase.__init__(self)
  29. base.set_background_color(0.1, 0.1, 0.8, 1)
  30. base.set_frame_rate_meter(True)
  31. base.cam.set_pos(0, -40, 10)
  32. base.cam.look_at(0, 0, 0)
  33. # Light
  34. alight = AmbientLight('ambientLight')
  35. alight.set_color((0.5, 0.5, 0.5, 1))
  36. alightNP = render.attach_new_node(alight)
  37. dlight = DirectionalLight('directionalLight')
  38. dlight.set_direction((5, 0, -2))
  39. dlight.set_color((0.7, 0.7, 0.7, 1))
  40. dlightNP = render.attach_new_node(dlight)
  41. render.clear_light()
  42. render.set_light(alightNP)
  43. render.set_light(dlightNP)
  44. # Input
  45. self.accept('escape', self.do_exit)
  46. self.accept('r', self.do_reset)
  47. self.accept('f1', base.toggle_wireframe)
  48. self.accept('f2', base.toggle_texture)
  49. self.accept('f3', self.toggle_debug)
  50. self.accept('f5', self.do_screenshot)
  51. # Task
  52. taskMgr.add(self.update, 'updateWorld')
  53. # Physics
  54. self.setup()
  55. def do_exit(self):
  56. self.cleanup()
  57. sys.exit(1)
  58. def do_reset(self):
  59. self.cleanup()
  60. self.setup()
  61. def toggle_debug(self):
  62. if self.debugNP.is_hidden():
  63. self.debugNP.show()
  64. else:
  65. self.debugNP.hide()
  66. def do_screenshot(self):
  67. base.screenshot('Bullet')
  68. def update(self, task):
  69. dt = globalClock.get_dt()
  70. self.world.do_physics(dt, 10, 0.004)
  71. return task.cont
  72. def cleanup(self):
  73. self.world = None
  74. self.worldNP.remove_node()
  75. def setup(self):
  76. self.worldNP = render.attach_new_node('World')
  77. # World
  78. self.debugNP = self.worldNP.attach_new_node(BulletDebugNode('Debug'))
  79. self.debugNP.show()
  80. self.world = BulletWorld()
  81. self.world.set_gravity((0, 0, -9.81))
  82. self.world.set_debug_node(self.debugNP.node())
  83. # Box
  84. shape = BulletBoxShape((1.0, 1.0, 1.0))
  85. boxNP = self.worldNP.attach_new_node(BulletRigidBodyNode('Box'))
  86. boxNP.node().set_mass(150.0)
  87. boxNP.node().add_shape(shape)
  88. boxNP.set_pos(0, 0, 2)
  89. boxNP.set_collide_mask(BitMask32.all_on())
  90. self.world.attach(boxNP.node())
  91. visualNP = loader.load_model('models/box.egg')
  92. visualNP.clear_model_nodes()
  93. visualNP.set_scale(2.0)
  94. visualNP.reparent_to(boxNP)
  95. # Soft body world information
  96. info = self.world.get_world_info()
  97. info.set_air_density(1.2)
  98. info.set_water_density(0)
  99. info.set_water_offset(0)
  100. info.set_water_normal((0, 0, 0))
  101. # Softbody
  102. nx = 31
  103. ny = 31
  104. p00 = LPoint3(-8, -8, 0)
  105. p10 = LPoint3( 8, -8, 0)
  106. p01 = LPoint3(-8, 8, 0)
  107. p11 = LPoint3( 8, 8, 0)
  108. bodyNode = BulletSoftBodyNode.make_patch(
  109. info, p00, p10, p01, p11, nx, ny, 1+2+4+8, True)
  110. material = bodyNode.append_material()
  111. material.set_linear_stiffness(0.4)
  112. #material.set_angular_stiffness(2.)
  113. bodyNode.generate_bending_constraints(2, material);
  114. bodyNode.set_total_mass(50.0)
  115. bodyNode.get_shape(0).set_margin(0.5)
  116. bodyNP = self.worldNP.attach_new_node(bodyNode)
  117. self.world.attach(bodyNode)
  118. # Rendering with Geom:
  119. fmt = GeomVertexFormat.get_v3n3t2()
  120. geom = BulletHelper.make_geom_from_faces(bodyNode, fmt, True)
  121. bodyNode.link_geom(geom)
  122. visNode = GeomNode('')
  123. visNode.add_geom(geom)
  124. visNP = bodyNP.attach_new_node(visNode)
  125. # Now we want to have a texture and texture coordinates.
  126. # The geom's format has already a column for texcoords, so we just need
  127. # to write texcoords using a GeomVertexRewriter.
  128. tex = loader.load_texture('models/panda.jpg')
  129. visNP.set_texture(tex)
  130. BulletHelper.make_texcoords_for_patch(geom, nx, ny)
  131. # Rendering with NURBS (please reduce nx & ny to 12):
  132. #surface = NurbsSurfaceEvaluator()
  133. #surface.reset(nx, ny)
  134. #bodyNode.link_surface(surface)
  135. #visNode = SheetNode('')
  136. #visNode.set_surface(surface)
  137. #visNode.set_num_u_subdiv(2)
  138. #visNode.set_num_v_subdiv(2)
  139. #visNP = self.worldNP.attach_new_node(visNode)
  140. #visNP.setTwoSided(True)
  141. game = Game()
  142. game.run()