2 * Copyright 2021-2022 (C) Mount0 Software, Harry Godden - All Rights Reserved
10 #include "rigidbody.h"
14 #include "skeleton_animator.h"
15 #include "shaders/viewchar.h"
17 static float k_ragdoll_floatyiness
= 10.0f
,
18 k_ragdoll_floatydrag
= 1.0f
;
20 vg_tex2d tex_characters
= { .path
= "textures/ch_gradient.qoi" };
22 static void character_register(void)
24 shader_viewchar_register();
27 static void character_init(void)
29 vg_tex2d_init( (vg_tex2d
*[]){ &tex_characters
}, 1 );
36 struct skeleton_anim
*anim_stand
,
40 *anim_push
, *anim_push_reverse
,
41 *anim_ollie
, *anim_ollie_reverse
,
42 *anim_grabs
, *anim_stop
,
43 *anim_walk
, *anim_run
, *anim_idle
;
71 static int character_load( struct character
*ch
, const char *name
)
75 snprintf( buf
, sizeof(buf
)-1, "models/%s.mdl", name
);
76 mdl_header
*src
= mdl_load( buf
);
81 mdl_unpack_glmesh( src
, &ch
->mesh
);
83 skeleton_setup( &ch
->sk
, src
);
84 ch
->anim_stand
= skeleton_get_anim( &ch
->sk
, "pose_stand" );
85 ch
->anim_highg
= skeleton_get_anim( &ch
->sk
, "pose_highg" );
86 ch
->anim_slide
= skeleton_get_anim( &ch
->sk
, "pose_slide" );
87 ch
->anim_air
= skeleton_get_anim( &ch
->sk
, "pose_air" );
88 ch
->anim_push
= skeleton_get_anim( &ch
->sk
, "push" );
89 ch
->anim_push_reverse
= skeleton_get_anim( &ch
->sk
, "push_reverse" );
90 ch
->anim_ollie
= skeleton_get_anim( &ch
->sk
, "ollie" );
91 ch
->anim_ollie_reverse
= skeleton_get_anim( &ch
->sk
, "ollie_reverse" );
92 ch
->anim_grabs
= skeleton_get_anim( &ch
->sk
, "grabs" );
93 ch
->anim_walk
= skeleton_get_anim( &ch
->sk
, "walk" );
94 ch
->anim_run
= skeleton_get_anim( &ch
->sk
, "run" );
95 ch
->anim_idle
= skeleton_get_anim( &ch
->sk
, "idle_cycle" );
97 ch
->id_hip
= skeleton_bone_id( &ch
->sk
, "hips" );
98 ch
->id_ik_hand_l
= skeleton_bone_id( &ch
->sk
, "hand.IK.L" );
99 ch
->id_ik_hand_r
= skeleton_bone_id( &ch
->sk
, "hand.IK.R" );
100 ch
->id_ik_elbow_l
= skeleton_bone_id( &ch
->sk
, "elbow.L" );
101 ch
->id_ik_elbow_r
= skeleton_bone_id( &ch
->sk
, "elbow.R" );
102 ch
->id_head
= skeleton_bone_id( &ch
->sk
, "head" );
106 if( ch
->sk
.collider_count
)
108 vg_info( "Alloc: %d\n", ch
->sk
.collider_count
);
109 ch
->ragdoll
= malloc(sizeof(struct ragdoll_part
) * ch
->sk
.collider_count
);
110 ch
->ragdoll_count
= 0;
112 for( u32 i
=0; i
<ch
->sk
.bone_count
; i
++ )
114 struct skeleton_bone
*bone
= &ch
->sk
.bones
[i
];
118 struct ragdoll_part
*rp
= &ch
->ragdoll
[ ch
->ragdoll_count
++ ];
122 v3_sub( bone
->hitbox
[1], bone
->hitbox
[0], delta
);
123 v3_muls( delta
, 0.5f
, delta
);
125 v3_add( bone
->hitbox
[0], delta
, rp
->offset
);
127 v3_copy( delta
, rp
->rb
.bbx
[1] );
128 v3_muls( delta
, -1.0f
, rp
->rb
.bbx
[0] );
130 q_identity( rp
->rb
.q
);
131 v3_add( bone
->co
, rp
->offset
, rp
->rb
.co
);
132 rp
->rb
.type
= k_rb_shape_box
;
134 rp
->parent
= 0xffffffff;
138 for( u32 j
=0; j
<ch
->ragdoll_count
; j
++ )
140 if( ch
->ragdoll
[ j
].bone_id
== bone
->parent
)
148 /* TODO: refactor to use this style elswhere */
149 struct mdl_node
*pnode
= mdl_node_from_id( src
, bone
->orig_node
);
150 struct classtype_bone
*bone_inf
= mdl_get_entdata( src
, pnode
);
152 rp
->use_limits
= bone_inf
->use_limits
;
153 v3_copy( bone_inf
->angle_limits
[0], rp
->limits
[0] );
154 v3_copy( bone_inf
->angle_limits
[1], rp
->limits
[1] );
165 static void character_eval( struct character
*ch
){}
166 static void character_draw( struct character
*ch
, float temp
, m4x3f camera
){}
167 static void character_init_ragdoll_joints( struct character
*ch
){}
168 static void character_init_ragdoll( struct character
*ch
){}
169 static void character_ragdoll_go( struct character
*ch
, v3f pos
){}
171 static void character_mimic_ragdoll( struct character
*ch
)
173 for( int i
=0; i
<ch
->ragdoll_count
; i
++ )
175 struct ragdoll_part
*part
= &ch
->ragdoll
[i
];
177 m3x3_identity(offset
);
178 v3_negate( part
->offset
, offset
[3] );
179 m4x3_mul( part
->rb
.to_world
, offset
, ch
->sk
.final_mtx
[part
->bone_id
] );
182 skeleton_apply_inverses( &ch
->sk
);
185 static void character_ragdoll_copypose( struct character
*ch
, v3f v
)
187 for( int i
=0; i
<ch
->ragdoll_count
; i
++ )
189 struct ragdoll_part
*part
= &ch
->ragdoll
[i
];
192 u32 bone
= part
->bone_id
;
194 m4x3_mulv( ch
->sk
.final_mtx
[bone
], ch
->sk
.bones
[bone
].co
, pos
);
195 m3x3_mulv( ch
->sk
.final_mtx
[bone
], part
->offset
, offset
);
196 v3_add( pos
, offset
, part
->rb
.co
);
197 m3x3_q( ch
->sk
.final_mtx
[bone
], part
->rb
.q
);
198 v3_copy( v
, part
->rb
.v
);
199 v3_zero( part
->rb
.w
);
201 rb_update_transform( &part
->rb
);
205 static void character_debug_ragdoll( struct character
*ch
)
207 for( u32 i
=0; i
<ch
->ragdoll_count
; i
++ )
208 rb_debug( &ch
->ragdoll
[i
].rb
, 0xff00ff00 );
211 static void character_ragdoll_iter( struct character
*ch
)
215 for( int i
=0; i
<ch
->ragdoll_count
; i
++ )
216 rb_collide( &ch
->ragdoll
[i
].rb
, &world
.rb_geo
);
218 rb_presolve_contacts( rb_contact_buffer
, rb_contact_count
);
222 float shoe_vel
[2] = {0.0f
,0.0f
};
223 for( int i
=0; i
<2; i
++ )
225 shoe_vel
[i
] = v3_length( ch
->ragdoll
[i
].rb
.v
);
227 for( int j
=0; j
<ch
->ragdoll_count
; j
++ )
229 struct ragdoll_part
*pj
= &ch
->ragdoll
[j
];
230 struct skeleton_bone
*bj
= &ch
->sk
.bones
[pj
->bone_id
];
232 if( pj
->parent
!= 0xffffffff )
234 struct ragdoll_part
*pp
= &ch
->ragdoll
[pj
->parent
];
235 struct skeleton_bone
*bp
= &ch
->sk
.bones
[pp
->bone_id
];
238 v3_negate( pj
->offset
, lca
);
239 v3_add( bp
->co
, pp
->offset
, lcb
);
240 v3_sub( bj
->co
, lcb
, lcb
);
242 rb_debug_constraint_position( &pj
->rb
, lca
, &pp
->rb
, lcb
);
246 rb_debug_constraint_limits( &pj
->rb
, &pp
->rb
, lca
, pj
->limits
);
250 v4f plane
= {0.0f
,1.0f
,0.0f
,0.0f
};
251 rb_effect_simple_bouyency( &pj
->rb
, plane
, k_ragdoll_floatyiness
,
252 k_ragdoll_floatydrag
);
256 for( int i
=0; i
<10; i
++ )
258 rb_solve_contacts( rb_contact_buffer
, rb_contact_count
);
260 for( int j
=0; j
<ch
->ragdoll_count
; j
++ )
262 struct ragdoll_part
*pj
= &ch
->ragdoll
[j
];
263 struct skeleton_bone
*bj
= &ch
->sk
.bones
[pj
->bone_id
];
265 if( pj
->parent
!= 0xffffffff && pj
->use_limits
)
267 struct ragdoll_part
*pp
= &ch
->ragdoll
[pj
->parent
];
268 struct skeleton_bone
*bp
= &ch
->sk
.bones
[pp
->bone_id
];
271 v3_negate( pj
->offset
, lca
);
272 v3_add( bp
->co
, pp
->offset
, lcb
);
273 v3_sub( bj
->co
, lcb
, lcb
);
275 rb_constraint_position( &pj
->rb
, lca
, &pp
->rb
, lcb
);
277 rb_constraint_limits( &pj
->rb
, lca
, &pp
->rb
, lcb
, pj
->limits
);
283 for( int i
=0; i
<ch
->ragdoll_count
; i
++ )
284 rb_iter( &ch
->ragdoll
[i
].rb
);
288 for( int i
=0; i
<ch
->ragdoll_count
; i
++ )
289 rb_update_transform( &ch
->ragdoll
[i
].rb
);