-
Notifications
You must be signed in to change notification settings - Fork 798
/
motionsensor.lua
261 lines (189 loc) · 6.82 KB
/
motionsensor.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
--
-- Should never really happen.
--
motionsensor = motionsensor or {}
--
-- These bones are used to draw the debug
-- kinect skeleton. You just need to loop through
-- and draw a line between each of these bones.
--
motionsensor.DebugBones =
{
-- Torso
{ SENSORBONE.HEAD, SENSORBONE.SHOULDER },
{ SENSORBONE.SHOULDER, SENSORBONE.SHOULDER_LEFT },
{ SENSORBONE.SHOULDER, SENSORBONE.SHOULDER_RIGHT },
{ SENSORBONE.SHOULDER, SENSORBONE.SPINE },
{ SENSORBONE.SHOULDER, SENSORBONE.HIP },
{ SENSORBONE.HIP, SENSORBONE.HIP_LEFT },
{ SENSORBONE.HIP, SENSORBONE.HIP_RIGHT },
-- Left Arm
{ SENSORBONE.SHOULDER_LEFT, SENSORBONE.ELBOW_LEFT },
{ SENSORBONE.ELBOW_LEFT, SENSORBONE.WRIST_LEFT },
{ SENSORBONE.WRIST_LEFT, SENSORBONE.HAND_LEFT },
-- Right Arm
{ SENSORBONE.SHOULDER_RIGHT, SENSORBONE.ELBOW_RIGHT },
{ SENSORBONE.ELBOW_RIGHT, SENSORBONE.WRIST_RIGHT },
{ SENSORBONE.WRIST_RIGHT, SENSORBONE.HAND_RIGHT },
-- left leg
{ SENSORBONE.HIP_LEFT, SENSORBONE.KNEE_LEFT },
{ SENSORBONE.KNEE_LEFT, SENSORBONE.ANKLE_LEFT },
{ SENSORBONE.ANKLE_LEFT, SENSORBONE.FOOT_LEFT },
-- right leg
{ SENSORBONE.HIP_RIGHT, SENSORBONE.KNEE_RIGHT },
{ SENSORBONE.KNEE_RIGHT, SENSORBONE.ANKLE_RIGHT },
{ SENSORBONE.ANKLE_RIGHT, SENSORBONE.FOOT_RIGHT },
}
motionsensor.ChooseBuilderFromEntity = function( ent )
local builders = list.Get( "SkeletonConvertor" )
for k, v in pairs( builders ) do
if ( v:IsApplicable( ent ) ) then return k end
end
return 'ValveBiped'
end
motionsensor.ProcessAngle = function( translator, sensor, pos, ang, special_vectors, boneid, v )
local a = nil
local b = nil
local up = special_vectors['up']
--
-- Using a vector from another angle.
-- If the angle isn't processed yet return
-- we will be added to the list to process again.
--
if ( v.up_up ) then
if ( !ang[ v.up_up ] ) then return end
up = ang[ v.up_up ]:Up()
end
if ( v.up_dn ) then
if ( !ang[ v.up_dn ] ) then return end
up = ang[ v.up_dn ]:Up() * -1
end
if ( v.up_fwd ) then
if ( !ang[ v.up_fwd ] ) then return end
up = ang[ v.up_fwd ]:Forward()
end
if ( v.up_lft ) then
if ( !ang[ v.up_lft ] ) then return end
up = ang[ v.up_lft ]:Right() * -1
end
if ( v.up_rgt ) then
if ( !ang[ v.up_rgt ] ) then return end
up = ang[ v.up_rgt ]:Right()
end
--
-- From -> To vectors
--
if ( v.from_sensor ) then a = sensor[ v.from_sensor ] end
if ( v.to_sensor ) then b = sensor[ v.to_sensor ] end
if ( v.from ) then a = pos[ v.from ] end
if ( v.to ) then b = pos[ v.to ] end
--
-- We can offer special vectors to define 'up'
--
if ( isstring( v.up ) ) then
up = special_vectors[ v.up ]
end
if ( a == nil || b == nil || up == nil ) then return end
ang[ boneid ] = (a-b):GetNormal():AngleEx( up:GetNormal() );
if ( v.adjust ) then
ang[ boneid ] = ang[ boneid ] + v.adjust
end
return true
end
--
-- Processes the AnglesTable from a skeleton constructor
--
motionsensor.ProcessAnglesTable = function( translator, sensor, pos, rotation )
if ( !translator.AnglesTable ) then return {} end
local ang = {}
local special_vectors = {}
special_vectors['right'] = rotation:Right()
special_vectors['left'] = special_vectors['right'] * -1
special_vectors['up'] = rotation:Up()
special_vectors['down'] = special_vectors['up'] * -1
special_vectors['forward'] = special_vectors['down']:Cross( special_vectors['right'] )
special_vectors['backward'] = special_vectors['forward'] * -1
special_vectors['hips_left'] = (sensor[SENSORBONE.HIP_RIGHT] - sensor[SENSORBONE.HIP_LEFT]):GetNormal()
special_vectors['hips_up'] = (sensor[SENSORBONE.HIP] - sensor[SENSORBONE.SHOULDER]):GetNormal()
special_vectors['hips_back'] = special_vectors['hips_up']:Cross( special_vectors['hips_left'] )
special_vectors['hips_fwd'] = special_vectors['hips_back'] * -1
special_vectors['chest_lft'] = (sensor[SENSORBONE.SHOULDER_RIGHT] - sensor[SENSORBONE.SHOULDER_LEFT]):GetNormal()
special_vectors['chest_rgt'] = special_vectors['chest_lft'] * -1
special_vectors['chest_up'] = (sensor[SENSORBONE.SPINE] - sensor[SENSORBONE.SHOULDER]):GetNormal()
special_vectors['chest_dn'] = special_vectors['chest_up'] * -1
special_vectors['chest_bck'] = special_vectors['chest_up']:Cross( special_vectors['chest_lft'] )
special_vectors['chest_fwd'] = special_vectors['chest_bck'] * -1
special_vectors['head_up'] = (sensor[SENSORBONE.SHOULDER] - sensor[SENSORBONE.HEAD]):GetNormal()
special_vectors['head_back'] = special_vectors['head_up']:Cross( special_vectors['chest_lft'] )
local reprocess = {}
for k, v in pairs( translator.AnglesTable ) do
table.insert( reprocess, k )
end
for iPasses = 1, 5 do
local cur_process = reprocess;
reprocess = {}
for k, v in pairs( cur_process ) do
if ( !motionsensor.ProcessAngle( translator, sensor, pos, ang, special_vectors, v, translator.AnglesTable[v] ) ) then
table.insert( reprocess, v )
end
end
if ( table.IsEmpty( reprocess ) ) then
--DebugInfo( 0, iPasses .. " Passes" )
return ang
end
end
--
-- If we got here then we're doing something wrong.
-- It should have out'ed before completing 5 passes.
--
DebugInfo( 0, "motionsensor.ProcessAnglesTable: 4+ passes!" )
return ang
end
--
-- Processes the PositionTable from a skeleton constructor
--
motionsensor.ProcessPositionTable = function( translator, sensor )
if ( !translator.PositionTable ) then return {} end
local pos = {}
for k, v in pairs( translator.PositionTable ) do
-- A number means get value straight from the sensor
if ( isnumber( v ) ) then pos[ k ] = sensor[ v ] end
if ( istable( v ) ) then
if ( v.type == "lerp" ) then pos[ k ] = LerpVector( v.value, sensor[ v.from ], sensor[ v.to ] ) end
end
end
return pos
end
--
-- Called to build the skeleton
--
motionsensor.BuildSkeleton = function( translator, player, rotation )
--
-- The kinect animations are recorded on the skunt, so rotate towards the player
--
rotation:RotateAroundAxis( rotation:Up(), -90 )
--
-- Pre-get and rotate all the player positions
--
local sensor = {}
for i=0, 19 do
sensor[ i ] = player:MotionSensorPos( i )
sensor[ i ]:Rotate( rotation )
end
if ( translator.PrePosition ) then
translator:PrePosition( sensor )
end
--
-- Fill out the position table..
--
local pos = motionsensor.ProcessPositionTable( translator, sensor )
--
-- Fill out the angles
--
local ang = motionsensor.ProcessAnglesTable( translator, sensor, pos, rotation )
--
-- Allow the bone builder to make any last minute changes
--
translator:Complete( player, sensor, rotation, pos, ang )
return pos, ang, sensor
end