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

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