i hope your hapy
[carveJwlIkooP6JGAAIwe30JlM.git] / player_walk.c
index 0503f48fde51853c300f8cccfaf868ca7c42d2b1..6b7788c1405a9be7854e8af56382b8d47c24c12f 100644 (file)
@@ -8,27 +8,34 @@ VG_STATIC void player_walk_drop_in_vector( player_instance *player, v3f vec )
    struct player_walk *w = &player->_walk;
 
    v3f axis, init_dir;
-   v3_cross( (v3f){0.0f,1.0f,0.0f}, w->state.drop_in_normal, axis );
+   v3_cross( player->basis[1], 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 float player_xyspeed2( player_instance *player )
+{
+   v3f xy;
+   v3_muladds( player->rb.v, player->basis[1], 
+               -v3_dot( player->basis[1], player->rb.v ), xy );
+
+   return v3_length2(xy);
+}
+
 VG_STATIC void player_walk_generic_to_skate( player_instance *player,
-                                             enum skate_activity init )
+                                             enum skate_activity init,
+                                             float yaw )
 {
    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;
+   v3f v;
 
-   if( v3_length2( xy_speed ) < 0.1f * 0.1f )
-      q_mulv( player->rb.q, (v3f){0.0f,0.0f,-1.6f}, v );
+   if( player_xyspeed2(player) < 0.1f * 0.1f )
+      q_mulv( player->rb.q, (v3f){0.0f,0.0f,1.6f}, v );
    else
       v3_copy( player->rb.v, v );
 
@@ -38,17 +45,22 @@ VG_STATIC void player_walk_generic_to_skate( player_instance *player,
    v3f dir;
    v3_copy( v, dir );
    v3_normalize( dir );
+   m3x3_mulv( player->invbasis, dir, dir );
 
-   q_axis_angle( player->rb.q, (v3f){0.0f,1.0f,0.0f}, 
-                 atan2f( -dir[0], -dir[2] ) );
+   q_axis_angle( player->rb.q, (v3f){0.0f,1.0f,0.0f}, atan2f(-dir[0],-dir[2]) );
+   q_mul( player->qbasis, player->rb.q, player->rb.q );
+   q_normalize( player->rb.q );
+
+   q_mulv( player->rb.q, (v3f){0.0f,1.0f,0.0f}, s->state.cog );
+   v3_add( s->state.cog, player->rb.co, s->state.cog );
 
-   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){yaw,0.0f,0.0f}, s->state.trick_euler );
 
    if( init == k_skate_activity_air )
       player__approximate_best_trajectory( player );
@@ -74,11 +86,39 @@ VG_STATIC void player_walk_drop_in_to_skate( player_instance *player )
    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( player->basis[1], 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;
+   world_instance *world = get_active_world();
 
    v3f dir, center;
    q_mulv( player->rb.q, (v3f){0.0f,0.0f,1.0f}, dir );
@@ -101,7 +141,7 @@ VG_STATIC int player_walk_scan_for_drop_in( player_instance *player )
       ray_hit *ray = &samples[ sample_count ];
       ray->dist = 2.0f;
 
-      if( ray_world( pos, ray_dir, ray ) )
+      if( ray_world( world, pos, ray_dir, ray ) )
       {
          vg_line( pos, ray->pos, VG__RED );
          vg_line_pt3( ray->pos, 0.025f, VG__BLACK );
@@ -153,6 +193,48 @@ VG_STATIC int player_walk_scan_for_drop_in( player_instance *player )
       {
          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( 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( 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;
@@ -186,10 +268,16 @@ VG_STATIC void player__walk_pre_update( player_instance *player )
          {
             player_walk_drop_in_to_skate( player );
          }
+         else if( w->state.outro_type == k_walk_outro_jump_to_air )
+         {
+            player_walk_generic_to_skate( player, k_skate_activity_air, 0.0f );
+         }
          else
          {
-            player_walk_generic_to_skate( player, k_skate_activity_air );
+            player_walk_generic_to_skate( player, 
+                                          k_skate_activity_ground, 1.0f );
          }
+
          return;
       }
    }
@@ -203,18 +291,22 @@ VG_STATIC void player__walk_pre_update( player_instance *player )
             w->state.outro_anim = w->anim_drop_in;
             w->state.outro_start_time = vg.time;
             w->state.activity = k_walk_activity_immobile;
-            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] );
 
             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_regular;
+            w->state.outro_anim = w->anim_intro;
+            w->state.outro_start_time = vg.time;
+            w->state.activity = k_walk_activity_lockedmove;
+
+            if( player_xyspeed2(player) < 0.1f * 0.1f )
+               q_mulv( player->rb.q, (v3f){0.0f,0.0f,1.6f}, player->rb.v );
+         }
       }
       else
       {
@@ -226,9 +318,9 @@ VG_STATIC void player__walk_pre_update( player_instance *player )
    }
 }
 
-VG_STATIC int player_walk_normal_standable( v3f n )
+VG_STATIC int player_walk_normal_standable( player_instance *player, v3f n )
 {
-   return n[1] > 0.70710678118f;
+   return v3_dot( n, player->basis[1] ) > 0.70710678118f;
 }
 
 VG_STATIC void player_accelerate( v3f v, v3f movedir, float speed, float accel )
@@ -276,8 +368,8 @@ VG_STATIC void player__walk_update( player_instance *player )
    w->collider.radius = 0.3f;
 
    m4x3f mtx;
-   m3x3_identity( mtx );
-   v3_add( player->rb.co, (v3f){0.0f, 1.0f, 0.0f}, mtx[3] );
+   m3x3_copy( player->rb.to_world, mtx );
+   v3_add( player->rb.co, player->basis[1], mtx[3] );
 
    debug_capsule( mtx, w->collider.radius, w->collider.height, VG__WHITE );
 
@@ -289,6 +381,10 @@ VG_STATIC void player__walk_update( player_instance *player )
    v3f forward_dir = { sinf(yaw),          0.0f, -cosf(yaw) };
    v3f right_dir   = { -forward_dir[2],    0.0f,  forward_dir[0] };
 
+   m3x3_mulv( player->basis, forward_dir, forward_dir );
+   m3x3_mulv( player->basis, right_dir,   right_dir );
+
+
    v2f walk = { player->input_walkh->axis.value,
                 player->input_walkv->axis.value };
 
@@ -300,26 +396,32 @@ VG_STATIC void player__walk_update( player_instance *player )
    /* 
     * Collision detection
     */
+   world_instance *world = get_active_world();
+
    len = rb_capsule__scene( mtx, &w->collider, NULL, 
-                            &world.rb_geo.inf.scene, manifold );
+                            &world->rb_geo.inf.scene, manifold );
    rb_manifold_filter_coplanar( manifold, len, 0.01f );
    len = rb_manifold_apply_filtered( manifold, len );
 
    v3f surface_avg = { 0.0f, 0.0f, 0.0f };
-   w->state.activity = k_walk_activity_air;
+
+   if( w->state.activity != k_walk_activity_lockedmove )
+      w->state.activity = k_walk_activity_air;
 
    for( int i=0; i<len; i++ )
    {
       struct contact *ct = &manifold[i];
       rb_debug_contact( ct );
 
-      if( player_walk_normal_standable( ct->n ) )
+      if( player_walk_normal_standable( player, ct->n ) )
       {
-         w->state.activity = k_walk_activity_ground;
+         if( w->state.activity != k_walk_activity_lockedmove )
+            w->state.activity = k_walk_activity_ground;
+
          v3_add( surface_avg, ct->n, surface_avg );
       }
 
-      rb_prepare_contact( ct );
+      rb_prepare_contact( ct, k_rb_delta );
    }
 
    /* 
@@ -327,18 +429,16 @@ VG_STATIC void player__walk_update( player_instance *player )
     */
    float accel_speed = 0.0f, nominal_speed = 0.0f;
    v3f movedir;
-   v3_muls( right_dir,   walk[0], movedir );
+   v3_muls( right_dir,  walk[0], movedir );
    v3_muladds( movedir, forward_dir, walk[1], movedir );
 
-   if( w->state.activity == k_walk_activity_ground )
-   {
+   if( w->state.activity == k_walk_activity_ground ){
       v3_normalize( surface_avg );
 
       v3f tx, ty;
       rb_tangent_basis( surface_avg, tx, ty );
 
-      if( v2_length2(walk) > 0.001f )
-      {
+      if( v2_length2(walk) > 0.001f ){
          /* clip movement to the surface */
          float d = v3_dot(surface_avg,movedir);
          v3_muladds( movedir, surface_avg, -d, movedir );
@@ -348,29 +448,25 @@ VG_STATIC void player__walk_update( player_instance *player )
       nominal_speed = k_walkspeed;
 
       /* jump */
-      if( player->input_jump->button.value )
-      {
-         player->rb.v[1] = 5.0f;
+      if( player->input_jump->button.value ){
+         v3_muladds( player->rb.v, player->basis[1], 5.0f, player->rb.v );
          w->state.activity = k_walk_activity_air;
          accel_speed = k_walk_air_accel;
          nominal_speed = k_airspeed;
       }
-      else
-      {
+      else{
          player_friction( player->rb.v );
 
-         struct world_material *surface_mat = world_contact_material(manifold);
-         w->surface = surface_mat->info.surface_prop;
+         struct world_surface *surf = world_contact_surface( world, manifold );
+         w->surface = surf->info.surface_prop;
       }
    }
-   else
-   {
+   else{
       accel_speed = k_walk_air_accel;
       nominal_speed = k_airspeed;
    }
 
-   if( v2_length2(walk) > 0.001f )
-   {
+   if( v2_length2(walk) > 0.001f ){
       player_accelerate( player->rb.v, movedir, nominal_speed, accel_speed );
       v3_normalize( movedir );
    }
@@ -378,10 +474,8 @@ VG_STATIC void player__walk_update( player_instance *player )
    /*
     * Resolve velocity constraints
     */
-   for( int j=0; j<5; j++ )
-   {
-      for( int i=0; i<len; i++ )
-      {
+   for( int j=0; j<5; j++ ){
+      for( int i=0; i<len; i++ ){
          struct contact *ct = &manifold[i];
          
          /*normal */
@@ -399,20 +493,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......
@@ -448,12 +529,13 @@ VG_STATIC void player__walk_update( player_instance *player )
 
    /* integrate */
    if( w->state.activity == k_walk_activity_air )
-      player->rb.v[1] += -k_gravity * k_rb_delta;
+   {
+      v3_muladds( player->rb.v, player->basis[1], -k_gravity*k_rb_delta, 
+                  player->rb.v );
+   }
 
    v3_muladds( player->rb.co, player->rb.v, k_rb_delta, player->rb.co );
-
-
-   v3_add( player->rb.co, (v3f){0.0f, 1.0f, 0.0f}, mtx[3] );
+   v3_add( player->rb.co, player->basis[1], mtx[3] );
    debug_capsule( mtx, w->collider.radius, w->collider.height, VG__GREEN );
 
    /* 
@@ -478,22 +560,27 @@ VG_STATIC void player__walk_update( player_instance *player )
       float t, sr = w->collider.radius-0.04f;
       v3f n;
 
-      if( spherecast_world( lwr_prev, lwr_now, sr, &t, n ) != -1 )
+      if( spherecast_world( world, lwr_prev, lwr_now, sr, &t, n ) != -1 )
       {
          v3_lerp( lwr_prev, lwr_now, vg_maxf(0.01f,t), player->rb.co );
          player->rb.co[1] -= w->collider.radius;
          rb_update_transform( &player->rb );
 
-         v3_add( player->rb.co, (v3f){0.0f, 1.0f, 0.0f}, mtx[3] );
+         v3_add( player->rb.co, player->basis[1], mtx[3] );
          debug_capsule( mtx, w->collider.radius, w->collider.height, VG__RED );
       }
    }
 
-   teleport_gate *gate;
-   if( (gate = world_intersect_gates( player->rb.co, w->state.prev_pos )) )
-   {
+   ent_gate *gate = 
+      world_intersect_gates( world, player->rb.co, w->state.prev_pos );
+   if( gate ){
       m4x3_mulv( gate->transport, player->rb.co, player->rb.co );
       m3x3_mulv( gate->transport, player->rb.v,  player->rb.v );
+
+      v4f transport_rotation;
+      m3x3_q( gate->transport, transport_rotation );
+      q_mul( transport_rotation, player->rb.q, player->rb.q );
+
       rb_update_transform( &player->rb );
 
       w->state_gate_storage = w->state;
@@ -506,8 +593,8 @@ VG_STATIC void player__walk_post_update( player_instance *player )
    struct player_walk *w = &player->_walk;
 
    m4x3f mtx;
-   m3x3_identity( mtx );
-   v3_add( player->rb.co, (v3f){0.0f, 1.0f, 0.0f}, mtx[3] );
+   m3x3_copy( player->rb.to_world, mtx );
+   v3_add( player->rb.co, player->basis[1], 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] );
@@ -515,15 +602,16 @@ VG_STATIC void player__walk_post_update( player_instance *player )
 
 
    /* 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 )
+   v3f v;
+   if( player_xyspeed2(player) > 0.1f*0.1f )
    {
-      float a = atan2f( player->rb.v[0], player->rb.v[2] );
+      v3f v_xy;
+      m3x3_mulv( player->invbasis, player->rb.v, v_xy );
+      float a = atan2f( v_xy[0], v_xy[2] );
+
       q_axis_angle( player->rb.q, (v3f){0.0f,1.0f,0.0f}, a );
+      q_mul( player->qbasis, player->rb.q, player->rb.q );
+      q_normalize( player->rb.q );
    }
 
    vg_line_pt3( w->state.drop_in_target, 0.1f, VG__GREEN );
@@ -541,6 +629,7 @@ VG_STATIC void player__walk_post_update( player_instance *player )
    p1[0] = sinf( a );
    p1[1] = 0.0f;
    p1[2] = cosf( a );
+   m3x3_mulv( player->basis, p1, p1 );
 
    v3_add( player->rb.co, p1, p1 );
    vg_line( player->rb.co, p1, VG__PINK );
@@ -658,30 +747,13 @@ VG_STATIC void player__walk_animate( player_instance *player,
          if( step_t >= 1.0f )
          {
             v4f final_q;
-            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] ) * dop_t;
-
-            q_axis_angle( final_q, axis, a );
+            player_walk_drop_in_overhang_transform( player, dop_t, 
+                                                    player->rb.co, final_q );
             q_mul( final_q, dest->root_q, dest->root_q );
 
-            float l = dop_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( final_q, overhang, overhang );
-
-            v3f overhang_pos;
-            v3_add( w->state.drop_in_target, overhang, player->rb.co );
             v4_copy( dest->root_q, player->rb.q );
             v3_muladds( dest->root_co, player->rb.to_world[1], 
-                        -0.28f * dop_t, dest->root_co );
+                        -0.1f * dop_t, dest->root_co );
 
             skeleton_copy_pose( sk, dest->pose, player->holdout_pose );
             player->holdout_time = 1.0f;
@@ -691,7 +763,7 @@ VG_STATIC void player__walk_animate( player_instance *player,
       else
       {
          v3_muladds( dest->root_co, player->rb.to_world[1], 
-                     -0.28f * outro_t, dest->root_co );
+                     -0.1f * outro_t, dest->root_co );
 
          skeleton_copy_pose( sk, dest->pose, player->holdout_pose );
          player->holdout_time = 1.0f;
@@ -703,6 +775,8 @@ VG_STATIC void player__walk_animate( player_instance *player,
    }
 
    q_axis_angle( dest->root_q, (v3f){0.0f,1.0f,0.0f}, walk_yaw + VG_PIf );
+   q_mul( player->qbasis, dest->root_q, dest->root_q );
+   q_normalize( dest->root_q );
 }
 
 VG_STATIC void player__walk_post_animate( player_instance *player )
@@ -740,7 +814,8 @@ VG_STATIC void player__walk_im_gui( player_instance *player )
                            (const char *[]){ "k_walk_activity_air",
                                              "k_walk_activity_ground",
                                              "k_walk_activity_sleep",
-                                             "k_walk_activity_immobile" }
+                                             "k_walk_activity_immobile",
+                                             "k_walk_activity_lockedmove" }
                                              [w->state.activity] );
 
    if( w->state.outro_anim )
@@ -764,6 +839,7 @@ VG_STATIC void player__walk_bind( player_instance *player )
    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" );
+   w->anim_intro        = skeleton_get_anim( sk, "into_skate" );
 }
 
 VG_STATIC void player__walk_transition( player_instance *player, v3f angles )
@@ -773,9 +849,12 @@ VG_STATIC void player__walk_transition( player_instance *player, v3f angles )
 
    v3f fwd = { 0.0f, 0.0f, 1.0f };
    q_mulv( player->rb.q, fwd, fwd );
+   m3x3_mulv( player->invbasis, fwd, fwd );
+
+   q_axis_angle( player->rb.q, (v3f){0.0f,1.0f,0.0f}, atan2f(fwd[0], fwd[2]) );
+   q_mul( player->qbasis, player->rb.q, player->rb.q );
+   q_normalize( player->rb.q );
 
-   q_axis_angle( player->rb.q, (v3f){0.0f,1.0f,0.0f}, 
-                 atan2f( fwd[0], fwd[2] ) );
    rb_update_transform( &player->rb );
 }