+VG_STATIC int rb_contact_count = 0;
+
+typedef struct rb_constr_pos rb_constr_pos;
+typedef struct rb_constr_swingtwist rb_constr_swingtwist;
+
+struct rb_constr_pos
+{
+ rigidbody *rba, *rbb;
+ v3f lca, lcb;
+};
+
+struct rb_constr_swingtwist
+{
+ rigidbody *rba, *rbb;
+
+ v4f conevx, conevy; /* relative to rba */
+ v3f view_offset, /* relative to rba */
+ coneva, conevxb;/* relative to rbb */
+
+ int tangent_violation, axis_violation;
+ v3f axis, tangent_axis, tangent_target, axis_target;
+
+ float conet;
+ float tangent_mass, axis_mass;
+};
+
+/*
+ * -----------------------------------------------------------------------------
+ * Math Utils
+ * -----------------------------------------------------------------------------
+ */
+
+VG_STATIC float sphere_volume( float radius )
+{
+ float r3 = radius*radius*radius;
+ return (4.0f/3.0f) * VG_PIf * r3;
+}
+
+VG_STATIC void rb_tangent_basis( v3f n, v3f tx, v3f ty )
+{
+ /* Compute tangent basis (box2d) */
+ if( fabsf( n[0] ) >= 0.57735027f )
+ {
+ tx[0] = n[1];
+ tx[1] = -n[0];
+ tx[2] = 0.0f;
+ }
+ else
+ {
+ tx[0] = 0.0f;
+ tx[1] = n[2];
+ tx[2] = -n[1];
+ }
+
+ v3_normalize( tx );
+ v3_cross( n, tx, ty );
+}
+
+/*
+ * -----------------------------------------------------------------------------
+ * Debugging
+ * -----------------------------------------------------------------------------
+ */
+
+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 );
+ vg_line_pt3( ct->co, 0.0025f, 0xff0000ff );
+ vg_line( ct->co, p1, 0xffffffff );
+ }
+}
+
+VG_STATIC void debug_sphere( m4x3f m, float radius, u32 colour )
+{
+ v3f ly = { 0.0f, 0.0f, radius },
+ lx = { 0.0f, radius, 0.0f },
+ lz = { 0.0f, 0.0f, radius };
+
+ for( int i=0; i<16; i++ )
+ {
+ float t = ((float)(i+1) * (1.0f/16.0f)) * VG_PIf * 2.0f,
+ s = sinf(t),
+ c = cosf(t);
+
+ v3f py = { s*radius, 0.0f, c*radius },
+ px = { s*radius, c*radius, 0.0f },
+ pz = { 0.0f, s*radius, c*radius };
+
+ v3f p0, p1, p2, p3, p4, p5;
+ m4x3_mulv( m, py, p0 );
+ m4x3_mulv( m, ly, p1 );
+ m4x3_mulv( m, px, p2 );
+ m4x3_mulv( m, lx, p3 );
+ m4x3_mulv( m, pz, p4 );
+ m4x3_mulv( m, lz, p5 );
+
+ vg_line( p0, p1, colour == 0x00? 0xff00ff00: colour );
+ vg_line( p2, p3, colour == 0x00? 0xff0000ff: colour );
+ vg_line( p4, p5, colour == 0x00? 0xffff0000: colour );
+
+ v3_copy( py, ly );
+ v3_copy( px, lx );
+ v3_copy( pz, lz );
+ }
+}
+
+VG_STATIC void debug_capsule( m4x3f m, float radius, float h, u32 colour )
+{
+ v3f ly = { 0.0f, 0.0f, radius },
+ lx = { 0.0f, radius, 0.0f },
+ lz = { 0.0f, 0.0f, radius };
+
+ float s0 = sinf(0.0f)*radius,
+ c0 = cosf(0.0f)*radius;
+
+ v3f p0, p1, up, right, forward;
+ m3x3_mulv( m, (v3f){0.0f,1.0f,0.0f}, up );
+ m3x3_mulv( m, (v3f){1.0f,0.0f,0.0f}, right );
+ m3x3_mulv( m, (v3f){0.0f,0.0f,-1.0f}, forward );
+ v3_muladds( m[3], up, -h*0.5f+radius, p0 );
+ v3_muladds( m[3], up, h*0.5f-radius, p1 );
+
+ v3f a0, a1, b0, b1;
+ v3_muladds( p0, right, radius, a0 );
+ v3_muladds( p1, right, radius, a1 );
+ v3_muladds( p0, forward, radius, b0 );
+ v3_muladds( p1, forward, radius, b1 );
+ vg_line( a0, a1, colour );
+ vg_line( b0, b1, colour );
+
+ v3_muladds( p0, right, -radius, a0 );
+ v3_muladds( p1, right, -radius, a1 );
+ v3_muladds( p0, forward, -radius, b0 );
+ v3_muladds( p1, forward, -radius, b1 );
+ vg_line( a0, a1, colour );
+ vg_line( b0, b1, colour );
+
+ for( int i=0; i<16; i++ )
+ {
+ float t = ((float)(i+1) * (1.0f/16.0f)) * VG_PIf * 2.0f,
+ s1 = sinf(t)*radius,
+ c1 = cosf(t)*radius;
+
+ v3f e0 = { s0, 0.0f, c0 },
+ e1 = { s1, 0.0f, c1 },
+ e2 = { s0, c0, 0.0f },
+ e3 = { s1, c1, 0.0f },
+ e4 = { 0.0f, c0, s0 },
+ e5 = { 0.0f, c1, s1 };
+
+ m3x3_mulv( m, e0, e0 );
+ m3x3_mulv( m, e1, e1 );
+ m3x3_mulv( m, e2, e2 );
+ m3x3_mulv( m, e3, e3 );
+ m3x3_mulv( m, e4, e4 );
+ m3x3_mulv( m, e5, e5 );