From: hgn Date: Sat, 23 Jul 2022 05:44:50 +0000 (+0100) Subject: latest X-Git-Url: https://skaterift.com/git/?a=commitdiff_plain;h=be0f28b651e5c39943e476c0cd68c146e9952857;p=carveJwlIkooP6JGAAIwe30JlM.git latest --- diff --git a/character.h b/character.h index 3421d3a..daa2e99 100644 --- a/character.h +++ b/character.h @@ -4,8 +4,10 @@ #include "common.h" #include "model.h" #include "scene.h" +#include "world.h" #include "ik.h" #include "rigidbody.h" +#include "render.h" #include "shaders/character.h" vg_tex2d tex_pallet = { .path = "textures/ch_gradient.qoi" }; @@ -882,22 +884,14 @@ static void character_debug_ragdoll( struct character *ch ) static void character_ragdoll_iter( struct character *ch ) { + /* TODO: Lots of the RB functions unimplemented here currently */ + + return; rb_solver_reset(); for( int i=0; iragdoll[i] ); - - /* TODO: Cars, piece-to-piece collision */ -#if 0 - u32 colliders[16]; - int len = bh_select( &world.bhcubes, ch->ragdoll[i].bbx_world, - colliders, 16 ); - - for( int j=0; jragdoll[i], - &world.temp_rbs[colliders[j]] ); -#endif + rb_collide( &ch->ragdoll[i], &world.rb_geo ); } rb_presolve_contacts( rb_contact_buffer, rb_contact_count ); diff --git a/main.c b/main.c index f3b5b8c..d8a2ceb 100644 --- a/main.c +++ b/main.c @@ -17,7 +17,7 @@ vg_tex2d tex_water = { .path = "textures/water.qoi" }; static int debugview = 0; static int sv_debugcam = 0; static int lightedit = 0; -static int sv_scene = 1; +static int sv_scene = 0; /* Components */ #include "road.h" @@ -55,6 +55,7 @@ void vg_register(void) static void init_other(void) { + player_init(); render_init(); gate_init(); world_init(); @@ -92,14 +93,6 @@ static int playermodel( int argc, char const *argv[] ) void vg_start(void) { - vg_convar_push( (struct vg_convar){ - .name = "scene", - .data = &sv_scene, - .data_type = k_convar_dtype_i32, - .opt_i32 = { .min=0, .max=1, .clamp=1 }, - .persistent = 1 - }); - vg_convar_push( (struct vg_convar){ .name = "fc", .data = &freecam, @@ -173,7 +166,7 @@ void vg_start(void) if( sv_scene == 0 ) { - character_load( &player.mdl, "ch_default" ); + character_load( &player.mdl, "ch_outlaw" ); character_init_ragdoll( &player.mdl ); world_load(); diff --git a/models/mp_dev.mdl b/models/mp_dev.mdl index d2a152d..3a57c6c 100644 Binary files a/models/mp_dev.mdl and b/models/mp_dev.mdl differ diff --git a/physics_test.h b/physics_test.h index 8143c1d..d3b9f06 100644 --- a/physics_test.h +++ b/physics_test.h @@ -136,39 +136,38 @@ static void physics_test_update(void) for( int i=0; i<4; i++ ) { rigidbody *fn = &funnel[i]; - rb_contact_count += rb_sphere_vs_box( &ball, fn, rb_global_ct()); - rb_contact_count += rb_sphere_vs_box( &ball1, fn, rb_global_ct()); - rb_contact_count += rb_capsule_vs_box( &jeff1, fn, rb_global_ct() ); + rb_collide( &ball, fn ); + rb_collide( &ball1, fn ); + rb_collide( &jeff1, fn ); for( int i=0; ico ); + v3_copy( rbf->co, rbf->to_world[3] ); + m4x3_mulv( player.rb.to_world, (v3f){0.0f,0.0f, k_board_length}, rbb->co ); + v3_copy( rbb->co, rbb->to_world[3] ); - debug_capsule( mboard, k_board_length*2.0f, k_board_radius, 0xff0000ff ); + m4x3_invert_affine( rbf->to_world, rbf->to_local ); + m4x3_invert_affine( rbb->to_world, rbb->to_local ); - boxf region = {{ -k_board_radius, -k_board_length, -k_board_radius }, - { k_board_radius, k_board_length, k_board_radius }}; - m4x3_transform_aabb( mboard, region ); - - u32 geo[256]; - v3f tri[3]; - int len = bh_select( &world.geo.bhtris, region, geo, 256 ); + rb_update_bounds( rbf ); + rb_update_bounds( rbb ); - v3f poles[2]; - m4x3_mulv(mboard, (v3f){0.0f,-k_board_length+k_board_radius,0.0f}, poles[0]); - m4x3_mulv(mboard, (v3f){0.0f, k_board_length-k_board_radius,0.0f}, poles[1]); - - struct contact manifold[12]; - int manifold_count = 0; + rb_debug( rbf, 0xff00ffff ); + rb_debug( rbb, 0xffffff00 ); - v3f surface_avg = {0.0f, 0.0f, 0.0f}; + rb_ct manifold[24]; + int len = 0; + + len += rb_sphere_vs_scene( rbf, &world.rb_geo, manifold+len ); + len += rb_sphere_vs_scene( rbb, &world.rb_geo, manifold+len ); +#if 0 for( int i=0; i 0.5f ) @@ -478,7 +488,7 @@ static void player_physics(void) for( int j=0; j<5; j++ ) { - for( int i=0; ibbx, rb->bbx_world ); + m4x3_transform_aabb( rb->to_world, rb->bbx_world ); +} + static void rb_update_transform( rigidbody *rb ) { q_normalize( rb->q ); @@ -89,12 +111,11 @@ static void rb_update_transform( rigidbody *rb ) m4x3_invert_affine( rb->to_world, rb->to_local ); - box_copy( rb->bbx, rb->bbx_world ); - m4x3_transform_aabb( rb->to_world, rb->bbx_world ); - m3x3_mulv( rb->to_world, (v3f){1.0f,0.0f, 0.0f}, rb->right ); m3x3_mulv( rb->to_world, (v3f){0.0f,1.0f, 0.0f}, rb->up ); m3x3_mulv( rb->to_world, (v3f){0.0f,0.0f,-1.0f}, rb->forward ); + + rb_update_bounds( rb ); } static float sphere_volume( float radius ) @@ -130,6 +151,11 @@ static void rb_init( rigidbody *rb ) rb->bbx[0][1] = -h; rb->bbx[1][1] = h; } + else if( rb->type == k_rb_shape_scene ) + { + rb->is_world = 1; + box_copy( rb->inf.scene.pscene->bbx, rb->bbx ); + } if( rb->is_world ) { @@ -195,8 +221,10 @@ static void rb_tangent_basis( v3f n, v3f tx, v3f ty ) } static void rb_solver_reset(void); +#ifdef RB_DEPR static void rb_build_manifold_terrain( rigidbody *rb ); static void rb_build_manifold_terrain_sphere( rigidbody *rb ); +#endif static void rb_solve_contacts( rb_ct *buf, int len ); static void rb_presolve_contacts( rb_ct *buffer, int len ); @@ -814,6 +842,88 @@ static int rb_sphere_vs_sphere( rigidbody *rba, rigidbody *rbb, rb_ct *buf ) return 0; } +/* TODO: these guys */ + +static int rb_capsule_vs_scene( rigidbody *rba, rigidbody *rbb, rb_ct *buf ) +{ + u32 geo[128]; + v3f tri[3]; + int len = bh_select( &rbb->inf.scene.pscene->bhtris, + rba->bbx_world, geo, 128 ); + + return 0; +} + +static int rb_sphere_vs_triangle( rigidbody *rba, rigidbody *rbb, + v3f tri[3], rb_ct *buf ) +{ + v3f delta, co; + + closest_on_triangle( rba->co, tri, co ); + 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; + + if( d2 < r*r ) + { + rb_ct *ct = buf; + + v3f ab, ac, tn; + v3_sub( tri[2], tri[0], ab ); + v3_sub( tri[1], tri[0], ac ); + v3_cross( ac, ab, tn ); + v3_copy( tn, ct->n ); + v3_normalize( ct->n ); + + float d = sqrtf(d2); + + v3_copy( co, ct->co ); + ct->p = r-d; + ct->rba = rba; + ct->rbb = rbb; + return 1; + } + + return 0; +} + +static int rb_sphere_vs_scene( rigidbody *rba, rigidbody *rbb, rb_ct *buf ) +{ + scene *sc = rbb->inf.scene.pscene; + + u32 geo[128]; + v3f tri[3]; + int len = bh_select( &sc->bhtris, rba->bbx_world, geo, 128 ); + + int count = 0; + + for( int i=0; iindices[ geo[i]*3 ]; + + for( int j=0; j<3; j++ ) + v3_copy( sc->verts[ptri[j]].co, tri[j] ); + + vg_line(tri[0],tri[1],0xff00ff00 ); + vg_line(tri[1],tri[2],0xff00ff00 ); + vg_line(tri[2],tri[0],0xff00ff00 ); + + count += rb_sphere_vs_triangle( rba, rbb, tri, buf+count ); + + if( count == 12 ) + { + vg_warn( "Exceeding sphere_vs_scene capacity. Geometry too dense!\n" ); + return count; + } + } + + return count; +} + static int RB_MATRIX_ERROR( rigidbody *rba, rigidbody *rbb, rb_ct *buf ) { vg_error( "Collision type is unimplemented between types %d and %d\n", @@ -841,16 +951,45 @@ static int (*rb_jump_table[4][4])( rigidbody *rba, rigidbody *rbb, rb_ct *buf ) = { /* box */ /* Sphere */ /* Capsule */ /*box */ { RB_MATRIX_ERROR, rb_box_vs_sphere, rb_box_vs_capsule, RB_MATRIX_ERROR }, -/*sphere */ { rb_sphere_vs_box, rb_sphere_vs_sphere, rb_sphere_vs_capsule, RB_MATRIX_ERROR }, +/*sphere */ { rb_sphere_vs_box, rb_sphere_vs_sphere, rb_sphere_vs_capsule, rb_sphere_vs_scene }, /*capsule*/ { rb_capsule_vs_box,rb_capsule_vs_sphere,rb_capsule_vs_capsule,RB_MATRIX_ERROR }, /*mesh */ { RB_MATRIX_ERROR, RB_MATRIX_ERROR, RB_MATRIX_ERROR, RB_MATRIX_ERROR } }; +static int rb_collide( rigidbody *rba, rigidbody *rbb ) +{ + int (*collider_jump)(rigidbody *rba, rigidbody *rbb, rb_ct *buf ) + = rb_jump_table[rba->type][rbb->type]; + + /* + * 12 is the maximum manifold size we can generate, so we are forced to abort + * potentially checking any more. + */ + if( rb_contact_count + 12 > vg_list_size(rb_contact_buffer) ) + { + vg_warn( "Too many contacts made in global collider buffer (%d of %d\n)", + rb_contact_count, vg_list_size(rb_contact_buffer) ); + return 0; + } + + /* + * TODO: Replace this with a more dedicated broad phase pass + */ + if( box_overlap( rba->bbx_world, rbb->bbx_world ) ) + { + int count = collider_jump( rba, rbb, rb_contact_buffer+rb_contact_count); + rb_contact_count += count; + return count; + } + else + return 0; +} /* * Generic functions */ +#ifdef RB_DEPR /* * This function does not accept triangle as a dynamic object, it is assumed * to always be static. @@ -891,8 +1030,6 @@ static int rb_sphere_vs_triangle( rigidbody *rba, v3f tri[3], rb_ct *buf ) return 0; } - -RB_DEPR static int sphere_vs_triangle( v3f c, float r, v3f tri[3], v3f co, v3f norm, float *p ) { @@ -926,6 +1063,7 @@ static int sphere_vs_triangle( v3f c, float r, v3f tri[3], } #include "world.h" +#endif static void rb_solver_reset(void) { @@ -937,6 +1075,7 @@ static rb_ct *rb_global_ct(void) return rb_contact_buffer + rb_contact_count; } +#ifdef RB_DEPR static struct contact *rb_start_contact(void) { if( rb_contact_count == vg_list_size(rb_contact_buffer) ) @@ -1004,7 +1143,6 @@ static void rb_build_manifold_terrain_sphere( rigidbody *rb ) } -RB_DEPR static void rb_build_manifold_terrain( rigidbody *rb ) { v3f *box = rb->bbx; @@ -1069,6 +1207,7 @@ static void rb_build_manifold_terrain( rigidbody *rb ) } } } +#endif /* * Initializing things like tangent vectors @@ -1235,7 +1374,6 @@ static void rb_constraint_angle_limit( struct rb_angle_limit *limit ) } -RB_DEPR static void rb_constraint_angle( rigidbody *rba, v3f va, rigidbody *rbb, v3f vb, float max, float spring ) @@ -1483,8 +1621,13 @@ static void rb_debug( rigidbody *rb, u32 colour ) debug_capsule( rb->to_world, r, h, colour ); } + else if( rb->type == k_rb_shape_scene ) + { + vg_line_boxf( rb->bbx, colour ); + } } +#ifdef RB_DEPR /* * out penetration distance, normal */ @@ -1577,4 +1720,6 @@ static bh_system bh_system_rigidbodies = .cast_ray = NULL }; +#endif + #endif /* RIGIDBODY_H */ diff --git a/world.h b/world.h index 9242023..0ebf23a 100644 --- a/world.h +++ b/world.h @@ -51,6 +51,7 @@ static struct gworld /* Rendering & geometry */ scene geo, foliage; + rigidbody rb_geo; mdl_submesh sm_geo_std_oob, sm_geo_std, sm_geo_vb; @@ -401,10 +402,22 @@ static void world_init(void) mdl_node *nholden = mdl_node_from_name( mcars, "holden" ); world.car_holden = *mdl_node_submesh( mcars, nholden, 0 ); free(mcars); + + /* + * Setup scene collider + */ + v3_zero( world.rb_geo.co ); + q_identity( world.rb_geo.q ); + + world.rb_geo.type = k_rb_shape_scene; + world.rb_geo.inf.scene.pscene = &world.geo; + world.rb_geo.is_world = 1; + rb_init( &world.rb_geo ); } static void world_update(void) { +#if 0 rb_solver_reset(); rb_build_manifold_terrain_sphere( &world.mr_ball ); @@ -414,6 +427,7 @@ static void world_update(void) rb_iter( &world.mr_ball ); rb_update_transform( &world.mr_ball ); rb_debug( &world.mr_ball, 0 ); +#endif for( int i=0; i