9 #include "shaders/character.h"
11 vg_tex2d tex_pallet
= { .path
= "textures/ch_gradient.qoi" };
13 static void character_register(void)
15 shader_character_register();
18 static void character_init(void)
20 vg_tex2d_init( (vg_tex2d
*[]){ &tex_pallet
}, 1 );
23 #define FOREACH_PART(FN) \
46 #define MAKE_ENUM(ENUM) k_chpart_##ENUM,
47 #define MAKE_STRING(STR) #STR,
49 #define PART_COUNT FOREACH_PART(ADD_ONE)
54 FOREACH_PART( MAKE_ENUM
)
57 static const char *character_part_strings
[] =
59 FOREACH_PART( MAKE_STRING
)
66 submodel parts
[ PART_COUNT
];
67 m4x3f matrices
[ PART_COUNT
];
69 /* Auxillary information */
70 v3f offsets
[ PART_COUNT
];
71 rigidbody ragdoll
[ PART_COUNT
];
75 * note: - base nodes of IK structures will be filled automatically
76 * - all positions are in local space to the mroot
78 struct ik_basic ik_arm_l
, ik_arm_r
,
84 float rhip
, rcollar
, /* twist of hip and collar controls,
85 these act in the local +y axis of the part */
90 v3f ground_normal
; /* Feet will be aligned to this */
96 static void character_offset( struct character
*ch
, enum character_part parent
,
97 enum character_part child
)
99 v3_sub( ch
->parts
[ child
].pivot
, ch
->parts
[ parent
].pivot
,
100 ch
->offsets
[ child
] );
103 static int character_load( struct character
*ch
, const char *name
)
107 snprintf( buf
, sizeof(buf
)-1, "models/%s.mdl", name
);
108 model
*src
= vg_asset_read( buf
);
112 vg_error( "Could not open 'models/%s.mdl'", name
);
118 for( int i
=0; i
<PART_COUNT
; i
++ )
120 snprintf( buf
, sizeof(buf
)-1, "%s_%s", name
, character_part_strings
[i
] );
121 submodel
*sm
= submodel_get( src
, buf
);
125 vg_warn( "Character file does not contain an '_%s' part.\n",
126 character_part_strings
[i
] );
129 memset( &ch
->parts
[i
], 0, sizeof(submodel
) );
136 model_unpack( src
, &ch
->mesh
);
139 vg_success( "Loaded character file '%s' with no errors\n", name
);
141 /* Create the offsets */
142 character_offset( ch
, k_chpart_body0
, k_chpart_body1
);
143 character_offset( ch
, k_chpart_body1
, k_chpart_neck
);
144 character_offset( ch
, k_chpart_neck
, k_chpart_head
);
146 character_offset( ch
, k_chpart_body1
, k_chpart_arm_l0
);
147 character_offset( ch
, k_chpart_arm_l0
, k_chpart_arm_l1
);
148 character_offset( ch
, k_chpart_arm_l1
, k_chpart_hand_l
);
150 character_offset( ch
, k_chpart_body1
, k_chpart_arm_r0
);
151 character_offset( ch
, k_chpart_arm_r0
, k_chpart_arm_r1
);
152 character_offset( ch
, k_chpart_arm_r1
, k_chpart_hand_r
);
154 character_offset( ch
, k_chpart_body0
, k_chpart_leg_l0
);
155 character_offset( ch
, k_chpart_leg_l0
, k_chpart_leg_l1
);
156 character_offset( ch
, k_chpart_leg_l1
, k_chpart_foot_l
);
158 character_offset( ch
, k_chpart_body0
, k_chpart_leg_r0
);
159 character_offset( ch
, k_chpart_leg_r0
, k_chpart_leg_r1
);
160 character_offset( ch
, k_chpart_leg_r1
, k_chpart_foot_r
);
162 character_offset( ch
, k_chpart_board
, k_chpart_wb
);
163 character_offset( ch
, k_chpart_board
, k_chpart_wf
);
165 ch
->ik_arm_l
.l1
= v3_length( ch
->offsets
[ k_chpart_arm_l1
] );
166 ch
->ik_arm_l
.l2
= v3_length( ch
->offsets
[ k_chpart_hand_l
] );
167 ch
->ik_arm_r
.l1
= v3_length( ch
->offsets
[ k_chpart_arm_r1
] );
168 ch
->ik_arm_r
.l2
= v3_length( ch
->offsets
[ k_chpart_hand_r
] );
170 ch
->ik_leg_l
.l1
= v3_length( ch
->offsets
[ k_chpart_leg_l1
] );
171 ch
->ik_leg_l
.l2
= v3_length( ch
->offsets
[ k_chpart_foot_l
] );
172 ch
->ik_leg_r
.l1
= v3_length( ch
->offsets
[ k_chpart_leg_r1
] );
173 ch
->ik_leg_r
.l2
= v3_length( ch
->offsets
[ k_chpart_foot_r
] );
175 ch
->ik_body
.l1
= v3_length( ch
->offsets
[ k_chpart_body1
] );
176 ch
->ik_body
.l2
= v3_length( ch
->offsets
[ k_chpart_neck
] );
182 static void align_to_board( struct character
*ch
, enum character_part id
)
184 /* Calculate rotation between board and feet */
185 m4x3f
*mats
= ch
->matrices
;
187 v3f foot_pos
, foot_fwd
, foot_target
, board_norm
, board_origin
;
188 v3_copy( mats
[id
][3], foot_pos
);
189 m3x3_mulv( mats
[id
], (v3f
){1.0f
,0.0f
,0.0f
}, foot_fwd
);
190 v3_add( foot_fwd
, foot_pos
, foot_target
);
192 m3x3_mulv( mats
[k_chpart_board
], (v3f
){0.0f
,1.0f
,0.0f
}, board_norm
);
193 m4x3_mulv( mats
[k_chpart_board
], (v3f
){0.0f
,0.13f
,0.0f
}, board_origin
);
195 vg_line( foot_pos
, foot_target
, 0xff00ff00 );
198 v3_sub( board_origin
, foot_target
, v0
);
199 float t
= v3_dot( v0
, board_norm
) / board_norm
[1];
201 vg_line( foot_pos
, foot_target
, 0xff00ffff );
203 v3_sub( foot_target
, foot_pos
, foot_target
);
204 v3_normalize( foot_target
);
205 float ang
= acosf( v3_dot( foot_target
, foot_fwd
) );
207 v4f qcorrection
; m3x3f correction
;
208 q_axis_angle( qcorrection
, (v3f
){0.0f
,0.0f
,1.0f
}, -ang
);
209 q_m3x3( qcorrection
, correction
);
210 m3x3_mul( mats
[id
], correction
, mats
[id
] );
213 static void character_eval( struct character
*ch
)
215 m4x3f
*mats
= ch
->matrices
;
216 v3f
*offs
= ch
->offsets
;
218 ik_basic( &ch
->ik_body
, mats
[k_chpart_body0
], mats
[k_chpart_body1
],
223 /* TODO: Do this directly via m3x3 */
225 q_axis_angle( body_rotation
, (v3f
){0.0f
,1.0f
,0.0f
}, ch
->rhip
);
226 q_m3x3( body_rotation
, temp
);
227 m3x3_mul( mats
[k_chpart_body0
], temp
, mats
[k_chpart_body0
] );
229 q_axis_angle( body_rotation
, (v3f
){0.0f
,1.0f
,0.0f
}, ch
->rcollar
);
230 q_m3x3( body_rotation
, temp
);
231 m3x3_mul( mats
[k_chpart_body1
], temp
, mats
[k_chpart_body1
] );
234 m4x3_mulv( mats
[k_chpart_body0
], offs
[k_chpart_leg_l0
], ch
->ik_leg_l
.base
);
235 m4x3_mulv( mats
[k_chpart_body0
], offs
[k_chpart_leg_r0
], ch
->ik_leg_r
.base
);
236 m4x3_mulv( mats
[k_chpart_body1
], offs
[k_chpart_arm_l0
], ch
->ik_arm_l
.base
);
237 m4x3_mulv( mats
[k_chpart_body1
], offs
[k_chpart_arm_r0
], ch
->ik_arm_r
.base
);
239 /* IK for arms and legs */
240 ik_basic( &ch
->ik_arm_l
, mats
[k_chpart_arm_l0
], mats
[k_chpart_arm_l1
],
242 ik_basic( &ch
->ik_arm_r
, mats
[k_chpart_arm_r0
], mats
[k_chpart_arm_r1
],
244 ik_basic( &ch
->ik_leg_l
, mats
[k_chpart_leg_l0
], mats
[k_chpart_leg_l1
],
246 ik_basic( &ch
->ik_leg_r
, mats
[k_chpart_leg_r0
], mats
[k_chpart_leg_r1
],
250 m3x3_copy( mats
[k_chpart_arm_l1
], mats
[k_chpart_hand_l
] );
251 m3x3_copy( mats
[k_chpart_arm_r1
], mats
[k_chpart_hand_r
] );
252 m4x3_mulv( mats
[k_chpart_arm_l1
], offs
[k_chpart_hand_l
],
253 mats
[k_chpart_hand_l
][3] );
254 m4x3_mulv( mats
[k_chpart_arm_r1
], offs
[k_chpart_hand_r
],
255 mats
[k_chpart_hand_r
][3] );
258 m3x3_copy( mats
[k_chpart_body1
], mats
[k_chpart_neck
] );
259 m4x3_mulv( mats
[k_chpart_body1
], offs
[k_chpart_neck
],
260 mats
[k_chpart_neck
][3] );
263 q_axis_angle( qhead
, (v3f
){ 0.0f
,1.0f
,0.0f
}, ch
->rhead
);
264 q_m3x3( qhead
, mats
[k_chpart_head
] );
265 //m3x3_mul( mats[k_chpart_neck], mats[k_chpart_head], mats[k_chpart_head] );
266 m4x3_mulv( mats
[k_chpart_neck
], offs
[k_chpart_head
], mats
[k_chpart_head
][3]);
269 m3x3_copy( mats
[k_chpart_leg_l1
], mats
[k_chpart_foot_l
] );
270 m3x3_copy( mats
[k_chpart_leg_r1
], mats
[k_chpart_foot_r
] );
271 m4x3_mulv( mats
[k_chpart_leg_l1
], offs
[k_chpart_foot_l
],
272 mats
[k_chpart_foot_l
][3] );
273 m4x3_mulv( mats
[k_chpart_leg_r1
], offs
[k_chpart_foot_r
],
274 mats
[k_chpart_foot_r
][3] );
276 align_to_board( ch
, k_chpart_foot_l
);
277 align_to_board( ch
, k_chpart_foot_r
);
279 for( int i
=0; i
<PART_COUNT
; i
++ )
280 m4x3_mul( ch
->mroot
, ch
->matrices
[i
], ch
->matrices
[i
] );
283 #define B3D_CO( X, Y, Z ) (v3f){ X, Z, -Y }
285 typedef struct character_pose character_pose
;
286 struct character_pose
288 v3f b0
, b1
, p
, fr
, fl
, pl
, pr
, hl
, hr
, apl
, apr
, cam
;
291 static character_pose pose_aero
=
293 .b0
= {0.0721f
, 0.8167f
, 0.1365f
},
294 .b1
= {-0.0773f
, 1.1559f
, -0.1699f
},
295 .p
= {0.0421f
, 1.1430f
, 0.2803f
},
296 .fr
= {0.0535f
, 0.1312f
, -0.3647f
},
297 .fl
= {-0.0605f
, 0.1464f
, 0.2917f
},
298 .pl
= {-0.1704f
, 0.6889f
, -0.4017f
},
299 .pr
= {0.0672f
, 0.7598f
, -0.5963f
},
300 .hl
= {-0.2153f
, 0.7195f
, -0.1345f
},
301 .hr
= {0.1974f
, 0.7940f
, -0.3522f
},
302 .apl
= {-0.2008f
, 0.9546f
, 0.3687f
},
303 .apr
= {0.3133f
, 0.9299f
, 0.0181f
},
304 .cam
= {-0.3394f
, 1.2661f
, 0.2936f
}
307 static character_pose pose_slide
=
309 .b0
= {0.6732f
, 0.5565f
, -0.0000f
},
310 .b1
= {0.8116f
, 1.0547f
, 0.0613f
},
311 .p
= {1.0404f
, 0.7907f
, 0.0186f
},
312 .fr
= {-0.0030f
, 0.1366f
, -0.4461f
},
313 .fl
= {-0.0030f
, 0.1366f
, 0.3480f
},
314 .pl
= {-0.0887f
, 0.8229f
, 0.3826f
},
315 .pr
= {-0.0887f
, 0.8229f
, -0.4621f
},
316 .hl
= {0.7749f
, 0.5545f
, 0.5310f
},
317 .hr
= {0.5844f
, 1.2445f
, -0.5456f
},
318 .apl
= {1.0999f
, 0.5390f
, 0.2398f
},
319 .apr
= {0.9816f
, 0.9536f
, -0.5463f
},
320 .cam
= {0.9888f
, 1.4037f
, 0.6081f
}
323 static character_pose pose_slide1
=
325 .b0
= {-0.2385f
, 0.6403f
, 0.1368f
},
326 .b1
= {-0.5151f
, 1.1351f
, 0.1380f
},
327 .p
= {-0.1158f
, 1.2118f
, 0.3895f
},
328 .fr
= {-0.0030f
, 0.1323f
, -0.3190f
},
329 .fl
= {-0.0030f
, 0.1323f
, 0.5797f
},
330 .pl
= {-0.6568f
, 0.4305f
, 0.2069f
},
331 .pr
= {-0.6850f
, 0.2740f
, -0.2969f
},
332 .hl
= {-0.7029f
, 0.6132f
, 0.2972f
},
333 .hr
= {-0.2572f
, 1.0104f
, -0.4770f
},
334 .apl
= {-0.4808f
, 0.8480f
, 0.3731f
},
335 .apr
= {-0.0836f
, 1.0480f
, -0.1201f
},
336 .cam
= {-1.0508f
, 1.0769f
, 0.0528f
}
339 static character_pose pose_aero_reverse
=
341 .b0
= {0.0616f
, 0.8167f
, -0.1415f
},
342 .b1
= {0.0148f
, 1.1559f
, 0.1861f
},
343 .p
= {0.0558f
, 1.1430f
, -0.2779f
},
344 .fr
= {0.0535f
, 0.1312f
, -0.3647f
},
345 .fl
= {0.0730f
, 0.1464f
, 0.2917f
},
346 .pl
= {-0.2073f
, 0.6889f
, 0.3839f
},
347 .pr
= {-0.3584f
, 0.4069f
, 0.1032f
},
348 .hl
= {0.1567f
, 0.7195f
, 0.1997f
},
349 .hr
= {-0.3055f
, 0.7940f
, 0.2639f
},
350 .apl
= {0.3143f
, 0.9546f
, -0.2784f
},
351 .apr
= {-0.2885f
, 0.9299f
, -0.1236f
},
352 .cam
= {-0.3394f
, 1.2661f
, -0.2936f
}
355 static character_pose pose_stand
=
357 .b0
= {0.1877f
, 1.0663f
, 0.0063f
},
358 .b1
= {0.0499f
, 1.5564f
, -0.0584f
},
359 .p
= {0.5982f
, 1.2810f
, 0.0842f
},
360 .fr
= {0.0535f
, 0.1312f
, -0.3647f
},
361 .fl
= {0.0354f
, 0.1464f
, 0.2917f
},
362 .pl
= {-0.4325f
, 0.6889f
, 0.1823f
},
363 .pr
= {-0.4794f
, 0.7598f
, -0.3610f
},
364 .hl
= {0.0498f
, 1.0058f
, 0.2317f
},
365 .hr
= {0.0188f
, 0.9786f
, -0.2725f
},
366 .apl
= {0.2898f
, 1.3453f
, 0.2303f
},
367 .apr
= {0.5273f
, 1.2876f
, -0.1848f
},
368 .cam
= {-0.3477f
, 1.5884f
, -0.0019f
}
371 static character_pose pose_stand_reverse
=
373 .b0
= {0.1624f
, 1.0688f
, -0.0632f
},
374 .b1
= {0.0499f
, 1.5564f
, -0.0013f
},
375 .p
= {0.5423f
, 1.2810f
, -0.2368f
},
376 .fr
= {0.0535f
, 0.1312f
, -0.3647f
},
377 .fl
= {0.0354f
, 0.1464f
, 0.2917f
},
378 .pl
= {-0.4325f
, 0.6889f
, 0.4591f
},
379 .pr
= {-0.4794f
, 0.7598f
, -0.0842f
},
380 .hl
= {0.0498f
, 1.0058f
, 0.2317f
},
381 .hr
= {0.0188f
, 0.9786f
, -0.2725f
},
382 .apl
= {0.2898f
, 1.3453f
, 0.0695f
},
383 .apr
= {0.4715f
, 1.2876f
, -0.4982f
},
384 .cam
= {-0.3477f
, 1.5884f
, -0.0730f
}
387 static character_pose pose_fly
=
389 .b0
= {0.2995f
, 0.6819f
, -0.1369f
},
390 .b1
= {0.1618f
, 1.1720f
, -0.2016f
},
391 .p
= {0.7477f
, 0.9173f
, -0.1885f
},
392 .fr
= {0.0535f
, 0.1312f
, -0.3647f
},
393 .fl
= {0.0354f
, 0.1464f
, 0.2917f
},
394 .pl
= {-0.2930f
, 0.4849f
, 0.5307f
},
395 .pr
= {-0.4754f
, 0.4124f
, -0.4874f
},
396 .hl
= {0.2650f
, 1.1897f
, 0.4626f
},
397 .hr
= {0.2494f
, 1.2059f
, -0.7985f
},
398 .apl
= {0.5165f
, 1.0990f
, 0.1655f
},
399 .apr
= {0.6759f
, 1.0661f
, -0.6014f
},
400 .cam
= {-0.2727f
, 1.2606f
, 0.3564f
}
404 void character_pose_blend( struct character
*ch
, character_pose
*pose
, float q
)
406 v3_muladds( ch
->ik_body
.base
, pose
->b0
, q
, ch
->ik_body
.base
);
407 v3_muladds( ch
->ik_body
.end
, pose
->b1
, q
, ch
->ik_body
.end
);
408 v3_muladds( ch
->ik_body
.pole
, pose
->p
, q
, ch
->ik_body
.pole
);
409 v3_muladds( ch
->ik_leg_l
.end
, pose
->fl
, q
, ch
->ik_leg_l
.end
);
410 v3_muladds( ch
->ik_leg_l
.pole
, pose
->pl
, q
, ch
->ik_leg_l
.pole
);
411 v3_muladds( ch
->ik_leg_r
.end
, pose
->fr
, q
, ch
->ik_leg_r
.end
);
412 v3_muladds( ch
->ik_leg_r
.pole
, pose
->pr
, q
, ch
->ik_leg_r
.pole
);
413 v3_muladds( ch
->ik_arm_l
.pole
, pose
->apl
, q
, ch
->ik_arm_l
.pole
);
414 v3_muladds( ch
->ik_arm_r
.pole
, pose
->apr
, q
, ch
->ik_arm_r
.pole
);
415 v3_muladds( ch
->ik_arm_l
.end
, pose
->hl
, q
, ch
->ik_arm_l
.end
);
416 v3_muladds( ch
->ik_arm_r
.end
, pose
->hr
, q
, ch
->ik_arm_r
.end
);
417 v3_muladds( ch
->cam_pos
, pose
->cam
, q
, ch
->cam_pos
);
421 void character_final_pose( struct character
*ch
, v3f cog
,
422 character_pose
*pose
, float q
)
424 character_pose npose
;
425 float dip
= vg_clampf(cog
[1], -1.0f
, 0.3f
) * 0.35f
,
426 tilt
= vg_clampf(cog
[2], -1.0f
, 1.0f
) * 0.3f
;
429 q_axis_angle( rz
, (v3f
){0.0f
,0.0f
,1.0f
}, -cog
[0]*0.6f
);
431 v3_copy( (v3f
){0.0f
,dip
,tilt
}, tr
[3] );
433 m4x3_mulv( tr
, pose
->b0
, npose
.b0
);
434 m4x3_mulv( tr
, pose
->b1
, npose
.b1
);
435 m4x3_mulv( tr
, pose
->p
, npose
.p
);
436 m4x3_mulv( tr
, pose
->pl
, npose
.pl
);
437 m4x3_mulv( tr
, pose
->pr
, npose
.pr
);
438 m4x3_mulv( tr
, pose
->hl
, npose
.hl
);
439 m4x3_mulv( tr
, pose
->hr
, npose
.hr
);
440 m4x3_mulv( tr
, pose
->apl
, npose
.apl
);
441 m4x3_mulv( tr
, pose
->apr
, npose
.apr
);
443 v3_copy( pose
->fr
, npose
.fr
);
444 v3_copy( pose
->fl
, npose
.fl
);
445 v3_copy( pose
->cam
, npose
.cam
);
447 character_pose_blend( ch
, &npose
, q
);
450 static void zero_ik_basic( struct ik_basic
*ik
)
457 static void character_pose_reset( struct character
*ch
)
459 zero_ik_basic( &ch
->ik_body
);
460 zero_ik_basic( &ch
->ik_leg_l
);
461 zero_ik_basic( &ch
->ik_leg_r
);
462 zero_ik_basic( &ch
->ik_arm_l
);
463 zero_ik_basic( &ch
->ik_arm_r
);
464 v3_zero( ch
->cam_pos
);
467 static void character_testpose( struct character
*ch
, float t
)
470 float *hips
= ch
->ik_body
.base
,
471 *collar
= ch
->ik_body
.end
,
472 *pole
= ch
->ik_body
.pole
;
474 hips
[0] = cosf(t
*1.325f
)*0.25f
;
475 hips
[1] = (sinf(t
)*0.2f
+0.6f
) * ch
->parts
[ k_chpart_body0
].pivot
[1];
479 collar
[1] = hips
[1] + (ch
->ik_body
.l1
+ch
->ik_body
.l2
)*(sinf(t
)*0.05f
+0.94f
);
480 collar
[2] = hips
[2] + cosf(t
*0.42f
)*0.01f
;
482 v3_add( hips
, collar
, pole
);
483 v3_muls( pole
, 0.5f
, pole
);
484 v3_add( pole
, (v3f
){ 1.0f
, 0.0f
, 0.0f
}, pole
);
487 float *footl
= ch
->ik_leg_l
.end
,
488 *footr
= ch
->ik_leg_r
.end
,
489 *polel
= ch
->ik_leg_l
.pole
,
490 *poler
= ch
->ik_leg_r
.pole
;
492 footl
[0] = sinf(t
*0.563f
);
498 footr
[2] = cosf(t
*0.672f
);
500 v3_add( hips
, footl
, polel
);
501 v3_muls( polel
, 0.4f
, polel
);
502 v3_add( polel
, (v3f
){ -1.0f
,0.0f
,0.0f
}, polel
);
504 v3_add( hips
, footr
, poler
);
505 v3_muls( poler
, 0.4f
, poler
);
506 v3_add( poler
, (v3f
){ -1.0f
,0.0f
,0.0f
}, poler
);
509 float *arml
= ch
->ik_arm_l
.end
,
510 *armr
= ch
->ik_arm_r
.end
;
511 polel
= ch
->ik_arm_l
.pole
;
512 poler
= ch
->ik_arm_r
.pole
;
514 v3_copy( (v3f
){ 0.0f
, 0.0f
, 1.0f
}, arml
);
515 v3_copy( (v3f
){ 0.0f
, 0.0f
,-1.0f
}, armr
);
516 v3_copy( (v3f
){ 1.0f
, 1.0f
, 0.5f
}, polel
);
517 v3_copy( (v3f
){ 1.0f
, 1.0f
,-0.5f
}, poler
);
520 ch
->rhip
= sinf(t
*0.2f
);
521 ch
->rcollar
= sinf(t
*0.35325f
);
522 q_identity( ch
->qhead
);
524 m4x3_identity( ch
->matrices
[k_chpart_board
] );
525 m4x3_identity( ch
->matrices
[k_chpart_wb
] );
526 m4x3_identity( ch
->matrices
[k_chpart_wf
] );
529 static void character_draw( struct character
*ch
, float temp
)
531 shader_character_use();
532 shader_character_uPv( vg_pv
);
534 vg_tex2d_bind( &tex_pallet
, 0 );
535 shader_character_uTexMain( 0 );
536 shader_character_uOpacity( temp
);
538 glEnable( GL_CULL_FACE
);
539 glCullFace( GL_BACK
);
541 mesh_bind( &ch
->mesh
);
543 for( int i
=4; i
<PART_COUNT
; i
++ )
545 shader_character_uMdl( ch
->matrices
[i
] );
546 submodel_draw( &ch
->parts
[i
] );
549 for( int i
=0; i
<2; i
++ )
553 shader_character_uMdl( ch
->matrices
[i
] );
554 submodel_draw( &ch
->parts
[i
] );
558 shader_character_uMdl( ch
->matrices
[i
+2] );
559 submodel_draw( &ch
->parts
[i
] );
560 shader_character_uMdl( ch
->matrices
[i
] );
561 submodel_draw( &ch
->parts
[i
+2] );
570 static void character_rd_box( struct character
*ch
, enum character_part id
,
573 v3_muls( dims
, -0.5f
, ch
->ragdoll
[id
].bbx
[0] );
574 v3_muls( dims
, 0.5f
, ch
->ragdoll
[id
].bbx
[1] );
579 enum character_part ia
, ib
;
590 static const float k_human_major
= 0.5f
,
591 k_human_minor
= 0.5f
,
592 k_human_major_max
= 0.4f
,
593 k_human_minor_max
= 0.1f
;
595 #define HUMAN_VERTICAL_DEFAULT \
597 .va = {1.0f,0.0f,0.0f}, .vb = {1.0f,0.0f,0.0f}, \
598 .spring = k_human_minor, .ang = k_human_minor_max \
601 .va = {0.0f,1.0f,0.0f}, .vb = {0.0f,1.0f,0.0f}, \
602 .spring = k_human_major, .ang = k_human_major_max \
605 #define HUMAN_ARM_LEFT \
607 .va = {1.0f,0.0f,0.0f}, .vb = {1.0f,0.0f,0.0f}, \
608 .spring = k_human_minor, .ang = k_human_minor_max \
611 .va = {0.0f,0.0f,1.0f}, .vb = {0.0f,0.0f,1.0f}, \
612 .spring = k_human_major, .ang = k_human_major_max \
615 #define HUMAN_ARM_RIGHT \
617 .va = {1.0f,0.0f,0.0f}, .vb = {1.0f,0.0f,0.0f}, \
618 .spring = k_human_minor, .ang = k_human_minor_max \
621 .va = {0.0f,0.0f,-1.0f}, .vb = {0.0f,0.0f,-1.0f}, \
622 .spring = k_human_major, .ang = k_human_major_max \
625 static struct rd_joint rd_joints
[] =
627 { .ia
= k_chpart_leg_l1
, .ib
= k_chpart_foot_l
, HUMAN_VERTICAL_DEFAULT
},
628 { .ia
= k_chpart_leg_r1
, .ib
= k_chpart_foot_r
, HUMAN_VERTICAL_DEFAULT
},
630 { .ia
= k_chpart_body0
, .ib
= k_chpart_body1
, HUMAN_VERTICAL_DEFAULT
},
631 { .ia
= k_chpart_body1
, .ib
= k_chpart_neck
, HUMAN_VERTICAL_DEFAULT
},
632 { .ia
= k_chpart_neck
, .ib
= k_chpart_head
, HUMAN_VERTICAL_DEFAULT
},
633 { .ia
= k_chpart_body0
, .ib
= k_chpart_leg_l0
, HUMAN_VERTICAL_DEFAULT
},
634 { .ia
= k_chpart_leg_l0
, .ib
= k_chpart_leg_l1
, HUMAN_VERTICAL_DEFAULT
},
635 { .ia
= k_chpart_body0
, .ib
= k_chpart_leg_r0
, HUMAN_VERTICAL_DEFAULT
},
636 { .ia
= k_chpart_leg_r0
, .ib
= k_chpart_leg_r1
, HUMAN_VERTICAL_DEFAULT
},
638 { .ia
= k_chpart_body1
, .ib
= k_chpart_arm_l0
, HUMAN_ARM_LEFT
},
639 { .ia
= k_chpart_arm_l0
, .ib
= k_chpart_arm_l1
, HUMAN_ARM_LEFT
},
640 { .ia
= k_chpart_arm_l1
, .ib
= k_chpart_hand_l
, HUMAN_ARM_LEFT
},
642 { .ia
= k_chpart_body1
, .ib
= k_chpart_arm_r0
, HUMAN_ARM_RIGHT
},
643 { .ia
= k_chpart_arm_r0
, .ib
= k_chpart_arm_r1
, HUMAN_ARM_RIGHT
},
644 { .ia
= k_chpart_arm_r1
, .ib
= k_chpart_hand_r
, HUMAN_ARM_RIGHT
}
647 /* Ragdoll should be in rest pose when calling this function */
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
);
659 for( int i
=0; i
<PART_COUNT
; i
++ )
661 float *pivot
= ch
->parts
[i
].pivot
;
662 v3_sub( ch
->ragdoll
[i
].co
, pivot
, ch
->ragdoll
[i
].delta
);
666 static void character_init_ragdoll( struct character
*ch
)
668 v3f
*offs
= ch
->offsets
;
669 rigidbody
*rbs
= ch
->ragdoll
;
672 float chest_width
= fabsf(offs
[k_chpart_arm_r0
][2])*2.0f
,
673 chest_depth
= chest_width
* 0.571f
,
674 chest_height
= offs
[k_chpart_neck
][1];
675 v3f chest_dims
= { chest_depth
, chest_height
, chest_width
};
676 character_rd_box( ch
, k_chpart_body1
, chest_dims
);
678 v3_copy( ch
->parts
[k_chpart_body1
].pivot
, rbs
[k_chpart_body1
].co
);
679 rbs
[k_chpart_body1
].co
[1] += chest_height
*0.5f
;
682 v3f torso_dims
= { chest_depth
,
683 offs
[k_chpart_body1
][1]-offs
[k_chpart_leg_l0
][1],
685 v3_copy( ch
->parts
[k_chpart_body0
].pivot
, rbs
[k_chpart_body0
].co
);
686 character_rd_box( ch
, k_chpart_body0
, torso_dims
);
689 v3f neck_dims
= { chest_depth
*0.5f
,
690 offs
[k_chpart_head
][1],
692 v3_copy( ch
->parts
[k_chpart_neck
].pivot
, rbs
[k_chpart_neck
].co
);
693 rbs
[k_chpart_neck
].co
[1] += neck_dims
[1]*0.5f
;
694 character_rd_box( ch
, k_chpart_neck
, neck_dims
);
697 v3f head_dims
= { chest_width
*0.5f
, chest_width
*0.5f
, chest_width
*0.5f
};
698 v3_copy( ch
->parts
[k_chpart_head
].pivot
, rbs
[k_chpart_head
].co
);
699 rbs
[k_chpart_head
].co
[1] += head_dims
[1]*0.5f
;
700 character_rd_box( ch
, k_chpart_head
, head_dims
);
703 v3f ua_dims
= { 0.0f
, 0.0f
, fabsf(offs
[k_chpart_arm_l1
][2]) };
704 ua_dims
[1] = 0.38f
*ua_dims
[2];
705 ua_dims
[0] = 0.38f
*ua_dims
[2];
706 v3f la_dims
= { ua_dims
[0], ua_dims
[1], fabsf(offs
[k_chpart_hand_l
][2]) };
707 v3f hand_dims
= { ua_dims
[1], ua_dims
[1]*0.5f
, ua_dims
[1] };
709 character_rd_box( ch
, k_chpart_arm_l0
, ua_dims
);
710 character_rd_box( ch
, k_chpart_arm_r0
, ua_dims
);
711 character_rd_box( ch
, k_chpart_arm_l1
, la_dims
);
712 character_rd_box( ch
, k_chpart_arm_r1
, la_dims
);
713 character_rd_box( ch
, k_chpart_hand_l
, hand_dims
);
714 character_rd_box( ch
, k_chpart_hand_r
, hand_dims
);
716 v3_copy( ch
->parts
[k_chpart_arm_l0
].pivot
, rbs
[k_chpart_arm_l0
].co
);
717 rbs
[k_chpart_arm_l0
].co
[2] += ua_dims
[2] * 0.5f
;
718 v3_copy( ch
->parts
[k_chpart_arm_l1
].pivot
, rbs
[k_chpart_arm_l1
].co
);
719 rbs
[k_chpart_arm_l1
].co
[2] += la_dims
[2] * 0.5f
;
720 v3_copy( ch
->parts
[k_chpart_hand_l
].pivot
, rbs
[k_chpart_hand_l
].co
);
721 rbs
[k_chpart_hand_l
].co
[2] += hand_dims
[2] * 0.5f
;
723 v3_copy( ch
->parts
[k_chpart_arm_r0
].pivot
, rbs
[k_chpart_arm_r0
].co
);
724 rbs
[k_chpart_arm_r0
].co
[2] -= ua_dims
[2] * 0.5f
;
725 v3_copy( ch
->parts
[k_chpart_arm_r1
].pivot
, rbs
[k_chpart_arm_r1
].co
);
726 rbs
[k_chpart_arm_r1
].co
[2] -= la_dims
[2] * 0.5f
;
727 v3_copy( ch
->parts
[k_chpart_hand_r
].pivot
, rbs
[k_chpart_hand_r
].co
);
728 rbs
[k_chpart_hand_r
].co
[2] -= hand_dims
[2] * 0.5f
;
731 v3f ul_dims
= { 0.0f
, fabsf(offs
[k_chpart_leg_l1
][1]), 0.0f
};
732 ul_dims
[0] = 0.38f
*ul_dims
[1];
733 ul_dims
[2] = 0.38f
*ul_dims
[1];
734 v3f ll_dims
= { ul_dims
[0], fabsf(offs
[k_chpart_foot_l
][1]), ul_dims
[2] };
735 v3f foot_dims
= { 2.0f
*ul_dims
[0], ul_dims
[0], ul_dims
[0] };
737 character_rd_box( ch
, k_chpart_leg_l0
, ul_dims
);
738 character_rd_box( ch
, k_chpart_leg_r0
, ul_dims
);
739 character_rd_box( ch
, k_chpart_leg_l1
, ll_dims
);
740 character_rd_box( ch
, k_chpart_leg_r1
, ll_dims
);
741 character_rd_box( ch
, k_chpart_foot_l
, foot_dims
);
742 character_rd_box( ch
, k_chpart_foot_r
, foot_dims
);
744 v3_copy( ch
->parts
[k_chpart_leg_l0
].pivot
, rbs
[k_chpart_leg_l0
].co
);
745 rbs
[k_chpart_leg_l0
].co
[1] -= ul_dims
[1] * 0.5f
;
746 v3_copy( ch
->parts
[k_chpart_leg_l1
].pivot
, rbs
[k_chpart_leg_l1
].co
);
747 rbs
[k_chpart_leg_l1
].co
[1] -= ll_dims
[1] * 0.5f
;
748 v3_copy( ch
->parts
[k_chpart_foot_l
].pivot
, rbs
[k_chpart_foot_l
].co
);
749 rbs
[k_chpart_foot_l
].co
[1] -= foot_dims
[1] * 0.5f
;
750 rbs
[k_chpart_foot_l
].co
[0] -= foot_dims
[0] * 0.5f
;
752 v3_copy( ch
->parts
[k_chpart_leg_r0
].pivot
, rbs
[k_chpart_leg_r0
].co
);
753 rbs
[k_chpart_leg_r0
].co
[1] -= ul_dims
[1] * 0.5f
;
754 v3_copy( ch
->parts
[k_chpart_leg_r1
].pivot
, rbs
[k_chpart_leg_r1
].co
);
755 rbs
[k_chpart_leg_r1
].co
[1] -= ll_dims
[1] * 0.5f
;
756 v3_copy( ch
->parts
[k_chpart_foot_r
].pivot
, rbs
[k_chpart_foot_r
].co
);
757 rbs
[k_chpart_foot_r
].co
[1] -= foot_dims
[1] * 0.5f
;
758 rbs
[k_chpart_foot_r
].co
[0] -= foot_dims
[0] * 0.5f
;
760 character_rd_box( ch
, k_chpart_sock_l
, foot_dims
);
761 character_rd_box( ch
, k_chpart_sock_r
, foot_dims
);
762 v3_copy( rbs
[k_chpart_foot_l
].co
, rbs
[k_chpart_sock_l
].co
);
763 v3_copy( rbs
[k_chpart_foot_r
].co
, rbs
[k_chpart_sock_r
].co
);
765 box_copy( (boxf
){{-0.2f
,-0.2f
,-0.7f
},{0.2f
,0.2f
,0.7f
}},
766 rbs
[k_chpart_board
].bbx
);
768 for( int i
=0; i
<PART_COUNT
; i
++ )
769 rb_init( &ch
->ragdoll
[i
] );
771 character_init_ragdoll_joints( ch
);
774 static void character_ragdoll_go( struct character
*ch
, v3f pos
)
776 character_init_ragdoll( ch
);
777 for( int i
=0; i
<PART_COUNT
; i
++ )
778 v3_add( pos
, ch
->ragdoll
[i
].co
, ch
->ragdoll
[i
].co
);
781 static void character_ragdoll_copypose( struct character
*ch
, v3f v
)
783 for( int i
=0; i
<PART_COUNT
; i
++ )
785 rigidbody
*rb
= &ch
->ragdoll
[i
];
787 m4x3_mulv( ch
->matrices
[i
], rb
->delta
, rb
->co
);
788 m3x3_q( ch
->matrices
[i
], rb
->q
);
791 rb
->manifold_count
= 0; /* ? */
793 rb_update_transform( rb
);
796 float vel
= v3_length(v
);
802 static void character_mimic_ragdoll( struct character
*ch
)
804 for( int i
=0; i
<PART_COUNT
; i
++ )
806 rigidbody
*rb
= &ch
->ragdoll
[i
];
807 v3f
*mat
= ch
->matrices
[i
];
809 m3x3_copy( rb
->to_world
, mat
);
811 v3_negate( rb
->delta
, inv_delta
);
812 m4x3_mulv( rb
->to_world
, inv_delta
, mat
[3] );
815 /* Attach wheels to board */
816 m3x3_copy( ch
->matrices
[k_chpart_board
], ch
->matrices
[k_chpart_wb
] );
817 m3x3_copy( ch
->matrices
[k_chpart_board
], ch
->matrices
[k_chpart_wf
] );
818 m4x3_mulv( ch
->matrices
[k_chpart_board
], ch
->offsets
[k_chpart_wb
],
819 ch
->matrices
[k_chpart_wb
][3] );
820 m4x3_mulv( ch
->matrices
[k_chpart_board
], ch
->offsets
[k_chpart_wf
],
821 ch
->matrices
[k_chpart_wf
][3] );
824 static void character_debug_ragdoll( struct character
*ch
)
826 rb_debug( &ch
->ragdoll
[k_chpart_body0
], 0xffffffff );
827 rb_debug( &ch
->ragdoll
[k_chpart_body1
], 0xffffffff );
828 rb_debug( &ch
->ragdoll
[k_chpart_neck
], 0xff00ff00 );
829 rb_debug( &ch
->ragdoll
[k_chpart_head
], 0xff00ff00 );
831 rb_debug( &ch
->ragdoll
[k_chpart_arm_l0
], 0xffffa500 );
832 rb_debug( &ch
->ragdoll
[k_chpart_arm_l1
], 0xffffa500 );
833 rb_debug( &ch
->ragdoll
[k_chpart_hand_l
], 0xffffa500 );
835 rb_debug( &ch
->ragdoll
[k_chpart_arm_r0
], 0xff00a5ff );
836 rb_debug( &ch
->ragdoll
[k_chpart_arm_r1
], 0xff00a5ff );
837 rb_debug( &ch
->ragdoll
[k_chpart_hand_r
], 0xff00a5ff );
839 rb_debug( &ch
->ragdoll
[k_chpart_leg_l0
], 0xffffa500 );
840 rb_debug( &ch
->ragdoll
[k_chpart_leg_l1
], 0xffffa500 );
841 rb_debug( &ch
->ragdoll
[k_chpart_foot_l
], 0xffffa500 );
842 rb_debug( &ch
->ragdoll
[k_chpart_leg_r0
], 0xff00a5ff );
843 rb_debug( &ch
->ragdoll
[k_chpart_leg_r1
], 0xff00a5ff );
844 rb_debug( &ch
->ragdoll
[k_chpart_foot_r
], 0xff00a5ff );
847 static void character_ragdoll_iter( struct character
*ch
)
849 for( int i
=0; i
<PART_COUNT
; i
++ )
851 rb_manifold_reset( &ch
->ragdoll
[i
] );
852 rb_build_manifold_terrain( &ch
->ragdoll
[i
] );
855 int len
= bh_select( &world
.bhcubes
, ch
->ragdoll
[i
].bbx_world
,
858 for( int j
=0; j
<len
; j
++ )
859 rb_build_manifold_rb_static( &ch
->ragdoll
[i
],
860 &world
.temp_rbs
[colliders
[j
]] );
866 for( int i
=0; i
<2; i
++ )
868 shoe_vel
[i
] = v3_length( ch
->ragdoll
[i
].v
);
870 for( int i
=0; i
<20; i
++ )
872 float const k_springfactor
= 1.0f
/20.0f
;
874 for( int j
=0; j
<PART_COUNT
; j
++ )
875 rb_constraint_manifold( &ch
->ragdoll
[j
] );
877 for( int j
=0; j
<vg_list_size(rd_joints
); j
++ )
879 struct rd_joint
*joint
= &rd_joints
[j
];
880 rigidbody
*rba
= &ch
->ragdoll
[joint
->ia
],
881 *rbb
= &ch
->ragdoll
[joint
->ib
];
883 rb_constraint_position( rba
, joint
->lca
, rbb
, joint
->lcb
);
884 rb_constraint_angle( rba
, joint
->maj
.va
, rbb
, joint
->maj
.vb
,
886 joint
->maj
.spring
* k_springfactor
);
888 rb_constraint_angle( rba
, joint
->min
.va
, rbb
, joint
->min
.vb
,
890 joint
->min
.spring
* k_springfactor
);
894 for( int j
=0; j
<vg_list_size(rd_joints
); j
++ )
896 struct rd_joint
*joint
= &rd_joints
[j
];
897 rigidbody
*rba
= &ch
->ragdoll
[joint
->ia
],
898 *rbb
= &ch
->ragdoll
[joint
->ib
];
899 rb_angle_limit_force( rba
, joint
->min
.va
, rbb
, joint
->min
.vb
,
901 rb_angle_limit_force( rba
, joint
->maj
.va
, rbb
, joint
->maj
.vb
,
905 for( int i
=0; i
<PART_COUNT
; i
++ )
906 rb_iter( &ch
->ragdoll
[i
] );
908 for( int i
=0; i
<2; i
++ )
912 float a
= v3_length( ch
->ragdoll
[i
].v
) - shoe_vel
[i
];
918 rigidbody
*src
= &ch
->ragdoll
[k_chpart_foot_l
];
919 rigidbody
*dst
= &ch
->ragdoll
[k_chpart_sock_l
];
921 v3_copy( src
->co
, dst
->co
);
922 v3_copy( src
->v
, dst
->v
);
923 v3_copy( src
->q
, dst
->q
);
924 v3_copy( src
->I
, dst
->I
);
929 for( int i
=0; i
<PART_COUNT
; i
++ )
930 rb_update_transform( &ch
->ragdoll
[i
] );