+ vg_console.cheats = 1;
+ vg_lines.render = 1;
+}
+
+static void demo0_refit(void){
+ if( k_spacial == 0 ) return;
+
+ for( u32 i=0; i<(u32)k_shapes; i ++ ){
+ f32 h = shapes_inf[i].h,
+ r = shapes_inf[i].r;
+
+ rigidbody *rb = &shapes[i];
+
+ v3f p0, p1;
+ v3_muladds( rb->to_world[3], rb->to_world[1], -h*0.5f+r, p0 );
+ v3_muladds( rb->to_world[3], rb->to_world[1], h*0.5f-r, p1 );
+
+ v3f *bbx = shapes_bbx[i];
+ v3_minv( p0, p1, bbx[0] );
+ v3_maxv( p0, p1, bbx[1] );
+ v3_muladds( bbx[0], (v3f){-1,-1,-1}, r, bbx[0] );
+ v3_muladds( bbx[1], (v3f){ 1, 1, 1}, r, bbx[1] );
+ }
+
+ if( k_spacial == 1 ) return;
+
+ if( k_spacial == 2 )
+ bh_rebuild( shape_bvh_tree, (u32)k_shapes );
+}
+
+static void demo0(void){
+ vg_profile_begin( &prof_refit );
+ demo0_refit();
+ vg_profile_end( &prof_refit );
+
+ static rigidbody _null,
+ _mover;
+ rb_solver_reset();
+
+ f32 t = vg.time * 0.1f * VG_TAUf;
+ v3f sphere_pos = { sinf(t)*2.0f, -1, cosf(t)*2.0f };
+ _mover.v[0] = (sinf(t+vg.time_fixed_delta)-sinf(t))*2.0f;
+ _mover.v[2] = (cosf(t+vg.time_fixed_delta)-cosf(t))*2.0f;
+
+ for( u32 i=0; i<(u32)k_shapes; i ++ ){
+ rigidbody *rbi = &shapes[i];
+ rb_capsule *infi = &shapes_inf[i];
+ v3f *bbxi = shapes_bbx[i];
+
+ if( rb_global_has_space() ){
+ rb_ct *buf = rb_global_buffer();
+ m4x3f mtx;
+ m4x3_identity( mtx );
+ u32 l = rb_capsule__box( rbi->to_world, infi,
+ mtx, mtx, floor_box, buf );
+
+ for( u32 k=0; k<l; k ++ ){
+ buf[k].rba = rbi;
+ buf[k].rbb = &_null;
+ }
+
+ rb_contact_count += l;
+ }
+ else break;
+
+ if( rb_global_has_space() ){
+ rb_ct *buf = rb_global_buffer();
+ u32 l = rb_capsule__sphere( rbi->to_world, infi, sphere_pos, 1, buf );
+
+ for( u32 k=0; k<l; k ++ ){
+ buf[k].rba = rbi;
+ buf[k].rbb = &_mover;
+ }
+
+ rb_contact_count += l;
+ }
+
+ if( k_spacial == 2 ){
+ bh_iter it;
+ bh_iter_init_box( 0, &it, bbxi );
+ i32 idx;
+
+ while(1){
+ vg_profile_begin( &prof_broad );
+ if( !bh_next( shape_bvh_tree, &it, &idx ) ){
+ vg_profile_end( &prof_broad );
+ break;
+ }
+ vg_profile_end( &prof_broad );
+
+ if( idx <= i ) continue;
+
+ vg_profile_begin( &prof_narrow );
+
+ rigidbody *rbj = &shapes[idx];
+ v3f *bbxj = shapes_bbx[idx];
+ rb_capsule *infj = &shapes_inf[idx];
+
+ if( rb_global_has_space() ){
+ rb_ct *buf = rb_global_buffer();
+ u32 l = rb_capsule__capsule( rbi->to_world, infi,
+ rbj->to_world, infj, buf );
+
+ for( u32 k=0; k<l; k ++ ){
+ buf[k].rba = rbi;
+ buf[k].rbb = rbj;
+ }
+
+ rb_contact_count += l;
+ }
+
+ vg_profile_end( &prof_narrow );
+ }
+ }
+ else {
+ if( i == ((u32)k_shapes)-1 ){
+ break;
+ }
+ for( u32 j=i+1; j<(u32)k_shapes; j ++ ){
+ rigidbody *rbj = &shapes[j];
+ v3f *bbxj = shapes_bbx[j];
+ rb_capsule *infj = &shapes_inf[j];
+
+ if( k_spacial == 1 ){
+ vg_profile_begin( &prof_broad );
+ if( !box_overlap( bbxi, bbxj ) ){
+ vg_profile_end( &prof_broad );
+ continue;
+ }
+ }
+
+ vg_profile_begin( &prof_narrow );
+ if( rb_global_has_space() ){
+ rb_ct *buf = rb_global_buffer();
+ u32 l = rb_capsule__capsule( rbi->to_world, infi,
+ rbj->to_world, infj, buf );
+
+ for( u32 k=0; k<l; k ++ ){
+ buf[k].rba = rbi;
+ buf[k].rbb = rbj;
+ }
+
+ rb_contact_count += l;
+ }
+ vg_profile_end( &prof_narrow );
+ }
+ }
+ }
+
+ vg_profile_increment( &prof_broad );
+ vg_profile_increment( &prof_narrow );
+
+ vg_profile_begin( &prof_solve );
+ rb_presolve_contacts( rb_contact_buffer, rb_contact_count );
+ for( u32 i=0; i<(u32)k_iterations; i ++ )
+ rb_solve_contacts( rb_contact_buffer, rb_contact_count );
+
+ for( u32 i=0; i<(u32)k_shapes; i ++ ){
+ rigidbody *rbi = &shapes[i];
+ if( k_gyro ){
+ m3x3f I;
+ m3x3_inv( rbi->iI, I );
+ rb_solve_gyroscopic( rbi, I, vg.time_fixed_delta );
+ }
+ rb_iter( rbi );
+ rb_update_matrices( rbi );
+ }
+ vg_profile_end( &prof_solve );
+}
+
+static void demo1(void){
+ vg_profile_begin( &prof_refit );
+ vg_profile_end( &prof_refit );
+
+ vg_profile_increment( &prof_broad );
+ vg_profile_increment( &prof_narrow );
+
+ vg_profile_begin( &prof_solve );
+
+ v3_muladds( racket.v, (v3f){0,9.8f,0}, vg.time_fixed_delta, racket.v );
+ if( k_gyro ) rb_solve_gyroscopic( &racket, racket_I, vg.time_fixed_delta );
+ rb_iter( &racket );
+ rb_update_matrices( &racket );
+
+ vg_profile_end( &prof_solve );