small clean
[carveJwlIkooP6JGAAIwe30JlM.git] / rigidbody.h
index 58b416eb072f982666a5f12ed6a4cc958b74aa90..0bba8f1b381fb6263e1bc99ce02d7cb8adeb4e91 100644 (file)
@@ -16,8 +16,6 @@
 VG_STATIC void rb_tangent_basis( v3f n, v3f tx, v3f ty );
 VG_STATIC bh_system bh_system_rigidbodies;
 
-
-
 #ifndef RIGIDBODY_H
 #define RIGIDBODY_H
 
@@ -214,13 +212,19 @@ VG_STATIC void rb_tangent_basis( v3f n, v3f tx, v3f ty )
 
 VG_STATIC void rb_debug_contact( rb_ct *ct )
 {
-   if( ct->type != k_contact_type_disabled )
+   v3f p1;
+   v3_muladds( ct->co, ct->n, 0.05f, p1 );
+
+   if( ct->type == k_contact_type_default )
    {
-      v3f p1;
-      v3_muladds( ct->co, ct->n, 0.05f, p1 );
       vg_line_pt3( ct->co, 0.0125f, 0xff0000ff );
       vg_line( ct->co, p1, 0xffffffff );
    }
+   else if( ct->type == k_contact_type_edge )
+   {
+      vg_line_pt3( ct->co, 0.0125f, 0xff00ffc0 );
+      vg_line( ct->co, p1, 0xffffffff );
+   }
 }
 
 VG_STATIC void debug_sphere( m4x3f m, float radius, u32 colour )
@@ -1993,19 +1997,37 @@ VG_STATIC rb_ct *rb_global_ct(void)
    return rb_contact_buffer + rb_contact_count;
 }
 
-VG_STATIC void rb_prepare_contact( rb_ct *ct )
+VG_STATIC void rb_prepare_contact( rb_ct *ct, float timestep )
 {
-   ct->bias = -0.2f * k_rb_rate * vg_minf( 0.0f, -ct->p+k_penetration_slop );
+   ct->bias = -0.2f * (timestep*3600.0f) 
+               * vg_minf( 0.0f, -ct->p+k_penetration_slop );
+   
    rb_tangent_basis( ct->n, ct->t[0], ct->t[1] );
-
-#if 0
-   ct->type = k_contact_type_default;
-#endif
    ct->norm_impulse = 0.0f;
    ct->tangent_impulse[0] = 0.0f;
    ct->tangent_impulse[1] = 0.0f;
 }
 
+/* calculate total move. manifold should belong to ONE object only */
+VG_STATIC void rb_depenetrate( rb_ct *manifold, int len, v3f dt )
+{
+   v3_zero( dt );
+
+   for( int j=0; j<7; j++ )
+   {
+      for( int i=0; i<len; i++ )
+      {
+         struct contact *ct = &manifold[i];
+
+         float resolved_amt = v3_dot( ct->n, dt ),
+               remaining    = (ct->p-k_penetration_slop) - resolved_amt,
+               apply        = vg_maxf( remaining, 0.0f ) * 0.4f;
+
+         v3_muladds( dt, ct->n, apply, dt );
+      }
+   }
+}
+
 /*
  * Initializing things like tangent vectors
  */
@@ -2014,7 +2036,7 @@ VG_STATIC void rb_presolve_contacts( rb_ct *buffer, int len )
    for( int i=0; i<len; i++ )
    {
       rb_ct *ct = &buffer[i];
-      rb_prepare_contact( ct );
+      rb_prepare_contact( ct, k_rb_delta );
 
       v3f ra, rb, raCn, rbCn, raCt, rbCt;
       v3_sub( ct->co, ct->rba->co, ra );
@@ -2625,6 +2647,24 @@ VG_STATIC void rb_effect_simple_bouyency( rigidbody *ra, v4f plane,
       v3_muls( ra->v, 1.0f-(drag*k_rb_delta), ra->v );
 }
 
+/* apply a spring&dampener force to match ra(worldspace) on rigidbody, to 
+ * rt(worldspace)
+ */
+VG_STATIC void rb_effect_spring_target_vector( rigidbody *rba, v3f ra, v3f rt,
+                                               float spring, float dampening,
+                                               float timestep )
+{
+   float a = acosf( vg_clampf( v3_dot( rt, ra ), -1.0f, 1.0f ) );
+
+   v3f axis;
+   v3_cross( rt, ra, axis );
+
+   float Fs = -a * spring,
+         Fd = -v3_dot( rba->w, axis ) * dampening;
+
+   v3_muladds( rba->w, axis, (Fs+Fd) * timestep, rba->w );
+}
+
 /*
  * -----------------------------------------------------------------------------
  * BVH implementation, this is ONLY for VG_STATIC rigidbodies, its to slow for