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 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 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 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 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 static void rb_update_bounds( rigidbody
*rb
){
195 box_init_inf( rb
->bbx_world
);
196 m4x3_expand_aabb_aabb( rb
->to_world
, rb
->bbx_world
, rb
->bbx
);
200 * Commit transform to rigidbody. Updates matrices
202 static void rb_update_transform( rigidbody
*rb
)
204 q_normalize( rb
->q
);
205 q_m3x3( rb
->q
, rb
->to_world
);
206 v3_copy( rb
->co
, rb
->to_world
[3] );
208 m4x3_invert_affine( rb
->to_world
, rb
->to_local
);
209 m3x3_mul( rb
->iI
, rb
->to_local
, rb
->iIw
);
210 m3x3_mul( rb
->to_world
, rb
->iIw
, rb
->iIw
);
212 rb_update_bounds( rb
);
216 * Extrapolate rigidbody into a transform based on vg accumulator.
217 * Useful for rendering
219 static void rb_extrapolate( rigidbody
*rb
, v3f co
, v4f q
)
221 float substep
= vg
.time_fixed_extrapolate
;
222 v3_muladds( rb
->co
, rb
->v
, k_rb_delta
*substep
, co
);
224 if( v3_length2( rb
->w
) > 0.0f
){
227 v3_copy( rb
->w
, axis
);
229 float mag
= v3_length( axis
);
230 v3_divs( axis
, mag
, axis
);
231 q_axis_angle( rotation
, axis
, mag
*k_rb_delta
*substep
);
232 q_mul( rotation
, rb
->q
, q
);
241 * Initialize rigidbody and calculate masses, inertia
243 static void rb_init_object( rb_object
*obj
){
247 if( obj
->type
== k_rb_shape_box
){
249 v3_sub( obj
->rb
.bbx
[1], obj
->rb
.bbx
[0], dims
);
250 volume
= dims
[0]*dims
[1]*dims
[2];
252 else if( obj
->type
== k_rb_shape_sphere
){
253 volume
= vg_sphere_volume( obj
->inf
.sphere
.radius
);
254 v3_fill( obj
->rb
.bbx
[0], -obj
->inf
.sphere
.radius
);
255 v3_fill( obj
->rb
.bbx
[1], obj
->inf
.sphere
.radius
);
257 else if( obj
->type
== k_rb_shape_capsule
){
258 float r
= obj
->inf
.capsule
.radius
,
259 h
= obj
->inf
.capsule
.height
;
260 volume
= vg_sphere_volume( r
) + VG_PIf
* r
*r
* (h
- r
*2.0f
);
262 v3_fill( obj
->rb
.bbx
[0], -r
);
263 v3_fill( obj
->rb
.bbx
[1], r
);
264 obj
->rb
.bbx
[0][1] = -h
;
265 obj
->rb
.bbx
[1][1] = h
;
267 else if( obj
->type
== k_rb_shape_scene
){
269 box_copy( obj
->inf
.scene
.bh_scene
->nodes
[0].bbx
, obj
->rb
.bbx
);
273 obj
->rb
.inv_mass
= 0.0f
;
274 v3_zero( obj
->rb
.I
);
275 m3x3_zero( obj
->rb
.iI
);
278 float mass
= 2.0f
*volume
;
279 obj
->rb
.inv_mass
= 1.0f
/mass
;
282 v3_sub( obj
->rb
.bbx
[1], obj
->rb
.bbx
[0], extent
);
283 v3_muls( extent
, 0.5f
, extent
);
285 /* local intertia tensor */
286 float scale
= k_inertia_scale
;
287 float ex2
= scale
*extent
[0]*extent
[0],
288 ey2
= scale
*extent
[1]*extent
[1],
289 ez2
= scale
*extent
[2]*extent
[2];
291 obj
->rb
.I
[0] = ((1.0f
/12.0f
) * mass
* (ey2
+ez2
));
292 obj
->rb
.I
[1] = ((1.0f
/12.0f
) * mass
* (ex2
+ez2
));
293 obj
->rb
.I
[2] = ((1.0f
/12.0f
) * mass
* (ex2
+ey2
));
295 m3x3_identity( obj
->rb
.iI
);
296 obj
->rb
.iI
[0][0] = obj
->rb
.I
[0];
297 obj
->rb
.iI
[1][1] = obj
->rb
.I
[1];
298 obj
->rb
.iI
[2][2] = obj
->rb
.I
[2];
299 m3x3_inv( obj
->rb
.iI
, obj
->rb
.iI
);
302 rb_update_transform( &obj
->rb
);
305 static void rb_iter( rigidbody
*rb
){
306 if( !vg_validf( rb
->v
[0] ) ||
307 !vg_validf( rb
->v
[1] ) ||
308 !vg_validf( rb
->v
[2] ) )
310 vg_fatal_error( "NaN velocity" );
313 v3f gravity
= { 0.0f
, -9.8f
, 0.0f
};
314 v3_muladds( rb
->v
, gravity
, k_rb_delta
, rb
->v
);
316 /* intergrate velocity */
317 v3_muladds( rb
->co
, rb
->v
, k_rb_delta
, rb
->co
);
318 v3_lerp( rb
->w
, (v3f
){0.0f
,0.0f
,0.0f
}, 0.0025f
, rb
->w
);
320 /* inegrate inertia */
321 if( v3_length2( rb
->w
) > 0.0f
)
325 v3_copy( rb
->w
, axis
);
327 float mag
= v3_length( axis
);
328 v3_divs( axis
, mag
, axis
);
329 q_axis_angle( rotation
, axis
, mag
*k_rb_delta
);
330 q_mul( rotation
, rb
->q
, rb
->q
);
334 v3_muls( rb
->v
, 1.0f
/(1.0f
+k_rb_delta
*k_damp_linear
), rb
->v
);
335 v3_muls( rb
->w
, 1.0f
/(1.0f
+k_rb_delta
*k_damp_angular
), rb
->w
);
340 * -----------------------------------------------------------------------------
341 * Boolean shape overlap functions
342 * -----------------------------------------------------------------------------
346 * Project AABB, and triangle interval onto axis to check if they overlap
348 static int rb_box_triangle_interval( v3f extent
, v3f axis
, v3f tri
[3] ){
351 r
= extent
[0] * fabsf(axis
[0]) +
352 extent
[1] * fabsf(axis
[1]) +
353 extent
[2] * fabsf(axis
[2]),
355 p0
= v3_dot( axis
, tri
[0] ),
356 p1
= v3_dot( axis
, tri
[1] ),
357 p2
= v3_dot( axis
, tri
[2] ),
359 e
= vg_maxf(-vg_maxf(p0
,vg_maxf(p1
,p2
)), vg_minf(p0
,vg_minf(p1
,p2
)));
361 if( e
> r
) return 0;
366 * Seperating axis test box vs triangle
368 static int rb_box_triangle_sat( v3f extent
, v3f center
,
369 m4x3f to_local
, v3f tri_src
[3] ){
372 for( int i
=0; i
<3; i
++ ){
373 m4x3_mulv( to_local
, tri_src
[i
], tri
[i
] );
374 v3_sub( tri
[i
], center
, tri
[i
] );
378 v3_sub( tri
[1], tri
[0], f0
);
379 v3_sub( tri
[2], tri
[1], f1
);
380 v3_sub( tri
[0], tri
[2], f2
);
384 v3_cross( (v3f
){1.0f
,0.0f
,0.0f
}, f0
, axis
[0] );
385 v3_cross( (v3f
){1.0f
,0.0f
,0.0f
}, f1
, axis
[1] );
386 v3_cross( (v3f
){1.0f
,0.0f
,0.0f
}, f2
, axis
[2] );
387 v3_cross( (v3f
){0.0f
,1.0f
,0.0f
}, f0
, axis
[3] );
388 v3_cross( (v3f
){0.0f
,1.0f
,0.0f
}, f1
, axis
[4] );
389 v3_cross( (v3f
){0.0f
,1.0f
,0.0f
}, f2
, axis
[5] );
390 v3_cross( (v3f
){0.0f
,0.0f
,1.0f
}, f0
, axis
[6] );
391 v3_cross( (v3f
){0.0f
,0.0f
,1.0f
}, f1
, axis
[7] );
392 v3_cross( (v3f
){0.0f
,0.0f
,1.0f
}, f2
, axis
[8] );
394 for( int i
=0; i
<9; i
++ )
395 if(!rb_box_triangle_interval( extent
, axis
[i
], tri
)) return 0;
398 if(!rb_box_triangle_interval( extent
, (v3f
){1.0f
,0.0f
,0.0f
}, tri
)) return 0;
399 if(!rb_box_triangle_interval( extent
, (v3f
){0.0f
,1.0f
,0.0f
}, tri
)) return 0;
400 if(!rb_box_triangle_interval( extent
, (v3f
){0.0f
,0.0f
,1.0f
}, tri
)) return 0;
403 v3_cross( f0
, f1
, n
);
404 if(!rb_box_triangle_interval( extent
, n
, tri
)) return 0;
410 * -----------------------------------------------------------------------------
412 * -----------------------------------------------------------------------------
415 static int rb_manifold_apply_filtered( rb_ct
*man
, int len
){
418 for( int i
=0; i
<len
; i
++ ){
421 if( ct
->type
== k_contact_type_disabled
)
431 * Merge two contacts if they are within radius(r) of eachother
433 static void rb_manifold_contact_weld( rb_ct
*ci
, rb_ct
*cj
, float r
){
434 if( v3_dist2( ci
->co
, cj
->co
) < r
*r
){
435 cj
->type
= k_contact_type_disabled
;
436 ci
->p
= (ci
->p
+ cj
->p
) * 0.5f
;
438 v3_add( ci
->co
, cj
->co
, ci
->co
);
439 v3_muls( ci
->co
, 0.5f
, ci
->co
);
442 v3_sub( ci
->rba
->co
, ci
->co
, delta
);
444 float c0
= v3_dot( ci
->n
, delta
),
445 c1
= v3_dot( cj
->n
, delta
);
447 if( c0
< 0.0f
|| c1
< 0.0f
){
449 ci
->type
= k_contact_type_disabled
;
453 v3_muls( ci
->n
, c0
, n
);
454 v3_muladds( n
, cj
->n
, c1
, n
);
464 static void rb_manifold_filter_joint_edges( rb_ct
*man
, int len
, float r
){
465 for( int i
=0; i
<len
-1; i
++ ){
467 if( ci
->type
!= k_contact_type_edge
)
470 for( int j
=i
+1; j
<len
; j
++ ){
472 if( cj
->type
!= k_contact_type_edge
)
475 rb_manifold_contact_weld( ci
, cj
, r
);
481 * Resolve overlapping pairs
483 static void rb_manifold_filter_pairs( rb_ct
*man
, int len
, float r
){
484 for( int i
=0; i
<len
-1; i
++ ){
488 if( ci
->type
== k_contact_type_disabled
) continue;
490 for( int j
=i
+1; j
<len
; j
++ ){
493 if( cj
->type
== k_contact_type_disabled
) continue;
495 if( v3_dist2( ci
->co
, cj
->co
) < r
*r
){
496 cj
->type
= k_contact_type_disabled
;
497 v3_add( cj
->n
, ci
->n
, ci
->n
);
504 float n
= 1.0f
/((float)similar
+1.0f
);
505 v3_muls( ci
->n
, n
, ci
->n
);
508 if( v3_length2(ci
->n
) < 0.1f
*0.1f
)
509 ci
->type
= k_contact_type_disabled
;
511 v3_normalize( ci
->n
);
517 * Remove contacts that are facing away from A
519 static void rb_manifold_filter_backface( rb_ct
*man
, int len
){
520 for( int i
=0; i
<len
; i
++ ){
522 if( ct
->type
== k_contact_type_disabled
)
526 v3_sub( ct
->co
, ct
->rba
->co
, delta
);
528 if( v3_dot( delta
, ct
->n
) > -0.001f
)
529 ct
->type
= k_contact_type_disabled
;
534 * Filter out duplicate coplanar results. Good for spheres.
536 static void rb_manifold_filter_coplanar( rb_ct
*man
, int len
, float w
){
537 for( int i
=0; i
<len
; i
++ ){
539 if( ci
->type
== k_contact_type_disabled
||
540 ci
->type
== k_contact_type_edge
)
543 float d1
= v3_dot( ci
->co
, ci
->n
);
545 for( int j
=0; j
<len
; j
++ ){
550 if( cj
->type
== k_contact_type_disabled
)
553 float d2
= v3_dot( cj
->co
, ci
->n
),
556 if( fabsf( d
) <= w
){
557 cj
->type
= k_contact_type_disabled
;
564 * -----------------------------------------------------------------------------
566 * -----------------------------------------------------------------------------
572 * These do not automatically allocate contacts, an appropriately sized
573 * buffer must be supplied. The function returns the size of the manifold
574 * which was generated.
576 * The values set on the contacts are: n, co, p, rba, rbb
580 * By collecting the minimum(time) and maximum(time) pairs of points, we
581 * build a reduced and stable exact manifold.
584 * rx: minimum distance of these points
585 * dx: the delta between the two points
587 * pairs will only ammend these if they are creating a collision
589 typedef struct capsule_manifold capsule_manifold
;
590 struct capsule_manifold
{
597 * Expand a line manifold with a new pair. t value is the time along segment
598 * on the oriented object which created this pair.
600 static void rb_capsule_manifold( v3f pa
, v3f pb
, float t
, float r
,
601 capsule_manifold
*manifold
){
603 v3_sub( pa
, pb
, delta
);
605 if( v3_length2(delta
) < r
*r
){
606 if( t
< manifold
->t0
){
607 v3_copy( delta
, manifold
->d0
);
612 if( t
> manifold
->t1
){
613 v3_copy( delta
, manifold
->d1
);
620 static void rb_capsule_manifold_init( capsule_manifold
*manifold
){
621 manifold
->t0
= INFINITY
;
622 manifold
->t1
= -INFINITY
;
625 static int rb_capsule__manifold_done( m4x3f mtx
, rb_capsule
*c
,
626 capsule_manifold
*manifold
,
629 v3_muladds( mtx
[3], mtx
[1], -c
->height
*0.5f
+c
->radius
, p0
);
630 v3_muladds( mtx
[3], mtx
[1], c
->height
*0.5f
-c
->radius
, p1
);
633 if( manifold
->t0
<= 1.0f
){
637 v3_muls( p0
, 1.0f
-manifold
->t0
, pa
);
638 v3_muladds( pa
, p1
, manifold
->t0
, pa
);
640 float d
= v3_length( manifold
->d0
);
641 v3_muls( manifold
->d0
, 1.0f
/d
, ct
->n
);
642 v3_muladds( pa
, ct
->n
, -c
->radius
, ct
->co
);
644 ct
->p
= manifold
->r0
- d
;
645 ct
->type
= k_contact_type_default
;
649 if( (manifold
->t1
>= 0.0f
) && (manifold
->t0
!= manifold
->t1
) ){
650 rb_ct
*ct
= buf
+count
;
653 v3_muls( p0
, 1.0f
-manifold
->t1
, pa
);
654 v3_muladds( pa
, p1
, manifold
->t1
, pa
);
656 float d
= v3_length( manifold
->d1
);
657 v3_muls( manifold
->d1
, 1.0f
/d
, ct
->n
);
658 v3_muladds( pa
, ct
->n
, -c
->radius
, ct
->co
);
660 ct
->p
= manifold
->r1
- d
;
661 ct
->type
= k_contact_type_default
;
671 vg_line( buf
[0].co
, buf
[1].co
, 0xff0000ff );
676 static int rb_capsule_sphere( rb_object
*obja
, rb_object
*objb
, rb_ct
*buf
){
677 rigidbody
*rba
= &obja
->rb
, *rbb
= &objb
->rb
;
678 float h
= obja
->inf
.capsule
.height
,
679 ra
= obja
->inf
.capsule
.radius
,
680 rb
= objb
->inf
.sphere
.radius
;
683 v3_muladds( rba
->co
, rba
->to_world
[1], -h
*0.5f
+ra
, p0
);
684 v3_muladds( rba
->co
, rba
->to_world
[1], h
*0.5f
-ra
, p1
);
687 closest_point_segment( p0
, p1
, rbb
->co
, c
);
688 v3_sub( c
, rbb
->co
, delta
);
690 float d2
= v3_length2(delta
),
697 v3_muls( delta
, 1.0f
/d
, ct
->n
);
701 v3_muladds( c
, ct
->n
, -ra
, p0
);
702 v3_muladds( rbb
->co
, ct
->n
, rb
, p1
);
703 v3_add( p0
, p1
, ct
->co
);
704 v3_muls( ct
->co
, 0.5f
, ct
->co
);
708 ct
->type
= k_contact_type_default
;
716 static int rb_capsule__capsule( m4x3f mtxA
, rb_capsule
*ca
,
717 m4x3f mtxB
, rb_capsule
*cb
, rb_ct
*buf
){
718 float ha
= ca
->height
,
725 v3_muladds( mtxA
[3], mtxA
[1], -ha
*0.5f
+ra
, p0
);
726 v3_muladds( mtxA
[3], mtxA
[1], ha
*0.5f
-ra
, p1
);
727 v3_muladds( mtxB
[3], mtxB
[1], -hb
*0.5f
+rb
, p2
);
728 v3_muladds( mtxB
[3], mtxB
[1], hb
*0.5f
-rb
, p3
);
730 capsule_manifold manifold
;
731 rb_capsule_manifold_init( &manifold
);
735 closest_segment_segment( p0
, p1
, p2
, p3
, &ta
, &tb
, pa
, pb
);
736 rb_capsule_manifold( pa
, pb
, ta
, r
, &manifold
);
738 ta
= closest_point_segment( p0
, p1
, p2
, pa
);
739 tb
= closest_point_segment( p0
, p1
, p3
, pb
);
740 rb_capsule_manifold( pa
, p2
, ta
, r
, &manifold
);
741 rb_capsule_manifold( pb
, p3
, tb
, r
, &manifold
);
743 closest_point_segment( p2
, p3
, p0
, pa
);
744 closest_point_segment( p2
, p3
, p1
, pb
);
745 rb_capsule_manifold( p0
, pa
, 0.0f
, r
, &manifold
);
746 rb_capsule_manifold( p1
, pb
, 1.0f
, r
, &manifold
);
748 return rb_capsule__manifold_done( mtxA
, ca
, &manifold
, buf
);
751 static int rb_sphere_box( rb_object
*obja
, rb_object
*objb
, rb_ct
*buf
){
753 rigidbody
*rba
= &obja
->rb
, *rbb
= &objb
->rb
;
755 closest_point_obb( rba
->co
, rbb
->bbx
, rbb
->to_world
, rbb
->to_local
, co
);
756 v3_sub( rba
->co
, co
, delta
);
758 float d2
= v3_length2(delta
),
759 r
= obja
->inf
.sphere
.radius
;
766 v3_sub( rba
->co
, rbb
->co
, delta
);
769 * some extra testing is required to find the best axis to push the
770 * object back outside the box. Since there isnt a clear seperating
771 * vector already, especially on really high aspect boxes.
773 float lx
= v3_dot( rbb
->to_world
[0], delta
),
774 ly
= v3_dot( rbb
->to_world
[1], delta
),
775 lz
= v3_dot( rbb
->to_world
[2], delta
),
776 px
= rbb
->bbx
[1][0] - fabsf(lx
),
777 py
= rbb
->bbx
[1][1] - fabsf(ly
),
778 pz
= rbb
->bbx
[1][2] - fabsf(lz
);
780 if( px
< py
&& px
< pz
)
781 v3_muls( rbb
->to_world
[0], vg_signf(lx
), ct
->n
);
783 v3_muls( rbb
->to_world
[1], vg_signf(ly
), ct
->n
);
785 v3_muls( rbb
->to_world
[2], vg_signf(lz
), ct
->n
);
787 v3_muladds( rba
->co
, ct
->n
, -r
, ct
->co
);
792 v3_muls( delta
, 1.0f
/d
, ct
->n
);
794 v3_copy( co
, ct
->co
);
799 ct
->type
= k_contact_type_default
;
806 static int rb_sphere_sphere( rb_object
*obja
, rb_object
*objb
, rb_ct
*buf
){
807 rigidbody
*rba
= &obja
->rb
, *rbb
= &objb
->rb
;
809 v3_sub( rba
->co
, rbb
->co
, delta
);
811 float d2
= v3_length2(delta
),
812 r
= obja
->inf
.sphere
.radius
+ objb
->inf
.sphere
.radius
;
818 v3_muls( delta
, 1.0f
/d
, ct
->n
);
821 v3_muladds( rba
->co
, ct
->n
,-obja
->inf
.sphere
.radius
, p0
);
822 v3_muladds( rbb
->co
, ct
->n
, objb
->inf
.sphere
.radius
, p1
);
823 v3_add( p0
, p1
, ct
->co
);
824 v3_muls( ct
->co
, 0.5f
, ct
->co
);
825 ct
->type
= k_contact_type_default
;
835 static int rb_sphere__triangle( m4x3f mtxA
, rb_sphere
*b
,
836 v3f tri
[3], rb_ct
*buf
){
838 enum contact_type type
= closest_on_triangle_1( mtxA
[3], tri
, co
);
840 v3_sub( mtxA
[3], co
, delta
);
842 float d2
= v3_length2( delta
),
849 v3_sub( tri
[2], tri
[0], ab
);
850 v3_sub( tri
[1], tri
[0], ac
);
851 v3_cross( ac
, ab
, tn
);
852 v3_copy( tn
, ct
->n
);
854 if( v3_length2( ct
->n
) <= 0.00001f
){
855 #ifdef RIGIDBODY_CRY_ABOUT_EVERYTHING
856 vg_error( "Zero area triangle!\n" );
861 v3_normalize( ct
->n
);
865 v3_copy( co
, ct
->co
);
874 static int rb_sphere__scene( m4x3f mtxA
, rb_sphere
*b
,
875 m4x3f mtxB
, rb_scene
*s
, rb_ct
*buf
,
877 scene_context
*sc
= s
->bh_scene
->user
;
881 float r
= b
->radius
+ 0.1f
;
883 v3_sub( mtxA
[3], (v3f
){ r
,r
,r
}, box
[0] );
884 v3_add( mtxA
[3], (v3f
){ r
,r
,r
}, box
[1] );
888 bh_iter_init_box( 0, &it
, box
);
890 while( bh_next( s
->bh_scene
, &it
, &idx
) ){
891 u32
*ptri
= &sc
->arrindices
[ idx
*3 ];
894 if( sc
->arrvertices
[ptri
[0]].flags
& ignore
) continue;
896 for( int j
=0; j
<3; j
++ )
897 v3_copy( sc
->arrvertices
[ptri
[j
]].co
, tri
[j
] );
899 buf
[ count
].element_id
= ptri
[0];
901 vg_line( tri
[0],tri
[1],0x70ff6000 );
902 vg_line( tri
[1],tri
[2],0x70ff6000 );
903 vg_line( tri
[2],tri
[0],0x70ff6000 );
905 int contact
= rb_sphere__triangle( mtxA
, b
, tri
, &buf
[count
] );
909 vg_warn( "Exceeding sphere_vs_scene capacity. Geometry too dense!\n" );
917 static int rb_box__scene( m4x3f mtxA
, boxf bbx
,
918 m4x3f mtxB
, rb_scene
*s
, rb_ct
*buf
, u16 ignore
){
919 scene_context
*sc
= s
->bh_scene
->user
;
923 v3_sub( bbx
[1], bbx
[0], extent
);
924 v3_muls( extent
, 0.5f
, extent
);
925 v3_add( bbx
[0], extent
, center
);
927 float r
= v3_length(extent
);
929 v3_fill( world_bbx
[0], -r
);
930 v3_fill( world_bbx
[1], r
);
931 for( int i
=0; i
<2; i
++ ){
932 v3_add( center
, world_bbx
[i
], world_bbx
[i
] );
933 v3_add( mtxA
[3], world_bbx
[i
], world_bbx
[i
] );
937 m4x3_invert_affine( mtxA
, to_local
);
940 bh_iter_init_box( 0, &it
, world_bbx
);
944 vg_line_boxf( world_bbx
, VG__RED
);
946 while( bh_next( s
->bh_scene
, &it
, &idx
) ){
947 u32
*ptri
= &sc
->arrindices
[ idx
*3 ];
948 if( sc
->arrvertices
[ptri
[0]].flags
& ignore
) continue;
950 for( int j
=0; j
<3; j
++ )
951 v3_copy( sc
->arrvertices
[ptri
[j
]].co
, tri
[j
] );
953 if( rb_box_triangle_sat( extent
, center
, to_local
, tri
) ){
954 vg_line(tri
[0],tri
[1],0xff50ff00 );
955 vg_line(tri
[1],tri
[2],0xff50ff00 );
956 vg_line(tri
[2],tri
[0],0xff50ff00 );
959 vg_line(tri
[0],tri
[1],0xff0000ff );
960 vg_line(tri
[1],tri
[2],0xff0000ff );
961 vg_line(tri
[2],tri
[0],0xff0000ff );
966 v3_sub( tri
[1], tri
[0], v0
);
967 v3_sub( tri
[2], tri
[0], v1
);
968 v3_cross( v0
, v1
, n
);
970 if( v3_length2( n
) <= 0.00001f
){
971 #ifdef RIGIDBODY_CRY_ABOUT_EVERYTHING
972 vg_error( "Zero area triangle!\n" );
979 /* find best feature */
980 float best
= v3_dot( mtxA
[0], n
);
983 for( int i
=1; i
<3; i
++ ){
984 float c
= v3_dot( mtxA
[i
], n
);
986 if( fabsf(c
) > fabsf(best
) ){
995 float px
= best
> 0.0f
? bbx
[0][0]: bbx
[1][0];
997 manifold
[0][1] = bbx
[0][1];
998 manifold
[0][2] = bbx
[0][2];
1000 manifold
[1][1] = bbx
[1][1];
1001 manifold
[1][2] = bbx
[0][2];
1002 manifold
[2][0] = px
;
1003 manifold
[2][1] = bbx
[1][1];
1004 manifold
[2][2] = bbx
[1][2];
1005 manifold
[3][0] = px
;
1006 manifold
[3][1] = bbx
[0][1];
1007 manifold
[3][2] = bbx
[1][2];
1009 else if( axis
== 1 ){
1010 float py
= best
> 0.0f
? bbx
[0][1]: bbx
[1][1];
1011 manifold
[0][0] = bbx
[0][0];
1012 manifold
[0][1] = py
;
1013 manifold
[0][2] = bbx
[0][2];
1014 manifold
[1][0] = bbx
[1][0];
1015 manifold
[1][1] = py
;
1016 manifold
[1][2] = bbx
[0][2];
1017 manifold
[2][0] = bbx
[1][0];
1018 manifold
[2][1] = py
;
1019 manifold
[2][2] = bbx
[1][2];
1020 manifold
[3][0] = bbx
[0][0];
1021 manifold
[3][1] = py
;
1022 manifold
[3][2] = bbx
[1][2];
1025 float pz
= best
> 0.0f
? bbx
[0][2]: bbx
[1][2];
1026 manifold
[0][0] = bbx
[0][0];
1027 manifold
[0][1] = bbx
[0][1];
1028 manifold
[0][2] = pz
;
1029 manifold
[1][0] = bbx
[1][0];
1030 manifold
[1][1] = bbx
[0][1];
1031 manifold
[1][2] = pz
;
1032 manifold
[2][0] = bbx
[1][0];
1033 manifold
[2][1] = bbx
[1][1];
1034 manifold
[2][2] = pz
;
1035 manifold
[3][0] = bbx
[0][0];
1036 manifold
[3][1] = bbx
[1][1];
1037 manifold
[3][2] = pz
;
1040 for( int j
=0; j
<4; j
++ )
1041 m4x3_mulv( mtxA
, manifold
[j
], manifold
[j
] );
1043 vg_line( manifold
[0], manifold
[1], 0xffffffff );
1044 vg_line( manifold
[1], manifold
[2], 0xffffffff );
1045 vg_line( manifold
[2], manifold
[3], 0xffffffff );
1046 vg_line( manifold
[3], manifold
[0], 0xffffffff );
1048 for( int j
=0; j
<4; j
++ ){
1049 rb_ct
*ct
= buf
+count
;
1051 v3_copy( manifold
[j
], ct
->co
);
1052 v3_copy( n
, ct
->n
);
1054 float l0
= v3_dot( tri
[0], n
),
1055 l1
= v3_dot( manifold
[j
], n
);
1057 ct
->p
= (l0
-l1
)*0.5f
;
1061 ct
->type
= k_contact_type_default
;
1071 static int rb_capsule__triangle( m4x3f mtxA
, rb_capsule
*c
,
1072 v3f tri
[3], rb_ct
*buf
){
1074 v3_muladds( mtxA
[3], mtxA
[1], -c
->height
*0.5f
+c
->radius
, p0w
);
1075 v3_muladds( mtxA
[3], mtxA
[1], c
->height
*0.5f
-c
->radius
, p1w
);
1077 capsule_manifold manifold
;
1078 rb_capsule_manifold_init( &manifold
);
1081 v3_sub( tri
[1], tri
[0], v0
);
1082 v3_sub( tri
[2], tri
[0], v1
);
1083 v3_cross( v0
, v1
, n
);
1085 if( v3_length2( n
) <= 0.00001f
){
1086 #ifdef RIGIDBODY_CRY_ABOUT_EVERYTHING
1087 vg_error( "Zero area triangle!\n" );
1095 /* deep penetration recovery. for when we clip through the triangles. so its
1096 * not very 'correct' */
1098 if( ray_tri( tri
, p0w
, mtxA
[1], &dist
, 1 ) ){
1099 f32 l
= c
->height
- c
->radius
*2.0f
;
1100 if( (dist
>= 0.0f
) && (dist
< l
) ){
1102 v3_muladds( p0w
, mtxA
[1], dist
, co
);
1103 vg_line_point( co
, 0.02f
, 0xffffff00 );
1106 v3_sub( p0w
, co
, d0
);
1107 v3_sub( p1w
, co
, d1
);
1109 f32 p
= vg_minf( v3_dot( n
, d0
), v3_dot( n
, d1
) ) - c
->radius
;
1113 ct
->type
= k_contact_type_default
;
1114 v3_copy( n
, ct
->n
);
1115 v3_muladds( co
, n
, p
, ct
->co
);
1123 closest_on_triangle_1( p0w
, tri
, c0
);
1124 closest_on_triangle_1( p1w
, tri
, c1
);
1127 v3_sub( c0
, p0w
, d0
);
1128 v3_sub( c1
, p1w
, d1
);
1129 v3_sub( p1w
, p0w
, da
);
1135 /* the two balls at the ends */
1136 if( v3_dot( da
, d0
) <= 0.01f
)
1137 rb_capsule_manifold( p0w
, c0
, 0.0f
, c
->radius
, &manifold
);
1138 if( v3_dot( da
, d1
) >= -0.01f
)
1139 rb_capsule_manifold( p1w
, c1
, 1.0f
, c
->radius
, &manifold
);
1141 /* the edges to edges */
1142 for( int i
=0; i
<3; i
++ ){
1148 closest_segment_segment( p0w
, p1w
, tri
[i0
], tri
[i1
], &ta
, &tb
, ca
, cb
);
1149 rb_capsule_manifold( ca
, cb
, ta
, c
->radius
, &manifold
);
1152 int count
= rb_capsule__manifold_done( mtxA
, c
, &manifold
, buf
);
1153 for( int i
=0; i
<count
; i
++ )
1154 v3_copy( n
, buf
[i
].n
);
1159 /* mtxB is defined only for tradition; it is not used currently */
1160 static int rb_capsule__scene( m4x3f mtxA
, rb_capsule
*c
,
1161 m4x3f mtxB
, rb_scene
*s
,
1162 rb_ct
*buf
, u16 ignore
){
1166 v3_sub( mtxA
[3], (v3f
){ c
->height
, c
->height
, c
->height
}, bbx
[0] );
1167 v3_add( mtxA
[3], (v3f
){ c
->height
, c
->height
, c
->height
}, bbx
[1] );
1169 scene_context
*sc
= s
->bh_scene
->user
;
1172 bh_iter_init_box( 0, &it
, bbx
);
1174 while( bh_next( s
->bh_scene
, &it
, &idx
) ){
1175 u32
*ptri
= &sc
->arrindices
[ idx
*3 ];
1176 if( sc
->arrvertices
[ptri
[0]].flags
& ignore
) continue;
1179 for( int j
=0; j
<3; j
++ )
1180 v3_copy( sc
->arrvertices
[ptri
[j
]].co
, tri
[j
] );
1182 buf
[ count
].element_id
= ptri
[0];
1184 int contact
= rb_capsule__triangle( mtxA
, c
, tri
, &buf
[count
] );
1188 vg_warn("Exceeding capsule_vs_scene capacity. Geometry too dense!\n");
1196 static int rb_global_has_space( void ){
1197 if( rb_contact_count
+ 16 > vg_list_size(rb_contact_buffer
) )
1203 static rb_ct
*rb_global_buffer( void ){
1204 return &rb_contact_buffer
[ rb_contact_count
];
1208 * -----------------------------------------------------------------------------
1210 * -----------------------------------------------------------------------------
1213 static void rb_solver_reset(void){
1214 rb_contact_count
= 0;
1217 static rb_ct
*rb_global_ct(void){
1218 return rb_contact_buffer
+ rb_contact_count
;
1221 static void rb_prepare_contact( rb_ct
*ct
, float timestep
){
1222 ct
->bias
= -0.2f
* (timestep
*3600.0f
)
1223 * vg_minf( 0.0f
, -ct
->p
+k_penetration_slop
);
1225 v3_tangent_basis( ct
->n
, ct
->t
[0], ct
->t
[1] );
1226 ct
->norm_impulse
= 0.0f
;
1227 ct
->tangent_impulse
[0] = 0.0f
;
1228 ct
->tangent_impulse
[1] = 0.0f
;
1231 /* calculate total move. manifold should belong to ONE object only */
1232 static void rb_depenetrate( rb_ct
*manifold
, int len
, v3f dt
){
1235 for( int j
=0; j
<7; j
++ )
1237 for( int i
=0; i
<len
; i
++ )
1239 struct contact
*ct
= &manifold
[i
];
1241 float resolved_amt
= v3_dot( ct
->n
, dt
),
1242 remaining
= (ct
->p
-k_penetration_slop
) - resolved_amt
,
1243 apply
= vg_maxf( remaining
, 0.0f
) * 0.4f
;
1245 v3_muladds( dt
, ct
->n
, apply
, dt
);
1251 * Initializing things like tangent vectors
1253 static void rb_presolve_contacts( rb_ct
*buffer
, int len
){
1254 for( int i
=0; i
<len
; i
++ ){
1255 rb_ct
*ct
= &buffer
[i
];
1256 rb_prepare_contact( ct
, k_rb_delta
);
1258 v3f ra
, rb
, raCn
, rbCn
, raCt
, rbCt
;
1259 v3_sub( ct
->co
, ct
->rba
->co
, ra
);
1260 v3_sub( ct
->co
, ct
->rbb
->co
, rb
);
1261 v3_cross( ra
, ct
->n
, raCn
);
1262 v3_cross( rb
, ct
->n
, rbCn
);
1264 /* orient inverse inertia tensors */
1266 m3x3_mulv( ct
->rba
->iIw
, raCn
, raCnI
);
1267 m3x3_mulv( ct
->rbb
->iIw
, rbCn
, rbCnI
);
1269 ct
->normal_mass
= ct
->rba
->inv_mass
+ ct
->rbb
->inv_mass
;
1270 ct
->normal_mass
+= v3_dot( raCn
, raCnI
);
1271 ct
->normal_mass
+= v3_dot( rbCn
, rbCnI
);
1272 ct
->normal_mass
= 1.0f
/ct
->normal_mass
;
1274 for( int j
=0; j
<2; j
++ ){
1276 v3_cross( ct
->t
[j
], ra
, raCt
);
1277 v3_cross( ct
->t
[j
], rb
, rbCt
);
1278 m3x3_mulv( ct
->rba
->iIw
, raCt
, raCtI
);
1279 m3x3_mulv( ct
->rbb
->iIw
, rbCt
, rbCtI
);
1281 ct
->tangent_mass
[j
] = ct
->rba
->inv_mass
+ ct
->rbb
->inv_mass
;
1282 ct
->tangent_mass
[j
] += v3_dot( raCt
, raCtI
);
1283 ct
->tangent_mass
[j
] += v3_dot( rbCt
, rbCtI
);
1284 ct
->tangent_mass
[j
] = 1.0f
/ct
->tangent_mass
[j
];
1287 rb_debug_contact( ct
);
1292 * Creates relative contact velocity vector
1294 static void rb_rcv( rigidbody
*rba
, rigidbody
*rbb
, v3f ra
, v3f rb
, v3f rv
){
1296 v3_cross( rba
->w
, ra
, rva
);
1297 v3_add( rba
->v
, rva
, rva
);
1298 v3_cross( rbb
->w
, rb
, rvb
);
1299 v3_add( rbb
->v
, rvb
, rvb
);
1301 v3_sub( rva
, rvb
, rv
);
1304 static void rb_contact_restitution( rb_ct
*ct
, float cr
){
1306 v3_sub( ct
->co
, ct
->rba
->co
, ra
);
1307 v3_sub( ct
->co
, ct
->rbb
->co
, rb
);
1308 rb_rcv( ct
->rba
, ct
->rbb
, ra
, rb
, rv
);
1310 float v
= v3_dot( rv
, ct
->n
);
1313 ct
->bias
+= -cr
* v
;
1318 * Apply impulse to object
1320 static void rb_linear_impulse( rigidbody
*rb
, v3f delta
, v3f impulse
){
1322 v3_muladds( rb
->v
, impulse
, rb
->inv_mass
, rb
->v
);
1324 /* Angular velocity */
1326 v3_cross( delta
, impulse
, wa
);
1328 m3x3_mulv( rb
->iIw
, wa
, wa
);
1329 v3_add( rb
->w
, wa
, rb
->w
);
1333 * One iteration to solve the contact constraint
1335 static void rb_solve_contacts( rb_ct
*buf
, int len
){
1336 for( int i
=0; i
<len
; i
++ ){
1337 struct contact
*ct
= &buf
[i
];
1340 v3_sub( ct
->co
, ct
->rba
->co
, ra
);
1341 v3_sub( ct
->co
, ct
->rbb
->co
, rb
);
1342 rb_rcv( ct
->rba
, ct
->rbb
, ra
, rb
, rv
);
1345 for( int j
=0; j
<2; j
++ ){
1346 float f
= k_friction
* ct
->norm_impulse
,
1347 vt
= v3_dot( rv
, ct
->t
[j
] ),
1348 lambda
= ct
->tangent_mass
[j
] * -vt
;
1350 float temp
= ct
->tangent_impulse
[j
];
1351 ct
->tangent_impulse
[j
] = vg_clampf( temp
+ lambda
, -f
, f
);
1352 lambda
= ct
->tangent_impulse
[j
] - temp
;
1355 v3_muls( ct
->t
[j
], lambda
, impulse
);
1356 rb_linear_impulse( ct
->rba
, ra
, impulse
);
1358 v3_muls( ct
->t
[j
], -lambda
, impulse
);
1359 rb_linear_impulse( ct
->rbb
, rb
, impulse
);
1363 rb_rcv( ct
->rba
, ct
->rbb
, ra
, rb
, rv
);
1364 float vn
= v3_dot( rv
, ct
->n
),
1365 lambda
= ct
->normal_mass
* (-vn
+ ct
->bias
);
1367 float temp
= ct
->norm_impulse
;
1368 ct
->norm_impulse
= vg_maxf( temp
+ lambda
, 0.0f
);
1369 lambda
= ct
->norm_impulse
- temp
;
1372 v3_muls( ct
->n
, lambda
, impulse
);
1373 rb_linear_impulse( ct
->rba
, ra
, impulse
);
1375 v3_muls( ct
->n
, -lambda
, impulse
);
1376 rb_linear_impulse( ct
->rbb
, rb
, impulse
);
1381 * -----------------------------------------------------------------------------
1383 * -----------------------------------------------------------------------------
1386 static void rb_debug_position_constraints( rb_constr_pos
*buffer
, int len
){
1387 for( int i
=0; i
<len
; i
++ ){
1388 rb_constr_pos
*constr
= &buffer
[i
];
1389 rigidbody
*rba
= constr
->rba
, *rbb
= constr
->rbb
;
1392 m3x3_mulv( rba
->to_world
, constr
->lca
, wca
);
1393 m3x3_mulv( rbb
->to_world
, constr
->lcb
, wcb
);
1396 v3_add( wca
, rba
->co
, p0
);
1397 v3_add( wcb
, rbb
->co
, p1
);
1398 vg_line_point( p0
, 0.0025f
, 0xff000000 );
1399 vg_line_point( p1
, 0.0025f
, 0xffffffff );
1400 vg_line2( p0
, p1
, 0xff000000, 0xffffffff );
1404 static void rb_presolve_swingtwist_constraints( rb_constr_swingtwist
*buf
,
1408 for( int i
=0; i
<len
; i
++ ){
1409 rb_constr_swingtwist
*st
= &buf
[ i
];
1411 v3f vx
, vy
, va
, vxb
, axis
, center
;
1413 m3x3_mulv( st
->rba
->to_world
, st
->conevx
, vx
);
1414 m3x3_mulv( st
->rbb
->to_world
, st
->conevxb
, vxb
);
1415 m3x3_mulv( st
->rba
->to_world
, st
->conevy
, vy
);
1416 m3x3_mulv( st
->rbb
->to_world
, st
->coneva
, va
);
1417 m4x3_mulv( st
->rba
->to_world
, st
->view_offset
, center
);
1418 v3_cross( vy
, vx
, axis
);
1420 /* Constraint violated ? */
1421 float fx
= v3_dot( vx
, va
), /* projection world */
1422 fy
= v3_dot( vy
, va
),
1423 fn
= v3_dot( va
, axis
),
1425 rx
= st
->conevx
[3], /* elipse radii */
1428 lx
= fx
/rx
, /* projection local (fn==lz) */
1431 st
->tangent_violation
= ((lx
*lx
+ ly
*ly
) > fn
*fn
) || (fn
<= 0.0f
);
1432 if( st
->tangent_violation
){
1433 /* Calculate a good position and the axis to solve on */
1434 v2f closest
, tangent
,
1435 p
= { fx
/fabsf(fn
), fy
/fabsf(fn
) };
1437 closest_point_elipse( p
, (v2f
){rx
,ry
}, closest
);
1438 tangent
[0] = -closest
[1] / (ry
*ry
);
1439 tangent
[1] = closest
[0] / (rx
*rx
);
1440 v2_normalize( tangent
);
1443 v3_muladds( axis
, vx
, closest
[0], v0
);
1444 v3_muladds( v0
, vy
, closest
[1], v0
);
1447 v3_muls( vx
, tangent
[0], v1
);
1448 v3_muladds( v1
, vy
, tangent
[1], v1
);
1450 v3_copy( v0
, st
->tangent_target
);
1451 v3_copy( v1
, st
->tangent_axis
);
1453 /* calculate mass */
1455 m3x3_mulv( st
->rba
->iIw
, st
->tangent_axis
, aIw
);
1456 m3x3_mulv( st
->rbb
->iIw
, st
->tangent_axis
, bIw
);
1457 st
->tangent_mass
= 1.0f
/ (v3_dot( st
->tangent_axis
, aIw
) +
1458 v3_dot( st
->tangent_axis
, bIw
));
1460 float angle
= v3_dot( va
, st
->tangent_target
);
1464 v3_cross( vy
, va
, refaxis
); /* our default rotation */
1465 v3_normalize( refaxis
);
1467 float angle
= v3_dot( refaxis
, vxb
);
1468 st
->axis_violation
= fabsf(angle
) < st
->conet
;
1470 if( st
->axis_violation
){
1472 v3_cross( refaxis
, vxb
, dir_test
);
1474 if( v3_dot(dir_test
, va
) < 0.0f
)
1475 st
->axis_violation
= -st
->axis_violation
;
1477 float newang
= (float)st
->axis_violation
* acosf(st
->conet
-0.0001f
);
1480 v3_cross( va
, refaxis
, refaxis_up
);
1481 v3_muls( refaxis_up
, sinf(newang
), st
->axis_target
);
1482 v3_muladds( st
->axis_target
, refaxis
, -cosf(newang
), st
->axis_target
);
1484 /* calculate mass */
1485 v3_copy( va
, st
->axis
);
1487 m3x3_mulv( st
->rba
->iIw
, st
->axis
, aIw
);
1488 m3x3_mulv( st
->rbb
->iIw
, st
->axis
, bIw
);
1489 st
->axis_mass
= 1.0f
/ (v3_dot( st
->axis
, aIw
) +
1490 v3_dot( st
->axis
, bIw
));
1495 static void rb_debug_swingtwist_constraints( rb_constr_swingtwist
*buf
,
1499 for( int i
=0; i
<len
; i
++ ){
1500 rb_constr_swingtwist
*st
= &buf
[ i
];
1502 v3f vx
, vxb
, vy
, va
, axis
, center
;
1504 m3x3_mulv( st
->rba
->to_world
, st
->conevx
, vx
);
1505 m3x3_mulv( st
->rbb
->to_world
, st
->conevxb
, vxb
);
1506 m3x3_mulv( st
->rba
->to_world
, st
->conevy
, vy
);
1507 m3x3_mulv( st
->rbb
->to_world
, st
->coneva
, va
);
1508 m4x3_mulv( st
->rba
->to_world
, st
->view_offset
, center
);
1509 v3_cross( vy
, vx
, axis
);
1511 float rx
= st
->conevx
[3], /* elipse radii */
1515 v3_muladds( center
, va
, size
, p1
);
1516 vg_line( center
, p1
, 0xffffffff );
1517 vg_line_point( p1
, 0.00025f
, 0xffffffff );
1519 if( st
->tangent_violation
){
1520 v3_muladds( center
, st
->tangent_target
, size
, p0
);
1522 vg_line( center
, p0
, 0xff00ff00 );
1523 vg_line_point( p0
, 0.00025f
, 0xff00ff00 );
1524 vg_line( p1
, p0
, 0xff000000 );
1527 for( int x
=0; x
<32; x
++ ){
1528 float t0
= ((float)x
* (1.0f
/32.0f
)) * VG_TAUf
,
1529 t1
= (((float)x
+1.0f
) * (1.0f
/32.0f
)) * VG_TAUf
,
1536 v3_muladds( axis
, vx
, c0
*rx
, v0
);
1537 v3_muladds( v0
, vy
, s0
*ry
, v0
);
1538 v3_muladds( axis
, vx
, c1
*rx
, v1
);
1539 v3_muladds( v1
, vy
, s1
*ry
, v1
);
1544 v3_muladds( center
, v0
, size
, p0
);
1545 v3_muladds( center
, v1
, size
, p1
);
1547 u32 col0r
= fabsf(c0
) * 255.0f
,
1548 col0g
= fabsf(s0
) * 255.0f
,
1549 col1r
= fabsf(c1
) * 255.0f
,
1550 col1g
= fabsf(s1
) * 255.0f
,
1551 col
= st
->tangent_violation
? 0xff0000ff: 0xff000000,
1552 col0
= col
| (col0r
<<16) | (col0g
<< 8),
1553 col1
= col
| (col1r
<<16) | (col1g
<< 8);
1555 vg_line2( center
, p0
, VG__NONE
, col0
);
1556 vg_line2( p0
, p1
, col0
, col1
);
1560 v3_muladds( center
, va
, size
, p0
);
1561 v3_muladds( p0
, vxb
, size
, p1
);
1563 vg_line( p0
, p1
, 0xff0000ff );
1565 if( st
->axis_violation
){
1566 v3_muladds( p0
, st
->axis_target
, size
*1.25f
, p1
);
1567 vg_line( p0
, p1
, 0xffffff00 );
1568 vg_line_point( p1
, 0.0025f
, 0xffffff80 );
1572 v3_cross( vy
, va
, refaxis
); /* our default rotation */
1573 v3_normalize( refaxis
);
1575 v3_cross( va
, refaxis
, refaxis_up
);
1576 float newang
= acosf(st
->conet
-0.0001f
);
1578 v3_muladds( p0
, refaxis_up
, sinf(newang
)*size
, p1
);
1579 v3_muladds( p1
, refaxis
, -cosf(newang
)*size
, p1
);
1580 vg_line( p0
, p1
, 0xff000000 );
1582 v3_muladds( p0
, refaxis_up
, sinf(-newang
)*size
, p1
);
1583 v3_muladds( p1
, refaxis
, -cosf(-newang
)*size
, p1
);
1584 vg_line( p0
, p1
, 0xff404040 );
1589 * Solve a list of positional constraints
1591 static void rb_solve_position_constraints( rb_constr_pos
*buf
, int len
){
1592 for( int i
=0; i
<len
; i
++ ){
1593 rb_constr_pos
*constr
= &buf
[i
];
1594 rigidbody
*rba
= constr
->rba
, *rbb
= constr
->rbb
;
1597 m3x3_mulv( rba
->to_world
, constr
->lca
, wa
);
1598 m3x3_mulv( rbb
->to_world
, constr
->lcb
, wb
);
1600 m3x3f ssra
, ssrat
, ssrb
, ssrbt
;
1602 m3x3_skew_symetric( ssrat
, wa
);
1603 m3x3_skew_symetric( ssrbt
, wb
);
1604 m3x3_transpose( ssrat
, ssra
);
1605 m3x3_transpose( ssrbt
, ssrb
);
1607 v3f b
, b_wa
, b_wb
, b_a
, b_b
;
1608 m3x3_mulv( ssra
, rba
->w
, b_wa
);
1609 m3x3_mulv( ssrb
, rbb
->w
, b_wb
);
1610 v3_add( rba
->v
, b_wa
, b
);
1611 v3_sub( b
, rbb
->v
, b
);
1612 v3_sub( b
, b_wb
, b
);
1613 v3_muls( b
, -1.0f
, b
);
1616 m3x3_diagonal( invMa
, rba
->inv_mass
);
1617 m3x3_diagonal( invMb
, rbb
->inv_mass
);
1620 m3x3_mul( ssra
, rba
->iIw
, ia
);
1621 m3x3_mul( ia
, ssrat
, ia
);
1622 m3x3_mul( ssrb
, rbb
->iIw
, ib
);
1623 m3x3_mul( ib
, ssrbt
, ib
);
1626 m3x3_add( invMa
, ia
, cma
);
1627 m3x3_add( invMb
, ib
, cmb
);
1630 m3x3_add( cma
, cmb
, A
);
1632 /* Solve Ax = b ( A^-1*b = x ) */
1635 m3x3_inv( A
, invA
);
1636 m3x3_mulv( invA
, b
, impulse
);
1638 v3f delta_va
, delta_wa
, delta_vb
, delta_wb
;
1640 m3x3_mul( rba
->iIw
, ssrat
, iwa
);
1641 m3x3_mul( rbb
->iIw
, ssrbt
, iwb
);
1643 m3x3_mulv( invMa
, impulse
, delta_va
);
1644 m3x3_mulv( invMb
, impulse
, delta_vb
);
1645 m3x3_mulv( iwa
, impulse
, delta_wa
);
1646 m3x3_mulv( iwb
, impulse
, delta_wb
);
1648 v3_add( rba
->v
, delta_va
, rba
->v
);
1649 v3_add( rba
->w
, delta_wa
, rba
->w
);
1650 v3_sub( rbb
->v
, delta_vb
, rbb
->v
);
1651 v3_sub( rbb
->w
, delta_wb
, rbb
->w
);
1655 static void rb_solve_swingtwist_constraints( rb_constr_swingtwist
*buf
,
1659 for( int i
=0; i
<len
; i
++ ){
1660 rb_constr_swingtwist
*st
= &buf
[ i
];
1662 if( !st
->axis_violation
)
1665 float rv
= v3_dot( st
->axis
, st
->rbb
->w
) -
1666 v3_dot( st
->axis
, st
->rba
->w
);
1668 if( rv
* (float)st
->axis_violation
> 0.0f
)
1671 v3f impulse
, wa
, wb
;
1672 v3_muls( st
->axis
, rv
*st
->axis_mass
, impulse
);
1673 m3x3_mulv( st
->rba
->iIw
, impulse
, wa
);
1674 v3_add( st
->rba
->w
, wa
, st
->rba
->w
);
1676 v3_muls( impulse
, -1.0f
, impulse
);
1677 m3x3_mulv( st
->rbb
->iIw
, impulse
, wb
);
1678 v3_add( st
->rbb
->w
, wb
, st
->rbb
->w
);
1680 float rv2
= v3_dot( st
->axis
, st
->rbb
->w
) -
1681 v3_dot( st
->axis
, st
->rba
->w
);
1684 for( int i
=0; i
<len
; i
++ ){
1685 rb_constr_swingtwist
*st
= &buf
[ i
];
1687 if( !st
->tangent_violation
)
1690 float rv
= v3_dot( st
->tangent_axis
, st
->rbb
->w
) -
1691 v3_dot( st
->tangent_axis
, st
->rba
->w
);
1696 v3f impulse
, wa
, wb
;
1697 v3_muls( st
->tangent_axis
, rv
*st
->tangent_mass
, impulse
);
1698 m3x3_mulv( st
->rba
->iIw
, impulse
, wa
);
1699 v3_add( st
->rba
->w
, wa
, st
->rba
->w
);
1701 v3_muls( impulse
, -1.0f
, impulse
);
1702 m3x3_mulv( st
->rbb
->iIw
, impulse
, wb
);
1703 v3_add( st
->rbb
->w
, wb
, st
->rbb
->w
);
1705 float rv2
= v3_dot( st
->tangent_axis
, st
->rbb
->w
) -
1706 v3_dot( st
->tangent_axis
, st
->rba
->w
);
1710 static void rb_solve_constr_angle( rigidbody
*rba
, rigidbody
*rbb
,
1712 m3x3f ssra
, ssrb
, ssrat
, ssrbt
;
1715 m3x3_skew_symetric( ssrat
, ra
);
1716 m3x3_skew_symetric( ssrbt
, rb
);
1717 m3x3_transpose( ssrat
, ssra
);
1718 m3x3_transpose( ssrbt
, ssrb
);
1720 m3x3_mul( ssra
, rba
->iIw
, cma
);
1721 m3x3_mul( cma
, ssrat
, cma
);
1722 m3x3_mul( ssrb
, rbb
->iIw
, cmb
);
1723 m3x3_mul( cmb
, ssrbt
, cmb
);
1726 m3x3_add( cma
, cmb
, A
);
1727 m3x3_inv( A
, invA
);
1730 m3x3_mulv( ssra
, rba
->w
, b_wa
);
1731 m3x3_mulv( ssrb
, rbb
->w
, b_wb
);
1732 v3_add( b_wa
, b_wb
, b
);
1736 m3x3_mulv( invA
, b
, impulse
);
1738 v3f delta_wa
, delta_wb
;
1740 m3x3_mul( rba
->iIw
, ssrat
, iwa
);
1741 m3x3_mul( rbb
->iIw
, ssrbt
, iwb
);
1742 m3x3_mulv( iwa
, impulse
, delta_wa
);
1743 m3x3_mulv( iwb
, impulse
, delta_wb
);
1744 v3_add( rba
->w
, delta_wa
, rba
->w
);
1745 v3_sub( rbb
->w
, delta_wb
, rbb
->w
);
1749 * Correct position constraint drift errors
1750 * [ 0.0 <= amt <= 1.0 ]: the correction amount
1752 static void rb_correct_position_constraints( rb_constr_pos
*buf
, int len
,
1754 for( int i
=0; i
<len
; i
++ ){
1755 rb_constr_pos
*constr
= &buf
[i
];
1756 rigidbody
*rba
= constr
->rba
, *rbb
= constr
->rbb
;
1759 m3x3_mulv( rba
->to_world
, constr
->lca
, p0
);
1760 m3x3_mulv( rbb
->to_world
, constr
->lcb
, p1
);
1761 v3_add( rba
->co
, p0
, p0
);
1762 v3_add( rbb
->co
, p1
, p1
);
1763 v3_sub( p1
, p0
, d
);
1765 v3_muladds( rbb
->co
, d
, -1.0f
* amt
, rbb
->co
);
1766 rb_update_transform( rbb
);
1770 static void rb_correct_swingtwist_constraints( rb_constr_swingtwist
*buf
,
1771 int len
, float amt
){
1772 for( int i
=0; i
<len
; i
++ ){
1773 rb_constr_swingtwist
*st
= &buf
[i
];
1775 if( !st
->tangent_violation
)
1779 m3x3_mulv( st
->rbb
->to_world
, st
->coneva
, va
);
1781 float angle
= v3_dot( va
, st
->tangent_target
);
1783 if( fabsf(angle
) < 0.9999f
){
1785 v3_cross( va
, st
->tangent_target
, axis
);
1788 q_axis_angle( correction
, axis
, acosf(angle
) * amt
);
1789 q_mul( correction
, st
->rbb
->q
, st
->rbb
->q
);
1790 rb_update_transform( st
->rbb
);
1794 for( int i
=0; i
<len
; i
++ ){
1795 rb_constr_swingtwist
*st
= &buf
[i
];
1797 if( !st
->axis_violation
)
1801 m3x3_mulv( st
->rbb
->to_world
, st
->conevxb
, vxb
);
1803 float angle
= v3_dot( vxb
, st
->axis_target
);
1805 if( fabsf(angle
) < 0.9999f
){
1807 v3_cross( vxb
, st
->axis_target
, axis
);
1810 q_axis_angle( correction
, axis
, acosf(angle
) * amt
);
1811 q_mul( correction
, st
->rbb
->q
, st
->rbb
->q
);
1812 rb_update_transform( st
->rbb
);
1817 static void rb_correct_contact_constraints( rb_ct
*buf
, int len
, float amt
){
1818 for( int i
=0; i
<len
; i
++ ){
1819 rb_ct
*ct
= &buf
[i
];
1820 rigidbody
*rba
= ct
->rba
,
1823 f32 mass_total
= 1.0f
/ (rba
->inv_mass
+ rbb
->inv_mass
),
1824 d
= ct
->p
*mass_total
*amt
;
1826 v3_muladds( rba
->co
, ct
->n
, -d
* rba
->inv_mass
, rba
->co
);
1827 v3_muladds( rbb
->co
, ct
->n
, d
* rbb
->inv_mass
, rbb
->co
);
1836 static void rb_effect_simple_bouyency( rigidbody
*ra
, v4f plane
,
1837 float amt
, float drag
){
1839 float depth
= v3_dot( plane
, ra
->co
) - plane
[3],
1840 lambda
= vg_clampf( -depth
, 0.0f
, 1.0f
) * amt
;
1842 v3_muladds( ra
->v
, plane
, lambda
* k_rb_delta
, ra
->v
);
1845 v3_muls( ra
->v
, 1.0f
-(drag
*k_rb_delta
), ra
->v
);
1848 /* apply a spring&dampener force to match ra(worldspace) on rigidbody, to
1851 static void rb_effect_spring_target_vector( rigidbody
*rba
, v3f ra
, v3f rt
,
1852 float spring
, float dampening
,
1854 float d
= v3_dot( rt
, ra
);
1855 float a
= acosf( vg_clampf( d
, -1.0f
, 1.0f
) );
1858 v3_cross( rt
, ra
, axis
);
1860 float Fs
= -a
* spring
,
1861 Fd
= -v3_dot( rba
->w
, axis
) * dampening
;
1863 v3_muladds( rba
->w
, axis
, (Fs
+Fd
) * timestep
, rba
->w
);
1866 #endif /* RIGIDBODY_H */