+ 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 );
+}
+
+VG_STATIC int player_update_grind_collision( rb_ct *contact )
+{
+ struct player_phys *phys = &player.phys;
+
+ bh_iter it;
+ bh_iter_init( 0, &it );
+
+ boxf region;
+
+ v3f p0, p1, c0, c1;
+ v3_muladds( phys->rb.co, phys->rb.forward, 0.5f, p0 );
+ v3_muladds( phys->rb.co, phys->rb.forward, -0.5f, p1 );
+ v3_muladds( p0, phys->rb.up, 0.125f, p0 );
+ v3_muladds( p1, phys->rb.up, 0.125f, p1 );
+
+ box_init_inf( region );
+ box_addpt( region, p0 );
+ box_addpt( region, p1 );
+
+ float const k_r = 0.25f;
+ v3_add( (v3f){ k_r, k_r, k_r}, region[1], region[1] );
+ v3_add( (v3f){-k_r,-k_r,-k_r}, region[0], region[0] );
+
+ vg_line( p0, p1, 0xff0000ff );
+ vg_line_boxf( region, 0xff0000ff );
+
+ float closest = k_r*k_r;
+ struct grind_edge *closest_edge = NULL;
+
+ int idx;
+ while( bh_next( world.grind_bh, &it, region, &idx ) )
+ {
+ struct grind_edge *edge = &world.grind_edges[ idx ];
+
+ float s,t;
+ v3f pa, pb;
+
+ float d2 =
+ closest_segment_segment( p0, p1, edge->p0, edge->p1, &s,&t, pa, pb );
+
+ if( d2 < closest )
+ {
+ closest = d2;
+ closest_edge = edge;
+ v3_copy( pa, c0 );
+ v3_copy( pb, c1 );
+ }
+ }
+
+ if( closest_edge )
+ {
+ vg_line_cross( c0, 0xff000000, 0.1f );
+ vg_line_cross( c1, 0xff000000, 0.1f );
+ vg_line( c0, c1, 0xff000000 );
+
+ v3f delta;
+ v3_sub( c1, c0, delta );
+
+ if( v3_dot( delta, phys->rb.up ) > 0.0f )
+ {
+ v3_copy( delta, contact->n );
+ float l = v3_length( contact->n );
+ v3_muls( contact->n, 1.0f/l, contact->n );
+ contact->p = l;
+ contact->type = k_contact_type_edge;
+ contact->element_id = 0;
+ v3_copy( c1, contact->co );
+ contact->rba = &player.phys.rb;
+ contact->rbb = &world.rb_geo;
+
+ v3f edge_dir, axis_dir;
+ v3_sub( closest_edge->p1, closest_edge->p0, edge_dir );
+ v3_normalize( edge_dir );
+ v3_cross( (v3f){0.0f,1.0f,0.0f}, edge_dir, axis_dir );
+ v3_cross( edge_dir, axis_dir, contact->n );
+
+ return 1;
+ }
+ else
+ return -1;
+ }
+
+ return 0;
+
+#if 0
+ v3f closest;
+ int idx = bh_closest_point( world.grind_bh, phys->rb.co, closest, INFINITY );
+ if( idx != -1 )
+ {
+ 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 );
+
+ v3f delta;
+ v3_sub( closest, phys->rb.co, delta );
+
+ if( v3_length2(delta) < 0.5f*0.5f )
+ {
+ v3_copy( closest, phys->rb.co );
+
+ v3f line_dir;
+ v3_sub( edge->p1, edge->p0, line_dir );
+ v3_normalize( line_dir );
+
+ float p = v3_dot( phys->rb.v, line_dir );
+ v3_muls( line_dir, p, phys->rb.v );
+ phys->grind = 1;
+ }
+ else
+ phys->grind = 0;
+ }
+#endif
+}
+
+/* Manifold must be able to hold at least 64 elements */
+VG_STATIC int player_update_collision_manifold( rb_ct *manifold )
+{
+ struct player_phys *phys = &player.phys;
+