Browse Source

adding ragdoll script

shuvit 5 years ago
parent
commit
dfba7c2c92
1 changed files with 201 additions and 0 deletions
  1. 201
    0
      scripts/ragdoll.py

+ 201
- 0
scripts/ragdoll.py View File

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

Loading…
Cancel
Save