-VG_STATIC void apply_gravity( v3f vel, float const timestep )
-{
- v3f gravity = { 0.0f, -9.6f, 0.0f };
- v3_muladds( vel, gravity, timestep, vel );
-}
-
-VG_STATIC struct
-grind_edge *player_grind_collect_edge( v3f p0, v3f p1,
- v3f c0, v3f c1, float max_dist )
-{
- struct player_phys *phys = &player.phys;
-
- bh_iter it;
- bh_iter_init( 0, &it );
-
- boxf region;
-
- box_init_inf( region );
- box_addpt( region, p0 );
- box_addpt( region, p1 );
-
- float k_r = max_dist;
- v3_add( (v3f){ k_r, k_r, k_r}, region[1], region[1] );
- v3_add( (v3f){-k_r,-k_r,-k_r}, region[0], region[0] );
-
- float closest = k_r*k_r;
- struct grind_edge *closest_edge = NULL;
-
- int idx;
- while( bh_next( world.grind_bh, &it, region, &idx ) )
- {
- struct grind_edge *edge = &world.grind_edges[ idx ];
-
- float s,t;
- v3f pa, pb;
-
- float d2 =
- closest_segment_segment( p0, p1, edge->p0, edge->p1, &s,&t, pa, pb );
-
- if( d2 < closest )
- {
- closest = d2;
- closest_edge = edge;
- v3_copy( pa, c0 );
- v3_copy( pb, c1 );
- }
- }
-
- return closest_edge;
-}
-
-/*
- * Called when launching into the air to predict and adjust trajectories
- */
-VG_STATIC void player_start_air(void)
-{
- struct player_phys *phys = &player.phys;
-
- float pstep = VG_TIMESTEP_FIXED * 10.0f;
- float best_velocity_delta = -9999.9f;
- float k_bias = 0.96f;
-
- v3f axis;
- v3_cross( phys->rb.up, phys->rb.v, axis );
- v3_normalize( axis );
- player.land_log_count = 0;
-
- m3x3_identity( phys->vr );
-
- for( int m=-3;m<=12; m++ )
- {
- struct land_log *log = &player.land_log[ player.land_log_count ++ ];
- log->count = 0;
- log->colour = 0xff000000;
-
- float vmod = ((float)m / 15.0f)*0.09f;
-
- v3f pco, pco1, pv;
- v3_copy( phys->rb.co, pco );
- v3_muls( phys->rb.v, k_bias, pv );
-
- /*
- * Try different 'rotations' of the velocity to find the best possible
- * landing normal. This conserves magnitude at the expense of slightly
- * unrealistic results
- */
-
- m3x3f vr;
- v4f vr_q;
-
- q_axis_angle( vr_q, axis, vmod );
- q_m3x3( vr_q, vr );
-
- m3x3_mulv( vr, pv, pv );
- v3_muladds( pco, pv, pstep, pco );
-
- struct grind_edge *best_grind = NULL;
- float closest_grind = INFINITY;
-
- for( int i=0; i<50; i++ )
- {
- v3_copy( pco, pco1 );
- apply_gravity( pv, pstep );
-
- m3x3_mulv( vr, pv, pv );
- 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 = player_grind_collect_edge( pco, pco1,
- c0, c1, 0.4f );
-
- if( ge && (v3_dot((v3f){0.0f,1.0f,0.0f},vdir) < -0.2f ) )
- {
- float d2 = v3_dist2( c0, c1 );
- if( d2 < closest_grind )
- {
- closest_grind = d2;
- best_grind = ge;
- }
- }
-
- if( ray_world( pco1, vdir, &contact ))
- {
- float land_delta = v3_dot( pv, contact.normal );
- u32 scolour = (u8)(vg_minf(-land_delta * 2.0f, 255.0f));
-
- /* Bias prediction towords ramps */
- if( ray_hit_material( &contact )->info.flags
- & k_material_flag_skate_surface )
- {
- land_delta *= 0.1f;
- scolour |= 0x0000a000;
- }
-
- if( (land_delta < 0.0f) && (land_delta > best_velocity_delta) )
- {
- best_velocity_delta = land_delta;
-
- v3_copy( contact.pos, player.land_target );
-
- m3x3_copy( vr, phys->vr_pstep );
- q_axis_angle( vr_q, axis, vmod*0.1f );
- q_m3x3( vr_q, phys->vr );
- }
-
- v3_copy( contact.pos, log->positions[ log->count ++ ] );
- log->colour = 0xff000000 | scolour;
- break;
- }
-
- v3_copy( pco, log->positions[ log->count ++ ] );
- }
-
- if( best_grind )
- {
- log->colour = 0xff0000ff;
-
- float score = -closest_grind * 0.05f;
-
- if( score > best_velocity_delta )
- {
- best_velocity_delta = score;
-
- m3x3_copy( vr, phys->vr_pstep );
- q_axis_angle( vr_q, axis, vmod*0.1f );
- q_m3x3( vr_q, phys->vr );
- }
- }
- }
-}
-
-
-VG_STATIC void player_physics_control_passive(void)
-{
- struct player_phys *phys = &player.phys;
- float grabt = player.input_grab->axis.value;
-
- if( grabt > 0.5f )
- {
- v2_muladds( phys->grab_mouse_delta, vg.mouse_delta, 0.02f,
- phys->grab_mouse_delta );
- v2_normalize_clamp( phys->grab_mouse_delta );
-
- if( freecam )
- v2_zero( phys->grab_mouse_delta );
- }
- else
- v2_zero( phys->grab_mouse_delta );
-
- phys->grab = vg_lerpf( phys->grab, grabt, 0.14f );
- player.phys.pushing = 0.0f;
-
- if( !phys->jump_charge || phys->in_air )
- {
- phys->jump -= k_jump_charge_speed * VG_TIMESTEP_FIXED;
- }
-
- phys->jump_charge = 0;
- phys->jump = vg_clampf( phys->jump, 0.0f, 1.0f );
-}
-
-/*
- * Main friction interface model
- */
-VG_STATIC void player_physics_control(void)
-{
- struct player_phys *phys = &player.phys;
-
- /*
- * Computing localized friction forces for controlling the character
- * Friction across X is significantly more than Z
- */
-
- v3f vel;
- m3x3_mulv( phys->rb.to_local, phys->rb.v, vel );
- float slip = 0.0f;
-
- if( fabsf(vel[2]) > 0.01f )
- slip = fabsf(-vel[0] / vel[2]) * vg_signf(vel[0]);
-
- if( fabsf( slip ) > 1.2f )
- slip = vg_signf( slip ) * 1.2f;
- phys->slip = slip;
- phys->reverse = -vg_signf(vel[2]);
-
- float substep = VG_TIMESTEP_FIXED * 0.2f;
- float fwd_resistance = k_friction_resistance;
-
- for( int i=0; i<5; i++ )
- {
- vel[2] = stable_force( vel[2],vg_signf(vel[2]) * -fwd_resistance*substep);
- vel[0] = stable_force( vel[0],vg_signf(vel[0]) * -k_friction_lat*substep);
- }
-
- if( player.input_jump->button.value )
- {
- phys->jump += VG_TIMESTEP_FIXED * k_jump_charge_speed;
-
- if( !phys->jump_charge )
- phys->jump_dir = phys->reverse > 0.0f? 1: 0;
-
- phys->jump_charge = 1;
- }
-
- static int push_thresh_last = 0;
- float push = player.input_push->button.value;
- int push_thresh = push>0.15f? 1: 0;
-
- if( push_thresh && !push_thresh_last )
- player.phys.start_push = vg.time;
-
- push_thresh_last = push_thresh;
-
- if( !player.input_jump->button.value && push_thresh )
- {
- player.phys.pushing = 1.0f;
- player.phys.push_time = vg.time - player.phys.start_push;
-
- float cycle_time = player.phys.push_time*k_push_cycle_rate,
- amt = k_push_accel * (sinf(cycle_time)*0.5f+0.5f)*VG_TIMESTEP_FIXED,
- current = v3_length( vel ),
- new_vel = vg_minf( current + amt, k_max_push_speed );
-
- new_vel -= vg_minf(current, k_max_push_speed);
- vel[2] -= new_vel * phys->reverse;
- }
-
- m3x3_mulv( phys->rb.to_world, vel, phys->rb.v );
-
- float input = player.input_js1h->axis.value,
- grab = player.input_grab->axis.value,
- steer = input * (1.0f-(phys->jump+grab)*0.4f),
- steer_scaled = vg_signf(steer) * powf(steer,2.0f) * k_steer_ground;
-
- phys->iY -= steer_scaled * VG_TIMESTEP_FIXED;
-
- if( !phys->jump_charge && phys->jump > 0.2f )
- {
- v3f jumpdir;
-
- /* Launch more up if alignment is up else improve velocity */
- float aup = fabsf(v3_dot( (v3f){0.0f,1.0f,0.0f}, phys->rb.up )),
- mod = 0.5f,
- dir = mod + aup*(1.0f-mod);
-
- v3_copy( phys->rb.v, jumpdir );
- v3_normalize( jumpdir );
- v3_muls( jumpdir, 1.0f-dir, jumpdir );
- v3_muladds( jumpdir, phys->rb.up, dir, jumpdir );
- v3_normalize( jumpdir );
-
- float force = k_jump_force*phys->jump;
- v3_muladds( phys->rb.v, jumpdir, force, phys->rb.v );
- phys->jump = 0.0f;
-
- player.jump_time = vg.time;
-
- /* TODO: Move to audio file */
- audio_lock();
- audio_player_set_flags( &audio_player_extra, AUDIO_FLAG_SPACIAL_3D );
- audio_player_set_position( &audio_player_extra, phys->rb.co );
- audio_player_set_vol( &audio_player_extra, 20.0f );
- audio_player_playclip( &audio_player_extra, &audio_jumps[rand()%2] );
- audio_unlock();
- }
-}
-
-VG_STATIC void player_physics_control_grind(void)
-{
- struct player_phys *phys = &player.phys;
- v2f steer = { player.input_js1h->axis.value,
- player.input_js1v->axis.value };
-
- float l2 = v2_length2( steer );
- if( l2 > 1.0f )
- v2_muls( steer, 1.0f/sqrtf(l2), steer );
-
- phys->iY -= steer[0] * k_steer_air * VG_TIMESTEP_FIXED;
-
- float iX = steer[1] * phys->reverse * k_steer_air * VG_TIMESTEP_FIXED;
-
- static float siX = 0.0f;
- siX = vg_lerpf( siX, iX, k_steer_air_lerp );
-
- v4f rotate;
- q_axis_angle( rotate, phys->rb.right, siX );
- q_mul( rotate, phys->rb.q, phys->rb.q );
-
- phys->slip = 0.0f;
-}