+ 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 ) )
+ 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 )
+{
+ struct player_skate *s = &player->_skate;
+ 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,player->rb.v)),
+ inf_avg.dir );
+
+ v3f axis_front, axis_back, axis;
+ v3_cross( inf_front->dir, inf_front->n, axis_front );
+ v3_cross( inf_back->dir, inf_back->n, axis_back );
+ v3_add( axis_front, axis_back, axis );
+ v3_normalize( axis );
+
+ v3_cross( axis, inf_avg.dir, inf_avg.n );
+ skate_grind_decay( player, &inf_avg, 1.0f );
+
+ v2f steer;
+ joystick_state( k_srjoystick_steer, steer );
+
+ float way = steer[1] *
+ vg_signf( v3_dot( player->rb.to_world[2], player->rb.v ) );
+ v4f q;
+ v3f up, target_up;
+ v3_copy( player->rb.to_world[1], up );
+ v3_copy( inf_avg.n, target_up );
+ q_axis_angle( q, player->rb.to_world[0], VG_PIf*0.25f * -way );
+ q_mulv( q, target_up, target_up );
+
+ v3_zero( s->weight_distribution );
+ s->weight_distribution[2] = k_board_length * -way;
+
+ rb_effect_spring_target_vector( &player->rb, up, target_up,
+ k_grind_spring,
+ k_grind_dampener,
+ k_rb_delta );
+ vg_line_arrow( player->rb.co, up, 1.0f, VG__GREEN );
+ vg_line_arrow( player->rb.co, target_up, 1.0f, VG__GREEN );
+
+ v3f fwd_nplane, dir_nplane;
+ v3_muladds( player->rb.to_world[2], inf_avg.n,
+ -v3_dot( player->rb.to_world[2], inf_avg.n ), fwd_nplane );
+
+ v3f dir;
+ v3_muls( inf_avg.dir, v3_dot( fwd_nplane, inf_avg.dir ), dir );
+ v3_muladds( dir, inf_avg.n, -v3_dot( dir, inf_avg.n ), dir_nplane );
+
+ v3_normalize( fwd_nplane );
+ v3_normalize( dir_nplane );
+
+ rb_effect_spring_target_vector( &player->rb, fwd_nplane, dir_nplane,
+ 1000.0f,
+ k_grind_dampener,
+ k_rb_delta );
+ vg_line_arrow( player->rb.co, fwd_nplane, 0.8f, VG__RED );
+ vg_line_arrow( player->rb.co, dir_nplane, 0.8f, VG__RED );
+
+ v3f pos_front = { 0.0f, -k_board_radius, -1.0f * k_board_length },
+ pos_back = { 0.0f, -k_board_radius, 1.0f * k_board_length },
+ delta_front, delta_back, delta_total;
+
+ m4x3_mulv( player->rb.to_world, pos_front, pos_front );
+ m4x3_mulv( player->rb.to_world, pos_back, pos_back );
+
+ v3_sub( inf_front->co, pos_front, delta_front );
+ v3_sub( inf_back->co, pos_back, delta_back );
+ v3_add( delta_front, delta_back, delta_total );
+
+ v3_muladds( player->rb.v, delta_total, 50.0f * k_rb_delta, player->rb.v );
+
+ /* Fake contact */
+ struct grind_limit *limit = &s->limits[ s->limit_count ++ ];
+ v3_zero( limit->ra );
+ m3x3_mulv( player->rb.to_local, inf_avg.n, limit->n );
+ limit->p = 0.0f;
+
+ v3_copy( inf_avg.dir, s->grind_dir );
+}
+
+VG_STATIC int skate_grind_truck_renew( player_instance *player, float sign,
+ struct grind_info *inf )
+{
+ struct player_skate *s = &player->_skate;
+
+ v3f wheel_co = { 0.0f, 0.0f, sign * k_board_length },
+ grind_co = { 0.0f, -k_board_radius, sign * k_board_length };
+
+ m4x3_mulv( player->rb.to_world, wheel_co, wheel_co );
+ m4x3_mulv( player->rb.to_world, grind_co, grind_co );
+
+ /* Exit condition: lost grind tracking */
+ if( !skate_grind_scansq( player, grind_co, player->rb.v, 0.3f, inf ) )
+ return 0;
+
+ /* Exit condition: cant see grind target directly */
+ if( !skate_point_visible( wheel_co, inf->co ) )
+ return 0;
+
+ /* Exit condition: minimum velocity not reached, but allow a bit of error */
+ float dv = fabsf(v3_dot( player->rb.v, inf->dir )),
+ minv = k_grind_axel_min_vel*0.8f;
+
+ if( dv < minv )
+ return 0;
+
+ if( fabsf(v3_dot( inf->dir, s->grind_dir )) < k_grind_max_edge_angle )
+ return 0;
+
+ v3_copy( inf->dir, s->grind_dir );
+ return 1;
+}
+
+VG_STATIC int skate_grind_truck_entry( player_instance *player, float sign,
+ struct grind_info *inf )
+{
+ 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 );
+
+ if( skate_grind_scansq( player, wsp, player->rb.v, 0.3, inf ) )