+static void draw_angle_limit( v3f c, v3f major, v3f minor,
+ float amin, float amax, float measured,
+ u32 colour )
+{
+ float f = 0.05f;
+ v3f ay, ax;
+ v3_muls( major, f, ay );
+ v3_muls( minor, f, ax );
+
+ for( int x=0; x<16; x++ )
+ {
+ float t0 = (float)x / 16.0f,
+ t1 = (float)(x+1) / 16.0f,
+ a0 = vg_lerpf( amin, amax, t0 ),
+ a1 = vg_lerpf( amin, amax, t1 );
+
+ v3f p0, p1;
+ v3_muladds( c, ay, cosf(a0), p0 );
+ v3_muladds( p0, ax, sinf(a0), p0 );
+ v3_muladds( c, ay, cosf(a1), p1 );
+ v3_muladds( p1, ax, sinf(a1), p1 );
+
+ vg_line( p0, p1, colour );
+
+ if( x == 0 )
+ vg_line( c, p0, colour );
+ if( x == 15 )
+ vg_line( c, p1, colour );
+ }
+
+ v3f p2;
+ v3_muladds( c, ay, cosf(measured)*1.2f, p2 );
+ v3_muladds( p2, ax, sinf(measured)*1.2f, p2 );
+ vg_line( c, p2, colour );
+}
+
+static void rb_debug_constraint_limits( rigidbody *ra, rigidbody *rb, v3f lca,
+ v3f limits[2] )
+{
+ v3f ax, ay, az, bx, by, bz;
+ m3x3_mulv( ra->to_world, (v3f){1.0f,0.0f,0.0f}, ax );
+ m3x3_mulv( ra->to_world, (v3f){0.0f,1.0f,0.0f}, ay );
+ m3x3_mulv( ra->to_world, (v3f){0.0f,0.0f,1.0f}, az );
+ m3x3_mulv( rb->to_world, (v3f){1.0f,0.0f,0.0f}, bx );
+ m3x3_mulv( rb->to_world, (v3f){0.0f,1.0f,0.0f}, by );
+ m3x3_mulv( rb->to_world, (v3f){0.0f,0.0f,1.0f}, bz );
+
+ v2f px, py, pz;
+ px[0] = v3_dot( ay, by );
+ px[1] = v3_dot( az, by );
+
+ py[0] = v3_dot( az, bz );
+ py[1] = v3_dot( ax, bz );
+
+ pz[0] = v3_dot( ax, bx );
+ pz[1] = v3_dot( ay, bx );
+
+ float r0 = atan2f( px[1], px[0] ),
+ r1 = atan2f( py[1], py[0] ),
+ r2 = atan2f( pz[1], pz[0] );
+
+ v3f c;
+ m4x3_mulv( ra->to_world, lca, c );
+ draw_angle_limit( c, ay, az, limits[0][0], limits[1][0], r0, 0xff0000ff );
+ draw_angle_limit( c, az, ax, limits[0][1], limits[1][1], r1, 0xff00ff00 );
+ draw_angle_limit( c, ax, ay, limits[0][2], limits[1][2], r2, 0xffff0000 );
+}
+
+static void rb_limit_cure( rigidbody *ra, rigidbody *rb, v3f axis, float d )
+{
+ if( d != 0.0f )
+ {
+ float avx = v3_dot( ra->w, axis ) - v3_dot( rb->w, axis );
+ float joint_mass = rb->inv_mass + ra->inv_mass;
+ joint_mass = 1.0f/joint_mass;
+
+ float bias = (0.04f * k_rb_rate) * d,
+ lambda = -(avx + bias) * joint_mass;
+
+ /* Angular velocity */
+ v3f wa, wb;
+ v3_muls( axis, lambda * ra->inv_mass, wa );
+ v3_muls( axis, -lambda * rb->inv_mass, wb );
+
+ v3_add( ra->w, wa, ra->w );
+ v3_add( rb->w, wb, rb->w );
+ }
+}
+
+static void rb_constraint_limits( rigidbody *ra, v3f lca,
+ rigidbody *rb, v3f lcb, v3f limits[2] )