+/*
+ * -----------------------------------------------------------------------------
+ * Math Utils
+ * -----------------------------------------------------------------------------
+ */
+
+static float sphere_volume( float radius )
+{
+ float r3 = radius*radius*radius;
+ return (4.0f/3.0f) * VG_PIf * r3;
+}
+
+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
+ * -----------------------------------------------------------------------------
+ */
+
+static void rb_debug_contact( rb_ct *ct )
+{
+ v3f p1;
+ v3_muladds( ct->co, ct->n, 0.1f, p1 );
+ vg_line_pt3( ct->co, 0.025f, 0xff0000ff );
+ vg_line( ct->co, p1, 0xffffffff );
+}
+
+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 );
+ }
+}
+
+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 );
+
+ v3_add( p0, e0, a0 );
+ v3_add( p0, e1, a1 );
+ v3_add( p1, e0, b0 );
+ v3_add( p1, e1, b1 );
+
+ vg_line( a0, a1, colour );
+ vg_line( b0, b1, colour );
+
+ if( c0 < 0.0f )
+ {
+ v3_add( p0, e2, a0 );
+ v3_add( p0, e3, a1 );
+ v3_add( p0, e4, b0 );
+ v3_add( p0, e5, b1 );
+ }
+ else
+ {
+ v3_add( p1, e2, a0 );
+ v3_add( p1, e3, a1 );
+ v3_add( p1, e4, b0 );
+ v3_add( p1, e5, b1 );
+ }
+
+ vg_line( a0, a1, colour );
+ vg_line( b0, b1, colour );
+
+ s0 = s1;
+ c0 = c1;
+ }
+}
+
+static void rb_debug( rigidbody *rb, u32 colour )
+{
+ if( rb->type == k_rb_shape_box )
+ {
+ v3f *box = rb->bbx;
+ vg_line_boxf_transformed( rb->to_world, rb->bbx, colour );
+ }
+ else if( rb->type == k_rb_shape_sphere )
+ {
+ debug_sphere( rb->to_world, rb->inf.sphere.radius, colour );
+ }
+ else if( rb->type == k_rb_shape_capsule )
+ {
+ m4x3f m0, m1;
+ float h = rb->inf.capsule.height,
+ r = rb->inf.capsule.radius;
+
+ debug_capsule( rb->to_world, r, h, colour );
+ }
+ else if( rb->type == k_rb_shape_scene )
+ {
+ vg_line_boxf( rb->bbx, colour );
+ }
+}
+
+/*
+ * -----------------------------------------------------------------------------
+ * Integration
+ * -----------------------------------------------------------------------------
+ */
+
+/*
+ * Update world space bounding box based on local one
+ */