10 SHADER_DEFINE( shader_player
,
12 /*Include*/ VERTEX_STANDARD_ATTRIBUTES
15 "uniform mat4x3 uMdl;"
16 "uniform float uOpacity;"
26 "vec3 world_pos = uMdl * vec4(a_co,1.0);"
27 "gl_Position = uPv * vec4(world_pos,1.0);"
31 "aNorm = mat3(uMdl) * a_norm;"
33 "aOpacity = 1.0-(gl_Position.y+0.5)*uOpacity;"
38 "uniform sampler2D uTexMain;"
39 "uniform vec4 uColour;"
49 "vec3 diffuse = texture( uTexMain, aUv ).rgb;"
50 "FragColor = vec4(pow(diffuse,vec3(1.0)),aOpacity);"
53 UNIFORMS({ "uTexMain", "uPv", "uMdl", "uOpacity" })
56 #define FOREACH_PART(FN) \
77 #define MAKE_ENUM(ENUM) k_chpart_##ENUM,
78 #define MAKE_STRING(STR) #STR,
80 #define PART_COUNT FOREACH_PART(ADD_ONE)
84 FOREACH_PART( MAKE_ENUM
)
87 static const char *character_part_strings
[] =
89 FOREACH_PART( MAKE_STRING
)
96 submodel parts
[ PART_COUNT
];
97 m4x3f matrices
[ PART_COUNT
];
99 /* Auxillary information */
100 v3f offsets
[ PART_COUNT
];
101 rigidbody ragdoll
[ PART_COUNT
];
105 * note: - base nodes of IK structures will be filled automatically
106 * - all positions are in local space to the mroot
108 struct ik_basic ik_arm_l
, ik_arm_r
,
114 float rhip
, rcollar
, /* twist of hip and collar controls,
115 these act in the local +y axis of the part */
120 v3f ground_normal
; /* Feet will be aligned to this */
124 static void character_offset( struct character
*ch
, enum character_part parent
,
125 enum character_part child
)
127 v3_sub( ch
->parts
[ child
].pivot
, ch
->parts
[ parent
].pivot
,
128 ch
->offsets
[ child
] );
131 static int character_load( struct character
*ch
, const char *name
)
135 snprintf( buf
, sizeof(buf
)-1, "models/%s.mdl", name
);
136 model
*src
= vg_asset_read( buf
);
140 vg_error( "Could not open 'models/%s.mdl'", name
);
146 for( int i
=0; i
<PART_COUNT
; i
++ )
148 snprintf( buf
, sizeof(buf
)-1, "%s_%s", name
, character_part_strings
[i
] );
149 submodel
*sm
= submodel_get( src
, buf
);
153 vg_warn( "Character file does not contain an '_%s' part.\n",
154 character_part_strings
[i
] );
157 memset( &ch
->parts
[i
], 0, sizeof(submodel
) );
164 model_unpack( src
, &ch
->mesh
);
167 vg_success( "Loaded character file '%s' with no errors\n", name
);
169 /* Create the offsets */
170 character_offset( ch
, k_chpart_body0
, k_chpart_body1
);
171 character_offset( ch
, k_chpart_body1
, k_chpart_neck
);
172 character_offset( ch
, k_chpart_neck
, k_chpart_head
);
174 character_offset( ch
, k_chpart_body1
, k_chpart_arm_l0
);
175 character_offset( ch
, k_chpart_arm_l0
, k_chpart_arm_l1
);
176 character_offset( ch
, k_chpart_arm_l1
, k_chpart_hand_l
);
178 character_offset( ch
, k_chpart_body1
, k_chpart_arm_r0
);
179 character_offset( ch
, k_chpart_arm_r0
, k_chpart_arm_r1
);
180 character_offset( ch
, k_chpart_arm_r1
, k_chpart_hand_r
);
182 character_offset( ch
, k_chpart_body0
, k_chpart_leg_l0
);
183 character_offset( ch
, k_chpart_leg_l0
, k_chpart_leg_l1
);
184 character_offset( ch
, k_chpart_leg_l1
, k_chpart_foot_l
);
186 character_offset( ch
, k_chpart_body0
, k_chpart_leg_r0
);
187 character_offset( ch
, k_chpart_leg_r0
, k_chpart_leg_r1
);
188 character_offset( ch
, k_chpart_leg_r1
, k_chpart_foot_r
);
190 character_offset( ch
, k_chpart_board
, k_chpart_wb
);
191 character_offset( ch
, k_chpart_board
, k_chpart_wf
);
193 ch
->ik_arm_l
.l1
= v3_length( ch
->offsets
[ k_chpart_arm_l1
] );
194 ch
->ik_arm_l
.l2
= v3_length( ch
->offsets
[ k_chpart_hand_l
] );
195 ch
->ik_arm_r
.l1
= v3_length( ch
->offsets
[ k_chpart_arm_r1
] );
196 ch
->ik_arm_r
.l2
= v3_length( ch
->offsets
[ k_chpart_hand_r
] );
198 ch
->ik_leg_l
.l1
= v3_length( ch
->offsets
[ k_chpart_leg_l1
] );
199 ch
->ik_leg_l
.l2
= v3_length( ch
->offsets
[ k_chpart_foot_l
] );
200 ch
->ik_leg_r
.l1
= v3_length( ch
->offsets
[ k_chpart_leg_r1
] );
201 ch
->ik_leg_r
.l2
= v3_length( ch
->offsets
[ k_chpart_foot_r
] );
203 ch
->ik_body
.l1
= v3_length( ch
->offsets
[ k_chpart_body1
] );
204 ch
->ik_body
.l2
= v3_length( ch
->offsets
[ k_chpart_neck
] );
210 static void align_to_board( struct character
*ch
, enum character_part id
)
212 /* Calculate rotation between board and feet */
213 m4x3f
*mats
= ch
->matrices
;
215 v3f foot_pos
, foot_fwd
, foot_target
, board_norm
, board_origin
;
216 v3_copy( mats
[id
][3], foot_pos
);
217 m3x3_mulv( mats
[id
], (v3f
){1.0f
,0.0f
,0.0f
}, foot_fwd
);
218 v3_add( foot_fwd
, foot_pos
, foot_target
);
220 m3x3_mulv( mats
[k_chpart_board
], (v3f
){0.0f
,1.0f
,0.0f
}, board_norm
);
221 m4x3_mulv( mats
[k_chpart_board
], (v3f
){0.0f
,0.13f
,0.0f
}, board_origin
);
223 vg_line( foot_pos
, foot_target
, 0xff00ff00 );
226 v3_sub( board_origin
, foot_target
, v0
);
227 float t
= v3_dot( v0
, board_norm
) / board_norm
[1];
229 vg_line( foot_pos
, foot_target
, 0xff00ffff );
231 v3_sub( foot_target
, foot_pos
, foot_target
);
232 v3_normalize( foot_target
);
233 float ang
= acosf( v3_dot( foot_target
, foot_fwd
) );
235 v4f qcorrection
; m3x3f correction
;
236 q_axis_angle( qcorrection
, (v3f
){0.0f
,0.0f
,1.0f
}, -ang
);
237 q_m3x3( qcorrection
, correction
);
238 m3x3_mul( mats
[id
], correction
, mats
[id
] );
241 static void character_eval( struct character
*ch
)
243 m4x3f
*mats
= ch
->matrices
;
244 v3f
*offs
= ch
->offsets
;
246 ik_basic( &ch
->ik_body
, mats
[k_chpart_body0
], mats
[k_chpart_body1
],
251 /* TODO: Do this directly via m3x3 */
253 q_axis_angle( body_rotation
, (v3f
){0.0f
,1.0f
,0.0f
}, ch
->rhip
);
254 q_m3x3( body_rotation
, temp
);
255 m3x3_mul( mats
[k_chpart_body0
], temp
, mats
[k_chpart_body0
] );
257 q_axis_angle( body_rotation
, (v3f
){0.0f
,1.0f
,0.0f
}, ch
->rcollar
);
258 q_m3x3( body_rotation
, temp
);
259 m3x3_mul( mats
[k_chpart_body1
], temp
, mats
[k_chpart_body1
] );
262 m4x3_mulv( mats
[k_chpart_body0
], offs
[k_chpart_leg_l0
], ch
->ik_leg_l
.base
);
263 m4x3_mulv( mats
[k_chpart_body0
], offs
[k_chpart_leg_r0
], ch
->ik_leg_r
.base
);
264 m4x3_mulv( mats
[k_chpart_body1
], offs
[k_chpart_arm_l0
], ch
->ik_arm_l
.base
);
265 m4x3_mulv( mats
[k_chpart_body1
], offs
[k_chpart_arm_r0
], ch
->ik_arm_r
.base
);
267 /* IK for arms and legs */
268 ik_basic( &ch
->ik_arm_l
, mats
[k_chpart_arm_l0
], mats
[k_chpart_arm_l1
],
270 ik_basic( &ch
->ik_arm_r
, mats
[k_chpart_arm_r0
], mats
[k_chpart_arm_r1
],
272 ik_basic( &ch
->ik_leg_l
, mats
[k_chpart_leg_l0
], mats
[k_chpart_leg_l1
],
274 ik_basic( &ch
->ik_leg_r
, mats
[k_chpart_leg_r0
], mats
[k_chpart_leg_r1
],
278 m3x3_copy( mats
[k_chpart_arm_l1
], mats
[k_chpart_hand_l
] );
279 m3x3_copy( mats
[k_chpart_arm_r1
], mats
[k_chpart_hand_r
] );
280 m4x3_mulv( mats
[k_chpart_arm_l1
], offs
[k_chpart_hand_l
],
281 mats
[k_chpart_hand_l
][3] );
282 m4x3_mulv( mats
[k_chpart_arm_r1
], offs
[k_chpart_hand_r
],
283 mats
[k_chpart_hand_r
][3] );
286 m3x3_copy( mats
[k_chpart_body1
], mats
[k_chpart_neck
] );
287 m4x3_mulv( mats
[k_chpart_body1
], offs
[k_chpart_neck
],
288 mats
[k_chpart_neck
][3] );
291 q_axis_angle( qhead
, (v3f
){ 0.0f
,1.0f
,0.0f
}, ch
->rhead
);
292 q_m3x3( qhead
, mats
[k_chpart_head
] );
293 //m3x3_mul( mats[k_chpart_neck], mats[k_chpart_head], mats[k_chpart_head] );
294 m4x3_mulv( mats
[k_chpart_neck
], offs
[k_chpart_head
], mats
[k_chpart_head
][3]);
297 m3x3_copy( mats
[k_chpart_leg_l1
], mats
[k_chpart_foot_l
] );
298 m3x3_copy( mats
[k_chpart_leg_r1
], mats
[k_chpart_foot_r
] );
299 m4x3_mulv( mats
[k_chpart_leg_l1
], offs
[k_chpart_foot_l
],
300 mats
[k_chpart_foot_l
][3] );
301 m4x3_mulv( mats
[k_chpart_leg_r1
], offs
[k_chpart_foot_r
],
302 mats
[k_chpart_foot_r
][3] );
304 align_to_board( ch
, k_chpart_foot_l
);
305 align_to_board( ch
, k_chpart_foot_r
);
307 for( int i
=0; i
<PART_COUNT
; i
++ )
308 m4x3_mul( ch
->mroot
, ch
->matrices
[i
], ch
->matrices
[i
] );
311 #define B3D_CO( X, Y, Z ) (v3f){ X, Z, -Y }
313 typedef struct character_pose character_pose
;
314 struct character_pose
316 v3f b0
, b1
, p
, fr
, fl
, pl
, pr
, hl
, hr
, apl
, apr
, cam
;
319 static character_pose pose_aero
=
321 .b0
= {0.0721f
, 0.8167f
, 0.1365f
},
322 .b1
= {-0.0773f
, 1.1559f
, -0.1699f
},
323 .p
= {0.0421f
, 1.1430f
, 0.2803f
},
324 .fr
= {0.0535f
, 0.1312f
, -0.3647f
},
325 .fl
= {-0.0605f
, 0.1464f
, 0.2917f
},
326 .pl
= {-0.1704f
, 0.6889f
, -0.4017f
},
327 .pr
= {0.0672f
, 0.7598f
, -0.5963f
},
328 .hl
= {-0.2153f
, 0.7195f
, -0.1345f
},
329 .hr
= {0.1974f
, 0.7940f
, -0.3522f
},
330 .apl
= {-0.2008f
, 0.9546f
, 0.3687f
},
331 .apr
= {0.3133f
, 0.9299f
, 0.0181f
},
332 .cam
= {-0.3394f
, 1.2661f
, 0.2936f
}
335 static character_pose pose_slide
=
337 .b0
= {0.6732f
, 0.5565f
, -0.0000f
},
338 .b1
= {0.8116f
, 1.0547f
, 0.0613f
},
339 .p
= {1.0404f
, 0.7907f
, 0.0186f
},
340 .fr
= {-0.0030f
, 0.1366f
, -0.4461f
},
341 .fl
= {-0.0030f
, 0.1366f
, 0.3480f
},
342 .pl
= {-0.0887f
, 0.8229f
, 0.3826f
},
343 .pr
= {-0.0887f
, 0.8229f
, -0.4621f
},
344 .hl
= {0.7749f
, 0.5545f
, 0.5310f
},
345 .hr
= {0.5844f
, 1.2445f
, -0.5456f
},
346 .apl
= {1.0999f
, 0.5390f
, 0.2398f
},
347 .apr
= {0.9816f
, 0.9536f
, -0.5463f
},
348 .cam
= {0.9888f
, 1.4037f
, 0.6081f
}
351 static character_pose pose_slide1
=
353 .b0
= {-0.2385f
, 0.6403f
, 0.1368f
},
354 .b1
= {-0.5151f
, 1.1351f
, 0.1380f
},
355 .p
= {-0.1158f
, 1.2118f
, 0.3895f
},
356 .fr
= {-0.0030f
, 0.1323f
, -0.3190f
},
357 .fl
= {-0.0030f
, 0.1323f
, 0.5797f
},
358 .pl
= {-0.6568f
, 0.4305f
, 0.2069f
},
359 .pr
= {-0.6850f
, 0.2740f
, -0.2969f
},
360 .hl
= {-0.7029f
, 0.6132f
, 0.2972f
},
361 .hr
= {-0.2572f
, 1.0104f
, -0.4770f
},
362 .apl
= {-0.4808f
, 0.8480f
, 0.3731f
},
363 .apr
= {-0.0836f
, 1.0480f
, -0.1201f
},
364 .cam
= {-1.0508f
, 1.0769f
, 0.0528f
}
367 static character_pose pose_aero_reverse
=
369 .b0
= {0.0616f
, 0.8167f
, -0.1415f
},
370 .b1
= {0.0148f
, 1.1559f
, 0.1861f
},
371 .p
= {0.0558f
, 1.1430f
, -0.2779f
},
372 .fr
= {0.0535f
, 0.1312f
, -0.3647f
},
373 .fl
= {0.0730f
, 0.1464f
, 0.2917f
},
374 .pl
= {-0.2073f
, 0.6889f
, 0.3839f
},
375 .pr
= {-0.3584f
, 0.4069f
, 0.1032f
},
376 .hl
= {0.1567f
, 0.7195f
, 0.1997f
},
377 .hr
= {-0.3055f
, 0.7940f
, 0.2639f
},
378 .apl
= {0.3143f
, 0.9546f
, -0.2784f
},
379 .apr
= {-0.2885f
, 0.9299f
, -0.1236f
},
380 .cam
= {-0.3394f
, 1.2661f
, -0.2936f
}
383 static character_pose pose_stand
=
385 .b0
= {0.1877f
, 1.0663f
, 0.0063f
},
386 .b1
= {0.0499f
, 1.5564f
, -0.0584f
},
387 .p
= {0.5982f
, 1.2810f
, 0.0842f
},
388 .fr
= {0.0535f
, 0.1312f
, -0.3647f
},
389 .fl
= {0.0354f
, 0.1464f
, 0.2917f
},
390 .pl
= {-0.4325f
, 0.6889f
, 0.1823f
},
391 .pr
= {-0.4794f
, 0.7598f
, -0.3610f
},
392 .hl
= {0.0498f
, 1.0058f
, 0.2317f
},
393 .hr
= {0.0188f
, 0.9786f
, -0.2725f
},
394 .apl
= {0.2898f
, 1.3453f
, 0.2303f
},
395 .apr
= {0.5273f
, 1.2876f
, -0.1848f
},
396 .cam
= {-0.3477f
, 1.5884f
, -0.0019f
}
399 static character_pose pose_fly
=
401 .b0
= {0.2995f
, 0.6819f
, -0.1369f
},
402 .b1
= {0.1618f
, 1.1720f
, -0.2016f
},
403 .p
= {0.7477f
, 0.9173f
, -0.1885f
},
404 .fr
= {0.0535f
, 0.1312f
, -0.3647f
},
405 .fl
= {0.0354f
, 0.1464f
, 0.2917f
},
406 .pl
= {-0.2930f
, 0.4849f
, 0.5307f
},
407 .pr
= {-0.4754f
, 0.4124f
, -0.4874f
},
408 .hl
= {0.2650f
, 1.1897f
, 0.4626f
},
409 .hr
= {0.2494f
, 1.2059f
, -0.7985f
},
410 .apl
= {0.5165f
, 1.0990f
, 0.1655f
},
411 .apr
= {0.6759f
, 1.0661f
, -0.6014f
},
412 .cam
= {-0.2727f
, 1.2606f
, 0.3564f
}
416 void character_pose_blend( struct character
*ch
, character_pose
*pose
, float q
)
418 v3_muladds( ch
->ik_body
.base
, pose
->b0
, q
, ch
->ik_body
.base
);
419 v3_muladds( ch
->ik_body
.end
, pose
->b1
, q
, ch
->ik_body
.end
);
420 v3_muladds( ch
->ik_body
.pole
, pose
->p
, q
, ch
->ik_body
.pole
);
421 v3_muladds( ch
->ik_leg_l
.end
, pose
->fl
, q
, ch
->ik_leg_l
.end
);
422 v3_muladds( ch
->ik_leg_l
.pole
, pose
->pl
, q
, ch
->ik_leg_l
.pole
);
423 v3_muladds( ch
->ik_leg_r
.end
, pose
->fr
, q
, ch
->ik_leg_r
.end
);
424 v3_muladds( ch
->ik_leg_r
.pole
, pose
->pr
, q
, ch
->ik_leg_r
.pole
);
425 v3_muladds( ch
->ik_arm_l
.pole
, pose
->apl
, q
, ch
->ik_arm_l
.pole
);
426 v3_muladds( ch
->ik_arm_r
.pole
, pose
->apr
, q
, ch
->ik_arm_r
.pole
);
427 v3_muladds( ch
->ik_arm_l
.end
, pose
->hl
, q
, ch
->ik_arm_l
.end
);
428 v3_muladds( ch
->ik_arm_r
.end
, pose
->hr
, q
, ch
->ik_arm_r
.end
);
429 v3_muladds( ch
->cam_pos
, pose
->cam
, q
, ch
->cam_pos
);
433 void character_final_pose( struct character
*ch
, v3f cog
,
434 character_pose
*pose
, float q
)
436 character_pose npose
;
437 float dip
= vg_clampf(cog
[1], -1.0f
, 0.3f
) * 0.35f
,
438 tilt
= vg_clampf(cog
[2], -1.0f
, 1.0f
) * 0.3f
;
441 q_axis_angle( rz
, (v3f
){0.0f
,0.0f
,1.0f
}, -cog
[0]*0.6f
);
443 v3_copy( (v3f
){0.0f
,dip
,tilt
}, tr
[3] );
445 m4x3_mulv( tr
, pose
->b0
, npose
.b0
);
446 m4x3_mulv( tr
, pose
->b1
, npose
.b1
);
447 m4x3_mulv( tr
, pose
->p
, npose
.p
);
448 m4x3_mulv( tr
, pose
->pl
, npose
.pl
);
449 m4x3_mulv( tr
, pose
->pr
, npose
.pr
);
450 m4x3_mulv( tr
, pose
->hl
, npose
.hl
);
451 m4x3_mulv( tr
, pose
->hr
, npose
.hr
);
452 m4x3_mulv( tr
, pose
->apl
, npose
.apl
);
453 m4x3_mulv( tr
, pose
->apr
, npose
.apr
);
455 v3_copy( pose
->fr
, npose
.fr
);
456 v3_copy( pose
->fl
, npose
.fl
);
457 v3_copy( pose
->cam
, npose
.cam
);
459 character_pose_blend( ch
, &npose
, q
);
462 static void zero_ik_basic( struct ik_basic
*ik
)
469 static void character_pose_reset( struct character
*ch
)
471 zero_ik_basic( &ch
->ik_body
);
472 zero_ik_basic( &ch
->ik_leg_l
);
473 zero_ik_basic( &ch
->ik_leg_r
);
474 zero_ik_basic( &ch
->ik_arm_l
);
475 zero_ik_basic( &ch
->ik_arm_r
);
476 v3_zero( ch
->cam_pos
);
479 static void character_testpose( struct character
*ch
, float t
)
482 float *hips
= ch
->ik_body
.base
,
483 *collar
= ch
->ik_body
.end
,
484 *pole
= ch
->ik_body
.pole
;
486 hips
[0] = cosf(t
*1.325f
)*0.25f
;
487 hips
[1] = (sinf(t
)*0.2f
+0.6f
) * ch
->parts
[ k_chpart_body0
].pivot
[1];
491 collar
[1] = hips
[1] + (ch
->ik_body
.l1
+ch
->ik_body
.l2
)*(sinf(t
)*0.05f
+0.94f
);
492 collar
[2] = hips
[2] + cosf(t
*0.42f
)*0.01f
;
494 v3_add( hips
, collar
, pole
);
495 v3_muls( pole
, 0.5f
, pole
);
496 v3_add( pole
, (v3f
){ 1.0f
, 0.0f
, 0.0f
}, pole
);
499 float *footl
= ch
->ik_leg_l
.end
,
500 *footr
= ch
->ik_leg_r
.end
,
501 *polel
= ch
->ik_leg_l
.pole
,
502 *poler
= ch
->ik_leg_r
.pole
;
504 footl
[0] = sinf(t
*0.563f
);
510 footr
[2] = cosf(t
*0.672f
);
512 v3_add( hips
, footl
, polel
);
513 v3_muls( polel
, 0.4f
, polel
);
514 v3_add( polel
, (v3f
){ -1.0f
,0.0f
,0.0f
}, polel
);
516 v3_add( hips
, footr
, poler
);
517 v3_muls( poler
, 0.4f
, poler
);
518 v3_add( poler
, (v3f
){ -1.0f
,0.0f
,0.0f
}, poler
);
521 float *arml
= ch
->ik_arm_l
.end
,
522 *armr
= ch
->ik_arm_r
.end
;
523 polel
= ch
->ik_arm_l
.pole
;
524 poler
= ch
->ik_arm_r
.pole
;
526 v3_copy( (v3f
){ 0.0f
, 0.0f
, 1.0f
}, arml
);
527 v3_copy( (v3f
){ 0.0f
, 0.0f
,-1.0f
}, armr
);
528 v3_copy( (v3f
){ 1.0f
, 1.0f
, 0.5f
}, polel
);
529 v3_copy( (v3f
){ 1.0f
, 1.0f
,-0.5f
}, poler
);
532 ch
->rhip
= sinf(t
*0.2f
);
533 ch
->rcollar
= sinf(t
*0.35325f
);
534 q_identity( ch
->qhead
);
536 m4x3_identity( ch
->matrices
[k_chpart_board
] );
537 m4x3_identity( ch
->matrices
[k_chpart_wb
] );
538 m4x3_identity( ch
->matrices
[k_chpart_wf
] );
541 static void character_draw( struct character
*ch
, float temp
)
543 SHADER_USE(shader_player
);
544 glUniformMatrix4fv( SHADER_UNIFORM( shader_player
, "uPv" ),
545 1, GL_FALSE
, (float *)vg_pv
);
547 glUniform1i( SHADER_UNIFORM( shader_player
, "uTexMain" ), 0 );
548 glUniform1f( SHADER_UNIFORM( shader_player
, "uOpacity" ), temp
);
550 GLint kuMdl
= SHADER_UNIFORM( shader_player
, "uMdl" );
552 glEnable( GL_CULL_FACE
);
553 glCullFace( GL_BACK
);
555 mesh_bind( &ch
->mesh
);
557 for( int i
=0; i
<PART_COUNT
; i
++ )
559 glUniformMatrix4x3fv( kuMdl
, 1, GL_FALSE
, (float *)ch
->matrices
[i
] );
560 submodel_draw( &ch
->parts
[i
] );
564 static void character_shader_register(void)
566 SHADER_INIT(shader_player
);
574 static void character_rd_box( struct character
*ch
, enum character_part id
,
577 v3_muls( dims
, -0.5f
, ch
->ragdoll
[id
].bbx
[0] );
578 v3_muls( dims
, 0.5f
, ch
->ragdoll
[id
].bbx
[1] );
583 enum character_part ia
, ib
;
594 static const float k_human_major
= 0.9f
,
595 k_human_minor
= 0.9f
,
596 k_human_major_max
= 1.4f
,
597 k_human_minor_max
= 0.4f
;
599 #define HUMAN_VERTICAL_DEFAULT \
601 .va = {1.0f,0.0f,0.0f}, .vb = {1.0f,0.0f,0.0f}, \
602 .spring = k_human_minor, .ang = k_human_minor_max \
605 .va = {0.0f,1.0f,0.0f}, .vb = {0.0f,1.0f,0.0f}, \
606 .spring = k_human_major, .ang = k_human_major_max \
609 #define HUMAN_ARM_LEFT \
611 .va = {1.0f,0.0f,0.0f}, .vb = {1.0f,0.0f,0.0f}, \
612 .spring = k_human_minor, .ang = k_human_minor_max \
615 .va = {0.0f,0.0f,1.0f}, .vb = {0.0f,0.0f,1.0f}, \
616 .spring = k_human_major, .ang = k_human_major_max \
619 #define HUMAN_ARM_RIGHT \
621 .va = {1.0f,0.0f,0.0f}, .vb = {1.0f,0.0f,0.0f}, \
622 .spring = k_human_minor, .ang = k_human_minor_max \
625 .va = {0.0f,0.0f,-1.0f}, .vb = {0.0f,0.0f,-1.0f}, \
626 .spring = k_human_major, .ang = k_human_major_max \
629 static struct rd_joint rd_joints
[] =
631 { .ia
= k_chpart_body0
, .ib
= k_chpart_body1
, HUMAN_VERTICAL_DEFAULT
},
632 { .ia
= k_chpart_body1
, .ib
= k_chpart_neck
, HUMAN_VERTICAL_DEFAULT
},
633 { .ia
= k_chpart_neck
, .ib
= k_chpart_head
, HUMAN_VERTICAL_DEFAULT
},
634 { .ia
= k_chpart_body0
, .ib
= k_chpart_leg_l0
, HUMAN_VERTICAL_DEFAULT
},
635 { .ia
= k_chpart_leg_l0
, .ib
= k_chpart_leg_l1
, HUMAN_VERTICAL_DEFAULT
},
636 { .ia
= k_chpart_body0
, .ib
= k_chpart_leg_r0
, HUMAN_VERTICAL_DEFAULT
},
637 { .ia
= k_chpart_leg_r0
, .ib
= k_chpart_leg_r1
, HUMAN_VERTICAL_DEFAULT
},
639 { .ia
= k_chpart_body1
, .ib
= k_chpart_arm_l0
, HUMAN_ARM_LEFT
},
640 { .ia
= k_chpart_arm_l0
, .ib
= k_chpart_arm_l1
, HUMAN_ARM_LEFT
},
641 { .ia
= k_chpart_arm_l1
, .ib
= k_chpart_hand_l
, HUMAN_ARM_LEFT
},
643 { .ia
= k_chpart_body1
, .ib
= k_chpart_arm_r0
, HUMAN_ARM_RIGHT
},
644 { .ia
= k_chpart_arm_r0
, .ib
= k_chpart_arm_r1
, HUMAN_ARM_RIGHT
},
645 { .ia
= k_chpart_arm_r1
, .ib
= k_chpart_hand_r
, HUMAN_ARM_RIGHT
},
648 static void character_init_ragdoll_joints( struct character
*ch
)
650 for( int i
=0; i
<vg_list_size(rd_joints
); i
++ )
652 struct rd_joint
*joint
= &rd_joints
[i
];
654 float *hinge
= ch
->parts
[joint
->ib
].pivot
;
655 v3_sub( hinge
, ch
->ragdoll
[joint
->ia
].co
, joint
->lca
);
656 v3_sub( hinge
, ch
->ragdoll
[joint
->ib
].co
, joint
->lcb
);
660 static void character_init_ragdoll( struct character
*ch
)
662 v3f
*offs
= ch
->offsets
;
663 rigidbody
*rbs
= ch
->ragdoll
;
666 float chest_width
= fabsf(offs
[k_chpart_arm_r0
][2])*2.0f
,
667 chest_depth
= chest_width
* 0.571f
,
668 chest_height
= offs
[k_chpart_neck
][1];
669 v3f chest_dims
= { chest_depth
, chest_height
, chest_width
};
670 character_rd_box( ch
, k_chpart_body1
, chest_dims
);
672 v3_copy( ch
->parts
[k_chpart_body1
].pivot
, rbs
[k_chpart_body1
].co
);
673 rbs
[k_chpart_body1
].co
[1] += chest_height
*0.5f
;
676 v3f torso_dims
= { chest_depth
,
677 offs
[k_chpart_body1
][1]-offs
[k_chpart_leg_l0
][1],
679 v3_copy( ch
->parts
[k_chpart_body0
].pivot
, rbs
[k_chpart_body0
].co
);
680 character_rd_box( ch
, k_chpart_body0
, torso_dims
);
683 v3f neck_dims
= { chest_depth
*0.5f
,
684 offs
[k_chpart_head
][1],
686 v3_copy( ch
->parts
[k_chpart_neck
].pivot
, rbs
[k_chpart_neck
].co
);
687 rbs
[k_chpart_neck
].co
[1] += neck_dims
[1]*0.5f
;
688 character_rd_box( ch
, k_chpart_neck
, neck_dims
);
691 v3f head_dims
= { chest_width
*0.5f
, chest_width
*0.5f
, chest_width
*0.5f
};
692 v3_copy( ch
->parts
[k_chpart_head
].pivot
, rbs
[k_chpart_head
].co
);
693 rbs
[k_chpart_head
].co
[1] += head_dims
[1]*0.5f
;
694 character_rd_box( ch
, k_chpart_head
, head_dims
);
697 v3f ua_dims
= { 0.0f
, 0.0f
, fabsf(offs
[k_chpart_arm_l1
][2]) };
698 ua_dims
[1] = 0.38f
*ua_dims
[2];
699 ua_dims
[0] = 0.38f
*ua_dims
[2];
700 v3f la_dims
= { ua_dims
[0], ua_dims
[1], fabsf(offs
[k_chpart_hand_l
][2]) };
701 v3f hand_dims
= { ua_dims
[1], ua_dims
[1]*0.5f
, ua_dims
[1] };
703 character_rd_box( ch
, k_chpart_arm_l0
, ua_dims
);
704 character_rd_box( ch
, k_chpart_arm_r0
, ua_dims
);
705 character_rd_box( ch
, k_chpart_arm_l1
, la_dims
);
706 character_rd_box( ch
, k_chpart_arm_r1
, la_dims
);
707 character_rd_box( ch
, k_chpart_hand_l
, hand_dims
);
708 character_rd_box( ch
, k_chpart_hand_r
, hand_dims
);
710 v3_copy( ch
->parts
[k_chpart_arm_l0
].pivot
, rbs
[k_chpart_arm_l0
].co
);
711 rbs
[k_chpart_arm_l0
].co
[2] += ua_dims
[2] * 0.5f
;
712 v3_copy( ch
->parts
[k_chpart_arm_l1
].pivot
, rbs
[k_chpart_arm_l1
].co
);
713 rbs
[k_chpart_arm_l1
].co
[2] += la_dims
[2] * 0.5f
;
714 v3_copy( ch
->parts
[k_chpart_hand_l
].pivot
, rbs
[k_chpart_hand_l
].co
);
715 rbs
[k_chpart_hand_l
].co
[2] += hand_dims
[2] * 0.5f
;
717 v3_copy( ch
->parts
[k_chpart_arm_r0
].pivot
, rbs
[k_chpart_arm_r0
].co
);
718 rbs
[k_chpart_arm_r0
].co
[2] -= ua_dims
[2] * 0.5f
;
719 v3_copy( ch
->parts
[k_chpart_arm_r1
].pivot
, rbs
[k_chpart_arm_r1
].co
);
720 rbs
[k_chpart_arm_r1
].co
[2] -= la_dims
[2] * 0.5f
;
721 v3_copy( ch
->parts
[k_chpart_hand_r
].pivot
, rbs
[k_chpart_hand_r
].co
);
722 rbs
[k_chpart_hand_r
].co
[2] -= hand_dims
[2] * 0.5f
;
725 v3f ul_dims
= { 0.0f
, fabsf(offs
[k_chpart_leg_l1
][1]), 0.0f
};
726 ul_dims
[0] = 0.38f
*ul_dims
[1];
727 ul_dims
[2] = 0.38f
*ul_dims
[1];
728 v3f ll_dims
= { ul_dims
[0], fabsf(offs
[k_chpart_foot_l
][1]), ul_dims
[2] };
729 v3f foot_dims
= { 2.0f
*ul_dims
[0], ul_dims
[0], ul_dims
[0] };
731 character_rd_box( ch
, k_chpart_leg_l0
, ul_dims
);
732 character_rd_box( ch
, k_chpart_leg_r0
, ul_dims
);
733 character_rd_box( ch
, k_chpart_leg_l1
, ll_dims
);
734 character_rd_box( ch
, k_chpart_leg_r1
, ll_dims
);
735 character_rd_box( ch
, k_chpart_foot_l
, foot_dims
);
736 character_rd_box( ch
, k_chpart_foot_r
, foot_dims
);
738 v3_copy( ch
->parts
[k_chpart_leg_l0
].pivot
, rbs
[k_chpart_leg_l0
].co
);
739 rbs
[k_chpart_leg_l0
].co
[1] -= ul_dims
[1] * 0.5f
;
740 v3_copy( ch
->parts
[k_chpart_leg_l1
].pivot
, rbs
[k_chpart_leg_l1
].co
);
741 rbs
[k_chpart_leg_l1
].co
[1] -= ll_dims
[1] * 0.5f
;
742 v3_copy( ch
->parts
[k_chpart_foot_l
].pivot
, rbs
[k_chpart_foot_l
].co
);
743 rbs
[k_chpart_foot_l
].co
[1] -= foot_dims
[1] * 0.5f
;
744 rbs
[k_chpart_foot_l
].co
[0] -= foot_dims
[0] * 0.5f
;
746 v3_copy( ch
->parts
[k_chpart_leg_r0
].pivot
, rbs
[k_chpart_leg_r0
].co
);
747 rbs
[k_chpart_leg_r0
].co
[1] -= ul_dims
[1] * 0.5f
;
748 v3_copy( ch
->parts
[k_chpart_leg_r1
].pivot
, rbs
[k_chpart_leg_r1
].co
);
749 rbs
[k_chpart_leg_r1
].co
[1] -= ll_dims
[1] * 0.5f
;
750 v3_copy( ch
->parts
[k_chpart_foot_r
].pivot
, rbs
[k_chpart_foot_r
].co
);
751 rbs
[k_chpart_foot_r
].co
[1] -= foot_dims
[1] * 0.5f
;
752 rbs
[k_chpart_foot_r
].co
[0] -= foot_dims
[0] * 0.5f
;
754 character_init_ragdoll_joints( ch
);
756 for( int i
=0; i
<PART_COUNT
; i
++ )
757 rb_init( &ch
->ragdoll
[i
] );
760 static void character_ragdoll_go( struct character
*ch
, v3f pos
)
762 character_init_ragdoll( ch
);
763 for( int i
=0; i
<PART_COUNT
; i
++ )
764 v3_add( pos
, ch
->ragdoll
[i
].co
, ch
->ragdoll
[i
].co
);
767 static void character_ragdoll_copypose( struct character
*ch
, v3f v
)
769 character_init_ragdoll(ch
);
771 for( int i
=0; i
<PART_COUNT
; i
++ )
774 rigidbody
*rb
= &ch
->ragdoll
[i
];
776 v3_sub( rb
->co
, ch
->parts
[i
].pivot
, offset
);
778 m4x3_mulv( ch
->matrices
[i
], offset
, rb
->co
);
779 m3x3_q( ch
->matrices
[i
], rb
->q
);
782 rb
->manifold_count
= 0; /* ? */
784 rb_update_transform( rb
);
788 static void character_debug_ragdoll( struct character
*ch
)
790 rb_debug( &ch
->ragdoll
[k_chpart_body0
], 0xffffffff );
791 rb_debug( &ch
->ragdoll
[k_chpart_body1
], 0xffffffff );
792 rb_debug( &ch
->ragdoll
[k_chpart_neck
], 0xff00ff00 );
793 rb_debug( &ch
->ragdoll
[k_chpart_head
], 0xff00ff00 );
795 rb_debug( &ch
->ragdoll
[k_chpart_arm_l0
], 0xffffa500 );
796 rb_debug( &ch
->ragdoll
[k_chpart_arm_l1
], 0xffffa500 );
797 rb_debug( &ch
->ragdoll
[k_chpart_hand_l
], 0xffffa500 );
799 rb_debug( &ch
->ragdoll
[k_chpart_arm_r0
], 0xff00a5ff );
800 rb_debug( &ch
->ragdoll
[k_chpart_arm_r1
], 0xff00a5ff );
801 rb_debug( &ch
->ragdoll
[k_chpart_hand_r
], 0xff00a5ff );
803 rb_debug( &ch
->ragdoll
[k_chpart_leg_l0
], 0xffffa500 );
804 rb_debug( &ch
->ragdoll
[k_chpart_leg_l1
], 0xffffa500 );
805 rb_debug( &ch
->ragdoll
[k_chpart_foot_l
], 0xffffa500 );
806 rb_debug( &ch
->ragdoll
[k_chpart_leg_r0
], 0xff00a5ff );
807 rb_debug( &ch
->ragdoll
[k_chpart_leg_r1
], 0xff00a5ff );
808 rb_debug( &ch
->ragdoll
[k_chpart_foot_r
], 0xff00a5ff );
811 static void character_ragdoll_iter( struct character
*ch
, scene
*sc
)
813 for( int i
=0; i
<PART_COUNT
; i
++ )
815 rb_build_manifold( &ch
->ragdoll
[i
], sc
);
818 for( int i
=0; i
<20; i
++ )
820 float const k_springfactor
= 1.0f
/20.0f
;
822 for( int j
=0; j
<PART_COUNT
; j
++ )
823 rb_constraint_manifold( &ch
->ragdoll
[j
] );
825 for( int j
=0; j
<vg_list_size(rd_joints
); j
++ )
827 struct rd_joint
*joint
= &rd_joints
[j
];
828 rigidbody
*rba
= &ch
->ragdoll
[joint
->ia
],
829 *rbb
= &ch
->ragdoll
[joint
->ib
];
831 rb_constraint_position( rba
, joint
->lca
, rbb
, joint
->lcb
);
832 rb_constraint_angle( rba
, joint
->maj
.va
, rbb
, joint
->maj
.vb
,
834 joint
->maj
.spring
* k_springfactor
);
836 rb_constraint_angle( rba
, joint
->min
.va
, rbb
, joint
->min
.vb
,
838 joint
->min
.spring
* k_springfactor
);
842 for( int i
=0; i
<PART_COUNT
; i
++ )
843 rb_iter( &ch
->ragdoll
[i
] );
845 for( int i
=0; i
<PART_COUNT
; i
++ )
846 rb_update_transform( &ch
->ragdoll
[i
] );