Shuvit game master repo. http://shuvit.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ragdoll.py 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. import bge
  2. from math import radians
  3. from mathutils import Quaternion, Vector, Matrix
  4. armature = "Char4"
  5. def get_bone_pose_matrix_cleaned(bone):
  6. offset_m4 = (Matrix.Translation(bone.location) * Quaternion(bone.rotation_quaternion).to_matrix().to_4x4())
  7. return bone.pose_matrix * offset_m4.inverted()
  8. def zero_constraints(own, scene):
  9. armature = scene.objects['Char4']
  10. for const in armature.constraints:
  11. if 'rd_cl' in str(const):
  12. const.enforce = 0
  13. print(const)
  14. if 'rd_cr' in str(const):
  15. const.enforce = 0
  16. for x in own['rd_rb'].groupMembers:
  17. x.suspendDynamics(True)
  18. def update_rb(physBody, scene):
  19. armature = scene.objects['Char4']
  20. master = armature.parent
  21. master = scene.objects['control_cube.002']
  22. for pB in physBody.groupMembers:
  23. if 'bone' in pB:
  24. bone_name = pB["bone"]
  25. bone = armature.channels[bone_name]
  26. pos = (bone.pose_head)+master.worldPosition
  27. rot = Vector(master.worldOrientation.to_euler()) * Vector(bone.channel_matrix.to_euler())
  28. pose_bone = bone
  29. obj = armature
  30. matrix_final = obj.worldTransform * get_bone_pose_matrix_cleaned(bone)
  31. pB.worldTransform = matrix_final
  32. def max_constraints(own, scene, physBody):
  33. armature = scene.objects['Char4']
  34. if armature.parent != None:
  35. master = armature.parent
  36. if not armature.parent.isSuspendDynamics:
  37. pass
  38. armature.removeParent()
  39. physBody.restoreDynamics()
  40. for pB in physBody.groupMembers:
  41. pB.restoreDynamics()
  42. if 'bone' in pB:
  43. bone_name = pB["bone"]
  44. bone = armature.channels[bone_name]
  45. if 'init' not in pB:
  46. pB["origin_pos"] = pB.worldPosition
  47. pB["origin_rot"] = Vector(pB.worldOrientation.to_euler())
  48. pos = (bone.pose_head)+master.worldPosition
  49. rot = Vector(bone.channel_matrix.to_euler())
  50. for const in armature.constraints:
  51. if pB['bone'] in str(const) and ('rd_cl' in str(const) or 'rd_cr' in str(const)):
  52. const.target = pB
  53. const.enforce = 1
  54. #print('enforcing', pB, const)
  55. else:
  56. pass
  57. else:
  58. #print('no bone in', pB)
  59. pass
  60. def main():
  61. cont = bge.logic.getCurrentController()
  62. own = cont.owner
  63. scene = bge.logic.getCurrentScene()
  64. sens = cont.sensors['rd_key']
  65. dict = bge.logic.globalDict
  66. if 'rdinited' not in own:
  67. own['rdinited'] = True
  68. own['ragdoll_active'] = False
  69. pB = scene.objectsInactive["npc_ed_arm_physBody"]
  70. own['rd_rb'] = scene.addObject(pB)
  71. own['rd_rb'].suspendDynamics()
  72. own['rd_set_vel'] = False
  73. own['rd_vel'] = None
  74. own['rd_incer'] = 0
  75. zero_constraints(own, scene)
  76. #if (sens.positive) or (dict['aBut'] == 1 and dict['bBut'] == 1) or own['fall'] == True:
  77. if (sens.positive) or own['fall'] == True:
  78. #print('option1')
  79. own['fall'] = False
  80. if own['ragdoll_active'] == False:
  81. #own['fall'] = 1
  82. own['fall'] = True
  83. print('do ragdoll')
  84. incer = -1
  85. update_rb(own['rd_rb'], scene)
  86. # while incer < 20:
  87. # incer += 1
  88. # scene.objects[armature].stopAction(incer)
  89. pB = scene.objectsInactive["npc_ed_arm_physBody"]
  90. physBody = own['rd_rb']
  91. physBody.worldPosition = own.worldPosition
  92. physBody.worldOrientation = own.worldOrientation
  93. own['rd_vel'] = own.worldLinearVelocity.copy()
  94. print('setting linvel', own['rd_vel'])
  95. own['rd_set_vel'] = True
  96. physBody.name = 'ragdollll'
  97. physBody['to_delete'] = True
  98. own['rd_to_delete'] = physBody
  99. own['ragdoll_active'] = True
  100. max_constraints(own, scene, physBody)
  101. own.visible = False
  102. #own.visible = True
  103. own['throw_deck'] = False
  104. cont.actuators['Camera'].height = 2.0
  105. #cont.actuators['Camera'].min = 14.0
  106. #cont.actuators['Camera'].max = 18.0
  107. cont.actuators['Camera'].damping = .000001
  108. cont.activate(cont.actuators['Camera'])
  109. own.suspendDynamics(True)
  110. else:
  111. if own['ragdoll_active'] == True:
  112. #pass
  113. own['rd_incer'] += 1
  114. # scene.objects[armature].stopAction(1)
  115. # scene.objects[armature].stopAction(2)
  116. # scene.objects[armature].stopAction(3)
  117. # scene.objects[armature].stopAction(7)
  118. #scene.objects[armature].update()
  119. #cont.actuators['Camera'].object = scene.objects['ragdoll_parent']
  120. scene.objects['control_cube.002'].worldPosition = scene.objects['control_cube.002'].worldPosition.lerp(scene.objects['ragdoll_parent'].worldPosition, .01)
  121. #scene.objects['control_cube.002'].worldPosition = scene.objects['ragdoll_parent'].worldPosition
  122. #print('doing the active thing')
  123. wp = scene.objects['camCube'].worldPosition - scene.objects['ragdoll_parent'].worldPosition
  124. wp = wp * .9
  125. cont.actuators['Camera'].height = 2.0
  126. cont.actuators['Camera'].min = 4.0
  127. cont.actuators['Camera'].max = 8.0
  128. cont.actuators['Camera'].damping = .000001
  129. cont.activate(cont.actuators['Camera'])
  130. #print(own['rd_set_vel'], 'vel setter')
  131. if own['stance']:
  132. own['requestAction'] = 'reg_walk'
  133. else:
  134. own['requestAction'] = 'fak_walk'
  135. if own['rd_set_vel'] == True:
  136. #print(own['rd_incer'], 'rdincer', own['rd_vel'])
  137. scene.objects['npc_ed_arm_phys_master'].setLinearVelocity(own['rd_vel'] * 20, False)
  138. own['rd_set_vel'] = False
  139. if own['rd_incer'] > 20:
  140. own['rd_incer'] += 1
  141. #print(
  142. #scene.objects['npc_ed_arm_phys_master'].setLinearVelocity(own['rd_vel'] * 4000, False)
  143. #scene.objects['npc_ed_arm_phys_master'].worldLinearVelocity.x = 100
  144. #scene.objects['npc_ed_arm_phys_master'].applyForce([5000,0,0], False)
  145. for pB in own['rd_rb'].groupMembers:
  146. #print('setting', pB, own['rd_vel'])
  147. try:
  148. #pass
  149. #pB.applyForce([5000,0,500], False)
  150. #pB.worldLinearVelocity.x = 100
  151. #pB.worldLinearVelocity = own['rd_vel']
  152. pB.setLinearVelocity(own['rd_vel'], False)
  153. #print('adding force to', pB, pB.worldLinearVelocity)
  154. except:
  155. print('cant add force to ', pB)
  156. else:
  157. own['rd_incer'] = 0
  158. own['rd_set_vel'] = False
  159. if dict['yBut'] == 1 or dict['kb_q'] == 2:
  160. print('turn off ragdoll')
  161. zero_constraints(own, scene)
  162. scene.objects[armature].setParent(own, False, True)
  163. own.restoreDynamics()
  164. for x in own['rd_rb'].groupMembers:
  165. x.suspendDynamics(True)
  166. own['ragdoll_active'] = False
  167. #print(scene.objects[armature].parent, 'parent')
  168. cont.actuators['Camera'].object = scene.objects['camCube']
  169. cont.activate(cont.actuators['Camera'])
  170. own.worldPosition = scene.objects['npc_ed_arm_phys_master'].worldPosition
  171. own.worldPosition.z += .8#4
  172. cont.activate(cont.actuators['walk'])
  173. own['getonboard'] = True
  174. dict['walk'] = 0
  175. own['fall'] = False
  176. if dict['lUD'] < -.08 and own['rd_incer'] > 200:
  177. physBody = own['rd_rb']
  178. excludes = ['npc_ed_arm_phys_hand.R', 'npc_ed_arm_phys_hand.L', 'npc_ed_arm_phys_forearm.fk.L', 'npc_ed_arm_phys_forearm.fk.R', 'npc_ed_arm_phys_upper_arm.fk.L', 'npc_ed_arm_phys_upper_arm.fk.R']
  179. #excludes = []
  180. # for x in physBody.groupMembers:
  181. # print(x.name)
  182. # if x.name not in excludes:
  183. # #pass
  184. # x.applyForce([0,0,15], False)
  185. # else:
  186. # x.applyForce([0,0,100], False)
  187. # #pass
  188. # scene.objects['npc_ed_arm_phys_master'].applyForce([0,0,500], False)
  189. scene.objects['npc_ed_arm_phys_head'].applyForce([0,0,400], False)
  190. # scene.objects['npc_ed_arm_phys_chest'].applyForce([0,0,200], False)
  191. scene.objects['npc_ed_arm_phys_chest-1'].applyForce([0,0,200], False)
  192. # scene.objects['npc_ed_arm_phys_spine'].applyForce([0,0,200], False)
  193. scene.objects['npc_ed_arm_phys_neck'].applyForce([0,0,200], False)
  194. scene.objects['npc_ed_arm_phys_forearm.fk.L'].applyForce([0,0,30], False)
  195. scene.objects['npc_ed_arm_phys_forearm.fk.R'].applyForce([0,0,30], False)
  196. # scene.objects['npc_ed_arm_phys_deltoid.L'].applyForce([0,0,200], False)
  197. # scene.objects['npc_ed_arm_phys_deltoid.R'].applyForce([0,0,200], False)
  198. # scene.objects['npc_ed_arm_phys_clavicle.L'].applyForce([0,0,200], False)
  199. # scene.objects['npc_ed_arm_phys_clavicle.R'].applyForce([0,0,200], False)
  200. if dict['lLR'] < -.04:
  201. scene.objects['npc_ed_arm_phys_spine'].applyRotation([0,.1,0], True)
  202. scene.objects['npc_ed_arm_phys_head'].applyRotation([0,.1,0], True)
  203. if dict['lLR'] > .04:
  204. scene.objects['npc_ed_arm_phys_spine'].applyRotation([0,-.1,0], True)
  205. scene.objects['npc_ed_arm_phys_head'].applyRotation([0,-.1,0], True)
  206. # if dict['rUD'] > .08 and own['rd_incer'] > 200:
  207. # scene.objects['npc_ed_arm_phys_master'].applyForce([0,600,0], True)
  208. else:
  209. update_rb(own['rd_rb'], scene)
  210. #pass