+VG_STATIC void skate_adjust_up_direction( player_instance *player ){
+ struct player_skate *s = &player->_skate;
+
+ if( s->state.activity == k_skate_activity_ground ){
+ v3f target;
+ v3_copy( s->surface_picture, target );
+
+ target[1] += 2.0f * s->surface_picture[1];
+ v3_normalize( target );
+
+ v3_lerp( s->state.up_dir, target,
+ 8.0f * s->substep_delta, s->state.up_dir );
+ }
+ else if( s->state.activity <= k_skate_activity_air_to_grind ){
+ v3_lerp( s->state.up_dir, player->rb.to_world[1],
+ 8.0f * s->substep_delta, s->state.up_dir );
+ }
+ else{
+ v3_lerp( s->state.up_dir, player->basis[1],
+ 12.0f * s->substep_delta, s->state.up_dir );
+ }
+}
+
+VG_STATIC int skate_point_visible( v3f origin, v3f target ){
+ v3f dir;
+ v3_sub( target, origin, dir );
+
+ ray_hit ray;
+ ray.dist = v3_length( dir );
+ v3_muls( dir, 1.0f/ray.dist, dir );
+ ray.dist -= 0.025f;
+
+ if( ray_world( world_current_instance(), origin, dir, &ray,
+ k_material_flag_walking ) )
+ return 0;
+
+ return 1;
+}
+
+VG_STATIC void skate_grind_orient( struct grind_info *inf, m3x3f mtx ){
+ v3_copy( inf->dir, mtx[0] );
+ v3_copy( inf->n, mtx[1] );
+ v3_cross( mtx[0], mtx[1], mtx[2] );
+}
+
+VG_STATIC void skate_grind_friction( player_instance *player,
+ struct grind_info *inf, float strength ){
+ v3f v2;
+ v3_muladds( player->rb.to_world[2], inf->n,
+ -v3_dot( player->rb.to_world[2], inf->n ), v2 );
+
+ float a = 1.0f-fabsf( v3_dot( v2, inf->dir ) ),
+ dir = vg_signf( v3_dot( player->rb.v, inf->dir ) ),
+ F = a * -dir * k_grind_max_friction;
+
+ v3_muladds( player->rb.v, inf->dir, F*k_rb_delta*strength, player->rb.v );
+}
+
+VG_STATIC void skate_grind_decay( player_instance *player,
+ struct grind_info *inf, float strength ){
+ m3x3f mtx, mtx_inv;
+ skate_grind_orient( inf, mtx );
+ m3x3_transpose( mtx, mtx_inv );
+
+ v3f v_grind;
+ m3x3_mulv( mtx_inv, player->rb.v, v_grind );
+
+ float decay = 1.0f - ( k_rb_delta * k_grind_decayxy * strength );
+ v3_mul( v_grind, (v3f){ 1.0f, decay, decay }, v_grind );
+ m3x3_mulv( mtx, v_grind, player->rb.v );
+}
+
+VG_STATIC void skate_grind_truck_apply( player_instance *player,
+ float sign, struct grind_info *inf,
+ float strength ){
+ struct player_skate *s = &player->_skate;
+
+ /* REFACTOR */
+ v3f ra = { 0.0f, -k_board_radius, sign * k_board_length };
+ v3f raw, wsp;
+ m3x3_mulv( player->rb.to_world, ra, raw );
+ v3_add( player->rb.co, raw, wsp );
+
+ v3_copy( ra, s->weight_distribution );
+
+ v3f delta;
+ v3_sub( inf->co, wsp, delta );
+
+ /* spring force */
+ v3_muladds( player->rb.v, delta, k_spring_force*strength*k_rb_delta,
+ player->rb.v );
+
+ skate_grind_decay( player, inf, strength );
+ skate_grind_friction( player, inf, strength );
+
+ /* yeah yeah yeah yeah */
+ v3f raw_nplane, axis;
+ v3_muladds( raw, inf->n, -v3_dot( inf->n, raw ), raw_nplane );
+ v3_cross( raw_nplane, inf->n, axis );
+ v3_normalize( axis );
+
+ /* orientation */
+ m3x3f mtx;
+ skate_grind_orient( inf, mtx );
+ v3f target_fwd, fwd, up, target_up;
+ m3x3_mulv( mtx, s->grind_vec, target_fwd );
+ v3_copy( raw_nplane, fwd );
+ v3_copy( player->rb.to_world[1], up );
+ v3_copy( inf->n, target_up );
+
+ v3_muladds( target_fwd, inf->n, -v3_dot(inf->n,target_fwd), target_fwd );
+ v3_muladds( fwd, inf->n, -v3_dot(inf->n,fwd), fwd );
+
+ v3_normalize( target_fwd );
+ v3_normalize( fwd );
+
+ v2f steer;
+ joystick_state( k_srjoystick_steer, steer );
+
+ float way = steer[1] * vg_signf( v3_dot( raw_nplane, player->rb.v ) );
+
+ v4f q;
+ q_axis_angle( q, axis, VG_PIf*0.125f * way );
+ q_mulv( q, target_up, target_up );
+ q_mulv( q, target_fwd, target_fwd );
+
+ rb_effect_spring_target_vector( &player->rb, up, target_up,
+ k_grind_spring,
+ k_grind_dampener,
+ k_rb_delta );
+
+ rb_effect_spring_target_vector( &player->rb, fwd, target_fwd,
+ k_grind_spring*strength,
+ k_grind_dampener*strength,
+ k_rb_delta );
+
+ vg_line_arrow( player->rb.co, target_up, 1.0f, VG__GREEN );
+ vg_line_arrow( player->rb.co, fwd, 0.8f, VG__RED );
+ vg_line_arrow( player->rb.co, target_fwd, 1.0f, VG__YELOW );
+
+ s->grind_strength = strength;
+
+ /* Fake contact */
+ struct grind_limit *limit = &s->limits[ s->limit_count ++ ];
+ m4x3_mulv( player->rb.to_local, wsp, limit->ra );
+ m3x3_mulv( player->rb.to_local, inf->n, limit->n );
+ limit->p = 0.0f;
+
+ v3_copy( inf->dir, s->grind_dir );
+}
+
+VG_STATIC void skate_5050_apply( player_instance *player,
+ struct grind_info *inf_front,
+ struct grind_info *inf_back )