- float fwd_resistance = vg_get_button( "break" )? 5.0f: k_friction_resistance;
-#else
- float fwd_resistance = k_friction_resistance;
-#endif
-
- 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( vg_get_button( "jump" ) )
- {
- 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_amt = vg_get_axis( "walk/push" ) * 0.5f + 0.5f;
- int push_thresh = push_amt>0.15f? 1: 0;
-
- if( push_thresh && !push_thresh_last )
- player.phys.start_push = vg.time;
-
- push_thresh_last = push_thresh;
-
- if( !vg_get_button("jump") && 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;
- }
-
- /* Pumping */
- static float previous = 0.0f;
- float delta = previous - phys->grab,
- pump = delta * k_pump_force * VG_TIMESTEP_FIXED;
- previous = phys->grab;
-
- v3f p1;
- v3_muladds( phys->rb.co, phys->rb.up, pump, p1 );
- vg_line( phys->rb.co, p1, 0xff0000ff );
-
- vel[1] += pump;
-
-
- m3x3_mulv( phys->rb.to_world, vel, phys->rb.v );
-
- float steer = vg_get_axis( "lookh" ),
- steer_scaled = vg_signf(steer) * powf(steer,2.0f) * k_steer_ground;
-
- phys->iY -= steer_scaled * VG_TIMESTEP_FIXED;
-}
-
-/*
- * Air control, no real physics
- */
-VG_STATIC void player_physics_control_air(void)
-{
- struct player_phys *phys = &player.phys;
-
- m3x3_mulv( phys->vr, phys->rb.v, phys->rb.v );
- vg_line_cross( player.land_target, 0xff0000ff, 0.25f );
-
- ray_hit hit;
-
- /*
- * Prediction
- */
- float pstep = VG_TIMESTEP_FIXED * 10.0f;
-
- v3f pco, pco1, pv;
- v3_copy( phys->rb.co, pco );
- v3_copy( phys->rb.v, pv );
-
- float time_to_impact = 0.0f;
- float limiter = 1.0f;
-
- for( int i=0; i<50; i++ )
- {
- v3_copy( pco, pco1 );
- m3x3_mulv( phys->vr_pstep, pv, pv );
- apply_gravity( pv, 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);
-
- float orig_dist = contact.dist;
- if( ray_world( pco1, vdir, &contact ))
- {
- float angle = v3_dot( phys->rb.up, contact.normal );
- v3f axis;
- v3_cross( phys->rb.up, contact.normal, axis );
-
- time_to_impact += (contact.dist/orig_dist)*pstep;
- limiter = vg_minf( 5.0f, time_to_impact )/5.0f;
- limiter = 1.0f-limiter;
- limiter *= limiter;
- limiter = 1.0f-limiter;
-
- if( angle < 0.99f )
- {
- v4f correction;
- q_axis_angle( correction, axis,
- acosf(angle)*(1.0f-limiter)*3.0f*VG_TIMESTEP_FIXED );
- q_mul( correction, phys->rb.q, phys->rb.q );
- }
-
- vg_line_cross( contact.pos, 0xffff0000, 0.25f );
- break;
- }
- time_to_impact += pstep;
- }
-
- phys->iY -= vg_get_axis( "lookh" ) * k_steer_air * VG_TIMESTEP_FIXED;
- {
- float iX = vg_get_axis( "lookv" ) *
- phys->reverse * k_steer_air * limiter * 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 );
- }
-
- v2f target = {0.0f,0.0f};
- v2_muladds( target, (v2f){ vg_get_axis("grabh"), vg_get_axis("grabv") },
- phys->grab, target );
-}
-
-/*
- * Entire Walking physics model
- * TODO: sleep when under certain velotiy
- */
-VG_STATIC void player_walk_physics(void)
-{
- struct player_phys *phys = &player.phys;
- rigidbody *rbf = &player.collide_front,
- *rbb = &player.collide_back;
-
- m3x3_copy( phys->rb.to_world, player.collide_front.to_world );
- m3x3_copy( phys->rb.to_world, player.collide_back.to_world );
-
- float h0 = 0.3f,
- h1 = 0.9f;
-
- m4x3_mulv( phys->rb.to_world, (v3f){0.0f,h0,0.0f}, rbf->co );
- v3_copy( rbf->co, rbf->to_world[3] );
- m4x3_mulv( phys->rb.to_world, (v3f){0.0f,h1,0.0f}, rbb->co );
- v3_copy( rbb->co, rbb->to_world[3] );
-
- m4x3_invert_affine( rbf->to_world, rbf->to_local );
- m4x3_invert_affine( rbb->to_world, rbb->to_local );
-
- rb_update_bounds( rbf );
- rb_update_bounds( rbb );
-
- rb_debug( rbf, 0xff0000ff );
- rb_debug( rbb, 0xff0000ff );
-
- rb_ct manifold[64];
- int len = 0;
-
- len += rb_sphere_scene( rbf, &world.rb_geo, manifold+len );
- len += rb_sphere_scene( rbb, &world.rb_geo, manifold+len );
-
- rb_presolve_contacts( manifold, len );
-
- for( int j=0; j<5; j++ )
- {
- for( int i=0; i<len; i++ )
- {
- struct contact *ct = &manifold[i];
-
- /*normal */
- float vn = -v3_dot( phys->rb.v, ct->n );
- vn += ct->bias;
-
- float temp = ct->norm_impulse;
- ct->norm_impulse = vg_maxf( temp + vn, 0.0f );
- vn = ct->norm_impulse - temp;
-
- v3f impulse;
- v3_muls( ct->n, vn, impulse );
-
- v3_add( impulse, phys->rb.v, phys->rb.v );
-
- /* friction */
- for( int j=0; j<2; j++ )
- {
- float f = k_friction * ct->norm_impulse,
- vt = v3_dot( phys->rb.v, ct->t[j] ),
- lambda = -vt;
-
- float temp = ct->tangent_impulse[j];
- ct->tangent_impulse[j] = vg_clampf( temp + lambda, -f, f );
- lambda = ct->tangent_impulse[j] - temp;
-
- v3_muladds( phys->rb.v, ct->t[j], lambda, phys->rb.v );
- }
- }
- }
-
- if( len == 0 )
- phys->in_air = 1;
- else
- {
- phys->in_air = 0;
- struct world_material *surface_mat = world_contact_material( manifold );
- player.surface_prop = surface_mat->info.surface_prop;
- }
-
- if( !phys->in_air )
- {
- float const DOWNFORCE = -k_walk_downforce*VG_TIMESTEP_FIXED;
- v3_muladds( phys->rb.v, (v3f){0.0f,-1.0f,0.0f}, DOWNFORCE, phys->rb.v );
-
- if( vg_get_button("jump") )
- {
- phys->rb.v[1] = 5.0f;
- }
- }
-
- v3_zero( phys->rb.w );
- q_axis_angle( phys->rb.q, (v3f){0.0f,1.0f,0.0f}, -player.angles[0] );
-
- v3f forward_dir = { sinf(player.angles[0]),0.0f,-cosf(player.angles[0]) };
-
- v3f p1;
- v3_muladds( phys->rb.co, forward_dir, 2.0f, p1 );
- vg_line( phys->rb.co, p1, 0xff0000ff );
-
- player.walk = powf( vg_get_axis("walk/push")*0.5f + 0.5f, 4.0f );
-
- if( player.walk > 0.025f )
- {
- float
- speed = vg_lerpf( 0.025f*k_runspeed, k_runspeed, player.walk ),
- amt = k_walk_accel * VG_TIMESTEP_FIXED,
- zvel = v3_dot( phys->rb.v, forward_dir ),
- new_vel = vg_minf( zvel + amt, speed ),
- diff = new_vel - vg_minf( zvel, speed );
-
- if( !phys->in_air )
- {
- v3_muladds( phys->rb.v, forward_dir, diff, phys->rb.v );
- }
- }
-
- if( !phys->in_air )
- {
- phys->rb.v[0] *= 1.0f - (VG_TIMESTEP_FIXED * k_walk_friction);
- phys->rb.v[2] *= 1.0f - (VG_TIMESTEP_FIXED * k_walk_friction);
- }
-}
-
-/*
- * Physics collision detection, and control
- */
-VG_STATIC void player_physics(void)
-{
- struct player_phys *phys = &player.phys;
- /*
- * Update collision fronts
- */
-
- rigidbody *rbf = &player.collide_front,
- *rbb = &player.collide_back;
-
- m3x3_copy( phys->rb.to_world, player.collide_front.to_world );
- m3x3_copy( phys->rb.to_world, player.collide_back.to_world );
-
- player.air_blend = vg_lerpf( player.air_blend, phys->in_air, 0.1f );
- float h = player.air_blend*0.2f;
-
- m4x3_mulv( phys->rb.to_world, (v3f){0.0f,h,-k_board_length}, rbf->co );
- v3_copy( rbf->co, rbf->to_world[3] );
- m4x3_mulv( phys->rb.to_world, (v3f){0.0f,h, k_board_length}, rbb->co );
- v3_copy( rbb->co, rbb->to_world[3] );
-
- m4x3_invert_affine( rbf->to_world, rbf->to_local );
- m4x3_invert_affine( rbb->to_world, rbb->to_local );
-
- rb_update_bounds( rbf );
- rb_update_bounds( rbb );
-
- rb_debug( rbf, 0xff00ffff );
- rb_debug( rbb, 0xffffff00 );
-
- rb_ct manifold[64];