assorted crap
[carveJwlIkooP6JGAAIwe30JlM.git] / player_physics.h
index 20ce7f5d2877aca286dd45a70d0ea0fe6a836356..41adc1e4150787e8ee13d021a54d8da3dede0e09 100644 (file)
@@ -137,12 +137,7 @@ VG_STATIC void player_physics_control(void)
    phys->reverse = -vg_signf(vel[2]);
 
    float substep = VG_TIMESTEP_FIXED * 0.2f;
-
-#if 0
-   float fwd_resistance = vg_get_button( "break" )? 5.0f: k_friction_resistance;
-#else
    float fwd_resistance = k_friction_resistance;
-#endif
 
    for( int i=0; i<5; i++ )
    {
@@ -183,18 +178,6 @@ VG_STATIC void player_physics_control(void)
       vel[2] -= new_vel * phys->reverse;
    }
 
-   /* Pumping */
-   static float previous = 0.0f;
-   float delta = previous - phys->grab,
-          pump = delta * k_pump_force * VG_TIMESTEP_FIXED;
-   previous = phys->grab;
-
-   v3f p1;
-   v3_muladds( phys->rb.co, phys->rb.up, pump, p1 );
-   vg_line( phys->rb.co, p1, 0xff0000ff );
-
-   vel[1] += pump;
-
    m3x3_mulv( phys->rb.to_world, vel, phys->rb.v );
    
    float input = player.input_js1h->axis.value,
@@ -339,12 +322,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 +399,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 +439,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 )
       {
@@ -498,6 +500,43 @@ VG_STATIC void player_walk_physics(void)
    }
 }
 
+VG_STATIC void player_grind(void)
+{
+   struct player_phys *phys = &player.phys;
+
+   v3f closest;
+   int idx = bh_closest_point( world.grind_bh, phys->rb.co, closest, INFINITY );
+   if( idx == -1 )
+      return;
+
+   struct grind_edge *edge = &world.grind_edges[ idx ];
+
+   vg_line( phys->rb.co, closest, 0xff000000 );
+   vg_line_cross( closest, 0xff000000, 0.3f );
+   vg_line( edge->p0, edge->p1, 0xff000000 );
+
+   return;
+
+   idx = bh_closest_point( world.geo_bh, phys->rb.co, closest, INFINITY );
+   vg_line( phys->rb.co, closest, 0xff000000 );
+   vg_line_cross( closest, 0xff000000, 0.3f );
+   
+   idx = world.scene_geo->arrindices[ idx * 3 ];
+   struct world_material *mat = world_tri_index_material( idx );
+
+   if( mat->info.flags & k_material_flag_grind_surface )
+   {
+      v3f grind_delta;
+      v3_sub( closest, phys->rb.co, grind_delta );
+      
+      float p = v3_dot( phys->rb.forward, grind_delta );
+      v3_muladds( grind_delta, phys->rb.forward, -p, grind_delta );
+      
+      float a = vg_maxf( 0.0f, 4.0f-v3_dist2( closest, phys->rb.co ) );
+      v3_muladds( phys->rb.v, grind_delta, a*0.2f, phys->rb.v );
+   }
+}
+
 /*
  * Physics collision detection, and control
  */
@@ -532,10 +571,58 @@ VG_STATIC void player_physics(void)
    rb_debug( rbb, 0xffffff00 );
 
    rb_ct manifold[64];
-   int len = 0;
+   int len_f = 0,
+       len_b = 0; 
 
-   len += rb_sphere_scene( rbf, &world.rb_geo, manifold+len );
-   len += rb_sphere_scene( rbb, &world.rb_geo, manifold+len );
+   len_f = rb_sphere_scene( rbf, &world.rb_geo, manifold );
+   rb_manifold_filter_coplanar( manifold, len_f, 0.05f );
+   if( len_f > 1 )
+   {
+      rb_manifold_filter_backface( manifold, len_f );
+      rb_manifold_filter_joint_edges( manifold, len_f, 0.05f );
+      rb_manifold_filter_pairs( manifold, len_f, 0.05f );
+   }
+   len_f = rb_manifold_apply_filtered( manifold, len_f );
+   
+   rb_ct *man_b = &manifold[len_f];
+   len_b = rb_sphere_scene( rbb, &world.rb_geo, man_b );
+   rb_manifold_filter_coplanar( man_b, len_b, 0.05f );
+   if( len_b > 1 )
+   {
+      rb_manifold_filter_backface( man_b, len_b );
+      rb_manifold_filter_joint_edges( man_b, len_b, 0.05f );
+      rb_manifold_filter_pairs( man_b, len_b, 0.05f );
+   }
+   len_b = rb_manifold_apply_filtered( man_b, len_b );
+
+   int len = len_f+len_b;
+
+   player_grind();
+
+   boxf bax;
+   v3_sub( phys->rb.co, (v3f){2.0f,2.0f,2.0f}, bax[0] );
+   v3_add( phys->rb.co, (v3f){2.0f,2.0f,2.0f}, bax[1] );
+
+   /* 
+    * 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};
@@ -626,8 +713,6 @@ VG_STATIC void player_physics(void)
 
    if( !phys->in_air )
    {
-      /* 20/10/22: make this only go axisways instead, may effect velocities. */
-
       v3f projected, axis;
 
       float d = v3_dot( phys->rb.forward, surface_avg );