2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
6 * Resources: Box2D - Erin Catto
15 VG_STATIC
void rb_tangent_basis( v3f n
, v3f tx
, v3f ty
);
16 VG_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.05f
, /* scale velocity 1/(1+x) */
32 k_damp_angular
= 0.1f
, /* scale angular 1/(1+x) */
34 k_joint_bias
= 0.08f
, /* positional joints */
35 k_joint_correction
= 0.01f
,
36 k_penetration_slop
= 0.01f
,
37 k_inertia_scale
= 4.0f
;
40 * -----------------------------------------------------------------------------
41 * structure definitions
42 * -----------------------------------------------------------------------------
45 typedef struct rigidbody rigidbody
;
46 typedef struct contact rb_ct
;
56 k_rb_shape_sphere
= 1,
57 k_rb_shape_capsule
= 2,
84 v3f right
, up
, forward
;
91 /* inertia model and inverse world tensor */
95 m4x3f to_world
, to_local
;
98 VG_STATIC
struct contact
100 rigidbody
*rba
, *rbb
;
103 float p
, bias
, norm_impulse
, tangent_impulse
[2],
104 normal_mass
, tangent_mass
[2];
108 enum contact_type type
;
110 rb_contact_buffer
[256];
111 VG_STATIC
int rb_contact_count
= 0;
114 * -----------------------------------------------------------------------------
116 * -----------------------------------------------------------------------------
119 VG_STATIC
float sphere_volume( float radius
)
121 float r3
= radius
*radius
*radius
;
122 return (4.0f
/3.0f
) * VG_PIf
* r3
;
125 VG_STATIC
void rb_tangent_basis( v3f n
, v3f tx
, v3f ty
)
127 /* Compute tangent basis (box2d) */
128 if( fabsf( n
[0] ) >= 0.57735027f
)
142 v3_cross( n
, tx
, ty
);
146 * -----------------------------------------------------------------------------
148 * -----------------------------------------------------------------------------
151 VG_STATIC
void rb_debug_contact( rb_ct
*ct
)
153 if( ct
->type
!= k_contact_type_disabled
)
156 v3_muladds( ct
->co
, ct
->n
, 0.1f
, p1
);
157 vg_line_pt3( ct
->co
, 0.025f
, 0xff0000ff );
158 vg_line( ct
->co
, p1
, 0xffffffff );
162 VG_STATIC
void debug_sphere( m4x3f m
, float radius
, u32 colour
)
164 v3f ly
= { 0.0f
, 0.0f
, radius
},
165 lx
= { 0.0f
, radius
, 0.0f
},
166 lz
= { 0.0f
, 0.0f
, radius
};
168 for( int i
=0; i
<16; i
++ )
170 float t
= ((float)(i
+1) * (1.0f
/16.0f
)) * VG_PIf
* 2.0f
,
174 v3f py
= { s
*radius
, 0.0f
, c
*radius
},
175 px
= { s
*radius
, c
*radius
, 0.0f
},
176 pz
= { 0.0f
, s
*radius
, c
*radius
};
178 v3f p0
, p1
, p2
, p3
, p4
, p5
;
179 m4x3_mulv( m
, py
, p0
);
180 m4x3_mulv( m
, ly
, p1
);
181 m4x3_mulv( m
, px
, p2
);
182 m4x3_mulv( m
, lx
, p3
);
183 m4x3_mulv( m
, pz
, p4
);
184 m4x3_mulv( m
, lz
, p5
);
186 vg_line( p0
, p1
, colour
== 0x00? 0xff00ff00: colour
);
187 vg_line( p2
, p3
, colour
== 0x00? 0xff0000ff: colour
);
188 vg_line( p4
, p5
, colour
== 0x00? 0xffff0000: colour
);
196 VG_STATIC
void debug_capsule( m4x3f m
, float radius
, float h
, u32 colour
)
198 v3f ly
= { 0.0f
, 0.0f
, radius
},
199 lx
= { 0.0f
, radius
, 0.0f
},
200 lz
= { 0.0f
, 0.0f
, radius
};
202 float s0
= sinf(0.0f
)*radius
,
203 c0
= cosf(0.0f
)*radius
;
205 v3f p0
, p1
, up
, right
, forward
;
206 m3x3_mulv( m
, (v3f
){0.0f
,1.0f
,0.0f
}, up
);
207 m3x3_mulv( m
, (v3f
){1.0f
,0.0f
,0.0f
}, right
);
208 m3x3_mulv( m
, (v3f
){0.0f
,0.0f
,-1.0f
}, forward
);
209 v3_muladds( m
[3], up
, -h
*0.5f
+radius
, p0
);
210 v3_muladds( m
[3], up
, h
*0.5f
-radius
, p1
);
213 v3_muladds( p0
, right
, radius
, a0
);
214 v3_muladds( p1
, right
, radius
, a1
);
215 v3_muladds( p0
, forward
, radius
, b0
);
216 v3_muladds( p1
, forward
, radius
, b1
);
217 vg_line( a0
, a1
, colour
);
218 vg_line( b0
, b1
, colour
);
220 v3_muladds( p0
, right
, -radius
, a0
);
221 v3_muladds( p1
, right
, -radius
, a1
);
222 v3_muladds( p0
, forward
, -radius
, b0
);
223 v3_muladds( p1
, forward
, -radius
, b1
);
224 vg_line( a0
, a1
, colour
);
225 vg_line( b0
, b1
, colour
);
227 for( int i
=0; i
<16; i
++ )
229 float t
= ((float)(i
+1) * (1.0f
/16.0f
)) * VG_PIf
* 2.0f
,
233 v3f e0
= { s0
, 0.0f
, c0
},
234 e1
= { s1
, 0.0f
, c1
},
235 e2
= { s0
, c0
, 0.0f
},
236 e3
= { s1
, c1
, 0.0f
},
237 e4
= { 0.0f
, c0
, s0
},
238 e5
= { 0.0f
, c1
, s1
};
240 m3x3_mulv( m
, e0
, e0
);
241 m3x3_mulv( m
, e1
, e1
);
242 m3x3_mulv( m
, e2
, e2
);
243 m3x3_mulv( m
, e3
, e3
);
244 m3x3_mulv( m
, e4
, e4
);
245 m3x3_mulv( m
, e5
, e5
);
247 v3_add( p0
, e0
, a0
);
248 v3_add( p0
, e1
, a1
);
249 v3_add( p1
, e0
, b0
);
250 v3_add( p1
, e1
, b1
);
252 vg_line( a0
, a1
, colour
);
253 vg_line( b0
, b1
, colour
);
257 v3_add( p0
, e2
, a0
);
258 v3_add( p0
, e3
, a1
);
259 v3_add( p0
, e4
, b0
);
260 v3_add( p0
, e5
, b1
);
264 v3_add( p1
, e2
, a0
);
265 v3_add( p1
, e3
, a1
);
266 v3_add( p1
, e4
, b0
);
267 v3_add( p1
, e5
, b1
);
270 vg_line( a0
, a1
, colour
);
271 vg_line( b0
, b1
, colour
);
278 VG_STATIC
void rb_debug( rigidbody
*rb
, u32 colour
)
280 if( rb
->type
== k_rb_shape_box
)
283 vg_line_boxf_transformed( rb
->to_world
, rb
->bbx
, colour
);
285 else if( rb
->type
== k_rb_shape_sphere
)
287 debug_sphere( rb
->to_world
, rb
->inf
.sphere
.radius
, colour
);
289 else if( rb
->type
== k_rb_shape_capsule
)
292 float h
= rb
->inf
.capsule
.height
,
293 r
= rb
->inf
.capsule
.radius
;
295 debug_capsule( rb
->to_world
, r
, h
, colour
);
297 else if( rb
->type
== k_rb_shape_scene
)
299 vg_line_boxf( rb
->bbx
, colour
);
304 * -----------------------------------------------------------------------------
306 * -----------------------------------------------------------------------------
310 * Update world space bounding box based on local one
312 VG_STATIC
void rb_update_bounds( rigidbody
*rb
)
314 box_copy( rb
->bbx
, rb
->bbx_world
);
315 m4x3_transform_aabb( rb
->to_world
, rb
->bbx_world
);
319 * Commit transform to rigidbody. Updates matrices
321 VG_STATIC
void rb_update_transform( rigidbody
*rb
)
323 q_normalize( rb
->q
);
324 q_m3x3( rb
->q
, rb
->to_world
);
325 v3_copy( rb
->co
, rb
->to_world
[3] );
327 m4x3_invert_affine( rb
->to_world
, rb
->to_local
);
329 m3x3_mulv( rb
->to_world
, (v3f
){1.0f
,0.0f
, 0.0f
}, rb
->right
);
330 m3x3_mulv( rb
->to_world
, (v3f
){0.0f
,1.0f
, 0.0f
}, rb
->up
);
331 m3x3_mulv( rb
->to_world
, (v3f
){0.0f
,0.0f
,-1.0f
}, rb
->forward
);
333 m3x3_mul( rb
->iI
, rb
->to_local
, rb
->iIw
);
334 m3x3_mul( rb
->to_world
, rb
->iIw
, rb
->iIw
);
336 rb_update_bounds( rb
);
340 * Extrapolate rigidbody into a transform based on vg accumulator.
341 * Useful for rendering
343 VG_STATIC
void rb_extrapolate_transform( rigidbody
*rb
, m4x3f transform
)
345 float substep
= vg_clampf( vg
.accumulator
/ k_rb_delta
, 0.0f
, 1.0f
);
350 v3_muladds( rb
->co
, rb
->v
, k_rb_delta
*substep
, co
);
352 if( v3_length2( rb
->w
) > 0.0f
)
356 v3_copy( rb
->w
, axis
);
358 float mag
= v3_length( axis
);
359 v3_divs( axis
, mag
, axis
);
360 q_axis_angle( rotation
, axis
, mag
*k_rb_delta
*substep
);
361 q_mul( rotation
, rb
->q
, q
);
369 q_m3x3( q
, transform
);
370 v3_copy( co
, transform
[3] );
374 * Initialize rigidbody and calculate masses, inertia
376 VG_STATIC
void rb_init( rigidbody
*rb
)
380 if( rb
->type
== k_rb_shape_box
)
383 v3_sub( rb
->bbx
[1], rb
->bbx
[0], dims
);
384 volume
= dims
[0]*dims
[1]*dims
[2];
386 else if( rb
->type
== k_rb_shape_sphere
)
388 volume
= sphere_volume( rb
->inf
.sphere
.radius
);
389 v3_fill( rb
->bbx
[0], -rb
->inf
.sphere
.radius
);
390 v3_fill( rb
->bbx
[1], rb
->inf
.sphere
.radius
);
392 else if( rb
->type
== k_rb_shape_capsule
)
394 float r
= rb
->inf
.capsule
.radius
,
395 h
= rb
->inf
.capsule
.height
;
396 volume
= sphere_volume( r
) + VG_PIf
* r
*r
* (h
- r
*2.0f
);
398 v3_fill( rb
->bbx
[0], -rb
->inf
.sphere
.radius
);
399 v3_fill( rb
->bbx
[1], rb
->inf
.sphere
.radius
);
403 else if( rb
->type
== k_rb_shape_scene
)
406 box_copy( rb
->inf
.scene
.bh_scene
->nodes
[0].bbx
, rb
->bbx
);
417 float mass
= 2.0f
*volume
;
418 rb
->inv_mass
= 1.0f
/mass
;
421 v3_sub( rb
->bbx
[1], rb
->bbx
[0], extent
);
422 v3_muls( extent
, 0.5f
, extent
);
424 /* local intertia tensor */
425 float scale
= k_inertia_scale
;
426 float ex2
= scale
*extent
[0]*extent
[0],
427 ey2
= scale
*extent
[1]*extent
[1],
428 ez2
= scale
*extent
[2]*extent
[2];
430 rb
->I
[0] = ((1.0f
/12.0f
) * mass
* (ey2
+ez2
));
431 rb
->I
[1] = ((1.0f
/12.0f
) * mass
* (ex2
+ez2
));
432 rb
->I
[2] = ((1.0f
/12.0f
) * mass
* (ex2
+ey2
));
434 m3x3_identity( rb
->iI
);
435 rb
->iI
[0][0] = rb
->I
[0];
436 rb
->iI
[1][1] = rb
->I
[1];
437 rb
->iI
[2][2] = rb
->I
[2];
438 m3x3_inv( rb
->iI
, rb
->iI
);
444 rb_update_transform( rb
);
447 VG_STATIC
void rb_iter( rigidbody
*rb
)
449 v3f gravity
= { 0.0f
, -9.8f
, 0.0f
};
450 v3_muladds( rb
->v
, gravity
, k_rb_delta
, rb
->v
);
452 /* intergrate velocity */
453 v3_muladds( rb
->co
, rb
->v
, k_rb_delta
, rb
->co
);
454 v3_lerp( rb
->w
, (v3f
){0.0f
,0.0f
,0.0f
}, 0.0025f
, rb
->w
);
456 /* inegrate inertia */
457 if( v3_length2( rb
->w
) > 0.0f
)
461 v3_copy( rb
->w
, axis
);
463 float mag
= v3_length( axis
);
464 v3_divs( axis
, mag
, axis
);
465 q_axis_angle( rotation
, axis
, mag
*k_rb_delta
);
466 q_mul( rotation
, rb
->q
, rb
->q
);
470 v3_muls( rb
->v
, 1.0f
/(1.0f
+k_rb_delta
*k_damp_linear
), rb
->v
);
471 v3_muls( rb
->w
, 1.0f
/(1.0f
+k_rb_delta
*k_damp_angular
), rb
->w
);
476 * -----------------------------------------------------------------------------
477 * Boolean shape overlap functions
478 * -----------------------------------------------------------------------------
482 * Project AABB, and triangle interval onto axis to check if they overlap
484 VG_STATIC
int rb_box_triangle_interval( v3f extent
, v3f axis
, v3f tri
[3] )
488 r
= extent
[0] * fabsf(axis
[0]) +
489 extent
[1] * fabsf(axis
[1]) +
490 extent
[2] * fabsf(axis
[2]),
492 p0
= v3_dot( axis
, tri
[0] ),
493 p1
= v3_dot( axis
, tri
[1] ),
494 p2
= v3_dot( axis
, tri
[2] ),
496 e
= vg_maxf(-vg_maxf(p0
,vg_maxf(p1
,p2
)), vg_minf(p0
,vg_minf(p1
,p2
)));
498 if( e
> r
) return 0;
503 * Seperating axis test box vs triangle
505 VG_STATIC
int rb_box_triangle_sat( rigidbody
*rba
, v3f tri_src
[3] )
510 v3_sub( rba
->bbx
[1], rba
->bbx
[0], extent
);
511 v3_muls( extent
, 0.5f
, extent
);
512 v3_add( rba
->bbx
[0], extent
, c
);
514 for( int i
=0; i
<3; i
++ )
516 m4x3_mulv( rba
->to_local
, tri_src
[i
], tri
[i
] );
517 v3_sub( tri
[i
], c
, tri
[i
] );
521 if(!rb_box_triangle_interval( extent
, (v3f
){1.0f
,0.0f
,0.0f
}, tri
)) return 0;
522 if(!rb_box_triangle_interval( extent
, (v3f
){0.0f
,1.0f
,0.0f
}, tri
)) return 0;
523 if(!rb_box_triangle_interval( extent
, (v3f
){0.0f
,0.0f
,1.0f
}, tri
)) return 0;
525 v3f v0
,v1
,v2
,n
, e0
,e1
,e2
;
526 v3_sub( tri
[1], tri
[0], v0
);
527 v3_sub( tri
[2], tri
[0], v1
);
528 v3_sub( tri
[2], tri
[1], v2
);
532 v3_cross( v0
, v1
, n
);
533 v3_cross( v0
, n
, e0
);
534 v3_cross( n
, v1
, e1
);
535 v3_cross( v2
, n
, e2
);
538 if(!rb_box_triangle_interval( extent
, n
, tri
)) return 0;
541 v3_cross( e0
, (v3f
){1.0f
,0.0f
,0.0f
}, axis
[0] );
542 v3_cross( e0
, (v3f
){0.0f
,1.0f
,0.0f
}, axis
[1] );
543 v3_cross( e0
, (v3f
){0.0f
,0.0f
,1.0f
}, axis
[2] );
544 v3_cross( e1
, (v3f
){1.0f
,0.0f
,0.0f
}, axis
[3] );
545 v3_cross( e1
, (v3f
){0.0f
,1.0f
,0.0f
}, axis
[4] );
546 v3_cross( e1
, (v3f
){0.0f
,0.0f
,1.0f
}, axis
[5] );
547 v3_cross( e2
, (v3f
){1.0f
,0.0f
,0.0f
}, axis
[6] );
548 v3_cross( e2
, (v3f
){0.0f
,1.0f
,0.0f
}, axis
[7] );
549 v3_cross( e2
, (v3f
){0.0f
,0.0f
,1.0f
}, axis
[8] );
551 for( int i
=0; i
<9; i
++ )
552 if(!rb_box_triangle_interval( extent
, axis
[i
], tri
)) return 0;
558 * -----------------------------------------------------------------------------
560 * -----------------------------------------------------------------------------
563 VG_STATIC
int rb_manifold_apply_filtered( rb_ct
*man
, int len
)
567 for( int i
=0; i
<len
; i
++ )
571 if( ct
->type
== k_contact_type_disabled
)
580 VG_STATIC
void rb_manifold_filter_joint_edges( rb_ct
*man
, int len
, float r
)
582 for( int i
=0; i
<len
-1; i
++ )
585 if( ci
->type
!= k_contact_type_edge
)
588 for( int j
=i
+1; j
<len
; j
++ )
591 if( cj
->type
!= k_contact_type_edge
)
594 if( v3_dist2( ci
->co
, cj
->co
) < r
*r
)
596 cj
->type
= k_contact_type_disabled
;
597 ci
->p
= (ci
->p
+ cj
->p
) * 0.5f
;
599 v3_add( ci
->co
, cj
->co
, ci
->co
);
600 v3_muls( ci
->co
, 0.5f
, ci
->co
);
603 v3_sub( ci
->rba
->co
, ci
->co
, delta
);
605 float c0
= v3_dot( ci
->n
, delta
),
606 c1
= v3_dot( cj
->n
, delta
);
608 if( c0
< 0.0f
|| c1
< 0.0f
)
611 ci
->type
= k_contact_type_disabled
;
616 v3_muls( ci
->n
, c0
, n
);
617 v3_muladds( n
, cj
->n
, c1
, n
);
627 * Resolve overlapping pairs
629 VG_STATIC
void rb_manifold_filter_pairs( rb_ct
*man
, int len
, float r
)
631 for( int i
=0; i
<len
-1; i
++ )
636 if( ci
->type
== k_contact_type_disabled
) continue;
638 for( int j
=i
+1; j
<len
; j
++ )
642 if( cj
->type
== k_contact_type_disabled
) continue;
644 if( v3_dist2( ci
->co
, cj
->co
) < r
*r
)
646 cj
->type
= k_contact_type_disabled
;
647 v3_add( cj
->n
, ci
->n
, ci
->n
);
655 float n
= 1.0f
/((float)similar
+1.0f
);
656 v3_muls( ci
->n
, n
, ci
->n
);
659 if( v3_length2(ci
->n
) < 0.1f
*0.1f
)
660 ci
->type
= k_contact_type_disabled
;
662 v3_normalize( ci
->n
);
668 * Remove contacts that are facing away from A
670 VG_STATIC
void rb_manifold_filter_backface( rb_ct
*man
, int len
)
672 for( int i
=0; i
<len
; i
++ )
675 if( ct
->type
== k_contact_type_disabled
)
679 v3_sub( ct
->co
, ct
->rba
->co
, delta
);
681 if( v3_dot( delta
, ct
->n
) > -0.001f
)
682 ct
->type
= k_contact_type_disabled
;
687 * Filter out duplicate coplanar results. Good for spheres.
689 VG_STATIC
void rb_manifold_filter_coplanar( rb_ct
*man
, int len
, float w
)
691 for( int i
=0; i
<len
; i
++ )
694 if( ci
->type
== k_contact_type_disabled
||
695 ci
->type
== k_contact_type_edge
)
698 float d1
= v3_dot( ci
->co
, ci
->n
);
700 for( int j
=0; j
<len
; j
++ )
706 if( cj
->type
== k_contact_type_disabled
)
709 float d2
= v3_dot( cj
->co
, ci
->n
),
712 if( fabsf( d
) <= w
)
714 cj
->type
= k_contact_type_disabled
;
721 * -----------------------------------------------------------------------------
723 * -----------------------------------------------------------------------------
729 * These do not automatically allocate contacts, an appropriately sized
730 * buffer must be supplied. The function returns the size of the manifold
731 * which was generated.
733 * The values set on the contacts are: n, co, p, rba, rbb
737 * By collecting the minimum(time) and maximum(time) pairs of points, we
738 * build a reduced and stable exact manifold.
741 * rx: minimum distance of these points
742 * dx: the delta between the two points
744 * pairs will only ammend these if they are creating a collision
746 typedef struct capsule_manifold capsule_manifold
;
747 struct capsule_manifold
755 * Expand a line manifold with a new pair. t value is the time along segment
756 * on the oriented object which created this pair.
758 VG_STATIC
void rb_capsule_manifold( v3f pa
, v3f pb
, float t
, float r
,
759 capsule_manifold
*manifold
)
762 v3_sub( pa
, pb
, delta
);
764 if( v3_length2(delta
) < r
*r
)
766 if( t
< manifold
->t0
)
768 v3_copy( delta
, manifold
->d0
);
773 if( t
> manifold
->t1
)
775 v3_copy( delta
, manifold
->d1
);
782 VG_STATIC
void rb_capsule_manifold_init( capsule_manifold
*manifold
)
784 manifold
->t0
= INFINITY
;
785 manifold
->t1
= -INFINITY
;
788 VG_STATIC
int rb_capsule_manifold_done( rigidbody
*rba
, rigidbody
*rbb
,
789 capsule_manifold
*manifold
, rb_ct
*buf
)
791 float h
= rba
->inf
.capsule
.height
,
792 ra
= rba
->inf
.capsule
.radius
;
795 v3_muladds( rba
->co
, rba
->up
, -h
*0.5f
+ra
, p0
);
796 v3_muladds( rba
->co
, rba
->up
, h
*0.5f
-ra
, p1
);
799 if( manifold
->t0
<= 1.0f
)
804 v3_muls( p0
, 1.0f
-manifold
->t0
, pa
);
805 v3_muladds( pa
, p1
, manifold
->t0
, pa
);
807 float d
= v3_length( manifold
->d0
);
808 v3_muls( manifold
->d0
, 1.0f
/d
, ct
->n
);
809 v3_muladds( pa
, ct
->n
, -ra
, ct
->co
);
811 ct
->p
= manifold
->r0
- d
;
814 ct
->type
= k_contact_type_default
;
819 if( (manifold
->t1
>= 0.0f
) && (manifold
->t0
!= manifold
->t1
) )
821 rb_ct
*ct
= buf
+count
;
824 v3_muls( p0
, 1.0f
-manifold
->t1
, pa
);
825 v3_muladds( pa
, p1
, manifold
->t1
, pa
);
827 float d
= v3_length( manifold
->d1
);
828 v3_muls( manifold
->d1
, 1.0f
/d
, ct
->n
);
829 v3_muladds( pa
, ct
->n
, -ra
, ct
->co
);
831 ct
->p
= manifold
->r1
- d
;
834 ct
->type
= k_contact_type_default
;
844 vg_line( buf
[0].co
, buf
[1].co
, 0xff0000ff );
849 VG_STATIC
int rb_capsule_sphere( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
851 float h
= rba
->inf
.capsule
.height
,
852 ra
= rba
->inf
.capsule
.radius
,
853 rb
= rbb
->inf
.sphere
.radius
;
856 v3_muladds( rba
->co
, rba
->up
, -h
*0.5f
+ra
, p0
);
857 v3_muladds( rba
->co
, rba
->up
, h
*0.5f
-ra
, p1
);
860 closest_point_segment( p0
, p1
, rbb
->co
, c
);
861 v3_sub( c
, rbb
->co
, delta
);
863 float d2
= v3_length2(delta
),
871 v3_muls( delta
, 1.0f
/d
, ct
->n
);
875 v3_muladds( c
, ct
->n
, -ra
, p0
);
876 v3_muladds( rbb
->co
, ct
->n
, rb
, p1
);
877 v3_add( p0
, p1
, ct
->co
);
878 v3_muls( ct
->co
, 0.5f
, ct
->co
);
882 ct
->type
= k_contact_type_default
;
890 VG_STATIC
int rb_capsule_capsule( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
892 float ha
= rba
->inf
.capsule
.height
,
893 hb
= rbb
->inf
.capsule
.height
,
894 ra
= rba
->inf
.capsule
.radius
,
895 rb
= rbb
->inf
.capsule
.radius
,
899 v3_muladds( rba
->co
, rba
->up
, -ha
*0.5f
+ra
, p0
);
900 v3_muladds( rba
->co
, rba
->up
, ha
*0.5f
-ra
, p1
);
901 v3_muladds( rbb
->co
, rbb
->up
, -hb
*0.5f
+rb
, p2
);
902 v3_muladds( rbb
->co
, rbb
->up
, hb
*0.5f
-rb
, p3
);
904 capsule_manifold manifold
;
905 rb_capsule_manifold_init( &manifold
);
909 closest_segment_segment( p0
, p1
, p2
, p3
, &ta
, &tb
, pa
, pb
);
910 rb_capsule_manifold( pa
, pb
, ta
, r
, &manifold
);
912 ta
= closest_point_segment( p0
, p1
, p2
, pa
);
913 tb
= closest_point_segment( p0
, p1
, p3
, pb
);
914 rb_capsule_manifold( pa
, p2
, ta
, r
, &manifold
);
915 rb_capsule_manifold( pb
, p3
, tb
, r
, &manifold
);
917 closest_point_segment( p2
, p3
, p0
, pa
);
918 closest_point_segment( p2
, p3
, p1
, pb
);
919 rb_capsule_manifold( p0
, pa
, 0.0f
, r
, &manifold
);
920 rb_capsule_manifold( p1
, pb
, 1.0f
, r
, &manifold
);
922 return rb_capsule_manifold_done( rba
, rbb
, &manifold
, buf
);
926 * Generates up to two contacts; optimised for the most stable manifold
928 VG_STATIC
int rb_capsule_box( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
930 float h
= rba
->inf
.capsule
.height
,
931 r
= rba
->inf
.capsule
.radius
;
934 * Solving this in symetric local space of the cube saves us some time and a
935 * couple branches when it comes to the quad stage.
938 v3_add( rbb
->bbx
[0], rbb
->bbx
[1], centroid
);
939 v3_muls( centroid
, 0.5f
, centroid
);
942 v3_sub( rbb
->bbx
[0], centroid
, bbx
[0] );
943 v3_sub( rbb
->bbx
[1], centroid
, bbx
[1] );
945 v3f pc
, p0w
, p1w
, p0
, p1
;
946 v3_muladds( rba
->co
, rba
->up
, -h
*0.5f
+r
, p0w
);
947 v3_muladds( rba
->co
, rba
->up
, h
*0.5f
-r
, p1w
);
949 m4x3_mulv( rbb
->to_local
, p0w
, p0
);
950 m4x3_mulv( rbb
->to_local
, p1w
, p1
);
951 v3_sub( p0
, centroid
, p0
);
952 v3_sub( p1
, centroid
, p1
);
953 v3_add( p0
, p1
, pc
);
954 v3_muls( pc
, 0.5f
, pc
);
957 * Finding an appropriate quad to collide lines with
960 v3_div( pc
, bbx
[1], region
);
963 if( (fabsf(region
[0]) > fabsf(region
[1])) &&
964 (fabsf(region
[0]) > fabsf(region
[2])) )
966 float px
= vg_signf(region
[0]) * bbx
[1][0];
967 v3_copy( (v3f
){ px
, bbx
[0][1], bbx
[0][2] }, quad
[0] );
968 v3_copy( (v3f
){ px
, bbx
[1][1], bbx
[0][2] }, quad
[1] );
969 v3_copy( (v3f
){ px
, bbx
[1][1], bbx
[1][2] }, quad
[2] );
970 v3_copy( (v3f
){ px
, bbx
[0][1], bbx
[1][2] }, quad
[3] );
972 else if( fabsf(region
[1]) > fabsf(region
[2]) )
974 float py
= vg_signf(region
[1]) * bbx
[1][1];
975 v3_copy( (v3f
){ bbx
[0][0], py
, bbx
[0][2] }, quad
[0] );
976 v3_copy( (v3f
){ bbx
[1][0], py
, bbx
[0][2] }, quad
[1] );
977 v3_copy( (v3f
){ bbx
[1][0], py
, bbx
[1][2] }, quad
[2] );
978 v3_copy( (v3f
){ bbx
[0][0], py
, bbx
[1][2] }, quad
[3] );
982 float pz
= vg_signf(region
[2]) * bbx
[1][2];
983 v3_copy( (v3f
){ bbx
[0][0], bbx
[0][1], pz
}, quad
[0] );
984 v3_copy( (v3f
){ bbx
[1][0], bbx
[0][1], pz
}, quad
[1] );
985 v3_copy( (v3f
){ bbx
[1][0], bbx
[1][1], pz
}, quad
[2] );
986 v3_copy( (v3f
){ bbx
[0][0], bbx
[1][1], pz
}, quad
[3] );
989 capsule_manifold manifold
;
990 rb_capsule_manifold_init( &manifold
);
993 closest_point_aabb( p0
, bbx
, c0
);
994 closest_point_aabb( p1
, bbx
, c1
);
997 v3_sub( c0
, p0
, d0
);
998 v3_sub( c1
, p1
, d1
);
999 v3_sub( p1
, p0
, da
);
1005 if( v3_dot( da
, d0
) <= 0.01f
)
1006 rb_capsule_manifold( p0
, c0
, 0.0f
, r
, &manifold
);
1008 if( v3_dot( da
, d1
) >= -0.01f
)
1009 rb_capsule_manifold( p1
, c1
, 1.0f
, r
, &manifold
);
1011 for( int i
=0; i
<4; i
++ )
1018 closest_segment_segment( p0
, p1
, quad
[i0
], quad
[i1
], &ta
, &tb
, ca
, cb
);
1019 rb_capsule_manifold( ca
, cb
, ta
, r
, &manifold
);
1023 * Create final contacts based on line manifold
1025 m3x3_mulv( rbb
->to_world
, manifold
.d0
, manifold
.d0
);
1026 m3x3_mulv( rbb
->to_world
, manifold
.d1
, manifold
.d1
);
1033 for( int i
=0; i
<4; i
++ )
1039 v3_add( quad
[i0
], centroid
, q0
);
1040 v3_add( quad
[i1
], centroid
, q1
);
1042 m4x3_mulv( rbb
->to_world
, q0
, q0
);
1043 m4x3_mulv( rbb
->to_world
, q1
, q1
);
1045 vg_line( q0
, q1
, 0xffffffff );
1049 return rb_capsule_manifold_done( rba
, rbb
, &manifold
, buf
);
1052 VG_STATIC
int rb_sphere_box( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1056 closest_point_obb( rba
->co
, rbb
->bbx
, rbb
->to_world
, rbb
->to_local
, co
);
1057 v3_sub( rba
->co
, co
, delta
);
1059 float d2
= v3_length2(delta
),
1060 r
= rba
->inf
.sphere
.radius
;
1069 v3_sub( rba
->co
, rbb
->co
, delta
);
1072 * some extra testing is required to find the best axis to push the
1073 * object back outside the box. Since there isnt a clear seperating
1074 * vector already, especially on really high aspect boxes.
1076 float lx
= v3_dot( rbb
->right
, delta
),
1077 ly
= v3_dot( rbb
->up
, delta
),
1078 lz
= v3_dot( rbb
->forward
, delta
),
1079 px
= rbb
->bbx
[1][0] - fabsf(lx
),
1080 py
= rbb
->bbx
[1][1] - fabsf(ly
),
1081 pz
= rbb
->bbx
[1][2] - fabsf(lz
);
1083 if( px
< py
&& px
< pz
)
1084 v3_muls( rbb
->right
, vg_signf(lx
), ct
->n
);
1086 v3_muls( rbb
->up
, vg_signf(ly
), ct
->n
);
1088 v3_muls( rbb
->forward
, vg_signf(lz
), ct
->n
);
1090 v3_muladds( rba
->co
, ct
->n
, -r
, ct
->co
);
1096 v3_muls( delta
, 1.0f
/d
, ct
->n
);
1098 v3_copy( co
, ct
->co
);
1103 ct
->type
= k_contact_type_default
;
1110 VG_STATIC
int rb_sphere_sphere( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1113 v3_sub( rba
->co
, rbb
->co
, delta
);
1115 float d2
= v3_length2(delta
),
1116 r
= rba
->inf
.sphere
.radius
+ rbb
->inf
.sphere
.radius
;
1120 float d
= sqrtf(d2
);
1123 v3_muls( delta
, 1.0f
/d
, ct
->n
);
1126 v3_muladds( rba
->co
, ct
->n
,-rba
->inf
.sphere
.radius
, p0
);
1127 v3_muladds( rbb
->co
, ct
->n
, rbb
->inf
.sphere
.radius
, p1
);
1128 v3_add( p0
, p1
, ct
->co
);
1129 v3_muls( ct
->co
, 0.5f
, ct
->co
);
1130 ct
->type
= k_contact_type_default
;
1140 //#define RIGIDBODY_DYNAMIC_MESH_EDGES
1142 VG_STATIC
int rb_sphere_triangle( rigidbody
*rba
, rigidbody
*rbb
,
1143 v3f tri
[3], rb_ct
*buf
)
1147 #ifdef RIGIDBODY_DYNAMIC_MESH_EDGES
1148 closest_on_triangle_1( rba
->co
, tri
, co
);
1150 enum contact_type type
= closest_on_triangle_1( rba
->co
, tri
, co
);
1153 v3_sub( rba
->co
, co
, delta
);
1155 float d2
= v3_length2( delta
),
1156 r
= rba
->inf
.sphere
.radius
;
1163 v3_sub( tri
[2], tri
[0], ab
);
1164 v3_sub( tri
[1], tri
[0], ac
);
1165 v3_cross( ac
, ab
, tn
);
1166 v3_copy( tn
, ct
->n
);
1167 v3_normalize( ct
->n
);
1169 float d
= sqrtf(d2
);
1171 v3_copy( co
, ct
->co
);
1183 VG_STATIC
void rb_debug_sharp_scene_edges( rigidbody
*rbb
, float sharp_ang
,
1184 boxf box
, u32 colour
)
1186 sharp_ang
= cosf( sharp_ang
);
1188 scene
*sc
= rbb
->inf
.scene
.bh_scene
->user
;
1189 vg_line_boxf( box
, 0xff00ff00 );
1192 bh_iter_init( 0, &it
);
1195 while( bh_next( rbb
->inf
.scene
.bh_scene
, &it
, box
, &idx
) )
1197 u32
*ptri
= &sc
->arrindices
[ idx
*3 ];
1200 for( int j
=0; j
<3; j
++ )
1201 v3_copy( sc
->arrvertices
[ptri
[j
]].co
, tri
[j
] );
1203 for( int j
=0; j
<3; j
++ )
1207 v3_sub( tri
[(j
+1)%3], tri
[j
], edir
);
1209 if( v3_dot( edir
, (v3f
){ 0.5184758473652127f
,
1210 0.2073903389460850f
,
1211 -0.8295613557843402f
} ) < 0.0f
)
1216 bh_iter_init( 0, &jt
);
1219 float const k_r
= 0.02f
;
1220 v3_add( (v3f
){ k_r
, k_r
, k_r
}, tri
[j
], region
[1] );
1221 v3_add( (v3f
){ -k_r
, -k_r
, -k_r
}, tri
[j
], region
[0] );
1224 while( bh_next( rbb
->inf
.scene
.bh_scene
, &jt
, region
, &jdx
) )
1229 u32
*ptrj
= &sc
->arrindices
[ jdx
*3 ];
1232 for( int k
=0; k
<3; k
++ )
1233 v3_copy( sc
->arrvertices
[ptrj
[k
]].co
, trj
[k
] );
1235 for( int k
=0; k
<3; k
++ )
1237 if( v3_dist2( tri
[j
], trj
[k
] ) <= k_r
*k_r
)
1244 if( v3_dist2( tri
[jp1
], trj
[km1
] ) <= k_r
*k_r
)
1247 v3_sub( tri
[jp1
], tri
[j
], b0
);
1248 v3_sub( tri
[jp2
], tri
[j
], b1
);
1249 v3_sub( trj
[km2
], tri
[j
], b2
);
1252 v3_cross( b0
, b1
, cx0
);
1253 v3_cross( b2
, b0
, cx1
);
1255 float polarity
= v3_dot( cx0
, b2
);
1257 if( polarity
< 0.0f
)
1260 vg_line( tri
[j
], tri
[jp1
], 0xff00ff00 );
1261 float ang
= v3_dot(cx0
,cx1
) /
1262 (v3_length(cx0
)*v3_length(cx1
));
1263 if( ang
< sharp_ang
)
1265 vg_line( tri
[j
], tri
[jp1
], 0xff00ff00 );
1277 VG_STATIC
int rb_sphere_scene( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1279 scene
*sc
= rbb
->inf
.scene
.bh_scene
->user
;
1282 bh_iter_init( 0, &it
);
1287 while( bh_next( rbb
->inf
.scene
.bh_scene
, &it
, rba
->bbx_world
, &idx
) )
1289 u32
*ptri
= &sc
->arrindices
[ idx
*3 ];
1292 for( int j
=0; j
<3; j
++ )
1293 v3_copy( sc
->arrvertices
[ptri
[j
]].co
, tri
[j
] );
1295 buf
[ count
].element_id
= ptri
[0];
1297 vg_line( tri
[0],tri
[1],0x70ff6000 );
1298 vg_line( tri
[1],tri
[2],0x70ff6000 );
1299 vg_line( tri
[2],tri
[0],0x70ff6000 );
1301 int contact
= rb_sphere_triangle( rba
, rbb
, tri
, buf
+count
);
1306 vg_warn( "Exceeding sphere_vs_scene capacity. Geometry too dense!\n" );
1314 VG_STATIC
int rb_box_scene( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1316 scene
*sc
= rbb
->inf
.scene
.bh_scene
->user
;
1321 bh_iter_init( 0, &it
);
1326 while( bh_next( rbb
->inf
.scene
.bh_scene
, &it
, rba
->bbx_world
, &idx
) )
1328 u32
*ptri
= &sc
->arrindices
[ idx
*3 ];
1330 for( int j
=0; j
<3; j
++ )
1331 v3_copy( sc
->arrvertices
[ptri
[j
]].co
, tri
[j
] );
1333 if( rb_box_triangle_sat( rba
, tri
) )
1335 vg_line(tri
[0],tri
[1],0xff50ff00 );
1336 vg_line(tri
[1],tri
[2],0xff50ff00 );
1337 vg_line(tri
[2],tri
[0],0xff50ff00 );
1341 vg_line(tri
[0],tri
[1],0xff0000ff );
1342 vg_line(tri
[1],tri
[2],0xff0000ff );
1343 vg_line(tri
[2],tri
[0],0xff0000ff );
1349 v3_sub( tri
[1], tri
[0], v0
);
1350 v3_sub( tri
[2], tri
[0], v1
);
1351 v3_cross( v0
, v1
, n
);
1354 /* find best feature */
1355 float best
= v3_dot( rba
->right
, n
);
1358 float cy
= v3_dot( rba
->up
, n
);
1359 if( fabsf(cy
) > fabsf(best
) )
1365 float cz
= -v3_dot( rba
->forward
, n
);
1366 if( fabsf(cz
) > fabsf(best
) )
1376 float px
= best
> 0.0f
? rba
->bbx
[0][0]: rba
->bbx
[1][0];
1377 manifold
[0][0] = px
;
1378 manifold
[0][1] = rba
->bbx
[0][1];
1379 manifold
[0][2] = rba
->bbx
[0][2];
1380 manifold
[1][0] = px
;
1381 manifold
[1][1] = rba
->bbx
[1][1];
1382 manifold
[1][2] = rba
->bbx
[0][2];
1383 manifold
[2][0] = px
;
1384 manifold
[2][1] = rba
->bbx
[1][1];
1385 manifold
[2][2] = rba
->bbx
[1][2];
1386 manifold
[3][0] = px
;
1387 manifold
[3][1] = rba
->bbx
[0][1];
1388 manifold
[3][2] = rba
->bbx
[1][2];
1390 else if( axis
== 1 )
1392 float py
= best
> 0.0f
? rba
->bbx
[0][1]: rba
->bbx
[1][1];
1393 manifold
[0][0] = rba
->bbx
[0][0];
1394 manifold
[0][1] = py
;
1395 manifold
[0][2] = rba
->bbx
[0][2];
1396 manifold
[1][0] = rba
->bbx
[1][0];
1397 manifold
[1][1] = py
;
1398 manifold
[1][2] = rba
->bbx
[0][2];
1399 manifold
[2][0] = rba
->bbx
[1][0];
1400 manifold
[2][1] = py
;
1401 manifold
[2][2] = rba
->bbx
[1][2];
1402 manifold
[3][0] = rba
->bbx
[0][0];
1403 manifold
[3][1] = py
;
1404 manifold
[3][2] = rba
->bbx
[1][2];
1408 float pz
= best
> 0.0f
? rba
->bbx
[0][2]: rba
->bbx
[1][2];
1409 manifold
[0][0] = rba
->bbx
[0][0];
1410 manifold
[0][1] = rba
->bbx
[0][1];
1411 manifold
[0][2] = pz
;
1412 manifold
[1][0] = rba
->bbx
[1][0];
1413 manifold
[1][1] = rba
->bbx
[0][1];
1414 manifold
[1][2] = pz
;
1415 manifold
[2][0] = rba
->bbx
[1][0];
1416 manifold
[2][1] = rba
->bbx
[1][1];
1417 manifold
[2][2] = pz
;
1418 manifold
[3][0] = rba
->bbx
[0][0];
1419 manifold
[3][1] = rba
->bbx
[1][1];
1420 manifold
[3][2] = pz
;
1423 for( int j
=0; j
<4; j
++ )
1424 m4x3_mulv( rba
->to_world
, manifold
[j
], manifold
[j
] );
1426 vg_line( manifold
[0], manifold
[1], 0xffffffff );
1427 vg_line( manifold
[1], manifold
[2], 0xffffffff );
1428 vg_line( manifold
[2], manifold
[3], 0xffffffff );
1429 vg_line( manifold
[3], manifold
[0], 0xffffffff );
1431 for( int j
=0; j
<4; j
++ )
1433 rb_ct
*ct
= buf
+count
;
1435 v3_copy( manifold
[j
], ct
->co
);
1436 v3_copy( n
, ct
->n
);
1438 float l0
= v3_dot( tri
[0], n
),
1439 l1
= v3_dot( manifold
[j
], n
);
1441 ct
->p
= (l0
-l1
)*0.5f
;
1445 ct
->type
= k_contact_type_default
;
1457 VG_STATIC
int RB_MATRIX_ERROR( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1459 vg_error( "Collision type is unimplemented between types %d and %d\n",
1460 rba
->type
, rbb
->type
);
1465 VG_STATIC
int rb_sphere_capsule( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1467 return rb_capsule_sphere( rbb
, rba
, buf
);
1470 VG_STATIC
int rb_box_capsule( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1472 return rb_capsule_box( rbb
, rba
, buf
);
1475 VG_STATIC
int rb_box_sphere( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1477 return rb_sphere_box( rbb
, rba
, buf
);
1480 VG_STATIC
int rb_scene_box( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1482 return rb_box_scene( rbb
, rba
, buf
);
1485 VG_STATIC
int (*rb_jump_table
[4][4])( rigidbody
*a
, rigidbody
*b
, rb_ct
*buf
) =
1487 /* box */ /* Sphere */ /* Capsule */ /* Mesh */
1488 { RB_MATRIX_ERROR
, rb_box_sphere
, rb_box_capsule
, rb_box_scene
},
1489 { rb_sphere_box
, rb_sphere_sphere
, rb_sphere_capsule
, rb_sphere_scene
},
1490 { rb_capsule_box
, rb_capsule_sphere
, rb_capsule_capsule
, RB_MATRIX_ERROR
},
1491 { rb_scene_box
, RB_MATRIX_ERROR
, RB_MATRIX_ERROR
, RB_MATRIX_ERROR
}
1494 VG_STATIC
int rb_collide( rigidbody
*rba
, rigidbody
*rbb
)
1496 int (*collider_jump
)(rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1497 = rb_jump_table
[rba
->type
][rbb
->type
];
1500 * 12 is the maximum manifold size we can generate, so we are forced to abort
1501 * potentially checking any more.
1503 if( rb_contact_count
+ 12 > vg_list_size(rb_contact_buffer
) )
1505 vg_warn( "Too many contacts made in global collider buffer (%d of %d\n)",
1506 rb_contact_count
, vg_list_size(rb_contact_buffer
) );
1511 * FUTURE: Replace this with a more dedicated broad phase pass
1513 if( box_overlap( rba
->bbx_world
, rbb
->bbx_world
) )
1515 int count
= collider_jump( rba
, rbb
, rb_contact_buffer
+rb_contact_count
);
1516 rb_contact_count
+= count
;
1524 * -----------------------------------------------------------------------------
1526 * -----------------------------------------------------------------------------
1529 VG_STATIC
void rb_solver_reset(void)
1531 rb_contact_count
= 0;
1534 VG_STATIC rb_ct
*rb_global_ct(void)
1536 return rb_contact_buffer
+ rb_contact_count
;
1540 * Initializing things like tangent vectors
1542 VG_STATIC
void rb_presolve_contacts( rb_ct
*buffer
, int len
)
1544 for( int i
=0; i
<len
; i
++ )
1546 rb_ct
*ct
= &buffer
[i
];
1548 ct
->bias
= -0.2f
* k_rb_rate
* vg_minf( 0.0f
, -ct
->p
+k_penetration_slop
);
1549 rb_tangent_basis( ct
->n
, ct
->t
[0], ct
->t
[1] );
1552 ct
->type
= k_contact_type_default
;
1554 ct
->norm_impulse
= 0.0f
;
1555 ct
->tangent_impulse
[0] = 0.0f
;
1556 ct
->tangent_impulse
[1] = 0.0f
;
1558 v3f ra
, rb
, raCn
, rbCn
, raCt
, rbCt
;
1559 v3_sub( ct
->co
, ct
->rba
->co
, ra
);
1560 v3_sub( ct
->co
, ct
->rbb
->co
, rb
);
1561 v3_cross( ra
, ct
->n
, raCn
);
1562 v3_cross( rb
, ct
->n
, rbCn
);
1564 /* orient inverse inertia tensors */
1566 m3x3_mulv( ct
->rba
->iIw
, raCn
, raCnI
);
1567 m3x3_mulv( ct
->rbb
->iIw
, rbCn
, rbCnI
);
1569 ct
->normal_mass
= ct
->rba
->inv_mass
+ ct
->rbb
->inv_mass
;
1570 ct
->normal_mass
+= v3_dot( raCn
, raCnI
);
1571 ct
->normal_mass
+= v3_dot( rbCn
, rbCnI
);
1572 ct
->normal_mass
= 1.0f
/ct
->normal_mass
;
1574 for( int j
=0; j
<2; j
++ )
1577 v3_cross( ct
->t
[j
], ra
, raCt
);
1578 v3_cross( ct
->t
[j
], rb
, rbCt
);
1579 m3x3_mulv( ct
->rba
->iIw
, raCt
, raCtI
);
1580 m3x3_mulv( ct
->rbb
->iIw
, rbCt
, rbCtI
);
1582 ct
->tangent_mass
[j
] = ct
->rba
->inv_mass
+ ct
->rbb
->inv_mass
;
1583 ct
->tangent_mass
[j
] += v3_dot( raCt
, raCtI
);
1584 ct
->tangent_mass
[j
] += v3_dot( rbCt
, rbCtI
);
1585 ct
->tangent_mass
[j
] = 1.0f
/ct
->tangent_mass
[j
];
1588 rb_debug_contact( ct
);
1593 * Creates relative contact velocity vector, and offsets between each body
1595 VG_STATIC
void rb_rcv( rb_ct
*ct
, v3f rv
, v3f da
, v3f db
)
1597 rigidbody
*rba
= ct
->rba
,
1600 v3_sub( ct
->co
, rba
->co
, da
);
1601 v3_sub( ct
->co
, rbb
->co
, db
);
1604 v3_cross( rba
->w
, da
, rva
);
1605 v3_add( rba
->v
, rva
, rva
);
1606 v3_cross( rbb
->w
, db
, rvb
);
1607 v3_add( rbb
->v
, rvb
, rvb
);
1609 v3_sub( rva
, rvb
, rv
);
1613 * Apply impulse to object
1615 VG_STATIC
void rb_linear_impulse( rigidbody
*rb
, v3f delta
, v3f impulse
)
1618 v3_muladds( rb
->v
, impulse
, rb
->inv_mass
, rb
->v
);
1620 /* Angular velocity */
1622 v3_cross( delta
, impulse
, wa
);
1624 m3x3_mulv( rb
->iIw
, wa
, wa
);
1625 v3_add( rb
->w
, wa
, rb
->w
);
1629 * One iteration to solve the contact constraint
1631 VG_STATIC
void rb_solve_contacts( rb_ct
*buf
, int len
)
1633 for( int i
=0; i
<len
; i
++ )
1635 struct contact
*ct
= &buf
[i
];
1637 rigidbody
*rb
= ct
->rba
;
1640 rb_rcv( ct
, rv
, da
, db
);
1643 for( int j
=0; j
<2; j
++ )
1645 float f
= k_friction
* ct
->norm_impulse
,
1646 vt
= v3_dot( rv
, ct
->t
[j
] ),
1647 lambda
= ct
->tangent_mass
[j
] * -vt
;
1649 float temp
= ct
->tangent_impulse
[j
];
1650 ct
->tangent_impulse
[j
] = vg_clampf( temp
+ lambda
, -f
, f
);
1651 lambda
= ct
->tangent_impulse
[j
] - temp
;
1654 v3_muls( ct
->t
[j
], lambda
, impulse
);
1655 rb_linear_impulse( ct
->rba
, da
, impulse
);
1657 v3_muls( ct
->t
[j
], -lambda
, impulse
);
1658 rb_linear_impulse( ct
->rbb
, db
, impulse
);
1662 rb_rcv( ct
, rv
, da
, db
);
1663 float vn
= v3_dot( rv
, ct
->n
),
1664 lambda
= ct
->normal_mass
* (-vn
+ ct
->bias
);
1666 float temp
= ct
->norm_impulse
;
1667 ct
->norm_impulse
= vg_maxf( temp
+ lambda
, 0.0f
);
1668 lambda
= ct
->norm_impulse
- temp
;
1671 v3_muls( ct
->n
, lambda
, impulse
);
1672 rb_linear_impulse( ct
->rba
, da
, impulse
);
1674 v3_muls( ct
->n
, -lambda
, impulse
);
1675 rb_linear_impulse( ct
->rbb
, db
, impulse
);
1680 * -----------------------------------------------------------------------------
1682 * -----------------------------------------------------------------------------
1685 VG_STATIC
void draw_angle_limit( v3f c
, v3f major
, v3f minor
,
1686 float amin
, float amax
, float measured
,
1691 v3_muls( major
, f
, ay
);
1692 v3_muls( minor
, f
, ax
);
1694 for( int x
=0; x
<16; x
++ )
1696 float t0
= (float)x
/ 16.0f
,
1697 t1
= (float)(x
+1) / 16.0f
,
1698 a0
= vg_lerpf( amin
, amax
, t0
),
1699 a1
= vg_lerpf( amin
, amax
, t1
);
1702 v3_muladds( c
, ay
, cosf(a0
), p0
);
1703 v3_muladds( p0
, ax
, sinf(a0
), p0
);
1704 v3_muladds( c
, ay
, cosf(a1
), p1
);
1705 v3_muladds( p1
, ax
, sinf(a1
), p1
);
1707 vg_line( p0
, p1
, colour
);
1710 vg_line( c
, p0
, colour
);
1712 vg_line( c
, p1
, colour
);
1716 v3_muladds( c
, ay
, cosf(measured
)*1.2f
, p2
);
1717 v3_muladds( p2
, ax
, sinf(measured
)*1.2f
, p2
);
1718 vg_line( c
, p2
, colour
);
1721 VG_STATIC
void rb_debug_constraint_limits( rigidbody
*ra
, rigidbody
*rb
, v3f lca
,
1724 v3f ax
, ay
, az
, bx
, by
, bz
;
1725 m3x3_mulv( ra
->to_world
, (v3f
){1.0f
,0.0f
,0.0f
}, ax
);
1726 m3x3_mulv( ra
->to_world
, (v3f
){0.0f
,1.0f
,0.0f
}, ay
);
1727 m3x3_mulv( ra
->to_world
, (v3f
){0.0f
,0.0f
,1.0f
}, az
);
1728 m3x3_mulv( rb
->to_world
, (v3f
){1.0f
,0.0f
,0.0f
}, bx
);
1729 m3x3_mulv( rb
->to_world
, (v3f
){0.0f
,1.0f
,0.0f
}, by
);
1730 m3x3_mulv( rb
->to_world
, (v3f
){0.0f
,0.0f
,1.0f
}, bz
);
1733 px
[0] = v3_dot( ay
, by
);
1734 px
[1] = v3_dot( az
, by
);
1736 py
[0] = v3_dot( az
, bz
);
1737 py
[1] = v3_dot( ax
, bz
);
1739 pz
[0] = v3_dot( ax
, bx
);
1740 pz
[1] = v3_dot( ay
, bx
);
1742 float r0
= atan2f( px
[1], px
[0] ),
1743 r1
= atan2f( py
[1], py
[0] ),
1744 r2
= atan2f( pz
[1], pz
[0] );
1747 m4x3_mulv( ra
->to_world
, lca
, c
);
1748 draw_angle_limit( c
, ay
, az
, limits
[0][0], limits
[1][0], r0
, 0xff0000ff );
1749 draw_angle_limit( c
, az
, ax
, limits
[0][1], limits
[1][1], r1
, 0xff00ff00 );
1750 draw_angle_limit( c
, ax
, ay
, limits
[0][2], limits
[1][2], r2
, 0xffff0000 );
1753 VG_STATIC
void rb_limit_cure( rigidbody
*ra
, rigidbody
*rb
, v3f axis
, float d
)
1757 float avx
= v3_dot( ra
->w
, axis
) - v3_dot( rb
->w
, axis
);
1758 float joint_mass
= rb
->inv_mass
+ ra
->inv_mass
;
1759 joint_mass
= 1.0f
/joint_mass
;
1761 float bias
= (k_limit_bias
* k_rb_rate
) * d
,
1762 lambda
= -(avx
+ bias
) * joint_mass
;
1764 /* Angular velocity */
1766 v3_muls( axis
, lambda
* ra
->inv_mass
, wa
);
1767 v3_muls( axis
, -lambda
* rb
->inv_mass
, wb
);
1769 v3_add( ra
->w
, wa
, ra
->w
);
1770 v3_add( rb
->w
, wb
, rb
->w
);
1774 VG_STATIC
void rb_constraint_limits( rigidbody
*ra
, v3f lca
,
1775 rigidbody
*rb
, v3f lcb
, v3f limits
[2] )
1777 v3f ax
, ay
, az
, bx
, by
, bz
;
1778 m3x3_mulv( ra
->to_world
, (v3f
){1.0f
,0.0f
,0.0f
}, ax
);
1779 m3x3_mulv( ra
->to_world
, (v3f
){0.0f
,1.0f
,0.0f
}, ay
);
1780 m3x3_mulv( ra
->to_world
, (v3f
){0.0f
,0.0f
,1.0f
}, az
);
1781 m3x3_mulv( rb
->to_world
, (v3f
){1.0f
,0.0f
,0.0f
}, bx
);
1782 m3x3_mulv( rb
->to_world
, (v3f
){0.0f
,1.0f
,0.0f
}, by
);
1783 m3x3_mulv( rb
->to_world
, (v3f
){0.0f
,0.0f
,1.0f
}, bz
);
1786 px
[0] = v3_dot( ay
, by
);
1787 px
[1] = v3_dot( az
, by
);
1789 py
[0] = v3_dot( az
, bz
);
1790 py
[1] = v3_dot( ax
, bz
);
1792 pz
[0] = v3_dot( ax
, bx
);
1793 pz
[1] = v3_dot( ay
, bx
);
1795 float r0
= atan2f( px
[1], px
[0] ),
1796 r1
= atan2f( py
[1], py
[0] ),
1797 r2
= atan2f( pz
[1], pz
[0] );
1799 /* calculate angle deltas */
1800 float dx
= 0.0f
, dy
= 0.0f
, dz
= 0.0f
;
1802 if( r0
< limits
[0][0] ) dx
= limits
[0][0] - r0
;
1803 if( r0
> limits
[1][0] ) dx
= limits
[1][0] - r0
;
1804 if( r1
< limits
[0][1] ) dy
= limits
[0][1] - r1
;
1805 if( r1
> limits
[1][1] ) dy
= limits
[1][1] - r1
;
1806 if( r2
< limits
[0][2] ) dz
= limits
[0][2] - r2
;
1807 if( r2
> limits
[1][2] ) dz
= limits
[1][2] - r2
;
1810 m3x3_mulv( ra
->to_world
, lca
, wca
);
1811 m3x3_mulv( rb
->to_world
, lcb
, wcb
);
1813 rb_limit_cure( ra
, rb
, ax
, dx
);
1814 rb_limit_cure( ra
, rb
, ay
, dy
);
1815 rb_limit_cure( ra
, rb
, az
, dz
);
1818 VG_STATIC
void rb_debug_constraint_position( rigidbody
*ra
, v3f lca
,
1819 rigidbody
*rb
, v3f lcb
)
1822 m3x3_mulv( ra
->to_world
, lca
, wca
);
1823 m3x3_mulv( rb
->to_world
, lcb
, wcb
);
1826 v3_add( wca
, ra
->co
, p0
);
1827 v3_add( wcb
, rb
->co
, p1
);
1828 vg_line_pt3( p0
, 0.005f
, 0xffffff00 );
1829 vg_line_pt3( p1
, 0.005f
, 0xffffff00 );
1830 vg_line( p0
, p1
, 0xffffff00 );
1833 VG_STATIC
void rb_constraint_position( rigidbody
*ra
, v3f lca
,
1834 rigidbody
*rb
, v3f lcb
)
1836 /* C = (COa + Ra*LCa) - (COb + Rb*LCb) = 0 */
1838 m3x3_mulv( ra
->to_world
, lca
, wca
);
1839 m3x3_mulv( rb
->to_world
, lcb
, wcb
);
1842 v3_sub( ra
->v
, rb
->v
, rcv
);
1845 v3_cross( ra
->w
, wca
, rcv_Ra
);
1846 v3_cross( rb
->w
, wcb
, rcv_Rb
);
1847 v3_add( rcv_Ra
, rcv
, rcv
);
1848 v3_sub( rcv
, rcv_Rb
, rcv
);
1852 v3_add( wca
, ra
->co
, p0
);
1853 v3_add( wcb
, rb
->co
, p1
);
1854 v3_sub( p1
, p0
, delta
);
1856 float dist2
= v3_length2( delta
);
1858 if( dist2
> 0.00001f
)
1860 float dist
= sqrtf(dist2
);
1861 v3_muls( delta
, 1.0f
/dist
, delta
);
1863 float joint_mass
= rb
->inv_mass
+ ra
->inv_mass
;
1865 v3f raCn
, rbCn
, raCt
, rbCt
;
1866 v3_cross( wca
, delta
, raCn
);
1867 v3_cross( wcb
, delta
, rbCn
);
1869 /* orient inverse inertia tensors */
1871 m3x3_mulv( ra
->iIw
, raCn
, raCnI
);
1872 m3x3_mulv( rb
->iIw
, rbCn
, rbCnI
);
1873 joint_mass
+= v3_dot( raCn
, raCnI
);
1874 joint_mass
+= v3_dot( rbCn
, rbCnI
);
1875 joint_mass
= 1.0f
/joint_mass
;
1877 float vd
= v3_dot( rcv
, delta
),
1878 bias
= -(k_joint_bias
* k_rb_rate
) * dist
,
1879 lambda
= -(vd
+ bias
) * joint_mass
;
1882 v3_muls( delta
, lambda
, impulse
);
1883 rb_linear_impulse( ra
, wca
, impulse
);
1884 v3_muls( delta
, -lambda
, impulse
);
1885 rb_linear_impulse( rb
, wcb
, impulse
);
1888 v3_muladds( ra
->co
, delta
, dist
* k_joint_correction
, ra
->co
);
1889 v3_muladds( rb
->co
, delta
, -dist
* k_joint_correction
, rb
->co
);
1897 VG_STATIC
void rb_effect_simple_bouyency( rigidbody
*ra
, v4f plane
,
1898 float amt
, float drag
)
1901 float depth
= v3_dot( plane
, ra
->co
) - plane
[3],
1902 lambda
= vg_clampf( -depth
, 0.0f
, 1.0f
) * amt
;
1904 v3_muladds( ra
->v
, plane
, lambda
* k_rb_delta
, ra
->v
);
1907 v3_muls( ra
->v
, 1.0f
-(drag
*k_rb_delta
), ra
->v
);
1911 * -----------------------------------------------------------------------------
1912 * BVH implementation, this is ONLY for VG_STATIC rigidbodies, its to slow for
1914 * -----------------------------------------------------------------------------
1917 VG_STATIC
void rb_bh_expand_bound( void *user
, boxf bound
, u32 item_index
)
1919 rigidbody
*rb
= &((rigidbody
*)user
)[ item_index
];
1920 box_concat( bound
, rb
->bbx_world
);
1923 VG_STATIC
float rb_bh_centroid( void *user
, u32 item_index
, int axis
)
1925 rigidbody
*rb
= &((rigidbody
*)user
)[ item_index
];
1926 return (rb
->bbx_world
[axis
][0] + rb
->bbx_world
[1][axis
]) * 0.5f
;
1929 VG_STATIC
void rb_bh_swap( void *user
, u32 ia
, u32 ib
)
1931 rigidbody temp
, *rba
, *rbb
;
1932 rba
= &((rigidbody
*)user
)[ ia
];
1933 rbb
= &((rigidbody
*)user
)[ ib
];
1940 VG_STATIC
void rb_bh_debug( void *user
, u32 item_index
)
1942 rigidbody
*rb
= &((rigidbody
*)user
)[ item_index
];
1943 rb_debug( rb
, 0xff00ffff );
1946 VG_STATIC bh_system bh_system_rigidbodies
=
1948 .expand_bound
= rb_bh_expand_bound
,
1949 .item_centroid
= rb_bh_centroid
,
1950 .item_swap
= rb_bh_swap
,
1951 .item_debug
= rb_bh_debug
,
1955 #endif /* RIGIDBODY_H */