X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=player_ragdoll.h;h=e4711634a218e6f3e79c60a105c4165ed070777c;hb=d6171f1c56789b2ca79efa3313fbbf74a13bda7a;hp=2bb0ae4220b1f7b3317f8b8dcbb490bdd58cdf96;hpb=343d594b37f42a94f220a5089c9db36da9da3ab4;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/player_ragdoll.h b/player_ragdoll.h index 2bb0ae4..e471163 100644 --- a/player_ragdoll.h +++ b/player_ragdoll.h @@ -7,6 +7,7 @@ #include "rigidbody.h" #include "player_model.h" #include "world.h" +#include "audio.h" struct player_ragdoll { @@ -42,8 +43,7 @@ VG_STATIC void player_init_ragdoll_bone_collider( struct skeleton_bone *bone, { m4x3_identity( rp->collider_mtx ); - if( bone->flags & k_bone_flag_collider_box ) - { + if( bone->collider == k_bone_collider_box ){ v3f delta; v3_sub( bone->hitbox[1], bone->hitbox[0], delta ); v3_muls( delta, 0.5f, delta ); @@ -56,18 +56,15 @@ VG_STATIC void player_init_ragdoll_bone_collider( struct skeleton_bone *bone, rp->rb.type = k_rb_shape_box; rp->colour = 0xffcccccc; } - else if( bone->flags & k_bone_flag_collider_capsule ) - { + else if( bone->collider == k_bone_collider_capsule ){ v3f v0, v1, tx, ty; v3_sub( bone->hitbox[1], bone->hitbox[0], v0 ); int major_axis = 0; float largest = -1.0f; - for( int i=0; i<3; i ++ ) - { - if( fabsf( v0[i] ) > largest ) - { + for( int i=0; i<3; i ++ ){ + if( fabsf( v0[i] ) > largest ){ largest = fabsf( v0[i] ); major_axis = i; } @@ -93,8 +90,10 @@ VG_STATIC void player_init_ragdoll_bone_collider( struct skeleton_bone *bone, rp->colour = 0xff000000 | (0xff << (major_axis*8)); } - else + else{ + vg_warn( "type: %u\n", bone->collider ); vg_fatal_exit_loop( "Invalid bone collider type" ); + } m4x3_invert_affine( rp->collider_mtx, rp->inv_collider_mtx ); @@ -133,14 +132,13 @@ VG_STATIC void player_setup_ragdoll_from_avatar( struct player_ragdoll *rd, rd->position_constraints_count = 0; rd->cone_constraints_count = 0; - for( u32 i=0; isk.bone_count; i ++ ) - { + for( u32 i=1; isk.bone_count; i ++ ){ struct skeleton_bone *bone = &av->sk.bones[i]; /* * Bones with colliders */ - if( !(bone->flags & k_bone_flag_collider_any) ) + if( !(bone->collider) ) continue; if( rd->part_count > vg_list_size(rd->parts) ) @@ -152,9 +150,6 @@ VG_STATIC void player_setup_ragdoll_from_avatar( struct player_ragdoll *rd, player_init_ragdoll_bone_collider( bone, rp ); - struct mdl_node *pnode = mdl_node_from_id( &av->meta, bone->orig_node ); - struct classtype_bone *inf = mdl_get_entdata( &av->meta, pnode ); - /* * Bones with collider and parent */ @@ -163,27 +158,30 @@ VG_STATIC void player_setup_ragdoll_from_avatar( struct player_ragdoll *rd, rp->parent = ragdoll_bone_parent( rd, av, bone->parent ); - /* Always assign a point-to-point constraint */ - struct rb_constr_pos *c = - &rd->position_constraints[ rd->position_constraints_count ++ ]; + + if( bone->orig_bone->flags & k_bone_flag_cone_constraint ){ + struct rb_constr_pos *c = + &rd->position_constraints[ rd->position_constraints_count ++ ]; - struct skeleton_bone *bj = &av->sk.bones[rp->bone_id]; - struct ragdoll_part *pp = &rd->parts[rp->parent]; - struct skeleton_bone *bp = &av->sk.bones[pp->bone_id]; + struct skeleton_bone *bj = &av->sk.bones[rp->bone_id]; + struct ragdoll_part *pp = &rd->parts[rp->parent]; + struct skeleton_bone *bp = &av->sk.bones[pp->bone_id]; - /* Convention: rba -- parent, rbb -- child */ - c->rba = &pp->rb; - c->rbb = &rp->rb; + /* Convention: rba -- parent, rbb -- child */ + c->rba = &pp->rb; + c->rbb = &rp->rb; + + v3f delta; + v3_sub( bj->co, bp->co, delta ); + m4x3_mulv( rp->inv_collider_mtx, (v3f){0.0f,0.0f,0.0f}, c->lcb ); + m4x3_mulv( pp->inv_collider_mtx, delta, c->lca ); + + + mdl_bone *inf = bone->orig_bone; - v3f delta; - v3_sub( bj->co, bp->co, delta ); - m4x3_mulv( rp->inv_collider_mtx, (v3f){0.0f,0.0f,0.0f}, c->lcb ); - m4x3_mulv( pp->inv_collider_mtx, delta, c->lca ); - - if( inf->flags & k_bone_flag_cone_constraint ) - { struct rb_constr_swingtwist *a = &rd->cone_constraints[ rd->cone_constraints_count ++ ]; + a->rba = &pp->rb; a->rbb = &rp->rb; a->conet = cosf( inf->conet )-0.0001f; @@ -216,8 +214,7 @@ VG_STATIC void player_setup_ragdoll_from_avatar( struct player_ragdoll *rd, VG_STATIC void copy_ragdoll_pose_to_avatar( struct player_ragdoll *rd, struct player_avatar *av ) { - for( int i=0; ipart_count; i++ ) - { + for( int i=0; ipart_count; i++ ){ struct ragdoll_part *part = &rd->parts[i]; m4x3f offset; m3x3_identity(offset); @@ -225,6 +222,22 @@ VG_STATIC void copy_ragdoll_pose_to_avatar( struct player_ragdoll *rd, av->sk.final_mtx[part->bone_id] ); } + for( u32 i=1; isk.bone_count; i++ ){ + struct skeleton_bone *sb = &av->sk.bones[i]; + + if( sb->parent && !sb->collider ){ + v3f delta; + v3_sub( av->sk.bones[i].co, av->sk.bones[sb->parent].co, delta ); + + m4x3f posemtx; + m3x3_identity( posemtx ); + v3_copy( delta, posemtx[3] ); + + /* final matrix */ + m4x3_mul( av->sk.final_mtx[sb->parent], posemtx, av->sk.final_mtx[i] ); + } + } + skeleton_apply_inverses( &av->sk ); } @@ -235,8 +248,7 @@ VG_STATIC void copy_avatar_pose_to_ragdoll( struct player_avatar *av, struct player_ragdoll *rd, v3f velocity ) { - for( int i=0; ipart_count; i++ ) - { + for( int i=0; ipart_count; i++ ){ struct ragdoll_part *part = &rd->parts[i]; v3f pos, offset; @@ -274,25 +286,34 @@ VG_STATIC void player_ragdoll_iter( struct player_ragdoll *rd ) int run_sim = 0; ragdoll_frame ++; - if( ragdoll_frame >= k_ragdoll_div ) - { + if( ragdoll_frame >= k_ragdoll_div ){ ragdoll_frame = 0; run_sim = 1; } rb_solver_reset(); - for( int i=0; ipart_count; i ++ ) - { - if( rb_global_has_space() ) - { + + float contact_velocities[256]; + + for( int i=0; ipart_count; i ++ ){ + if( rb_global_has_space() ){ rb_ct *buf = rb_global_buffer(); - int l = rb_capsule__scene( rd->parts[i].rb.to_world, - &rd->parts[i].rb.inf.capsule, - NULL, &world->rb_geo.inf.scene, buf ); + int l; + + if( rd->parts[i].rb.type == k_rb_shape_capsule ){ + l = rb_capsule__scene( rd->parts[i].rb.to_world, + &rd->parts[i].rb.inf.capsule, + NULL, &world->rb_geo.inf.scene, buf ); + } + else if( rd->parts[i].rb.type == k_rb_shape_box ){ + l = rb_box__scene( rd->parts[i].rb.to_world, + rd->parts[i].rb.bbx, + NULL, &world->rb_geo.inf.scene, buf ); + } + else continue; - for( int j=0; jparts[i].rb; buf[j].rbb = &world->rb_geo; } @@ -302,42 +323,42 @@ VG_STATIC void player_ragdoll_iter( struct player_ragdoll *rd ) } /* - * COLLISION DETECTION + * self-collision */ - for( int i=0; ipart_count-1; i ++ ) - { - for( int j=i+1; jpart_count; j ++ ) - { - if( rd->parts[j].parent != i ) - { - if( rb_global_has_space() ) - { - rb_ct *buf = rb_global_buffer(); - - int l = rb_capsule__capsule( rd->parts[i].rb.to_world, - &rd->parts[i].rb.inf.capsule, - rd->parts[j].rb.to_world, - &rd->parts[j].rb.inf.capsule, - buf ); - - for( int k=0; kparts[i].rb; - buf[k].rbb = &rd->parts[j].rb; - } - - rb_contact_count += l; + for( int i=0; ipart_count-1; i ++ ){ + for( int j=i+1; jpart_count; j ++ ){ + if( rd->parts[j].parent != i ){ + if( !rb_global_has_space() ) + break; + + if( rd->parts[j].rb.type != k_rb_shape_capsule ) + continue; + + if( rd->parts[i].rb.type != k_rb_shape_capsule ) + continue; + + rb_ct *buf = rb_global_buffer(); + + int l = rb_capsule__capsule( rd->parts[i].rb.to_world, + &rd->parts[i].rb.inf.capsule, + rd->parts[j].rb.to_world, + &rd->parts[j].rb.inf.capsule, + buf ); + + for( int k=0; kparts[i].rb; + buf[k].rbb = &rd->parts[j].rb; } + + rb_contact_count += l; } } } - for( int j=0; jpart_count; j++ ) - { + for( int j=0; jpart_count; j++ ){ struct ragdoll_part *pj = &rd->parts[j]; - if( run_sim ) - { + if( run_sim ){ v4f plane = {0.0f,1.0f,0.0f,0.0f}; rb_effect_simple_bouyency( &pj->rb, plane, k_ragdoll_floatyiness, k_ragdoll_floatydrag ); @@ -347,6 +368,18 @@ VG_STATIC void player_ragdoll_iter( struct player_ragdoll *rd ) /* * PRESOLVE */ + for( u32 i=0; ico, ct->rba->co, ra ); + v3_sub( ct->co, ct->rbb->co, rb ); + rb_rcv( ct->rba, ct->rbb, ra, rb, rv ); + float vn = v3_dot( rv, ct->n ); + + contact_velocities[i] = vn; + } + rb_presolve_contacts( rb_contact_buffer, rb_contact_count ); rb_presolve_swingtwist_constraints( rd->cone_constraints, rd->cone_constraints_count ); @@ -354,14 +387,12 @@ VG_STATIC void player_ragdoll_iter( struct player_ragdoll *rd ) /* * DEBUG */ - if( k_ragdoll_debug_collider ) - { + if( k_ragdoll_debug_collider ){ for( u32 i=0; ipart_count; i ++ ) rb_debug( &rd->parts[i].rb, rd->parts[i].colour ); } - if( k_ragdoll_debug_constraints ) - { + if( k_ragdoll_debug_constraints ){ rb_debug_position_constraints( rd->position_constraints, rd->position_constraints_count ); @@ -372,10 +403,8 @@ VG_STATIC void player_ragdoll_iter( struct player_ragdoll *rd ) /* * SOLVE CONSTRAINTS */ - if( run_sim ) - { - for( int i=0; i<25; i++ ) - { + if( run_sim ){ + for( int i=0; i<16; i++ ){ rb_solve_contacts( rb_contact_buffer, rb_contact_count ); rb_solve_swingtwist_constraints( rd->cone_constraints, rd->cone_constraints_count ); @@ -396,6 +425,38 @@ VG_STATIC void player_ragdoll_iter( struct player_ragdoll *rd ) rd->position_constraints_count, 0.5f ); } + rb_ct *stress = NULL; + float max_stress = 1.0f; + + for( u32 i=0; ico, ct->rba->co, ra ); + v3_sub( ct->co, ct->rbb->co, rb ); + rb_rcv( ct->rba, ct->rbb, ra, rb, rv ); + float vn = v3_dot( rv, ct->n ); + + float s = fabsf(vn - contact_velocities[i]); + if( s > max_stress ){ + stress = ct; + max_stress = s; + } + } + + static u32 temp_filter = 0; + + if( temp_filter ){ + temp_filter --; + return; + } + + if( stress ){ + temp_filter = 20; + audio_lock(); + audio_oneshot_3d( &audio_hits[rand()%5], stress->co, 20.0f, 1.0f ); + audio_unlock(); + } } #endif /* PLAYER_RAGDOLL_H */