- 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 );