X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=rigidbody.h;h=5c497f21ada170c53a4d7850095b54beffcd025f;hb=2ab1c45f664daf5a452fd212c89dcfd918f7dd81;hp=d39cf1b6012926941f63860c8e441927f160bc4b;hpb=d45f2b7d71311ce5ce8cd3496844b4ec7d2f46ac;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/rigidbody.h b/rigidbody.h index d39cf1b..5c497f2 100644 --- a/rigidbody.h +++ b/rigidbody.h @@ -16,6 +16,8 @@ VG_STATIC void rb_tangent_basis( v3f n, v3f tx, v3f ty ); VG_STATIC bh_system bh_system_rigidbodies; + + #ifndef RIGIDBODY_H #define RIGIDBODY_H @@ -33,7 +35,8 @@ VG_STATIC const float k_damp_angular = 0.1f, /* scale angular 1/(1+x) */ k_penetration_slop = 0.01f, k_inertia_scale = 8.0f, - k_phys_baumgarte = 0.2f; + k_phys_baumgarte = 0.2f, + k_gravity = 9.6f; VG_STATIC float k_limit_bias = 0.02f, @@ -72,6 +75,24 @@ VG_STATIC void rb_register_cvar(void) typedef struct rigidbody rigidbody; typedef struct contact rb_ct; +typedef struct rb_sphere rb_sphere; +typedef struct rb_capsule rb_capsule; +typedef struct rb_scene rb_scene; + +struct rb_sphere +{ + float radius; +}; + +struct rb_capsule +{ + float height, radius; +}; + +struct rb_scene +{ + bh_tree *bh_scene; +}; struct rigidbody { @@ -89,23 +110,9 @@ struct rigidbody union { - struct rb_sphere - { - float radius; - } - sphere; - - struct rb_capsule - { - float height, radius; - } - capsule; - - struct rb_scene - { - bh_tree *bh_scene; - } - scene; + struct rb_sphere sphere; + struct rb_capsule capsule; + struct rb_scene scene; } inf; @@ -857,6 +864,7 @@ VG_STATIC void rb_capsule_manifold_init( capsule_manifold *manifold ) manifold->t1 = -INFINITY; } +__attribute__ ((deprecated)) VG_STATIC int rb_capsule_manifold_done( rigidbody *rba, rigidbody *rbb, capsule_manifold *manifold, rb_ct *buf ) { @@ -918,6 +926,60 @@ VG_STATIC int rb_capsule_manifold_done( rigidbody *rba, rigidbody *rbb, return count; } +VG_STATIC int rb_capsule__manifold_done( m4x3f mtx, rb_capsule *c, + capsule_manifold *manifold, + rb_ct *buf ) +{ + v3f p0, p1; + v3_muladds( mtx[3], mtx[1], -c->height*0.5f+c->radius, p0 ); + v3_muladds( mtx[3], mtx[1], c->height*0.5f-c->radius, p1 ); + + int count = 0; + if( manifold->t0 <= 1.0f ) + { + rb_ct *ct = buf; + + v3f pa; + v3_muls( p0, 1.0f-manifold->t0, pa ); + v3_muladds( pa, p1, manifold->t0, pa ); + + float d = v3_length( manifold->d0 ); + v3_muls( manifold->d0, 1.0f/d, ct->n ); + v3_muladds( pa, ct->n, -c->radius, ct->co ); + + ct->p = manifold->r0 - d; + ct->type = k_contact_type_default; + count ++; + } + + if( (manifold->t1 >= 0.0f) && (manifold->t0 != manifold->t1) ) + { + rb_ct *ct = buf+count; + + v3f pa; + v3_muls( p0, 1.0f-manifold->t1, pa ); + v3_muladds( pa, p1, manifold->t1, pa ); + + float d = v3_length( manifold->d1 ); + v3_muls( manifold->d1, 1.0f/d, ct->n ); + v3_muladds( pa, ct->n, -c->radius, ct->co ); + + ct->p = manifold->r1 - d; + ct->type = k_contact_type_default; + + count ++; + } + + /* + * Debugging + */ + + if( count == 2 ) + vg_line( buf[0].co, buf[1].co, 0xff0000ff ); + + return count; +} + VG_STATIC int rb_capsule_sphere( rigidbody *rba, rigidbody *rbb, rb_ct *buf ) { float h = rba->inf.capsule.height, @@ -1536,9 +1598,63 @@ VG_STATIC int rb_box_scene( rigidbody *rba, rigidbody *rbb, rb_ct *buf ) return count; } +VG_STATIC int rb_capsule__triangle( m4x3f mtxA, rb_capsule *c, + v3f tri[3], rb_ct *buf ) +{ + v3f pc, p0w, p1w; + v3_muladds( mtxA[3], mtxA[1], -c->height*0.5f+c->radius, p0w ); + v3_muladds( mtxA[3], mtxA[1], c->height*0.5f-c->radius, p1w ); + + capsule_manifold manifold; + rb_capsule_manifold_init( &manifold ); + + v3f c0, c1; + closest_on_triangle_1( p0w, tri, c0 ); + closest_on_triangle_1( p1w, tri, c1 ); + + v3f d0, d1, da; + v3_sub( c0, p0w, d0 ); + v3_sub( c1, p1w, d1 ); + v3_sub( p1w, p0w, da ); + + v3_normalize(d0); + v3_normalize(d1); + v3_normalize(da); + + if( v3_dot( da, d0 ) <= 0.01f ) + rb_capsule_manifold( p0w, c0, 0.0f, c->radius, &manifold ); + + if( v3_dot( da, d1 ) >= -0.01f ) + rb_capsule_manifold( p1w, c1, 1.0f, c->radius, &manifold ); + + for( int i=0; i<3; i++ ) + { + int i0 = i, + i1 = (i+1)%3; + + v3f ca, cb; + float ta, tb; + closest_segment_segment( p0w, p1w, tri[i0], tri[i1], &ta, &tb, ca, cb ); + rb_capsule_manifold( ca, cb, ta, c->radius, &manifold ); + } + + v3f v0, v1, n; + v3_sub( tri[1], tri[0], v0 ); + v3_sub( tri[2], tri[0], v1 ); + v3_cross( v0, v1, n ); + v3_normalize( n ); + + int count = rb_capsule__manifold_done( mtxA, c, &manifold, buf ); + for( int i=0; iinf.capsule.height, - r = rba->inf.capsule.radius, - g = 90.8f; + bh_iter it; + bh_iter_init( 0, &it ); + int idx; + int count = 0; + + boxf bbx; + v3_sub( mtxA[3], (v3f){ c->height, c->height, c->height }, bbx[0] ); + v3_add( mtxA[3], (v3f){ c->height, c->height, c->height }, bbx[1] ); + + scene *sc = s->bh_scene->user; - v3f p[2]; - v3_muladds( rba->co, rba->up, -h*0.5f+r, p[0] ); - v3_muladds( rba->co, rba->up, h*0.5f-r, p[1] ); + while( bh_next( s->bh_scene, &it, bbx, &idx ) ) + { + u32 *ptri = &sc->arrindices[ idx*3 ]; + v3f tri[3]; - int count = 0; + for( int j=0; j<3; j++ ) + v3_copy( sc->arrvertices[ptri[j]].co, tri[j] ); + + buf[ count ].element_id = ptri[0]; + int contact = rb_capsule__triangle( mtxA, c, tri, &buf[count] ); + count += contact; - for( int i=0; i<2; i++ ) - { - if( p[i][1] < g + r ) + if( count == 16 ) { - rb_ct *ct = &buf[ count ++ ]; - - v3_copy( p[i], ct->co ); - ct->p = r - (p[i][1]-g); - ct->co[1] -= r; - v3_copy( (v3f){0.0f,1.0f,0.0f}, ct->n ); - ct->rba = rba; - ct->rbb = rbb; - ct->type = k_contact_type_default; + vg_warn("Exceeding capsule_vs_scene capacity. Geometry too dense!\n"); + return count; } } return count; +} -#else +__attribute__ ((deprecated)) +VG_STATIC int rb_capsule_scene( rigidbody *rba, rigidbody *rbb, rb_ct *buf ) +{ scene *sc = rbb->inf.scene.bh_scene->user; bh_iter it; @@ -1663,7 +1788,6 @@ VG_STATIC int rb_capsule_scene( rigidbody *rba, rigidbody *rbb, rb_ct *buf ) } return count; -#endif } VG_STATIC int rb_scene_capsule( rigidbody *rba, rigidbody *rbb, rb_ct *buf ) @@ -1755,6 +1879,19 @@ VG_STATIC rb_ct *rb_global_ct(void) return rb_contact_buffer + rb_contact_count; } +VG_STATIC void rb_prepare_contact( rb_ct *ct ) +{ + ct->bias = -0.2f * k_rb_rate * vg_minf( 0.0f, -ct->p+k_penetration_slop ); + rb_tangent_basis( ct->n, ct->t[0], ct->t[1] ); + +#if 0 + ct->type = k_contact_type_default; +#endif + ct->norm_impulse = 0.0f; + ct->tangent_impulse[0] = 0.0f; + ct->tangent_impulse[1] = 0.0f; +} + /* * Initializing things like tangent vectors */ @@ -1763,16 +1900,7 @@ VG_STATIC void rb_presolve_contacts( rb_ct *buffer, int len ) for( int i=0; ibias = -0.2f * k_rb_rate * vg_minf( 0.0f, -ct->p+k_penetration_slop ); - rb_tangent_basis( ct->n, ct->t[0], ct->t[1] ); - -#if 0 - ct->type = k_contact_type_default; -#endif - ct->norm_impulse = 0.0f; - ct->tangent_impulse[0] = 0.0f; - ct->tangent_impulse[1] = 0.0f; + rb_prepare_contact( ct ); v3f ra, rb, raCn, rbCn, raCt, rbCt; v3_sub( ct->co, ct->rba->co, ra );