shits fucked
authorhgn <hgodden00@gmail.com>
Sat, 11 Feb 2023 19:42:00 +0000 (19:42 +0000)
committerhgn <hgodden00@gmail.com>
Sat, 11 Feb 2023 19:42:00 +0000 (19:42 +0000)
common.h
maps_src/mp_gridmap.mdl
player_skate.c
player_skate.h
rigidbody.h

index 2c48560e90c4ce5354a75510f68ba74603aa65bc..f9bcafa93a7c0fe34496c51365c6c48ba40609d0 100644 (file)
--- 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;
 
 
index 43b26adbd59e1191d08075e9b632a9018d9b3fd8..d7ae80c1c7518d383b5dab42b86cfcba300255ed 100644 (file)
Binary files a/maps_src/mp_gridmap.mdl and b/maps_src/mp_gridmap.mdl differ
index a04a0ddddd9d6b5f750b3978340686b2b4dda040..7088cf1377a24f29453a49839bc43d1540bf7469 100644 (file)
@@ -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; i<k_wheel_count; i++ )
-      wheels[i].state = k_collider_state_default;
-
-   /* calculate transform one step into future */
-   v3f future_co;
-   v4f future_q;
-   v3_muladds( player->rb.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; i<k_wheel_count; i++ )
-   {
-      if( wheels[i].state == k_collider_state_disabled )
-         continue;
-
-      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;
 
-   /*
-    * 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; i<k_wheel_count; i++ )
-   {
-      if( wheels[i].state == k_collider_state_disabled )
-         continue;
-
-      m4x3f mtx;
-      m3x3_identity( mtx );
-      m4x3_mulv( player->rb.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; j<l; j++ )
-         {
-            v3f ra;
-            v3_sub( man[j].co, player->rb.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; i<k_wheel_count; i++ )
+      wheels[i].state = k_collider_state_default;
 
+   /* calculate transform one step into future */
+   v3f future_co;
+   v4f future_q;
+   v3_muladds( player->rb.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; i<k_wheel_count; i++ )
+   {
+      if( wheels[i].state == k_collider_state_disabled )
+         continue;
 
-      v3_muladds( player->rb.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; i<k_wheel_count; i++ )
+   {
+      if( wheels[i].state == k_collider_state_disabled )
+         continue;
+
+      m4x3f mtx;
+      m3x3_identity( mtx );
+      m4x3_mulv( player->rb.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; j<l; j++ )
+         {
+            v3f ra;
+            v3_sub( man[j].co, player->rb.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; i<manifold_len; i ++ )
+   {
       rb_prepare_contact( &manifold[i], s->substep_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; i<manifold_len; i++ )
@@ -1921,7 +2047,10 @@ begin_collision:;
 
          float normal_mass = 1.0f / (inv_mass + v3_dot(raCn,raCnI)),
                vn = v3_dot( rv, ct->n ),
-               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; i<vg_list_size(apply_to); i ++ )
+         {
+            mdl_keyframe *kf = &dest->pose[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 */
index e87e4ba7ab64158e4e3649437b04d9557c18efad..11c66e9d203ccc2c4493613cf289664eae8328e2 100644 (file)
@@ -39,6 +39,9 @@ struct player_skate
       m3x3f velocity_bias,
             velocity_bias_pstep;
       v3f apex;
+      v3f up_dir;
+
+      v3f head_position;
 
       int lift_frames;
 
index 324e5e1a45cf7eff25441714e276d664867b1491..5ca3e1e4e4e8e7d7909a1535004619189db8b374 100644 (file)
@@ -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