move rigidbody to vg
[carveJwlIkooP6JGAAIwe30JlM.git] / player_skate.c
index 191c708ad1a160340e3040846a34bcbafc21c0b8..c6ed4e073b6b65233ba98e10879c46ccef72e400 100644 (file)
@@ -8,9 +8,13 @@
 #include "ent_skateshop.h"
 #include "addon.h"
 
+#include "ent_tornado.c"
+#include "vg/vg_rigidbody.h"
+#include "scene_rigidbody.h"
+
 static void player__skate_bind(void){
    struct skeleton *sk = &localplayer.skeleton;
-   rb_update_transform( &localplayer.rb );
+   rb_update_matrices( &localplayer.rb );
 
    struct { struct skeleton_anim **anim; const char *name; }
    bindings[] = {
@@ -59,11 +63,11 @@ static void player__skate_kill_audio(void){
  * Does collision detection on a sphere vs world, and applies some smoothing
  * filters to the manifold afterwards
  */
-static int skate_collide_smooth( m4x3f mtx, rb_sphere *sphere, rb_ct *man ){
+static int skate_collide_smooth( m4x3f mtx, f32 r, rb_ct *man ){
    world_instance *world = world_current_instance();
 
    int len = 0;
-   len = rb_sphere__scene( mtx, sphere, NULL, &world->rb_geo.inf.scene, man,
+   len = rb_sphere__scene( mtx, r, NULL, world->geo_bh, man,
                            k_material_flag_walking );
 
    for( int i=0; i<len; i++ ){
@@ -403,18 +407,29 @@ static void player__approximate_best_trajectory(void){
       inf->gravity = gravity;
       v3_copy( launch_v, inf->v );
 
+      /* initial conditions */
+      v3f v;
+      v3_copy( launch_v, v );
+      v3_copy( launch_co, co1 );
+
       for( int i=1; i<=50; i++ ){
-         float t = (float)i * k_trace_delta;
+         f32 t = (f32)i * k_trace_delta;
 
-         v3_muls( launch_v, t, co1 );
-         co1[1] += -0.5f * gravity * t*t;
-         v3_add( launch_co, co1, co1 );
+         /* integrate forces */
+         v3f a;
+         ent_tornado_forces( co1, v, a );
+         a[1] -= gravity;
 
-         float launch_vy = launch_v[1];
+         /* position */
+         v3_muladds( co1, v, k_trace_delta, co1 );
+         v3_muladds( co1, a, 0.5f*k_trace_delta*k_trace_delta, co1 );
+
+         /* velocity */
+         v3_muladds( v, a, k_trace_delta, v );
 
          int search_for_grind = 1;
          if( grind_located ) search_for_grind = 0;
-         if( launch_vy - gravity*t > 0.0f ) search_for_grind = 0;
+         if( v[1] > 0.0f ) search_for_grind = 0;
 
          /* REFACTOR */
 
@@ -448,13 +463,9 @@ static void player__approximate_best_trajectory(void){
          }
 
          if( search_for_grind ){
-            v3f ve;
-            v3_copy( launch_v, ve );
-            ve[1] += -gravity * t;
-
-            if( skate_grind_scansq( closest, ve, 0.5f, &grind ) ){
+            if( skate_grind_scansq( closest, v, 0.5f, &grind ) ){
                /* check alignment */
-               v2f v0 = { ve[0], ve[2] },
+               v2f v0 = { v[0], v[2] },
                    v1 = { grind.dir[0], grind.dir[2] };
 
                v2_normalize( v0 );
@@ -467,7 +478,7 @@ static void player__approximate_best_trajectory(void){
                   a_min = cosf( VG_PIf * 0.05f );
 
                /* check speed */
-               if( (fabsf(v3_dot( ve, grind.dir ))>=k_grind_axel_min_vel) &&
+               if( (fabsf(v3_dot( v, grind.dir ))>=k_grind_axel_min_vel) &&
                    (a >= a_min) && 
                    (fabsf(grind.dir[1]) < 0.70710678118654752f))
                {
@@ -509,12 +520,7 @@ static void player__approximate_best_trajectory(void){
                world_tri_index_surface( trace_world, tri[0] );
 
             inf->type = k_prediction_land;
-
-            v3f ve;
-            v3_copy( launch_v, ve );
-            ve[1] += -gravity * t;
-
-            inf->score = -v3_dot( ve, inf->n );
+            inf->score = -v3_dot( v, inf->n );
             inf->land_dist = t + k_trace_delta * t1;
 
             /* Bias prediction towords ramps */
@@ -758,7 +764,6 @@ static void skate_apply_trick_model(void){
       if( (v3_length2(state->trick_vel) >= 0.0001f ) &&
           state->trick_time > 0.2f)
       {
-         player__skate_kill_audio();
          player__dead_transition( k_player_die_type_feet );
       }
 
@@ -902,10 +907,20 @@ static void skate_apply_friction_model(void){
    f32 lat = k_friction_lat;
 
    if( fabsf(axis_state(k_sraxis_skid)) > 0.1f ){
-      lat = k_friction_lat * 2.0f;
+      if( (player_skate.surface == k_surface_prop_snow) ||
+          (player_skate.surface == k_surface_prop_sand) ){
+         lat *= 8.0f;
+      }
+      else
+         lat *= 1.5f;
    }
 
-   vel[0] += vg_cfrictf( vel[0], k_friction_lat * k_rb_delta );
+   if( player_skate.surface == k_surface_prop_snow )
+      lat *= 0.5f;
+   else if( player_skate.surface == k_surface_prop_sand )
+      lat *= 0.6f;
+
+   vel[0] += vg_cfrictf( vel[0], lat * k_rb_delta );
    vel[2] += vg_cfrictf( vel[2], k_friction_resistance * k_rb_delta );
 
    /* Pushing additive force */
@@ -1133,7 +1148,7 @@ static void skate_integrate(void){
                localplayer.rb.w );
 
    state->flip_time += state->flip_rate * k_rb_delta;
-   rb_update_transform( &localplayer.rb );
+   rb_update_matrices( &localplayer.rb );
 }
 
 static enum trick_type player_skate_trick_input(void){
@@ -1196,7 +1211,7 @@ static void player__skate_pre_update(void){
       m3x3_q( transfer, qtransfer );
       q_mul( qtransfer, state->store_smoothed, state->smoothed_rotation );
       q_normalize( state->smoothed_rotation );
-      rb_update_transform( &localplayer.rb );
+      rb_update_matrices( &localplayer.rb );
 
       if( end ){
          state->activity = k_skate_activity_air;
@@ -1207,6 +1222,19 @@ static void player__skate_pre_update(void){
    if( button_down(k_srbind_use) && (v3_length2(state->trick_vel) < 0.01f) ){
       localplayer.subsystem = k_player_subsystem_walk;
 
+      if( state->activity <= k_skate_activity_air_to_grind ){
+         localplayer.subsystem = k_player_subsystem_glide;
+
+         v3_copy( localplayer.rb.co, player_glide.rb.co );
+         v4_copy( localplayer.rb.q,  player_glide.rb.q );
+         v3_copy( localplayer.rb.v,  player_glide.rb.v );
+         v3_copy( localplayer.rb.w,  player_glide.rb.w );
+         rb_update_matrices( &player_glide.rb );
+
+         player__begin_holdout( (v3f){0,0,0} );
+         return;
+      }
+
       v3f angles;
       v3_copy( localplayer.cam.angles, localplayer.angles );
       localplayer.angles[2] = 0.0f;
@@ -1220,9 +1248,9 @@ static void player__skate_pre_update(void){
                   localplayer.rb.co );
 
       player__begin_holdout( offset );
-      player__skate_kill_audio();
       player__walk_transition( state->activity <= k_skate_activity_air_to_grind?
                                0: 1, state->trick_euler[0] );
+
       return;
    }
 
@@ -1262,45 +1290,26 @@ static void player__skate_pre_update(void){
       state->trick_type = k_trick_type_none;
 }
 
-static void player__skate_post_update(void){
-   struct player_skate_state *state = &player_skate.state;
-
-   for( int i=0; i<player_skate.possible_jump_count; i++ ){
-      jump_info *jump = &player_skate.possible_jumps[i];
-
-      if( jump->log_length == 0 ){
-         vg_fatal_error( "assert: jump->log_length == 0\n" );
-      }
-      
-      for( int j=0; j<jump->log_length - 1; j ++ ){
-         float brightness = jump->score*jump->score*jump->score;
-         v3f p1;
-         v3_lerp( jump->log[j], jump->log[j+1], brightness, p1 );
-         vg_line( jump->log[j], p1, jump->colour );
-      }
-
-      vg_line_cross( jump->log[jump->log_length-1], jump->colour, 0.25f );
-
-      v3f p1;
-      v3_add( jump->log[jump->log_length-1], jump->n, p1 );
-      vg_line( jump->log[jump->log_length-1], p1, 0xffffffff );
-
-      vg_line_point( jump->apex, 0.02f, 0xffffffff );
-   }
-
+static void player__skate_comp_audio( void *_animator ){
+   struct player_skate_animator *animator = _animator;
    audio_lock();
 
-   float air   = ((state->activity <= k_skate_activity_air_to_grind) ||
-                  (state->activity == k_skate_activity_handplant))? 1.0f: 0.0f,
-         speed = v3_length( localplayer.rb.v ),
-         attn  = vg_minf( 1.0f, speed*0.1f ),
-         slide = vg_clampf( fabsf(state->slip), 0.0f, 1.0f );
+   f32 air   = ((animator->activity <= k_skate_activity_air_to_grind) ||
+                (animator->activity == k_skate_activity_handplant))? 1.0f: 0.0f,
+       speed = v3_length( animator->root_v ),
+       attn  = vg_minf( 1.0f, speed*0.1f ),
+       slide = animator->slide;
 
-   if( state->activity >= k_skate_activity_grind_any ){
+   if( animator->activity >= k_skate_activity_grind_any )
       slide = 0.0f;
+
+   f32 gate = skaterift.time_rate;
+
+   if( skaterift.activity == k_skaterift_replay ){
+      gate = vg_minf( 1.0f, fabsf(skaterift.track_velocity) );
    }
 
-   f32 gate        = skaterift.time_rate,
+   f32
        vol_main    = sqrtf( (1.0f-air)*attn*(1.0f-slide) * 0.4f ) * gate,
        vol_air     = sqrtf(       air *attn * 0.5f )              * gate,
        vol_slide   = sqrtf( (1.0f-air)*attn*slide * 0.25f )       * gate;
@@ -1325,8 +1334,8 @@ static void player__skate_post_update(void){
    float sidechain_amt = 0.0f,
          hz            = vg_maxf( speed * 2.0f, 2.0f );
 
-   if( (player_skate.surface == k_surface_prop_tiles) &&
-       (state->activity < k_skate_activity_grind_any) )
+   if( (animator->surface == k_surface_prop_tiles) &&
+       (animator->activity < k_skate_activity_grind_any) )
       sidechain_amt = 1.0f;
    else
       sidechain_amt = 0.0f;
@@ -1341,29 +1350,33 @@ static void player__skate_post_update(void){
    else{
       int sample_type = k_skate_sample_concrete;
 
-      if( state->activity == k_skate_activity_grind_5050 ){
-         if( player_skate.surface == k_surface_prop_metal )
+      if( animator->activity == k_skate_activity_grind_5050 ){
+         if( animator->surface == k_surface_prop_metal )
             sample_type = k_skate_sample_metal_scrape_generic;
          else
             sample_type = k_skate_sample_concrete_scrape_metal;
       }
-      else if( (state->activity == k_skate_activity_grind_back50) ||
-               (state->activity == k_skate_activity_grind_front50) )
+      else if( (animator->activity == k_skate_activity_grind_back50) ||
+               (animator->activity == k_skate_activity_grind_front50) )
       {
-         if( player_skate.surface == k_surface_prop_metal ){
+         if( animator->surface == k_surface_prop_metal ){
             sample_type = k_skate_sample_metal_scrape_generic;
          }
          else{
+#if 0
             float a = v3_dot( localplayer.rb.to_world[2], 
                               player_skate.grind_dir );
             if( fabsf(a) > 0.70710678118654752f )
                sample_type = k_skate_sample_concrete_scrape_wood;
             else 
                sample_type = k_skate_sample_concrete_scrape_metal;
+#endif
+
+            sample_type = k_skate_sample_concrete_scrape_wood;
          }
       }
-      else if( state->activity == k_skate_activity_grind_boardslide ){
-         if( player_skate.surface == k_surface_prop_metal )
+      else if( animator->activity == k_skate_activity_grind_boardslide ){
+         if( animator->surface == k_surface_prop_metal )
             sample_type = k_skate_sample_metal_scrape_generic;
          else
             sample_type = k_skate_sample_concrete_scrape_wood;
@@ -1392,7 +1405,7 @@ static void player__skate_post_update(void){
    if( player_skate.aud_main ){
       player_skate.aud_main->colour = 0x00103efe;
       audio_channel_set_spacial( player_skate.aud_main, 
-                                 localplayer.rb.co, 40.0f );
+                                 animator->root_co, 40.0f );
       //audio_channel_slope_volume( player_skate.aud_main, 0.05f, vol_main );
       audio_channel_edit_volume( player_skate.aud_main, vol_main, 1 );
       audio_channel_sidechain_lfo( player_skate.aud_main, 0, sidechain_amt );
@@ -1404,7 +1417,7 @@ static void player__skate_post_update(void){
    if( player_skate.aud_slide ){
       player_skate.aud_slide->colour = 0x00103efe;
       audio_channel_set_spacial( player_skate.aud_slide, 
-                                 localplayer.rb.co, 40.0f );
+                                 animator->root_co, 40.0f );
       //audio_channel_slope_volume( player_skate.aud_slide, 0.05f, vol_slide );
       audio_channel_edit_volume( player_skate.aud_slide, vol_slide, 1 );
       audio_channel_sidechain_lfo( player_skate.aud_slide, 0, sidechain_amt );
@@ -1413,7 +1426,7 @@ static void player__skate_post_update(void){
    if( player_skate.aud_air ){
       player_skate.aud_air->colour = 0x00103efe;
       audio_channel_set_spacial( player_skate.aud_air, 
-                                 localplayer.rb.co, 40.0f );
+                                 animator->root_co, 40.0f );
       //audio_channel_slope_volume( player_skate.aud_air, 0.05f, vol_air );
       audio_channel_edit_volume( player_skate.aud_air, vol_air, 1 );
    }
@@ -1421,6 +1434,33 @@ static void player__skate_post_update(void){
    audio_unlock();
 }
 
+static void player__skate_post_update(void){
+   struct player_skate_state *state = &player_skate.state;
+
+   for( int i=0; i<player_skate.possible_jump_count; i++ ){
+      jump_info *jump = &player_skate.possible_jumps[i];
+
+      if( jump->log_length == 0 ){
+         vg_fatal_error( "assert: jump->log_length == 0\n" );
+      }
+      
+      for( int j=0; j<jump->log_length - 1; j ++ ){
+         float brightness = jump->score*jump->score*jump->score;
+         v3f p1;
+         v3_lerp( jump->log[j], jump->log[j+1], brightness, p1 );
+         vg_line( jump->log[j], p1, jump->colour );
+      }
+
+      vg_line_cross( jump->log[jump->log_length-1], jump->colour, 0.25f );
+
+      v3f p1;
+      v3_add( jump->log[jump->log_length-1], jump->n, p1 );
+      vg_line( jump->log[jump->log_length-1], p1, 0xffffffff );
+
+      vg_line_point( jump->apex, 0.02f, 0xffffffff );
+   }
+}
+
 /*
  * truck alignment model at ra(local)
  * returns 1 if valid surface:
@@ -2177,7 +2217,6 @@ static void player__skate_update(void){
          player__networked_sfx( k_player_subsystem_walk, 32, 
                                 k_player_walk_soundeffect_splash,
                                 localplayer.rb.co, 1.0f );
-         player__skate_kill_audio();
          player__dead_transition( k_player_die_type_generic );
          return;
       }
@@ -2343,6 +2382,11 @@ grinding:;
    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, k_rb_delta, localplayer.rb.v );
+
 begin_collision:;
 
    /*
@@ -2431,7 +2475,7 @@ begin_collision:;
       v3_add( localplayer.rb.co, cg_offset, localplayer.rb.co );
    }
 
-   rb_update_transform( &localplayer.rb );
+   rb_update_matrices( &localplayer.rb );
    localplayer.rb.v[1] += -state->gravity_bias * player_skate.substep_delta;
 
    player_skate.substep -= player_skate.substep_delta;
@@ -2451,9 +2495,8 @@ begin_collision:;
                           k_material_flag_walking ) != -1) )
    {
       v3_lerp( start_co, localplayer.rb.co, t, localplayer.rb.co );
-      rb_update_transform( &localplayer.rb );
+      rb_update_matrices( &localplayer.rb );
 
-      player__skate_kill_audio();
       player__dead_transition( k_player_die_type_head );
       return;
    }
@@ -2470,12 +2513,10 @@ begin_collision:;
       m4x3f mtx;
       m3x3_identity( mtx );
       m4x3_mulv( localplayer.rb.to_world, wheels[i].pos, mtx[3] );
-      
-      rb_sphere collider = { .radius = wheels[i].radius };
 
       rb_ct *man = &manifold[ manifold_len ];
 
-      int l = skate_collide_smooth( mtx, &collider, man );
+      int l = skate_collide_smooth( mtx, wheels[i].radius, man );
       if( l )
          wheels[i].state = k_collider_state_colliding;
 
@@ -2483,8 +2524,8 @@ begin_collision:;
    }
 
    float grind_radius = k_board_radius * 0.75f;
-   rb_capsule capsule = { .height = (k_board_length+0.2f)*2.0f, 
-                          .radius=grind_radius };
+   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] );
@@ -2494,7 +2535,7 @@ begin_collision:;
 
    rb_ct *cman = &manifold[manifold_len];
 
-   int l = rb_capsule__scene( mtx, &capsule, NULL, &world->rb_geo.inf.scene,
+   int l = rb_capsule__scene( mtx, &capsule, NULL, world->geo_bh,
                               cman, k_material_flag_walking );
 
    /* weld joints */
@@ -2506,7 +2547,7 @@ begin_collision:;
    manifold_len += l;
 
    if( vg_lines.draw )
-      vg_line_capsule( mtx, capsule.radius, capsule.height, VG__WHITE );
+      vg_line_capsule( mtx, capsule.r, capsule.h, VG__WHITE );
 
    /* add limits */
    if( state->activity >= k_skate_activity_grind_any ){
@@ -2567,7 +2608,7 @@ begin_collision:;
           * regular dance; calculate velocity & total mass, apply impulse.
           */
 
-         struct contact *ct = &manifold[i];
+         rb_ct *ct = &manifold[i];
          
          v3f rv, delta;
          v3_sub( ct->co, world_cog, delta ); 
@@ -2606,7 +2647,7 @@ begin_collision:;
    v3f dt;
    rb_depenetrate( manifold, manifold_len, dt );
    v3_add( dt, localplayer.rb.co, localplayer.rb.co );
-   rb_update_transform( &localplayer.rb );
+   rb_update_matrices( &localplayer.rb );
 
    substep_count ++;
 
@@ -2623,7 +2664,6 @@ begin_collision:;
       if( nforce > 17.6f ){
          v3_muladds( localplayer.rb.v, normal_total, -1.0f, localplayer.rb.v );
          player__dead_transition( k_player_die_type_feet );
-         player__skate_kill_audio();
          return;
       }
 
@@ -2677,7 +2717,9 @@ begin_collision:;
       q_mul( transport_rotation, localplayer.rb.q, localplayer.rb.q );
       q_mul( transport_rotation, state->smoothed_rotation,
                                  state->smoothed_rotation );
-      rb_update_transform( &localplayer.rb );
+      q_normalize( localplayer.rb.q );
+      q_normalize( state->smoothed_rotation );
+      rb_update_matrices( &localplayer.rb );
       player__pass_gate( id );
    }
 
@@ -2896,6 +2938,7 @@ static void player__skate_animate(void){
    animator->grind_balance = vg_lerpf( animator->grind_balance, grind_frame, 
                                 5.0f*vg.time_delta );
    animator->activity = state->activity;
+   animator->surface = player_skate.surface;
 
    /* pushing */
    animator->push_time = vg.time - state->start_push;
@@ -3399,27 +3442,37 @@ static void player__skate_pose( void *_animator, player_pose *pose ){
 static void player__skate_effects( void *_animator, m4x3f *final_mtx,
                                    struct player_board *board,
                                    struct player_effects_data *effect_data ){
-
    struct skeleton *sk = &localplayer.skeleton;
    struct player_skate_animator *animator = _animator;
 
-   if( animator->grind > 0.5f ){
-      v3f vp0, vp1, vpc;
-      if( board ){
-         v3_copy((v3f){0.0f,0.02f, board->truck_positions[0][2]}, vp1 );
-         v3_copy((v3f){0.0f,0.02f, board->truck_positions[1][2]}, vp0 );
-      }
-      else{
-         v3_zero( vp0 );
-         v3_zero( vp1 );
-      }
+   v3f vp0, vp1, vpc;
+   if( board ){
+      v3_copy((v3f){0.0f,0.02f, board->truck_positions[0][2]}, vp1 );
+      v3_copy((v3f){0.0f,0.02f, board->truck_positions[1][2]}, vp0 );
+   }
+   else{
+      v3_zero( vp0 );
+      v3_zero( vp1 );
+   }
+
+   v3f *board_mtx = final_mtx[ localplayer.id_board ];
+   m4x3_mulv( board_mtx, vp0, vp0 );
+   m4x3_mulv( board_mtx, vp1, vp1 );
+   v3_add( vp0, vp1, vpc );
+   v3_muls( vpc, 0.5f, vpc );
+
+   if( animator->surface == k_surface_prop_sand ){
+      if( (animator->slide>0.4f) && (v3_length2(animator->root_v)>4.0f*4.0f) ){
+         v3f v, co;
+         v3_muls( animator->root_v, 0.5f, v );
+         v3_lerp( vp0, vp1, vg_randf64(&vg.rand), co );
 
-      v3f *board_mtx = final_mtx[ localplayer.id_board ];
-      m4x3_mulv( board_mtx, vp0, vp0 );
-      m4x3_mulv( board_mtx, vp1, vp1 );
-      v3_add( vp0, vp1, vpc );
-      v3_muls( vpc, 0.5f, vpc );
+         effect_data->sand.colour = 0xff8ec4e6;
+         effect_spark_apply( &effect_data->sand, co, v, vg.time_delta * 8.0 );
+      }
+   }
 
+   if( animator->grind > 0.5f ){
       int back = 0, front = 0, mid = 0;
 
       if( animator->activity == k_skate_activity_grind_5050 ){
@@ -3561,6 +3614,7 @@ static void player__skate_animator_exchange( bitpack_ctx *ctx, void *data ){
 
    bitpack_qf32( ctx, 16,  0.0f, 120.0f, &animator->push_time );
    bitpack_qf32( ctx, 16,  0.0f, 120.0f, &animator->jump_time );
+   bitpack_qf32( ctx, 16,  0.0f, 4.0f, &animator->handplant_t );
    bitpack_qv3f( ctx, 16, -100.0f, 100.0f, animator->root_v );
    bitpack_bytes( ctx, 1, &animator->activity );
 }