physics adjustments
[carveJwlIkooP6JGAAIwe30JlM.git] / player_physics.h
index 52e4c3080f82aa4dcae6a9e5e4d6a182942d2c86..9d58e17dfa7196147b7ead3cc944a677c4dfbe09 100644 (file)
@@ -161,7 +161,7 @@ VG_STATIC void player_physics_control(void)
    }
 
    static int push_thresh_last = 0;
-   float push = player.input_push->axis.value;
+   float push = player.input_push->button.value;
    int push_thresh = push>0.15f? 1: 0;
    
    if( push_thresh && !push_thresh_last )
@@ -339,12 +339,32 @@ VG_STATIC void player_walk_physics(void)
    v3f forward_dir = { sinf(player.angles[0]),0.0f,-cosf(player.angles[0]) };
    v3f right_dir = { -forward_dir[2], 0.0f, forward_dir[0] };
 
+   v2f walk = { player.input_walkh->axis.value,
+                player.input_walkv->axis.value };
+   
+   if( v2_length2(walk) > 0.001f )
+      v2_normalize_clamp( walk );
+
    if( phys->in_air )
    {
       player_walk_update_collision();
       rb_debug( rbf, 0xff0000ff );
       rb_debug( rbb, 0xff0000ff );
 
+      /* allow player to accelerate a bit */
+      v3f walk_3d;
+      v3_muls( forward_dir, walk[1], walk_3d );
+      v3_muladds( walk_3d, right_dir, walk[0], walk_3d );
+
+      float current_vel = fabsf(v3_dot( walk_3d, phys->rb.v )),
+            new_vel     = current_vel + VG_TIMESTEP_FIXED*k_air_accelerate,
+            clamped_new = vg_clampf( new_vel, 0.0f, k_walkspeed ),
+            vel_diff    = vg_maxf( 0.0f, clamped_new - current_vel );
+
+      v3_muladds( phys->rb.v, right_dir,   walk[0] * vel_diff, phys->rb.v );
+      v3_muladds( phys->rb.v, forward_dir, walk[1] * vel_diff, phys->rb.v );
+
+
       len = 0;
       len += rb_sphere_scene( rbf, &world.rb_geo, manifold+len );
       len += rb_sphere_scene( rbb, &world.rb_geo, manifold+len );
@@ -396,15 +416,12 @@ VG_STATIC void player_walk_physics(void)
    }
    else
    {
-      /* translate player */
-      v2f walk = { player.input_walkh->axis.value,
-                   player.input_walkv->axis.value };
-      
-      if( v2_length2(walk) > 0.001f )
-         v2_normalize( walk );
+      player.walk = v2_length( walk );
+
+      if( player.input_walk->button.value )
+         v2_muls( walk, 0.5f, walk );
 
-      v2_muls( walk, vg_maxf( player.input_push->axis.value, 0.5f ) *
-                        k_walkspeed * VG_TIMESTEP_FIXED, walk );
+      v2_muls( walk, k_walkspeed * VG_TIMESTEP_FIXED, walk );
 
       v3f walk_apply;
       v3_zero( walk_apply );
@@ -533,6 +550,27 @@ VG_STATIC void player_physics(void)
    len += rb_sphere_scene( rbf, &world.rb_geo, manifold+len );
    len += rb_sphere_scene( rbb, &world.rb_geo, manifold+len );
 
+   /* 
+    * Preprocess collision points, and create a surface picture.
+    * we want contacts that are within our 'capsule's internal line to be 
+    * clamped so that they face the line and do not oppose, to stop the
+    * player hanging up on stuff
+    */
+   for( int i=0; i<len; i++ )
+   {
+      v3f dfront, dback;
+      v3_sub( manifold[i].co, rbf->co, dfront );
+      v3_sub( manifold[i].co, rbb->co, dback );
+
+      if( (v3_dot( dfront, phys->rb.forward ) < -0.02f) &&
+          (v3_dot( dback,  phys->rb.forward ) > 0.02f))
+      {
+         float p = v3_dot( manifold[i].n, phys->rb.forward );
+         v3_muladds( manifold[i].n, phys->rb.forward, -p, manifold[i].n );
+         v3_normalize( manifold[i].n );
+      }
+   }
+
    rb_presolve_contacts( manifold, len );
    v3f surface_avg = {0.0f, 0.0f, 0.0f};
 
@@ -829,6 +867,9 @@ VG_STATIC void player_freecam(void)
    m3x3_mulv( camera_mtx, sidedir, sidedir );
    
    static v3f move_vel = { 0.0f, 0.0f, 0.0f };
+
+   /* TODO */
+#if 0
    if( vg_get_button( "forward" ) )
       v3_muladds( move_vel, lookdir, VG_TIMESTEP_FIXED * movespeed, move_vel );
    if( vg_get_button( "back" ) )
@@ -837,6 +878,7 @@ VG_STATIC void player_freecam(void)
       v3_muladds( move_vel, sidedir, VG_TIMESTEP_FIXED *-movespeed, move_vel );
    if( vg_get_button( "right" ) )
       v3_muladds( move_vel, sidedir, VG_TIMESTEP_FIXED * movespeed, move_vel );
+#endif
 
    v3_muls( move_vel, 0.7f, move_vel );
    v3_add( move_vel, player.camera_pos, player.camera_pos );