assorted crap
authorhgn <hgodden00@gmail.com>
Wed, 23 Nov 2022 21:06:24 +0000 (21:06 +0000)
committerhgn <hgodden00@gmail.com>
Wed, 23 Nov 2022 21:06:24 +0000 (21:06 +0000)
blender_export.py
bvh.h
distq.h [new file with mode: 0644]
maps_src/mp_gridmap.mdl
model.h
player_physics.h
rigidbody.h
scene.h
world.h
world_gen.h

index 7b0751e1fee2d63fef019248239dfec1088f2cc9..cf6d3f7dd9d4fcde68765723ec608ed3ffa2291a 100644 (file)
@@ -1124,8 +1124,11 @@ def encoder_process_material( mat ):
    dest.pstr_name = encoder_process_pstr( mat.name )
    
    flags = 0x00
-   if mat.cv_data.skate_surface: flags |= 0x1
-   if mat.cv_data.collision: flags |= 0x2
+   if mat.cv_data.collision: 
+      flags |= 0x2
+      if mat.cv_data.skate_surface: flags |= 0x1
+      if mat.cv_data.grind_surface: flags |= (0x8|0x1)
+
    if mat.cv_data.grow_grass: flags |= 0x4
    dest.flags = flags
 
@@ -1336,18 +1339,18 @@ def encoder_vertex_push( vertex_reference, co,norm,uv,colour,groups,weights ):
           int(norm[2]*m+0.5),
           int(uv[0]*m+0.5),
           int(uv[1]*m+0.5),
-          colour[0]*m+0.5,    # these guys are already quantized
-          colour[1]*m+0.5,    # .
-          colour[2]*m+0.5,    # .
-          colour[3]*m+0.5,    # .
-          weights[0]*m+0.5,   # v
-          weights[1]*m+0.5,
-          weights[2]*m+0.5,
-          weights[3]*m+0.5,
-          groups[0]*m+0.5,
-          groups[1]*m+0.5,
-          groups[2]*m+0.5,
-          groups[3]*m+0.5)
+          colour[0],    # these guys are already quantized
+          colour[1],    # .
+          colour[2],    # .
+          colour[3],    # .
+          weights[0],   # v
+          weights[1],
+          weights[2],
+          weights[3],
+          groups[0],
+          groups[1],
+          groups[2],
+          groups[3])
 
    if key in vertex_reference:
       return vertex_reference[key]
@@ -1535,6 +1538,20 @@ def encoder_compile_mesh( node, node_def ):
                      weights[ml] = max( weights[ml], 0 )
                   #}
                #}
+            #}
+            else:
+            #{
+               li1 = tri.loops[(j+1)%3]
+               vi1 = data.loops[li1].vertex_index
+               e0 = data.edges[ data.loops[li].edge_index ]
+
+               if e0.use_freestyle_mark and \
+                     ((e0.vertices[0] == vi and e0.vertices[1] == vi1) or \
+                      (e0.vertices[0] == vi1 and e0.vertices[1] == vi)):
+               #{
+                  weights[0] = 1
+               #}
+            #}
             
             # Add vertex and expand bound box
             #
@@ -2477,6 +2494,11 @@ class CV_MATERIAL_SETTINGS(bpy.types.PropertyGroup):
          default=True,\
          description = "Should the game try to target this surface?" \
    )
+   grind_surface: bpy.props.BoolProperty( \
+         name="Grind Surface", \
+         default=False,\
+         description = "Grind face?" \
+   )
    grow_grass: bpy.props.BoolProperty( \
          name="Grow Grass", \
          default=False,\
@@ -2535,6 +2557,7 @@ class CV_MATERIAL_PANEL(bpy.types.Panel):
 
       if active_mat.cv_data.collision:
          _.layout.prop( active_mat.cv_data, "skate_surface" )
+         _.layout.prop( active_mat.cv_data, "grind_surface" )
          _.layout.prop( active_mat.cv_data, "grow_grass" )
 
       if active_mat.cv_data.shader == "terrain_blend":
diff --git a/bvh.h b/bvh.h
index 9e4528604d59c8cf205f20d3d172b671102085e1..491756acb0db460df2d5e697002b9b5e5e560169 100644 (file)
--- a/bvh.h
+++ b/bvh.h
@@ -5,6 +5,7 @@
 #ifndef BVH_H
 #define BVH_H
 #include "common.h"
+#include "distq.h"
 
 /*
  * Usage:
@@ -39,8 +40,8 @@ struct bh_tree
       boxf bbx;
 
       /* if il is 0, this is a leaf */
-      u32 il, count;
-      union{ u32 ir, start; };
+      int il, count;
+      union{ int ir, start; };
    } 
    nodes[];
 };
@@ -49,6 +50,7 @@ struct bh_system
 {
    void  (*expand_bound)( void *user, boxf bound, u32 item_index );
    float (*item_centroid)( void *user, u32 item_index, int axis );
+   void  (*item_closest)( void *user, u32 item_index, v3f point, v3f closest );
    void  (*item_swap)( void *user, u32 ia, u32 ib );
 
    /*
@@ -260,54 +262,127 @@ VG_STATIC int bh_ray( bh_tree *bh, v3f co, v3f dir, ray_hit *hit )
    return count;
 }
 
-VG_STATIC int bh_select( bh_tree *bh, boxf box, u32 *buffer, int len )
+typedef struct bh_iter bh_iter;
+struct bh_iter
 {
-   if( bh->node_count < 2 )
-      return 0;
+   struct 
+   {
+      int id, depth;
+   }
+   stack[64];
 
-   int count = 0;
-   u32 stack[100];
-   u32 depth = 2;
+   int depth, i;
+};
 
-   stack[0] = 0;
-   stack[1] = bh->nodes[0].il;
-   stack[2] = bh->nodes[0].ir;
-   
-   while(depth)
+VG_STATIC void bh_iter_init( int root, bh_iter *it )
+{
+   it->stack[0].id = root;
+   it->stack[0].depth = 0;
+   it->depth = 0;
+   it->i = 0;
+}
+
+VG_STATIC int bh_next( bh_tree *bh, bh_iter *it, boxf box, int *em )
+{
+   while( it->depth >= 0 )
    {
-      bh_node *inode = &bh->nodes[ stack[depth] ];
+      bh_node *inode = &bh->nodes[ it->stack[it->depth].id ];
+      
       if( box_overlap( inode->bbx, box ) )
       {
          if( inode->count )
          {
-            if( count + inode->count >= len )
-               return count;
-
-            for( u32 i=0; i<inode->count; i++ )
-               buffer[ count ++ ] = inode->start+i;
-
-            depth --;
+            if( it->i < inode->count )
+            {
+               *em = inode->start+it->i;
+               it->i ++;
+               return 1;
+            }
+            else
+            {
+               it->depth --;
+               it->i = 0;
+            }
          }
          else
          {
-            if( depth+1 >= vg_list_size(stack) )
+            if( it->depth+1 >= vg_list_size(it->stack) )
             {
                vg_error( "Maximum stack reached!\n" );
-               return count;
+               return 0;
             }
 
-            stack[depth] = inode->il;
-            stack[depth+1] = inode->ir;
-            depth ++;
+            it->stack[it->depth  ].id = inode->il;
+            it->stack[it->depth+1].id = inode->ir;
+            it->depth ++;
+            it->i = 0;
          }
       }
       else
       {
-         depth --;
+         it->depth --;
       }
    }
 
-   return count;
+   return 0;
+}
+
+VG_STATIC int bh_closest_point( bh_tree *bh, v3f pos, 
+                                v3f closest, float max_dist )
+{
+   if( bh->node_count < 2 )
+      return -1;
+
+   max_dist = max_dist*max_dist;
+
+   int queue[ 128 ],
+       depth = 0,
+       best_item = -1;
+
+   queue[0] = 0;
+
+   while( depth >= 0 )
+   {
+      bh_node *inode = &bh->nodes[ queue[depth] ];
+
+      v3f p1;
+      closest_point_aabb( pos, inode->bbx, p1 );
+
+      /* branch into node if its closer than current best */
+      float node_dist = v3_dist2( pos, p1 );
+      if( node_dist < max_dist )
+      {
+         if( inode->count )
+         {
+            for( int i=0; i<inode->count; i++ )
+            {
+               v3f p2;
+               bh->system->item_closest( bh->user, inode->start+i, pos, p2 );
+
+               float item_dist = v3_dist2( pos, p2 );
+               if( item_dist < max_dist )
+               {
+                  max_dist = item_dist;
+                  v3_copy( p2, closest );
+                  best_item = inode->start+i;
+               }
+            }
+
+            depth --;
+         }
+         else
+         {
+            queue[depth] = inode->il;
+            queue[depth+1] = inode->ir;
+
+            depth ++;
+         }
+      }
+      else
+         depth --;
+   }
+
+   return best_item;
 }
 
 #endif /* BVH_H */
diff --git a/distq.h b/distq.h
new file mode 100644 (file)
index 0000000..623010f
--- /dev/null
+++ b/distq.h
@@ -0,0 +1,301 @@
+#ifndef DISTQ_H
+#define DISTQ_H
+
+#include "vg_m.h"
+
+enum contact_type
+{
+   k_contact_type_default,
+   k_contact_type_disabled,
+   k_contact_type_edge
+};
+
+/*
+ * -----------------------------------------------------------------------------
+ *                        Closest point functions
+ * -----------------------------------------------------------------------------
+ */
+
+/* 
+ * These closest point tests were learned from Real-Time Collision Detection by 
+ * Christer Ericson 
+ */
+VG_STATIC float closest_segment_segment( v3f p1, v3f q1, v3f p2, v3f q2, 
+   float *s, float *t, v3f c1, v3f c2)
+{
+   v3f d1,d2,r;
+   v3_sub( q1, p1, d1 );
+   v3_sub( q2, p2, d2 );
+   v3_sub( p1, p2, r );
+
+   float a = v3_length2( d1 ),
+         e = v3_length2( d2 ),
+         f = v3_dot( d2, r );
+
+   const float kEpsilon = 0.0001f;
+
+   if( a <= kEpsilon && e <= kEpsilon )
+   {
+      *s = 0.0f;
+      *t = 0.0f;
+      v3_copy( p1, c1 );
+      v3_copy( p2, c2 );
+
+      v3f v0;
+      v3_sub( c1, c2, v0 );
+
+      return v3_length2( v0 );
+   }
+   
+   if( a<= kEpsilon )
+   {
+      *s = 0.0f;
+      *t = vg_clampf( f / e, 0.0f, 1.0f );
+   }
+   else
+   {
+      float c = v3_dot( d1, r );
+      if( e <= kEpsilon )
+      {
+         *t = 0.0f;
+         *s = vg_clampf( -c / a, 0.0f, 1.0f );
+      }
+      else
+      {
+         float b = v3_dot(d1,d2),
+               d = a*e-b*b;
+
+         if( d != 0.0f )
+         {
+            *s = vg_clampf((b*f - c*e)/d, 0.0f, 1.0f);
+         }
+         else
+         {
+            *s = 0.0f;
+         }
+
+         *t = (b*(*s)+f) / e;
+
+         if( *t < 0.0f )
+         {
+            *t = 0.0f;
+            *s = vg_clampf( -c / a, 0.0f, 1.0f );
+         }
+         else if( *t > 1.0f )
+         {
+            *t = 1.0f;
+            *s = vg_clampf((b-c)/a,0.0f,1.0f);
+         }
+      }
+   }
+
+   v3_muladds( p1, d1, *s, c1 );
+   v3_muladds( p2, d2, *t, c2 );
+
+   v3f v0;
+   v3_sub( c1, c2, v0 );
+   return v3_length2( v0 );
+}
+
+VG_STATIC void closest_point_aabb( v3f p, boxf box, v3f dest )
+{
+   v3_maxv( p, box[0], dest );
+   v3_minv( dest, box[1], dest );
+}
+
+VG_STATIC void closest_point_obb( v3f p, boxf box, 
+                                  m4x3f mtx, m4x3f inv_mtx, v3f dest )
+{
+   v3f local;
+   m4x3_mulv( inv_mtx, p, local );
+   closest_point_aabb( local, box, local );
+   m4x3_mulv( mtx, local, dest );
+}
+
+VG_STATIC float closest_point_segment( v3f a, v3f b, v3f point, v3f dest )
+{
+   v3f v0, v1;
+   v3_sub( b, a, v0 );
+   v3_sub( point, a, v1 );
+
+   float t = v3_dot( v1, v0 ) / v3_length2(v0);
+   t = vg_clampf(t,0.0f,1.0f);
+   v3_muladds( a, v0, t, dest );
+   return t;
+}
+
+VG_STATIC void closest_on_triangle( v3f p, v3f tri[3], v3f dest )
+{
+   v3f ab, ac, ap;
+   float d1, d2;
+
+   /* Region outside A */
+   v3_sub( tri[1], tri[0], ab );
+   v3_sub( tri[2], tri[0], ac );
+   v3_sub( p, tri[0], ap );
+   
+   d1 = v3_dot(ab,ap);
+   d2 = v3_dot(ac,ap);
+   if( d1 <= 0.0f && d2 <= 0.0f ) 
+   {
+      v3_copy( tri[0], dest );
+      v3_copy( (v3f){INFINITY,INFINITY,INFINITY}, dest );
+      return;
+   }
+
+   /* Region outside B */
+   v3f bp;
+   float d3, d4;
+
+   v3_sub( p, tri[1], bp );
+   d3 = v3_dot( ab, bp );
+   d4 = v3_dot( ac, bp );
+
+   if( d3 >= 0.0f && d4 <= d3 )
+   {
+      v3_copy( tri[1], dest );
+      v3_copy( (v3f){INFINITY,INFINITY,INFINITY}, dest );
+      return;
+   }
+   
+   /* Edge region of AB */
+   float vc = d1*d4 - d3*d2;
+   if( vc <= 0.0f && d1 >= 0.0f && d3 <= 0.0f )
+   {
+      float v = d1 / (d1-d3);
+      v3_muladds( tri[0], ab, v, dest );
+      v3_copy( (v3f){INFINITY,INFINITY,INFINITY}, dest );
+      return;
+   }
+
+   /* Region outside C */
+   v3f cp;
+   float d5, d6;
+   v3_sub( p, tri[2], cp );
+   d5 = v3_dot(ab, cp);
+   d6 = v3_dot(ac, cp);
+   
+   if( d6 >= 0.0f && d5 <= d6 )
+   {
+      v3_copy( tri[2], dest );
+      v3_copy( (v3f){INFINITY,INFINITY,INFINITY}, dest );
+      return;
+   }
+
+   /* Region of AC */
+   float vb = d5*d2 - d1*d6;
+   if( vb <= 0.0f && d2 >= 0.0f && d6 <= 0.0f )
+   {
+      float w = d2 / (d2-d6);
+      v3_muladds( tri[0], ac, w, dest );
+      v3_copy( (v3f){INFINITY,INFINITY,INFINITY}, dest );
+      return;
+   }
+
+   /* Region of BC */
+   float va = d3*d6 - d5*d4;
+   if( va <= 0.0f && (d4-d3) >= 0.0f && (d5-d6) >= 0.0f )
+   {
+      float w = (d4-d3) / ((d4-d3) + (d5-d6));
+      v3f bc;
+      v3_sub( tri[2], tri[1], bc );
+      v3_muladds( tri[1], bc, w, dest );
+      v3_copy( (v3f){INFINITY,INFINITY,INFINITY}, dest );
+      return;
+   }
+
+   /* P inside region, Q via barycentric coordinates uvw */
+   float d = 1.0f/(va+vb+vc),
+         v = vb*d,
+         w = vc*d;
+
+   v3_muladds( tri[0], ab, v, dest );
+   v3_muladds( dest, ac, w, dest );
+}
+
+VG_STATIC enum contact_type closest_on_triangle_1( v3f p, v3f tri[3], v3f dest )
+{
+   v3f ab, ac, ap;
+   float d1, d2;
+
+   /* Region outside A */
+   v3_sub( tri[1], tri[0], ab );
+   v3_sub( tri[2], tri[0], ac );
+   v3_sub( p, tri[0], ap );
+   
+   d1 = v3_dot(ab,ap);
+   d2 = v3_dot(ac,ap);
+   if( d1 <= 0.0f && d2 <= 0.0f ) 
+   {
+      v3_copy( tri[0], dest );
+      return k_contact_type_default;
+   }
+
+   /* Region outside B */
+   v3f bp;
+   float d3, d4;
+
+   v3_sub( p, tri[1], bp );
+   d3 = v3_dot( ab, bp );
+   d4 = v3_dot( ac, bp );
+
+   if( d3 >= 0.0f && d4 <= d3 )
+   {
+      v3_copy( tri[1], dest );
+      return k_contact_type_edge;
+   }
+   
+   /* Edge region of AB */
+   float vc = d1*d4 - d3*d2;
+   if( vc <= 0.0f && d1 >= 0.0f && d3 <= 0.0f )
+   {
+      float v = d1 / (d1-d3);
+      v3_muladds( tri[0], ab, v, dest );
+      return k_contact_type_edge;
+   }
+
+   /* Region outside C */
+   v3f cp;
+   float d5, d6;
+   v3_sub( p, tri[2], cp );
+   d5 = v3_dot(ab, cp);
+   d6 = v3_dot(ac, cp);
+   
+   if( d6 >= 0.0f && d5 <= d6 )
+   {
+      v3_copy( tri[2], dest );
+      return k_contact_type_edge;
+   }
+
+   /* Region of AC */
+   float vb = d5*d2 - d1*d6;
+   if( vb <= 0.0f && d2 >= 0.0f && d6 <= 0.0f )
+   {
+      float w = d2 / (d2-d6);
+      v3_muladds( tri[0], ac, w, dest );
+      return k_contact_type_edge;
+   }
+
+   /* Region of BC */
+   float va = d3*d6 - d5*d4;
+   if( va <= 0.0f && (d4-d3) >= 0.0f && (d5-d6) >= 0.0f )
+   {
+      float w = (d4-d3) / ((d4-d3) + (d5-d6));
+      v3f bc;
+      v3_sub( tri[2], tri[1], bc );
+      v3_muladds( tri[1], bc, w, dest );
+      return k_contact_type_edge;
+   }
+
+   /* P inside region, Q via barycentric coordinates uvw */
+   float d = 1.0f/(va+vb+vc),
+         v = vb*d,
+         w = vc*d;
+
+   v3_muladds( tri[0], ab, v, dest );
+   v3_muladds( dest, ac, w, dest );
+
+   return k_contact_type_default;
+}
+
+#endif /* DISTQ_H */
index 8b9ce51208db69023f5c3329de300ce2d61c6b87..2b2d2e56f23c3528cf55f2e22dfbfebe2fba881f 100644 (file)
Binary files a/maps_src/mp_gridmap.mdl and b/maps_src/mp_gridmap.mdl differ
diff --git a/model.h b/model.h
index 99aa2f112d44346bbb546793803eef2c7edff0dc..9bdc2bca58b32c8563cb14dbe6a6be77fd867e9e 100644 (file)
--- a/model.h
+++ b/model.h
@@ -64,7 +64,8 @@ enum material_flag
 {
    k_material_flag_skate_surface    = 0x1,
    k_material_flag_collision        = 0x2,
-   k_material_flag_grow_grass       = 0x4
+   k_material_flag_grow_grass       = 0x4,
+   k_material_flag_grind_surface    = 0x8
 };
 
 #pragma pack(push,1)
index 85f3744be2a274f7f585a0ad3343466c90888c39..41adc1e4150787e8ee13d021a54d8da3dede0e09 100644 (file)
@@ -137,12 +137,7 @@ VG_STATIC void player_physics_control(void)
    phys->reverse = -vg_signf(vel[2]);
 
    float substep = VG_TIMESTEP_FIXED * 0.2f;
-
-#if 0
-   float fwd_resistance = vg_get_button( "break" )? 5.0f: k_friction_resistance;
-#else
    float fwd_resistance = k_friction_resistance;
-#endif
 
    for( int i=0; i<5; i++ )
    {
@@ -183,18 +178,6 @@ VG_STATIC void player_physics_control(void)
       vel[2] -= new_vel * phys->reverse;
    }
 
-   /* Pumping */
-   static float previous = 0.0f;
-   float delta = previous - phys->grab,
-          pump = delta * k_pump_force * VG_TIMESTEP_FIXED;
-   previous = phys->grab;
-
-   v3f p1;
-   v3_muladds( phys->rb.co, phys->rb.up, pump, p1 );
-   vg_line( phys->rb.co, p1, 0xff0000ff );
-
-   vel[1] += pump;
-
    m3x3_mulv( phys->rb.to_world, vel, phys->rb.v );
    
    float input = player.input_js1h->axis.value,
@@ -517,6 +500,43 @@ VG_STATIC void player_walk_physics(void)
    }
 }
 
+VG_STATIC void player_grind(void)
+{
+   struct player_phys *phys = &player.phys;
+
+   v3f closest;
+   int idx = bh_closest_point( world.grind_bh, phys->rb.co, closest, INFINITY );
+   if( idx == -1 )
+      return;
+
+   struct grind_edge *edge = &world.grind_edges[ idx ];
+
+   vg_line( phys->rb.co, closest, 0xff000000 );
+   vg_line_cross( closest, 0xff000000, 0.3f );
+   vg_line( edge->p0, edge->p1, 0xff000000 );
+
+   return;
+
+   idx = bh_closest_point( world.geo_bh, phys->rb.co, closest, INFINITY );
+   vg_line( phys->rb.co, closest, 0xff000000 );
+   vg_line_cross( closest, 0xff000000, 0.3f );
+   
+   idx = world.scene_geo->arrindices[ idx * 3 ];
+   struct world_material *mat = world_tri_index_material( idx );
+
+   if( mat->info.flags & k_material_flag_grind_surface )
+   {
+      v3f grind_delta;
+      v3_sub( closest, phys->rb.co, grind_delta );
+      
+      float p = v3_dot( phys->rb.forward, grind_delta );
+      v3_muladds( grind_delta, phys->rb.forward, -p, grind_delta );
+      
+      float a = vg_maxf( 0.0f, 4.0f-v3_dist2( closest, phys->rb.co ) );
+      v3_muladds( phys->rb.v, grind_delta, a*0.2f, phys->rb.v );
+   }
+}
+
 /*
  * Physics collision detection, and control
  */
@@ -556,22 +576,33 @@ VG_STATIC void player_physics(void)
 
    len_f = rb_sphere_scene( rbf, &world.rb_geo, manifold );
    rb_manifold_filter_coplanar( manifold, len_f, 0.05f );
-   rb_manifold_filter_pairs( manifold, len_f, 0.05f );
    if( len_f > 1 )
+   {
       rb_manifold_filter_backface( manifold, len_f );
+      rb_manifold_filter_joint_edges( manifold, len_f, 0.05f );
+      rb_manifold_filter_pairs( manifold, len_f, 0.05f );
+   }
    len_f = rb_manifold_apply_filtered( manifold, len_f );
    
    rb_ct *man_b = &manifold[len_f];
    len_b = rb_sphere_scene( rbb, &world.rb_geo, man_b );
    rb_manifold_filter_coplanar( man_b, len_b, 0.05f );
-   rb_manifold_filter_pairs( man_b, len_b, 0.05f );
    if( len_b > 1 )
+   {
       rb_manifold_filter_backface( man_b, len_b );
+      rb_manifold_filter_joint_edges( man_b, len_b, 0.05f );
+      rb_manifold_filter_pairs( man_b, len_b, 0.05f );
+   }
    len_b = rb_manifold_apply_filtered( man_b, len_b );
 
    int len = len_f+len_b;
 
-#if 0
+   player_grind();
+
+   boxf bax;
+   v3_sub( phys->rb.co, (v3f){2.0f,2.0f,2.0f}, bax[0] );
+   v3_add( phys->rb.co, (v3f){2.0f,2.0f,2.0f}, bax[1] );
+
    /* 
     * Preprocess collision points, and create a surface picture.
     * we want contacts that are within our 'capsule's internal line to be 
@@ -592,7 +623,6 @@ VG_STATIC void player_physics(void)
          v3_normalize( manifold[i].n );
       }
    }
-#endif
 
    rb_presolve_contacts( manifold, len );
    v3f surface_avg = {0.0f, 0.0f, 0.0f};
@@ -683,8 +713,6 @@ VG_STATIC void player_physics(void)
 
    if( !phys->in_air )
    {
-      /* 20/10/22: make this only go axisways instead, may effect velocities. */
-
       v3f projected, axis;
 
       float d = v3_dot( phys->rb.forward, surface_avg );
index 2e66fe68e5fea5800f3fcfc0201abfded3c4adf4..16d38ea0df86d5003039acc09d2a4563e9151995 100644 (file)
@@ -10,6 +10,7 @@
 #include "common.h"
 #include "bvh.h"
 #include "scene.h"
+#include "distq.h"
 
 VG_STATIC void rb_tangent_basis( v3f n, v3f tx, v3f ty );
 VG_STATIC bh_system bh_system_rigidbodies;
@@ -103,7 +104,8 @@ VG_STATIC struct contact
          normal_mass, tangent_mass[2];
 
    u32 element_id;
-   int disabled;
+
+   enum contact_type type;
 }
 rb_contact_buffer[256];
 VG_STATIC int rb_contact_count = 0;
@@ -148,7 +150,7 @@ VG_STATIC void rb_tangent_basis( v3f n, v3f tx, v3f ty )
 
 VG_STATIC void rb_debug_contact( rb_ct *ct )
 {
-   if( !ct->disabled )
+   if( ct->type != k_contact_type_disabled )
    {
       v3f p1;
       v3_muladds( ct->co, ct->n, 0.1f, p1 );
@@ -469,290 +471,6 @@ VG_STATIC void rb_iter( rigidbody *rb )
    v3_muls( rb->w, 1.0f/(1.0f+k_rb_delta*k_damp_angular), rb->w );
 }
 
-/*
- * -----------------------------------------------------------------------------
- *                        Closest point functions
- * -----------------------------------------------------------------------------
- */
-
-/* 
- * These closest point tests were learned from Real-Time Collision Detection by 
- * Christer Ericson 
- */
-VG_STATIC float closest_segment_segment( v3f p1, v3f q1, v3f p2, v3f q2, 
-   float *s, float *t, v3f c1, v3f c2)
-{
-   v3f d1,d2,r;
-   v3_sub( q1, p1, d1 );
-   v3_sub( q2, p2, d2 );
-   v3_sub( p1, p2, r );
-
-   float a = v3_length2( d1 ),
-         e = v3_length2( d2 ),
-         f = v3_dot( d2, r );
-
-   const float kEpsilon = 0.0001f;
-
-   if( a <= kEpsilon && e <= kEpsilon )
-   {
-      *s = 0.0f;
-      *t = 0.0f;
-      v3_copy( p1, c1 );
-      v3_copy( p2, c2 );
-
-      v3f v0;
-      v3_sub( c1, c2, v0 );
-
-      return v3_length2( v0 );
-   }
-   
-   if( a<= kEpsilon )
-   {
-      *s = 0.0f;
-      *t = vg_clampf( f / e, 0.0f, 1.0f );
-   }
-   else
-   {
-      float c = v3_dot( d1, r );
-      if( e <= kEpsilon )
-      {
-         *t = 0.0f;
-         *s = vg_clampf( -c / a, 0.0f, 1.0f );
-      }
-      else
-      {
-         float b = v3_dot(d1,d2),
-               d = a*e-b*b;
-
-         if( d != 0.0f )
-         {
-            *s = vg_clampf((b*f - c*e)/d, 0.0f, 1.0f);
-         }
-         else
-         {
-            *s = 0.0f;
-         }
-
-         *t = (b*(*s)+f) / e;
-
-         if( *t < 0.0f )
-         {
-            *t = 0.0f;
-            *s = vg_clampf( -c / a, 0.0f, 1.0f );
-         }
-         else if( *t > 1.0f )
-         {
-            *t = 1.0f;
-            *s = vg_clampf((b-c)/a,0.0f,1.0f);
-         }
-      }
-   }
-
-   v3_muladds( p1, d1, *s, c1 );
-   v3_muladds( p2, d2, *t, c2 );
-
-   v3f v0;
-   v3_sub( c1, c2, v0 );
-   return v3_length2( v0 );
-}
-
-VG_STATIC void closest_point_aabb( v3f p, boxf box, v3f dest )
-{
-   v3_maxv( p, box[0], dest );
-   v3_minv( dest, box[1], dest );
-}
-
-VG_STATIC void closest_point_obb( v3f p, rigidbody *rb, v3f dest )
-{
-   v3f local;
-   m4x3_mulv( rb->to_local, p, local );
-   closest_point_aabb( local, rb->bbx, local );
-   m4x3_mulv( rb->to_world, local, dest );
-}
-
-VG_STATIC float closest_point_segment( v3f a, v3f b, v3f point, v3f dest )
-{
-   v3f v0, v1;
-   v3_sub( b, a, v0 );
-   v3_sub( point, a, v1 );
-
-   float t = v3_dot( v1, v0 ) / v3_length2(v0);
-   t = vg_clampf(t,0.0f,1.0f);
-   v3_muladds( a, v0, t, dest );
-   return t;
-}
-
-VG_STATIC void closest_on_triangle( v3f p, v3f tri[3], v3f dest )
-{
-   v3f ab, ac, ap;
-   float d1, d2;
-
-   /* Region outside A */
-   v3_sub( tri[1], tri[0], ab );
-   v3_sub( tri[2], tri[0], ac );
-   v3_sub( p, tri[0], ap );
-   
-   d1 = v3_dot(ab,ap);
-   d2 = v3_dot(ac,ap);
-   if( d1 <= 0.0f && d2 <= 0.0f ) 
-   {
-      v3_copy( tri[0], dest );
-      v3_copy( (v3f){INFINITY,INFINITY,INFINITY}, dest );
-      return;
-   }
-
-   /* Region outside B */
-   v3f bp;
-   float d3, d4;
-
-   v3_sub( p, tri[1], bp );
-   d3 = v3_dot( ab, bp );
-   d4 = v3_dot( ac, bp );
-
-   if( d3 >= 0.0f && d4 <= d3 )
-   {
-      v3_copy( tri[1], dest );
-      v3_copy( (v3f){INFINITY,INFINITY,INFINITY}, dest );
-      return;
-   }
-   
-   /* Edge region of AB */
-   float vc = d1*d4 - d3*d2;
-   if( vc <= 0.0f && d1 >= 0.0f && d3 <= 0.0f )
-   {
-      float v = d1 / (d1-d3);
-      v3_muladds( tri[0], ab, v, dest );
-      v3_copy( (v3f){INFINITY,INFINITY,INFINITY}, dest );
-      return;
-   }
-
-   /* Region outside C */
-   v3f cp;
-   float d5, d6;
-   v3_sub( p, tri[2], cp );
-   d5 = v3_dot(ab, cp);
-   d6 = v3_dot(ac, cp);
-   
-   if( d6 >= 0.0f && d5 <= d6 )
-   {
-      v3_copy( tri[2], dest );
-      v3_copy( (v3f){INFINITY,INFINITY,INFINITY}, dest );
-      return;
-   }
-
-   /* Region of AC */
-   float vb = d5*d2 - d1*d6;
-   if( vb <= 0.0f && d2 >= 0.0f && d6 <= 0.0f )
-   {
-      float w = d2 / (d2-d6);
-      v3_muladds( tri[0], ac, w, dest );
-      v3_copy( (v3f){INFINITY,INFINITY,INFINITY}, dest );
-      return;
-   }
-
-   /* Region of BC */
-   float va = d3*d6 - d5*d4;
-   if( va <= 0.0f && (d4-d3) >= 0.0f && (d5-d6) >= 0.0f )
-   {
-      float w = (d4-d3) / ((d4-d3) + (d5-d6));
-      v3f bc;
-      v3_sub( tri[2], tri[1], bc );
-      v3_muladds( tri[1], bc, w, dest );
-      v3_copy( (v3f){INFINITY,INFINITY,INFINITY}, dest );
-      return;
-   }
-
-   /* P inside region, Q via barycentric coordinates uvw */
-   float d = 1.0f/(va+vb+vc),
-         v = vb*d,
-         w = vc*d;
-
-   v3_muladds( tri[0], ab, v, dest );
-   v3_muladds( dest, ac, w, dest );
-}
-
-VG_STATIC void closest_on_triangle_1( v3f p, v3f tri[3], v3f dest )
-{
-   v3f ab, ac, ap;
-   float d1, d2;
-
-   /* Region outside A */
-   v3_sub( tri[1], tri[0], ab );
-   v3_sub( tri[2], tri[0], ac );
-   v3_sub( p, tri[0], ap );
-   
-   d1 = v3_dot(ab,ap);
-   d2 = v3_dot(ac,ap);
-   if( d1 <= 0.0f && d2 <= 0.0f ) 
-   {
-      v3_copy( tri[0], dest );
-      return;
-   }
-
-   /* Region outside B */
-   v3f bp;
-   float d3, d4;
-
-   v3_sub( p, tri[1], bp );
-   d3 = v3_dot( ab, bp );
-   d4 = v3_dot( ac, bp );
-
-   if( d3 >= 0.0f && d4 <= d3 )
-   {
-      v3_copy( tri[1], dest );
-      return;
-   }
-   
-   /* Edge region of AB */
-   float vc = d1*d4 - d3*d2;
-   if( vc <= 0.0f && d1 >= 0.0f && d3 <= 0.0f )
-   {
-      float v = d1 / (d1-d3);
-      v3_muladds( tri[0], ab, v, dest );
-      return;
-   }
-
-   /* Region outside C */
-   v3f cp;
-   float d5, d6;
-   v3_sub( p, tri[2], cp );
-   d5 = v3_dot(ab, cp);
-   d6 = v3_dot(ac, cp);
-   
-   if( d6 >= 0.0f && d5 <= d6 )
-   {
-      v3_copy( tri[2], dest );
-      return;
-   }
-
-   /* Region of AC */
-   float vb = d5*d2 - d1*d6;
-   if( vb <= 0.0f && d2 >= 0.0f && d6 <= 0.0f )
-   {
-      float w = d2 / (d2-d6);
-      v3_muladds( tri[0], ac, w, dest );
-      return;
-   }
-
-   /* Region of BC */
-   float va = d3*d6 - d5*d4;
-   if( va <= 0.0f && (d4-d3) >= 0.0f && (d5-d6) >= 0.0f )
-   {
-      float w = (d4-d3) / ((d4-d3) + (d5-d6));
-      v3f bc;
-      v3_sub( tri[2], tri[1], bc );
-      v3_muladds( tri[1], bc, w, dest );
-      return;
-   }
-
-   /* P inside region, Q via barycentric coordinates uvw */
-   float d = 1.0f/(va+vb+vc),
-         v = vb*d,
-         w = vc*d;
-
-   v3_muladds( tri[0], ab, v, dest );
-   v3_muladds( dest, ac, w, dest );
-}
 
 /*
  * -----------------------------------------------------------------------------
@@ -850,7 +568,7 @@ VG_STATIC int rb_manifold_apply_filtered( rb_ct *man, int len )
    {
       rb_ct *ct = &man[i];
 
-      if( ct->disabled )
+      if( ct->type == k_contact_type_disabled )
          continue;
 
       man[k ++] = man[i];
@@ -859,6 +577,52 @@ VG_STATIC int rb_manifold_apply_filtered( rb_ct *man, int len )
    return k;
 }
 
+VG_STATIC void rb_manifold_filter_joint_edges( rb_ct *man, int len, float r )
+{
+   for( int i=0; i<len-1; i++ )
+   {
+      rb_ct *ci = &man[i];
+      if( ci->type != k_contact_type_edge ) 
+         continue;
+
+      for( int j=i+1; j<len; j++ )
+      {
+         rb_ct *cj = &man[j];
+         if( cj->type != k_contact_type_edge ) 
+            continue;
+
+         if( v3_dist2( ci->co, cj->co ) < r*r )
+         {
+            cj->type = k_contact_type_disabled;
+            ci->p = (ci->p + cj->p) * 0.5f;
+
+            v3_add( ci->co, cj->co, ci->co );
+            v3_muls( ci->co, 0.5f, ci->co );
+
+            v3f delta;
+            v3_sub( ci->rba->co, ci->co, delta );
+
+            float c0 = v3_dot( ci->n, delta ),
+                  c1 = v3_dot( cj->n, delta );
+
+            if( c0 < 0.0f || c1 < 0.0f )
+            {
+               /* error */
+               ci->type = k_contact_type_disabled;
+            }
+            else
+            {
+               v3f n;
+               v3_muls( ci->n, c0, n );
+               v3_muladds( n, cj->n, c1, n );
+               v3_normalize( n );
+               v3_copy( n, ci->n );
+            }
+         }
+      }
+   }
+}
+
 /*
  * Resolve overlapping pairs
  */
@@ -869,17 +633,17 @@ VG_STATIC void rb_manifold_filter_pairs( rb_ct *man, int len, float r )
       rb_ct *ci = &man[i];
       int similar = 0;
 
-      if( ci->disabled ) continue;
+      if( ci->type == k_contact_type_disabled ) continue;
 
       for( int j=i+1; j<len; j++ )
       {
          rb_ct *cj = &man[j];
 
-         if( cj->disabled ) continue;
+         if( cj->type == k_contact_type_disabled ) continue;
 
          if( v3_dist2( ci->co, cj->co ) < r*r )
          {
-            cj->disabled = 1;
+            cj->type = k_contact_type_disabled;
             v3_add( cj->n, ci->n, ci->n );
             ci->p += cj->p;
             similar ++;
@@ -893,7 +657,7 @@ VG_STATIC void rb_manifold_filter_pairs( rb_ct *man, int len, float r )
          ci->p *= n;
 
          if( v3_length2(ci->n) < 0.1f*0.1f )
-            ci->disabled = 1;
+            ci->type = k_contact_type_disabled;
          else
             v3_normalize( ci->n );
       }
@@ -908,13 +672,14 @@ VG_STATIC void rb_manifold_filter_backface( rb_ct *man, int len )
    for( int i=0; i<len; i++ )
    {
       rb_ct *ct = &man[i];
-      if( ct->disabled ) continue;
+      if( ct->type == k_contact_type_disabled ) 
+         continue;
 
       v3f delta;
       v3_sub( ct->co, ct->rba->co, delta );
       
       if( v3_dot( delta, ct->n ) > -0.001f )
-         ct->disabled = 1;
+         ct->type = k_contact_type_disabled;
    }
 }
 
@@ -923,23 +688,31 @@ VG_STATIC void rb_manifold_filter_backface( rb_ct *man, int len )
  */
 VG_STATIC void rb_manifold_filter_coplanar( rb_ct *man, int len, float w )
 {
-   for( int i=0; i<len-1; i++ )
+   for( int i=0; i<len; i++ )
    {
       rb_ct *ci = &man[i];
-      if( ci->disabled ) continue;
+      if( ci->type == k_contact_type_disabled ||
+          ci->type == k_contact_type_edge ) 
+         continue;
 
       float d1 = v3_dot( ci->co, ci->n );
 
-      for( int j=i+1; j<len; j++ )
+      for( int j=0; j<len; j++ )
       {
+         if( j == i )
+            continue;
+
          rb_ct *cj = &man[j];
-         if( cj->disabled ) continue;
+         if( cj->type == k_contact_type_disabled ) 
+            continue;
          
          float d2 = v3_dot( cj->co, ci->n ),
                d  = d2-d1;
 
          if( fabsf( d ) <= w )
-            cj->disabled = 1;
+         {
+            cj->type = k_contact_type_disabled;
+         }
       }
    }
 }
@@ -1038,7 +811,7 @@ VG_STATIC int rb_capsule_manifold_done( rigidbody *rba, rigidbody *rbb,
       ct->p = manifold->r0 - d;
       ct->rba = rba;
       ct->rbb = rbb;
-      ct->disabled = 0;
+      ct->type = k_contact_type_default;
 
       count ++;
    }
@@ -1058,7 +831,7 @@ VG_STATIC int rb_capsule_manifold_done( rigidbody *rba, rigidbody *rbb,
       ct->p = manifold->r1 - d;
       ct->rba = rba;
       ct->rbb = rbb;
-      ct->disabled = 0;
+      ct->type = k_contact_type_default;
 
       count ++;
    }
@@ -1106,7 +879,7 @@ VG_STATIC int rb_capsule_sphere( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
 
       ct->rba = rba;
       ct->rbb = rbb;
-      ct->disabled = 0;
+      ct->type = k_contact_type_default;
 
       return 1;
    }
@@ -1280,7 +1053,7 @@ VG_STATIC int rb_sphere_box( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
 {
    v3f co, delta;
 
-   closest_point_obb( rba->co, rbb, co );
+   closest_point_obb( rba->co, rbb->bbx, rbb->to_world, rbb->to_local, co );
    v3_sub( rba->co, co, delta );
 
    float d2 = v3_length2(delta),
@@ -1327,7 +1100,7 @@ VG_STATIC int rb_sphere_box( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
 
       ct->rba = rba;
       ct->rbb = rbb;
-      ct->disabled = 0;
+      ct->type = k_contact_type_default;
       return 1;
    }
 
@@ -1354,7 +1127,7 @@ VG_STATIC int rb_sphere_sphere( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
       v3_muladds( rbb->co, ct->n, rbb->inf.sphere.radius, p1 );
       v3_add( p0, p1, ct->co );
       v3_muls( ct->co, 0.5f, ct->co );
-      ct->disabled = 0;
+      ct->type = k_contact_type_default;
       ct->p = r-d;
       ct->rba = rba;
       ct->rbb = rbb;
@@ -1364,7 +1137,7 @@ VG_STATIC int rb_sphere_sphere( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
    return 0;
 }
 
-#define RIGIDBODY_DYNAMIC_MESH_EDGES
+//#define RIGIDBODY_DYNAMIC_MESH_EDGES
 
 VG_STATIC int rb_sphere_triangle( rigidbody *rba, rigidbody *rbb, 
                                   v3f tri[3], rb_ct *buf )
@@ -1372,16 +1145,13 @@ VG_STATIC int rb_sphere_triangle( rigidbody *rba, rigidbody *rbb,
    v3f delta, co;
 
 #ifdef RIGIDBODY_DYNAMIC_MESH_EDGES
-   closest_on_triangle( rba->co, tri, co );
-#else
    closest_on_triangle_1( rba->co, tri, co );
+#else
+   enum contact_type type = closest_on_triangle_1( rba->co, tri, co );
 #endif
 
    v3_sub( rba->co, co, delta );
 
-   vg_line( rba->co, co, 0xffff0000 );
-   vg_line_pt3( rba->co, 0.1f, 0xff00ffff );
-
    float d2 = v3_length2( delta ),
           r = rba->inf.sphere.radius;
 
@@ -1399,7 +1169,7 @@ VG_STATIC int rb_sphere_triangle( rigidbody *rba, rigidbody *rbb,
       float d = sqrtf(d2);
 
       v3_copy( co, ct->co );
-      ct->disabled = 0;
+      ct->type = type;
       ct->p = r-d;
       ct->rba = rba;
       ct->rbb = rbb;
@@ -1409,213 +1179,135 @@ VG_STATIC int rb_sphere_triangle( rigidbody *rba, rigidbody *rbb,
    return 0;
 }
 
-VG_STATIC int rb_sphere_scene( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
-{
-   scene *sc = rbb->inf.scene.bh_scene->user;
-   
-   u32 geo[128];
 
-   int len = bh_select( rbb->inf.scene.bh_scene, rba->bbx_world, geo, 128 );
-   int count = 0;
-
-#ifdef RIGIDBODY_DYNAMIC_MESH_EDGES
-   /* !experimental! build edge array on the fly. time could be improved! */
+VG_STATIC void rb_debug_sharp_scene_edges( rigidbody *rbb, float sharp_ang,
+                                           boxf box, u32 colour )
+{
+   sharp_ang = cosf( sharp_ang );
 
-   v3f co_picture[128*3];
-   int unique_cos = 0;
+   scene *sc = rbb->inf.scene.bh_scene->user;
+   vg_line_boxf( box, 0xff00ff00 );
 
-   struct face_info
+   bh_iter it;
+   bh_iter_init( 0, &it );
+   int idx;
+   
+   while( bh_next( rbb->inf.scene.bh_scene, &it, box, &idx ) )
    {
-      int unique_cos[3];   /* indexes co_picture array */
-      int collided;
-      v3f normal;
-      u32 element_id;
-   }
-   faces[128];
+      u32 *ptri = &sc->arrindices[ idx*3 ];
+      v3f tri[3];
 
-   /* create geometry picture */
-   for( int i=0; i<len; i++ )
-   {
-      u32 *tri_indices = &sc->arrindices[ geo[i]*3 ];
-      struct face_info *inf = &faces[i];
-      inf->element_id = tri_indices[0];
-      inf->collided = 0;
+      for( int j=0; j<3; j++ )
+         v3_copy( sc->arrvertices[ptri[j]].co, tri[j] );
 
       for( int j=0; j<3; j++ )
       {
-         struct mdl_vert *pvert = &sc->arrvertices[tri_indices[j]];
-
-         for( int k=0; k<unique_cos; k++ )
-         {
-            if( v3_dist( pvert->co, co_picture[k] ) < 0.01f*0.01f )
-            {
-               inf->unique_cos[j] = k;
-               goto next_vert;
-            }
-         }
-
-         inf->unique_cos[j] = unique_cos;
-         v3_copy( pvert->co, co_picture[ unique_cos ++ ] );
-next_vert:;
-      }
+#if 0
+         v3f edir;
+         v3_sub( tri[(j+1)%3], tri[j], edir );
 
-      v3f ab, ac;
-      v3_sub( co_picture[inf->unique_cos[2]], 
-              co_picture[inf->unique_cos[0]], ab );
+         if( v3_dot( edir, (v3f){ 0.5184758473652127f,
+                                  0.2073903389460850f,
+                                 -0.8295613557843402f } ) < 0.0f )
+            continue;
+#endif
 
-      v3_sub( co_picture[inf->unique_cos[1]],
-              co_picture[inf->unique_cos[0]], ac );
-      v3_cross( ac, ab, inf->normal );
-      v3_normalize( inf->normal );
-   }
+         bh_iter jt;
+         bh_iter_init( 0, &jt );
 
+         boxf region;
+         float const k_r = 0.02f;
+         v3_add( (v3f){  k_r,  k_r,  k_r }, tri[j], region[1] );
+         v3_add( (v3f){ -k_r, -k_r, -k_r }, tri[j], region[0] );
 
-   /* build edges brute force */
-   int edge_picture[ 128*3 ][4];
-   int unique_edges = 0;
+         int jdx;
+         while( bh_next( rbb->inf.scene.bh_scene, &jt, region, &jdx ) )
+         {
+            if( idx <= jdx )
+               continue;
 
-   for( int i=0; i<len; i++ )
-   {
-      struct face_info *inf = &faces[i];
-      
-      for( int j=0; j<3; j++ )
-      {
-         int i0 = j,
-             i1 = (j+1)%3,
-             e0 = VG_MIN( inf->unique_cos[i0], inf->unique_cos[i1] ),
-             e1 = VG_MAX( inf->unique_cos[i0], inf->unique_cos[i1] ),
-             matched = 0;
+            u32 *ptrj = &sc->arrindices[ jdx*3 ];
+            v3f trj[3];
 
-         for( int k=0; k<unique_edges; k ++ )
-         {
-            int k0 = VG_MIN( edge_picture[k][0], edge_picture[k][1] ),
-                k1 = VG_MAX( edge_picture[k][0], edge_picture[k][1] );
+            for( int k=0; k<3; k++ )
+               v3_copy( sc->arrvertices[ptrj[k]].co, trj[k] );
 
-            /* matched ! */
-            if( (k0 == e0) && (k1 == e1) )
+            for( int k=0; k<3; k++ )
             {
-               edge_picture[ k ][3] = i;
-               matched = 1;
-               break;
+               if( v3_dist2( tri[j], trj[k] ) <= k_r*k_r )
+               {
+                  int jp1 = (j+1)%3,
+                      jp2 = (j+2)%3,
+                      km1 = (k+3-1)%3,
+                      km2 = (k+3-2)%3;
+
+                  if( v3_dist2( tri[jp1], trj[km1] ) <= k_r*k_r )
+                  {
+                     v3f b0, b1, b2;
+                     v3_sub( tri[jp1], tri[j], b0 );
+                     v3_sub( tri[jp2], tri[j], b1 );
+                     v3_sub( trj[km2], tri[j], b2 );
+
+                     v3f cx0, cx1;
+                     v3_cross( b0, b1, cx0 );
+                     v3_cross( b2, b0, cx1 );
+
+                     float polarity = v3_dot( cx0, b2 );
+
+                     if( polarity < 0.0f )
+                     {
+#if 0
+                        vg_line( tri[j], tri[jp1], 0xff00ff00 );
+                        float ang = v3_dot(cx0,cx1) / 
+                                       (v3_length(cx0)*v3_length(cx1));
+                        if( ang < sharp_ang )
+                        {
+                           vg_line( tri[j], tri[jp1], 0xff00ff00 );
+                        }
+#endif
+                     }
+                  }
+               }
             }
          }
-      
-         if( !matched )
-         {
-            /* create new edge */
-            edge_picture[ unique_edges ][0] = inf->unique_cos[i0];
-            edge_picture[ unique_edges ][1] = inf->unique_cos[i1];
-
-            edge_picture[ unique_edges ][2] = i;
-            edge_picture[ unique_edges ][3] = -1;
-
-            unique_edges ++;
-         }
       }
    }
-#endif
-
-   v3f tri[3];
-
-   for( int i=0; i<len; i++ )
-   {
-#ifdef RIGIDBODY_DYNAMIC_MESH_EDGES
-      struct face_info *inf = &faces[i];
+}
 
-      float *v0 = co_picture[inf->unique_cos[0]],
-            *v1 = co_picture[inf->unique_cos[1]],
-            *v2 = co_picture[inf->unique_cos[2]];
+VG_STATIC int rb_sphere_scene( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
+{
+   scene *sc = rbb->inf.scene.bh_scene->user;
 
-      v3_copy( v0, tri[0] );
-      v3_copy( v1, tri[1] );
-      v3_copy( v2, tri[2] );
+   bh_iter it;
+   bh_iter_init( 0, &it );
+   int idx;
 
-      buf[count].element_id = inf->element_id;
-#else
-      u32 *ptri = &sc->arrindices[ geo[i]*3 ];
+   int count = 0;
+   
+   while( bh_next( rbb->inf.scene.bh_scene, &it, rba->bbx_world, &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];
-#endif
+      buf[ count ].element_id = ptri[0];
 
-      vg_line( tri[0],tri[1],0x10ffffff );
-      vg_line( tri[1],tri[2],0x10ffffff );
-      vg_line( tri[2],tri[0],0x10ffffff );
+      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( rba, rbb, tri, buf+count );
-
-#ifdef RIGIDBODY_DYNAMIC_MESH_EDGES
-      if( contact )
-         inf->collided = 1;
-#endif
       count += contact;
 
-      if( count == 12 )
+      if( count == 16 )
       {
          vg_warn( "Exceeding sphere_vs_scene capacity. Geometry too dense!\n" );
          return count;
       }
    }
 
-#ifdef RIGIDBODY_DYNAMIC_MESH_EDGES
-   for( int i=0; i<unique_edges; i++ )
-   {
-      int *edge = edge_picture[i];
-
-      if( edge[3] == -1 )
-         continue;
-
-      struct face_info *inf_i = &faces[edge[2]],
-                       *inf_j = &faces[edge[3]];
-      
-      if( inf_i->collided || inf_j->collided )
-         continue;
-
-      v3f co, delta;
-      closest_point_segment( co_picture[edge[0]], co_picture[edge[1]], 
-                             rba->co, co );
-   
-      v3_sub( rba->co, co, delta );
-      float d2 = v3_length2( delta ),
-            r  = rba->inf.sphere.radius;
-
-      if( d2 < r*r )
-      {
-         float d = sqrtf(d2);
-
-         v3_muls( delta, 1.0f/d, delta );
-         float c0 = v3_dot( inf_i->normal, delta ),
-               c1 = v3_dot( inf_j->normal, delta );
-
-         if( c0 < 0.0f || c1 < 0.0f )
-            continue;
-
-         rb_ct *ct = buf+count;
-
-         v3_muls( inf_i->normal, c0, ct->n );
-         v3_muladds( ct->n, inf_j->normal, c1, ct->n );
-         v3_normalize( ct->n );
-
-         v3_copy( co, ct->co );
-         ct->disabled = 0;
-         ct->p = r-d;
-         ct->rba = rba;
-         ct->rbb = rbb;
-         ct->element_id = inf_i->element_id;
-
-         count ++;
-
-         if( count == 12 )
-         {
-            vg_warn( "Geometry too dense!\n" );
-            return count;
-         }
-      }
-   }
-#endif
-
    return count;
 }
 
@@ -1623,15 +1315,17 @@ VG_STATIC int rb_box_scene( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
 {
    scene *sc = rbb->inf.scene.bh_scene->user;
    
-   u32 geo[128];
    v3f tri[3];
-   int len = bh_select( rbb->inf.scene.bh_scene, rba->bbx_world, geo, 128 );
 
-   int count = 0;
+   bh_iter it;
+   bh_iter_init( 0, &it );
+   int idx;
 
-   for( int i=0; i<len; i++ )
+   int count = 0;
+   
+   while( bh_next( rbb->inf.scene.bh_scene, &it, rba->bbx_world, &idx ) )
    {
-      u32 *ptri = &sc->arrindices[ geo[i]*3 ];
+      u32 *ptri = &sc->arrindices[ idx*3 ];
 
       for( int j=0; j<3; j++ )
          v3_copy( sc->arrvertices[ptri[j]].co, tri[j] );
@@ -1748,7 +1442,7 @@ VG_STATIC int rb_box_scene( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
          if( ct->p < 0.0f )
             continue;
 
-         ct->disabled = 0;
+         ct->type = k_contact_type_default;
          ct->rba = rba;
          ct->rbb = rbb;
          count ++;
@@ -1854,7 +1548,9 @@ VG_STATIC void rb_presolve_contacts( rb_ct *buffer, int len )
       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] );
 
-      ct->disabled = 0;
+#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;
diff --git a/scene.h b/scene.h
index 5d436875cfd3187a7efd04ec980f5ee55699d2ce..7fc29f3ad94c8e2a2019d6d964bd76b99aa00537 100644 (file)
--- a/scene.h
+++ b/scene.h
@@ -4,6 +4,7 @@
 #include "common.h"
 #include "model.h"
 #include "bvh.h"
+#include "distq.h"
 
 typedef struct scene scene;
 
@@ -100,6 +101,10 @@ VG_STATIC void scene_add_submesh( scene *pscene, mdl_context *mdl,
       pvert->colour[1] = src->colour[1];
       pvert->colour[2] = src->colour[2];
       pvert->colour[3] = src->colour[3];
+      pvert->weights[0] = src->weights[0];
+      pvert->weights[1] = src->weights[1];
+      pvert->weights[2] = src->weights[2];
+      pvert->weights[3] = src->weights[3];
       v2_copy( src->uv, pvert->uv );
    }
 
@@ -296,10 +301,23 @@ VG_STATIC int scene_bh_ray( void *user, u32 index, v3f co,
    return 0;
 }
 
+VG_STATIC void scene_bh_closest( void *user, u32 index, v3f point, v3f closest )
+{
+   scene *s = user;
+
+   v3f positions[3];
+   u32 *tri = &s->arrindices[ index*3 ];
+   for( int i=0; i<3; i++ )
+      v3_copy( s->arrvertices[tri[i]].co, positions[i] );
+
+   closest_on_triangle_1( point, positions, closest );
+}
+
 VG_STATIC bh_system bh_system_scene = 
 {
    .expand_bound = scene_bh_expand_bound,
    .item_centroid = scene_bh_centroid,
+   .item_closest = scene_bh_closest,
    .item_swap = scene_bh_swap,
    .item_debug = scene_bh_debug,
    .cast_ray = scene_bh_ray
diff --git a/world.h b/world.h
index a92591e294963636653dba6f7c294304aeb297ac..bf58144a3b8727c9b80fc5fea3bfa7752d1fa968 100644 (file)
--- a/world.h
+++ b/world.h
@@ -306,10 +306,18 @@ VG_STATIC struct gworld
          *scene_no_collide,
          *scene_lines;
 
+   struct grind_edge
+   {
+      v3f p0, p1;
+   }
+   *grind_edges;
+   u32 grind_edge_count;
+
    /* spacial mappings */
    bh_tree *audio_bh,
            *trigger_bh,
-           *geo_bh;
+           *geo_bh,
+           *grind_bh;
 
    /* graphics */
    glmesh mesh_route_lines;
index 147c9af489eb2be321b6f40249e65d4ff1fa3671..96928b60bd166b05fb58c52810b43f6f83b4a130 100644 (file)
@@ -306,6 +306,102 @@ VG_STATIC void world_entities_process(void)
    }
 }
 
+VG_STATIC void edge_bh_expand_bound( void *user, boxf bound, u32 item_index )
+{
+   struct grind_edge *edge_array = user,
+                     *edge = &edge_array[ item_index ];
+   
+  box_addpt( bound, edge->p0 );
+  box_addpt( bound, edge->p1 );
+}
+
+VG_STATIC float edge_bh_centroid( void *user, u32 item_index, int axis )
+{
+   struct grind_edge *edge_array = user,
+                     *edge = &edge_array[ item_index ];
+
+   return (edge->p0[axis] + edge->p1[axis]) * 0.5f;
+}
+
+VG_STATIC void edge_bh_swap( void *user, u32 ia, u32 ib )
+{
+   struct grind_edge *edge_array = user,
+                     *e0 = &edge_array[ ia ],
+                     *e1 = &edge_array[ ib ],
+                      et;
+   et = *e0;
+   *e0 = *e1;
+   *e1 = et;
+}
+
+VG_STATIC void edge_bh_closest( void *user, u32 index, v3f point, v3f closest )
+{
+   struct grind_edge *edge_array = user,
+                     *edge = &edge_array[ index ];
+   
+   closest_point_segment( edge->p0, edge->p1, point, closest );
+}
+
+VG_STATIC bh_system bh_system_edges =
+{
+   .expand_bound = edge_bh_expand_bound,
+   .item_centroid = edge_bh_centroid,
+   .item_closest = edge_bh_closest,
+   .item_swap = edge_bh_swap,
+   .item_debug = NULL,
+   .cast_ray = NULL
+};
+
+VG_STATIC void world_generate_edges(void)
+{
+   vg_info( "Generating edge array\n" );
+   world.grind_edges = vg_linear_alloc( world.dynamic_vgl, 
+                                        5000*sizeof(struct grind_edge ) );
+   world.grind_edge_count = 0;
+
+   u32 fs_count = 0;
+   for( u32 i=0; i<world.scene_geo->vertex_count; i++ )
+      if( world.scene_geo->arrvertices[i].weights[0] )
+         fs_count ++;
+
+   vg_info( "Grind verts: %u\n", fs_count );
+
+   for( u32 i=0; i<world.scene_geo->indice_count/3; i++ )
+   {
+      u32 *ptri = &world.scene_geo->arrindices[ i*3 ];
+
+      for( int j=0; j<3; j++ )
+      {
+         u32 i0 = ptri[j],
+             i1 = ptri[(j+1)%3];
+
+         mdl_vert *v0 = &world.scene_geo->arrvertices[ i0 ],
+                  *v1 = &world.scene_geo->arrvertices[ i1 ];
+
+         if( v0->weights[0] )
+         {
+            if( world.grind_edge_count == 5000 )
+               vg_fatal_exit_loop( "Edge capacity exceeded" );
+
+            struct grind_edge *ge = 
+               &world.grind_edges[ world.grind_edge_count ++ ];
+
+            v3_copy( v0->co, ge->p0 );
+            v3_copy( v1->co, ge->p1 );
+         }
+      }
+   }
+
+   vg_info( "Grind edge count: %u\n", world.grind_edge_count );
+
+   world.grind_edges = vg_linear_resize( world.dynamic_vgl, world.grind_edges,
+                           world.grind_edge_count*sizeof(struct grind_edge) );
+
+   world.grind_bh = bh_create( world.dynamic_vgl, &bh_system_edges, 
+                               world.grind_edges, world.grind_edge_count, 
+                               2 );
+}
+
 VG_STATIC void world_generate(void)
 {
    /* 
@@ -397,6 +493,8 @@ VG_STATIC void world_generate(void)
 
    vg_linear_del( world.dynamic_vgl, world.scene_no_collide );
    world.scene_no_collide = NULL;
+
+   world_generate_edges();
 }
 
 VG_STATIC int reset_player( int argc, char const *argv[] );
@@ -600,7 +698,10 @@ VG_STATIC void world_unload(void)
    world.scene_geo = NULL;
    world.scene_no_collide = NULL;
    world.scene_lines = NULL;
+   world.grind_edges = NULL;
+   world.grind_edge_count = 0;
 
+   world.grind_bh = NULL;
    world.geo_bh = NULL;
    world.trigger_bh = NULL;
    world.audio_bh = NULL;