17_character.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #!/usr/bin/env python
  2. import sys
  3. from direct.actor.Actor import Actor
  4. from direct.showbase.ShowBase import ShowBase
  5. from direct.showbase.InputStateGlobal import inputState
  6. from panda3d.core import load_prc_file_data
  7. from panda3d.core import AmbientLight
  8. from panda3d.core import DirectionalLight
  9. from panda3d.core import LVector3
  10. from panda3d.core import TransformState
  11. from panda3d.core import BitMask32
  12. from panda3d.core import Filename
  13. from panda3d.core import PNMImage
  14. from panda3d.bullet import BulletWorld
  15. from panda3d.bullet import BulletPlaneShape
  16. from panda3d.bullet import BulletBoxShape
  17. from panda3d.bullet import BulletRigidBodyNode
  18. from panda3d.bullet import BulletDebugNode
  19. from panda3d.bullet import BulletCapsuleShape
  20. from panda3d.bullet import BulletCharacterControllerNode
  21. from panda3d.bullet import BulletHeightfieldShape
  22. from panda3d.bullet import BulletTriangleMesh
  23. from panda3d.bullet import BulletTriangleMeshShape
  24. from panda3d.bullet import ZUp
  25. class Game(ShowBase):
  26. def __init__(self):
  27. """
  28. Load some configuration variables, it's important for this to happen
  29. before the ShowBase is initialized
  30. """
  31. load_prc_file_data("", """
  32. sync-video #t
  33. ### add entries below if you are not running from an installation.
  34. #model-path /path/to/panda3d
  35. """)
  36. ShowBase.__init__(self)
  37. base.set_background_color(0.1, 0.1, 0.8, 1)
  38. base.set_frame_rate_meter(True)
  39. base.cam.set_pos(0, -20, 4)
  40. base.cam.look_at(0, 0, 0)
  41. # Light
  42. alight = AmbientLight('ambientLight')
  43. alight.set_color((0.5, 0.5, 0.5, 1))
  44. alightNP = render.attach_new_node(alight)
  45. dlight = DirectionalLight('directionalLight')
  46. dlight.set_direction((1, 1, -1))
  47. dlight.set_color((0.7, 0.7, 0.7, 1))
  48. dlightNP = render.attach_new_node(dlight)
  49. render.clear_light()
  50. render.set_light(alightNP)
  51. render.set_light(dlightNP)
  52. # Input
  53. self.accept('escape', self.do_exit)
  54. self.accept('r', self.do_reset)
  55. self.accept('f1', base.toggle_wireframe)
  56. self.accept('f2', base.toggle_texture)
  57. self.accept('f3', self.toggle_debug)
  58. self.accept('f5', self.do_screenshot)
  59. self.accept('space', self.do_jump)
  60. self.accept('c', self.do_crouch)
  61. inputState.watchWithModifiers('forward', 'w')
  62. inputState.watchWithModifiers('left', 'a')
  63. inputState.watchWithModifiers('reverse', 's')
  64. inputState.watchWithModifiers('right', 'd')
  65. inputState.watchWithModifiers('turnLeft', 'q')
  66. inputState.watchWithModifiers('turnRight', 'e')
  67. # Task
  68. taskMgr.add(self.update, 'updateWorld')
  69. # Physics
  70. self.setup()
  71. def do_exit(self):
  72. self.cleanup()
  73. sys.exit(1)
  74. def do_reset(self):
  75. self.cleanup()
  76. self.setup()
  77. def toggle_debug(self):
  78. if self.debugNP.is_hidden():
  79. self.debugNP.show()
  80. else:
  81. self.debugNP.hide()
  82. def do_screenshot(self):
  83. base.screenshot('Bullet')
  84. def do_jump(self):
  85. self.character.set_max_jump_height(5.0)
  86. self.character.set_jump_speed(8.0)
  87. self.character.do_jump()
  88. self.actorNP.play("jump")
  89. def do_crouch(self):
  90. self.crouching = not self.crouching
  91. sz = self.crouching and 0.6 or 1.0
  92. self.characterNP.set_scale((1, 1, sz))
  93. #self.character.get_shape().set_local_scale(LVector3(1, 1, sz))
  94. #self.characterNP.set_scale(LVector3(1, 1, sz) * 0.3048)
  95. #self.characterNP.set_pos(0, 0, -1 * sz)
  96. def process_input(self, dt):
  97. speed = LVector3(0, 0, 0)
  98. omega = 0.0
  99. if inputState.isSet('forward'): speed.y = 2.0
  100. if inputState.isSet('reverse'): speed.y = -2.0
  101. if inputState.isSet('left'): speed.x = -2.0
  102. if inputState.isSet('right'): speed.x = 2.0
  103. if inputState.isSet('turnLeft'): omega = 120.0
  104. if inputState.isSet('turnRight'): omega = -120.0
  105. self.character.set_angular_movement(omega)
  106. self.character.set_linear_movement(speed, True)
  107. def update(self, task):
  108. dt = globalClock.get_dt()
  109. self.process_input(dt)
  110. self.world.do_physics(dt, 4, 1./240.)
  111. return task.cont
  112. def cleanup(self):
  113. self.world = None
  114. self.worldNP.remove_node()
  115. def setup(self):
  116. self.worldNP = render.attach_new_node('World')
  117. # World
  118. self.debugNP = self.worldNP.attach_new_node(BulletDebugNode('Debug'))
  119. self.debugNP.show()
  120. self.world = BulletWorld()
  121. self.world.set_gravity((0, 0, -9.81))
  122. self.world.set_debug_node(self.debugNP.node())
  123. # Ground
  124. shape = BulletPlaneShape((0, 0, 1), 0)
  125. #img = PNMImage(Filename('models/elevation.png'))
  126. #shape = BulletHeightfieldShape(img, 1.0, ZUp)
  127. np = self.worldNP.attach_new_node(BulletRigidBodyNode('Ground'))
  128. np.node().add_shape(shape)
  129. np.set_pos(0, 0, -1)
  130. np.set_collide_mask(BitMask32.all_on())
  131. self.world.attach(np.node())
  132. # Box
  133. shape = BulletBoxShape((1.0, 3.0, 0.3))
  134. np = self.worldNP.attach_new_node(BulletRigidBodyNode('Box'))
  135. np.node().set_mass(10.0)
  136. np.node().add_shape(shape)
  137. np.set_pos(3, 0, 4)
  138. np.setH(20.0)
  139. np.set_collide_mask(BitMask32.all_on())
  140. self.world.attach(np.node())
  141. # Character
  142. self.crouching = False
  143. h = 1.75
  144. w = 0.4
  145. shape = BulletCapsuleShape(w, h - 2 * w, ZUp)
  146. self.character = BulletCharacterControllerNode(shape, 0.4, 'Player')
  147. self.characterNP = self.worldNP.attach_new_node(self.character)
  148. self.characterNP.set_pos(-2, 0, 14)
  149. self.characterNP.set_h(45)
  150. self.characterNP.set_collide_mask(BitMask32.all_on())
  151. self.world.attach(self.character)
  152. self.actorNP = Actor('../roaming-ralph/models/ralph.egg.pz',
  153. {'run' : '../roaming-ralph/models/ralph-run.egg.pz',
  154. 'walk' : '../roaming-ralph/models/ralph-walk.egg.pz'})
  155. self.actorNP.reparent_to(self.characterNP)
  156. self.actorNP.set_scale(0.3048) # 1ft = 0.3048m
  157. self.actorNP.setH(180)
  158. self.actorNP.set_pos(0, 0, -1)
  159. game = Game()
  160. game.run()