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 if( isnanf( rb
->v
[0] ) ||
450 isnanf( rb
->v
[1] ) ||
453 vg_fatal_exit_loop( "NaN velocity" );
456 v3f gravity
= { 0.0f
, -9.8f
, 0.0f
};
457 v3_muladds( rb
->v
, gravity
, k_rb_delta
, rb
->v
);
459 /* intergrate velocity */
460 v3_muladds( rb
->co
, rb
->v
, k_rb_delta
, rb
->co
);
461 v3_lerp( rb
->w
, (v3f
){0.0f
,0.0f
,0.0f
}, 0.0025f
, rb
->w
);
463 /* inegrate inertia */
464 if( v3_length2( rb
->w
) > 0.0f
)
468 v3_copy( rb
->w
, axis
);
470 float mag
= v3_length( axis
);
471 v3_divs( axis
, mag
, axis
);
472 q_axis_angle( rotation
, axis
, mag
*k_rb_delta
);
473 q_mul( rotation
, rb
->q
, rb
->q
);
477 v3_muls( rb
->v
, 1.0f
/(1.0f
+k_rb_delta
*k_damp_linear
), rb
->v
);
478 v3_muls( rb
->w
, 1.0f
/(1.0f
+k_rb_delta
*k_damp_angular
), rb
->w
);
483 * -----------------------------------------------------------------------------
484 * Boolean shape overlap functions
485 * -----------------------------------------------------------------------------
489 * Project AABB, and triangle interval onto axis to check if they overlap
491 VG_STATIC
int rb_box_triangle_interval( v3f extent
, v3f axis
, v3f tri
[3] )
495 r
= extent
[0] * fabsf(axis
[0]) +
496 extent
[1] * fabsf(axis
[1]) +
497 extent
[2] * fabsf(axis
[2]),
499 p0
= v3_dot( axis
, tri
[0] ),
500 p1
= v3_dot( axis
, tri
[1] ),
501 p2
= v3_dot( axis
, tri
[2] ),
503 e
= vg_maxf(-vg_maxf(p0
,vg_maxf(p1
,p2
)), vg_minf(p0
,vg_minf(p1
,p2
)));
505 if( e
> r
) return 0;
510 * Seperating axis test box vs triangle
512 VG_STATIC
int rb_box_triangle_sat( rigidbody
*rba
, v3f tri_src
[3] )
517 v3_sub( rba
->bbx
[1], rba
->bbx
[0], extent
);
518 v3_muls( extent
, 0.5f
, extent
);
519 v3_add( rba
->bbx
[0], extent
, c
);
521 for( int i
=0; i
<3; i
++ )
523 m4x3_mulv( rba
->to_local
, tri_src
[i
], tri
[i
] );
524 v3_sub( tri
[i
], c
, tri
[i
] );
528 if(!rb_box_triangle_interval( extent
, (v3f
){1.0f
,0.0f
,0.0f
}, tri
)) return 0;
529 if(!rb_box_triangle_interval( extent
, (v3f
){0.0f
,1.0f
,0.0f
}, tri
)) return 0;
530 if(!rb_box_triangle_interval( extent
, (v3f
){0.0f
,0.0f
,1.0f
}, tri
)) return 0;
532 v3f v0
,v1
,v2
,n
, e0
,e1
,e2
;
533 v3_sub( tri
[1], tri
[0], v0
);
534 v3_sub( tri
[2], tri
[0], v1
);
535 v3_sub( tri
[2], tri
[1], v2
);
539 v3_cross( v0
, v1
, n
);
540 v3_cross( v0
, n
, e0
);
541 v3_cross( n
, v1
, e1
);
542 v3_cross( v2
, n
, e2
);
545 if(!rb_box_triangle_interval( extent
, n
, tri
)) return 0;
548 v3_cross( e0
, (v3f
){1.0f
,0.0f
,0.0f
}, axis
[0] );
549 v3_cross( e0
, (v3f
){0.0f
,1.0f
,0.0f
}, axis
[1] );
550 v3_cross( e0
, (v3f
){0.0f
,0.0f
,1.0f
}, axis
[2] );
551 v3_cross( e1
, (v3f
){1.0f
,0.0f
,0.0f
}, axis
[3] );
552 v3_cross( e1
, (v3f
){0.0f
,1.0f
,0.0f
}, axis
[4] );
553 v3_cross( e1
, (v3f
){0.0f
,0.0f
,1.0f
}, axis
[5] );
554 v3_cross( e2
, (v3f
){1.0f
,0.0f
,0.0f
}, axis
[6] );
555 v3_cross( e2
, (v3f
){0.0f
,1.0f
,0.0f
}, axis
[7] );
556 v3_cross( e2
, (v3f
){0.0f
,0.0f
,1.0f
}, axis
[8] );
558 for( int i
=0; i
<9; i
++ )
559 if(!rb_box_triangle_interval( extent
, axis
[i
], tri
)) return 0;
565 * -----------------------------------------------------------------------------
567 * -----------------------------------------------------------------------------
570 VG_STATIC
int rb_manifold_apply_filtered( rb_ct
*man
, int len
)
574 for( int i
=0; i
<len
; i
++ )
578 if( ct
->type
== k_contact_type_disabled
)
587 VG_STATIC
void rb_manifold_filter_joint_edges( rb_ct
*man
, int len
, float r
)
589 for( int i
=0; i
<len
-1; i
++ )
592 if( ci
->type
!= k_contact_type_edge
)
595 for( int j
=i
+1; j
<len
; j
++ )
598 if( cj
->type
!= k_contact_type_edge
)
601 if( v3_dist2( ci
->co
, cj
->co
) < r
*r
)
603 cj
->type
= k_contact_type_disabled
;
604 ci
->p
= (ci
->p
+ cj
->p
) * 0.5f
;
606 v3_add( ci
->co
, cj
->co
, ci
->co
);
607 v3_muls( ci
->co
, 0.5f
, ci
->co
);
610 v3_sub( ci
->rba
->co
, ci
->co
, delta
);
612 float c0
= v3_dot( ci
->n
, delta
),
613 c1
= v3_dot( cj
->n
, delta
);
615 if( c0
< 0.0f
|| c1
< 0.0f
)
618 ci
->type
= k_contact_type_disabled
;
623 v3_muls( ci
->n
, c0
, n
);
624 v3_muladds( n
, cj
->n
, c1
, n
);
634 * Resolve overlapping pairs
636 VG_STATIC
void rb_manifold_filter_pairs( rb_ct
*man
, int len
, float r
)
638 for( int i
=0; i
<len
-1; i
++ )
643 if( ci
->type
== k_contact_type_disabled
) continue;
645 for( int j
=i
+1; j
<len
; j
++ )
649 if( cj
->type
== k_contact_type_disabled
) continue;
651 if( v3_dist2( ci
->co
, cj
->co
) < r
*r
)
653 cj
->type
= k_contact_type_disabled
;
654 v3_add( cj
->n
, ci
->n
, ci
->n
);
662 float n
= 1.0f
/((float)similar
+1.0f
);
663 v3_muls( ci
->n
, n
, ci
->n
);
666 if( v3_length2(ci
->n
) < 0.1f
*0.1f
)
667 ci
->type
= k_contact_type_disabled
;
669 v3_normalize( ci
->n
);
675 * Remove contacts that are facing away from A
677 VG_STATIC
void rb_manifold_filter_backface( rb_ct
*man
, int len
)
679 for( int i
=0; i
<len
; i
++ )
682 if( ct
->type
== k_contact_type_disabled
)
686 v3_sub( ct
->co
, ct
->rba
->co
, delta
);
688 if( v3_dot( delta
, ct
->n
) > -0.001f
)
689 ct
->type
= k_contact_type_disabled
;
694 * Filter out duplicate coplanar results. Good for spheres.
696 VG_STATIC
void rb_manifold_filter_coplanar( rb_ct
*man
, int len
, float w
)
698 for( int i
=0; i
<len
; i
++ )
701 if( ci
->type
== k_contact_type_disabled
||
702 ci
->type
== k_contact_type_edge
)
705 float d1
= v3_dot( ci
->co
, ci
->n
);
707 for( int j
=0; j
<len
; j
++ )
713 if( cj
->type
== k_contact_type_disabled
)
716 float d2
= v3_dot( cj
->co
, ci
->n
),
719 if( fabsf( d
) <= w
)
721 cj
->type
= k_contact_type_disabled
;
728 * -----------------------------------------------------------------------------
730 * -----------------------------------------------------------------------------
736 * These do not automatically allocate contacts, an appropriately sized
737 * buffer must be supplied. The function returns the size of the manifold
738 * which was generated.
740 * The values set on the contacts are: n, co, p, rba, rbb
744 * By collecting the minimum(time) and maximum(time) pairs of points, we
745 * build a reduced and stable exact manifold.
748 * rx: minimum distance of these points
749 * dx: the delta between the two points
751 * pairs will only ammend these if they are creating a collision
753 typedef struct capsule_manifold capsule_manifold
;
754 struct capsule_manifold
762 * Expand a line manifold with a new pair. t value is the time along segment
763 * on the oriented object which created this pair.
765 VG_STATIC
void rb_capsule_manifold( v3f pa
, v3f pb
, float t
, float r
,
766 capsule_manifold
*manifold
)
769 v3_sub( pa
, pb
, delta
);
771 if( v3_length2(delta
) < r
*r
)
773 if( t
< manifold
->t0
)
775 v3_copy( delta
, manifold
->d0
);
780 if( t
> manifold
->t1
)
782 v3_copy( delta
, manifold
->d1
);
789 VG_STATIC
void rb_capsule_manifold_init( capsule_manifold
*manifold
)
791 manifold
->t0
= INFINITY
;
792 manifold
->t1
= -INFINITY
;
795 VG_STATIC
int rb_capsule_manifold_done( rigidbody
*rba
, rigidbody
*rbb
,
796 capsule_manifold
*manifold
, rb_ct
*buf
)
798 float h
= rba
->inf
.capsule
.height
,
799 ra
= rba
->inf
.capsule
.radius
;
802 v3_muladds( rba
->co
, rba
->up
, -h
*0.5f
+ra
, p0
);
803 v3_muladds( rba
->co
, rba
->up
, h
*0.5f
-ra
, p1
);
806 if( manifold
->t0
<= 1.0f
)
811 v3_muls( p0
, 1.0f
-manifold
->t0
, pa
);
812 v3_muladds( pa
, p1
, manifold
->t0
, pa
);
814 float d
= v3_length( manifold
->d0
);
815 v3_muls( manifold
->d0
, 1.0f
/d
, ct
->n
);
816 v3_muladds( pa
, ct
->n
, -ra
, ct
->co
);
818 ct
->p
= manifold
->r0
- d
;
821 ct
->type
= k_contact_type_default
;
826 if( (manifold
->t1
>= 0.0f
) && (manifold
->t0
!= manifold
->t1
) )
828 rb_ct
*ct
= buf
+count
;
831 v3_muls( p0
, 1.0f
-manifold
->t1
, pa
);
832 v3_muladds( pa
, p1
, manifold
->t1
, pa
);
834 float d
= v3_length( manifold
->d1
);
835 v3_muls( manifold
->d1
, 1.0f
/d
, ct
->n
);
836 v3_muladds( pa
, ct
->n
, -ra
, ct
->co
);
838 ct
->p
= manifold
->r1
- d
;
841 ct
->type
= k_contact_type_default
;
851 vg_line( buf
[0].co
, buf
[1].co
, 0xff0000ff );
856 VG_STATIC
int rb_capsule_sphere( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
858 float h
= rba
->inf
.capsule
.height
,
859 ra
= rba
->inf
.capsule
.radius
,
860 rb
= rbb
->inf
.sphere
.radius
;
863 v3_muladds( rba
->co
, rba
->up
, -h
*0.5f
+ra
, p0
);
864 v3_muladds( rba
->co
, rba
->up
, h
*0.5f
-ra
, p1
);
867 closest_point_segment( p0
, p1
, rbb
->co
, c
);
868 v3_sub( c
, rbb
->co
, delta
);
870 float d2
= v3_length2(delta
),
878 v3_muls( delta
, 1.0f
/d
, ct
->n
);
882 v3_muladds( c
, ct
->n
, -ra
, p0
);
883 v3_muladds( rbb
->co
, ct
->n
, rb
, p1
);
884 v3_add( p0
, p1
, ct
->co
);
885 v3_muls( ct
->co
, 0.5f
, ct
->co
);
889 ct
->type
= k_contact_type_default
;
897 VG_STATIC
int rb_capsule_capsule( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
899 float ha
= rba
->inf
.capsule
.height
,
900 hb
= rbb
->inf
.capsule
.height
,
901 ra
= rba
->inf
.capsule
.radius
,
902 rb
= rbb
->inf
.capsule
.radius
,
906 v3_muladds( rba
->co
, rba
->up
, -ha
*0.5f
+ra
, p0
);
907 v3_muladds( rba
->co
, rba
->up
, ha
*0.5f
-ra
, p1
);
908 v3_muladds( rbb
->co
, rbb
->up
, -hb
*0.5f
+rb
, p2
);
909 v3_muladds( rbb
->co
, rbb
->up
, hb
*0.5f
-rb
, p3
);
911 capsule_manifold manifold
;
912 rb_capsule_manifold_init( &manifold
);
916 closest_segment_segment( p0
, p1
, p2
, p3
, &ta
, &tb
, pa
, pb
);
917 rb_capsule_manifold( pa
, pb
, ta
, r
, &manifold
);
919 ta
= closest_point_segment( p0
, p1
, p2
, pa
);
920 tb
= closest_point_segment( p0
, p1
, p3
, pb
);
921 rb_capsule_manifold( pa
, p2
, ta
, r
, &manifold
);
922 rb_capsule_manifold( pb
, p3
, tb
, r
, &manifold
);
924 closest_point_segment( p2
, p3
, p0
, pa
);
925 closest_point_segment( p2
, p3
, p1
, pb
);
926 rb_capsule_manifold( p0
, pa
, 0.0f
, r
, &manifold
);
927 rb_capsule_manifold( p1
, pb
, 1.0f
, r
, &manifold
);
929 return rb_capsule_manifold_done( rba
, rbb
, &manifold
, buf
);
933 * Generates up to two contacts; optimised for the most stable manifold
935 VG_STATIC
int rb_capsule_box( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
937 float h
= rba
->inf
.capsule
.height
,
938 r
= rba
->inf
.capsule
.radius
;
941 * Solving this in symetric local space of the cube saves us some time and a
942 * couple branches when it comes to the quad stage.
945 v3_add( rbb
->bbx
[0], rbb
->bbx
[1], centroid
);
946 v3_muls( centroid
, 0.5f
, centroid
);
949 v3_sub( rbb
->bbx
[0], centroid
, bbx
[0] );
950 v3_sub( rbb
->bbx
[1], centroid
, bbx
[1] );
952 v3f pc
, p0w
, p1w
, p0
, p1
;
953 v3_muladds( rba
->co
, rba
->up
, -h
*0.5f
+r
, p0w
);
954 v3_muladds( rba
->co
, rba
->up
, h
*0.5f
-r
, p1w
);
956 m4x3_mulv( rbb
->to_local
, p0w
, p0
);
957 m4x3_mulv( rbb
->to_local
, p1w
, p1
);
958 v3_sub( p0
, centroid
, p0
);
959 v3_sub( p1
, centroid
, p1
);
960 v3_add( p0
, p1
, pc
);
961 v3_muls( pc
, 0.5f
, pc
);
964 * Finding an appropriate quad to collide lines with
967 v3_div( pc
, bbx
[1], region
);
970 if( (fabsf(region
[0]) > fabsf(region
[1])) &&
971 (fabsf(region
[0]) > fabsf(region
[2])) )
973 float px
= vg_signf(region
[0]) * bbx
[1][0];
974 v3_copy( (v3f
){ px
, bbx
[0][1], bbx
[0][2] }, quad
[0] );
975 v3_copy( (v3f
){ px
, bbx
[1][1], bbx
[0][2] }, quad
[1] );
976 v3_copy( (v3f
){ px
, bbx
[1][1], bbx
[1][2] }, quad
[2] );
977 v3_copy( (v3f
){ px
, bbx
[0][1], bbx
[1][2] }, quad
[3] );
979 else if( fabsf(region
[1]) > fabsf(region
[2]) )
981 float py
= vg_signf(region
[1]) * bbx
[1][1];
982 v3_copy( (v3f
){ bbx
[0][0], py
, bbx
[0][2] }, quad
[0] );
983 v3_copy( (v3f
){ bbx
[1][0], py
, bbx
[0][2] }, quad
[1] );
984 v3_copy( (v3f
){ bbx
[1][0], py
, bbx
[1][2] }, quad
[2] );
985 v3_copy( (v3f
){ bbx
[0][0], py
, bbx
[1][2] }, quad
[3] );
989 float pz
= vg_signf(region
[2]) * bbx
[1][2];
990 v3_copy( (v3f
){ bbx
[0][0], bbx
[0][1], pz
}, quad
[0] );
991 v3_copy( (v3f
){ bbx
[1][0], bbx
[0][1], pz
}, quad
[1] );
992 v3_copy( (v3f
){ bbx
[1][0], bbx
[1][1], pz
}, quad
[2] );
993 v3_copy( (v3f
){ bbx
[0][0], bbx
[1][1], pz
}, quad
[3] );
996 capsule_manifold manifold
;
997 rb_capsule_manifold_init( &manifold
);
1000 closest_point_aabb( p0
, bbx
, c0
);
1001 closest_point_aabb( p1
, bbx
, c1
);
1004 v3_sub( c0
, p0
, d0
);
1005 v3_sub( c1
, p1
, d1
);
1006 v3_sub( p1
, p0
, da
);
1012 if( v3_dot( da
, d0
) <= 0.01f
)
1013 rb_capsule_manifold( p0
, c0
, 0.0f
, r
, &manifold
);
1015 if( v3_dot( da
, d1
) >= -0.01f
)
1016 rb_capsule_manifold( p1
, c1
, 1.0f
, r
, &manifold
);
1018 for( int i
=0; i
<4; i
++ )
1025 closest_segment_segment( p0
, p1
, quad
[i0
], quad
[i1
], &ta
, &tb
, ca
, cb
);
1026 rb_capsule_manifold( ca
, cb
, ta
, r
, &manifold
);
1030 * Create final contacts based on line manifold
1032 m3x3_mulv( rbb
->to_world
, manifold
.d0
, manifold
.d0
);
1033 m3x3_mulv( rbb
->to_world
, manifold
.d1
, manifold
.d1
);
1040 for( int i
=0; i
<4; i
++ )
1046 v3_add( quad
[i0
], centroid
, q0
);
1047 v3_add( quad
[i1
], centroid
, q1
);
1049 m4x3_mulv( rbb
->to_world
, q0
, q0
);
1050 m4x3_mulv( rbb
->to_world
, q1
, q1
);
1052 vg_line( q0
, q1
, 0xffffffff );
1056 return rb_capsule_manifold_done( rba
, rbb
, &manifold
, buf
);
1059 VG_STATIC
int rb_sphere_box( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1063 closest_point_obb( rba
->co
, rbb
->bbx
, rbb
->to_world
, rbb
->to_local
, co
);
1064 v3_sub( rba
->co
, co
, delta
);
1066 float d2
= v3_length2(delta
),
1067 r
= rba
->inf
.sphere
.radius
;
1076 v3_sub( rba
->co
, rbb
->co
, delta
);
1079 * some extra testing is required to find the best axis to push the
1080 * object back outside the box. Since there isnt a clear seperating
1081 * vector already, especially on really high aspect boxes.
1083 float lx
= v3_dot( rbb
->right
, delta
),
1084 ly
= v3_dot( rbb
->up
, delta
),
1085 lz
= v3_dot( rbb
->forward
, delta
),
1086 px
= rbb
->bbx
[1][0] - fabsf(lx
),
1087 py
= rbb
->bbx
[1][1] - fabsf(ly
),
1088 pz
= rbb
->bbx
[1][2] - fabsf(lz
);
1090 if( px
< py
&& px
< pz
)
1091 v3_muls( rbb
->right
, vg_signf(lx
), ct
->n
);
1093 v3_muls( rbb
->up
, vg_signf(ly
), ct
->n
);
1095 v3_muls( rbb
->forward
, vg_signf(lz
), ct
->n
);
1097 v3_muladds( rba
->co
, ct
->n
, -r
, ct
->co
);
1103 v3_muls( delta
, 1.0f
/d
, ct
->n
);
1105 v3_copy( co
, ct
->co
);
1110 ct
->type
= k_contact_type_default
;
1117 VG_STATIC
int rb_sphere_sphere( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1120 v3_sub( rba
->co
, rbb
->co
, delta
);
1122 float d2
= v3_length2(delta
),
1123 r
= rba
->inf
.sphere
.radius
+ rbb
->inf
.sphere
.radius
;
1127 float d
= sqrtf(d2
);
1130 v3_muls( delta
, 1.0f
/d
, ct
->n
);
1133 v3_muladds( rba
->co
, ct
->n
,-rba
->inf
.sphere
.radius
, p0
);
1134 v3_muladds( rbb
->co
, ct
->n
, rbb
->inf
.sphere
.radius
, p1
);
1135 v3_add( p0
, p1
, ct
->co
);
1136 v3_muls( ct
->co
, 0.5f
, ct
->co
);
1137 ct
->type
= k_contact_type_default
;
1147 //#define RIGIDBODY_DYNAMIC_MESH_EDGES
1149 VG_STATIC
int rb_sphere_triangle( rigidbody
*rba
, rigidbody
*rbb
,
1150 v3f tri
[3], rb_ct
*buf
)
1154 #ifdef RIGIDBODY_DYNAMIC_MESH_EDGES
1155 closest_on_triangle_1( rba
->co
, tri
, co
);
1157 enum contact_type type
= closest_on_triangle_1( rba
->co
, tri
, co
);
1160 v3_sub( rba
->co
, co
, delta
);
1162 float d2
= v3_length2( delta
),
1163 r
= rba
->inf
.sphere
.radius
;
1170 v3_sub( tri
[2], tri
[0], ab
);
1171 v3_sub( tri
[1], tri
[0], ac
);
1172 v3_cross( ac
, ab
, tn
);
1173 v3_copy( tn
, ct
->n
);
1174 v3_normalize( ct
->n
);
1176 float d
= sqrtf(d2
);
1178 v3_copy( co
, ct
->co
);
1190 VG_STATIC
void rb_debug_sharp_scene_edges( rigidbody
*rbb
, float sharp_ang
,
1191 boxf box
, u32 colour
)
1193 sharp_ang
= cosf( sharp_ang
);
1195 scene
*sc
= rbb
->inf
.scene
.bh_scene
->user
;
1196 vg_line_boxf( box
, 0xff00ff00 );
1199 bh_iter_init( 0, &it
);
1202 while( bh_next( rbb
->inf
.scene
.bh_scene
, &it
, box
, &idx
) )
1204 u32
*ptri
= &sc
->arrindices
[ idx
*3 ];
1207 for( int j
=0; j
<3; j
++ )
1208 v3_copy( sc
->arrvertices
[ptri
[j
]].co
, tri
[j
] );
1210 for( int j
=0; j
<3; j
++ )
1214 v3_sub( tri
[(j
+1)%3], tri
[j
], edir
);
1216 if( v3_dot( edir
, (v3f
){ 0.5184758473652127f
,
1217 0.2073903389460850f
,
1218 -0.8295613557843402f
} ) < 0.0f
)
1223 bh_iter_init( 0, &jt
);
1226 float const k_r
= 0.02f
;
1227 v3_add( (v3f
){ k_r
, k_r
, k_r
}, tri
[j
], region
[1] );
1228 v3_add( (v3f
){ -k_r
, -k_r
, -k_r
}, tri
[j
], region
[0] );
1231 while( bh_next( rbb
->inf
.scene
.bh_scene
, &jt
, region
, &jdx
) )
1236 u32
*ptrj
= &sc
->arrindices
[ jdx
*3 ];
1239 for( int k
=0; k
<3; k
++ )
1240 v3_copy( sc
->arrvertices
[ptrj
[k
]].co
, trj
[k
] );
1242 for( int k
=0; k
<3; k
++ )
1244 if( v3_dist2( tri
[j
], trj
[k
] ) <= k_r
*k_r
)
1251 if( v3_dist2( tri
[jp1
], trj
[km1
] ) <= k_r
*k_r
)
1254 v3_sub( tri
[jp1
], tri
[j
], b0
);
1255 v3_sub( tri
[jp2
], tri
[j
], b1
);
1256 v3_sub( trj
[km2
], tri
[j
], b2
);
1259 v3_cross( b0
, b1
, cx0
);
1260 v3_cross( b2
, b0
, cx1
);
1262 float polarity
= v3_dot( cx0
, b2
);
1264 if( polarity
< 0.0f
)
1267 vg_line( tri
[j
], tri
[jp1
], 0xff00ff00 );
1268 float ang
= v3_dot(cx0
,cx1
) /
1269 (v3_length(cx0
)*v3_length(cx1
));
1270 if( ang
< sharp_ang
)
1272 vg_line( tri
[j
], tri
[jp1
], 0xff00ff00 );
1284 VG_STATIC
int rb_sphere_scene( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1286 scene
*sc
= rbb
->inf
.scene
.bh_scene
->user
;
1289 bh_iter_init( 0, &it
);
1294 while( bh_next( rbb
->inf
.scene
.bh_scene
, &it
, rba
->bbx_world
, &idx
) )
1296 u32
*ptri
= &sc
->arrindices
[ idx
*3 ];
1299 for( int j
=0; j
<3; j
++ )
1300 v3_copy( sc
->arrvertices
[ptri
[j
]].co
, tri
[j
] );
1302 buf
[ count
].element_id
= ptri
[0];
1304 vg_line( tri
[0],tri
[1],0x70ff6000 );
1305 vg_line( tri
[1],tri
[2],0x70ff6000 );
1306 vg_line( tri
[2],tri
[0],0x70ff6000 );
1308 int contact
= rb_sphere_triangle( rba
, rbb
, tri
, buf
+count
);
1313 vg_warn( "Exceeding sphere_vs_scene capacity. Geometry too dense!\n" );
1321 VG_STATIC
int rb_box_scene( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1323 scene
*sc
= rbb
->inf
.scene
.bh_scene
->user
;
1328 bh_iter_init( 0, &it
);
1333 while( bh_next( rbb
->inf
.scene
.bh_scene
, &it
, rba
->bbx_world
, &idx
) )
1335 u32
*ptri
= &sc
->arrindices
[ idx
*3 ];
1337 for( int j
=0; j
<3; j
++ )
1338 v3_copy( sc
->arrvertices
[ptri
[j
]].co
, tri
[j
] );
1340 if( rb_box_triangle_sat( rba
, tri
) )
1342 vg_line(tri
[0],tri
[1],0xff50ff00 );
1343 vg_line(tri
[1],tri
[2],0xff50ff00 );
1344 vg_line(tri
[2],tri
[0],0xff50ff00 );
1348 vg_line(tri
[0],tri
[1],0xff0000ff );
1349 vg_line(tri
[1],tri
[2],0xff0000ff );
1350 vg_line(tri
[2],tri
[0],0xff0000ff );
1356 v3_sub( tri
[1], tri
[0], v0
);
1357 v3_sub( tri
[2], tri
[0], v1
);
1358 v3_cross( v0
, v1
, n
);
1361 /* find best feature */
1362 float best
= v3_dot( rba
->right
, n
);
1365 float cy
= v3_dot( rba
->up
, n
);
1366 if( fabsf(cy
) > fabsf(best
) )
1372 float cz
= -v3_dot( rba
->forward
, n
);
1373 if( fabsf(cz
) > fabsf(best
) )
1383 float px
= best
> 0.0f
? rba
->bbx
[0][0]: rba
->bbx
[1][0];
1384 manifold
[0][0] = px
;
1385 manifold
[0][1] = rba
->bbx
[0][1];
1386 manifold
[0][2] = rba
->bbx
[0][2];
1387 manifold
[1][0] = px
;
1388 manifold
[1][1] = rba
->bbx
[1][1];
1389 manifold
[1][2] = rba
->bbx
[0][2];
1390 manifold
[2][0] = px
;
1391 manifold
[2][1] = rba
->bbx
[1][1];
1392 manifold
[2][2] = rba
->bbx
[1][2];
1393 manifold
[3][0] = px
;
1394 manifold
[3][1] = rba
->bbx
[0][1];
1395 manifold
[3][2] = rba
->bbx
[1][2];
1397 else if( axis
== 1 )
1399 float py
= best
> 0.0f
? rba
->bbx
[0][1]: rba
->bbx
[1][1];
1400 manifold
[0][0] = rba
->bbx
[0][0];
1401 manifold
[0][1] = py
;
1402 manifold
[0][2] = rba
->bbx
[0][2];
1403 manifold
[1][0] = rba
->bbx
[1][0];
1404 manifold
[1][1] = py
;
1405 manifold
[1][2] = rba
->bbx
[0][2];
1406 manifold
[2][0] = rba
->bbx
[1][0];
1407 manifold
[2][1] = py
;
1408 manifold
[2][2] = rba
->bbx
[1][2];
1409 manifold
[3][0] = rba
->bbx
[0][0];
1410 manifold
[3][1] = py
;
1411 manifold
[3][2] = rba
->bbx
[1][2];
1415 float pz
= best
> 0.0f
? rba
->bbx
[0][2]: rba
->bbx
[1][2];
1416 manifold
[0][0] = rba
->bbx
[0][0];
1417 manifold
[0][1] = rba
->bbx
[0][1];
1418 manifold
[0][2] = pz
;
1419 manifold
[1][0] = rba
->bbx
[1][0];
1420 manifold
[1][1] = rba
->bbx
[0][1];
1421 manifold
[1][2] = pz
;
1422 manifold
[2][0] = rba
->bbx
[1][0];
1423 manifold
[2][1] = rba
->bbx
[1][1];
1424 manifold
[2][2] = pz
;
1425 manifold
[3][0] = rba
->bbx
[0][0];
1426 manifold
[3][1] = rba
->bbx
[1][1];
1427 manifold
[3][2] = pz
;
1430 for( int j
=0; j
<4; j
++ )
1431 m4x3_mulv( rba
->to_world
, manifold
[j
], manifold
[j
] );
1433 vg_line( manifold
[0], manifold
[1], 0xffffffff );
1434 vg_line( manifold
[1], manifold
[2], 0xffffffff );
1435 vg_line( manifold
[2], manifold
[3], 0xffffffff );
1436 vg_line( manifold
[3], manifold
[0], 0xffffffff );
1438 for( int j
=0; j
<4; j
++ )
1440 rb_ct
*ct
= buf
+count
;
1442 v3_copy( manifold
[j
], ct
->co
);
1443 v3_copy( n
, ct
->n
);
1445 float l0
= v3_dot( tri
[0], n
),
1446 l1
= v3_dot( manifold
[j
], n
);
1448 ct
->p
= (l0
-l1
)*0.5f
;
1452 ct
->type
= k_contact_type_default
;
1464 VG_STATIC
int RB_MATRIX_ERROR( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1466 vg_error( "Collision type is unimplemented between types %d and %d\n",
1467 rba
->type
, rbb
->type
);
1472 VG_STATIC
int rb_sphere_capsule( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1474 return rb_capsule_sphere( rbb
, rba
, buf
);
1477 VG_STATIC
int rb_box_capsule( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1479 return rb_capsule_box( rbb
, rba
, buf
);
1482 VG_STATIC
int rb_box_sphere( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1484 return rb_sphere_box( rbb
, rba
, buf
);
1487 VG_STATIC
int rb_scene_box( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1489 return rb_box_scene( rbb
, rba
, buf
);
1492 VG_STATIC
int (*rb_jump_table
[4][4])( rigidbody
*a
, rigidbody
*b
, rb_ct
*buf
) =
1494 /* box */ /* Sphere */ /* Capsule */ /* Mesh */
1495 { RB_MATRIX_ERROR
, rb_box_sphere
, rb_box_capsule
, rb_box_scene
},
1496 { rb_sphere_box
, rb_sphere_sphere
, rb_sphere_capsule
, rb_sphere_scene
},
1497 { rb_capsule_box
, rb_capsule_sphere
, rb_capsule_capsule
, RB_MATRIX_ERROR
},
1498 { rb_scene_box
, RB_MATRIX_ERROR
, RB_MATRIX_ERROR
, RB_MATRIX_ERROR
}
1501 VG_STATIC
int rb_collide( rigidbody
*rba
, rigidbody
*rbb
)
1503 int (*collider_jump
)(rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1504 = rb_jump_table
[rba
->type
][rbb
->type
];
1507 * 12 is the maximum manifold size we can generate, so we are forced to abort
1508 * potentially checking any more.
1510 if( rb_contact_count
+ 12 > vg_list_size(rb_contact_buffer
) )
1512 vg_warn( "Too many contacts made in global collider buffer (%d of %d\n)",
1513 rb_contact_count
, vg_list_size(rb_contact_buffer
) );
1518 * FUTURE: Replace this with a more dedicated broad phase pass
1520 if( box_overlap( rba
->bbx_world
, rbb
->bbx_world
) )
1522 int count
= collider_jump( rba
, rbb
, rb_contact_buffer
+rb_contact_count
);
1523 rb_contact_count
+= count
;
1531 * -----------------------------------------------------------------------------
1533 * -----------------------------------------------------------------------------
1536 VG_STATIC
void rb_solver_reset(void)
1538 rb_contact_count
= 0;
1541 VG_STATIC rb_ct
*rb_global_ct(void)
1543 return rb_contact_buffer
+ rb_contact_count
;
1547 * Initializing things like tangent vectors
1549 VG_STATIC
void rb_presolve_contacts( rb_ct
*buffer
, int len
)
1551 for( int i
=0; i
<len
; i
++ )
1553 rb_ct
*ct
= &buffer
[i
];
1555 ct
->bias
= -0.2f
* k_rb_rate
* vg_minf( 0.0f
, -ct
->p
+k_penetration_slop
);
1556 rb_tangent_basis( ct
->n
, ct
->t
[0], ct
->t
[1] );
1559 ct
->type
= k_contact_type_default
;
1561 ct
->norm_impulse
= 0.0f
;
1562 ct
->tangent_impulse
[0] = 0.0f
;
1563 ct
->tangent_impulse
[1] = 0.0f
;
1565 v3f ra
, rb
, raCn
, rbCn
, raCt
, rbCt
;
1566 v3_sub( ct
->co
, ct
->rba
->co
, ra
);
1567 v3_sub( ct
->co
, ct
->rbb
->co
, rb
);
1568 v3_cross( ra
, ct
->n
, raCn
);
1569 v3_cross( rb
, ct
->n
, rbCn
);
1571 /* orient inverse inertia tensors */
1573 m3x3_mulv( ct
->rba
->iIw
, raCn
, raCnI
);
1574 m3x3_mulv( ct
->rbb
->iIw
, rbCn
, rbCnI
);
1576 ct
->normal_mass
= ct
->rba
->inv_mass
+ ct
->rbb
->inv_mass
;
1577 ct
->normal_mass
+= v3_dot( raCn
, raCnI
);
1578 ct
->normal_mass
+= v3_dot( rbCn
, rbCnI
);
1579 ct
->normal_mass
= 1.0f
/ct
->normal_mass
;
1581 for( int j
=0; j
<2; j
++ )
1584 v3_cross( ct
->t
[j
], ra
, raCt
);
1585 v3_cross( ct
->t
[j
], rb
, rbCt
);
1586 m3x3_mulv( ct
->rba
->iIw
, raCt
, raCtI
);
1587 m3x3_mulv( ct
->rbb
->iIw
, rbCt
, rbCtI
);
1589 ct
->tangent_mass
[j
] = ct
->rba
->inv_mass
+ ct
->rbb
->inv_mass
;
1590 ct
->tangent_mass
[j
] += v3_dot( raCt
, raCtI
);
1591 ct
->tangent_mass
[j
] += v3_dot( rbCt
, rbCtI
);
1592 ct
->tangent_mass
[j
] = 1.0f
/ct
->tangent_mass
[j
];
1595 rb_debug_contact( ct
);
1600 * Creates relative contact velocity vector, and offsets between each body
1602 VG_STATIC
void rb_rcv( rb_ct
*ct
, v3f rv
, v3f da
, v3f db
)
1604 rigidbody
*rba
= ct
->rba
,
1607 v3_sub( ct
->co
, rba
->co
, da
);
1608 v3_sub( ct
->co
, rbb
->co
, db
);
1611 v3_cross( rba
->w
, da
, rva
);
1612 v3_add( rba
->v
, rva
, rva
);
1613 v3_cross( rbb
->w
, db
, rvb
);
1614 v3_add( rbb
->v
, rvb
, rvb
);
1616 v3_sub( rva
, rvb
, rv
);
1620 * Apply impulse to object
1622 VG_STATIC
void rb_linear_impulse( rigidbody
*rb
, v3f delta
, v3f impulse
)
1625 v3_muladds( rb
->v
, impulse
, rb
->inv_mass
, rb
->v
);
1627 /* Angular velocity */
1629 v3_cross( delta
, impulse
, wa
);
1631 m3x3_mulv( rb
->iIw
, wa
, wa
);
1632 v3_add( rb
->w
, wa
, rb
->w
);
1636 * One iteration to solve the contact constraint
1638 VG_STATIC
void rb_solve_contacts( rb_ct
*buf
, int len
)
1640 for( int i
=0; i
<len
; i
++ )
1642 struct contact
*ct
= &buf
[i
];
1644 rigidbody
*rb
= ct
->rba
;
1647 rb_rcv( ct
, rv
, da
, db
);
1650 for( int j
=0; j
<2; j
++ )
1652 float f
= k_friction
* ct
->norm_impulse
,
1653 vt
= v3_dot( rv
, ct
->t
[j
] ),
1654 lambda
= ct
->tangent_mass
[j
] * -vt
;
1656 float temp
= ct
->tangent_impulse
[j
];
1657 ct
->tangent_impulse
[j
] = vg_clampf( temp
+ lambda
, -f
, f
);
1658 lambda
= ct
->tangent_impulse
[j
] - temp
;
1661 v3_muls( ct
->t
[j
], lambda
, impulse
);
1662 rb_linear_impulse( ct
->rba
, da
, impulse
);
1664 v3_muls( ct
->t
[j
], -lambda
, impulse
);
1665 rb_linear_impulse( ct
->rbb
, db
, impulse
);
1669 rb_rcv( ct
, rv
, da
, db
);
1670 float vn
= v3_dot( rv
, ct
->n
),
1671 lambda
= ct
->normal_mass
* (-vn
+ ct
->bias
);
1673 float temp
= ct
->norm_impulse
;
1674 ct
->norm_impulse
= vg_maxf( temp
+ lambda
, 0.0f
);
1675 lambda
= ct
->norm_impulse
- temp
;
1678 v3_muls( ct
->n
, lambda
, impulse
);
1679 rb_linear_impulse( ct
->rba
, da
, impulse
);
1681 v3_muls( ct
->n
, -lambda
, impulse
);
1682 rb_linear_impulse( ct
->rbb
, db
, impulse
);
1687 * -----------------------------------------------------------------------------
1689 * -----------------------------------------------------------------------------
1692 VG_STATIC
void draw_angle_limit( v3f c
, v3f major
, v3f minor
,
1693 float amin
, float amax
, float measured
,
1698 v3_muls( major
, f
, ay
);
1699 v3_muls( minor
, f
, ax
);
1701 for( int x
=0; x
<16; x
++ )
1703 float t0
= (float)x
/ 16.0f
,
1704 t1
= (float)(x
+1) / 16.0f
,
1705 a0
= vg_lerpf( amin
, amax
, t0
),
1706 a1
= vg_lerpf( amin
, amax
, t1
);
1709 v3_muladds( c
, ay
, cosf(a0
), p0
);
1710 v3_muladds( p0
, ax
, sinf(a0
), p0
);
1711 v3_muladds( c
, ay
, cosf(a1
), p1
);
1712 v3_muladds( p1
, ax
, sinf(a1
), p1
);
1714 vg_line( p0
, p1
, colour
);
1717 vg_line( c
, p0
, colour
);
1719 vg_line( c
, p1
, colour
);
1723 v3_muladds( c
, ay
, cosf(measured
)*1.2f
, p2
);
1724 v3_muladds( p2
, ax
, sinf(measured
)*1.2f
, p2
);
1725 vg_line( c
, p2
, colour
);
1728 VG_STATIC
void rb_debug_constraint_limits( rigidbody
*ra
, rigidbody
*rb
, v3f lca
,
1731 v3f ax
, ay
, az
, bx
, by
, bz
;
1732 m3x3_mulv( ra
->to_world
, (v3f
){1.0f
,0.0f
,0.0f
}, ax
);
1733 m3x3_mulv( ra
->to_world
, (v3f
){0.0f
,1.0f
,0.0f
}, ay
);
1734 m3x3_mulv( ra
->to_world
, (v3f
){0.0f
,0.0f
,1.0f
}, az
);
1735 m3x3_mulv( rb
->to_world
, (v3f
){1.0f
,0.0f
,0.0f
}, bx
);
1736 m3x3_mulv( rb
->to_world
, (v3f
){0.0f
,1.0f
,0.0f
}, by
);
1737 m3x3_mulv( rb
->to_world
, (v3f
){0.0f
,0.0f
,1.0f
}, bz
);
1740 px
[0] = v3_dot( ay
, by
);
1741 px
[1] = v3_dot( az
, by
);
1743 py
[0] = v3_dot( az
, bz
);
1744 py
[1] = v3_dot( ax
, bz
);
1746 pz
[0] = v3_dot( ax
, bx
);
1747 pz
[1] = v3_dot( ay
, bx
);
1749 float r0
= atan2f( px
[1], px
[0] ),
1750 r1
= atan2f( py
[1], py
[0] ),
1751 r2
= atan2f( pz
[1], pz
[0] );
1754 m4x3_mulv( ra
->to_world
, lca
, c
);
1755 draw_angle_limit( c
, ay
, az
, limits
[0][0], limits
[1][0], r0
, 0xff0000ff );
1756 draw_angle_limit( c
, az
, ax
, limits
[0][1], limits
[1][1], r1
, 0xff00ff00 );
1757 draw_angle_limit( c
, ax
, ay
, limits
[0][2], limits
[1][2], r2
, 0xffff0000 );
1760 VG_STATIC
void rb_limit_cure( rigidbody
*ra
, rigidbody
*rb
, v3f axis
, float d
)
1764 float avx
= v3_dot( ra
->w
, axis
) - v3_dot( rb
->w
, axis
);
1765 float joint_mass
= rb
->inv_mass
+ ra
->inv_mass
;
1766 joint_mass
= 1.0f
/joint_mass
;
1768 float bias
= (k_limit_bias
* k_rb_rate
) * d
,
1769 lambda
= -(avx
+ bias
) * joint_mass
;
1771 /* Angular velocity */
1773 v3_muls( axis
, lambda
* ra
->inv_mass
, wa
);
1774 v3_muls( axis
, -lambda
* rb
->inv_mass
, wb
);
1776 v3_add( ra
->w
, wa
, ra
->w
);
1777 v3_add( rb
->w
, wb
, rb
->w
);
1781 VG_STATIC
void rb_constraint_limits( rigidbody
*ra
, v3f lca
,
1782 rigidbody
*rb
, v3f lcb
, v3f limits
[2] )
1784 v3f ax
, ay
, az
, bx
, by
, bz
;
1785 m3x3_mulv( ra
->to_world
, (v3f
){1.0f
,0.0f
,0.0f
}, ax
);
1786 m3x3_mulv( ra
->to_world
, (v3f
){0.0f
,1.0f
,0.0f
}, ay
);
1787 m3x3_mulv( ra
->to_world
, (v3f
){0.0f
,0.0f
,1.0f
}, az
);
1788 m3x3_mulv( rb
->to_world
, (v3f
){1.0f
,0.0f
,0.0f
}, bx
);
1789 m3x3_mulv( rb
->to_world
, (v3f
){0.0f
,1.0f
,0.0f
}, by
);
1790 m3x3_mulv( rb
->to_world
, (v3f
){0.0f
,0.0f
,1.0f
}, bz
);
1793 px
[0] = v3_dot( ay
, by
);
1794 px
[1] = v3_dot( az
, by
);
1796 py
[0] = v3_dot( az
, bz
);
1797 py
[1] = v3_dot( ax
, bz
);
1799 pz
[0] = v3_dot( ax
, bx
);
1800 pz
[1] = v3_dot( ay
, bx
);
1802 float r0
= atan2f( px
[1], px
[0] ),
1803 r1
= atan2f( py
[1], py
[0] ),
1804 r2
= atan2f( pz
[1], pz
[0] );
1806 /* calculate angle deltas */
1807 float dx
= 0.0f
, dy
= 0.0f
, dz
= 0.0f
;
1809 if( r0
< limits
[0][0] ) dx
= limits
[0][0] - r0
;
1810 if( r0
> limits
[1][0] ) dx
= limits
[1][0] - r0
;
1811 if( r1
< limits
[0][1] ) dy
= limits
[0][1] - r1
;
1812 if( r1
> limits
[1][1] ) dy
= limits
[1][1] - r1
;
1813 if( r2
< limits
[0][2] ) dz
= limits
[0][2] - r2
;
1814 if( r2
> limits
[1][2] ) dz
= limits
[1][2] - r2
;
1817 m3x3_mulv( ra
->to_world
, lca
, wca
);
1818 m3x3_mulv( rb
->to_world
, lcb
, wcb
);
1820 rb_limit_cure( ra
, rb
, ax
, dx
);
1821 rb_limit_cure( ra
, rb
, ay
, dy
);
1822 rb_limit_cure( ra
, rb
, az
, dz
);
1825 VG_STATIC
void rb_debug_constraint_position( rigidbody
*ra
, v3f lca
,
1826 rigidbody
*rb
, v3f lcb
)
1829 m3x3_mulv( ra
->to_world
, lca
, wca
);
1830 m3x3_mulv( rb
->to_world
, lcb
, wcb
);
1833 v3_add( wca
, ra
->co
, p0
);
1834 v3_add( wcb
, rb
->co
, p1
);
1835 vg_line_pt3( p0
, 0.005f
, 0xffffff00 );
1836 vg_line_pt3( p1
, 0.005f
, 0xffffff00 );
1837 vg_line( p0
, p1
, 0xffffff00 );
1840 VG_STATIC
void rb_constraint_position( rigidbody
*ra
, v3f lca
,
1841 rigidbody
*rb
, v3f lcb
)
1843 /* C = (COa + Ra*LCa) - (COb + Rb*LCb) = 0 */
1845 m3x3_mulv( ra
->to_world
, lca
, wca
);
1846 m3x3_mulv( rb
->to_world
, lcb
, wcb
);
1849 v3_sub( ra
->v
, rb
->v
, rcv
);
1852 v3_cross( ra
->w
, wca
, rcv_Ra
);
1853 v3_cross( rb
->w
, wcb
, rcv_Rb
);
1854 v3_add( rcv_Ra
, rcv
, rcv
);
1855 v3_sub( rcv
, rcv_Rb
, rcv
);
1859 v3_add( wca
, ra
->co
, p0
);
1860 v3_add( wcb
, rb
->co
, p1
);
1861 v3_sub( p1
, p0
, delta
);
1863 float dist2
= v3_length2( delta
);
1865 if( dist2
> 0.00001f
)
1867 float dist
= sqrtf(dist2
);
1868 v3_muls( delta
, 1.0f
/dist
, delta
);
1870 float joint_mass
= rb
->inv_mass
+ ra
->inv_mass
;
1872 v3f raCn
, rbCn
, raCt
, rbCt
;
1873 v3_cross( wca
, delta
, raCn
);
1874 v3_cross( wcb
, delta
, rbCn
);
1876 /* orient inverse inertia tensors */
1878 m3x3_mulv( ra
->iIw
, raCn
, raCnI
);
1879 m3x3_mulv( rb
->iIw
, rbCn
, rbCnI
);
1880 joint_mass
+= v3_dot( raCn
, raCnI
);
1881 joint_mass
+= v3_dot( rbCn
, rbCnI
);
1882 joint_mass
= 1.0f
/joint_mass
;
1884 float vd
= v3_dot( rcv
, delta
),
1885 bias
= -(k_joint_bias
* k_rb_rate
) * dist
,
1886 lambda
= -(vd
+ bias
) * joint_mass
;
1889 v3_muls( delta
, lambda
, impulse
);
1890 rb_linear_impulse( ra
, wca
, impulse
);
1891 v3_muls( delta
, -lambda
, impulse
);
1892 rb_linear_impulse( rb
, wcb
, impulse
);
1895 v3_muladds( ra
->co
, delta
, dist
* k_joint_correction
, ra
->co
);
1896 v3_muladds( rb
->co
, delta
, -dist
* k_joint_correction
, rb
->co
);
1904 VG_STATIC
void rb_effect_simple_bouyency( rigidbody
*ra
, v4f plane
,
1905 float amt
, float drag
)
1908 float depth
= v3_dot( plane
, ra
->co
) - plane
[3],
1909 lambda
= vg_clampf( -depth
, 0.0f
, 1.0f
) * amt
;
1911 v3_muladds( ra
->v
, plane
, lambda
* k_rb_delta
, ra
->v
);
1914 v3_muls( ra
->v
, 1.0f
-(drag
*k_rb_delta
), ra
->v
);
1918 * -----------------------------------------------------------------------------
1919 * BVH implementation, this is ONLY for VG_STATIC rigidbodies, its to slow for
1921 * -----------------------------------------------------------------------------
1924 VG_STATIC
void rb_bh_expand_bound( void *user
, boxf bound
, u32 item_index
)
1926 rigidbody
*rb
= &((rigidbody
*)user
)[ item_index
];
1927 box_concat( bound
, rb
->bbx_world
);
1930 VG_STATIC
float rb_bh_centroid( void *user
, u32 item_index
, int axis
)
1932 rigidbody
*rb
= &((rigidbody
*)user
)[ item_index
];
1933 return (rb
->bbx_world
[axis
][0] + rb
->bbx_world
[1][axis
]) * 0.5f
;
1936 VG_STATIC
void rb_bh_swap( void *user
, u32 ia
, u32 ib
)
1938 rigidbody temp
, *rba
, *rbb
;
1939 rba
= &((rigidbody
*)user
)[ ia
];
1940 rbb
= &((rigidbody
*)user
)[ ib
];
1947 VG_STATIC
void rb_bh_debug( void *user
, u32 item_index
)
1949 rigidbody
*rb
= &((rigidbody
*)user
)[ item_index
];
1950 rb_debug( rb
, 0xff00ffff );
1953 VG_STATIC bh_system bh_system_rigidbodies
=
1955 .expand_bound
= rb_bh_expand_bound
,
1956 .item_centroid
= rb_bh_centroid
,
1957 .item_swap
= rb_bh_swap
,
1958 .item_debug
= rb_bh_debug
,
1962 #endif /* RIGIDBODY_H */