+/*
+ * Cast a sphere from a to b and see what time it hits
+ */
+VG_STATIC int spherecast_world( v3f pa, v3f pb, float r, float *t, v3f n )
+{
+ struct player_phys *phys = &player.phys;
+
+ bh_iter it;
+ bh_iter_init( 0, &it );
+
+ boxf region;
+ box_init_inf( region );
+ box_addpt( region, pa );
+ box_addpt( region, pb );
+
+ v3_add( (v3f){ r, r, r}, region[1], region[1] );
+ v3_add( (v3f){-r,-r,-r}, region[0], region[0] );
+
+ v3f dir;
+ v3_sub( pb, pa, dir );
+
+ v3f dir_inv;
+ dir_inv[0] = 1.0f/dir[0];
+ dir_inv[1] = 1.0f/dir[1];
+ dir_inv[2] = 1.0f/dir[2];
+
+ int hit = -1;
+ float min_t = 1.0f;
+
+ int idx;
+ while( bh_next( world.geo_bh, &it, region, &idx ) )
+ {
+ u32 *ptri = &world.scene_geo->arrindices[ idx*3 ];
+ v3f tri[3];
+
+ boxf box;
+ box_init_inf( box );
+
+ for( int j=0; j<3; j++ )
+ {
+ v3_copy( world.scene_geo->arrvertices[ptri[j]].co, tri[j] );
+ box_addpt( box, tri[j] );
+ }
+
+ v3_add( (v3f){ r, r, r}, box[1], box[1] );
+ v3_add( (v3f){-r,-r,-r}, box[0], box[0] );
+
+ if( !ray_aabb1( box, pa, dir_inv, 1.0f ) )
+ continue;
+
+ float t;
+ v3f n1;
+ if( spherecast_triangle( tri, pa, dir, r, &t, n1 ) )
+ {
+ if( t < min_t )
+ {
+ min_t = t;
+ hit = idx;
+ v3_copy( n1, n );
+ }
+ }
+ }
+
+ *t = min_t;
+ return hit;
+}
+
+/*
+ * Trace a path given a velocity rotation.
+ * Closest to 0 is best.
+ */
+VG_STATIC void player_predict_land( m3x3f vr,
+ struct land_prediction *prediction )
+{
+ struct player_phys *phys = &player.phys;
+
+ float pstep = VG_TIMESTEP_FIXED * 10.0f;
+ float k_bias = 0.96f;
+
+ v3f pco, pco1, pv;
+ v3_copy( phys->rb.co, pco );
+ v3_muls( phys->rb.v, k_bias, pv );
+
+ m3x3_mulv( vr, pv, pv );
+ v3_muladds( pco, pv, pstep, pco );
+
+ struct grind_edge *best_grind = NULL;
+ float closest_grind = INFINITY;
+
+ float grind_score = INFINITY,
+ air_score = INFINITY;
+
+ prediction->log_length = 0;
+
+ for( int i=0; i<vg_list_size(prediction->log); i++ )
+ {
+ v3_copy( pco, pco1 );
+ apply_gravity( pv, pstep );
+
+ m3x3_mulv( vr, pv, pv );
+ v3_muladds( pco, pv, pstep, pco );
+
+ v3f vdir;
+
+ v3_sub( pco, pco1, vdir );
+
+ float l = v3_length( vdir );
+ v3_muls( vdir, 1.0f/l, vdir );
+
+ v3f c0, c1;
+ struct grind_edge *ge = player_grind_collect_edge( pco, pco1,
+ c0, c1, 0.4f );
+
+ if( ge && (v3_dot((v3f){0.0f,1.0f,0.0f},vdir) < -0.2f ) )
+ {
+ float d2 = v3_dist2( c0, c1 );
+ if( d2 < closest_grind )
+ {
+ closest_grind = d2;
+ best_grind = ge;
+ grind_score = closest_grind * 0.05f;
+ }
+ }
+
+ v3f n1;
+
+ float t1;
+ int idx = spherecast_world( pco1, pco, 0.4f, &t1, n1 );
+ if( idx != -1 )
+ {
+ v3_copy( n1, prediction->n );
+ air_score = -v3_dot( pv, n1 );
+
+ u32 vert_index = world.scene_geo->arrindices[ idx*3 ];
+ struct world_material *mat = world_tri_index_material( vert_index );
+
+ /* Bias prediction towords ramps */
+ if( mat->info.flags & k_material_flag_skate_surface )
+ air_score *= 0.1f;
+
+ v3_lerp( pco1, pco, t1, prediction->log[ prediction->log_length ++ ] );
+ break;
+ }
+
+ v3_copy( pco, prediction->log[ prediction->log_length ++ ] );
+ }
+
+ if( grind_score < air_score )
+ {
+ prediction->score = grind_score;
+ prediction->type = k_prediction_grind;
+ }
+ else if( air_score < INFINITY )
+ {
+ prediction->score = air_score;
+ prediction->type = k_prediction_land;
+ }
+ else
+ {
+ prediction->score = INFINITY;
+ prediction->type = k_prediction_none;
+ }
+}
+