2 * Copyright (C) 2021-2023 Mt.ZERO Software, Harry Godden - All Rights Reserved
6 * Resources: Box2D - Erin Catto
10 #include "vg/vg_console.h"
16 static bh_system bh_system_rigidbodies
;
22 * -----------------------------------------------------------------------------
24 * -----------------------------------------------------------------------------
28 k_rb_rate
= (1.0/VG_TIMESTEP_FIXED
),
29 k_rb_delta
= (1.0/k_rb_rate
),
31 k_damp_linear
= 0.1f
, /* scale velocity 1/(1+x) */
32 k_damp_angular
= 0.1f
, /* scale angular 1/(1+x) */
33 k_penetration_slop
= 0.01f
,
34 k_inertia_scale
= 8.0f
,
35 k_phys_baumgarte
= 0.2f
,
40 k_joint_correction
= 0.01f
,
41 k_joint_impulse
= 1.0f
,
42 k_joint_bias
= 0.08f
; /* positional joints */
44 static void rb_register_cvar(void){
45 VG_VAR_F32( k_limit_bias
, flags
=VG_VAR_CHEAT
);
46 VG_VAR_F32( k_joint_bias
, flags
=VG_VAR_CHEAT
);
47 VG_VAR_F32( k_joint_correction
, flags
=VG_VAR_CHEAT
);
48 VG_VAR_F32( k_joint_impulse
, flags
=VG_VAR_CHEAT
);
52 * -----------------------------------------------------------------------------
53 * structure definitions
54 * -----------------------------------------------------------------------------
57 typedef struct rigidbody rigidbody
;
58 typedef struct rb_object rb_object
;
59 typedef struct contact rb_ct
;
60 typedef struct rb_sphere rb_sphere
;
61 typedef struct rb_capsule rb_capsule
;
62 typedef struct rb_scene rb_scene
;
83 /* inertia model and inverse world tensor */
86 m4x3f to_world
, to_local
;
94 k_rb_shape_sphere
= 1,
95 k_rb_shape_capsule
= 2,
101 struct rb_sphere sphere
;
102 struct rb_capsule capsule
;
103 struct rb_scene scene
;
108 VG_STATIC
struct contact
{
109 rigidbody
*rba
, *rbb
;
112 float p
, bias
, norm_impulse
, tangent_impulse
[2],
113 normal_mass
, tangent_mass
[2];
117 enum contact_type type
;
119 rb_contact_buffer
[256];
120 VG_STATIC
int rb_contact_count
= 0;
122 typedef struct rb_constr_pos rb_constr_pos
;
123 typedef struct rb_constr_swingtwist rb_constr_swingtwist
;
125 struct rb_constr_pos
{
126 rigidbody
*rba
, *rbb
;
130 struct rb_constr_swingtwist
{
131 rigidbody
*rba
, *rbb
;
133 v4f conevx
, conevy
; /* relative to rba */
134 v3f view_offset
, /* relative to rba */
135 coneva
, conevxb
;/* relative to rbb */
137 int tangent_violation
, axis_violation
;
138 v3f axis
, tangent_axis
, tangent_target
, axis_target
;
141 float tangent_mass
, axis_mass
;
145 * -----------------------------------------------------------------------------
147 * -----------------------------------------------------------------------------
150 VG_STATIC
void rb_debug_contact( rb_ct
*ct
){
152 v3_muladds( ct
->co
, ct
->n
, 0.05f
, p1
);
154 if( ct
->type
== k_contact_type_default
){
155 vg_line_point( ct
->co
, 0.0125f
, 0xff0000ff );
156 vg_line( ct
->co
, p1
, 0xffffffff );
158 else if( ct
->type
== k_contact_type_edge
){
159 vg_line_point( ct
->co
, 0.0125f
, 0xff00ffc0 );
160 vg_line( ct
->co
, p1
, 0xffffffff );
165 VG_STATIC
void rb_object_debug( rb_object
*obj
, u32 colour
){
166 if( obj
->type
== k_rb_shape_box
){
167 v3f
*box
= obj
->rb
.bbx
;
168 vg_line_boxf_transformed( obj
->rb
.to_world
, obj
->rb
.bbx
, colour
);
170 else if( obj
->type
== k_rb_shape_sphere
){
171 vg_line_sphere( obj
->rb
.to_world
, obj
->inf
.sphere
.radius
, colour
);
173 else if( obj
->type
== k_rb_shape_capsule
){
175 float h
= obj
->inf
.capsule
.height
,
176 r
= obj
->inf
.capsule
.radius
;
178 vg_line_capsule( obj
->rb
.to_world
, r
, h
, colour
);
180 else if( obj
->type
== k_rb_shape_scene
){
181 vg_line_boxf( obj
->rb
.bbx
, colour
);
186 * -----------------------------------------------------------------------------
188 * -----------------------------------------------------------------------------
192 * Update world space bounding box based on local one
194 VG_STATIC
void rb_update_bounds( rigidbody
*rb
)
196 box_copy( rb
->bbx
, rb
->bbx_world
);
197 m4x3_transform_aabb( rb
->to_world
, rb
->bbx_world
);
201 * Commit transform to rigidbody. Updates matrices
203 VG_STATIC
void rb_update_transform( rigidbody
*rb
)
205 q_normalize( rb
->q
);
206 q_m3x3( rb
->q
, rb
->to_world
);
207 v3_copy( rb
->co
, rb
->to_world
[3] );
209 m4x3_invert_affine( rb
->to_world
, rb
->to_local
);
210 m3x3_mul( rb
->iI
, rb
->to_local
, rb
->iIw
);
211 m3x3_mul( rb
->to_world
, rb
->iIw
, rb
->iIw
);
213 rb_update_bounds( rb
);
217 * Extrapolate rigidbody into a transform based on vg accumulator.
218 * Useful for rendering
220 VG_STATIC
void rb_extrapolate( rigidbody
*rb
, v3f co
, v4f q
)
222 float substep
= vg
.time_fixed_extrapolate
;
223 v3_muladds( rb
->co
, rb
->v
, k_rb_delta
*substep
, co
);
225 if( v3_length2( rb
->w
) > 0.0f
){
228 v3_copy( rb
->w
, axis
);
230 float mag
= v3_length( axis
);
231 v3_divs( axis
, mag
, axis
);
232 q_axis_angle( rotation
, axis
, mag
*k_rb_delta
*substep
);
233 q_mul( rotation
, rb
->q
, q
);
242 * Initialize rigidbody and calculate masses, inertia
244 VG_STATIC
void rb_init_object( rb_object
*obj
){
248 if( obj
->type
== k_rb_shape_box
){
250 v3_sub( obj
->rb
.bbx
[1], obj
->rb
.bbx
[0], dims
);
251 volume
= dims
[0]*dims
[1]*dims
[2];
253 else if( obj
->type
== k_rb_shape_sphere
){
254 volume
= vg_sphere_volume( obj
->inf
.sphere
.radius
);
255 v3_fill( obj
->rb
.bbx
[0], -obj
->inf
.sphere
.radius
);
256 v3_fill( obj
->rb
.bbx
[1], obj
->inf
.sphere
.radius
);
258 else if( obj
->type
== k_rb_shape_capsule
){
259 float r
= obj
->inf
.capsule
.radius
,
260 h
= obj
->inf
.capsule
.height
;
261 volume
= vg_sphere_volume( r
) + VG_PIf
* r
*r
* (h
- r
*2.0f
);
263 v3_fill( obj
->rb
.bbx
[0], -r
);
264 v3_fill( obj
->rb
.bbx
[1], r
);
265 obj
->rb
.bbx
[0][1] = -h
;
266 obj
->rb
.bbx
[1][1] = h
;
268 else if( obj
->type
== k_rb_shape_scene
){
270 box_copy( obj
->inf
.scene
.bh_scene
->nodes
[0].bbx
, obj
->rb
.bbx
);
274 obj
->rb
.inv_mass
= 0.0f
;
275 v3_zero( obj
->rb
.I
);
276 m3x3_zero( obj
->rb
.iI
);
279 float mass
= 2.0f
*volume
;
280 obj
->rb
.inv_mass
= 1.0f
/mass
;
283 v3_sub( obj
->rb
.bbx
[1], obj
->rb
.bbx
[0], extent
);
284 v3_muls( extent
, 0.5f
, extent
);
286 /* local intertia tensor */
287 float scale
= k_inertia_scale
;
288 float ex2
= scale
*extent
[0]*extent
[0],
289 ey2
= scale
*extent
[1]*extent
[1],
290 ez2
= scale
*extent
[2]*extent
[2];
292 obj
->rb
.I
[0] = ((1.0f
/12.0f
) * mass
* (ey2
+ez2
));
293 obj
->rb
.I
[1] = ((1.0f
/12.0f
) * mass
* (ex2
+ez2
));
294 obj
->rb
.I
[2] = ((1.0f
/12.0f
) * mass
* (ex2
+ey2
));
296 m3x3_identity( obj
->rb
.iI
);
297 obj
->rb
.iI
[0][0] = obj
->rb
.I
[0];
298 obj
->rb
.iI
[1][1] = obj
->rb
.I
[1];
299 obj
->rb
.iI
[2][2] = obj
->rb
.I
[2];
300 m3x3_inv( obj
->rb
.iI
, obj
->rb
.iI
);
303 rb_update_transform( &obj
->rb
);
306 VG_STATIC
void rb_iter( rigidbody
*rb
){
307 if( !vg_validf( rb
->v
[0] ) ||
308 !vg_validf( rb
->v
[1] ) ||
309 !vg_validf( rb
->v
[2] ) )
311 vg_fatal_error( "NaN velocity" );
314 v3f gravity
= { 0.0f
, -9.8f
, 0.0f
};
315 v3_muladds( rb
->v
, gravity
, k_rb_delta
, rb
->v
);
317 /* intergrate velocity */
318 v3_muladds( rb
->co
, rb
->v
, k_rb_delta
, rb
->co
);
319 v3_lerp( rb
->w
, (v3f
){0.0f
,0.0f
,0.0f
}, 0.0025f
, rb
->w
);
321 /* inegrate inertia */
322 if( v3_length2( rb
->w
) > 0.0f
)
326 v3_copy( rb
->w
, axis
);
328 float mag
= v3_length( axis
);
329 v3_divs( axis
, mag
, axis
);
330 q_axis_angle( rotation
, axis
, mag
*k_rb_delta
);
331 q_mul( rotation
, rb
->q
, rb
->q
);
335 v3_muls( rb
->v
, 1.0f
/(1.0f
+k_rb_delta
*k_damp_linear
), rb
->v
);
336 v3_muls( rb
->w
, 1.0f
/(1.0f
+k_rb_delta
*k_damp_angular
), rb
->w
);
341 * -----------------------------------------------------------------------------
342 * Boolean shape overlap functions
343 * -----------------------------------------------------------------------------
347 * Project AABB, and triangle interval onto axis to check if they overlap
349 VG_STATIC
int rb_box_triangle_interval( v3f extent
, v3f axis
, v3f tri
[3] ){
352 r
= extent
[0] * fabsf(axis
[0]) +
353 extent
[1] * fabsf(axis
[1]) +
354 extent
[2] * fabsf(axis
[2]),
356 p0
= v3_dot( axis
, tri
[0] ),
357 p1
= v3_dot( axis
, tri
[1] ),
358 p2
= v3_dot( axis
, tri
[2] ),
360 e
= vg_maxf(-vg_maxf(p0
,vg_maxf(p1
,p2
)), vg_minf(p0
,vg_minf(p1
,p2
)));
362 if( e
> r
) return 0;
367 * Seperating axis test box vs triangle
369 VG_STATIC
int rb_box_triangle_sat( v3f extent
, v3f center
,
370 m4x3f to_local
, v3f tri_src
[3] ){
373 for( int i
=0; i
<3; i
++ ){
374 m4x3_mulv( to_local
, tri_src
[i
], tri
[i
] );
375 v3_sub( tri
[i
], center
, tri
[i
] );
379 v3_sub( tri
[1], tri
[0], f0
);
380 v3_sub( tri
[2], tri
[1], f1
);
381 v3_sub( tri
[0], tri
[2], f2
);
385 v3_cross( (v3f
){1.0f
,0.0f
,0.0f
}, f0
, axis
[0] );
386 v3_cross( (v3f
){1.0f
,0.0f
,0.0f
}, f1
, axis
[1] );
387 v3_cross( (v3f
){1.0f
,0.0f
,0.0f
}, f2
, axis
[2] );
388 v3_cross( (v3f
){0.0f
,1.0f
,0.0f
}, f0
, axis
[3] );
389 v3_cross( (v3f
){0.0f
,1.0f
,0.0f
}, f1
, axis
[4] );
390 v3_cross( (v3f
){0.0f
,1.0f
,0.0f
}, f2
, axis
[5] );
391 v3_cross( (v3f
){0.0f
,0.0f
,1.0f
}, f0
, axis
[6] );
392 v3_cross( (v3f
){0.0f
,0.0f
,1.0f
}, f1
, axis
[7] );
393 v3_cross( (v3f
){0.0f
,0.0f
,1.0f
}, f2
, axis
[8] );
395 for( int i
=0; i
<9; i
++ )
396 if(!rb_box_triangle_interval( extent
, axis
[i
], tri
)) return 0;
399 if(!rb_box_triangle_interval( extent
, (v3f
){1.0f
,0.0f
,0.0f
}, tri
)) return 0;
400 if(!rb_box_triangle_interval( extent
, (v3f
){0.0f
,1.0f
,0.0f
}, tri
)) return 0;
401 if(!rb_box_triangle_interval( extent
, (v3f
){0.0f
,0.0f
,1.0f
}, tri
)) return 0;
404 v3_cross( f0
, f1
, n
);
405 if(!rb_box_triangle_interval( extent
, n
, tri
)) return 0;
411 * -----------------------------------------------------------------------------
413 * -----------------------------------------------------------------------------
416 VG_STATIC
int rb_manifold_apply_filtered( rb_ct
*man
, int len
){
419 for( int i
=0; i
<len
; i
++ ){
422 if( ct
->type
== k_contact_type_disabled
)
432 * Merge two contacts if they are within radius(r) of eachother
434 VG_STATIC
void rb_manifold_contact_weld( rb_ct
*ci
, rb_ct
*cj
, float r
){
435 if( v3_dist2( ci
->co
, cj
->co
) < r
*r
){
436 cj
->type
= k_contact_type_disabled
;
437 ci
->p
= (ci
->p
+ cj
->p
) * 0.5f
;
439 v3_add( ci
->co
, cj
->co
, ci
->co
);
440 v3_muls( ci
->co
, 0.5f
, ci
->co
);
443 v3_sub( ci
->rba
->co
, ci
->co
, delta
);
445 float c0
= v3_dot( ci
->n
, delta
),
446 c1
= v3_dot( cj
->n
, delta
);
448 if( c0
< 0.0f
|| c1
< 0.0f
){
450 ci
->type
= k_contact_type_disabled
;
454 v3_muls( ci
->n
, c0
, n
);
455 v3_muladds( n
, cj
->n
, c1
, n
);
465 VG_STATIC
void rb_manifold_filter_joint_edges( rb_ct
*man
, int len
, float r
){
466 for( int i
=0; i
<len
-1; i
++ ){
468 if( ci
->type
!= k_contact_type_edge
)
471 for( int j
=i
+1; j
<len
; j
++ ){
473 if( cj
->type
!= k_contact_type_edge
)
476 rb_manifold_contact_weld( ci
, cj
, r
);
482 * Resolve overlapping pairs
484 VG_STATIC
void rb_manifold_filter_pairs( rb_ct
*man
, int len
, float r
){
485 for( int i
=0; i
<len
-1; i
++ ){
489 if( ci
->type
== k_contact_type_disabled
) continue;
491 for( int j
=i
+1; j
<len
; j
++ ){
494 if( cj
->type
== k_contact_type_disabled
) continue;
496 if( v3_dist2( ci
->co
, cj
->co
) < r
*r
){
497 cj
->type
= k_contact_type_disabled
;
498 v3_add( cj
->n
, ci
->n
, ci
->n
);
505 float n
= 1.0f
/((float)similar
+1.0f
);
506 v3_muls( ci
->n
, n
, ci
->n
);
509 if( v3_length2(ci
->n
) < 0.1f
*0.1f
)
510 ci
->type
= k_contact_type_disabled
;
512 v3_normalize( ci
->n
);
518 * Remove contacts that are facing away from A
520 VG_STATIC
void rb_manifold_filter_backface( rb_ct
*man
, int len
){
521 for( int i
=0; i
<len
; i
++ ){
523 if( ct
->type
== k_contact_type_disabled
)
527 v3_sub( ct
->co
, ct
->rba
->co
, delta
);
529 if( v3_dot( delta
, ct
->n
) > -0.001f
)
530 ct
->type
= k_contact_type_disabled
;
535 * Filter out duplicate coplanar results. Good for spheres.
537 VG_STATIC
void rb_manifold_filter_coplanar( rb_ct
*man
, int len
, float w
){
538 for( int i
=0; i
<len
; i
++ ){
540 if( ci
->type
== k_contact_type_disabled
||
541 ci
->type
== k_contact_type_edge
)
544 float d1
= v3_dot( ci
->co
, ci
->n
);
546 for( int j
=0; j
<len
; j
++ ){
551 if( cj
->type
== k_contact_type_disabled
)
554 float d2
= v3_dot( cj
->co
, ci
->n
),
557 if( fabsf( d
) <= w
){
558 cj
->type
= k_contact_type_disabled
;
565 * -----------------------------------------------------------------------------
567 * -----------------------------------------------------------------------------
573 * These do not automatically allocate contacts, an appropriately sized
574 * buffer must be supplied. The function returns the size of the manifold
575 * which was generated.
577 * The values set on the contacts are: n, co, p, rba, rbb
581 * By collecting the minimum(time) and maximum(time) pairs of points, we
582 * build a reduced and stable exact manifold.
585 * rx: minimum distance of these points
586 * dx: the delta between the two points
588 * pairs will only ammend these if they are creating a collision
590 typedef struct capsule_manifold capsule_manifold
;
591 struct capsule_manifold
{
598 * Expand a line manifold with a new pair. t value is the time along segment
599 * on the oriented object which created this pair.
601 VG_STATIC
void rb_capsule_manifold( v3f pa
, v3f pb
, float t
, float r
,
602 capsule_manifold
*manifold
){
604 v3_sub( pa
, pb
, delta
);
606 if( v3_length2(delta
) < r
*r
){
607 if( t
< manifold
->t0
){
608 v3_copy( delta
, manifold
->d0
);
613 if( t
> manifold
->t1
){
614 v3_copy( delta
, manifold
->d1
);
621 VG_STATIC
void rb_capsule_manifold_init( capsule_manifold
*manifold
){
622 manifold
->t0
= INFINITY
;
623 manifold
->t1
= -INFINITY
;
626 VG_STATIC
int rb_capsule__manifold_done( m4x3f mtx
, rb_capsule
*c
,
627 capsule_manifold
*manifold
,
630 v3_muladds( mtx
[3], mtx
[1], -c
->height
*0.5f
+c
->radius
, p0
);
631 v3_muladds( mtx
[3], mtx
[1], c
->height
*0.5f
-c
->radius
, p1
);
634 if( manifold
->t0
<= 1.0f
){
638 v3_muls( p0
, 1.0f
-manifold
->t0
, pa
);
639 v3_muladds( pa
, p1
, manifold
->t0
, pa
);
641 float d
= v3_length( manifold
->d0
);
642 v3_muls( manifold
->d0
, 1.0f
/d
, ct
->n
);
643 v3_muladds( pa
, ct
->n
, -c
->radius
, ct
->co
);
645 ct
->p
= manifold
->r0
- d
;
646 ct
->type
= k_contact_type_default
;
650 if( (manifold
->t1
>= 0.0f
) && (manifold
->t0
!= manifold
->t1
) ){
651 rb_ct
*ct
= buf
+count
;
654 v3_muls( p0
, 1.0f
-manifold
->t1
, pa
);
655 v3_muladds( pa
, p1
, manifold
->t1
, pa
);
657 float d
= v3_length( manifold
->d1
);
658 v3_muls( manifold
->d1
, 1.0f
/d
, ct
->n
);
659 v3_muladds( pa
, ct
->n
, -c
->radius
, ct
->co
);
661 ct
->p
= manifold
->r1
- d
;
662 ct
->type
= k_contact_type_default
;
672 vg_line( buf
[0].co
, buf
[1].co
, 0xff0000ff );
677 VG_STATIC
int rb_capsule_sphere( rb_object
*obja
, rb_object
*objb
, rb_ct
*buf
){
678 rigidbody
*rba
= &obja
->rb
, *rbb
= &objb
->rb
;
679 float h
= obja
->inf
.capsule
.height
,
680 ra
= obja
->inf
.capsule
.radius
,
681 rb
= objb
->inf
.sphere
.radius
;
684 v3_muladds( rba
->co
, rba
->to_world
[1], -h
*0.5f
+ra
, p0
);
685 v3_muladds( rba
->co
, rba
->to_world
[1], h
*0.5f
-ra
, p1
);
688 closest_point_segment( p0
, p1
, rbb
->co
, c
);
689 v3_sub( c
, rbb
->co
, delta
);
691 float d2
= v3_length2(delta
),
698 v3_muls( delta
, 1.0f
/d
, ct
->n
);
702 v3_muladds( c
, ct
->n
, -ra
, p0
);
703 v3_muladds( rbb
->co
, ct
->n
, rb
, p1
);
704 v3_add( p0
, p1
, ct
->co
);
705 v3_muls( ct
->co
, 0.5f
, ct
->co
);
709 ct
->type
= k_contact_type_default
;
717 VG_STATIC
int rb_capsule__capsule( m4x3f mtxA
, rb_capsule
*ca
,
718 m4x3f mtxB
, rb_capsule
*cb
, rb_ct
*buf
){
719 float ha
= ca
->height
,
726 v3_muladds( mtxA
[3], mtxA
[1], -ha
*0.5f
+ra
, p0
);
727 v3_muladds( mtxA
[3], mtxA
[1], ha
*0.5f
-ra
, p1
);
728 v3_muladds( mtxB
[3], mtxB
[1], -hb
*0.5f
+rb
, p2
);
729 v3_muladds( mtxB
[3], mtxB
[1], hb
*0.5f
-rb
, p3
);
731 capsule_manifold manifold
;
732 rb_capsule_manifold_init( &manifold
);
736 closest_segment_segment( p0
, p1
, p2
, p3
, &ta
, &tb
, pa
, pb
);
737 rb_capsule_manifold( pa
, pb
, ta
, r
, &manifold
);
739 ta
= closest_point_segment( p0
, p1
, p2
, pa
);
740 tb
= closest_point_segment( p0
, p1
, p3
, pb
);
741 rb_capsule_manifold( pa
, p2
, ta
, r
, &manifold
);
742 rb_capsule_manifold( pb
, p3
, tb
, r
, &manifold
);
744 closest_point_segment( p2
, p3
, p0
, pa
);
745 closest_point_segment( p2
, p3
, p1
, pb
);
746 rb_capsule_manifold( p0
, pa
, 0.0f
, r
, &manifold
);
747 rb_capsule_manifold( p1
, pb
, 1.0f
, r
, &manifold
);
749 return rb_capsule__manifold_done( mtxA
, ca
, &manifold
, buf
);
752 VG_STATIC
int rb_sphere_box( rb_object
*obja
, rb_object
*objb
, rb_ct
*buf
){
754 rigidbody
*rba
= &obja
->rb
, *rbb
= &objb
->rb
;
756 closest_point_obb( rba
->co
, rbb
->bbx
, rbb
->to_world
, rbb
->to_local
, co
);
757 v3_sub( rba
->co
, co
, delta
);
759 float d2
= v3_length2(delta
),
760 r
= obja
->inf
.sphere
.radius
;
767 v3_sub( rba
->co
, rbb
->co
, delta
);
770 * some extra testing is required to find the best axis to push the
771 * object back outside the box. Since there isnt a clear seperating
772 * vector already, especially on really high aspect boxes.
774 float lx
= v3_dot( rbb
->to_world
[0], delta
),
775 ly
= v3_dot( rbb
->to_world
[1], delta
),
776 lz
= v3_dot( rbb
->to_world
[2], delta
),
777 px
= rbb
->bbx
[1][0] - fabsf(lx
),
778 py
= rbb
->bbx
[1][1] - fabsf(ly
),
779 pz
= rbb
->bbx
[1][2] - fabsf(lz
);
781 if( px
< py
&& px
< pz
)
782 v3_muls( rbb
->to_world
[0], vg_signf(lx
), ct
->n
);
784 v3_muls( rbb
->to_world
[1], vg_signf(ly
), ct
->n
);
786 v3_muls( rbb
->to_world
[2], vg_signf(lz
), ct
->n
);
788 v3_muladds( rba
->co
, ct
->n
, -r
, ct
->co
);
793 v3_muls( delta
, 1.0f
/d
, ct
->n
);
795 v3_copy( co
, ct
->co
);
800 ct
->type
= k_contact_type_default
;
807 VG_STATIC
int rb_sphere_sphere( rb_object
*obja
, rb_object
*objb
, rb_ct
*buf
){
808 rigidbody
*rba
= &obja
->rb
, *rbb
= &objb
->rb
;
810 v3_sub( rba
->co
, rbb
->co
, delta
);
812 float d2
= v3_length2(delta
),
813 r
= obja
->inf
.sphere
.radius
+ objb
->inf
.sphere
.radius
;
819 v3_muls( delta
, 1.0f
/d
, ct
->n
);
822 v3_muladds( rba
->co
, ct
->n
,-obja
->inf
.sphere
.radius
, p0
);
823 v3_muladds( rbb
->co
, ct
->n
, objb
->inf
.sphere
.radius
, p1
);
824 v3_add( p0
, p1
, ct
->co
);
825 v3_muls( ct
->co
, 0.5f
, ct
->co
);
826 ct
->type
= k_contact_type_default
;
836 VG_STATIC
int rb_sphere__triangle( m4x3f mtxA
, rb_sphere
*b
,
837 v3f tri
[3], rb_ct
*buf
){
839 enum contact_type type
= closest_on_triangle_1( mtxA
[3], tri
, co
);
841 v3_sub( mtxA
[3], co
, delta
);
843 float d2
= v3_length2( delta
),
850 v3_sub( tri
[2], tri
[0], ab
);
851 v3_sub( tri
[1], tri
[0], ac
);
852 v3_cross( ac
, ab
, tn
);
853 v3_copy( tn
, ct
->n
);
855 if( v3_length2( ct
->n
) <= 0.00001f
){
856 vg_error( "Zero area triangle!\n" );
860 v3_normalize( ct
->n
);
864 v3_copy( co
, ct
->co
);
873 VG_STATIC
int rb_sphere__scene( m4x3f mtxA
, rb_sphere
*b
,
874 m4x3f mtxB
, rb_scene
*s
, rb_ct
*buf
){
875 scene_context
*sc
= s
->bh_scene
->user
;
879 float r
= b
->radius
+ 0.1f
;
881 v3_sub( mtxA
[3], (v3f
){ r
,r
,r
}, box
[0] );
882 v3_add( mtxA
[3], (v3f
){ r
,r
,r
}, box
[1] );
886 bh_iter_init_box( 0, &it
, box
);
888 while( bh_next( s
->bh_scene
, &it
, &idx
) ){
889 u32
*ptri
= &sc
->arrindices
[ idx
*3 ];
892 for( int j
=0; j
<3; j
++ )
893 v3_copy( sc
->arrvertices
[ptri
[j
]].co
, tri
[j
] );
895 buf
[ count
].element_id
= ptri
[0];
897 vg_line( tri
[0],tri
[1],0x70ff6000 );
898 vg_line( tri
[1],tri
[2],0x70ff6000 );
899 vg_line( tri
[2],tri
[0],0x70ff6000 );
901 int contact
= rb_sphere__triangle( mtxA
, b
, tri
, &buf
[count
] );
905 vg_warn( "Exceeding sphere_vs_scene capacity. Geometry too dense!\n" );
913 VG_STATIC
int rb_box__scene( m4x3f mtxA
, boxf bbx
,
914 m4x3f mtxB
, rb_scene
*s
, rb_ct
*buf
){
915 scene_context
*sc
= s
->bh_scene
->user
;
919 v3_sub( bbx
[1], bbx
[0], extent
);
920 v3_muls( extent
, 0.5f
, extent
);
921 v3_add( bbx
[0], extent
, center
);
923 float r
= v3_length(extent
);
925 v3_fill( world_bbx
[0], -r
);
926 v3_fill( world_bbx
[1], r
);
927 for( int i
=0; i
<2; i
++ ){
928 v3_add( center
, world_bbx
[i
], world_bbx
[i
] );
929 v3_add( mtxA
[3], world_bbx
[i
], world_bbx
[i
] );
933 m4x3_invert_affine( mtxA
, to_local
);
936 bh_iter_init_box( 0, &it
, world_bbx
);
940 vg_line_boxf( world_bbx
, VG__RED
);
942 while( bh_next( s
->bh_scene
, &it
, &idx
) ){
943 u32
*ptri
= &sc
->arrindices
[ idx
*3 ];
945 for( int j
=0; j
<3; j
++ )
946 v3_copy( sc
->arrvertices
[ptri
[j
]].co
, tri
[j
] );
948 if( rb_box_triangle_sat( extent
, center
, to_local
, tri
) ){
949 vg_line(tri
[0],tri
[1],0xff50ff00 );
950 vg_line(tri
[1],tri
[2],0xff50ff00 );
951 vg_line(tri
[2],tri
[0],0xff50ff00 );
954 vg_line(tri
[0],tri
[1],0xff0000ff );
955 vg_line(tri
[1],tri
[2],0xff0000ff );
956 vg_line(tri
[2],tri
[0],0xff0000ff );
961 v3_sub( tri
[1], tri
[0], v0
);
962 v3_sub( tri
[2], tri
[0], v1
);
963 v3_cross( v0
, v1
, n
);
966 /* find best feature */
967 float best
= v3_dot( mtxA
[0], n
);
970 for( int i
=1; i
<3; i
++ ){
971 float c
= v3_dot( mtxA
[i
], n
);
973 if( fabsf(c
) > fabsf(best
) ){
982 float px
= best
> 0.0f
? bbx
[0][0]: bbx
[1][0];
984 manifold
[0][1] = bbx
[0][1];
985 manifold
[0][2] = bbx
[0][2];
987 manifold
[1][1] = bbx
[1][1];
988 manifold
[1][2] = bbx
[0][2];
990 manifold
[2][1] = bbx
[1][1];
991 manifold
[2][2] = bbx
[1][2];
993 manifold
[3][1] = bbx
[0][1];
994 manifold
[3][2] = bbx
[1][2];
996 else if( axis
== 1 ){
997 float py
= best
> 0.0f
? bbx
[0][1]: bbx
[1][1];
998 manifold
[0][0] = bbx
[0][0];
1000 manifold
[0][2] = bbx
[0][2];
1001 manifold
[1][0] = bbx
[1][0];
1002 manifold
[1][1] = py
;
1003 manifold
[1][2] = bbx
[0][2];
1004 manifold
[2][0] = bbx
[1][0];
1005 manifold
[2][1] = py
;
1006 manifold
[2][2] = bbx
[1][2];
1007 manifold
[3][0] = bbx
[0][0];
1008 manifold
[3][1] = py
;
1009 manifold
[3][2] = bbx
[1][2];
1012 float pz
= best
> 0.0f
? bbx
[0][2]: bbx
[1][2];
1013 manifold
[0][0] = bbx
[0][0];
1014 manifold
[0][1] = bbx
[0][1];
1015 manifold
[0][2] = pz
;
1016 manifold
[1][0] = bbx
[1][0];
1017 manifold
[1][1] = bbx
[0][1];
1018 manifold
[1][2] = pz
;
1019 manifold
[2][0] = bbx
[1][0];
1020 manifold
[2][1] = bbx
[1][1];
1021 manifold
[2][2] = pz
;
1022 manifold
[3][0] = bbx
[0][0];
1023 manifold
[3][1] = bbx
[1][1];
1024 manifold
[3][2] = pz
;
1027 for( int j
=0; j
<4; j
++ )
1028 m4x3_mulv( mtxA
, manifold
[j
], manifold
[j
] );
1030 vg_line( manifold
[0], manifold
[1], 0xffffffff );
1031 vg_line( manifold
[1], manifold
[2], 0xffffffff );
1032 vg_line( manifold
[2], manifold
[3], 0xffffffff );
1033 vg_line( manifold
[3], manifold
[0], 0xffffffff );
1035 for( int j
=0; j
<4; j
++ ){
1036 rb_ct
*ct
= buf
+count
;
1038 v3_copy( manifold
[j
], ct
->co
);
1039 v3_copy( n
, ct
->n
);
1041 float l0
= v3_dot( tri
[0], n
),
1042 l1
= v3_dot( manifold
[j
], n
);
1044 ct
->p
= (l0
-l1
)*0.5f
;
1048 ct
->type
= k_contact_type_default
;
1058 VG_STATIC
int rb_capsule__triangle( m4x3f mtxA
, rb_capsule
*c
,
1059 v3f tri
[3], rb_ct
*buf
){
1061 v3_muladds( mtxA
[3], mtxA
[1], -c
->height
*0.5f
+c
->radius
, p0w
);
1062 v3_muladds( mtxA
[3], mtxA
[1], c
->height
*0.5f
-c
->radius
, p1w
);
1064 capsule_manifold manifold
;
1065 rb_capsule_manifold_init( &manifold
);
1068 closest_on_triangle_1( p0w
, tri
, c0
);
1069 closest_on_triangle_1( p1w
, tri
, c1
);
1072 v3_sub( c0
, p0w
, d0
);
1073 v3_sub( c1
, p1w
, d1
);
1074 v3_sub( p1w
, p0w
, da
);
1080 if( v3_dot( da
, d0
) <= 0.01f
)
1081 rb_capsule_manifold( p0w
, c0
, 0.0f
, c
->radius
, &manifold
);
1083 if( v3_dot( da
, d1
) >= -0.01f
)
1084 rb_capsule_manifold( p1w
, c1
, 1.0f
, c
->radius
, &manifold
);
1086 for( int i
=0; i
<3; i
++ ){
1092 closest_segment_segment( p0w
, p1w
, tri
[i0
], tri
[i1
], &ta
, &tb
, ca
, cb
);
1093 rb_capsule_manifold( ca
, cb
, ta
, c
->radius
, &manifold
);
1097 v3_sub( tri
[1], tri
[0], v0
);
1098 v3_sub( tri
[2], tri
[0], v1
);
1099 v3_cross( v0
, v1
, n
);
1101 if( v3_length2( n
) <= 0.00001f
){
1102 vg_error( "Zero area triangle!\n" );
1108 int count
= rb_capsule__manifold_done( mtxA
, c
, &manifold
, buf
);
1109 for( int i
=0; i
<count
; i
++ )
1110 v3_copy( n
, buf
[i
].n
);
1115 /* mtxB is defined only for tradition; it is not used currently */
1116 VG_STATIC
int rb_capsule__scene( m4x3f mtxA
, rb_capsule
*c
,
1117 m4x3f mtxB
, rb_scene
*s
,
1122 v3_sub( mtxA
[3], (v3f
){ c
->height
, c
->height
, c
->height
}, bbx
[0] );
1123 v3_add( mtxA
[3], (v3f
){ c
->height
, c
->height
, c
->height
}, bbx
[1] );
1125 scene_context
*sc
= s
->bh_scene
->user
;
1128 bh_iter_init_box( 0, &it
, bbx
);
1130 while( bh_next( s
->bh_scene
, &it
, &idx
) ){
1131 u32
*ptri
= &sc
->arrindices
[ idx
*3 ];
1134 for( int j
=0; j
<3; j
++ )
1135 v3_copy( sc
->arrvertices
[ptri
[j
]].co
, tri
[j
] );
1137 buf
[ count
].element_id
= ptri
[0];
1139 int contact
= rb_capsule__triangle( mtxA
, c
, tri
, &buf
[count
] );
1143 vg_warn("Exceeding capsule_vs_scene capacity. Geometry too dense!\n");
1151 VG_STATIC
int rb_global_has_space( void ){
1152 if( rb_contact_count
+ 16 > vg_list_size(rb_contact_buffer
) )
1158 VG_STATIC rb_ct
*rb_global_buffer( void ){
1159 return &rb_contact_buffer
[ rb_contact_count
];
1163 * -----------------------------------------------------------------------------
1165 * -----------------------------------------------------------------------------
1168 VG_STATIC
void rb_solver_reset(void){
1169 rb_contact_count
= 0;
1172 VG_STATIC rb_ct
*rb_global_ct(void){
1173 return rb_contact_buffer
+ rb_contact_count
;
1176 VG_STATIC
void rb_prepare_contact( rb_ct
*ct
, float timestep
){
1177 ct
->bias
= -0.2f
* (timestep
*3600.0f
)
1178 * vg_minf( 0.0f
, -ct
->p
+k_penetration_slop
);
1180 v3_tangent_basis( ct
->n
, ct
->t
[0], ct
->t
[1] );
1181 ct
->norm_impulse
= 0.0f
;
1182 ct
->tangent_impulse
[0] = 0.0f
;
1183 ct
->tangent_impulse
[1] = 0.0f
;
1186 /* calculate total move. manifold should belong to ONE object only */
1187 VG_STATIC
void rb_depenetrate( rb_ct
*manifold
, int len
, v3f dt
){
1190 for( int j
=0; j
<7; j
++ )
1192 for( int i
=0; i
<len
; i
++ )
1194 struct contact
*ct
= &manifold
[i
];
1196 float resolved_amt
= v3_dot( ct
->n
, dt
),
1197 remaining
= (ct
->p
-k_penetration_slop
) - resolved_amt
,
1198 apply
= vg_maxf( remaining
, 0.0f
) * 0.4f
;
1200 v3_muladds( dt
, ct
->n
, apply
, dt
);
1206 * Initializing things like tangent vectors
1208 VG_STATIC
void rb_presolve_contacts( rb_ct
*buffer
, int len
){
1209 for( int i
=0; i
<len
; i
++ ){
1210 rb_ct
*ct
= &buffer
[i
];
1211 rb_prepare_contact( ct
, k_rb_delta
);
1213 v3f ra
, rb
, raCn
, rbCn
, raCt
, rbCt
;
1214 v3_sub( ct
->co
, ct
->rba
->co
, ra
);
1215 v3_sub( ct
->co
, ct
->rbb
->co
, rb
);
1216 v3_cross( ra
, ct
->n
, raCn
);
1217 v3_cross( rb
, ct
->n
, rbCn
);
1219 /* orient inverse inertia tensors */
1221 m3x3_mulv( ct
->rba
->iIw
, raCn
, raCnI
);
1222 m3x3_mulv( ct
->rbb
->iIw
, rbCn
, rbCnI
);
1224 ct
->normal_mass
= ct
->rba
->inv_mass
+ ct
->rbb
->inv_mass
;
1225 ct
->normal_mass
+= v3_dot( raCn
, raCnI
);
1226 ct
->normal_mass
+= v3_dot( rbCn
, rbCnI
);
1227 ct
->normal_mass
= 1.0f
/ct
->normal_mass
;
1229 for( int j
=0; j
<2; j
++ ){
1231 v3_cross( ct
->t
[j
], ra
, raCt
);
1232 v3_cross( ct
->t
[j
], rb
, rbCt
);
1233 m3x3_mulv( ct
->rba
->iIw
, raCt
, raCtI
);
1234 m3x3_mulv( ct
->rbb
->iIw
, rbCt
, rbCtI
);
1236 ct
->tangent_mass
[j
] = ct
->rba
->inv_mass
+ ct
->rbb
->inv_mass
;
1237 ct
->tangent_mass
[j
] += v3_dot( raCt
, raCtI
);
1238 ct
->tangent_mass
[j
] += v3_dot( rbCt
, rbCtI
);
1239 ct
->tangent_mass
[j
] = 1.0f
/ct
->tangent_mass
[j
];
1242 rb_debug_contact( ct
);
1247 * Creates relative contact velocity vector
1249 VG_STATIC
void rb_rcv( rigidbody
*rba
, rigidbody
*rbb
, v3f ra
, v3f rb
, v3f rv
){
1251 v3_cross( rba
->w
, ra
, rva
);
1252 v3_add( rba
->v
, rva
, rva
);
1253 v3_cross( rbb
->w
, rb
, rvb
);
1254 v3_add( rbb
->v
, rvb
, rvb
);
1256 v3_sub( rva
, rvb
, rv
);
1259 VG_STATIC
void rb_contact_restitution( rb_ct
*ct
, float cr
){
1261 v3_sub( ct
->co
, ct
->rba
->co
, ra
);
1262 v3_sub( ct
->co
, ct
->rbb
->co
, rb
);
1263 rb_rcv( ct
->rba
, ct
->rbb
, ra
, rb
, rv
);
1265 float v
= v3_dot( rv
, ct
->n
);
1268 ct
->bias
+= -cr
* v
;
1273 * Apply impulse to object
1275 VG_STATIC
void rb_linear_impulse( rigidbody
*rb
, v3f delta
, v3f impulse
){
1277 v3_muladds( rb
->v
, impulse
, rb
->inv_mass
, rb
->v
);
1279 /* Angular velocity */
1281 v3_cross( delta
, impulse
, wa
);
1283 m3x3_mulv( rb
->iIw
, wa
, wa
);
1284 v3_add( rb
->w
, wa
, rb
->w
);
1288 * One iteration to solve the contact constraint
1290 VG_STATIC
void rb_solve_contacts( rb_ct
*buf
, int len
){
1291 for( int i
=0; i
<len
; i
++ ){
1292 struct contact
*ct
= &buf
[i
];
1295 v3_sub( ct
->co
, ct
->rba
->co
, ra
);
1296 v3_sub( ct
->co
, ct
->rbb
->co
, rb
);
1297 rb_rcv( ct
->rba
, ct
->rbb
, ra
, rb
, rv
);
1300 for( int j
=0; j
<2; j
++ ){
1301 float f
= k_friction
* ct
->norm_impulse
,
1302 vt
= v3_dot( rv
, ct
->t
[j
] ),
1303 lambda
= ct
->tangent_mass
[j
] * -vt
;
1305 float temp
= ct
->tangent_impulse
[j
];
1306 ct
->tangent_impulse
[j
] = vg_clampf( temp
+ lambda
, -f
, f
);
1307 lambda
= ct
->tangent_impulse
[j
] - temp
;
1310 v3_muls( ct
->t
[j
], lambda
, impulse
);
1311 rb_linear_impulse( ct
->rba
, ra
, impulse
);
1313 v3_muls( ct
->t
[j
], -lambda
, impulse
);
1314 rb_linear_impulse( ct
->rbb
, rb
, impulse
);
1318 rb_rcv( ct
->rba
, ct
->rbb
, ra
, rb
, rv
);
1319 float vn
= v3_dot( rv
, ct
->n
),
1320 lambda
= ct
->normal_mass
* (-vn
+ ct
->bias
);
1322 float temp
= ct
->norm_impulse
;
1323 ct
->norm_impulse
= vg_maxf( temp
+ lambda
, 0.0f
);
1324 lambda
= ct
->norm_impulse
- temp
;
1327 v3_muls( ct
->n
, lambda
, impulse
);
1328 rb_linear_impulse( ct
->rba
, ra
, impulse
);
1330 v3_muls( ct
->n
, -lambda
, impulse
);
1331 rb_linear_impulse( ct
->rbb
, rb
, impulse
);
1336 * -----------------------------------------------------------------------------
1338 * -----------------------------------------------------------------------------
1341 VG_STATIC
void rb_debug_position_constraints( rb_constr_pos
*buffer
, int len
){
1342 for( int i
=0; i
<len
; i
++ ){
1343 rb_constr_pos
*constr
= &buffer
[i
];
1344 rigidbody
*rba
= constr
->rba
, *rbb
= constr
->rbb
;
1347 m3x3_mulv( rba
->to_world
, constr
->lca
, wca
);
1348 m3x3_mulv( rbb
->to_world
, constr
->lcb
, wcb
);
1351 v3_add( wca
, rba
->co
, p0
);
1352 v3_add( wcb
, rbb
->co
, p1
);
1353 vg_line_point( p0
, 0.0025f
, 0xff000000 );
1354 vg_line_point( p1
, 0.0025f
, 0xffffffff );
1355 vg_line2( p0
, p1
, 0xff000000, 0xffffffff );
1359 VG_STATIC
void rb_presolve_swingtwist_constraints( rb_constr_swingtwist
*buf
,
1363 for( int i
=0; i
<len
; i
++ ){
1364 rb_constr_swingtwist
*st
= &buf
[ i
];
1366 v3f vx
, vy
, va
, vxb
, axis
, center
;
1368 m3x3_mulv( st
->rba
->to_world
, st
->conevx
, vx
);
1369 m3x3_mulv( st
->rbb
->to_world
, st
->conevxb
, vxb
);
1370 m3x3_mulv( st
->rba
->to_world
, st
->conevy
, vy
);
1371 m3x3_mulv( st
->rbb
->to_world
, st
->coneva
, va
);
1372 m4x3_mulv( st
->rba
->to_world
, st
->view_offset
, center
);
1373 v3_cross( vy
, vx
, axis
);
1375 /* Constraint violated ? */
1376 float fx
= v3_dot( vx
, va
), /* projection world */
1377 fy
= v3_dot( vy
, va
),
1378 fn
= v3_dot( va
, axis
),
1380 rx
= st
->conevx
[3], /* elipse radii */
1383 lx
= fx
/rx
, /* projection local (fn==lz) */
1386 st
->tangent_violation
= ((lx
*lx
+ ly
*ly
) > fn
*fn
) || (fn
<= 0.0f
);
1387 if( st
->tangent_violation
){
1388 /* Calculate a good position and the axis to solve on */
1389 v2f closest
, tangent
,
1390 p
= { fx
/fabsf(fn
), fy
/fabsf(fn
) };
1392 closest_point_elipse( p
, (v2f
){rx
,ry
}, closest
);
1393 tangent
[0] = -closest
[1] / (ry
*ry
);
1394 tangent
[1] = closest
[0] / (rx
*rx
);
1395 v2_normalize( tangent
);
1398 v3_muladds( axis
, vx
, closest
[0], v0
);
1399 v3_muladds( v0
, vy
, closest
[1], v0
);
1402 v3_muls( vx
, tangent
[0], v1
);
1403 v3_muladds( v1
, vy
, tangent
[1], v1
);
1405 v3_copy( v0
, st
->tangent_target
);
1406 v3_copy( v1
, st
->tangent_axis
);
1408 /* calculate mass */
1410 m3x3_mulv( st
->rba
->iIw
, st
->tangent_axis
, aIw
);
1411 m3x3_mulv( st
->rbb
->iIw
, st
->tangent_axis
, bIw
);
1412 st
->tangent_mass
= 1.0f
/ (v3_dot( st
->tangent_axis
, aIw
) +
1413 v3_dot( st
->tangent_axis
, bIw
));
1415 float angle
= v3_dot( va
, st
->tangent_target
);
1419 v3_cross( vy
, va
, refaxis
); /* our default rotation */
1420 v3_normalize( refaxis
);
1422 float angle
= v3_dot( refaxis
, vxb
);
1423 st
->axis_violation
= fabsf(angle
) < st
->conet
;
1425 if( st
->axis_violation
){
1427 v3_cross( refaxis
, vxb
, dir_test
);
1429 if( v3_dot(dir_test
, va
) < 0.0f
)
1430 st
->axis_violation
= -st
->axis_violation
;
1432 float newang
= (float)st
->axis_violation
* acosf(st
->conet
-0.0001f
);
1435 v3_cross( va
, refaxis
, refaxis_up
);
1436 v3_muls( refaxis_up
, sinf(newang
), st
->axis_target
);
1437 v3_muladds( st
->axis_target
, refaxis
, -cosf(newang
), st
->axis_target
);
1439 /* calculate mass */
1440 v3_copy( va
, st
->axis
);
1442 m3x3_mulv( st
->rba
->iIw
, st
->axis
, aIw
);
1443 m3x3_mulv( st
->rbb
->iIw
, st
->axis
, bIw
);
1444 st
->axis_mass
= 1.0f
/ (v3_dot( st
->axis
, aIw
) +
1445 v3_dot( st
->axis
, bIw
));
1450 VG_STATIC
void rb_debug_swingtwist_constraints( rb_constr_swingtwist
*buf
,
1454 for( int i
=0; i
<len
; i
++ ){
1455 rb_constr_swingtwist
*st
= &buf
[ i
];
1457 v3f vx
, vxb
, vy
, va
, axis
, center
;
1459 m3x3_mulv( st
->rba
->to_world
, st
->conevx
, vx
);
1460 m3x3_mulv( st
->rbb
->to_world
, st
->conevxb
, vxb
);
1461 m3x3_mulv( st
->rba
->to_world
, st
->conevy
, vy
);
1462 m3x3_mulv( st
->rbb
->to_world
, st
->coneva
, va
);
1463 m4x3_mulv( st
->rba
->to_world
, st
->view_offset
, center
);
1464 v3_cross( vy
, vx
, axis
);
1466 float rx
= st
->conevx
[3], /* elipse radii */
1470 v3_muladds( center
, va
, size
, p1
);
1471 vg_line( center
, p1
, 0xffffffff );
1472 vg_line_point( p1
, 0.00025f
, 0xffffffff );
1474 if( st
->tangent_violation
){
1475 v3_muladds( center
, st
->tangent_target
, size
, p0
);
1477 vg_line( center
, p0
, 0xff00ff00 );
1478 vg_line_point( p0
, 0.00025f
, 0xff00ff00 );
1479 vg_line( p1
, p0
, 0xff000000 );
1482 for( int x
=0; x
<32; x
++ ){
1483 float t0
= ((float)x
* (1.0f
/32.0f
)) * VG_TAUf
,
1484 t1
= (((float)x
+1.0f
) * (1.0f
/32.0f
)) * VG_TAUf
,
1491 v3_muladds( axis
, vx
, c0
*rx
, v0
);
1492 v3_muladds( v0
, vy
, s0
*ry
, v0
);
1493 v3_muladds( axis
, vx
, c1
*rx
, v1
);
1494 v3_muladds( v1
, vy
, s1
*ry
, v1
);
1499 v3_muladds( center
, v0
, size
, p0
);
1500 v3_muladds( center
, v1
, size
, p1
);
1502 u32 col0r
= fabsf(c0
) * 255.0f
,
1503 col0g
= fabsf(s0
) * 255.0f
,
1504 col1r
= fabsf(c1
) * 255.0f
,
1505 col1g
= fabsf(s1
) * 255.0f
,
1506 col
= st
->tangent_violation
? 0xff0000ff: 0xff000000,
1507 col0
= col
| (col0r
<<16) | (col0g
<< 8),
1508 col1
= col
| (col1r
<<16) | (col1g
<< 8);
1510 vg_line2( center
, p0
, VG__NONE
, col0
);
1511 vg_line2( p0
, p1
, col0
, col1
);
1515 v3_muladds( center
, va
, size
, p0
);
1516 v3_muladds( p0
, vxb
, size
, p1
);
1518 vg_line( p0
, p1
, 0xff0000ff );
1520 if( st
->axis_violation
){
1521 v3_muladds( p0
, st
->axis_target
, size
*1.25f
, p1
);
1522 vg_line( p0
, p1
, 0xffffff00 );
1523 vg_line_point( p1
, 0.0025f
, 0xffffff80 );
1527 v3_cross( vy
, va
, refaxis
); /* our default rotation */
1528 v3_normalize( refaxis
);
1530 v3_cross( va
, refaxis
, refaxis_up
);
1531 float newang
= acosf(st
->conet
-0.0001f
);
1533 v3_muladds( p0
, refaxis_up
, sinf(newang
)*size
, p1
);
1534 v3_muladds( p1
, refaxis
, -cosf(newang
)*size
, p1
);
1535 vg_line( p0
, p1
, 0xff000000 );
1537 v3_muladds( p0
, refaxis_up
, sinf(-newang
)*size
, p1
);
1538 v3_muladds( p1
, refaxis
, -cosf(-newang
)*size
, p1
);
1539 vg_line( p0
, p1
, 0xff404040 );
1544 * Solve a list of positional constraints
1546 VG_STATIC
void rb_solve_position_constraints( rb_constr_pos
*buf
, int len
){
1547 for( int i
=0; i
<len
; i
++ ){
1548 rb_constr_pos
*constr
= &buf
[i
];
1549 rigidbody
*rba
= constr
->rba
, *rbb
= constr
->rbb
;
1552 m3x3_mulv( rba
->to_world
, constr
->lca
, wa
);
1553 m3x3_mulv( rbb
->to_world
, constr
->lcb
, wb
);
1555 m3x3f ssra
, ssrat
, ssrb
, ssrbt
;
1557 m3x3_skew_symetric( ssrat
, wa
);
1558 m3x3_skew_symetric( ssrbt
, wb
);
1559 m3x3_transpose( ssrat
, ssra
);
1560 m3x3_transpose( ssrbt
, ssrb
);
1562 v3f b
, b_wa
, b_wb
, b_a
, b_b
;
1563 m3x3_mulv( ssra
, rba
->w
, b_wa
);
1564 m3x3_mulv( ssrb
, rbb
->w
, b_wb
);
1565 v3_add( rba
->v
, b_wa
, b
);
1566 v3_sub( b
, rbb
->v
, b
);
1567 v3_sub( b
, b_wb
, b
);
1568 v3_muls( b
, -1.0f
, b
);
1571 m3x3_diagonal( invMa
, rba
->inv_mass
);
1572 m3x3_diagonal( invMb
, rbb
->inv_mass
);
1575 m3x3_mul( ssra
, rba
->iIw
, ia
);
1576 m3x3_mul( ia
, ssrat
, ia
);
1577 m3x3_mul( ssrb
, rbb
->iIw
, ib
);
1578 m3x3_mul( ib
, ssrbt
, ib
);
1581 m3x3_add( invMa
, ia
, cma
);
1582 m3x3_add( invMb
, ib
, cmb
);
1585 m3x3_add( cma
, cmb
, A
);
1587 /* Solve Ax = b ( A^-1*b = x ) */
1590 m3x3_inv( A
, invA
);
1591 m3x3_mulv( invA
, b
, impulse
);
1593 v3f delta_va
, delta_wa
, delta_vb
, delta_wb
;
1595 m3x3_mul( rba
->iIw
, ssrat
, iwa
);
1596 m3x3_mul( rbb
->iIw
, ssrbt
, iwb
);
1598 m3x3_mulv( invMa
, impulse
, delta_va
);
1599 m3x3_mulv( invMb
, impulse
, delta_vb
);
1600 m3x3_mulv( iwa
, impulse
, delta_wa
);
1601 m3x3_mulv( iwb
, impulse
, delta_wb
);
1603 v3_add( rba
->v
, delta_va
, rba
->v
);
1604 v3_add( rba
->w
, delta_wa
, rba
->w
);
1605 v3_sub( rbb
->v
, delta_vb
, rbb
->v
);
1606 v3_sub( rbb
->w
, delta_wb
, rbb
->w
);
1610 VG_STATIC
void rb_solve_swingtwist_constraints( rb_constr_swingtwist
*buf
,
1614 for( int i
=0; i
<len
; i
++ ){
1615 rb_constr_swingtwist
*st
= &buf
[ i
];
1617 if( !st
->axis_violation
)
1620 float rv
= v3_dot( st
->axis
, st
->rbb
->w
) -
1621 v3_dot( st
->axis
, st
->rba
->w
);
1623 if( rv
* (float)st
->axis_violation
> 0.0f
)
1626 v3f impulse
, wa
, wb
;
1627 v3_muls( st
->axis
, rv
*st
->axis_mass
, impulse
);
1628 m3x3_mulv( st
->rba
->iIw
, impulse
, wa
);
1629 v3_add( st
->rba
->w
, wa
, st
->rba
->w
);
1631 v3_muls( impulse
, -1.0f
, impulse
);
1632 m3x3_mulv( st
->rbb
->iIw
, impulse
, wb
);
1633 v3_add( st
->rbb
->w
, wb
, st
->rbb
->w
);
1635 float rv2
= v3_dot( st
->axis
, st
->rbb
->w
) -
1636 v3_dot( st
->axis
, st
->rba
->w
);
1639 for( int i
=0; i
<len
; i
++ ){
1640 rb_constr_swingtwist
*st
= &buf
[ i
];
1642 if( !st
->tangent_violation
)
1645 float rv
= v3_dot( st
->tangent_axis
, st
->rbb
->w
) -
1646 v3_dot( st
->tangent_axis
, st
->rba
->w
);
1651 v3f impulse
, wa
, wb
;
1652 v3_muls( st
->tangent_axis
, rv
*st
->tangent_mass
, impulse
);
1653 m3x3_mulv( st
->rba
->iIw
, impulse
, wa
);
1654 v3_add( st
->rba
->w
, wa
, st
->rba
->w
);
1656 v3_muls( impulse
, -1.0f
, impulse
);
1657 m3x3_mulv( st
->rbb
->iIw
, impulse
, wb
);
1658 v3_add( st
->rbb
->w
, wb
, st
->rbb
->w
);
1660 float rv2
= v3_dot( st
->tangent_axis
, st
->rbb
->w
) -
1661 v3_dot( st
->tangent_axis
, st
->rba
->w
);
1665 VG_STATIC
void rb_solve_constr_angle( rigidbody
*rba
, rigidbody
*rbb
,
1667 m3x3f ssra
, ssrb
, ssrat
, ssrbt
;
1670 m3x3_skew_symetric( ssrat
, ra
);
1671 m3x3_skew_symetric( ssrbt
, rb
);
1672 m3x3_transpose( ssrat
, ssra
);
1673 m3x3_transpose( ssrbt
, ssrb
);
1675 m3x3_mul( ssra
, rba
->iIw
, cma
);
1676 m3x3_mul( cma
, ssrat
, cma
);
1677 m3x3_mul( ssrb
, rbb
->iIw
, cmb
);
1678 m3x3_mul( cmb
, ssrbt
, cmb
);
1681 m3x3_add( cma
, cmb
, A
);
1682 m3x3_inv( A
, invA
);
1685 m3x3_mulv( ssra
, rba
->w
, b_wa
);
1686 m3x3_mulv( ssrb
, rbb
->w
, b_wb
);
1687 v3_add( b_wa
, b_wb
, b
);
1691 m3x3_mulv( invA
, b
, impulse
);
1693 v3f delta_wa
, delta_wb
;
1695 m3x3_mul( rba
->iIw
, ssrat
, iwa
);
1696 m3x3_mul( rbb
->iIw
, ssrbt
, iwb
);
1697 m3x3_mulv( iwa
, impulse
, delta_wa
);
1698 m3x3_mulv( iwb
, impulse
, delta_wb
);
1699 v3_add( rba
->w
, delta_wa
, rba
->w
);
1700 v3_sub( rbb
->w
, delta_wb
, rbb
->w
);
1704 * Correct position constraint drift errors
1705 * [ 0.0 <= amt <= 1.0 ]: the correction amount
1707 VG_STATIC
void rb_correct_position_constraints( rb_constr_pos
*buf
, int len
,
1709 for( int i
=0; i
<len
; i
++ ){
1710 rb_constr_pos
*constr
= &buf
[i
];
1711 rigidbody
*rba
= constr
->rba
, *rbb
= constr
->rbb
;
1714 m3x3_mulv( rba
->to_world
, constr
->lca
, p0
);
1715 m3x3_mulv( rbb
->to_world
, constr
->lcb
, p1
);
1716 v3_add( rba
->co
, p0
, p0
);
1717 v3_add( rbb
->co
, p1
, p1
);
1718 v3_sub( p1
, p0
, d
);
1720 v3_muladds( rbb
->co
, d
, -1.0f
* amt
, rbb
->co
);
1721 rb_update_transform( rbb
);
1725 VG_STATIC
void rb_correct_swingtwist_constraints( rb_constr_swingtwist
*buf
,
1726 int len
, float amt
){
1727 for( int i
=0; i
<len
; i
++ ){
1728 rb_constr_swingtwist
*st
= &buf
[i
];
1730 if( !st
->tangent_violation
)
1734 m3x3_mulv( st
->rbb
->to_world
, st
->coneva
, va
);
1736 float angle
= v3_dot( va
, st
->tangent_target
);
1738 if( fabsf(angle
) < 0.9999f
){
1740 v3_cross( va
, st
->tangent_target
, axis
);
1743 q_axis_angle( correction
, axis
, acosf(angle
) * amt
);
1744 q_mul( correction
, st
->rbb
->q
, st
->rbb
->q
);
1745 rb_update_transform( st
->rbb
);
1749 for( int i
=0; i
<len
; i
++ ){
1750 rb_constr_swingtwist
*st
= &buf
[i
];
1752 if( !st
->axis_violation
)
1756 m3x3_mulv( st
->rbb
->to_world
, st
->conevxb
, vxb
);
1758 float angle
= v3_dot( vxb
, st
->axis_target
);
1760 if( fabsf(angle
) < 0.9999f
){
1762 v3_cross( vxb
, st
->axis_target
, axis
);
1765 q_axis_angle( correction
, axis
, acosf(angle
) * amt
);
1766 q_mul( correction
, st
->rbb
->q
, st
->rbb
->q
);
1767 rb_update_transform( st
->rbb
);
1772 VG_STATIC
void rb_correct_contact_constraints( rb_ct
*buf
, int len
, float amt
){
1773 for( int i
=0; i
<len
; i
++ ){
1774 rb_ct
*ct
= &buf
[i
];
1775 rigidbody
*rba
= ct
->rba
,
1778 float mass_total
= 1.0f
/ (rba
->inv_mass
+ rbb
->inv_mass
);
1780 v3_muladds( rba
->co
, ct
->n
, -mass_total
* rba
->inv_mass
, rba
->co
);
1781 v3_muladds( rbb
->co
, ct
->n
, mass_total
* rbb
->inv_mass
, rbb
->co
);
1790 VG_STATIC
void rb_effect_simple_bouyency( rigidbody
*ra
, v4f plane
,
1791 float amt
, float drag
){
1793 float depth
= v3_dot( plane
, ra
->co
) - plane
[3],
1794 lambda
= vg_clampf( -depth
, 0.0f
, 1.0f
) * amt
;
1796 v3_muladds( ra
->v
, plane
, lambda
* k_rb_delta
, ra
->v
);
1799 v3_muls( ra
->v
, 1.0f
-(drag
*k_rb_delta
), ra
->v
);
1802 /* apply a spring&dampener force to match ra(worldspace) on rigidbody, to
1805 VG_STATIC
void rb_effect_spring_target_vector( rigidbody
*rba
, v3f ra
, v3f rt
,
1806 float spring
, float dampening
,
1808 float d
= v3_dot( rt
, ra
);
1809 float a
= acosf( vg_clampf( d
, -1.0f
, 1.0f
) );
1812 v3_cross( rt
, ra
, axis
);
1814 float Fs
= -a
* spring
,
1815 Fd
= -v3_dot( rba
->w
, axis
) * dampening
;
1817 v3_muladds( rba
->w
, axis
, (Fs
+Fd
) * timestep
, rba
->w
);
1820 #endif /* RIGIDBODY_H */