X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=rigidbody.h;h=0bba8f1b381fb6263e1bc99ce02d7cb8adeb4e91;hb=9a751c9645f63a4e324ef2ea486efb7b669fddc5;hp=5c497f21ada170c53a4d7850095b54beffcd025f;hpb=2ab1c45f664daf5a452fd212c89dcfd918f7dd81;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/rigidbody.h b/rigidbody.h index 5c497f2..0bba8f1 100644 --- a/rigidbody.h +++ b/rigidbody.h @@ -16,8 +16,6 @@ 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 @@ -169,6 +167,11 @@ struct rb_constr_swingtwist float tangent_mass, axis_mass; }; +struct rb_constr_spring +{ + int nothing; +}; + /* * ----------------------------------------------------------------------------- * Math Utils @@ -209,11 +212,17 @@ VG_STATIC void rb_tangent_basis( v3f n, v3f tx, v3f ty ) VG_STATIC void rb_debug_contact( rb_ct *ct ) { - if( ct->type != k_contact_type_disabled ) + v3f p1; + v3_muladds( ct->co, ct->n, 0.05f, p1 ); + + if( ct->type == k_contact_type_default ) + { + vg_line_pt3( ct->co, 0.0125f, 0xff0000ff ); + vg_line( ct->co, p1, 0xffffffff ); + } + else if( ct->type == k_contact_type_edge ) { - v3f p1; - v3_muladds( ct->co, ct->n, 0.05f, p1 ); - vg_line_pt3( ct->co, 0.0025f, 0xff0000ff ); + vg_line_pt3( ct->co, 0.0125f, 0xff00ffc0 ); vg_line( ct->co, p1, 0xffffffff ); } } @@ -399,6 +408,7 @@ VG_STATIC void rb_update_transform( rigidbody *rb ) * Extrapolate rigidbody into a transform based on vg accumulator. * Useful for rendering */ +__attribute__ ((deprecated)) VG_STATIC void rb_extrapolate_transform( rigidbody *rb, m4x3f transform ) { float substep = vg_clampf( vg.accumulator / k_rb_delta, 0.0f, 1.0f ); @@ -429,6 +439,30 @@ VG_STATIC void rb_extrapolate_transform( rigidbody *rb, m4x3f transform ) v3_copy( co, transform[3] ); } +VG_STATIC void rb_extrapolate( rigidbody *rb, v3f co, v4f q ) +{ + float substep = vg_clampf( vg.accumulator / k_rb_delta, 0.0f, 1.0f ); + + v3_muladds( rb->co, rb->v, k_rb_delta*substep, co ); + + if( v3_length2( rb->w ) > 0.0f ) + { + v4f rotation; + v3f axis; + v3_copy( rb->w, axis ); + + float mag = v3_length( axis ); + v3_divs( axis, mag, axis ); + q_axis_angle( rotation, axis, mag*k_rb_delta*substep ); + q_mul( rotation, rb->q, q ); + q_normalize( q ); + } + else + { + v4_copy( rb->q, q ); + } +} + /* * Initialize rigidbody and calculate masses, inertia */ @@ -1276,6 +1310,7 @@ VG_STATIC int rb_sphere_sphere( rigidbody *rba, rigidbody *rbb, rb_ct *buf ) //#define RIGIDBODY_DYNAMIC_MESH_EDGES +__attribute__ ((deprecated)) VG_STATIC int rb_sphere_triangle( rigidbody *rba, rigidbody *rbb, v3f tri[3], rb_ct *buf ) { @@ -1323,6 +1358,45 @@ VG_STATIC int rb_sphere_triangle( rigidbody *rba, rigidbody *rbb, return 0; } +VG_STATIC int rb_sphere__triangle( m4x3f mtxA, rb_sphere *b, + v3f tri[3], rb_ct *buf ) +{ + v3f delta, co; + enum contact_type type = closest_on_triangle_1( mtxA[3], tri, co ); + + v3_sub( mtxA[3], co, delta ); + + float d2 = v3_length2( delta ), + r = b->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 ); + + if( v3_length2( ct->n ) <= 0.00001f ) + { + vg_error( "Zero area triangle!\n" ); + return 0; + } + + v3_normalize( ct->n ); + + float d = sqrtf(d2); + + v3_copy( co, ct->co ); + ct->type = type; + ct->p = r-d; + return 1; + } + + return 0; +} VG_STATIC void rb_debug_sharp_scene_edges( rigidbody *rbb, float sharp_ang, boxf box, u32 colour ) @@ -1418,6 +1492,50 @@ VG_STATIC void rb_debug_sharp_scene_edges( rigidbody *rbb, float sharp_ang, } } +VG_STATIC int rb_sphere__scene( m4x3f mtxA, rb_sphere *b, + m4x3f mtxB, rb_scene *s, rb_ct *buf ) +{ + scene *sc = s->bh_scene->user; + + bh_iter it; + bh_iter_init( 0, &it ); + int idx; + + int count = 0; + + float r = b->radius + 0.1f; + boxf box; + v3_sub( mtxA[3], (v3f){ r,r,r }, box[0] ); + v3_add( mtxA[3], (v3f){ r,r,r }, box[1] ); + + while( bh_next( s->bh_scene, &it, box, &idx ) ) + { + u32 *ptri = &sc->arrindices[ idx*3 ]; + v3f tri[3]; + + for( int j=0; j<3; j++ ) + v3_copy( sc->arrvertices[ptri[j]].co, tri[j] ); + + buf[ count ].element_id = ptri[0]; + + vg_line( tri[0],tri[1],0x70ff6000 ); + vg_line( tri[1],tri[2],0x70ff6000 ); + vg_line( tri[2],tri[0],0x70ff6000 ); + + int contact = rb_sphere__triangle( mtxA, b, tri, &buf[count] ); + count += contact; + + if( count == 16 ) + { + vg_warn( "Exceeding sphere_vs_scene capacity. Geometry too dense!\n" ); + return count; + } + } + + return count; +} + +__attribute__ ((deprecated)) VG_STATIC int rb_sphere_scene( rigidbody *rba, rigidbody *rbb, rb_ct *buf ) { scene *sc = rbb->inf.scene.bh_scene->user; @@ -1740,7 +1858,7 @@ VG_STATIC int rb_capsule__scene( m4x3f mtxA, rb_capsule *c, int contact = rb_capsule__triangle( mtxA, c, tri, &buf[count] ); count += contact; - if( count == 16 ) + if( count >= 16 ) { vg_warn("Exceeding capsule_vs_scene capacity. Geometry too dense!\n"); return count; @@ -1879,19 +1997,37 @@ VG_STATIC rb_ct *rb_global_ct(void) return rb_contact_buffer + rb_contact_count; } -VG_STATIC void rb_prepare_contact( rb_ct *ct ) +VG_STATIC void rb_prepare_contact( rb_ct *ct, float timestep ) { - ct->bias = -0.2f * k_rb_rate * vg_minf( 0.0f, -ct->p+k_penetration_slop ); + ct->bias = -0.2f * (timestep*3600.0f) + * 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; } +/* calculate total move. manifold should belong to ONE object only */ +VG_STATIC void rb_depenetrate( rb_ct *manifold, int len, v3f dt ) +{ + v3_zero( dt ); + + for( int j=0; j<7; j++ ) + { + for( int i=0; in, dt ), + remaining = (ct->p-k_penetration_slop) - resolved_amt, + apply = vg_maxf( remaining, 0.0f ) * 0.4f; + + v3_muladds( dt, ct->n, apply, dt ); + } + } +} + /* * Initializing things like tangent vectors */ @@ -1900,7 +2036,7 @@ VG_STATIC void rb_presolve_contacts( rb_ct *buffer, int len ) for( int i=0; ico, ct->rba->co, ra ); @@ -2511,6 +2647,24 @@ VG_STATIC void rb_effect_simple_bouyency( rigidbody *ra, v4f plane, v3_muls( ra->v, 1.0f-(drag*k_rb_delta), ra->v ); } +/* apply a spring&dampener force to match ra(worldspace) on rigidbody, to + * rt(worldspace) + */ +VG_STATIC void rb_effect_spring_target_vector( rigidbody *rba, v3f ra, v3f rt, + float spring, float dampening, + float timestep ) +{ + float a = acosf( vg_clampf( v3_dot( rt, ra ), -1.0f, 1.0f ) ); + + v3f axis; + v3_cross( rt, ra, axis ); + + float Fs = -a * spring, + Fd = -v3_dot( rba->w, axis ) * dampening; + + v3_muladds( rba->w, axis, (Fs+Fd) * timestep, rba->w ); +} + /* * ----------------------------------------------------------------------------- * BVH implementation, this is ONLY for VG_STATIC rigidbodies, its to slow for