clean up rigidbody
[carveJwlIkooP6JGAAIwe30JlM.git] / player_model.h
1 #ifndef CHARACTER_H
2 #define CHARACTER_H
3
4 #include "common.h"
5 #include "model.h"
6 #include "rigidbody.h"
7 #include "render.h"
8 #include "skeleton.h"
9 #include "world.h"
10 #include "skeleton_animator.h"
11 #include "shaders/viewchar.h"
12
13 vg_tex2d tex_characters = { .path = "textures/ch_gradient.qoi" };
14
15 static void character_register(void)
16 {
17 shader_viewchar_register();
18 }
19
20 static void character_init(void)
21 {
22 vg_tex2d_init( (vg_tex2d *[]){ &tex_characters }, 1 );
23 }
24
25 struct character
26 {
27 glmesh mesh;
28 struct skeleton sk;
29 struct skeleton_anim *anim_stand,
30 *anim_highg,
31 *anim_slide,
32 *anim_air,
33 *anim_push, *anim_push_reverse,
34 *anim_ollie;
35
36 u32 id_hip,
37 id_ik_hand_l,
38 id_ik_hand_r,
39 id_ik_elbow_l,
40 id_ik_elbow_r,
41 id_head;
42
43 v3f cam_pos;
44
45 struct ragdoll_part
46 {
47 u32 bone_id;
48 v3f offset;
49
50 u32 use_limits;
51 v3f limits[2];
52
53 rigidbody rb;
54 u32 parent;
55 }
56 *ragdoll;
57 u32 ragdoll_count;
58
59 int shoes[2];
60 };
61
62 static int character_load( struct character *ch, const char *name )
63 {
64 char buf[64];
65
66 snprintf( buf, sizeof(buf)-1, "models/%s.mdl", name );
67 mdl_header *src = mdl_load( buf );
68
69 if( !src )
70 return 0;
71
72 mdl_unpack_glmesh( src, &ch->mesh );
73
74 skeleton_setup( &ch->sk, src );
75 ch->anim_stand = skeleton_get_anim( &ch->sk, "pose_stand" );
76 ch->anim_highg = skeleton_get_anim( &ch->sk, "pose_highg" );
77 ch->anim_slide = skeleton_get_anim( &ch->sk, "pose_slide" );
78 ch->anim_air = skeleton_get_anim( &ch->sk, "pose_air" );
79 ch->anim_push = skeleton_get_anim( &ch->sk, "push" );
80 ch->anim_push_reverse = skeleton_get_anim( &ch->sk, "push_reverse" );
81 ch->anim_ollie = skeleton_get_anim( &ch->sk, "ollie" );
82
83 ch->id_hip = skeleton_bone_id( &ch->sk, "hips" );
84 ch->id_ik_hand_l = skeleton_bone_id( &ch->sk, "hand.IK.L" );
85 ch->id_ik_hand_r = skeleton_bone_id( &ch->sk, "hand.IK.R" );
86 ch->id_ik_elbow_l = skeleton_bone_id( &ch->sk, "elbow.L" );
87 ch->id_ik_elbow_r = skeleton_bone_id( &ch->sk, "elbow.R" );
88 ch->id_head = skeleton_bone_id( &ch->sk, "head" );
89
90 /* setup ragdoll */
91
92 if( ch->sk.collider_count )
93 {
94 vg_info( "Alloc: %d\n", ch->sk.collider_count );
95 ch->ragdoll = malloc(sizeof(struct ragdoll_part) * ch->sk.collider_count);
96 ch->ragdoll_count = 0;
97
98 for( u32 i=0; i<ch->sk.bone_count; i ++ )
99 {
100 struct skeleton_bone *bone = &ch->sk.bones[i];
101
102 if( bone->collider )
103 {
104 struct ragdoll_part *rp = &ch->ragdoll[ ch->ragdoll_count ++ ];
105 rp->bone_id = i;
106
107 v3f delta;
108 v3_sub( bone->hitbox[1], bone->hitbox[0], delta );
109 v3_muls( delta, 0.5f, delta );
110
111 v3_add( bone->hitbox[0], delta, rp->offset );
112
113 v3_copy( delta, rp->rb.bbx[1] );
114 v3_muls( delta, -1.0f, rp->rb.bbx[0] );
115
116 q_identity( rp->rb.q );
117 v3_add( bone->co, rp->offset, rp->rb.co );
118 rp->rb.type = k_rb_shape_box;
119 rp->rb.is_world = 0;
120 rp->parent = 0xffffffff;
121
122 if( bone->parent )
123 {
124 for( u32 j=0; j<ch->ragdoll_count; j++ )
125 {
126 if( ch->ragdoll[ j ].bone_id == bone->parent )
127 {
128 rp->parent = j;
129 break;
130 }
131 }
132 }
133
134 /* TODO: refactor to use this style elswhere */
135 struct mdl_node *pnode = mdl_node_from_id( src, bone->orig_node );
136 struct classtype_bone *bone_inf = mdl_get_entdata( src, pnode );
137
138 rp->use_limits = bone_inf->use_limits;
139 v3_copy( bone_inf->angle_limits[0], rp->limits[0] );
140 v3_copy( bone_inf->angle_limits[1], rp->limits[1] );
141
142 rb_init( &rp->rb );
143 }
144 }
145 }
146
147 free( src );
148 return 1;
149 }
150
151 static void character_eval( struct character *ch ){}
152 static void character_draw( struct character *ch, float temp, m4x3f camera ){}
153 static void character_init_ragdoll_joints( struct character *ch ){}
154 static void character_init_ragdoll( struct character *ch ){}
155 static void character_ragdoll_go( struct character *ch, v3f pos ){}
156
157 static void character_mimic_ragdoll( struct character *ch )
158 {
159 for( int i=0; i<ch->ragdoll_count; i++ )
160 {
161 struct ragdoll_part *part = &ch->ragdoll[i];
162 m4x3f offset;
163 m3x3_identity(offset);
164 v3_negate( part->offset, offset[3] );
165 m4x3_mul( part->rb.to_world, offset, ch->sk.final_mtx[part->bone_id] );
166 }
167
168 skeleton_apply_inverses( &ch->sk );
169 }
170
171 static void character_ragdoll_copypose( struct character *ch, v3f v )
172 {
173 for( int i=0; i<ch->ragdoll_count; i++ )
174 {
175 struct ragdoll_part *part = &ch->ragdoll[i];
176
177 v3f pos, offset;
178 u32 bone = part->bone_id;
179
180 m4x3_mulv( ch->sk.final_mtx[bone], ch->sk.bones[bone].co, pos );
181 m3x3_mulv( ch->sk.final_mtx[bone], part->offset, offset );
182 v3_add( pos, offset, part->rb.co );
183 m3x3_q( ch->sk.final_mtx[bone], part->rb.q );
184 v3_copy( v, part->rb.v );
185 v3_zero( part->rb.w );
186
187 rb_update_transform( &part->rb );
188 }
189 }
190
191 static void character_debug_ragdoll( struct character *ch )
192 {
193 for( u32 i=0; i<ch->ragdoll_count; i ++ )
194 rb_debug( &ch->ragdoll[i].rb, 0xff00ff00 );
195 }
196
197 static void character_ragdoll_iter( struct character *ch )
198 {
199 rb_solver_reset();
200
201 for( int i=0; i<ch->ragdoll_count; i ++ )
202 rb_collide( &ch->ragdoll[i].rb, &world.rb_geo );
203
204 rb_presolve_contacts( rb_contact_buffer, rb_contact_count );
205
206 v3f rv;
207
208 float shoe_vel[2] = {0.0f,0.0f};
209 for( int i=0; i<2; i++ )
210 if( ch->shoes[i] )
211 shoe_vel[i] = v3_length( ch->ragdoll[i].rb.v );
212
213 for( int j=0; j<ch->ragdoll_count; j++ )
214 {
215 struct ragdoll_part *pj = &ch->ragdoll[j];
216 struct skeleton_bone *bj = &ch->sk.bones[pj->bone_id];
217
218 if( pj->parent != 0xffffffff )
219 {
220 struct ragdoll_part *pp = &ch->ragdoll[pj->parent];
221 struct skeleton_bone *bp = &ch->sk.bones[pp->bone_id];
222
223 v3f lca, lcb;
224 v3_negate( pj->offset, lca );
225 v3_add( bp->co, pp->offset, lcb );
226 v3_sub( bj->co, lcb, lcb );
227
228 rb_debug_constraint_position( &pj->rb, lca, &pp->rb, lcb );
229
230 if( pj->use_limits )
231 {
232 rb_debug_constraint_limits( &pj->rb, &pp->rb, lca, pj->limits );
233 }
234 }
235 }
236
237 /* CONSTRAINTS */
238 for( int i=0; i<10; i++ )
239 {
240 rb_solve_contacts( rb_contact_buffer, rb_contact_count );
241
242 for( int j=0; j<ch->ragdoll_count; j++ )
243 {
244 struct ragdoll_part *pj = &ch->ragdoll[j];
245 struct skeleton_bone *bj = &ch->sk.bones[pj->bone_id];
246
247 if( pj->parent != 0xffffffff && pj->use_limits )
248 {
249 struct ragdoll_part *pp = &ch->ragdoll[pj->parent];
250 struct skeleton_bone *bp = &ch->sk.bones[pp->bone_id];
251
252 v3f lca, lcb;
253 v3_negate( pj->offset, lca );
254 v3_add( bp->co, pp->offset, lcb );
255 v3_sub( bj->co, lcb, lcb );
256
257 rb_constraint_position( &pj->rb, lca, &pp->rb, lcb );
258
259 rb_constraint_limits( &pj->rb, lca, &pp->rb, lcb, pj->limits );
260 }
261 }
262 }
263
264 /* INTEGRATION */
265 for( int i=0; i<ch->ragdoll_count; i++ )
266 rb_iter( &ch->ragdoll[i].rb );
267
268 /* SHOES */
269
270 for( int i=0; i<ch->ragdoll_count; i++ )
271 rb_update_transform( &ch->ragdoll[i].rb );
272 }
273
274 #endif