i hope your hapy
[carveJwlIkooP6JGAAIwe30JlM.git] / rigidbody.h
index f4d6ef9ccf0e1c5336087ba135b7fcde726118b6..fe8197569e689f87d316905f2ce4244b5b351e69 100644 (file)
@@ -33,7 +33,8 @@ VG_STATIC const float
    k_damp_angular     = 0.1f,                /* scale angular  1/(1+x) */
    k_penetration_slop = 0.01f,
    k_inertia_scale    = 8.0f,
-   k_phys_baumgarte   = 0.2f;
+   k_phys_baumgarte   = 0.2f,
+   k_gravity          = 9.6f;
 
 VG_STATIC float
    k_limit_bias       = 0.02f,
@@ -43,24 +44,24 @@ VG_STATIC float
 
 VG_STATIC void rb_register_cvar(void)
 {
-   vg_convar_push( (struct vg_convar){
+   vg_var_push( (struct vg_var){
       .name = "k_limit_bias", .data = &k_limit_bias,
-      .data_type = k_convar_dtype_f32, .opt_f32 = {.clamp = 0}, .persistent = 1
+      .data_type = k_var_dtype_f32, .opt_f32 = {.clamp = 0}, .persistent = 1
    });
 
-   vg_convar_push( (struct vg_convar){
+   vg_var_push( (struct vg_var){
       .name = "k_joint_bias", .data = &k_joint_bias,
-      .data_type = k_convar_dtype_f32, .opt_f32 = {.clamp = 0}, .persistent = 1
+      .data_type = k_var_dtype_f32, .opt_f32 = {.clamp = 0}, .persistent = 1
    });
 
-   vg_convar_push( (struct vg_convar){
+   vg_var_push( (struct vg_var){
       .name = "k_joint_correction", .data = &k_joint_correction,
-      .data_type = k_convar_dtype_f32, .opt_f32 = {.clamp = 0}, .persistent = 1
+      .data_type = k_var_dtype_f32, .opt_f32 = {.clamp = 0}, .persistent = 1
    });
 
-   vg_convar_push( (struct vg_convar){
+   vg_var_push( (struct vg_var){
       .name = "k_joint_impulse", .data = &k_joint_impulse,
-      .data_type = k_convar_dtype_f32, .opt_f32 = {.clamp = 0}, .persistent = 1
+      .data_type = k_var_dtype_f32, .opt_f32 = {.clamp = 0}, .persistent = 1
    });
 }
 
@@ -72,6 +73,24 @@ VG_STATIC void rb_register_cvar(void)
 
 typedef struct rigidbody rigidbody;
 typedef struct contact rb_ct;
+typedef struct rb_sphere rb_sphere;
+typedef struct rb_capsule rb_capsule;
+typedef struct rb_scene rb_scene;
+
+struct rb_sphere
+{
+   float radius;
+};
+
+struct rb_capsule
+{
+   float height, radius;
+};
+
+struct rb_scene
+{
+   bh_tree *bh_scene;
+};
 
 struct rigidbody
 {
@@ -89,23 +108,9 @@ struct rigidbody
    
    union
    {
-      struct rb_sphere
-      {
-         float radius;
-      }
-      sphere;
-
-      struct rb_capsule
-      {
-         float height, radius;
-      } 
-      capsule;
-
-      struct rb_scene
-      {
-         bh_tree *bh_scene;
-      }
-      scene;
+      struct rb_sphere sphere;
+      struct rb_capsule capsule;
+      struct rb_scene scene;
    }
    inf;
 
@@ -162,6 +167,11 @@ struct rb_constr_swingtwist
    float tangent_mass, axis_mass;
 };
 
+struct rb_constr_spring
+{
+   int nothing;
+};
+
 /*
  * -----------------------------------------------------------------------------
  *                               Math Utils
@@ -202,11 +212,17 @@ 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 )
+   {
+      vg_line_pt3( ct->co, 0.0125f, 0xff0000ff );
+      vg_line( ct->co, p1, 0xffffffff );
+   }
+   else if( ct->type == k_contact_type_edge )
    {
-      v3f p1;
-      v3_muladds( ct->co, ct->n, 0.05f, p1 );
-      vg_line_pt3( ct->co, 0.0025f, 0xff0000ff );
+      vg_line_pt3( ct->co, 0.0125f, 0xff00ffc0 );
       vg_line( ct->co, p1, 0xffffffff );
    }
 }
@@ -392,6 +408,8 @@ VG_STATIC void rb_update_transform( rigidbody *rb )
  * Extrapolate rigidbody into a transform based on vg accumulator.
  * Useful for rendering
  */
+#if 0
+__attribute__ ((deprecated))
 VG_STATIC void rb_extrapolate_transform( rigidbody *rb, m4x3f transform )
 {
    float substep = vg_clampf( vg.accumulator / k_rb_delta, 0.0f, 1.0f );
@@ -421,6 +439,31 @@ VG_STATIC void rb_extrapolate_transform( rigidbody *rb, m4x3f transform )
    q_m3x3( q, transform );
    v3_copy( co, transform[3] );
 }
+#endif
+
+VG_STATIC void rb_extrapolate( rigidbody *rb, v3f co, v4f q )
+{
+   float substep = vg_clampf( vg.accumulator / k_rb_delta, 0.0f, 1.0f );
+
+   v3_muladds( rb->co, rb->v, k_rb_delta*substep, co );
+
+   if( v3_length2( rb->w ) > 0.0f )
+   {
+      v4f rotation;
+      v3f axis;
+      v3_copy( rb->w, axis );
+      
+      float mag = v3_length( axis );
+      v3_divs( axis, mag, axis );
+      q_axis_angle( rotation, axis, mag*k_rb_delta*substep );
+      q_mul( rotation, rb->q, q );
+      q_normalize( q );
+   }
+   else
+   {
+      v4_copy( rb->q, q );
+   }
+}
 
 /*
  * Initialize rigidbody and calculate masses, inertia
@@ -561,19 +604,14 @@ VG_STATIC int rb_box_triangle_interval( v3f extent, v3f axis, v3f tri[3] )
 /*
  * Seperating axis test box vs triangle
  */
-VG_STATIC int rb_box_triangle_sat( rigidbody *rba, v3f tri_src[3] )
+VG_STATIC int rb_box_triangle_sat( v3f extent, v3f center,
+                                   m4x3f to_local, v3f tri_src[3] )
 {
    v3f tri[3];
 
-   v3f extent, c;
-   v3_sub( rba->bbx[1], rba->bbx[0], extent );
-   v3_muls( extent, 0.5f, extent );
-   v3_add( rba->bbx[0], extent, c );
-
-   for( int i=0; i<3; i++ )
-   {
-      m4x3_mulv( rba->to_local, tri_src[i], tri[i] );
-      v3_sub( tri[i], c, tri[i] );
+   for( int i=0; i<3; i++ ){
+      m4x3_mulv( to_local, tri_src[i], tri[i] );
+      v3_sub( tri[i], center, tri[i] );
    }
 
    /* u0, u1, u2 */
@@ -857,6 +895,8 @@ VG_STATIC void rb_capsule_manifold_init( capsule_manifold *manifold )
    manifold->t1 = -INFINITY;
 }
 
+#if 0
+__attribute__ ((deprecated))
 VG_STATIC int rb_capsule_manifold_done( rigidbody *rba, rigidbody *rbb, 
                                      capsule_manifold *manifold, rb_ct *buf )
 {
@@ -917,6 +957,61 @@ VG_STATIC int rb_capsule_manifold_done( rigidbody *rba, rigidbody *rbb,
 
    return count;
 }
+#endif
+
+VG_STATIC int rb_capsule__manifold_done( m4x3f mtx, rb_capsule *c,
+                                         capsule_manifold *manifold,
+                                         rb_ct *buf )
+{
+   v3f p0, p1;
+   v3_muladds( mtx[3], mtx[1], -c->height*0.5f+c->radius, p0 );
+   v3_muladds( mtx[3], mtx[1],  c->height*0.5f-c->radius, p1 );
+
+   int count = 0;
+   if( manifold->t0 <= 1.0f )
+   {
+      rb_ct *ct = buf;
+
+      v3f pa;
+      v3_muls( p0, 1.0f-manifold->t0, pa );
+      v3_muladds( pa, p1, manifold->t0, pa );
+
+      float d = v3_length( manifold->d0 );
+      v3_muls( manifold->d0, 1.0f/d, ct->n );
+      v3_muladds( pa, ct->n, -c->radius, ct->co );
+
+      ct->p = manifold->r0 - d;
+      ct->type = k_contact_type_default;
+      count ++;
+   }
+
+   if( (manifold->t1 >= 0.0f) && (manifold->t0 != manifold->t1) )
+   {
+      rb_ct *ct = buf+count;
+
+      v3f pa;
+      v3_muls( p0, 1.0f-manifold->t1, pa );
+      v3_muladds( pa, p1, manifold->t1, pa );
+
+      float d = v3_length( manifold->d1 );
+      v3_muls( manifold->d1, 1.0f/d, ct->n );
+      v3_muladds( pa, ct->n, -c->radius, ct->co );
+
+      ct->p = manifold->r1 - d;
+      ct->type = k_contact_type_default;
+
+      count ++;
+   }
+
+   /*
+    * Debugging
+    */
+
+   if( count == 2 )
+      vg_line( buf[0].co, buf[1].co, 0xff0000ff );
+
+   return count;
+}
 
 VG_STATIC int rb_capsule_sphere( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
 {
@@ -959,22 +1054,20 @@ VG_STATIC int rb_capsule_sphere( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
    return 0;
 }
 
-VG_STATIC int rb_capsule_capsule( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
+VG_STATIC int rb_capsule__capsule( m4x3f mtxA, rb_capsule *ca,
+                                   m4x3f mtxB, rb_capsule *cb, rb_ct *buf )
 {
-   if( !box_overlap( rba->bbx_world, rbb->bbx_world ) )
-      return 0;
-
-   float ha = rba->inf.capsule.height,
-         hb = rbb->inf.capsule.height,
-         ra = rba->inf.capsule.radius,
-         rb = rbb->inf.capsule.radius,
+   float ha = ca->height,
+         hb = cb->height,
+         ra = ca->radius,
+         rb = cb->radius,
           r = ra+rb;
 
    v3f p0, p1, p2, p3;
-   v3_muladds( rba->co, rba->up, -ha*0.5f+ra, p0 );
-   v3_muladds( rba->co, rba->up,  ha*0.5f-ra, p1 );
-   v3_muladds( rbb->co, rbb->up, -hb*0.5f+rb, p2 );
-   v3_muladds( rbb->co, rbb->up,  hb*0.5f-rb, p3 );
+   v3_muladds( mtxA[3], mtxA[1], -ha*0.5f+ra, p0 );
+   v3_muladds( mtxA[3], mtxA[1],  ha*0.5f-ra, p1 );
+   v3_muladds( mtxB[3], mtxB[1], -hb*0.5f+rb, p2 );
+   v3_muladds( mtxB[3], mtxB[1],  hb*0.5f-rb, p3 );
 
    capsule_manifold manifold;
    rb_capsule_manifold_init( &manifold );
@@ -994,9 +1087,10 @@ VG_STATIC int rb_capsule_capsule( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
    rb_capsule_manifold( p0, pa, 0.0f, r, &manifold );
    rb_capsule_manifold( p1, pb, 1.0f, r, &manifold );
    
-   return rb_capsule_manifold_done( rba, rbb, &manifold, buf );
+   return rb_capsule__manifold_done( mtxA, ca, &manifold, buf );
 }
 
+#if 0
 /*
  * Generates up to two contacts; optimised for the most stable manifold
  */
@@ -1123,6 +1217,7 @@ VG_STATIC int rb_capsule_box( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
 
    return rb_capsule_manifold_done( rba, rbb, &manifold, buf );
 }
+#endif
 
 VG_STATIC int rb_sphere_box( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
 {
@@ -1214,6 +1309,8 @@ VG_STATIC int rb_sphere_sphere( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
 
 //#define RIGIDBODY_DYNAMIC_MESH_EDGES
 
+#if 0
+__attribute__ ((deprecated))
 VG_STATIC int rb_sphere_triangle( rigidbody *rba, rigidbody *rbb, 
                                   v3f tri[3], rb_ct *buf )
 {
@@ -1260,7 +1357,47 @@ VG_STATIC int rb_sphere_triangle( rigidbody *rba, rigidbody *rbb,
 
    return 0;
 }
+#endif
+
+VG_STATIC int rb_sphere__triangle( m4x3f mtxA, rb_sphere *b,
+                                   v3f tri[3], rb_ct *buf )
+{
+   v3f delta, co;
+   enum contact_type type = closest_on_triangle_1( mtxA[3], tri, co );
+
+   v3_sub( mtxA[3], co, delta );
+
+   float d2 = v3_length2( delta ),
+          r = b->radius;
+
+   if( d2 <= r*r )
+   {
+      rb_ct *ct = buf;
+
+      v3f ab, ac, tn;
+      v3_sub( tri[2], tri[0], ab );
+      v3_sub( tri[1], tri[0], ac );
+      v3_cross( ac, ab, tn );
+      v3_copy( tn, ct->n );
 
+      if( v3_length2( ct->n ) <= 0.00001f )
+      {
+         vg_error( "Zero area triangle!\n" );
+         return 0;
+      }
+
+      v3_normalize( ct->n );
+
+      float d = sqrtf(d2);
+
+      v3_copy( co, ct->co );
+      ct->type = type;
+      ct->p = r-d;
+      return 1;
+   }
+
+   return 0;
+}
 
 VG_STATIC void rb_debug_sharp_scene_edges( rigidbody *rbb, float sharp_ang,
                                            boxf box, u32 colour )
@@ -1356,6 +1493,51 @@ VG_STATIC void rb_debug_sharp_scene_edges( rigidbody *rbb, float sharp_ang,
    }
 }
 
+VG_STATIC int rb_sphere__scene( m4x3f mtxA, rb_sphere *b,
+                                m4x3f mtxB, rb_scene *s, rb_ct *buf )
+{
+   scene *sc = s->bh_scene->user;
+
+   bh_iter it;
+   bh_iter_init( 0, &it );
+   int idx;
+
+   int count = 0;
+
+   float r = b->radius + 0.1f;
+   boxf box;
+   v3_sub( mtxA[3], (v3f){ r,r,r }, box[0] );
+   v3_add( mtxA[3], (v3f){ r,r,r }, box[1] );
+   
+   while( bh_next( s->bh_scene, &it, box, &idx ) )
+   {
+      u32 *ptri = &sc->arrindices[ idx*3 ];
+      v3f tri[3];
+
+      for( int j=0; j<3; j++ )
+         v3_copy( sc->arrvertices[ptri[j]].co, tri[j] );
+      
+      buf[ count ].element_id = ptri[0];
+
+      vg_line( tri[0],tri[1],0x70ff6000 );
+      vg_line( tri[1],tri[2],0x70ff6000 );
+      vg_line( tri[2],tri[0],0x70ff6000 );
+
+      int contact = rb_sphere__triangle( mtxA, b, tri, &buf[count] );
+      count += contact;
+
+      if( count == 16 )
+      {
+         vg_warn( "Exceeding sphere_vs_scene capacity. Geometry too dense!\n" );
+         return count;
+      }
+   }
+
+   return count;
+}
+
+#if 0
+__attribute__ ((deprecated))
 VG_STATIC int rb_sphere_scene( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
 {
    scene *sc = rbb->inf.scene.bh_scene->user;
@@ -1392,7 +1574,156 @@ VG_STATIC int rb_sphere_scene( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
 
    return count;
 }
+#endif
+
+VG_STATIC int rb_box__scene( m4x3f mtxA, boxf bbx,
+                             m4x3f mtxB, rb_scene *s, rb_ct *buf )
+{
+   scene *sc = s->bh_scene->user;
+   v3f tri[3];
+
+   v3f extent, center;
+   v3_sub( bbx[1], bbx[0], extent );
+   v3_muls( extent, 0.5f, extent );
+   v3_add( bbx[0], extent, center );
+
+   float r = v3_length(extent);
+   boxf world_bbx;
+   v3_fill( world_bbx[0], -r );
+   v3_fill( world_bbx[1],  r );
+   for( int i=0; i<2; i++ ){
+      v3_add( center, world_bbx[i], world_bbx[i] );
+      v3_add( mtxA[3], world_bbx[i], world_bbx[i] );
+   }
+
+   m4x3f to_local;
+   m4x3_invert_affine( mtxA, to_local );
+
+   bh_iter it;
+   bh_iter_init( 0, &it );
+   int idx;
+   int count = 0;
+
+   vg_line_boxf( world_bbx, VG__RED );
+   
+   while( bh_next( s->bh_scene, &it, world_bbx, &idx ) ){
+      u32 *ptri = &sc->arrindices[ idx*3 ];
+
+      for( int j=0; j<3; j++ )
+         v3_copy( sc->arrvertices[ptri[j]].co, tri[j] );
+
+      if( rb_box_triangle_sat( extent, center, to_local, tri ) ){
+         vg_line(tri[0],tri[1],0xff50ff00 );
+         vg_line(tri[1],tri[2],0xff50ff00 );
+         vg_line(tri[2],tri[0],0xff50ff00 );
+      }
+      else{
+         vg_line(tri[0],tri[1],0xff0000ff );
+         vg_line(tri[1],tri[2],0xff0000ff );
+         vg_line(tri[2],tri[0],0xff0000ff );
+         continue;
+      }
+
+      v3f v0,v1,n;
+      v3_sub( tri[1], tri[0], v0 );
+      v3_sub( tri[2], tri[0], v1 );
+      v3_cross( v0, v1, n );
+      v3_normalize( n );
+
+      /* find best feature */
+      float best = v3_dot( mtxA[0], n );
+      int axis = 0;
+
+      for( int i=1; i<3; i++ ){
+         float c = v3_dot( mtxA[i], n );
+
+         if( fabsf(c) > fabsf(best) ){
+            best = c;
+            axis = i;
+         }
+      }
+
+      v3f manifold[4];
+
+      if( axis == 0 ){
+         float px = best > 0.0f? bbx[0][0]: bbx[1][0];
+         manifold[0][0] = px;
+         manifold[0][1] = bbx[0][1];
+         manifold[0][2] = bbx[0][2];
+         manifold[1][0] = px;
+         manifold[1][1] = bbx[1][1];
+         manifold[1][2] = bbx[0][2];
+         manifold[2][0] = px;
+         manifold[2][1] = bbx[1][1];
+         manifold[2][2] = bbx[1][2];
+         manifold[3][0] = px;
+         manifold[3][1] = bbx[0][1];
+         manifold[3][2] = bbx[1][2];
+      }
+      else if( axis == 1 ){
+         float py = best > 0.0f? bbx[0][1]: bbx[1][1];
+         manifold[0][0] = bbx[0][0];
+         manifold[0][1] = py;
+         manifold[0][2] = bbx[0][2];
+         manifold[1][0] = bbx[1][0];
+         manifold[1][1] = py;
+         manifold[1][2] = bbx[0][2];
+         manifold[2][0] = bbx[1][0];
+         manifold[2][1] = py;
+         manifold[2][2] = bbx[1][2];
+         manifold[3][0] = bbx[0][0];
+         manifold[3][1] = py;
+         manifold[3][2] = bbx[1][2];
+      }
+      else{
+         float pz = best > 0.0f? bbx[0][2]: bbx[1][2];
+         manifold[0][0] = bbx[0][0];
+         manifold[0][1] = bbx[0][1];
+         manifold[0][2] = pz;
+         manifold[1][0] = bbx[1][0];
+         manifold[1][1] = bbx[0][1];
+         manifold[1][2] = pz;
+         manifold[2][0] = bbx[1][0];
+         manifold[2][1] = bbx[1][1];
+         manifold[2][2] = pz;
+         manifold[3][0] = bbx[0][0];
+         manifold[3][1] = bbx[1][1];
+         manifold[3][2] = pz;
+      }
+   
+      for( int j=0; j<4; j++ )
+         m4x3_mulv( mtxA, manifold[j], manifold[j] );
+
+      vg_line( manifold[0], manifold[1], 0xffffffff );
+      vg_line( manifold[1], manifold[2], 0xffffffff );
+      vg_line( manifold[2], manifold[3], 0xffffffff );
+      vg_line( manifold[3], manifold[0], 0xffffffff );
+
+      for( int j=0; j<4; j++ ){
+         rb_ct *ct = buf+count;
+
+         v3_copy( manifold[j], ct->co );
+         v3_copy( n, ct->n );
+
+         float l0 = v3_dot( tri[0], n ),
+               l1 = v3_dot( manifold[j], n );
+
+         ct->p = (l0-l1)*0.5f;
+         if( ct->p < 0.0f )
+            continue;
+
+         ct->type = k_contact_type_default;
+         count ++;
+
+         if( count >= 12 )
+            return count;
+      }
+   }
+   return count;
+}
 
+#if 0
+__attribute__ ((deprecated))
 VG_STATIC int rb_box_scene( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
 {
    scene *sc = rbb->inf.scene.bh_scene->user;
@@ -1535,10 +1866,66 @@ VG_STATIC int rb_box_scene( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
    }
    return count;
 }
+#endif
+
+VG_STATIC int rb_capsule__triangle( m4x3f mtxA, rb_capsule *c,
+                                    v3f tri[3], rb_ct *buf )
+{
+   v3f pc, p0w, p1w;
+   v3_muladds( mtxA[3], mtxA[1], -c->height*0.5f+c->radius, p0w );
+   v3_muladds( mtxA[3], mtxA[1],  c->height*0.5f-c->radius, p1w );
+
+   capsule_manifold manifold;
+   rb_capsule_manifold_init( &manifold );
+   
+   v3f c0, c1;
+   closest_on_triangle_1( p0w, tri, c0 );
+   closest_on_triangle_1( p1w, tri, c1 );
+
+   v3f d0, d1, da;
+   v3_sub( c0, p0w, d0 );
+   v3_sub( c1, p1w, d1 );
+   v3_sub( p1w, p0w, da );
+   
+   v3_normalize(d0);
+   v3_normalize(d1);
+   v3_normalize(da);
+
+   if( v3_dot( da, d0 ) <= 0.01f )
+      rb_capsule_manifold( p0w, c0, 0.0f, c->radius, &manifold );
+
+   if( v3_dot( da, d1 ) >= -0.01f )
+      rb_capsule_manifold( p1w, c1, 1.0f, c->radius, &manifold );
+
+   for( int i=0; i<3; i++ )
+   {
+      int i0 = i,
+          i1 = (i+1)%3;
+
+      v3f ca, cb;
+      float ta, tb;
+      closest_segment_segment( p0w, p1w, tri[i0], tri[i1], &ta, &tb, ca, cb );
+      rb_capsule_manifold( ca, cb, ta, c->radius, &manifold );
+   }
+
+   v3f v0, v1, n;
+   v3_sub( tri[1], tri[0], v0 );
+   v3_sub( tri[2], tri[0], v1 );
+   v3_cross( v0, v1, n );
+   v3_normalize( n );
+
+   int count = rb_capsule__manifold_done( mtxA, c, &manifold, buf );
+   for( int i=0; i<count; i++ )
+      v3_copy( n, buf[i].n );
+
+   return count;
+}
 
 /*
  * Generates up to two contacts; optimised for the most stable manifold
  */
+#if 0
+__attribute__ ((deprecated))
 VG_STATIC int rb_capsule_triangle( rigidbody *rba, rigidbody *rbb, 
                                    v3f tri[3], rb_ct *buf )
 {
@@ -1594,40 +1981,51 @@ VG_STATIC int rb_capsule_triangle( rigidbody *rba, rigidbody *rbb,
 
    return count;
 }
+#endif
 
-VG_STATIC int rb_capsule_scene( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
+/* mtxB is defined only for tradition; it is not used currently */
+VG_STATIC int rb_capsule__scene( m4x3f mtxA, rb_capsule *c,
+                                 m4x3f mtxB, rb_scene *s, 
+                                 rb_ct *buf )
 {
-#if 0
-   float h = rba->inf.capsule.height,
-         r = rba->inf.capsule.radius,
-         g = 90.8f;
+   bh_iter it;
+   bh_iter_init( 0, &it );
+   int idx;
+   int count = 0;
+
+   boxf bbx;
+   v3_sub( mtxA[3], (v3f){ c->height, c->height, c->height }, bbx[0] );
+   v3_add( mtxA[3], (v3f){ c->height, c->height, c->height }, bbx[1] );
+   
+   scene *sc = s->bh_scene->user;
    
-   v3f p[2];
-   v3_muladds( rba->co, rba->up, -h*0.5f+r, p[0] );
-   v3_muladds( rba->co, rba->up,  h*0.5f-r, p[1] );
+   while( bh_next( s->bh_scene, &it, bbx, &idx ) )
+   {
+      u32 *ptri = &sc->arrindices[ idx*3 ];
+      v3f tri[3];
 
-   int count = 0;
+      for( int j=0; j<3; j++ )
+         v3_copy( sc->arrvertices[ptri[j]].co, tri[j] );
+      
+      buf[ count ].element_id = ptri[0];
 
+      int contact = rb_capsule__triangle( mtxA, c, tri, &buf[count] );
+      count += contact;
 
-   for( int i=0; i<2; i++ )
-   {
-      if( p[i][1] < g + r )
+      if( count >= 16 )
       {
-         rb_ct *ct = &buf[ count ++ ];
-
-         v3_copy( p[i], ct->co );
-         ct->p = r - (p[i][1]-g);
-         ct->co[1] -= r;
-         v3_copy( (v3f){0.0f,1.0f,0.0f}, ct->n );
-         ct->rba = rba;
-         ct->rbb = rbb;
-         ct->type = k_contact_type_default;
+         vg_warn("Exceeding capsule_vs_scene capacity. Geometry too dense!\n");
+         return count;
       }
    }
 
    return count;
+}
 
-#else
+#if 0
+__attribute__ ((deprecated))
+VG_STATIC int rb_capsule_scene( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
+{
    scene *sc = rbb->inf.scene.bh_scene->user;
 
    bh_iter it;
@@ -1663,13 +2061,13 @@ VG_STATIC int rb_capsule_scene( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
    }
 
    return count;
-#endif
 }
 
 VG_STATIC int rb_scene_capsule( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
 {
    return rb_capsule_scene( rbb, rba, buf );
 }
+#endif
 
 VG_STATIC int RB_MATRIX_ERROR( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
 {
@@ -1686,21 +2084,26 @@ VG_STATIC int rb_sphere_capsule( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
    return rb_capsule_sphere( rbb, rba, buf );
 }
 
+#if 0
 VG_STATIC int rb_box_capsule( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
 {
    return rb_capsule_box( rbb, rba, buf );
 }
+#endif
 
 VG_STATIC int rb_box_sphere( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
 {
    return rb_sphere_box( rbb, rba, buf );
 }
 
+#if 0
 VG_STATIC int rb_scene_box( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
 {
    return rb_box_scene( rbb, rba, buf );
 }
+#endif
 
+#if 0
 VG_STATIC int (*rb_jump_table[4][4])( rigidbody *a, rigidbody *b, rb_ct *buf ) = 
 {
     /* box */        /* Sphere */       /* Capsule */       /* Mesh */
@@ -1738,6 +2141,20 @@ VG_STATIC int rb_collide( rigidbody *rba, rigidbody *rbb )
    else
       return 0;
 }
+#endif
+
+VG_STATIC int rb_global_has_space( void )
+{
+   if( rb_contact_count + 16 > vg_list_size(rb_contact_buffer) )
+      return 0;
+
+   return 1;
+}
+
+VG_STATIC rb_ct *rb_global_buffer( void )
+{
+   return &rb_contact_buffer[ rb_contact_count ];
+}
 
 /*
  * -----------------------------------------------------------------------------
@@ -1755,6 +2172,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, float timestep )
+{
+   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] );
+   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
  */
@@ -1763,16 +2211,7 @@ VG_STATIC void rb_presolve_contacts( rb_ct *buffer, int len )
    for( int i=0; i<len; i++ )
    {
       rb_ct *ct = &buffer[i];
-
-      ct->bias = -0.2f * k_rb_rate * 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;
+      rb_prepare_contact( ct, k_rb_delta );
 
       v3f ra, rb, raCn, rbCn, raCt, rbCt;
       v3_sub( ct->co, ct->rba->co, ra );
@@ -2350,6 +2789,21 @@ VG_STATIC void rb_correct_swingtwist_constraints( rb_constr_swingtwist *buf,
    }
 }
 
+VG_STATIC void rb_correct_contact_constraints( rb_ct *buf, int len, float amt )
+{
+   for( int i=0; i<len; i++ )
+   {
+      rb_ct *ct = &buf[i];
+      rigidbody *rba = ct->rba,
+                *rbb = ct->rbb;
+
+      float mass_total = 1.0f / (rba->inv_mass + rbb->inv_mass);
+
+      v3_muladds( rba->co, ct->n, -mass_total * rba->inv_mass, rba->co );
+      v3_muladds( rbb->co, ct->n,  mass_total * rbb->inv_mass, rbb->co );
+   }
+}
+
 
 /* 
  * Effectors
@@ -2368,6 +2822,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