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 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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'] = True
  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['throw_deck'] = False
  103. cont.actuators['Camera'].height = 2.0
  104. #cont.actuators['Camera'].min = 14.0
  105. #cont.actuators['Camera'].max = 18.0
  106. cont.actuators['Camera'].damping = .000001
  107. cont.activate(cont.actuators['Camera'])
  108. #own.suspendDynamics()
  109. else:
  110. if own['ragdoll_active'] == True:
  111. #pass
  112. scene.objects[armature].stopAction(2)
  113. scene.objects[armature].stopAction(7)
  114. scene.objects[armature].update()
  115. #cont.actuators['Camera'].object = scene.objects['ragdoll_parent']
  116. #scene.objects['control_cube.002'].worldPosition = scene.objects['control_cube.002'].worldPosition.lerp(scene.objects['ragdoll_parent'].worldPosition, .01)
  117. scene.objects['control_cube.002'].worldPosition = scene.objects['ragdoll_parent'].worldPosition
  118. wp = scene.objects['camCube'].worldPosition - scene.objects['ragdoll_parent'].worldPosition
  119. wp = wp * .9
  120. cont.actuators['Camera'].height = 2.0
  121. cont.actuators['Camera'].min = 4.0
  122. cont.actuators['Camera'].max = 8.0
  123. cont.actuators['Camera'].damping = .000001
  124. cont.activate(cont.actuators['Camera'])
  125. #print(own['rd_set_vel'], 'vel setter')
  126. if own['stance']:
  127. own['requestAction'] = 'reg_walk'
  128. else:
  129. own['requestAction'] = 'fak_walk'
  130. if own['rd_set_vel'] == True:
  131. print(own['rd_incer'], 'rdincer', own['rd_vel'])
  132. scene.objects['npc_ed_arm_phys_master'].setLinearVelocity(own['rd_vel'] * 20, False)
  133. own['rd_set_vel'] = False
  134. if own['rd_incer'] > 20:
  135. own['rd_incer'] += 1
  136. #print(
  137. #scene.objects['npc_ed_arm_phys_master'].setLinearVelocity(own['rd_vel'] * 4000, False)
  138. #scene.objects['npc_ed_arm_phys_master'].worldLinearVelocity.x = 100
  139. #scene.objects['npc_ed_arm_phys_master'].applyForce([5000,0,0], False)
  140. for pB in own['rd_rb'].groupMembers:
  141. print('setting', pB, own['rd_vel'])
  142. try:
  143. #pass
  144. #pB.applyForce([5000,0,500], False)
  145. #pB.worldLinearVelocity.x = 100
  146. #pB.worldLinearVelocity = own['rd_vel']
  147. pB.setLinearVelocity(own['rd_vel'], False)
  148. print('adding force to', pB, pB.worldLinearVelocity)
  149. except:
  150. print('cant add force to ', pB)
  151. else:
  152. own['rd_incer'] = 0
  153. own['rd_set_vel'] = False
  154. if dict['yBut'] == 1 or dict['kb_q'] == 2:
  155. print('turn off ragdoll')
  156. zero_constraints(own, scene)
  157. scene.objects[armature].setParent(own, False, True)
  158. own.restoreDynamics()
  159. for x in own['rd_rb'].groupMembers:
  160. x.suspendDynamics(True)
  161. own['ragdoll_active'] = False
  162. print(scene.objects[armature].parent, 'parent')
  163. cont.actuators['Camera'].object = scene.objects['camCube']
  164. cont.activate(cont.actuators['Camera'])
  165. own.worldPosition = scene.objects['npc_ed_arm_phys_master'].worldPosition
  166. own.worldPosition.z += .4
  167. cont.activate(cont.actuators['walk'])
  168. if dict['lUD'] < -.08 and own['rd_incer'] > 200:
  169. physBody = own['rd_rb']
  170. 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']
  171. #excludes = []
  172. for x in physBody.groupMembers:
  173. print(x.name)
  174. if x.name not in excludes:
  175. pass
  176. #x.applyForce([0,0,15], False)
  177. else:
  178. #x.applyForce([0,0,100], False)
  179. pass
  180. scene.objects['npc_ed_arm_phys_master'].applyForce([0,0,500], False)
  181. scene.objects['npc_ed_arm_phys_head'].applyForce([0,0,400], False)
  182. scene.objects['npc_ed_arm_phys_chest'].applyForce([0,0,200], False)
  183. scene.objects['npc_ed_arm_phys_chest-1'].applyForce([0,0,200], False)
  184. scene.objects['npc_ed_arm_phys_spine'].applyForce([0,0,200], False)
  185. scene.objects['npc_ed_arm_phys_neck'].applyForce([0,0,200], False)
  186. scene.objects['npc_ed_arm_phys_deltoid.L'].applyForce([0,0,200], False)
  187. scene.objects['npc_ed_arm_phys_deltoid.R'].applyForce([0,0,200], False)
  188. scene.objects['npc_ed_arm_phys_clavicle.L'].applyForce([0,0,200], False)
  189. scene.objects['npc_ed_arm_phys_clavicle.R'].applyForce([0,0,200], False)
  190. if dict['rUD'] > .08 and own['rd_incer'] > 200:
  191. scene.objects['npc_ed_arm_phys_master'].applyForce([0,600,0], True)
  192. else:
  193. update_rb(own['rd_rb'], scene)
  194. #pass