better? grinds
[carveJwlIkooP6JGAAIwe30JlM.git] / player_walk.c
index 77386fa22b089285b91b67049360579ce25516b1..d5f4aa0c28df9d57a1200ff762b6723f3f5c5bcb 100644 (file)
 
 #include "player.h"
 
-VG_STATIC void player__walk_pre_update( player_instance *player )
+VG_STATIC void player_walk_drop_in_vector( player_instance *player, v3f vec )
 {
    struct player_walk *w = &player->_walk;
-   player_look( player, w->state.angles );
 
-   if( vg_input_button_down( player->input_use ) )
-   {
-      v3f xy_speed, v;
+   v3f axis, init_dir;
+   v3_cross( (v3f){0.0f,1.0f,0.0f}, w->state.drop_in_normal, axis );
+   v3_cross( axis, w->state.drop_in_normal, init_dir );
+   v3_normalize( init_dir );
+   v3_muls( init_dir, 7.0f, vec );
+}
+
+VG_STATIC void player_walk_generic_to_skate( player_instance *player,
+                                             enum skate_activity init )
+{
+   player->subsystem = k_player_subsystem_skate;
+
+   struct player_walk *w = &player->_walk;
+   struct player_skate *s = &player->_skate;
+
+   v3f xy_speed, v;
+
+   v3_copy( player->rb.v, xy_speed );
+   xy_speed[1] = 0.0f;
+
+   if( v3_length2( xy_speed ) < 0.1f * 0.1f )
+      q_mulv( player->rb.q, (v3f){0.0f,0.0f,-1.6f}, v );
+   else
       v3_copy( player->rb.v, v );
-      v3_copy( v, xy_speed );
-      xy_speed[1] = 0.0f;
 
-      if( v3_length2( xy_speed ) < 0.1f * 0.1f )
+   s->state.activity_prev = k_skate_activity_ground;
+   s->state.activity = init;
+
+   v3f dir;
+   v3_copy( v, dir );
+   v3_normalize( dir );
+
+   q_axis_angle( player->rb.q, (v3f){0.0f,1.0f,0.0f}, 
+                 atan2f( -dir[0], -dir[2] ) );
+
+   v3_muladds( player->rb.co, player->rb.to_world[1], 1.0f, s->state.cog );
+   v3_copy( v, s->state.cog_v );
+   v3_copy( v, player->rb.v );
+
+   player__skate_reset_animator( player );
+   player__skate_clear_mechanics( player );
+   rb_update_transform( &player->rb );
+   v3_copy( (v3f){0.0f,0.0f,0.0f}, s->state.trick_euler );
+
+   if( init == k_skate_activity_air )
+      player__approximate_best_trajectory( player );
+}
+
+VG_STATIC void player_walk_drop_in_to_skate( player_instance *player )
+{
+   player->subsystem = k_player_subsystem_skate;
+
+   struct player_walk *w = &player->_walk;
+   struct player_skate *s = &player->_skate;
+   s->state.activity_prev = k_skate_activity_ground;
+   s->state.activity = k_skate_activity_ground;
+
+   player__skate_clear_mechanics( player );
+   player__skate_reset_animator( player );
+
+   v3f init_velocity;
+   player_walk_drop_in_vector( player, init_velocity );
+
+   rb_update_transform( &player->rb );
+   v3_muladds( player->rb.co, player->rb.to_world[1], 1.0f, s->state.cog );
+   v3_copy( init_velocity, s->state.cog_v );
+   v3_copy( init_velocity, player->rb.v );
+   v3_copy( init_velocity, player->cam_velocity_smooth );
+   v3_copy( (v3f){1.0f,0.0f,0.0f}, s->state.trick_euler );
+}
+
+VG_STATIC void player_walk_drop_in_overhang_transform( player_instance *player,
+                                                       float t,
+                                                       v3f co, v4f q )
+{
+   struct player_walk *w = &player->_walk;
+
+   v3f axis;
+   v3_cross( (v3f){0.0f,1.0f,0.0f}, w->state.drop_in_normal, axis );
+   v3_normalize( axis );
+
+   float a = acosf( w->state.drop_in_normal[1] ) * t;
+
+   q_axis_angle( q, axis, a );
+
+   float l = t * 0.5f,
+         heading_angle = w->state.drop_in_angle;
+
+   v3f overhang;
+   overhang[0] = sinf( heading_angle ) * l;
+   overhang[1] = 0.28f * l;
+   overhang[2] = cosf( heading_angle ) * l;
+
+   q_mulv( q, overhang, overhang );
+   v3_add( w->state.drop_in_target, overhang, co );
+}
+
+VG_STATIC int player_walk_scan_for_drop_in( player_instance *player )
+{
+   struct player_walk *w = &player->_walk;
+
+   v3f dir, center;
+   q_mulv( player->rb.q, (v3f){0.0f,0.0f,1.0f}, dir );
+   v3_muladds( player->rb.co, player->rb.to_world[1], -1.0f, center );
+
+   ray_hit samples[20];
+   int sample_count = 0;
+
+   for( int i=0; i<20; i ++ )
+   {
+      float t = (float)i * (1.0f/19.0f),
+            s = sinf( t * VG_PIf * 0.25f ),
+            c = cosf( t * VG_PIf * 0.25f );
+
+      v3f ray_dir, pos;
+      v3_muls   ( player->rb.to_world[1], -c, ray_dir );
+      v3_muladds( ray_dir, dir, -s, ray_dir );
+      v3_muladds( center, ray_dir, -2.0f, pos );
+
+      ray_hit *ray = &samples[ sample_count ];
+      ray->dist = 2.0f;
+
+      if( ray_world( pos, ray_dir, ray ) )
       {
-         v[0] = -sinf( -w->state.angles[0] );
-         v[1] =  0.0f;
-         v[2] = -cosf( -w->state.angles[0] );
-         v3_muls( v, 1.6f, v );
+         vg_line( pos, ray->pos, VG__RED );
+         vg_line_pt3( ray->pos, 0.025f, VG__BLACK );
+         
+         sample_count ++;
       }
+   }
 
-      player->subsystem = k_player_subsystem_skate;
-      player__skate_transition( player, v );
-      return;
+   float min_a = 0.70710678118654752f;
+   ray_hit *candidate = NULL;
+
+   if( sample_count >= 2 )
+   {
+      for( int i=0; i<sample_count-1; i++ )
+      {
+         ray_hit *s0 = &samples[i],
+                 *s1 = &samples[i+1];
+
+         float a = v3_dot( s0->normal, s1->normal );
+
+         if( (a < min_a) && (a >= -0.1f) && (s0->normal[1]>s1->normal[1]) )
+         {
+            min_a = a;
+            candidate = s0;
+         }
+      }
+   }
+
+   if( candidate )
+   {
+      v4f pa, pb, pc;
+      
+      ray_hit *s0 = candidate,
+              *s1 = candidate+1;
+
+      vg_line( s0->pos, s1->pos, VG__WHITE );
+
+      v3_copy( s0->normal, pa );
+      v3_copy( s1->normal, pb );
+      v3_cross( player->rb.to_world[1], dir, pc );
+      v3_normalize( pc );
+
+      pa[3] = v3_dot( pa, s0->pos );
+      pb[3] = v3_dot( pb, s1->pos );
+      pc[3] = v3_dot( pc, player->rb.co );
+
+      v3f edge;
+      if( plane_intersect3( pa, pb, pc, edge ) )
+      {
+         v3_copy( edge, w->state.drop_in_target );
+         v3_copy( s1->normal, w->state.drop_in_normal );
+         v3_copy( player->rb.co, w->state.drop_in_start );
+
+         w->state.drop_in_start_angle = player_get_heading_yaw( player );
+         w->state.drop_in_angle = atan2f( w->state.drop_in_normal[0],
+                                          w->state.drop_in_normal[2] );
+
+         /* TODO: scan multiple of these? */
+         v3f oco;
+         v4f oq;
+         player_walk_drop_in_overhang_transform( player, 1.0f, oco, oq );
+
+         v3f va = {0.0f,0.0f,-k_board_length - 0.3f},
+             vb = {0.0f,0.0f, k_board_length + 0.3f};
+
+         q_mulv( oq, va, va );
+         q_mulv( oq, vb, vb );
+         v3_add( oco, va, va );
+         v3_add( oco, vb, vb );
+
+         v3f v0;
+         v3_sub( vb, va, v0 );
+         v3_normalize( v0 );
+
+         ray_hit ray;
+         ray.dist = k_board_length*2.0f + 0.6f;
+         
+         if( ray_world( va, v0, &ray ) )
+         {
+            vg_line( va, vb, VG__RED );
+            vg_line_pt3( ray.pos, 0.1f, VG__RED );
+            vg_error( "invalidated\n" );
+            return 0;
+         }
+
+         v3_muls( v0, -1.0f, v0 );
+         if( ray_world( vb, v0, &ray ) )
+         {
+            vg_line( va, vb, VG__RED );
+            vg_line_pt3( ray.pos, 0.1f, VG__RED );
+            vg_error( "invalidated\n" );
+            return 0;
+         }
+
+         player_walk_drop_in_vector( player, player->rb.v );
+         return 1;
+      }
+      else
+      {
+         vg_error( "failed to find intersection of drop in\n" );
+      }
+   }
+
+   return 0;
+}
+
+VG_STATIC void player__walk_pre_update( player_instance *player )
+{
+   struct player_walk *w = &player->_walk;
+
+   if( w->state.activity != k_walk_activity_immobile )
+      player_look( player, player->angles );
+
+   if( w->state.outro_anim )
+   {
+      float outro_length = (float)w->state.outro_anim->length /
+                                  w->state.outro_anim->rate,
+            outro_time   = vg.time - w->state.outro_start_time;
+
+      if( outro_time >= outro_length )
+      {
+         w->state.outro_anim = NULL;
+         if( w->state.outro_type == k_walk_outro_drop_in )
+         {
+            player_walk_drop_in_to_skate( player );
+         }
+         else
+         {
+            player_walk_generic_to_skate( player, k_skate_activity_air );
+         }
+         return;
+      }
+   }
+   else if( vg_input_button_down( player->input_use ) )
+   {
+      if( w->state.activity == k_walk_activity_ground )
+      {
+         if( player_walk_scan_for_drop_in( player ) )
+         {
+            w->state.outro_type = k_walk_outro_drop_in;
+            w->state.outro_anim = w->anim_drop_in;
+            w->state.outro_start_time = vg.time;
+            w->state.activity = k_walk_activity_immobile;
+
+            struct player_avatar *av = player->playeravatar;
+            m4x3_mulv( av->sk.final_mtx[ av->id_ik_foot_r ], 
+                       av->sk.bones[ av->id_ik_foot_r ].co, 
+                       w->state.drop_in_foot_anchor );
+         }
+         return;
+      }
+      else
+      {
+         w->state.outro_type = k_walk_outro_jump_to_air;
+         w->state.outro_anim = w->anim_jump_to_air;
+         w->state.outro_start_time = vg.time;
+         return;
+      }
    }
 }
 
@@ -72,6 +334,9 @@ VG_STATIC void player__walk_update( player_instance *player )
    struct player_walk *w = &player->_walk;
    v3_copy( player->rb.co, w->state.prev_pos );
 
+   if( w->state.activity == k_walk_activity_immobile )
+      return;
+
    w->collider.height = 2.0f;
    w->collider.radius = 0.3f;
 
@@ -84,7 +349,7 @@ VG_STATIC void player__walk_update( player_instance *player )
    rb_ct manifold[64];
    int len;
 
-   float yaw = w->state.angles[0];
+   float yaw = player->angles[0];
 
    v3f forward_dir = { sinf(yaw),          0.0f, -cosf(yaw) };
    v3f right_dir   = { -forward_dir[2],    0.0f,  forward_dir[0] };
@@ -119,9 +384,8 @@ VG_STATIC void player__walk_update( player_instance *player )
          v3_add( surface_avg, ct->n, surface_avg );
       }
 
-      rb_prepare_contact( ct );
+      rb_prepare_contact( ct, k_rb_delta );
    }
-   
 
    /* 
     * Move & Friction
@@ -200,20 +464,7 @@ VG_STATIC void player__walk_update( player_instance *player )
     * Depenetrate
     */
    v3f dt;
-   v3_zero( dt );
-   for( int j=0; j<8; j++ )
-   {
-      for( int i=0; i<len; i++ )
-      {
-         struct contact *ct = &manifold[i];
-
-         float resolved_amt = v3_dot( ct->n, dt ),
-               remaining    = (ct->p-k_penetration_slop) - resolved_amt,
-               apply        = vg_maxf( remaining, 0.0f ) * 0.3f;
-
-         v3_muladds( dt, ct->n, apply, dt );
-      }
-   }
+   rb_depenetrate( manifold, len, dt );
    v3_add( dt, player->rb.co, player->rb.co );
 
    /* TODO: Stepping......
@@ -297,13 +548,6 @@ VG_STATIC void player__walk_update( player_instance *player )
       m3x3_mulv( gate->transport, player->rb.v,  player->rb.v );
       rb_update_transform( &player->rb );
 
-      /* analytical rotation of yaw */
-      v3f fwd_dir = { cosf(w->state.angles[0]),
-                      0.0f,
-                      sinf(w->state.angles[0])};
-      m3x3_mulv( gate->transport, fwd_dir, fwd_dir );
-      w->state.angles[0] = atan2f( fwd_dir[2], fwd_dir[0] );
-
       w->state_gate_storage = w->state;
       player__pass_gate( player, gate );
    }
@@ -314,12 +558,44 @@ VG_STATIC void player__walk_post_update( player_instance *player )
    struct player_walk *w = &player->_walk;
 
    m4x3f mtx;
-   m3x3_identity( mtx );
+   m3x3_copy( player->rb.to_world, mtx );
    v3_add( player->rb.co, (v3f){0.0f, 1.0f, 0.0f}, mtx[3] );
 
    float substep = vg_clampf( vg.accumulator / k_rb_delta, 0.0f, 1.0f );
    v3_muladds( mtx[3], player->rb.v, k_rb_delta*substep, mtx[3] );
    debug_capsule( mtx, w->collider.radius, w->collider.height, VG__YELOW );
+
+
+   /* Calculate header */
+   v3f xy_speed, v;
+
+   v3_copy( player->rb.v, xy_speed );
+   xy_speed[1] = 0.0f;
+
+   if( v3_length2( xy_speed ) > 0.1f * 0.1f )
+   {
+      float a = atan2f( player->rb.v[0], player->rb.v[2] );
+      q_axis_angle( player->rb.q, (v3f){0.0f,1.0f,0.0f}, a );
+   }
+
+   vg_line_pt3( w->state.drop_in_target, 0.1f, VG__GREEN );
+   v3f p1;
+   v3_muladds( w->state.drop_in_target, w->state.drop_in_normal, 0.3f, p1 );
+   vg_line( w->state.drop_in_target, p1, VG__GREEN );
+   v3_muladds( w->state.drop_in_target, player->rb.to_world[1], 0.3f, p1 );
+   vg_line( w->state.drop_in_target, p1, VG__GREEN );
+
+   vg_line( w->state.drop_in_target, w->state.drop_in_foot_anchor, VG__WHITE );
+   vg_line_pt3( w->state.drop_in_foot_anchor, 0.08f, VG__PINK );
+
+
+   float a = player_get_heading_yaw( player );
+   p1[0] = sinf( a );
+   p1[1] = 0.0f;
+   p1[2] = cosf( a );
+
+   v3_add( player->rb.co, p1, p1 );
+   vg_line( player->rb.co, p1, VG__PINK );
 }
 
 VG_STATIC void player__walk_animate( player_instance *player, 
@@ -378,12 +654,90 @@ VG_STATIC void player__walk_animate( player_instance *player,
 
    /* air */
    skeleton_sample_anim( sk, w->anim_jump, vg.time*0.6f, bpose );
-   skeleton_lerp_pose( sk, apose, bpose, w->blend_fly, dest->pose );
+   skeleton_lerp_pose( sk, apose, bpose, w->blend_fly, apose );
 
    /* Create transform */
    rb_extrapolate( &player->rb, dest->root_co, dest->root_q );
-   q_axis_angle( dest->root_q, (v3f){0.0f,1.0f,0.0f}, 
-                 -w->state.angles[0]-VG_PIf*0.5f );
+
+   float walk_yaw = player_get_heading_yaw( player );
+
+   if( w->state.outro_anim )
+   {
+      struct player_avatar *av = player->playeravatar;
+      float outro_length = (float)w->state.outro_anim->length /
+                                  w->state.outro_anim->rate,
+            outro_time   = vg.time - w->state.outro_start_time,
+            outro_t      = outro_time / outro_length;
+
+      /* TODO: Compression */
+      skeleton_sample_anim_clamped( sk, w->state.outro_anim, 
+                                    outro_time, bpose );
+      skeleton_lerp_pose( sk, apose, bpose, outro_t * 10.0f, dest->pose );
+
+      if( w->state.outro_type == k_walk_outro_drop_in )
+      {
+         float inv_rate      = 1.0f / w->state.outro_anim->rate,
+               anim_frames   = w->state.outro_anim->length * inv_rate,
+               step_frames   = 12.0f * inv_rate,
+               commit_frames = 6.0f * inv_rate,
+               drop_frames   = anim_frames - step_frames,
+               step_t        = vg_minf( 1.0f, outro_time / step_frames ),
+               remaind_time  = vg_maxf( 0.0f, outro_time - step_frames ),
+               dop_t         = vg_minf( 1.0f, remaind_time / drop_frames ),
+               commit_t      = vg_minf( 1.0f, remaind_time / commit_frames );
+
+         walk_yaw = vg_alerpf( w->state.drop_in_start_angle, 
+                               w->state.drop_in_angle, step_t );
+
+         v3_lerp( w->state.drop_in_start, w->state.drop_in_target,
+                  step_t, player->rb.co );
+         q_axis_angle( dest->root_q, (v3f){0.0f,1.0f,0.0f}, walk_yaw + VG_PIf );
+
+         m4x3f transform, inverse;
+         q_m3x3( dest->root_q, transform );
+         v3_copy( dest->root_co, transform[3] );
+         m4x3_invert_affine( transform, inverse );
+
+         v3f anchored_pos;
+         m4x3_mulv( inverse, w->state.drop_in_foot_anchor, anchored_pos );
+
+         v3_lerp( dest->pose[ av->id_ik_foot_r-1 ].co, anchored_pos, 
+                  1.0f-commit_t,
+                  dest->pose[ av->id_ik_foot_r-1 ].co );
+         
+
+         /* the drop in bit */
+         if( step_t >= 1.0f )
+         {
+            v4f final_q;
+            player_walk_drop_in_overhang_transform( player, dop_t, 
+                                                    player->rb.co, final_q );
+            q_mul( final_q, dest->root_q, dest->root_q );
+
+            v4_copy( dest->root_q, player->rb.q );
+            v3_muladds( dest->root_co, player->rb.to_world[1], 
+                        -0.1f * dop_t, dest->root_co );
+
+            skeleton_copy_pose( sk, dest->pose, player->holdout_pose );
+            player->holdout_time = 1.0f;
+         }
+         return;
+      }
+      else
+      {
+         v3_muladds( dest->root_co, player->rb.to_world[1], 
+                     -0.1f * outro_t, dest->root_co );
+
+         skeleton_copy_pose( sk, dest->pose, player->holdout_pose );
+         player->holdout_time = 1.0f;
+      }
+   }
+   else
+   {
+      skeleton_copy_pose( sk, apose, dest->pose );
+   }
+
+   q_axis_angle( dest->root_q, (v3f){0.0f,1.0f,0.0f}, walk_yaw + VG_PIf );
 }
 
 VG_STATIC void player__walk_post_animate( player_instance *player )
@@ -394,46 +748,43 @@ VG_STATIC void player__walk_post_animate( player_instance *player )
    struct player_walk *w = &player->_walk;
    struct player_avatar *av = player->playeravatar;
 
-   /* 3RD */
-   m3x3f angles;
-   euler_m3x3( w->state.angles, angles );
-
-   v3f cast_dir, origin;
-
-   v3_add( player->rb.co, (v3f){0.0f,2.0f,0.0f}, origin );
-
-   v3_muladds( origin, angles[2], 2.0f, player->cam3.co );
-   v3_muladds( player->cam3.co, angles[0], 0.5f, player->cam3.co );
-
-   float t;
-   v3f n;
-   if( spherecast_world( origin, player->cam3.co, 0.1f, &t, n ) != -1 )
-      v3_lerp( origin, player->cam3.co, t, player->cam3.co );
-   v3_copy( w->state.angles, player->cam3.angles );
-
-   /* 1ST */
-   /* FIXME: viewpoint entity */
-   v3f vp = {-0.1f,1.8f,0.0f};
-   m4x3_mulv( av->sk.final_mtx[ av->id_head-1 ], vp, player->cam1.co );
-   v3_copy( w->state.angles, player->cam1.angles );
-
-   /* FIXME: Organize this. Its int wrong fucking place */
-   v3f vp0 = {0.0f,0.1f, 0.6f},
-       vp1 = {0.0f,0.1f,-0.6f};
+   if( w->state.outro_anim )
+   {
+      float outro_length = (float)w->state.outro_anim->length /
+                                  w->state.outro_anim->rate,
+            outro_time   = vg.time - w->state.outro_start_time,
+            outro_t      = outro_time / outro_length;
 
-   m4x3_mulv( av->sk.final_mtx[ av->id_board ], vp0, TEMP_BOARD_0 );
-   m4x3_mulv( av->sk.final_mtx[ av->id_board ], vp1, TEMP_BOARD_1 );
+      player->cam_velocity_influence = outro_t;
+   }
+   else
+      player->cam_velocity_influence = 0.0f;
 }
 
 
 VG_STATIC void player__walk_im_gui( player_instance *player )
 {
+   struct player_walk *w = &player->_walk;
    player__debugtext( 1, "V:  %5.2f %5.2f %5.2f",player->rb.v[0],
                                                  player->rb.v[1],
                                                  player->rb.v[2] );
    player__debugtext( 1, "CO: %5.2f %5.2f %5.2f",player->rb.co[0],
                                                  player->rb.co[1],
                                                  player->rb.co[2] );
+   player__debugtext( 1, "activity: %s\n",
+                           (const char *[]){ "k_walk_activity_air",
+                                             "k_walk_activity_ground",
+                                             "k_walk_activity_sleep",
+                                             "k_walk_activity_immobile" }
+                                             [w->state.activity] );
+
+   if( w->state.outro_anim )
+   {
+      float outro_length = (float)w->state.outro_anim->length /
+                                  w->state.outro_anim->rate,
+            outro_time   = vg.time - w->state.outro_start_time;
+      player__debugtext( 1, "outro time: %f / %f", outro_time, outro_length );
+   }
 }
 
 VG_STATIC void player__walk_bind( player_instance *player )
@@ -442,16 +793,25 @@ VG_STATIC void player__walk_bind( player_instance *player )
    struct player_avatar *av = player->playeravatar;
    struct skeleton *sk = &av->sk;
 
-   w->anim_idle = skeleton_get_anim( sk, "idle_cycle" );
-   w->anim_walk = skeleton_get_anim( sk, "walk" );
-   w->anim_run  = skeleton_get_anim( sk, "run" );
-   w->anim_jump = skeleton_get_anim( sk, "jump" );
+   w->anim_idle         = skeleton_get_anim( sk, "idle_cycle+y" );
+   w->anim_walk         = skeleton_get_anim( sk, "walk+y" );
+   w->anim_run          = skeleton_get_anim( sk, "run+y" );
+   w->anim_jump         = skeleton_get_anim( sk, "jump+y" );
+   w->anim_jump_to_air  = skeleton_get_anim( sk, "jump_to_air" );
+   w->anim_drop_in      = skeleton_get_anim( sk, "drop_in" );
 }
 
 VG_STATIC void player__walk_transition( player_instance *player, v3f angles )
 {
    struct player_walk *w = &player->_walk;
-   v3_copy( angles, w->state.angles );
+   w->state.activity = k_walk_activity_air;
+
+   v3f fwd = { 0.0f, 0.0f, 1.0f };
+   q_mulv( player->rb.q, fwd, fwd );
+
+   q_axis_angle( player->rb.q, (v3f){0.0f,1.0f,0.0f}, 
+                 atan2f( fwd[0], fwd[2] ) );
+   rb_update_transform( &player->rb );
 }
 
 #endif /* PLAYER_DEVICE_WALK_H */