refactor
[carveJwlIkooP6JGAAIwe30JlM.git] / player_physics.h
index 20ce7f5d2877aca286dd45a70d0ea0fe6a836356..e396e0031d3771cb937a5955b73c5f13bbed2de3 100644 (file)
@@ -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,13 +416,6 @@ 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_clamp( walk );
-
       player.walk = v2_length( walk );
 
       if( player.input_walk->button.value )
@@ -443,6 +456,12 @@ VG_STATIC void player_walk_physics(void)
       }
       v3_add( dt, phys->rb.co, phys->rb.co );
 
+      if( len )
+      {
+         struct world_material *surface_mat = world_contact_material(manifold);
+         player.surface_prop = surface_mat->info.surface_prop;
+      }
+
       /* jump */
       if( player.input_jump->button.value )
       {
@@ -537,6 +556,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};