+
+ 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 );
+
+ 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 );
+
+ 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 );
+
+ 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 ) )
+ {
+ if( fabsf(v3_dot( player->rb.v, inf->dir )) < k_grind_axel_min_vel )
+ return 0;
+
+ /* velocity should be at least 60% aligned */
+ v3f pv, axis;
+ v3_cross( inf->n, inf->dir, axis );
+ v3_muladds( player->rb.v, inf->n, -v3_dot( player->rb.v, inf->n ), pv );
+
+ if( v3_length2( pv ) < 0.0001f )
+ return 0;
+ v3_normalize( pv );
+
+ if( fabsf(v3_dot( pv, inf->dir )) < k_grind_axel_max_angle )
+ return 0;
+
+ if( v3_dot( player->rb.v, inf->n ) > 0.5f )
+ return 0;
+
+#if 0
+ /* check for vertical alignment */
+ if( v3_dot( player->rb.to_world[1], inf->n ) < k_grind_axel_max_vangle )
+ return 0;
+#endif
+
+ v3f local_co, local_dir, local_n;
+ m4x3_mulv( player->rb.to_local, inf->co, local_co );
+ m3x3_mulv( player->rb.to_local, inf->dir, local_dir );
+ m3x3_mulv( player->rb.to_local, inf->n, local_n );
+
+ v2f delta = { local_co[0], local_co[2] - k_board_length*sign };
+
+ float truck_height = -(k_board_radius+0.03f);
+
+ v3f rv;
+ v3_cross( player->rb.w, raw, rv );
+ v3_add( player->rb.v, rv, rv );
+
+ if( (local_co[1] >= truck_height) &&
+ (v2_length2( delta ) <= k_board_radius*k_board_radius) )
+ {
+ return 1;
+ }
+ }
+
+ return 0;
+}
+
+VG_STATIC void skate_boardslide_apply( player_instance *player,
+ struct grind_info *inf )
+{
+ struct player_skate *s = &player->_skate;
+
+ v3f local_co, local_dir, local_n;
+ m4x3_mulv( player->rb.to_local, inf->co, local_co );
+ m3x3_mulv( player->rb.to_local, inf->dir, local_dir );
+ m3x3_mulv( player->rb.to_local, inf->n, local_n );
+
+ v3f intersection;
+ v3_muladds( local_co, local_dir, local_co[0]/-local_dir[0],
+ intersection );
+ v3_copy( intersection, s->weight_distribution );
+
+ skate_grind_decay( player, inf, 0.0125f );
+ skate_grind_friction( player, inf, 0.25f );
+
+ /* direction alignment */
+ v3f dir, perp;
+ v3_cross( local_dir, local_n, perp );
+ v3_muls( local_dir, vg_signf(local_dir[0]), dir );
+ v3_muls( perp, vg_signf(perp[2]), perp );
+
+ m3x3_mulv( player->rb.to_world, dir, dir );
+ m3x3_mulv( player->rb.to_world, perp, perp );
+
+ v4f qbalance;
+ q_axis_angle( qbalance, dir, local_co[0]*k_grind_balance );
+ q_mulv( qbalance, perp, perp );
+
+ rb_effect_spring_target_vector( &player->rb, player->rb.to_world[0],
+ dir,
+ k_grind_spring, k_grind_dampener,
+ k_rb_delta );
+
+ rb_effect_spring_target_vector( &player->rb, player->rb.to_world[2],
+ perp,
+ k_grind_spring, k_grind_dampener,
+ k_rb_delta );
+
+ vg_line_arrow( player->rb.co, dir, 0.5f, VG__GREEN );
+ vg_line_arrow( player->rb.co, perp, 0.5f, VG__BLUE );
+
+ v3_copy( inf->dir, s->grind_dir );
+}
+
+VG_STATIC int skate_boardslide_entry( player_instance *player,
+ struct grind_info *inf )
+{
+ struct player_skate *s = &player->_skate;
+
+ if( skate_grind_scansq( player, player->rb.co,
+ player->rb.to_world[0], k_board_length,
+ inf ) )
+ {
+ v3f local_co, local_dir;
+ m4x3_mulv( player->rb.to_local, inf->co, local_co );
+ m3x3_mulv( player->rb.to_local, inf->dir, local_dir );
+
+ if( (fabsf(local_co[2]) <= k_board_length) && /* within wood area */
+ (local_co[1] >= 0.0f) && /* at deck level */
+ (fabsf(local_dir[0]) >= 0.25f) ) /* perpendicular to us */
+ {
+ if( fabsf(v3_dot( player->rb.v, inf->dir )) < k_grind_axel_min_vel )
+ return 0;
+
+ return 1;
+ }
+ }
+
+ return 0;