- skate_score_biased_path( player->rb.co, player->rb.v, bias, p );
-
- if( p->type != k_prediction_none )
- {
- if( p->score < min_score )
- {
- min_score = p->score;
- best_vmod = vmod;
- s->land_dist = p->land_dist;
- v3_copy( p->apex, s->state.apex );
- }
-
- if( p->score > max_score )
- max_score = p->score;
- }
- }
-
- v4f vr_q;
- q_axis_angle( vr_q, axis, best_vmod*0.1f );
- q_m3x3( vr_q, s->state.velocity_bias );
-
- q_axis_angle( vr_q, axis, best_vmod );
- q_m3x3( vr_q, s->state.velocity_bias_pstep );
-
- /*
- * Logging
- */
- for( int i=0; i<s->prediction_count; i ++ )
- {
- struct land_prediction *p = &s->predictions[i];
-
- float l = p->score;
-
- if( l < 0.0f )
- {
- vg_error( "negative score! (%f)\n", l );
- }
-
- l -= min_score;
- l /= (max_score-min_score);
- l = 1.0f - l;
- l *= 255.0f;
-
- p->colour = l;
- p->colour <<= 8;
- p->colour |= 0xff000000;
- }
-
-
- v2f steer = { player->input_js1h->axis.value,
- player->input_js1v->axis.value };
- v2_normalize_clamp( steer );
-
- if( (fabsf(steer[1]) > 0.5f) && (s->land_dist >= 1.0f) )
- {
- s->state.flip_rate = (1.0f/s->land_dist) * vg_signf(steer[1]) *
- s->state.reverse ;
- s->state.flip_time = 0.0f;
- v3_copy( player->rb.to_world[0], s->state.flip_axis );
- }
- else
- {
- s->state.flip_rate = 0.0f;
- v3_zero( s->state.flip_axis );
- }
-}
-
-/*
- *
- * Varius physics models
- * ------------------------------------------------
- */
-
-#if 0
-VG_STATIC void skate_apply_grind_model( player_instance *player,
- rb_ct *manifold, int len )
-{
- struct player_skate *s = &player->_skate;
-
- /* FIXME: Queue audio events instead */
- if( len == 0 )
- {
- if( s->state.activity == k_skate_activity_grind )
- {
-#if 0
- audio_lock();
- audio_player_set_flags( &audio_player_extra,
- AUDIO_FLAG_SPACIAL_3D );
- audio_player_set_position( &audio_player_extra, player.rb.co );
- audio_player_set_vol( &audio_player_extra, 20.0f );
- audio_player_playclip( &audio_player_extra, &audio_board[6] );
- audio_unlock();
-#endif
-
- s->state.activity = k_skate_activity_air;
- }
- return;
- }
-
- v2f steer = { player->input_js1h->axis.value,
- player->input_js1v->axis.value };
- v2_normalize_clamp( steer );
-
-#if 0
- s->state.steery -= steer[0] * k_steer_air * k_rb_delta;
- s->state.steerx += steer[1] * s->state.reverse * k_steer_air * k_rb_delta;
-#endif
-
-#if 0
- v4f rotate;
- q_axis_angle( rotate, player->rb.to_world[0], siX );
- q_mul( rotate, player.rb.q, player.rb.q );
-#endif
-
- s->state.slip = 0.0f;
- s->state.activity = k_skate_activity_grind;
-
- /* TODO: Compression */
- v3f up = { 0.0f, 1.0f, 0.0f };
- float angle = v3_dot( player->rb.to_world[1], up );
-
- if( fabsf(angle) < 0.99f )
- {
- v3f axis;
- v3_cross( player->rb.to_world[1], up, axis );
-
- v4f correction;
- q_axis_angle( correction, axis, k_rb_delta * 10.0f * acosf(angle) );
- q_mul( correction, player->rb.q, player->rb.q );
- }
-
- float const DOWNFORCE = -k_downforce*1.2f*VG_TIMESTEP_FIXED;
- v3_muladds( player->rb.v, manifold->n, DOWNFORCE, player->rb.v );
- m3x3_identity( s->state.velocity_bias );
- m3x3_identity( s->state.velocity_bias_pstep );
-
- if( s->state.activity_prev != k_skate_activity_grind )
- {
- /* FIXME: Queue audio events instead */
-#if 0
- audio_lock();
- audio_player_set_flags( &audio_player_extra,
- AUDIO_FLAG_SPACIAL_3D );
- audio_player_set_position( &audio_player_extra, player.rb.co );
- audio_player_set_vol( &audio_player_extra, 20.0f );
- audio_player_playclip( &audio_player_extra, &audio_board[5] );
- audio_unlock();
-#endif
- }
-}
-#endif
-
-/*
- * Air control, no real physics
- */
-VG_STATIC void skate_apply_air_model( player_instance *player )
-{
- struct player_skate *s = &player->_skate;
-
- if( s->state.activity_prev != k_skate_activity_air )
- player__approximate_best_trajectory( player );
-
- m3x3_mulv( s->state.velocity_bias, player->rb.v, player->rb.v );
- ray_hit hit;
-
- /*
- * Prediction
- */
- float pstep = VG_TIMESTEP_FIXED * 1.0f;
- float k_bias = 0.98f;
-
- v3f pco, pco1, pv;
- v3_copy( player->rb.co, pco );
- v3_muls( player->rb.v, 1.0f, pv );
-
- float time_to_impact = 0.0f;
- float limiter = 1.0f;
-
- struct grind_edge *best_grind = NULL;
- float closest_grind = INFINITY;
-
- v3f target_normal = { 0.0f, 1.0f, 0.0f };
- int has_target = 0;
-
- for( int i=0; i<250; i++ )
- {
- v3_copy( pco, pco1 );
- m3x3_mulv( s->state.velocity_bias, pv, pv );
-
- pv[1] += -k_gravity * pstep;
- v3_muladds( pco, pv, pstep, pco );
-
- ray_hit contact;
- v3f vdir;
-
- v3_sub( pco, pco1, vdir );
- contact.dist = v3_length( vdir );
- v3_divs( vdir, contact.dist, vdir);
-
- v3f c0, c1;
- struct grind_edge *ge = skate_collect_grind_edge( pco, pco1,
- c0, c1, 0.4f );
-
- if( ge && (v3_dot((v3f){0.0f,1.0f,0.0f},vdir) < -0.2f ) )
- {
- vg_line( ge->p0, ge->p1, 0xff0000ff );
- vg_line_cross( pco, 0xff0000ff, 0.25f );
- has_target = 1;
- break;
- }
-
- float orig_dist = contact.dist;
- if( ray_world( pco1, vdir, &contact ) )
- {
- v3_copy( contact.normal, target_normal );
- has_target = 1;
- time_to_impact += (contact.dist/orig_dist)*pstep;
- vg_line_cross( contact.pos, 0xffff0000, 0.25f );
- break;
- }
- time_to_impact += pstep;
- }
-
- if( has_target )
- {
- float angle = v3_dot( player->rb.to_world[1], target_normal );
- v3f axis;
- v3_cross( player->rb.to_world[1], target_normal, axis );
-
- limiter = vg_minf( 5.0f, time_to_impact )/5.0f;
- limiter = 1.0f-limiter;
- limiter *= limiter;
- limiter = 1.0f-limiter;
-
-#if 0
- if( fabsf(angle) < 0.9999f )
- {
- v4f correction;
- q_axis_angle( correction, axis,
- acosf(angle)*(1.0f-limiter)*2.0f*VG_TIMESTEP_FIXED );
- q_mul( correction, player->rb.q, player->rb.q );
- }
-#endif
- }
-
- v2f steer = { player->input_js1h->axis.value,
- player->input_js1v->axis.value };
- v2_normalize_clamp( steer );
-
- s->land_dist = time_to_impact;
- v3_copy( target_normal, s->land_normal );
-}
-
-#if 0
-VG_STATIC void skate_get_board_points( player_instance *player,
- v3f front, v3f back )
-{
- v3f pos_front = {0.0f,0.0f,-k_board_length},
- pos_back = {0.0f,0.0f, k_board_length};
-
- m4x3_mulv( player->rb.to_world, pos_front, front );
- m4x3_mulv( player->rb.to_world, pos_back, back );
-}
-#endif
-
-#if 0
-/*
- * Casts and pushes a sphere-spring model into the world
- */
-VG_STATIC int skate_simulate_spring( player_instance *player,
- v3f pos )
-{
- struct player_skate *s = &player->_skate;
-
- float mod = 0.7f * player->input_grab->axis.value + 0.3f,
- spring_k = mod * k_spring_force,
- damp_k = mod * k_spring_dampener,
- disp_k = 0.4f;
-
- v3f start, end;
- v3_copy( pos, start );
- v3_muladds( pos, player->rb.to_world[1], -disp_k, end );
-
- float t;
- v3f n;
- int hit_info = spherecast_world( start, end, 0.2f, &t, n );
-
- if( hit_info != -1 )
- {
- v3f F, delta;
- v3_sub( start, player->rb.co, delta );
-
- float displacement = vg_clampf( 1.0f-t, 0.0f, 1.0f ),
- damp =
- vg_maxf( 0.0f, v3_dot( player->rb.to_world[1], player->rb.v ) );
-
- v3_muls( player->rb.to_world[1], displacement*spring_k*k_rb_delta -
- damp*damp_k*k_rb_delta, F );
-
- v3_muladds( player->rb.v, F, 1.0f, player->rb.v );
-
- /* Angular velocity */
- v3f wa;
- v3_cross( delta, F, wa );
- v3_muladds( player->rb.w, wa, k_spring_angular, player->rb.w );
-
- v3_lerp( start, end, t, pos );
- return 1;
- }
- else
- {
- v3_copy( end, pos );
- return 0;
- }
-}
-#endif
-
-
-/*
- * Handles connection between the player and the ground
- *
- * TODO: Must save original velocity to use here
- */
-VG_STATIC void skate_apply_interface_model( player_instance *player,
- rb_ct *manifold, int len )
-{
- struct player_skate *s = &player->_skate;
-
- /* springs */
-
-#if 0
- v3f spring0, spring1;
-
- skate_get_board_points( player, spring1, spring0 );
- int spring_hit0 = 0, //skate_simulate_spring( player, s, spring0 ),
- spring_hit1 = 0; //skate_simulate_spring( player, s, spring1 );
-
- v3f animavg, animdelta;
- v3_add( spring0, spring1, animavg );
- v3_muls( animavg, 0.5f, animavg );
-
- v3_sub( spring1, spring0, animdelta );
- v3_normalize( animdelta );
-
- m4x3_mulv( player->rb.to_local, animavg, s->board_offset );
-
- float dx = -v3_dot( animdelta, player->rb.to_world[2] ),
- dy = v3_dot( animdelta, player->rb.to_world[1] );
-
- float angle = -atan2f( dy, dx );
- q_axis_angle( s->board_rotation, (v3f){1.0f,0.0f,0.0f}, angle );
-
- int lift_frames_limit = 6;
-
- /* Surface connection */
- if( len == 0 && !(spring_hit0 && spring_hit1) )
- {
- s->state.lift_frames ++;
-
- if( s->state.lift_frames >= lift_frames_limit )
- s->state.activity = k_skate_activity_air;
- }
- else
- {
- v3f surface_avg;
- v3_zero( surface_avg );
-
- for( int i=0; i<len; i++ )
- v3_add( surface_avg, manifold[i].n, surface_avg );
- v3_normalize( surface_avg );
-
- if( v3_dot( player->rb.v, surface_avg ) > 0.7f )
- {
- s->state.lift_frames ++;
-
- if( s->state.lift_frames >= lift_frames_limit )
- s->state.activity = k_skate_activity_air;
- }
- else
- {
- s->state.activity = k_skate_activity_ground;
- s->state.lift_frames = 0;
- v3f projected, axis;
-
- if( s->state.activity_prev == k_skate_activity_air )
- {
- player->cam_land_punch_v += v3_dot( player->rb.v, surface_avg ) *
- k_cam_punch;
- }
-
- float const DOWNFORCE = -k_downforce*VG_TIMESTEP_FIXED;
- v3_muladds( player->rb.v, player->rb.to_world[1],
- DOWNFORCE, player->rb.v );
-
- float d = v3_dot( player->rb.to_world[2], surface_avg );
- v3_muladds( surface_avg, player->rb.to_world[2], -d, projected );
- v3_normalize( projected );
-
- float angle = v3_dot( player->rb.to_world[1], projected );
- v3_cross( player->rb.to_world[1], projected, axis );
-
-#if 0
- if( fabsf(angle) < 0.9999f )
- {
- v4f correction;
- q_axis_angle( correction, axis,
- acosf(angle)*4.0f*VG_TIMESTEP_FIXED );
- q_mul( correction, player->rb.q, player->rb.q );
- }
-#endif
- }
- }
-#endif
-}
-
-VG_STATIC int player_skate_trick_input( player_instance *player );
-VG_STATIC void skate_apply_trick_model( player_instance *player )
-{
- struct player_skate *s = &player->_skate;
-
- v3f Fd, Fs, F;
- v3f strength = { 3.7f, 3.6f, 8.0f };
-
- v3_muls( s->board_trick_residualv, -4.0f , Fd );
- v3_muls( s->board_trick_residuald, -10.0f, Fs );
- v3_add( Fd, Fs, F );
- v3_mul( strength, F, F );
-
- v3_muladds( s->board_trick_residualv, F, k_rb_delta,
- s->board_trick_residualv );
- v3_muladds( s->board_trick_residuald, s->board_trick_residualv,
- k_rb_delta, s->board_trick_residuald );
-
- if( s->state.activity == k_skate_activity_air )
- {
- if( v3_length2( s->state.trick_vel ) < 0.0001f )
- return;
-
- int carry_on = player_skate_trick_input( player );