+VG_STATIC void player_walk_drop_in_vector( player_instance *player, v3f vec )
+{
+ struct player_walk *w = &player->_walk;
+
+ v3f axis, init_dir;
+ v3_cross( player->basis[1], w->state.drop_in_normal, axis );
+ v3_cross( axis, w->state.drop_in_normal, init_dir );
+ v3_normalize( init_dir );
+ v3_muls( init_dir, 7.0f, vec );
+}
+
+VG_STATIC float player_xyspeed2( player_instance *player )
+{
+ v3f xy;
+ v3_muladds( player->rb.v, player->basis[1],
+ -v3_dot( player->basis[1], player->rb.v ), xy );
+
+ return v3_length2(xy);
+}
+
+VG_STATIC void player_walk_generic_to_skate( player_instance *player,
+ enum skate_activity init,
+ float yaw )
+{
+ player->subsystem = k_player_subsystem_skate;
+
+ struct player_walk *w = &player->_walk;
+ struct player_skate *s = &player->_skate;
+
+ v3f v;
+
+ if( player_xyspeed2(player) < 0.1f * 0.1f )
+ q_mulv( player->rb.q, (v3f){0.0f,0.0f,1.6f}, v );
+ else
+ v3_copy( player->rb.v, v );
+
+ s->state.activity_prev = k_skate_activity_ground;
+ s->state.activity = init;
+
+ v3f dir;
+ v3_copy( v, dir );
+ v3_normalize( dir );
+ m3x3_mulv( player->invbasis, dir, dir );
+
+ q_axis_angle( player->rb.q, (v3f){0.0f,1.0f,0.0f}, atan2f(-dir[0],-dir[2]) );
+ q_mul( player->qbasis, player->rb.q, player->rb.q );
+ q_normalize( player->rb.q );
+
+ q_mulv( player->rb.q, (v3f){0.0f,1.0f,0.0f}, s->state.cog );
+ v3_add( s->state.cog, player->rb.co, s->state.cog );
+
+ v3_copy( v, s->state.cog_v );
+ v3_copy( v, player->rb.v );
+
+ player__skate_reset_animator( player );
+ player__skate_clear_mechanics( player );
+ rb_update_transform( &player->rb );
+ v3_copy( (v3f){yaw,0.0f,0.0f}, s->state.trick_euler );
+
+ if( init == k_skate_activity_air )
+ player__approximate_best_trajectory( player );
+}
+
+VG_STATIC void player_walk_drop_in_to_skate( player_instance *player )
+{
+ player->subsystem = k_player_subsystem_skate;
+
+ struct player_walk *w = &player->_walk;
+ struct player_skate *s = &player->_skate;
+ s->state.activity_prev = k_skate_activity_ground;
+ s->state.activity = k_skate_activity_ground;
+
+ player__skate_clear_mechanics( player );
+ player__skate_reset_animator( player );
+
+ v3f init_velocity;
+ player_walk_drop_in_vector( player, init_velocity );
+
+ rb_update_transform( &player->rb );
+ v3_muladds( player->rb.co, player->rb.to_world[1], 1.0f, s->state.cog );
+ v3_copy( init_velocity, s->state.cog_v );
+ v3_copy( init_velocity, player->rb.v );
+ v3_copy( init_velocity, player->cam_velocity_smooth );
+ v3_copy( (v3f){1.0f,0.0f,0.0f}, s->state.trick_euler );
+}
+
+VG_STATIC void player_walk_drop_in_overhang_transform( player_instance *player,
+ float t,
+ v3f co, v4f q )
+{
+ struct player_walk *w = &player->_walk;
+
+ v3f axis;
+ v3_cross( player->basis[1], w->state.drop_in_normal, axis );
+ v3_normalize( axis );
+
+ float a = acosf( w->state.drop_in_normal[1] ) * t;
+
+ q_axis_angle( q, axis, a );
+
+ float l = t * 0.5f,
+ heading_angle = w->state.drop_in_angle;
+
+ v3f overhang;
+ overhang[0] = sinf( heading_angle ) * l;
+ overhang[1] = 0.28f * l;
+ overhang[2] = cosf( heading_angle ) * l;
+
+ q_mulv( q, overhang, overhang );
+ v3_add( w->state.drop_in_target, overhang, co );
+}
+
+VG_STATIC int player_walk_scan_for_drop_in( player_instance *player )
+{
+ struct player_walk *w = &player->_walk;
+ world_instance *world = get_active_world();
+
+ v3f dir, center;
+ q_mulv( player->rb.q, (v3f){0.0f,0.0f,1.0f}, dir );
+ v3_muladds( player->rb.co, player->rb.to_world[1], -1.0f, center );
+
+ ray_hit samples[20];
+ int sample_count = 0;
+
+ for( int i=0; i<20; i ++ )
+ {
+ float t = (float)i * (1.0f/19.0f),
+ s = sinf( t * VG_PIf * 0.25f ),
+ c = cosf( t * VG_PIf * 0.25f );
+
+ v3f ray_dir, pos;
+ v3_muls ( player->rb.to_world[1], -c, ray_dir );
+ v3_muladds( ray_dir, dir, -s, ray_dir );
+ v3_muladds( center, ray_dir, -2.0f, pos );
+
+ ray_hit *ray = &samples[ sample_count ];
+ ray->dist = 2.0f;
+
+ if( ray_world( world, pos, ray_dir, ray ) )
+ {
+ vg_line( pos, ray->pos, VG__RED );
+ vg_line_pt3( ray->pos, 0.025f, VG__BLACK );
+
+ sample_count ++;
+ }
+ }
+
+ float min_a = 0.70710678118654752f;
+ ray_hit *candidate = NULL;
+
+ if( sample_count >= 2 )
+ {
+ for( int i=0; i<sample_count-1; i++ )
+ {
+ ray_hit *s0 = &samples[i],
+ *s1 = &samples[i+1];
+
+ float a = v3_dot( s0->normal, s1->normal );
+
+ if( (a < min_a) && (a >= -0.1f) && (s0->normal[1]>s1->normal[1]) )
+ {
+ min_a = a;
+ candidate = s0;
+ }
+ }
+ }
+
+ if( candidate )
+ {
+ v4f pa, pb, pc;
+
+ ray_hit *s0 = candidate,
+ *s1 = candidate+1;
+
+ vg_line( s0->pos, s1->pos, VG__WHITE );
+
+ v3_copy( s0->normal, pa );
+ v3_copy( s1->normal, pb );
+ v3_cross( player->rb.to_world[1], dir, pc );
+ v3_normalize( pc );
+
+ pa[3] = v3_dot( pa, s0->pos );
+ pb[3] = v3_dot( pb, s1->pos );
+ pc[3] = v3_dot( pc, player->rb.co );
+
+ v3f edge;
+ if( plane_intersect3( pa, pb, pc, edge ) )
+ {
+ v3_copy( edge, w->state.drop_in_target );
+ v3_copy( s1->normal, w->state.drop_in_normal );
+ v3_copy( player->rb.co, w->state.drop_in_start );
+
+ w->state.drop_in_start_angle = player_get_heading_yaw( player );
+ w->state.drop_in_angle = atan2f( w->state.drop_in_normal[0],
+ w->state.drop_in_normal[2] );
+
+ /* TODO: scan multiple of these? */
+ v3f oco;
+ v4f oq;
+ player_walk_drop_in_overhang_transform( player, 1.0f, oco, oq );
+
+ v3f va = {0.0f,0.0f,-k_board_length - 0.3f},
+ vb = {0.0f,0.0f, k_board_length + 0.3f};
+
+ q_mulv( oq, va, va );
+ q_mulv( oq, vb, vb );
+ v3_add( oco, va, va );
+ v3_add( oco, vb, vb );
+
+ v3f v0;
+ v3_sub( vb, va, v0 );
+ v3_normalize( v0 );
+
+ ray_hit ray;
+ ray.dist = k_board_length*2.0f + 0.6f;
+
+ if( ray_world( world, va, v0, &ray ) )
+ {
+ vg_line( va, vb, VG__RED );
+ vg_line_pt3( ray.pos, 0.1f, VG__RED );
+ vg_error( "invalidated\n" );
+ return 0;
+ }
+
+ v3_muls( v0, -1.0f, v0 );
+ if( ray_world( world, vb, v0, &ray ) )
+ {
+ vg_line( va, vb, VG__RED );
+ vg_line_pt3( ray.pos, 0.1f, VG__RED );
+ vg_error( "invalidated\n" );
+ return 0;
+ }
+
+ player_walk_drop_in_vector( player, player->rb.v );
+ return 1;
+ }
+ else
+ {
+ vg_error( "failed to find intersection of drop in\n" );
+ }
+ }
+
+ return 0;
+}
+