+ 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;