i forgo
[carveJwlIkooP6JGAAIwe30JlM.git] / character.h
index f5a9d676ab710686aa7cefcc9807607d1bbf2eb1..f161f7d210214fabe0e07befa0aec976518477c7 100644 (file)
@@ -54,6 +54,10 @@ SHADER_DEFINE( shader_player,
 )
 
 #define FOREACH_PART(FN) \
+   FN( foot_l ) \
+   FN( foot_r ) \
+   FN( sock_l ) \
+   FN( sock_r ) \
    FN( body0 ) \
    FN( body1 ) \
    FN( neck ) \
@@ -66,10 +70,8 @@ SHADER_DEFINE( shader_player,
    FN( hand_r ) \
    FN( leg_l0 ) \
    FN( leg_l1 ) \
-   FN( foot_l ) \
    FN( leg_r0 ) \
    FN( leg_r1 ) \
-   FN( foot_r ) \
    FN( wf ) \
    FN( wb ) \
    FN( board ) \
@@ -119,6 +121,8 @@ struct character
 
    v3f ground_normal;   /* Feet will be aligned to this */
    m4x3f mroot;
+
+   int shoes[2];
 };
 
 static void character_offset( struct character *ch, enum character_part parent,
@@ -554,11 +558,27 @@ static void character_draw( struct character *ch, float temp )
 
    mesh_bind( &ch->mesh );
 
-   for( int i=0; i<PART_COUNT; i++ )
+   for( int i=4; i<PART_COUNT; i++ )
    {
       glUniformMatrix4x3fv( kuMdl, 1, GL_FALSE, (float *)ch->matrices[i] );
       submodel_draw( &ch->parts[i] );
    }
+
+   for( int i=0; i<2; i++ )
+   {
+      if( ch->shoes[i] )
+      {
+         glUniformMatrix4x3fv( kuMdl, 1, GL_FALSE, (float *)ch->matrices[i] );
+         submodel_draw( &ch->parts[i] );
+      }
+      else
+      {
+         glUniformMatrix4x3fv( kuMdl, 1, GL_FALSE, (float *)ch->matrices[i+2] );
+         submodel_draw( &ch->parts[i] );
+         glUniformMatrix4x3fv( kuMdl, 1, GL_FALSE, (float *)ch->matrices[i] );
+         submodel_draw( &ch->parts[i+2] );
+      }
+   }
 }
 
 static void character_shader_register(void)
@@ -591,10 +611,10 @@ struct rd_joint
    min, maj;
 };
 
-static const float k_human_major = 0.9f,
-                   k_human_minor = 0.9f,
-                   k_human_major_max = 1.4f,
-                   k_human_minor_max = 0.4f;
+static const float k_human_major = 0.5f,
+                   k_human_minor = 0.5f,
+                   k_human_major_max = 0.4f,
+                   k_human_minor_max = 0.1f;
 
 #define HUMAN_VERTICAL_DEFAULT                        \
 .min = {                                              \
@@ -622,12 +642,15 @@ static const float k_human_major = 0.9f,
    .spring = k_human_minor, .ang = k_human_minor_max  \
 },                                                    \
 .maj = {                                              \
-   .va = {0.0f,0.0f,-1.0f}, .vb = {0.0f,0.0f,-1.0f},    \
+   .va = {0.0f,0.0f,-1.0f}, .vb = {0.0f,0.0f,-1.0f},  \
    .spring = k_human_major, .ang = k_human_major_max  \
 }
 
 static struct rd_joint rd_joints[] =
 {
+   { .ia = k_chpart_leg_l1, .ib = k_chpart_foot_l, HUMAN_VERTICAL_DEFAULT },
+   { .ia = k_chpart_leg_r1, .ib = k_chpart_foot_r, HUMAN_VERTICAL_DEFAULT },
+
    { .ia = k_chpart_body0, .ib = k_chpart_body1, HUMAN_VERTICAL_DEFAULT },
    { .ia = k_chpart_body1, .ib = k_chpart_neck, HUMAN_VERTICAL_DEFAULT },
    { .ia = k_chpart_neck, .ib = k_chpart_head, HUMAN_VERTICAL_DEFAULT },
@@ -642,9 +665,10 @@ static struct rd_joint rd_joints[] =
 
    { .ia = k_chpart_body1, .ib = k_chpart_arm_r0, HUMAN_ARM_RIGHT },
    { .ia = k_chpart_arm_r0, .ib = k_chpart_arm_r1, HUMAN_ARM_RIGHT },
-   { .ia = k_chpart_arm_r1, .ib = k_chpart_hand_r, HUMAN_ARM_RIGHT },
+   { .ia = k_chpart_arm_r1, .ib = k_chpart_hand_r, HUMAN_ARM_RIGHT }
 };
 
+/* Ragdoll should be in rest pose when calling this function */
 static void character_init_ragdoll_joints( struct character *ch )
 {
    for( int i=0; i<vg_list_size(rd_joints); i++ )
@@ -655,6 +679,12 @@ static void character_init_ragdoll_joints( struct character *ch )
       v3_sub( hinge, ch->ragdoll[joint->ia].co, joint->lca );
       v3_sub( hinge, ch->ragdoll[joint->ib].co, joint->lcb );
    }
+
+   for( int i=0; i<PART_COUNT; i++ )
+   {
+      float *pivot = ch->parts[i].pivot;
+      v3_sub( ch->ragdoll[i].co, pivot, ch->ragdoll[i].delta );
+   }
 }
 
 static void character_init_ragdoll( struct character *ch )
@@ -751,10 +781,18 @@ static void character_init_ragdoll( struct character *ch )
    rbs[k_chpart_foot_r].co[1] -= foot_dims[1] * 0.5f;
    rbs[k_chpart_foot_r].co[0] -= foot_dims[0] * 0.5f;
 
-   character_init_ragdoll_joints( ch );
+   character_rd_box( ch, k_chpart_sock_l, foot_dims );
+   character_rd_box( ch, k_chpart_sock_r, foot_dims );
+   v3_copy( rbs[k_chpart_foot_l].co, rbs[k_chpart_sock_l].co );
+   v3_copy( rbs[k_chpart_foot_r].co, rbs[k_chpart_sock_r].co );
+
+   box_copy( (boxf){{-0.2f,-0.2f,-0.7f},{0.2f,0.2f,0.7f}}, 
+         rbs[k_chpart_board].bbx );
 
    for( int i=0; i<PART_COUNT; i++ )
       rb_init( &ch->ragdoll[i] );
+
+   character_init_ragdoll_joints( ch );
 }
 
 static void character_ragdoll_go( struct character *ch, v3f pos )
@@ -766,16 +804,11 @@ static void character_ragdoll_go( struct character *ch, v3f pos )
 
 static void character_ragdoll_copypose( struct character *ch, v3f v )
 {
-   character_init_ragdoll(ch);
-
    for( int i=0; i<PART_COUNT; i++ )
    {
-      v3f offset;
       rigidbody *rb = &ch->ragdoll[i];
 
-      v3_sub( rb->co, ch->parts[i].pivot, offset );
-
-      m4x3_mulv( ch->matrices[i], offset, rb->co );
+      m4x3_mulv( ch->matrices[i], rb->delta, rb->co );
       m3x3_q( ch->matrices[i], rb->q );
       v3_copy( v, rb->v );
       v3_zero( rb->I );
@@ -783,6 +816,33 @@ static void character_ragdoll_copypose( struct character *ch, v3f v )
 
       rb_update_transform( rb );
    }
+
+   float vel = v3_length(v);
+
+   ch->shoes[0] = 1;
+   ch->shoes[1] = 1;
+}
+
+static void character_mimic_ragdoll( struct character *ch )
+{
+   for( int i=0; i<PART_COUNT; i++ )
+   {
+      rigidbody *rb = &ch->ragdoll[i];
+      v3f *mat = ch->matrices[i];
+      
+      m3x3_copy( rb->to_world, mat );
+      v3f inv_delta;
+      v3_negate( rb->delta, inv_delta );
+      m4x3_mulv( rb->to_world, inv_delta, mat[3] );
+   }
+
+   /* Attach wheels to board */
+   m3x3_copy( ch->matrices[k_chpart_board], ch->matrices[k_chpart_wb] );
+   m3x3_copy( ch->matrices[k_chpart_board], ch->matrices[k_chpart_wf] );
+   m4x3_mulv( ch->matrices[k_chpart_board], ch->offsets[k_chpart_wb],
+                                            ch->matrices[k_chpart_wb][3] );
+   m4x3_mulv( ch->matrices[k_chpart_board], ch->offsets[k_chpart_wf],
+                                            ch->matrices[k_chpart_wf][3] );
 }
 
 static void character_debug_ragdoll( struct character *ch )
@@ -815,6 +875,13 @@ static void character_ragdoll_iter( struct character *ch, scene *sc )
       rb_build_manifold( &ch->ragdoll[i], sc );
    }
 
+   v3f rv;
+
+   float shoe_vel[2];
+   for( int i=0; i<2; i++ )
+      if( ch->shoes[i] )
+         shoe_vel[i] = v3_length( ch->ragdoll[i].v );
+
    for( int i=0; i<20; i++ )
    {
       float const k_springfactor = 1.0f/20.0f;
@@ -839,8 +906,40 @@ static void character_ragdoll_iter( struct character *ch, scene *sc )
       }
    }
 
+   for( int j=0; j<vg_list_size(rd_joints); j++ )
+   {
+      struct rd_joint *joint = &rd_joints[j];
+      rigidbody *rba = &ch->ragdoll[joint->ia],
+                *rbb = &ch->ragdoll[joint->ib];
+      rb_angle_limit_force( rba, joint->min.va, rbb, joint->min.vb,
+                                 joint->min.ang );
+      rb_angle_limit_force( rba, joint->maj.va, rbb, joint->maj.vb,
+                                 joint->maj.ang );
+   }
+
    for( int i=0; i<PART_COUNT; i++ )
       rb_iter( &ch->ragdoll[i] );
+   
+   for( int i=0; i<2; i++ )
+   {
+      if( ch->shoes[i] )
+      {
+         float a = v3_length( ch->ragdoll[i].v ) - shoe_vel[i];
+
+         if( a > 2.0f )
+         {
+            ch->shoes[i] = 0;
+            
+            rigidbody *src = &ch->ragdoll[k_chpart_foot_l];
+            rigidbody *dst = &ch->ragdoll[k_chpart_sock_l];
+
+            v3_copy( src->co, dst->co );
+            v3_copy( src->v, dst->v );
+            v3_copy( src->q, dst->q );
+            v3_copy( src->I, dst->I );
+         }
+      }
+   }
 
    for( int i=0; i<PART_COUNT; i++ )
       rb_update_transform( &ch->ragdoll[i] );