+static float player_xyspeed2(void){
+ return v3_length2( (v3f){localplayer.rb.v[0], 0.0f, localplayer.rb.v[2]} );
+}
+
+static void player_walk_generic_to_skate( enum skate_activity init, f32 yaw ){
+ localplayer.subsystem = k_player_subsystem_skate;
+
+ v3f v;
+
+ if( player_xyspeed2() < 0.1f * 0.1f )
+ q_mulv( localplayer.rb.q, (v3f){0.0f,0.0f,1.6f}, v );
+ else
+ v3_copy( localplayer.rb.v, v );
+
+ player_skate.state.activity_prev = k_skate_activity_ground;
+ player_skate.state.activity = init;
+
+ v3f dir;
+ v3_copy( v, dir );
+ v3_normalize( dir );
+
+ q_axis_angle( localplayer.rb.q, (v3f){0.0f,1.0f,0.0f},
+ atan2f(-dir[0],-dir[2]) );
+ q_normalize( localplayer.rb.q );
+
+ q_mulv( localplayer.rb.q, (v3f){0.0f,1.0f,0.0f}, player_skate.state.cog );
+ v3_add( player_skate.state.cog, localplayer.rb.co, player_skate.state.cog );
+
+ v3_copy( v, player_skate.state.cog_v );
+ v3_copy( v, localplayer.rb.v );
+
+ player__begin_holdout( (v3f){0.0f,0.0f,0.0f} );
+ player__skate_reset_animator();
+ player__skate_clear_mechanics();
+ rb_update_matrices( &localplayer.rb );
+ v3_copy( (v3f){yaw,0.0f,0.0f}, player_skate.state.trick_euler );
+
+ if( init == k_skate_activity_air )
+ player__approximate_best_trajectory();
+}
+
+static void player_walk_drop_in_to_skate(void){
+ localplayer.immobile = 0;
+ localplayer.subsystem = k_player_subsystem_skate;
+
+ player_skate.state.activity_prev = k_skate_activity_ground;
+ player_skate.state.activity = k_skate_activity_ground;
+
+ player__begin_holdout( (v3f){0,0,0} );
+ player__skate_clear_mechanics();
+ player__skate_reset_animator();
+
+ v3f init_velocity;
+ player_walk_drop_in_vector( init_velocity );
+
+ rb_update_matrices( &localplayer.rb );
+ v3_muladds( localplayer.rb.co, localplayer.rb.to_world[1], 1.0f,
+ player_skate.state.cog );
+ v3_copy( init_velocity, player_skate.state.cog_v );
+ v3_copy( init_velocity, localplayer.rb.v );
+ v3_copy( init_velocity, localplayer.cam_control.cam_velocity_smooth );
+ v3_copy( (v3f){player_walk.animator.board_yaw+1.0f,0,0},
+ player_skate.state.trick_euler );
+}
+
+static void player_walk_drop_in_overhang_transform( f32 t, v3f co, v4f q ){
+ v3f axis;
+ v3_cross( (v3f){0,1,0}, player_walk.state.drop_in_normal, axis );
+ v3_normalize( axis );
+
+ float a = acosf( player_walk.state.drop_in_normal[1] ) * t;
+ q_axis_angle( q, axis, a );
+
+ float l = t * 0.5f,
+ heading_angle = player_walk.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( player_walk.state.drop_in_target, overhang, co );
+}
+
+static int player_walk_scan_for_drop_in(void){
+ world_instance *world = world_current_instance();
+
+ v3f dir, center;
+ q_mulv( localplayer.rb.q, (v3f){0.0f,0.0f,1.0f}, dir );
+ v3_muladds( localplayer.rb.co, localplayer.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 ( localplayer.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, 0 ) ){
+ vg_line( pos, ray->pos, VG__RED );
+ vg_line_point( 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( localplayer.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, localplayer.rb.co );
+
+ v3f edge;
+ if( plane_intersect3( pa, pb, pc, edge ) ){
+ v3_copy( edge, player_walk.state.drop_in_target );
+ v3_copy( s1->normal, player_walk.state.drop_in_normal );
+ v3_copy( localplayer.rb.co, player_walk.state.drop_in_start );
+
+ player_walk.state.drop_in_start_angle = player_get_heading_yaw();
+ player_walk.state.drop_in_angle =
+ atan2f( player_walk.state.drop_in_normal[0],
+ player_walk.state.drop_in_normal[2] );
+
+ /* TODO: scan multiple of these? */
+ v3f oco;
+ v4f oq;
+ player_walk_drop_in_overhang_transform( 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, 0 ) ){
+ vg_line( va, vb, VG__RED );
+ vg_line_point( 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, 0 ) ){
+ vg_line( va, vb, VG__RED );
+ vg_line_point( ray.pos, 0.1f, VG__RED );
+ vg_error( "invalidated\n" );
+ return 0;
+ }
+
+ player_walk_drop_in_vector( localplayer.rb.v );
+ return 1;
+ }
+ else{
+ vg_error( "failed to find intersection of drop in\n" );