+ 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;
+}
+
+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] );
+}
+
+static void skate_grind_friction( struct grind_info *inf, float strength ){
+ v3f v2;
+ v3_muladds( localplayer.rb.to_world[2], inf->n,
+ -v3_dot( localplayer.rb.to_world[2], inf->n ), v2 );
+
+ float a = 1.0f-fabsf( v3_dot( v2, inf->dir ) ),
+ dir = vg_signf( v3_dot( localplayer.rb.v, inf->dir ) ),
+ F = a * -dir * k_grind_max_friction;
+
+ v3_muladds( localplayer.rb.v, inf->dir, F*k_rb_delta*strength,
+ localplayer.rb.v );
+}
+
+static void skate_grind_decay( 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, localplayer.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, localplayer.rb.v );
+}
+
+static void skate_grind_truck_apply( float sign, struct grind_info *inf,
+ float strength ){
+ struct player_skate_state *state = &player_skate.state;
+
+ /* REFACTOR */
+ v3f ra = { 0.0f, -k_board_radius, sign * k_board_length };
+ v3f raw, wsp;
+ m3x3_mulv( localplayer.rb.to_world, ra, raw );
+ v3_add( localplayer.rb.co, raw, wsp );
+
+ v3_copy( ra, player_skate.weight_distribution );
+
+ v3f delta;
+ v3_sub( inf->co, wsp, delta );
+
+ /* spring force */
+ v3_muladds( localplayer.rb.v, delta, k_spring_force*strength*k_rb_delta,
+ localplayer.rb.v );
+
+ skate_grind_decay( inf, strength );
+ skate_grind_friction( 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, player_skate.grind_vec, target_fwd );
+ v3_copy( raw_nplane, fwd );
+ v3_copy( localplayer.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, localplayer.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( &localplayer.rb, up, target_up,
+ k_grind_spring,
+ k_grind_dampener,
+ k_rb_delta );
+
+ rb_effect_spring_target_vector( &localplayer.rb, fwd, target_fwd,
+ k_grind_spring*strength,
+ k_grind_dampener*strength,
+ k_rb_delta );
+
+ vg_line_arrow( localplayer.rb.co, target_up, 1.0f, VG__GREEN );
+ vg_line_arrow( localplayer.rb.co, fwd, 0.8f, VG__RED );
+ vg_line_arrow( localplayer.rb.co, target_fwd, 1.0f, VG__YELOW );
+
+ player_skate.grind_strength = strength;
+
+ /* Fake contact */
+ struct grind_limit *limit =
+ &player_skate.limits[ player_skate.limit_count ++ ];
+ m4x3_mulv( localplayer.rb.to_local, wsp, limit->ra );
+ m3x3_mulv( localplayer.rb.to_local, inf->n, limit->n );
+ limit->p = 0.0f;
+
+ v3_copy( inf->dir, player_skate.grind_dir );
+}
+
+static void skate_5050_apply( struct grind_info *inf_front,
+ struct grind_info *inf_back ){
+ struct player_skate_state *state = &player_skate.state;
+ struct grind_info inf_avg;
+
+ v3_sub( inf_front->co, inf_back->co, inf_avg.dir );
+ v3_muladds( inf_back->co, inf_avg.dir, 0.5f, inf_avg.co );
+ v3_normalize( inf_avg.dir );
+
+ /* dont ask */
+ v3_muls( inf_avg.dir, vg_signf(v3_dot(inf_avg.dir,localplayer.rb.v)),
+ inf_avg.dir );