From b1557496a922904b7abcd47d5a59a6da44d70fe8 Mon Sep 17 00:00:00 2001 From: hgn Date: Sat, 11 Feb 2023 19:42:00 +0000 Subject: [PATCH] shits fucked --- common.h | 6 +- maps_src/mp_gridmap.mdl | Bin 1424944 -> 1434112 bytes player_skate.c | 612 ++++++++++++++++++++++++++-------------- player_skate.h | 3 + rigidbody.h | 18 ++ 5 files changed, 424 insertions(+), 215 deletions(-) diff --git a/common.h b/common.h index 2c48560..f9bcafa 100644 --- a/common.h +++ b/common.h @@ -133,16 +133,16 @@ VG_STATIC float k_spring_dampener = 5.0f, k_grind_spring = 100.0f, - k_grind_dampener = 1.0f, + k_grind_dampener = 6.0f, k_board_spring = 100.0f, - k_board_dampener = 20.0f, + k_board_dampener = 40.0f, k_manul_spring = 200.0f, k_manul_dampener = 30.0f, k_board_interia = 8.0f, k_board_length = 0.45f, k_board_width = 0.13f, - k_board_end_radius = 0.2f, + k_board_end_radius = 0.1f, k_board_radius = 0.07f; diff --git a/maps_src/mp_gridmap.mdl b/maps_src/mp_gridmap.mdl index 43b26adbd59e1191d08075e9b632a9018d9b3fd8..d7ae80c1c7518d383b5dab42b86cfcba300255ed 100644 GIT binary patch delta 8360 zcma)B3s@9o_uqltogHSm%S8bJ5kV0F5s?(e1;JZh@SmXp0x2q9QcS`UUA&ODQ27u} zqEu#v8HtKErZB7~dEshB_GP7}b~Vcs^|RamoMBg2V0+rzR&dw|c{&+h7 zRMH%UqUl{mfuC<&=&vFaX)%%)$+ilG4RI%hLWO*55B!6lR^+3M++d|p_;jc$F1sld zG2<074;jNK^26;EhC(aF$ful(S2_9>ZG4VD;j%T>&U0-)41Y`D*v#(=WO*4!AM+0wP8 zxk9#Be_9!0&!ma%pD8bvF`LBm*{XLL?yn8OHJ5Akf_{Hko!(Zx) z__VJ0k7sn?x7%uB1t_1T4&lP543A!fYBkV?OJ=R8%2Y@4ob{91Xux13h+mA`;~{6U zI*GXnO|rB_7sPFxNaGqcqdHcSNA=Bd+|QLr#iJ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JLY(#)JYk`8u};6A!w!RGIj&0~v@Dmjkx3=z-rD`8oz3#s z8}=>pFb_4oI*s$Ud~GuB%CEERyJYH^>3Hkf6+0{A`V~?krT3CO<=(N&*kfqONBp2u zEmy>?WaW^s8k;&!M#x=mIV=+jR^xcOpmH^qZUW01MDdWdMzRt;G_JuGzve;#&KxoG zq>WMqA;ZCQGp>j6Y)o~@yj5%*ST^Gp+rvvYOFw&1+r=cNwq7zW07nVV=+&Lg#sp|6 zkrIXRA&-owK(hz6ZVrxSqfdIVO}t=_1c5JKqTqi4H>Uy0FGzhv#SdJR`doy71#in* zDE$_nk{!_atu&8RfxZzXj<*NP1N>LAyWnWV&aZ=}35RuWXQ(`C)=D-65}G8dkl!>; z4ufM&nEeg3Hc2Ve(&j7APt0Ksuv|u!a=ninP?*EE@dKA7tth;={~fs;np>nQI*Kvmq;@#IJUw0D1g&(9rgj@sl9qCuH0K7Y}$Y3yZV2c7AaH1G! z?!c9f;bEQ9vysBlA*pf&gi&RX@Sju3atx$VrH=dv#TcQ1p;mdaM~!hgG*G2hnBfyI z<2%5~<#VRVN#L)-%PIOP@?##SQYH>2A3=G5GKDbk9Eol+&lst!rQ|UL2P<{@zJPRN zd{ZV{GJBEn>C-vpv=Jq*Jc4b<51t8IdZErVq#5vBbA&UTcv0Je1E5r@t#O!bTVn=hV4r0 zVUN%5mCNgUxM^3=j>$07K*zuZ6Me(|lfJLyxbPj5k9L@0k%?-^XP`0DAYt@rSM~%F z@DgYBQ!~{GPaUns`%r17TKtr@n(6sSvIGv~P-`T$7*FCa1KWOg$Vu(8GjG#=SPD+ms+X+&ejvobljx5R7?M|wY?wq@`hYmMuZnH?Cf8V zWv40RAhg)2j@$x(vkPkPs{Zo~fpL14No diff --git a/player_skate.c b/player_skate.c index a04a0dd..7088cf1 100644 --- a/player_skate.c +++ b/player_skate.c @@ -149,13 +149,11 @@ VG_STATIC int skate_grind_collide( player_instance *player, rb_ct *contact ) return 0; } -VG_STATIC int skate_grind_scansq( player_instance *player, v3f ra ) +VG_STATIC int skate_grind_scansq( player_instance *player, v3f pos, + v3f result_co, v3f result_dir, v3f result_n ) { - v3f pos; - m4x3_mulv( player->rb.to_world, ra, pos ); - v4f plane; - v3_copy( player->rb.to_world[2], plane ); + v3_copy( player->rb.v, plane ); v3_normalize( plane ); plane[3] = v3_dot( plane, pos ); @@ -253,10 +251,12 @@ VG_STATIC int skate_grind_scansq( player_instance *player, v3f ra ) return 0; v3f average_position, - average_direction; + average_direction, + average_normal; v3_zero( average_position ); v3_zero( average_direction ); + v3_zero( average_normal ); int passed_samples = 0; @@ -302,8 +302,13 @@ VG_STATIC int skate_grind_scansq( player_instance *player, v3f ra ) /* make sure the directions all face a common hemisphere */ v3_muls( dir, vg_signf(v3_dot(dir,plane)), dir ); - v3_add( average_direction, dir, average_direction ); + + if( si->normal3[1] > sj->normal3[1] ) + v3_add( si->normal3, average_normal, average_normal ); + else + v3_add( sj->normal3, average_normal, average_normal ); + passed_samples ++; } } @@ -311,9 +316,14 @@ VG_STATIC int skate_grind_scansq( player_instance *player, v3f ra ) if( !passed_samples ) return 0; + if( (v3_length2( average_direction ) <= 0.001f) || + (v3_length2( average_normal ) <= 0.001f ) ) + return 0; + float div = 1.0f/(float)passed_samples; v3_muls( average_position, div, average_position ); - v3_muls( average_direction, div, average_direction ); /* !! not normed */ + v3_normalize( average_direction ); + v3_normalize( average_normal ); v3_add( pos, average_position, average_position ); vg_line_pt3( average_position, 0.02f, VG__GREEN ); @@ -323,38 +333,9 @@ VG_STATIC int skate_grind_scansq( player_instance *player, v3f ra ) v3_muladds( average_position, average_direction, -0.35f, p1 ); vg_line( p0, p1, VG__PINK ); -#if 0 - if( passed_samples ) - { - v3f displacement, dir; - v3_sub( pos, average_position, displacement ); - v3_copy( displacement, dir ); - v3_normalize( dir ); - - v3f rv, raW; - q_mulv( player->rb.q, ra, raW ); - - v3_cross( player->rb.w, raW, rv ); - v3_add( player->rb.v, rv, rv ); - - v3_muladds( rv, player->rb.to_world[2], - -v3_dot( rv, player->rb.to_world[2] ), rv ); - - v3f Fd, Fs, F; - v3_muls( displacement, -k_grind_spring, Fs ); - v3_muls( rv, -k_grind_dampener, Fd ); - - v3_add( Fd, Fs, F ); - v3_muls( F, k_rb_delta, F ); - - v3_add( player->rb.v, F, player->rb.v ); - v3f wa; - v3_cross( raW, F, wa ); - v3_add( player->rb.w, wa, player->rb.w ); - - /* Constraint based */ - } -#endif + v3_copy( average_position, result_co ); + v3_copy( average_normal, result_n ); + v3_copy( average_direction, result_dir ); return passed_samples; } @@ -1230,8 +1211,11 @@ VG_STATIC void skate_apply_cog_model( player_instance *player ) { struct player_skate *s = &player->_skate; - v3f ideal_cog, ideal_diff; - v3_muladds( player->rb.co, player->rb.to_world[1], + v3f ideal_cog, ideal_diff, ideal_dir; + v3_copy( s->state.up_dir, ideal_dir ); + v3_normalize( ideal_dir ); + + v3_muladds( player->rb.co, ideal_dir, 1.0f-player->input_grab->axis.value, ideal_cog ); v3_sub( ideal_cog, s->state.cog, ideal_diff ); @@ -1506,21 +1490,21 @@ VG_STATIC void player__skate_update( player_instance *player ) .colour = VG__GREEN }, { - { 0.0f, k_board_end_radius, -k_board_length - k_board_end_radius }, + { 0.0f, 0.2f, -k_board_length - k_board_end_radius }, .radius = k_board_end_radius, .apply_angular = 0, .colour = VG__YELOW }, { - { 0.0f, k_board_end_radius, k_board_length + k_board_end_radius }, + { 0.0f, 0.2f, k_board_length + k_board_end_radius }, .radius = k_board_end_radius, .apply_angular = 0, .colour = VG__YELOW }, }; - const int k_wheel_count = 4; - + const int k_wheel_count = 2; +#if 0 if( skate_grind_scansq( player, (v3f){ 0.0f, 0.0f, -k_board_length } ) ) { #if 0 @@ -1536,139 +1520,15 @@ VG_STATIC void player__skate_update( player_instance *player ) wheel_states[3] = 0; #endif } +#endif s->substep = k_rb_delta; + s->substep_delta = s->substep; int substep_count = 0; - v3f original_velocity; - v3_muladds( player->rb.v, (v3f){0.0f,-k_gravity,0.0f}, - k_rb_delta, original_velocity ); - -begin_collision:; - - /* - * Phase 0: Continous collision detection - * -------------------------------------------------------------------------- - */ - - for( int i=0; irb.co, player->rb.v, s->substep, future_co ); - - if( v3_length2( player->rb.w ) > 0.0f ) - { - v4f rotation; - v3f axis; - v3_copy( player->rb.w, axis ); - - float mag = v3_length( axis ); - v3_divs( axis, mag, axis ); - q_axis_angle( rotation, axis, mag*s->substep ); - q_mul( rotation, player->rb.q, future_q ); - q_normalize( future_q ); - } - - /* calculate the minimum time we can move */ - float max_time = s->substep; - - for( int i=0; irb.q, wheels[i].pos, current ); - v3_add( current, player->rb.co, current ); - - float t; - v3f n; - - float cast_radius = wheels[i].radius - k_penetration_slop * 1.2f; - if( spherecast_world( current, future, cast_radius, &t, n ) != -1) - max_time = vg_minf( max_time, t * s->substep ); - } - - /* clamp to a fraction of delta, to prevent locking */ - float rate_lock = substep_count; - rate_lock *= k_rb_delta * 0.1f; - rate_lock *= rate_lock; - - max_time = vg_maxf( max_time, rate_lock ); - s->substep_delta = max_time; - - /* integrate */ - v3_muladds( player->rb.co, player->rb.v, s->substep_delta, player->rb.co ); - if( v3_length2( player->rb.w ) > 0.0f ) - { - v4f rotation; - v3f axis; - v3_copy( player->rb.w, axis ); - - float mag = v3_length( axis ); - v3_divs( axis, mag, axis ); - q_axis_angle( rotation, axis, mag*s->substep_delta ); - q_mul( rotation, player->rb.q, player->rb.q ); - } - - rb_update_transform( &player->rb ); - v3f gravity = { 0.0f, -9.6f, 0.0f }; - v3_muladds( player->rb.v, gravity, s->substep_delta, player->rb.v ); - s->substep -= s->substep_delta; - /* - * Phase 1: Regular collision detection - * TODO: Me might want to automatically add contacts from CCD, - * since at high angular velocities, theres a small change - * that discreet detection will miss. - * -------------------------------------------------------------------------- - */ - - rb_ct manifold[128]; - - int manifold_len = 0; - - for( int i=0; irb.to_world, wheels[i].pos, mtx[3] ); - - rb_sphere collider = { .radius = wheels[i].radius }; - - rb_ct *man = &manifold[ manifold_len ]; - - int l = skate_collide_smooth( player, mtx, &collider, man ); - if( l ) - wheels[i].state = k_collider_state_colliding; - - /* for non-angular contacts we just want Y. contact positions are - * snapped to the local xz plane */ - if( !wheels[i].apply_angular ) - { - for( int j=0; jrb.co, ra ); - - float dy = v3_dot( player->rb.to_world[1], ra ); - v3_muladds( man[j].co, player->rb.to_world[1], -dy, man[j].co ); - } - } - - manifold_len += l; - } /* * Phase 2: Truck alignment (spring/dampener model) @@ -1681,6 +1541,9 @@ begin_collision:; for( int i=0; i<2; i++ ) { + if( wheels[i].state == k_collider_state_disabled ) + continue; + v3f truck, left, right; m4x3_mulv( player->rb.to_world, wheels[i].pos, truck ); v3_muladds( truck, player->rb.to_world[0], -k_board_width, left ); @@ -1717,7 +1580,7 @@ begin_collision:; if( v3_dot( ray_r.normal, player->rb.to_world[1] ) < 0.7071f ) res_r = 0; else - v3_add( ray_l.normal, surface_picture, surface_picture ); + v3_add( ray_r.normal, surface_picture, surface_picture ); } v3f v0; @@ -1750,37 +1613,32 @@ begin_collision:; for( int j=0; j<3; j++ ) v3_copy( world.scene_geo->arrvertices[ tri[j] ].co, verts[j] ); - v3f v0, v1, n; - v3_sub( verts[1], verts[0], v0 ); - v3_sub( verts[2], verts[0], v1 ); - v3_cross( v0, v1, n ); + v3f vert0, vert1, n; + v3_sub( verts[1], verts[0], vert0 ); + v3_sub( verts[2], verts[0], vert1 ); + v3_cross( vert0, vert1, n ); v3_normalize( n ); - if( v3_dot( n, player->rb.to_world[1] ) < 0.7071f ) + if( v3_dot( n, player->rb.to_world[1] ) < 0.3f ) continue; - continue; + v3_cross( n, player->rb.to_world[2], v0 ); + v3_muladds( v0, player->rb.to_world[2], + -v3_dot( player->rb.to_world[2], v0 ), v0 ); + v3_normalize( v0 ); } else continue; } - float a = vg_clampf( v3_dot( v0, player->rb.to_world[0] ), -1.0f, 1.0f ); - a = acosf( a ); - v3_muladds( truck, v0, k_board_width, right ); v3_muladds( truck, v0, -k_board_width, left ); vg_line( left, right, VG__WHITE ); - v3f axis; - v3_cross( v0, player->rb.to_world[0], axis ); - - float Fs = -a * k_board_spring, - Fd = -v3_dot( player->rb.w, axis ) * k_board_dampener; - - v3_muladds( player->rb.w, axis, (Fs+Fd) * s->substep_delta, - player->rb.w ); + rb_effect_spring_target_vector( &player->rb, player->rb.to_world[0], v0, + k_board_spring, k_board_dampener, + s->substep_delta ); } /* @@ -1796,7 +1654,8 @@ begin_collision:; if( s->state.manual_direction == 0 ) { if( (player->input_js1v->axis.value > 0.7f) && - s->state.activity == k_skate_activity_ground ) + (s->state.activity == k_skate_activity_ground) && + (s->state.jump_charge <= 0.01f) ) s->state.manual_direction = reverse_dir; } else @@ -1821,17 +1680,33 @@ begin_collision:; weight[2] = k_board_length * amt * (float)s->state.manual_direction; } - m4x3_mulv( player->rb.to_world, weight, world_cog ); - vg_line_pt3( world_cog, 0.1f, VG__BLACK ); + if( v3_length2( surface_picture ) > 0.001f ) + { + v3_normalize( surface_picture ); + + v3f target; + v3_copy( surface_picture, target ); + + target[1] += 2.0f * surface_picture[1]; + v3_normalize( target ); + + v3_lerp( s->state.up_dir, target, + 8.0f * s->substep_delta, s->state.up_dir ); + } + else + { + v3_lerp( s->state.up_dir, player->rb.to_world[1], + 8.0f * s->substep_delta, s->state.up_dir ); + } + /* TODO: Fall back on land normal */ /* TODO: Lerp weight distribution */ - + /* TODO: Can start manual only if not charge jump */ if( v3_length2( surface_picture ) > 0.001f && v3_length2( weight ) > 0.001f && s->state.manual_direction ) { - v3_normalize( surface_picture ); v3f plane_z; m3x3_mulv( player->rb.to_world, weight, plane_z ); @@ -1852,17 +1727,262 @@ begin_collision:; v3_muls( player->rb.to_world[2], -(float)s->state.manual_direction, refdir ); - float a = v3_dot( plane_z, refdir ); - a = acosf( vg_clampf( a, -1.0f, 1.0f ) ); + rb_effect_spring_target_vector( &player->rb, refdir, plane_z, + k_manul_spring, k_manul_dampener, + s->substep_delta ); + } + + + + + + + + + +begin_collision:; + + /* + * Phase 0: Continous collision detection + * -------------------------------------------------------------------------- + */ + + v3f head_wp0, head_wp1, start_co; + m4x3_mulv( player->rb.to_world, s->state.head_position, head_wp0 ); + v3_copy( player->rb.co, start_co ); + + for( int i=0; irb.co, player->rb.v, s->substep, future_co ); + + if( v3_length2( player->rb.w ) > 0.0f ) + { + v4f rotation; v3f axis; - v3_cross( plane_z, refdir, axis ); + v3_copy( player->rb.w, axis ); + + float mag = v3_length( axis ); + v3_divs( axis, mag, axis ); + q_axis_angle( rotation, axis, mag*s->substep ); + q_mul( rotation, player->rb.q, future_q ); + q_normalize( future_q ); + } - float Fs = -a * k_manul_spring, - Fd = -v3_dot( player->rb.w, axis ) * k_manul_dampener; + /* calculate the minimum time we can move */ + float max_time = s->substep; + + for( int i=0; irb.w, axis, (Fs+Fd) * s->substep_delta, - player->rb.w ); + v3f current, future; + q_mulv( future_q, wheels[i].pos, future ); + v3_add( future, future_co, future ); + + q_mulv( player->rb.q, wheels[i].pos, current ); + v3_add( current, player->rb.co, current ); + + float t; + v3f n; + + float cast_radius = wheels[i].radius - k_penetration_slop * 1.2f; + if( spherecast_world( current, future, cast_radius, &t, n ) != -1) + max_time = vg_minf( max_time, t * s->substep ); + } + + /* clamp to a fraction of delta, to prevent locking */ + float rate_lock = substep_count; + rate_lock *= k_rb_delta * 0.1f; + rate_lock *= rate_lock; + + max_time = vg_maxf( max_time, rate_lock ); + s->substep_delta = max_time; + + /* integrate */ + v3_muladds( player->rb.co, player->rb.v, s->substep_delta, player->rb.co ); + if( v3_length2( player->rb.w ) > 0.0f ) + { + v4f rotation; + v3f axis; + v3_copy( player->rb.w, axis ); + + float mag = v3_length( axis ); + v3_divs( axis, mag, axis ); + q_axis_angle( rotation, axis, mag*s->substep_delta ); + q_mul( rotation, player->rb.q, player->rb.q ); + } + + rb_update_transform( &player->rb ); + + v3f gravity = { 0.0f, -9.6f, 0.0f }; + v3_muladds( player->rb.v, gravity, s->substep_delta, player->rb.v ); + + s->substep -= s->substep_delta; + + + rb_ct manifold[128]; + int manifold_len = 0; + + /* + * Phase -1: head detection + * -------------------------------------------------------------------------- + */ + m4x3_mulv( player->rb.to_world, s->state.head_position, head_wp1 ); + vg_line( head_wp0, head_wp1, VG__RED ); + + float t; + v3f n; + if( (v3_dist2( head_wp0, head_wp1 ) > 0.001f) && + (spherecast_world( head_wp0, head_wp1, 0.2f, &t, n ) != -1) ) + { + v3_lerp( start_co, player->rb.co, t, player->rb.co ); + rb_update_transform( &player->rb ); + +#if 0 + player__dead_transition( player ); +#endif + return; + } + + /* + * Phase 2-1+0.5: Grind collision + * -------------------------------------------------------------------------- + */ + + for( int i=0; i<1; i++ ) + { + + /* + * Grind collision detection + * ------------------------------------------------ + */ + v3f grind_co, grind_n, grind_dir; + if( skate_grind_scansq( player, player->rb.co, + grind_co, grind_dir, grind_n ) ) + { +#if 0 + rb_ct *ct = &manifold[ manifold_len ++ ]; + + v3_copy( truck, ct->co ); + v3_copy( grind_n, ct->n ); + ct->p = vg_maxf( 0.0f, ct->co[1] - truck[1] ); +#endif + + v3f target_dir; + v3_cross( grind_dir, (v3f){0.0f,1.0f,0.0f}, target_dir ); + target_dir[1] = 0.0f; + + if( v3_length2( target_dir ) <= 0.001f ) + continue; + + if( fabsf(v3_dot( player->rb.v, grind_dir )) < 0.7071f ) + continue; + + v3_copy( grind_co, player->rb.co ); + + q_axis_angle( player->rb.q, (v3f){0.0f,1.0f,0.0f}, + -atan2f( target_dir[2], target_dir[0] ) ); + + wheels[0].state = k_collider_state_disabled; + wheels[1].state = k_collider_state_disabled; + v3_muls( grind_dir, v3_dot(player->rb.v,grind_dir), player->rb.v ); + v3_zero( player->rb.w ); + + rb_update_transform( &player->rb ); + + +#if 0 + v3f displacement, dir; + v3_sub( truck, grind_co, displacement ); + v3_copy( displacement, dir ); + v3_normalize( dir ); + + v3f rv, raW; + q_mulv( player->rb.q, wheels[i].pos, raW ); + + v3_cross( player->rb.w, raW, rv ); + v3_add( player->rb.v, rv, rv ); + + v3_muladds( rv, player->rb.to_world[2], + -v3_dot( rv, player->rb.to_world[2] ), rv ); + + v3f Fd, Fs, F; + v3_muls( displacement, -k_grind_spring, Fs ); + v3_muls( rv, -k_grind_dampener, Fd ); + + v3_add( Fd, Fs, F ); + v3_muls( F, s->substep_delta, F ); + + v3_add( player->rb.v, F, player->rb.v ); + v3f wa; + v3_cross( raW, F, wa ); + v3_add( player->rb.w, wa, player->rb.w ); + + rb_effect_spring_target_vector( &player->rb, player->rb.to_world[1], + grind_n, + k_board_spring, k_board_dampener, + s->substep_delta ); + + v3f adj; + v3_cross( grind_dir, (v3f){0.0f,1.0f,0.0f}, adj ); + rb_effect_spring_target_vector( &player->rb, player->rb.to_world[2], + adj, + k_grind_spring, k_grind_dampener, + s->substep_delta ); +#endif + + s->state.activity = k_skate_activity_grind; + } + else + s->state.activity = k_skate_activity_ground; + } + + + /* + * Phase 1: Regular collision detection + * TODO: Me might want to automatically add contacts from CCD, + * since at high angular velocities, theres a small change + * that discreet detection will miss. + * -------------------------------------------------------------------------- + */ + + for( int i=0; irb.to_world, wheels[i].pos, mtx[3] ); + + rb_sphere collider = { .radius = wheels[i].radius }; + + rb_ct *man = &manifold[ manifold_len ]; + + int l = skate_collide_smooth( player, mtx, &collider, man ); + if( l ) + wheels[i].state = k_collider_state_colliding; + + /* for non-angular contacts we just want Y. contact positions are + * snapped to the local xz plane */ + if( !wheels[i].apply_angular ) + { + for( int j=0; jrb.co, ra ); + + float dy = v3_dot( player->rb.to_world[1], ra ); + v3_muladds( man[j].co, player->rb.to_world[1], -dy, man[j].co ); + } + } + + manifold_len += l; } /* @@ -1871,7 +1991,10 @@ begin_collision:; */ for( int i=0; isubstep_delta ); + rb_debug_contact( &manifold[i] ); + } /* yes, we are currently rebuilding mass matrices every frame. too bad! */ v3f extent = { k_board_width, 0.1f, k_board_length }; @@ -1898,6 +2021,9 @@ begin_collision:; m3x3_mul( iI, player->rb.to_local, iIw ); m3x3_mul( player->rb.to_world, iIw, iIw ); + m4x3_mulv( player->rb.to_world, weight, world_cog ); + vg_line_pt3( world_cog, 0.1f, VG__BLACK ); + for( int j=0; j<10; j++ ) { for( int i=0; in ), - lambda = normal_mass * ( -vn + ct->bias ); + lambda = normal_mass * ( -vn ); + + /* FIXME! */ + v3_muladds( player->rb.co, ct->n, ct->bias*0.02f, player->rb.co ); float temp = ct->norm_impulse; ct->norm_impulse = vg_maxf( temp + lambda, 0.0f ); @@ -1961,13 +2090,6 @@ begin_collision:; wheels[i].colour }[ wheels[i].state ]); } - v3f velocity_change, p1; - v3_sub( player->rb.v, original_velocity, velocity_change ); - - v3_normalize( velocity_change ); - v3_muladds( player->rb.co, velocity_change, 2.0f, p1 ); - vg_line( player->rb.co, p1, VG__PINK ); - #if 0 skate_apply_grind_model( player, &manifold[manifold_len], grind_len ); #endif @@ -2001,6 +2123,8 @@ begin_collision:; m4x3_mulv( gate->transport, s->state.cog, s->state.cog ); m3x3_mulv( gate->transport, s->state.cog_v, s->state.cog_v ); m3x3_mulv( gate->transport, s->state.throw_v, s->state.throw_v ); + m3x3_mulv( gate->transport, s->state.head_position, + s->state.head_position ); v4f transport_rotation; m3x3_q( gate->transport, transport_rotation ); @@ -2063,8 +2187,18 @@ VG_STATIC void player__skate_animate( player_instance *player, v3f offset; v3_zero( offset ); - m4x3_mulv( player->rb.to_local, s->state.cog, offset ); - v3_muls( offset, -4.0f, offset ); + v3f cog_local, cog_ideal; + m4x3_mulv( player->rb.to_local, s->state.cog, cog_local ); + + v3_copy( s->state.up_dir, cog_ideal ); + v3_normalize( cog_ideal ); + m3x3_mulv( player->rb.to_local, cog_ideal, cog_ideal ); + + v3_sub( cog_ideal, cog_local, offset ); + + + v3_muls( offset, 4.0f, offset ); + offset[1] *= -1.0f; float curspeed = v3_length( player->rb.v ), kickspeed = vg_clampf( curspeed*(1.0f/40.0f), 0.0f, 1.0f ), @@ -2213,6 +2347,52 @@ VG_STATIC void player__skate_animate( player_instance *player, dest->pose[apply_to[i]-1].co[2] += offset[2]*add_grab_mod; } + + + + /* angle correction */ + if( v3_length2( s->state.up_dir ) > 0.001f ) + { + v3f ndir; + m3x3_mulv( player->rb.to_local, s->state.up_dir, ndir ); + v3_normalize( ndir ); + + v3f up = { 0.0f, 1.0f, 0.0f }; + + float a = v3_dot( ndir, up ); + a = acosf( vg_clampf( a, -1.0f, 1.0f ) ); + + v3f axis; + v4f q; + + v3_cross( up, ndir, axis ); + q_axis_angle( q, axis, a ); + + mdl_keyframe *kf_hip = &dest->pose[av->id_hip-1]; + + for( int i=0; ipose[apply_to[i]-1]; + + v3f v0; + v3_sub( kf->co, kf_hip->co, v0 ); + q_mulv( q, v0, v0 ); + v3_add( v0, kf_hip->co, kf->co ); + + q_mul( q, kf->q, kf->q ); + q_normalize( kf->q ); + } + + v3f p1, p2; + m4x3_mulv( player->rb.to_world, up, p1 ); + m4x3_mulv( player->rb.to_world, ndir, p2 ); + + vg_line( player->rb.co, p1, VG__PINK ); + vg_line( player->rb.co, p2, VG__CYAN ); + } + + + mdl_keyframe *kf_board = &dest->pose[av->id_board-1], *kf_foot_l = &dest->pose[av->id_ik_foot_l-1], *kf_foot_r = &dest->pose[av->id_ik_foot_r-1]; @@ -2325,6 +2505,11 @@ VG_STATIC void player__skate_post_animate( player_instance *player ) struct player_avatar *av = player->playeravatar; player->cam_velocity_influence = 1.0f; + + v3f head = { 0.0f, 1.8f, 0.0f }; /* FIXME: Viewpoint entity */ + m4x3_mulv( av->sk.final_mtx[ av->id_head ], head, s->state.head_position ); + m4x3_mulv( player->rb.to_local, s->state.head_position, + s->state.head_position ); } VG_STATIC void player__skate_reset_animator( player_instance *player ) @@ -2382,6 +2567,9 @@ VG_STATIC void player__skate_reset( player_instance *player, player__skate_clear_mechanics( player ); player__skate_reset_animator( player ); + + v3_zero( s->state.head_position ); + s->state.head_position[1] = 1.8f; } #endif /* PLAYER_SKATE_C */ diff --git a/player_skate.h b/player_skate.h index e87e4ba..11c66e9 100644 --- a/player_skate.h +++ b/player_skate.h @@ -39,6 +39,9 @@ struct player_skate m3x3f velocity_bias, velocity_bias_pstep; v3f apex; + v3f up_dir; + + v3f head_position; int lift_frames; diff --git a/rigidbody.h b/rigidbody.h index 324e5e1..5ca3e1e 100644 --- a/rigidbody.h +++ b/rigidbody.h @@ -2623,6 +2623,24 @@ VG_STATIC void rb_effect_simple_bouyency( rigidbody *ra, v4f plane, v3_muls( ra->v, 1.0f-(drag*k_rb_delta), ra->v ); } +/* apply a spring&dampener force to match ra(worldspace) on rigidbody, to + * rt(worldspace) + */ +VG_STATIC void rb_effect_spring_target_vector( rigidbody *rba, v3f ra, v3f rt, + float spring, float dampening, + float timestep ) +{ + float a = acosf( vg_clampf( v3_dot( rt, ra ), -1.0f, 1.0f ) ); + + v3f axis; + v3_cross( rt, ra, axis ); + + float Fs = -a * spring, + Fd = -v3_dot( rba->w, axis ) * dampening; + + v3_muladds( rba->w, axis, (Fs+Fd) * timestep, rba->w ); +} + /* * ----------------------------------------------------------------------------- * BVH implementation, this is ONLY for VG_STATIC rigidbodies, its to slow for -- 2.25.1