+ float tilt = steer[1];
+
+ if( fabsf(tilt) >= 0.25f ){
+ v3f raw = {0.0f,0.0f,tilt};
+ m3x3_mulv( localplayer.rb.to_world, raw, raw );
+
+ float way = tilt * vg_signf( v3_dot( raw, localplayer.rb.v ) );
+
+ if( way < 0.0f ) allow_front = 0;
+ else allow_back = 0;
+ }
+ }
+
+ if( state->activity == k_skate_activity_grind_boardslide ){
+ res_slide = skate_boardslide_renew( &inf_slide );
+ }
+ else if( state->activity == k_skate_activity_grind_back50 ){
+ res_back50 = skate_grind_truck_renew( 1.0f, &inf_back50 );
+
+ if( allow_front )
+ res_front50 = skate_grind_truck_entry( -1.0f, &inf_front50 );
+ }
+ else if( state->activity == k_skate_activity_grind_front50 ){
+ res_front50 = skate_grind_truck_renew( -1.0f, &inf_front50 );
+
+ if( allow_back )
+ res_back50 = skate_grind_truck_entry( 1.0f, &inf_back50 );
+ }
+ else if( state->activity == k_skate_activity_grind_5050 ){
+ if( allow_front )
+ res_front50 = skate_grind_truck_renew( -1.0f, &inf_front50 );
+ if( allow_back )
+ res_back50 = skate_grind_truck_renew( 1.0f, &inf_back50 );
+ }
+ else{
+ res_slide = skate_boardslide_entry( &inf_slide );
+
+ if( allow_back )
+ res_back50 = skate_grind_truck_entry( 1.0f, &inf_back50 );
+
+ if( allow_front )
+ res_front50 = skate_grind_truck_entry( -1.0f, &inf_front50 );
+
+ if( res_back50 != res_front50 ){
+ int wants_to_do_that = fabsf(steer[1]) >= 0.25f;
+
+ res_back50 &= wants_to_do_that;
+ res_front50 &= wants_to_do_that;
+ }
+ }
+
+ const enum skate_activity table[] =
+ { /* slide | back | front */
+ k_skate_activity_undefined, /* 0 0 0 */
+ k_skate_activity_grind_front50, /* 0 0 1 */
+ k_skate_activity_grind_back50, /* 0 1 0 */
+ k_skate_activity_grind_5050, /* 0 1 1 */
+
+ /* slide has priority always */
+ k_skate_activity_grind_boardslide, /* 1 0 0 */
+ k_skate_activity_grind_boardslide, /* 1 0 1 */
+ k_skate_activity_grind_boardslide, /* 1 1 0 */
+ k_skate_activity_grind_boardslide, /* 1 1 1 */
+ }
+ , new_activity = table[ res_slide << 2 | res_back50 << 1 | res_front50 ];
+
+ if( new_activity == k_skate_activity_undefined ){
+ if( state->activity >= k_skate_activity_grind_any ){
+ state->grind_cooldown = 15;
+ state->surface_cooldown = 10;
+ }
+ }
+ else if( new_activity == k_skate_activity_grind_boardslide ){
+ skate_boardslide_apply( &inf_slide );
+ }
+ else if( new_activity == k_skate_activity_grind_back50 ){
+ if( state->activity != k_skate_activity_grind_back50 )
+ skate_store_grind_vec( &inf_back50 );
+
+ skate_grind_truck_apply( 1.0f, &inf_back50, 1.0f );
+ }
+ else if( new_activity == k_skate_activity_grind_front50 ){
+ if( state->activity != k_skate_activity_grind_front50 )
+ skate_store_grind_vec( &inf_front50 );
+
+ skate_grind_truck_apply( -1.0f, &inf_front50, 1.0f );
+ }
+ else if( new_activity == k_skate_activity_grind_5050 )
+ skate_5050_apply( &inf_front50, &inf_back50 );
+
+ return new_activity;
+}
+
+void player__skate_update(void){
+ struct player_skate_state *state = &player_skate.state;
+ world_instance *world = world_current_instance();
+
+ if( state->activity == k_skate_activity_handplant )
+ return;
+
+ if( !world_water_player_safe( world, 0.25f ) ) return;
+
+ v3_copy( localplayer.rb.co, state->prev_pos );
+ state->activity_prev = state->activity;
+ v3f normal_total;
+ v3_zero( normal_total );
+
+ struct board_collider
+ {
+ v3f pos;
+ float radius;
+
+ u32 colour;
+
+ enum board_collider_state
+ {
+ k_collider_state_default,
+ k_collider_state_disabled,
+ k_collider_state_colliding
+ }
+ state;
+ }
+ wheels[] =
+ {
+ {
+ { 0.0f, 0.0f, -k_board_length },
+ .radius = k_board_radius,
+ .colour = VG__RED
+ },
+ {
+ { 0.0f, 0.0f, k_board_length },
+ .radius = k_board_radius,
+ .colour = VG__GREEN
+ }
+ };
+
+ float slap = 0.0f;
+
+ if( state->activity <= k_skate_activity_air_to_grind ){
+ float min_dist = 0.6f;
+ for( int i=0; i<2; i++ ){
+ v3f wpos, closest;
+ m4x3_mulv( localplayer.rb.to_world, wheels[i].pos, wpos );
+
+ if( bh_closest_point( world->geo_bh, wpos, closest, min_dist ) != -1 ){
+ min_dist = vg_minf( min_dist, v3_dist( closest, wpos ) );
+ }
+ }
+ min_dist -= 0.2f;
+ float vy = vg_maxf( 0.0f, localplayer.rb.v[1] );
+ slap = vg_clampf( (min_dist/0.5f) + vy, 0.0f, 1.0f )*0.3f;
+ }
+ state->slap = vg_lerpf( state->slap, slap, 10.0f*vg.time_fixed_delta );
+
+ wheels[0].pos[1] = state->slap;
+ wheels[1].pos[1] = state->slap;
+
+
+ const int k_wheel_count = 2;
+
+ player_skate.substep = vg.time_fixed_delta;
+ player_skate.substep_delta = player_skate.substep;
+ player_skate.limit_count = 0;
+
+ int substep_count = 0;
+
+ v3_zero( player_skate.surface_picture );
+
+ int prev_contacts[2];
+
+ for( int i=0; i<k_wheel_count; i++ ){
+ wheels[i].state = k_collider_state_default;
+ prev_contacts[i] = player_skate.wheel_contacts[i];
+ }
+
+ /* check if we can enter or continue grind */
+ enum skate_activity grindable_activity = skate_availible_grind();
+ if( grindable_activity != k_skate_activity_undefined ){
+ state->activity = grindable_activity;
+ goto grinding;
+ }
+
+ int contact_count = 0;
+ for( int i=0; i<2; i++ ){
+ v3f normal, axel;
+ v3_copy( localplayer.rb.to_world[0], axel );
+
+ if( skate_compute_surface_alignment( wheels[i].pos,
+ wheels[i].colour, normal, axel ) )
+ {
+ rb_effect_spring_target_vector( &localplayer.rb,
+ localplayer.rb.to_world[0],
+ axel,
+ k_surface_spring, k_surface_dampener,
+ player_skate.substep_delta );
+
+ v3_add( normal, player_skate.surface_picture,
+ player_skate.surface_picture );
+ contact_count ++;
+ player_skate.wheel_contacts[i] = 1;
+ }
+ else{
+ player_skate.wheel_contacts[i] = 0;
+ }
+
+ m3x3_mulv( localplayer.rb.to_local, axel, player_skate.truckv0[i] );
+ }
+
+ if( state->surface_cooldown ){
+ state->surface_cooldown --;
+ contact_count = 0;
+ }
+
+ if( (prev_contacts[0]+prev_contacts[1] == 1) && (contact_count == 2) ){
+ for( int i=0; i<2; i++ ){
+ if( !prev_contacts[i] ){
+ v3f co;
+ m4x3_mulv( localplayer.rb.to_world, wheels[i].pos, co );
+ player__networked_sfx( k_player_subsystem_skate, 32,
+ k_player_skate_soundeffect_tap,
+ localplayer.rb.co, 0.75f );
+ }
+ }
+ }
+
+ if( contact_count ){
+ state->activity = k_skate_activity_ground;
+ state->gravity_bias = k_gravity;
+ v3_normalize( player_skate.surface_picture );
+
+ skate_apply_friction_model();
+ skate_weight_distribute();
+ }
+ else{
+ if( state->activity > k_skate_activity_air_to_grind )
+ state->activity = k_skate_activity_air;
+
+ v3_zero( player_skate.weight_distribution );
+ skate_apply_air_model();
+ }
+
+grinding:;
+
+ if( state->activity == k_skate_activity_grind_back50 )
+ wheels[1].state = k_collider_state_disabled;
+ if( state->activity == k_skate_activity_grind_front50 )
+ wheels[0].state = k_collider_state_disabled;
+ if( state->activity == k_skate_activity_grind_5050 ){
+ wheels[0].state = k_collider_state_disabled;
+ wheels[1].state = k_collider_state_disabled;
+ }
+
+ /* all activities */
+ skate_apply_steering_model();
+ skate_adjust_up_direction();
+ skate_apply_cog_model();
+ skate_apply_jump_model();
+ skate_apply_handplant_model();
+ skate_apply_grab_model();
+ skate_apply_trick_model();
+ skate_apply_pump_model();
+
+ ent_tornado_debug();
+ v3f a;
+ ent_tornado_forces( localplayer.rb.co, localplayer.rb.v, a );
+ v3_muladds( localplayer.rb.v, a, vg.time_fixed_delta, localplayer.rb.v );
+
+begin_collision:;
+
+ /*
+ * Phase 0: Continous collision detection
+ * --------------------------------------------------------------------------
+ */
+
+ v3f head_wp0, head_wp1, start_co;
+ m4x3_mulv( localplayer.rb.to_world, state->head_position, head_wp0 );
+ v3_copy( localplayer.rb.co, start_co );
+
+ /* calculate transform one step into future */
+ v3f future_co;
+ v4f future_q;
+ v3_muladds( localplayer.rb.co, localplayer.rb.v, player_skate.substep,
+ future_co );
+
+ if( v3_length2( localplayer.rb.w ) > 0.0f ){
+ v4f rotation;
+ v3f axis;
+ v3_copy( localplayer.rb.w, axis );
+
+ float mag = v3_length( axis );
+ v3_divs( axis, mag, axis );
+ q_axis_angle( rotation, axis, mag*player_skate.substep );
+ q_mul( rotation, localplayer.rb.q, future_q );
+ q_normalize( future_q );
+ }
+ else
+ v4_copy( localplayer.rb.q, future_q );
+
+ v3f future_cg, current_cg, cg_offset;
+ q_mulv( localplayer.rb.q, player_skate.weight_distribution, current_cg );
+ q_mulv( future_q, player_skate.weight_distribution, future_cg );
+ v3_sub( future_cg, current_cg, cg_offset );
+
+ /* calculate the minimum time we can move */
+ float max_time = player_skate.substep;
+
+ for( int i=0; i<k_wheel_count; i++ ){
+ if( wheels[i].state == k_collider_state_disabled )
+ continue;
+
+ v3f current, future, r_cg;
+
+ q_mulv( future_q, wheels[i].pos, future );
+ v3_add( future, future_co, future );
+ v3_add( cg_offset, future, future );
+
+ q_mulv( localplayer.rb.q, wheels[i].pos, current );
+ v3_add( current, localplayer.rb.co, current );
+
+ float t;
+ v3f n;
+
+ float cast_radius = wheels[i].radius - k_penetration_slop * 2.0f;
+ if( spherecast_world( world, current, future, cast_radius, &t, n,
+ k_material_flag_walking ) != -1)
+ max_time = vg_minf( max_time, t * player_skate.substep );
+ }
+
+ /* clamp to a fraction of delta, to prevent locking */
+ float rate_lock = substep_count;
+ rate_lock *= vg.time_fixed_delta * 0.1f;
+ rate_lock *= rate_lock;
+
+ max_time = vg_maxf( max_time, rate_lock );
+ player_skate.substep_delta = max_time;
+
+ /* integrate */
+ v3_muladds( localplayer.rb.co, localplayer.rb.v,
+ player_skate.substep_delta, localplayer.rb.co );
+ if( v3_length2( localplayer.rb.w ) > 0.0f ){
+ v4f rotation;
+ v3f axis;
+ v3_copy( localplayer.rb.w, axis );
+
+ float mag = v3_length( axis );
+ v3_divs( axis, mag, axis );
+ q_axis_angle( rotation, axis, mag*player_skate.substep_delta );
+ q_mul( rotation, localplayer.rb.q, localplayer.rb.q );
+ q_normalize( localplayer.rb.q );
+
+ q_mulv( localplayer.rb.q, player_skate.weight_distribution, future_cg );
+ v3_sub( current_cg, future_cg, cg_offset );
+ v3_add( localplayer.rb.co, cg_offset, localplayer.rb.co );
+ }
+
+ rb_update_matrices( &localplayer.rb );
+ localplayer.rb.v[1] += -state->gravity_bias * player_skate.substep_delta;
+
+ player_skate.substep -= player_skate.substep_delta;
+
+ rb_ct manifold[128];
+ int manifold_len = 0;
+ /*
+ * Phase -1: head detection
+ * --------------------------------------------------------------------------
+ */
+ m4x3_mulv( localplayer.rb.to_world, state->head_position, head_wp1 );
+
+ float t;
+ v3f n;
+ if( (v3_dist2( head_wp0, head_wp1 ) > 0.001f) &&
+ (spherecast_world( world, head_wp0, head_wp1, 0.2f, &t, n,
+ k_material_flag_walking ) != -1) )
+ {
+ v3_lerp( start_co, localplayer.rb.co, t, localplayer.rb.co );
+ rb_update_matrices( &localplayer.rb );
+
+ vg_info( "player fell of due to hitting head\n" );
+ player__dead_transition( k_player_die_type_head );
+ return;
+ }
+
+ /*
+ * Phase 1: Regular collision detection
+ * --------------------------------------------------------------------------
+ */
+
+ for( int i=0; i<k_wheel_count; i++ ){
+ if( wheels[i].state == k_collider_state_disabled )
+ continue;
+
+ m4x3f mtx;
+ m3x3_identity( mtx );
+ m4x3_mulv( localplayer.rb.to_world, wheels[i].pos, mtx[3] );
+
+ rb_ct *man = &manifold[ manifold_len ];
+
+ int l = skate_collide_smooth( mtx, wheels[i].radius, man );
+ if( l )
+ wheels[i].state = k_collider_state_colliding;
+
+ manifold_len += l;
+ }
+
+ float grind_radius = k_board_radius * 0.75f;
+ rb_capsule capsule = { .h = (k_board_length+0.2f)*2.0f,
+ .r = grind_radius };
+ m4x3f mtx;
+ v3_muls( localplayer.rb.to_world[0], 1.0f, mtx[0] );
+ v3_muls( localplayer.rb.to_world[2], -1.0f, mtx[1] );
+ v3_muls( localplayer.rb.to_world[1], 1.0f, mtx[2] );
+ v3_muladds( localplayer.rb.to_world[3], localplayer.rb.to_world[1],
+ grind_radius + k_board_radius*0.25f+state->slap, mtx[3] );
+
+ rb_ct *cman = &manifold[manifold_len];
+
+ int l = rb_capsule__scene( mtx, &capsule, NULL, world->geo_bh,
+ cman, k_material_flag_walking );
+
+ /* weld joints */
+ for( int i=0; i<l; i ++ )
+ cman[l].type = k_contact_type_edge;
+ rb_manifold_filter_joint_edges( cman, l, 0.03f );
+ l = rb_manifold_apply_filtered( cman, l );
+
+ manifold_len += l;
+ vg_line_capsule( mtx, capsule.r, capsule.h, VG__WHITE );
+
+ /* add limits */
+ if( state->activity >= k_skate_activity_grind_any ){
+ for( int i=0; i<player_skate.limit_count; i++ ){
+ struct grind_limit *limit = &player_skate.limits[i];
+ rb_ct *ct = &manifold[ manifold_len ++ ];
+ m4x3_mulv( localplayer.rb.to_world, limit->ra, ct->co );
+ m3x3_mulv( localplayer.rb.to_world, limit->n, ct->n );
+ ct->p = limit->p;
+ ct->type = k_contact_type_default;
+ }
+ }
+
+ /*
+ * Phase 3: Dynamics
+ * --------------------------------------------------------------------------
+ */
+
+
+ v3f world_cog;
+ m4x3_mulv( localplayer.rb.to_world,
+ player_skate.weight_distribution, world_cog );
+ vg_line_point( world_cog, 0.02f, VG__BLACK );
+
+ for( int i=0; i<manifold_len; i ++ ){
+ rb_prepare_contact( &manifold[i], player_skate.substep_delta );
+ rb_debug_contact( &manifold[i] );
+ }
+
+ /* yes, we are currently rebuilding mass matrices every frame. too bad! */
+ v3f extent = { k_board_width*10.0f, 0.1f, k_board_length };
+ float ex2 = k_board_interia*extent[0]*extent[0],
+ ey2 = k_board_interia*extent[1]*extent[1],
+ ez2 = k_board_interia*extent[2]*extent[2];
+
+ float mass = 2.0f * (extent[0]*extent[1]*extent[2]);
+ float inv_mass = 1.0f/mass;
+
+ v3f I;
+ I[0] = ((1.0f/12.0f) * mass * (ey2+ez2));
+ I[1] = ((1.0f/12.0f) * mass * (ex2+ez2));
+ I[2] = ((1.0f/12.0f) * mass * (ex2+ey2));
+
+ m3x3f iI;
+ m3x3_identity( iI );
+ iI[0][0] = I[0];
+ iI[1][1] = I[1];
+ iI[2][2] = I[2];
+ m3x3_inv( iI, iI );
+
+ m3x3f iIw;
+ m3x3_mul( iI, localplayer.rb.to_local, iIw );
+ m3x3_mul( localplayer.rb.to_world, iIw, iIw );
+
+ for( int j=0; j<10; j++ ){
+ for( int i=0; i<manifold_len; i++ ){
+ /*
+ * regular dance; calculate velocity & total mass, apply impulse.
+ */
+
+ rb_ct *ct = &manifold[i];
+
+ v3f rv, delta;
+ v3_sub( ct->co, world_cog, delta );
+ v3_cross( localplayer.rb.w, delta, rv );
+ v3_add( localplayer.rb.v, rv, rv );
+
+ v3f raCn;
+ v3_cross( delta, ct->n, raCn );
+
+ v3f raCnI, rbCnI;
+ m3x3_mulv( iIw, raCn, raCnI );
+
+ float normal_mass = 1.0f / (inv_mass + v3_dot(raCn,raCnI)),
+ vn = v3_dot( rv, ct->n ),
+ lambda = normal_mass * ( -vn );
+
+ float temp = ct->norm_impulse;
+ ct->norm_impulse = vg_maxf( temp + lambda, 0.0f );
+ lambda = ct->norm_impulse - temp;
+
+ v3f impulse;
+ v3_muls( ct->n, lambda, impulse );
+
+ v3_muladds( normal_total, impulse, inv_mass, normal_total );
+ v3_muladds( localplayer.rb.v, impulse, inv_mass, localplayer.rb.v );
+ v3_cross( delta, impulse, impulse );
+ m3x3_mulv( iIw, impulse, impulse );
+ v3_add( impulse, localplayer.rb.w, localplayer.rb.w );
+
+ v3_cross( localplayer.rb.w, delta, rv );
+ v3_add( localplayer.rb.v, rv, rv );
+ vn = v3_dot( rv, ct->n );
+ }
+ }
+
+ v3f dt;
+ rb_depenetrate( manifold, manifold_len, dt );
+ v3_add( dt, localplayer.rb.co, localplayer.rb.co );
+ rb_update_matrices( &localplayer.rb );
+
+ substep_count ++;
+
+ if( player_skate.substep >= 0.0001f )
+ goto begin_collision; /* again! */
+
+ /*
+ * End of collision and dynamics routine
+ * --------------------------------------------------------------------------
+ */
+
+ f32 nforce = v3_length(normal_total);
+ if( nforce > 4.0f ){
+ if( nforce > 17.6f ){
+ vg_info( "player fell off due to hitting ground too hard\n" );
+ v3_muladds( localplayer.rb.v, normal_total, -1.0f, localplayer.rb.v );
+ player__dead_transition( k_player_die_type_feet );
+ return;
+ }
+
+ f32 amt = k_cam_punch;
+ if( localplayer.cam_control.camera_mode == k_cam_firstperson ){
+ amt *= 0.25f;
+ }
+
+ v3_muladds( localplayer.cam_land_punch_v, normal_total, amt,
+ localplayer.cam_land_punch_v );
+ }
+
+ player_skate.surface = k_surface_prop_concrete;
+
+ for( int i=0; i<manifold_len; i++ ){
+ rb_ct *ct = &manifold[i];
+ struct world_surface *surf = world_contact_surface( world, ct );
+
+ if( surf->info.surface_prop > player_skate.surface )
+ player_skate.surface = surf->info.surface_prop;
+ }
+
+ for( int i=0; i<k_wheel_count; i++ ){
+ m4x3f mtx;
+ m3x3_copy( localplayer.rb.to_world, mtx );
+ m4x3_mulv( localplayer.rb.to_world, wheels[i].pos, mtx[3] );
+ vg_line_sphere( mtx, wheels[i].radius,
+ (u32[]){ VG__WHITE, VG__BLACK,
+ wheels[i].colour }[ wheels[i].state ]);
+ }
+
+ skate_integrate();
+ vg_line_point( state->cog, 0.02f, VG__WHITE );
+
+ u32 id = world_intersect_gates( world, localplayer.rb.co, state->prev_pos );
+
+ if( id ){
+ ent_gate *gate = mdl_arritm( &world->ent_gate, mdl_entity_id_id(id) );
+
+ m4x3_mulv( gate->transport, localplayer.rb.co, localplayer.rb.co );
+ m3x3_mulv( gate->transport, localplayer.rb.v, localplayer.rb.v );
+ m4x3_mulv( gate->transport, state->cog, state->cog );
+ m3x3_mulv( gate->transport, state->cog_v, state->cog_v );
+ m3x3_mulv( gate->transport, state->throw_v, state->throw_v );
+ m3x3_mulv( gate->transport, state->head_position,
+ state->head_position );
+ m3x3_mulv( gate->transport, state->up_dir, state->up_dir );