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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 update_rb(physBody, scene):
  20. #print('updating rigid body positions')
  21. armature = scene.objects['Char4']
  22. master = armature.parent
  23. master = scene.objects['control_cube.002']
  24. for pB in physBody.groupMembers:
  25. if 'bone' in pB:
  26. bone_name = pB["bone"]
  27. bone = armature.channels[bone_name]
  28. pos = (bone.pose_head)+master.worldPosition
  29. rot = Vector(master.worldOrientation.to_euler()) * Vector(bone.channel_matrix.to_euler())
  30. pose_bone = bone
  31. obj = armature
  32. matrix_final = obj.worldTransform * get_bone_pose_matrix_cleaned(bone)
  33. pB.worldTransform = matrix_final
  34. def max_constraints(own, scene, physBody):
  35. armature = scene.objects['Char4']
  36. if armature.parent != None:
  37. master = armature.parent
  38. if not armature.parent.isSuspendDynamics:
  39. #armature.parent.suspendDynamics(True)
  40. pass
  41. armature.removeParent()
  42. #print(armature.parent)
  43. #print(physBody.groupMembers)
  44. physBody.restoreDynamics()
  45. for pB in physBody.groupMembers:
  46. pB.restoreDynamics()
  47. if 'bone' in pB:
  48. bone_name = pB["bone"]
  49. bone = armature.channels[bone_name]
  50. if 'init' not in pB:
  51. pB["origin_pos"] = pB.worldPosition
  52. pB["origin_rot"] = Vector(pB.worldOrientation.to_euler())
  53. pos = (bone.pose_head)+master.worldPosition
  54. rot = Vector(bone.channel_matrix.to_euler())
  55. for const in armature.constraints:
  56. if pB['bone'] in str(const) and ('rd_cl' in str(const) or 'rd_cr' in str(const)):
  57. const.target = pB
  58. const.enforce = 1
  59. #print('enforcing', pB, const)
  60. else:
  61. pass
  62. else:
  63. #print('no bone in', pB)
  64. pass
  65. def main():
  66. #print('doing main')
  67. cont = bge.logic.getCurrentController()
  68. own = cont.owner
  69. scene = bge.logic.getCurrentScene()
  70. sens = cont.sensors['rd_key']
  71. dict = bge.logic.globalDict
  72. if 'inited' not in own:
  73. own['inited'] = True
  74. own['ragdoll_active'] = False
  75. pB = scene.objectsInactive["npc_ed_arm_physBody"]
  76. own['rd_rb'] = scene.addObject(pB)
  77. own['rd_rb'].suspendDynamics()
  78. own['rd_set_vel'] = False
  79. own['rd_vel'] = None
  80. own['rd_incer'] = 0
  81. zero_constraints(own, scene)
  82. #if (sens.positive) or (dict['aBut'] == 1 and dict['bBut'] == 1) or own['fall'] == True:
  83. if (sens.positive) or own['fall'] == True:
  84. #print('option1')
  85. #own['fall'] = True
  86. if own['ragdoll_active'] == False:
  87. #own['fall'] = 1
  88. own['fall'] = True
  89. print('do ragdoll')
  90. incer = -1
  91. update_rb(own['rd_rb'], scene)
  92. while incer < 20:
  93. incer += 1
  94. scene.objects[armature].stopAction(incer)
  95. pB = scene.objectsInactive["npc_ed_arm_physBody"]
  96. physBody = own['rd_rb']
  97. physBody.worldPosition = own.worldPosition
  98. physBody.worldOrientation = own.worldOrientation
  99. own['rd_vel'] = own.worldLinearVelocity.copy()
  100. print('setting linvel', own['rd_vel'])
  101. own['rd_set_vel'] = True
  102. physBody.name = 'ragdollll'
  103. physBody['to_delete'] = True
  104. own['rd_to_delete'] = physBody
  105. own['ragdoll_active'] = True
  106. max_constraints(own, scene, physBody)
  107. own.visible = False
  108. own['throw_deck'] = False
  109. cont.actuators['Camera'].height = 2.0
  110. #cont.actuators['Camera'].min = 14.0
  111. #cont.actuators['Camera'].max = 18.0
  112. cont.actuators['Camera'].damping = .000001
  113. cont.activate(cont.actuators['Camera'])
  114. #own.suspendDynamics()
  115. else:
  116. if own['ragdoll_active'] == True:
  117. #pass
  118. scene.objects[armature].stopAction(2)
  119. scene.objects[armature].stopAction(7)
  120. scene.objects[armature].update()
  121. cont.actuators['Camera'].object = scene.objects['ragdoll_parent']
  122. wp = scene.objects['camCube'].worldPosition - scene.objects['ragdoll_parent'].worldPosition
  123. wp = wp * .9
  124. cont.actuators['Camera'].height = 2.0
  125. cont.actuators['Camera'].min = 4.0
  126. cont.actuators['Camera'].max = 8.0
  127. cont.actuators['Camera'].damping = .000001
  128. cont.activate(cont.actuators['Camera'])
  129. #print(own['rd_set_vel'], 'vel setter')
  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:
  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:
  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:
  191. scene.objects['npc_ed_arm_phys_master'].applyForce([0,600,0], True)
  192. else:
  193. update_rb(own['rd_rb'], scene)
  194. #pass