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

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