2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
6 * Resources: Box2D - Erin Catto
14 VG_STATIC
void rb_tangent_basis( v3f n
, v3f tx
, v3f ty
);
15 VG_STATIC bh_system bh_system_rigidbodies
;
21 * -----------------------------------------------------------------------------
23 * -----------------------------------------------------------------------------
27 k_rb_rate
= (1.0/VG_TIMESTEP_FIXED
),
28 k_rb_delta
= (1.0/k_rb_rate
),
30 k_damp_linear
= 0.05f
, /* scale velocity 1/(1+x) */
31 k_damp_angular
= 0.1f
, /* scale angular 1/(1+x) */
33 k_joint_bias
= 0.08f
, /* positional joints */
34 k_joint_correction
= 0.01f
,
35 k_penetration_slop
= 0.01f
,
36 k_inertia_scale
= 4.0f
;
39 * -----------------------------------------------------------------------------
40 * structure definitions
41 * -----------------------------------------------------------------------------
44 typedef struct rigidbody rigidbody
;
45 typedef struct contact rb_ct
;
55 k_rb_shape_sphere
= 1,
56 k_rb_shape_capsule
= 2,
83 v3f right
, up
, forward
;
90 /* inertia model and inverse world tensor */
94 m4x3f to_world
, to_local
;
97 VG_STATIC
struct contact
102 float p
, bias
, norm_impulse
, tangent_impulse
[2],
103 normal_mass
, tangent_mass
[2];
108 rb_contact_buffer
[256];
109 VG_STATIC
int rb_contact_count
= 0;
112 * -----------------------------------------------------------------------------
114 * -----------------------------------------------------------------------------
117 VG_STATIC
float sphere_volume( float radius
)
119 float r3
= radius
*radius
*radius
;
120 return (4.0f
/3.0f
) * VG_PIf
* r3
;
123 VG_STATIC
void rb_tangent_basis( v3f n
, v3f tx
, v3f ty
)
125 /* Compute tangent basis (box2d) */
126 if( fabsf( n
[0] ) >= 0.57735027f
)
140 v3_cross( n
, tx
, ty
);
144 * -----------------------------------------------------------------------------
146 * -----------------------------------------------------------------------------
149 VG_STATIC
void rb_debug_contact( rb_ct
*ct
)
152 v3_muladds( ct
->co
, ct
->n
, 0.1f
, p1
);
153 vg_line_pt3( ct
->co
, 0.025f
, 0xff0000ff );
154 vg_line( ct
->co
, p1
, 0xffffffff );
157 VG_STATIC
void debug_sphere( m4x3f m
, float radius
, u32 colour
)
159 v3f ly
= { 0.0f
, 0.0f
, radius
},
160 lx
= { 0.0f
, radius
, 0.0f
},
161 lz
= { 0.0f
, 0.0f
, radius
};
163 for( int i
=0; i
<16; i
++ )
165 float t
= ((float)(i
+1) * (1.0f
/16.0f
)) * VG_PIf
* 2.0f
,
169 v3f py
= { s
*radius
, 0.0f
, c
*radius
},
170 px
= { s
*radius
, c
*radius
, 0.0f
},
171 pz
= { 0.0f
, s
*radius
, c
*radius
};
173 v3f p0
, p1
, p2
, p3
, p4
, p5
;
174 m4x3_mulv( m
, py
, p0
);
175 m4x3_mulv( m
, ly
, p1
);
176 m4x3_mulv( m
, px
, p2
);
177 m4x3_mulv( m
, lx
, p3
);
178 m4x3_mulv( m
, pz
, p4
);
179 m4x3_mulv( m
, lz
, p5
);
181 vg_line( p0
, p1
, colour
== 0x00? 0xff00ff00: colour
);
182 vg_line( p2
, p3
, colour
== 0x00? 0xff0000ff: colour
);
183 vg_line( p4
, p5
, colour
== 0x00? 0xffff0000: colour
);
191 VG_STATIC
void debug_capsule( m4x3f m
, float radius
, float h
, u32 colour
)
193 v3f ly
= { 0.0f
, 0.0f
, radius
},
194 lx
= { 0.0f
, radius
, 0.0f
},
195 lz
= { 0.0f
, 0.0f
, radius
};
197 float s0
= sinf(0.0f
)*radius
,
198 c0
= cosf(0.0f
)*radius
;
200 v3f p0
, p1
, up
, right
, forward
;
201 m3x3_mulv( m
, (v3f
){0.0f
,1.0f
,0.0f
}, up
);
202 m3x3_mulv( m
, (v3f
){1.0f
,0.0f
,0.0f
}, right
);
203 m3x3_mulv( m
, (v3f
){0.0f
,0.0f
,-1.0f
}, forward
);
204 v3_muladds( m
[3], up
, -h
*0.5f
+radius
, p0
);
205 v3_muladds( m
[3], up
, h
*0.5f
-radius
, p1
);
208 v3_muladds( p0
, right
, radius
, a0
);
209 v3_muladds( p1
, right
, radius
, a1
);
210 v3_muladds( p0
, forward
, radius
, b0
);
211 v3_muladds( p1
, forward
, radius
, b1
);
212 vg_line( a0
, a1
, colour
);
213 vg_line( b0
, b1
, colour
);
215 v3_muladds( p0
, right
, -radius
, a0
);
216 v3_muladds( p1
, right
, -radius
, a1
);
217 v3_muladds( p0
, forward
, -radius
, b0
);
218 v3_muladds( p1
, forward
, -radius
, b1
);
219 vg_line( a0
, a1
, colour
);
220 vg_line( b0
, b1
, colour
);
222 for( int i
=0; i
<16; i
++ )
224 float t
= ((float)(i
+1) * (1.0f
/16.0f
)) * VG_PIf
* 2.0f
,
228 v3f e0
= { s0
, 0.0f
, c0
},
229 e1
= { s1
, 0.0f
, c1
},
230 e2
= { s0
, c0
, 0.0f
},
231 e3
= { s1
, c1
, 0.0f
},
232 e4
= { 0.0f
, c0
, s0
},
233 e5
= { 0.0f
, c1
, s1
};
235 m3x3_mulv( m
, e0
, e0
);
236 m3x3_mulv( m
, e1
, e1
);
237 m3x3_mulv( m
, e2
, e2
);
238 m3x3_mulv( m
, e3
, e3
);
239 m3x3_mulv( m
, e4
, e4
);
240 m3x3_mulv( m
, e5
, e5
);
242 v3_add( p0
, e0
, a0
);
243 v3_add( p0
, e1
, a1
);
244 v3_add( p1
, e0
, b0
);
245 v3_add( p1
, e1
, b1
);
247 vg_line( a0
, a1
, colour
);
248 vg_line( b0
, b1
, colour
);
252 v3_add( p0
, e2
, a0
);
253 v3_add( p0
, e3
, a1
);
254 v3_add( p0
, e4
, b0
);
255 v3_add( p0
, e5
, b1
);
259 v3_add( p1
, e2
, a0
);
260 v3_add( p1
, e3
, a1
);
261 v3_add( p1
, e4
, b0
);
262 v3_add( p1
, e5
, b1
);
265 vg_line( a0
, a1
, colour
);
266 vg_line( b0
, b1
, colour
);
273 VG_STATIC
void rb_debug( rigidbody
*rb
, u32 colour
)
275 if( rb
->type
== k_rb_shape_box
)
278 vg_line_boxf_transformed( rb
->to_world
, rb
->bbx
, colour
);
280 else if( rb
->type
== k_rb_shape_sphere
)
282 debug_sphere( rb
->to_world
, rb
->inf
.sphere
.radius
, colour
);
284 else if( rb
->type
== k_rb_shape_capsule
)
287 float h
= rb
->inf
.capsule
.height
,
288 r
= rb
->inf
.capsule
.radius
;
290 debug_capsule( rb
->to_world
, r
, h
, colour
);
292 else if( rb
->type
== k_rb_shape_scene
)
294 vg_line_boxf( rb
->bbx
, colour
);
299 * -----------------------------------------------------------------------------
301 * -----------------------------------------------------------------------------
305 * Update world space bounding box based on local one
307 VG_STATIC
void rb_update_bounds( rigidbody
*rb
)
309 box_copy( rb
->bbx
, rb
->bbx_world
);
310 m4x3_transform_aabb( rb
->to_world
, rb
->bbx_world
);
314 * Commit transform to rigidbody. Updates matrices
316 VG_STATIC
void rb_update_transform( rigidbody
*rb
)
318 q_normalize( rb
->q
);
319 q_m3x3( rb
->q
, rb
->to_world
);
320 v3_copy( rb
->co
, rb
->to_world
[3] );
322 m4x3_invert_affine( rb
->to_world
, rb
->to_local
);
324 m3x3_mulv( rb
->to_world
, (v3f
){1.0f
,0.0f
, 0.0f
}, rb
->right
);
325 m3x3_mulv( rb
->to_world
, (v3f
){0.0f
,1.0f
, 0.0f
}, rb
->up
);
326 m3x3_mulv( rb
->to_world
, (v3f
){0.0f
,0.0f
,-1.0f
}, rb
->forward
);
328 m3x3_mul( rb
->iI
, rb
->to_local
, rb
->iIw
);
329 m3x3_mul( rb
->to_world
, rb
->iIw
, rb
->iIw
);
331 rb_update_bounds( rb
);
335 * Extrapolate rigidbody into a transform based on vg accumulator.
336 * Useful for rendering
338 VG_STATIC
void rb_extrapolate_transform( rigidbody
*rb
, m4x3f transform
)
340 float substep
= vg_clampf( vg
.accumulator
/ k_rb_delta
, 0.0f
, 1.0f
);
345 v3_muladds( rb
->co
, rb
->v
, k_rb_delta
*substep
, co
);
347 if( v3_length2( rb
->w
) > 0.0f
)
351 v3_copy( rb
->w
, axis
);
353 float mag
= v3_length( axis
);
354 v3_divs( axis
, mag
, axis
);
355 q_axis_angle( rotation
, axis
, mag
*k_rb_delta
*substep
);
356 q_mul( rotation
, rb
->q
, q
);
364 q_m3x3( q
, transform
);
365 v3_copy( co
, transform
[3] );
369 * Initialize rigidbody and calculate masses, inertia
371 VG_STATIC
void rb_init( rigidbody
*rb
)
375 if( rb
->type
== k_rb_shape_box
)
378 v3_sub( rb
->bbx
[1], rb
->bbx
[0], dims
);
379 volume
= dims
[0]*dims
[1]*dims
[2];
381 else if( rb
->type
== k_rb_shape_sphere
)
383 volume
= sphere_volume( rb
->inf
.sphere
.radius
);
384 v3_fill( rb
->bbx
[0], -rb
->inf
.sphere
.radius
);
385 v3_fill( rb
->bbx
[1], rb
->inf
.sphere
.radius
);
387 else if( rb
->type
== k_rb_shape_capsule
)
389 float r
= rb
->inf
.capsule
.radius
,
390 h
= rb
->inf
.capsule
.height
;
391 volume
= sphere_volume( r
) + VG_PIf
* r
*r
* (h
- r
*2.0f
);
393 v3_fill( rb
->bbx
[0], -rb
->inf
.sphere
.radius
);
394 v3_fill( rb
->bbx
[1], rb
->inf
.sphere
.radius
);
398 else if( rb
->type
== k_rb_shape_scene
)
401 box_copy( rb
->inf
.scene
.bh_scene
->nodes
[0].bbx
, rb
->bbx
);
412 float mass
= 2.0f
*volume
;
413 rb
->inv_mass
= 1.0f
/mass
;
416 v3_sub( rb
->bbx
[1], rb
->bbx
[0], extent
);
417 v3_muls( extent
, 0.5f
, extent
);
419 /* local intertia tensor */
420 float scale
= k_inertia_scale
;
421 float ex2
= scale
*extent
[0]*extent
[0],
422 ey2
= scale
*extent
[1]*extent
[1],
423 ez2
= scale
*extent
[2]*extent
[2];
425 rb
->I
[0] = ((1.0f
/12.0f
) * mass
* (ey2
+ez2
));
426 rb
->I
[1] = ((1.0f
/12.0f
) * mass
* (ex2
+ez2
));
427 rb
->I
[2] = ((1.0f
/12.0f
) * mass
* (ex2
+ey2
));
429 m3x3_identity( rb
->iI
);
430 rb
->iI
[0][0] = rb
->I
[0];
431 rb
->iI
[1][1] = rb
->I
[1];
432 rb
->iI
[2][2] = rb
->I
[2];
433 m3x3_inv( rb
->iI
, rb
->iI
);
439 rb_update_transform( rb
);
442 VG_STATIC
void rb_iter( rigidbody
*rb
)
444 v3f gravity
= { 0.0f
, -9.8f
, 0.0f
};
445 v3_muladds( rb
->v
, gravity
, k_rb_delta
, rb
->v
);
447 /* intergrate velocity */
448 v3_muladds( rb
->co
, rb
->v
, k_rb_delta
, rb
->co
);
449 v3_lerp( rb
->w
, (v3f
){0.0f
,0.0f
,0.0f
}, 0.0025f
, rb
->w
);
451 /* inegrate inertia */
452 if( v3_length2( rb
->w
) > 0.0f
)
456 v3_copy( rb
->w
, axis
);
458 float mag
= v3_length( axis
);
459 v3_divs( axis
, mag
, axis
);
460 q_axis_angle( rotation
, axis
, mag
*k_rb_delta
);
461 q_mul( rotation
, rb
->q
, rb
->q
);
465 v3_muls( rb
->v
, 1.0f
/(1.0f
+k_rb_delta
*k_damp_linear
), rb
->v
);
466 v3_muls( rb
->w
, 1.0f
/(1.0f
+k_rb_delta
*k_damp_angular
), rb
->w
);
470 * -----------------------------------------------------------------------------
471 * Closest point functions
472 * -----------------------------------------------------------------------------
476 * These closest point tests were learned from Real-Time Collision Detection by
479 VG_STATIC
float closest_segment_segment( v3f p1
, v3f q1
, v3f p2
, v3f q2
,
480 float *s
, float *t
, v3f c1
, v3f c2
)
483 v3_sub( q1
, p1
, d1
);
484 v3_sub( q2
, p2
, d2
);
487 float a
= v3_length2( d1
),
488 e
= v3_length2( d2
),
491 const float kEpsilon
= 0.0001f
;
493 if( a
<= kEpsilon
&& e
<= kEpsilon
)
501 v3_sub( c1
, c2
, v0
);
503 return v3_length2( v0
);
509 *t
= vg_clampf( f
/ e
, 0.0f
, 1.0f
);
513 float c
= v3_dot( d1
, r
);
517 *s
= vg_clampf( -c
/ a
, 0.0f
, 1.0f
);
521 float b
= v3_dot(d1
,d2
),
526 *s
= vg_clampf((b
*f
- c
*e
)/d
, 0.0f
, 1.0f
);
538 *s
= vg_clampf( -c
/ a
, 0.0f
, 1.0f
);
543 *s
= vg_clampf((b
-c
)/a
,0.0f
,1.0f
);
548 v3_muladds( p1
, d1
, *s
, c1
);
549 v3_muladds( p2
, d2
, *t
, c2
);
552 v3_sub( c1
, c2
, v0
);
553 return v3_length2( v0
);
556 VG_STATIC
void closest_point_aabb( v3f p
, boxf box
, v3f dest
)
558 v3_maxv( p
, box
[0], dest
);
559 v3_minv( dest
, box
[1], dest
);
562 VG_STATIC
void closest_point_obb( v3f p
, rigidbody
*rb
, v3f dest
)
565 m4x3_mulv( rb
->to_local
, p
, local
);
566 closest_point_aabb( local
, rb
->bbx
, local
);
567 m4x3_mulv( rb
->to_world
, local
, dest
);
570 VG_STATIC
float closest_point_segment( v3f a
, v3f b
, v3f point
, v3f dest
)
574 v3_sub( point
, a
, v1
);
576 float t
= v3_dot( v1
, v0
) / v3_length2(v0
);
577 t
= vg_clampf(t
,0.0f
,1.0f
);
578 v3_muladds( a
, v0
, t
, dest
);
582 VG_STATIC
void closest_on_triangle( v3f p
, v3f tri
[3], v3f dest
)
587 /* Region outside A */
588 v3_sub( tri
[1], tri
[0], ab
);
589 v3_sub( tri
[2], tri
[0], ac
);
590 v3_sub( p
, tri
[0], ap
);
594 if( d1
<= 0.0f
&& d2
<= 0.0f
)
596 v3_copy( tri
[0], dest
);
597 v3_copy( (v3f
){INFINITY
,INFINITY
,INFINITY
}, dest
);
601 /* Region outside B */
605 v3_sub( p
, tri
[1], bp
);
606 d3
= v3_dot( ab
, bp
);
607 d4
= v3_dot( ac
, bp
);
609 if( d3
>= 0.0f
&& d4
<= d3
)
611 v3_copy( tri
[1], dest
);
612 v3_copy( (v3f
){INFINITY
,INFINITY
,INFINITY
}, dest
);
616 /* Edge region of AB */
617 float vc
= d1
*d4
- d3
*d2
;
618 if( vc
<= 0.0f
&& d1
>= 0.0f
&& d3
<= 0.0f
)
620 float v
= d1
/ (d1
-d3
);
621 v3_muladds( tri
[0], ab
, v
, dest
);
622 v3_copy( (v3f
){INFINITY
,INFINITY
,INFINITY
}, dest
);
626 /* Region outside C */
629 v3_sub( p
, tri
[2], cp
);
633 if( d6
>= 0.0f
&& d5
<= d6
)
635 v3_copy( tri
[2], dest
);
636 v3_copy( (v3f
){INFINITY
,INFINITY
,INFINITY
}, dest
);
641 float vb
= d5
*d2
- d1
*d6
;
642 if( vb
<= 0.0f
&& d2
>= 0.0f
&& d6
<= 0.0f
)
644 float w
= d2
/ (d2
-d6
);
645 v3_muladds( tri
[0], ac
, w
, dest
);
646 v3_copy( (v3f
){INFINITY
,INFINITY
,INFINITY
}, dest
);
651 float va
= d3
*d6
- d5
*d4
;
652 if( va
<= 0.0f
&& (d4
-d3
) >= 0.0f
&& (d5
-d6
) >= 0.0f
)
654 float w
= (d4
-d3
) / ((d4
-d3
) + (d5
-d6
));
656 v3_sub( tri
[2], tri
[1], bc
);
657 v3_muladds( tri
[1], bc
, w
, dest
);
658 v3_copy( (v3f
){INFINITY
,INFINITY
,INFINITY
}, dest
);
662 /* P inside region, Q via barycentric coordinates uvw */
663 float d
= 1.0f
/(va
+vb
+vc
),
667 v3_muladds( tri
[0], ab
, v
, dest
);
668 v3_muladds( dest
, ac
, w
, dest
);
671 VG_STATIC
void closest_on_triangle_1( v3f p
, v3f tri
[3], v3f dest
)
676 /* Region outside A */
677 v3_sub( tri
[1], tri
[0], ab
);
678 v3_sub( tri
[2], tri
[0], ac
);
679 v3_sub( p
, tri
[0], ap
);
683 if( d1
<= 0.0f
&& d2
<= 0.0f
)
685 v3_copy( tri
[0], dest
);
689 /* Region outside B */
693 v3_sub( p
, tri
[1], bp
);
694 d3
= v3_dot( ab
, bp
);
695 d4
= v3_dot( ac
, bp
);
697 if( d3
>= 0.0f
&& d4
<= d3
)
699 v3_copy( tri
[1], dest
);
703 /* Edge region of AB */
704 float vc
= d1
*d4
- d3
*d2
;
705 if( vc
<= 0.0f
&& d1
>= 0.0f
&& d3
<= 0.0f
)
707 float v
= d1
/ (d1
-d3
);
708 v3_muladds( tri
[0], ab
, v
, dest
);
712 /* Region outside C */
715 v3_sub( p
, tri
[2], cp
);
719 if( d6
>= 0.0f
&& d5
<= d6
)
721 v3_copy( tri
[2], dest
);
726 float vb
= d5
*d2
- d1
*d6
;
727 if( vb
<= 0.0f
&& d2
>= 0.0f
&& d6
<= 0.0f
)
729 float w
= d2
/ (d2
-d6
);
730 v3_muladds( tri
[0], ac
, w
, dest
);
735 float va
= d3
*d6
- d5
*d4
;
736 if( va
<= 0.0f
&& (d4
-d3
) >= 0.0f
&& (d5
-d6
) >= 0.0f
)
738 float w
= (d4
-d3
) / ((d4
-d3
) + (d5
-d6
));
740 v3_sub( tri
[2], tri
[1], bc
);
741 v3_muladds( tri
[1], bc
, w
, dest
);
745 /* P inside region, Q via barycentric coordinates uvw */
746 float d
= 1.0f
/(va
+vb
+vc
),
750 v3_muladds( tri
[0], ab
, v
, dest
);
751 v3_muladds( dest
, ac
, w
, dest
);
755 * -----------------------------------------------------------------------------
756 * Boolean shape overlap functions
757 * -----------------------------------------------------------------------------
761 * Project AABB, and triangle interval onto axis to check if they overlap
763 VG_STATIC
int rb_box_triangle_interval( v3f extent
, v3f axis
, v3f tri
[3] )
767 r
= extent
[0] * fabsf(axis
[0]) +
768 extent
[1] * fabsf(axis
[1]) +
769 extent
[2] * fabsf(axis
[2]),
771 p0
= v3_dot( axis
, tri
[0] ),
772 p1
= v3_dot( axis
, tri
[1] ),
773 p2
= v3_dot( axis
, tri
[2] ),
775 e
= vg_maxf(-vg_maxf(p0
,vg_maxf(p1
,p2
)), vg_minf(p0
,vg_minf(p1
,p2
)));
777 if( e
> r
) return 0;
782 * Seperating axis test box vs triangle
784 VG_STATIC
int rb_box_triangle_sat( rigidbody
*rba
, v3f tri_src
[3] )
789 v3_sub( rba
->bbx
[1], rba
->bbx
[0], extent
);
790 v3_muls( extent
, 0.5f
, extent
);
791 v3_add( rba
->bbx
[0], extent
, c
);
793 for( int i
=0; i
<3; i
++ )
795 m4x3_mulv( rba
->to_local
, tri_src
[i
], tri
[i
] );
796 v3_sub( tri
[i
], c
, tri
[i
] );
800 if(!rb_box_triangle_interval( extent
, (v3f
){1.0f
,0.0f
,0.0f
}, tri
)) return 0;
801 if(!rb_box_triangle_interval( extent
, (v3f
){0.0f
,1.0f
,0.0f
}, tri
)) return 0;
802 if(!rb_box_triangle_interval( extent
, (v3f
){0.0f
,0.0f
,1.0f
}, tri
)) return 0;
804 v3f v0
,v1
,v2
,n
, e0
,e1
,e2
;
805 v3_sub( tri
[1], tri
[0], v0
);
806 v3_sub( tri
[2], tri
[0], v1
);
807 v3_sub( tri
[2], tri
[1], v2
);
811 v3_cross( v0
, v1
, n
);
812 v3_cross( v0
, n
, e0
);
813 v3_cross( n
, v1
, e1
);
814 v3_cross( v2
, n
, e2
);
817 if(!rb_box_triangle_interval( extent
, n
, tri
)) return 0;
820 v3_cross( e0
, (v3f
){1.0f
,0.0f
,0.0f
}, axis
[0] );
821 v3_cross( e0
, (v3f
){0.0f
,1.0f
,0.0f
}, axis
[1] );
822 v3_cross( e0
, (v3f
){0.0f
,0.0f
,1.0f
}, axis
[2] );
823 v3_cross( e1
, (v3f
){1.0f
,0.0f
,0.0f
}, axis
[3] );
824 v3_cross( e1
, (v3f
){0.0f
,1.0f
,0.0f
}, axis
[4] );
825 v3_cross( e1
, (v3f
){0.0f
,0.0f
,1.0f
}, axis
[5] );
826 v3_cross( e2
, (v3f
){1.0f
,0.0f
,0.0f
}, axis
[6] );
827 v3_cross( e2
, (v3f
){0.0f
,1.0f
,0.0f
}, axis
[7] );
828 v3_cross( e2
, (v3f
){0.0f
,0.0f
,1.0f
}, axis
[8] );
830 for( int i
=0; i
<9; i
++ )
831 if(!rb_box_triangle_interval( extent
, axis
[i
], tri
)) return 0;
837 * -----------------------------------------------------------------------------
839 * -----------------------------------------------------------------------------
845 * These do not automatically allocate contacts, an appropriately sized
846 * buffer must be supplied. The function returns the size of the manifold
847 * which was generated.
849 * The values set on the contacts are: n, co, p, rba, rbb
853 * By collecting the minimum(time) and maximum(time) pairs of points, we
854 * build a reduced and stable exact manifold.
857 * rx: minimum distance of these points
858 * dx: the delta between the two points
860 * pairs will only ammend these if they are creating a collision
862 typedef struct capsule_manifold capsule_manifold
;
863 struct capsule_manifold
871 * Expand a line manifold with a new pair. t value is the time along segment
872 * on the oriented object which created this pair.
874 VG_STATIC
void rb_capsule_manifold( v3f pa
, v3f pb
, float t
, float r
,
875 capsule_manifold
*manifold
)
878 v3_sub( pa
, pb
, delta
);
880 if( v3_length2(delta
) < r
*r
)
882 if( t
< manifold
->t0
)
884 v3_copy( delta
, manifold
->d0
);
889 if( t
> manifold
->t1
)
891 v3_copy( delta
, manifold
->d1
);
898 VG_STATIC
void rb_capsule_manifold_init( capsule_manifold
*manifold
)
900 manifold
->t0
= INFINITY
;
901 manifold
->t1
= -INFINITY
;
904 VG_STATIC
int rb_capsule_manifold_done( rigidbody
*rba
, rigidbody
*rbb
,
905 capsule_manifold
*manifold
, rb_ct
*buf
)
907 float h
= rba
->inf
.capsule
.height
,
908 ra
= rba
->inf
.capsule
.radius
;
911 v3_muladds( rba
->co
, rba
->up
, -h
*0.5f
+ra
, p0
);
912 v3_muladds( rba
->co
, rba
->up
, h
*0.5f
-ra
, p1
);
915 if( manifold
->t0
<= 1.0f
)
920 v3_muls( p0
, 1.0f
-manifold
->t0
, pa
);
921 v3_muladds( pa
, p1
, manifold
->t0
, pa
);
923 float d
= v3_length( manifold
->d0
);
924 v3_muls( manifold
->d0
, 1.0f
/d
, ct
->n
);
925 v3_muladds( pa
, ct
->n
, -ra
, ct
->co
);
927 ct
->p
= manifold
->r0
- d
;
934 if( (manifold
->t1
>= 0.0f
) && (manifold
->t0
!= manifold
->t1
) )
936 rb_ct
*ct
= buf
+count
;
939 v3_muls( p0
, 1.0f
-manifold
->t1
, pa
);
940 v3_muladds( pa
, p1
, manifold
->t1
, pa
);
942 float d
= v3_length( manifold
->d1
);
943 v3_muls( manifold
->d1
, 1.0f
/d
, ct
->n
);
944 v3_muladds( pa
, ct
->n
, -ra
, ct
->co
);
946 ct
->p
= manifold
->r1
- d
;
958 vg_line( buf
[0].co
, buf
[1].co
, 0xff0000ff );
963 VG_STATIC
int rb_capsule_sphere( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
965 float h
= rba
->inf
.capsule
.height
,
966 ra
= rba
->inf
.capsule
.radius
,
967 rb
= rbb
->inf
.sphere
.radius
;
970 v3_muladds( rba
->co
, rba
->up
, -h
*0.5f
+ra
, p0
);
971 v3_muladds( rba
->co
, rba
->up
, h
*0.5f
-ra
, p1
);
974 closest_point_segment( p0
, p1
, rbb
->co
, c
);
975 v3_sub( c
, rbb
->co
, delta
);
977 float d2
= v3_length2(delta
),
985 v3_muls( delta
, 1.0f
/d
, ct
->n
);
989 v3_muladds( c
, ct
->n
, -ra
, p0
);
990 v3_muladds( rbb
->co
, ct
->n
, rb
, p1
);
991 v3_add( p0
, p1
, ct
->co
);
992 v3_muls( ct
->co
, 0.5f
, ct
->co
);
1003 VG_STATIC
int rb_capsule_capsule( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1005 float ha
= rba
->inf
.capsule
.height
,
1006 hb
= rbb
->inf
.capsule
.height
,
1007 ra
= rba
->inf
.capsule
.radius
,
1008 rb
= rbb
->inf
.capsule
.radius
,
1012 v3_muladds( rba
->co
, rba
->up
, -ha
*0.5f
+ra
, p0
);
1013 v3_muladds( rba
->co
, rba
->up
, ha
*0.5f
-ra
, p1
);
1014 v3_muladds( rbb
->co
, rbb
->up
, -hb
*0.5f
+rb
, p2
);
1015 v3_muladds( rbb
->co
, rbb
->up
, hb
*0.5f
-rb
, p3
);
1017 capsule_manifold manifold
;
1018 rb_capsule_manifold_init( &manifold
);
1022 closest_segment_segment( p0
, p1
, p2
, p3
, &ta
, &tb
, pa
, pb
);
1023 rb_capsule_manifold( pa
, pb
, ta
, r
, &manifold
);
1025 ta
= closest_point_segment( p0
, p1
, p2
, pa
);
1026 tb
= closest_point_segment( p0
, p1
, p3
, pb
);
1027 rb_capsule_manifold( pa
, p2
, ta
, r
, &manifold
);
1028 rb_capsule_manifold( pb
, p3
, tb
, r
, &manifold
);
1030 closest_point_segment( p2
, p3
, p0
, pa
);
1031 closest_point_segment( p2
, p3
, p1
, pb
);
1032 rb_capsule_manifold( p0
, pa
, 0.0f
, r
, &manifold
);
1033 rb_capsule_manifold( p1
, pb
, 1.0f
, r
, &manifold
);
1035 return rb_capsule_manifold_done( rba
, rbb
, &manifold
, buf
);
1039 * Generates up to two contacts; optimised for the most stable manifold
1041 VG_STATIC
int rb_capsule_box( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1043 float h
= rba
->inf
.capsule
.height
,
1044 r
= rba
->inf
.capsule
.radius
;
1047 * Solving this in symetric local space of the cube saves us some time and a
1048 * couple branches when it comes to the quad stage.
1051 v3_add( rbb
->bbx
[0], rbb
->bbx
[1], centroid
);
1052 v3_muls( centroid
, 0.5f
, centroid
);
1055 v3_sub( rbb
->bbx
[0], centroid
, bbx
[0] );
1056 v3_sub( rbb
->bbx
[1], centroid
, bbx
[1] );
1058 v3f pc
, p0w
, p1w
, p0
, p1
;
1059 v3_muladds( rba
->co
, rba
->up
, -h
*0.5f
+r
, p0w
);
1060 v3_muladds( rba
->co
, rba
->up
, h
*0.5f
-r
, p1w
);
1062 m4x3_mulv( rbb
->to_local
, p0w
, p0
);
1063 m4x3_mulv( rbb
->to_local
, p1w
, p1
);
1064 v3_sub( p0
, centroid
, p0
);
1065 v3_sub( p1
, centroid
, p1
);
1066 v3_add( p0
, p1
, pc
);
1067 v3_muls( pc
, 0.5f
, pc
);
1070 * Finding an appropriate quad to collide lines with
1073 v3_div( pc
, bbx
[1], region
);
1076 if( (fabsf(region
[0]) > fabsf(region
[1])) &&
1077 (fabsf(region
[0]) > fabsf(region
[2])) )
1079 float px
= vg_signf(region
[0]) * bbx
[1][0];
1080 v3_copy( (v3f
){ px
, bbx
[0][1], bbx
[0][2] }, quad
[0] );
1081 v3_copy( (v3f
){ px
, bbx
[1][1], bbx
[0][2] }, quad
[1] );
1082 v3_copy( (v3f
){ px
, bbx
[1][1], bbx
[1][2] }, quad
[2] );
1083 v3_copy( (v3f
){ px
, bbx
[0][1], bbx
[1][2] }, quad
[3] );
1085 else if( fabsf(region
[1]) > fabsf(region
[2]) )
1087 float py
= vg_signf(region
[1]) * bbx
[1][1];
1088 v3_copy( (v3f
){ bbx
[0][0], py
, bbx
[0][2] }, quad
[0] );
1089 v3_copy( (v3f
){ bbx
[1][0], py
, bbx
[0][2] }, quad
[1] );
1090 v3_copy( (v3f
){ bbx
[1][0], py
, bbx
[1][2] }, quad
[2] );
1091 v3_copy( (v3f
){ bbx
[0][0], py
, bbx
[1][2] }, quad
[3] );
1095 float pz
= vg_signf(region
[2]) * bbx
[1][2];
1096 v3_copy( (v3f
){ bbx
[0][0], bbx
[0][1], pz
}, quad
[0] );
1097 v3_copy( (v3f
){ bbx
[1][0], bbx
[0][1], pz
}, quad
[1] );
1098 v3_copy( (v3f
){ bbx
[1][0], bbx
[1][1], pz
}, quad
[2] );
1099 v3_copy( (v3f
){ bbx
[0][0], bbx
[1][1], pz
}, quad
[3] );
1102 capsule_manifold manifold
;
1103 rb_capsule_manifold_init( &manifold
);
1106 closest_point_aabb( p0
, bbx
, c0
);
1107 closest_point_aabb( p1
, bbx
, c1
);
1110 v3_sub( c0
, p0
, d0
);
1111 v3_sub( c1
, p1
, d1
);
1112 v3_sub( p1
, p0
, da
);
1118 if( v3_dot( da
, d0
) <= 0.01f
)
1119 rb_capsule_manifold( p0
, c0
, 0.0f
, r
, &manifold
);
1121 if( v3_dot( da
, d1
) >= -0.01f
)
1122 rb_capsule_manifold( p1
, c1
, 1.0f
, r
, &manifold
);
1124 for( int i
=0; i
<4; i
++ )
1131 closest_segment_segment( p0
, p1
, quad
[i0
], quad
[i1
], &ta
, &tb
, ca
, cb
);
1132 rb_capsule_manifold( ca
, cb
, ta
, r
, &manifold
);
1136 * Create final contacts based on line manifold
1138 m3x3_mulv( rbb
->to_world
, manifold
.d0
, manifold
.d0
);
1139 m3x3_mulv( rbb
->to_world
, manifold
.d1
, manifold
.d1
);
1146 for( int i
=0; i
<4; i
++ )
1152 v3_add( quad
[i0
], centroid
, q0
);
1153 v3_add( quad
[i1
], centroid
, q1
);
1155 m4x3_mulv( rbb
->to_world
, q0
, q0
);
1156 m4x3_mulv( rbb
->to_world
, q1
, q1
);
1158 vg_line( q0
, q1
, 0xffffffff );
1162 return rb_capsule_manifold_done( rba
, rbb
, &manifold
, buf
);
1165 VG_STATIC
int rb_sphere_box( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1169 closest_point_obb( rba
->co
, rbb
, co
);
1170 v3_sub( rba
->co
, co
, delta
);
1172 float d2
= v3_length2(delta
),
1173 r
= rba
->inf
.sphere
.radius
;
1182 v3_sub( rba
->co
, rbb
->co
, delta
);
1185 * some extra testing is required to find the best axis to push the
1186 * object back outside the box. Since there isnt a clear seperating
1187 * vector already, especially on really high aspect boxes.
1189 float lx
= v3_dot( rbb
->right
, delta
),
1190 ly
= v3_dot( rbb
->up
, delta
),
1191 lz
= v3_dot( rbb
->forward
, delta
),
1192 px
= rbb
->bbx
[1][0] - fabsf(lx
),
1193 py
= rbb
->bbx
[1][1] - fabsf(ly
),
1194 pz
= rbb
->bbx
[1][2] - fabsf(lz
);
1196 if( px
< py
&& px
< pz
)
1197 v3_muls( rbb
->right
, vg_signf(lx
), ct
->n
);
1199 v3_muls( rbb
->up
, vg_signf(ly
), ct
->n
);
1201 v3_muls( rbb
->forward
, vg_signf(lz
), ct
->n
);
1203 v3_muladds( rba
->co
, ct
->n
, -r
, ct
->co
);
1209 v3_muls( delta
, 1.0f
/d
, ct
->n
);
1211 v3_copy( co
, ct
->co
);
1222 VG_STATIC
int rb_sphere_sphere( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1225 v3_sub( rba
->co
, rbb
->co
, delta
);
1227 float d2
= v3_length2(delta
),
1228 r
= rba
->inf
.sphere
.radius
+ rbb
->inf
.sphere
.radius
;
1232 float d
= sqrtf(d2
);
1235 v3_muls( delta
, 1.0f
/d
, ct
->n
);
1238 v3_muladds( rba
->co
, ct
->n
,-rba
->inf
.sphere
.radius
, p0
);
1239 v3_muladds( rbb
->co
, ct
->n
, rbb
->inf
.sphere
.radius
, p1
);
1240 v3_add( p0
, p1
, ct
->co
);
1241 v3_muls( ct
->co
, 0.5f
, ct
->co
);
1251 #define RIGIDBODY_DYNAMIC_MESH_EDGES
1253 VG_STATIC
int rb_sphere_triangle( rigidbody
*rba
, rigidbody
*rbb
,
1254 v3f tri
[3], rb_ct
*buf
)
1258 #ifdef RIGIDBODY_DYNAMIC_MESH_EDGES
1259 closest_on_triangle( rba
->co
, tri
, co
);
1261 closest_on_triangle_1( rba
->co
, tri
, co
);
1264 v3_sub( rba
->co
, co
, delta
);
1266 vg_line( rba
->co
, co
, 0xffff0000 );
1267 vg_line_pt3( rba
->co
, 0.1f
, 0xff00ffff );
1269 float d2
= v3_length2( delta
),
1270 r
= rba
->inf
.sphere
.radius
;
1277 v3_sub( tri
[2], tri
[0], ab
);
1278 v3_sub( tri
[1], tri
[0], ac
);
1279 v3_cross( ac
, ab
, tn
);
1280 v3_copy( tn
, ct
->n
);
1281 v3_normalize( ct
->n
);
1283 float d
= sqrtf(d2
);
1285 v3_copy( co
, ct
->co
);
1295 VG_STATIC
int rb_sphere_scene( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1297 scene
*sc
= rbb
->inf
.scene
.bh_scene
->user
;
1301 int len
= bh_select( rbb
->inf
.scene
.bh_scene
, rba
->bbx_world
, geo
, 128 );
1304 #ifdef RIGIDBODY_DYNAMIC_MESH_EDGES
1305 /* !experimental! build edge array on the fly. time could be improved! */
1307 v3f co_picture
[128*3];
1312 int unique_cos
[3]; /* indexes co_picture array */
1319 /* create geometry picture */
1320 for( int i
=0; i
<len
; i
++ )
1322 u32
*tri_indices
= &sc
->arrindices
[ geo
[i
]*3 ];
1323 struct face_info
*inf
= &faces
[i
];
1324 inf
->element_id
= tri_indices
[0];
1327 for( int j
=0; j
<3; j
++ )
1329 struct mdl_vert
*pvert
= &sc
->arrvertices
[tri_indices
[j
]];
1331 for( int k
=0; k
<unique_cos
; k
++ )
1333 if( v3_dist( pvert
->co
, co_picture
[k
] ) < 0.01f
*0.01f
)
1335 inf
->unique_cos
[j
] = k
;
1340 inf
->unique_cos
[j
] = unique_cos
;
1341 v3_copy( pvert
->co
, co_picture
[ unique_cos
++ ] );
1346 v3_sub( co_picture
[inf
->unique_cos
[2]],
1347 co_picture
[inf
->unique_cos
[0]], ab
);
1349 v3_sub( co_picture
[inf
->unique_cos
[1]],
1350 co_picture
[inf
->unique_cos
[0]], ac
);
1351 v3_cross( ac
, ab
, inf
->normal
);
1352 v3_normalize( inf
->normal
);
1356 /* build edges brute force */
1357 int edge_picture
[ 128*3 ][4];
1358 int unique_edges
= 0;
1360 for( int i
=0; i
<len
; i
++ )
1362 struct face_info
*inf
= &faces
[i
];
1364 for( int j
=0; j
<3; j
++ )
1368 e0
= VG_MIN( inf
->unique_cos
[i0
], inf
->unique_cos
[i1
] ),
1369 e1
= VG_MAX( inf
->unique_cos
[i0
], inf
->unique_cos
[i1
] ),
1372 for( int k
=0; k
<unique_edges
; k
++ )
1374 int k0
= VG_MIN( edge_picture
[k
][0], edge_picture
[k
][1] ),
1375 k1
= VG_MAX( edge_picture
[k
][0], edge_picture
[k
][1] );
1378 if( (k0
== e0
) && (k1
== e1
) )
1380 edge_picture
[ k
][3] = i
;
1388 /* create new edge */
1389 edge_picture
[ unique_edges
][0] = inf
->unique_cos
[i0
];
1390 edge_picture
[ unique_edges
][1] = inf
->unique_cos
[i1
];
1392 edge_picture
[ unique_edges
][2] = i
;
1393 edge_picture
[ unique_edges
][3] = -1;
1403 for( int i
=0; i
<len
; i
++ )
1405 #ifdef RIGIDBODY_DYNAMIC_MESH_EDGES
1406 struct face_info
*inf
= &faces
[i
];
1408 float *v0
= co_picture
[inf
->unique_cos
[0]],
1409 *v1
= co_picture
[inf
->unique_cos
[1]],
1410 *v2
= co_picture
[inf
->unique_cos
[2]];
1412 v3_copy( v0
, tri
[0] );
1413 v3_copy( v1
, tri
[1] );
1414 v3_copy( v2
, tri
[2] );
1416 buf
[count
].element_id
= inf
->element_id
;
1418 u32
*ptri
= &sc
->arrindices
[ geo
[i
]*3 ];
1420 for( int j
=0; j
<3; j
++ )
1421 v3_copy( sc
->arrvertices
[ptri
[j
]].co
, tri
[j
] );
1423 buf
[count
].element_id
= ptri
[0];
1426 vg_line( tri
[0],tri
[1],0x10ffffff );
1427 vg_line( tri
[1],tri
[2],0x10ffffff );
1428 vg_line( tri
[2],tri
[0],0x10ffffff );
1430 int contact
= rb_sphere_triangle( rba
, rbb
, tri
, buf
+count
);
1432 #ifdef RIGIDBODY_DYNAMIC_MESH_EDGES
1440 vg_warn( "Exceeding sphere_vs_scene capacity. Geometry too dense!\n" );
1445 #ifdef RIGIDBODY_DYNAMIC_MESH_EDGES
1446 for( int i
=0; i
<unique_edges
; i
++ )
1448 int *edge
= edge_picture
[i
];
1453 struct face_info
*inf_i
= &faces
[edge
[2]],
1454 *inf_j
= &faces
[edge
[3]];
1456 if( inf_i
->collided
|| inf_j
->collided
)
1460 closest_point_segment( co_picture
[edge
[0]], co_picture
[edge
[1]],
1463 v3_sub( rba
->co
, co
, delta
);
1464 float d2
= v3_length2( delta
),
1465 r
= rba
->inf
.sphere
.radius
;
1469 float d
= sqrtf(d2
);
1471 v3_muls( delta
, 1.0f
/d
, delta
);
1472 float c0
= v3_dot( inf_i
->normal
, delta
),
1473 c1
= v3_dot( inf_j
->normal
, delta
);
1475 if( c0
< 0.0f
|| c1
< 0.0f
)
1478 rb_ct
*ct
= buf
+count
;
1480 v3_muls( inf_i
->normal
, c0
, ct
->n
);
1481 v3_muladds( ct
->n
, inf_j
->normal
, c1
, ct
->n
);
1482 v3_normalize( ct
->n
);
1484 v3_copy( co
, ct
->co
);
1488 ct
->element_id
= inf_i
->element_id
;
1494 vg_warn( "Geometry too dense!\n" );
1504 VG_STATIC
int rb_box_scene( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1506 scene
*sc
= rbb
->inf
.scene
.bh_scene
->user
;
1510 int len
= bh_select( rbb
->inf
.scene
.bh_scene
, rba
->bbx_world
, geo
, 128 );
1514 for( int i
=0; i
<len
; i
++ )
1516 u32
*ptri
= &sc
->arrindices
[ geo
[i
]*3 ];
1518 for( int j
=0; j
<3; j
++ )
1519 v3_copy( sc
->arrvertices
[ptri
[j
]].co
, tri
[j
] );
1521 if( rb_box_triangle_sat( rba
, tri
) )
1523 vg_line(tri
[0],tri
[1],0xff50ff00 );
1524 vg_line(tri
[1],tri
[2],0xff50ff00 );
1525 vg_line(tri
[2],tri
[0],0xff50ff00 );
1529 vg_line(tri
[0],tri
[1],0xff0000ff );
1530 vg_line(tri
[1],tri
[2],0xff0000ff );
1531 vg_line(tri
[2],tri
[0],0xff0000ff );
1537 v3_sub( tri
[1], tri
[0], v0
);
1538 v3_sub( tri
[2], tri
[0], v1
);
1539 v3_cross( v0
, v1
, n
);
1542 /* find best feature */
1543 float best
= v3_dot( rba
->right
, n
);
1546 float cy
= v3_dot( rba
->up
, n
);
1547 if( fabsf(cy
) > fabsf(best
) )
1553 float cz
= -v3_dot( rba
->forward
, n
);
1554 if( fabsf(cz
) > fabsf(best
) )
1564 float px
= best
> 0.0f
? rba
->bbx
[0][0]: rba
->bbx
[1][0];
1565 manifold
[0][0] = px
;
1566 manifold
[0][1] = rba
->bbx
[0][1];
1567 manifold
[0][2] = rba
->bbx
[0][2];
1568 manifold
[1][0] = px
;
1569 manifold
[1][1] = rba
->bbx
[1][1];
1570 manifold
[1][2] = rba
->bbx
[0][2];
1571 manifold
[2][0] = px
;
1572 manifold
[2][1] = rba
->bbx
[1][1];
1573 manifold
[2][2] = rba
->bbx
[1][2];
1574 manifold
[3][0] = px
;
1575 manifold
[3][1] = rba
->bbx
[0][1];
1576 manifold
[3][2] = rba
->bbx
[1][2];
1578 else if( axis
== 1 )
1580 float py
= best
> 0.0f
? rba
->bbx
[0][1]: rba
->bbx
[1][1];
1581 manifold
[0][0] = rba
->bbx
[0][0];
1582 manifold
[0][1] = py
;
1583 manifold
[0][2] = rba
->bbx
[0][2];
1584 manifold
[1][0] = rba
->bbx
[1][0];
1585 manifold
[1][1] = py
;
1586 manifold
[1][2] = rba
->bbx
[0][2];
1587 manifold
[2][0] = rba
->bbx
[1][0];
1588 manifold
[2][1] = py
;
1589 manifold
[2][2] = rba
->bbx
[1][2];
1590 manifold
[3][0] = rba
->bbx
[0][0];
1591 manifold
[3][1] = py
;
1592 manifold
[3][2] = rba
->bbx
[1][2];
1596 float pz
= best
> 0.0f
? rba
->bbx
[0][2]: rba
->bbx
[1][2];
1597 manifold
[0][0] = rba
->bbx
[0][0];
1598 manifold
[0][1] = rba
->bbx
[0][1];
1599 manifold
[0][2] = pz
;
1600 manifold
[1][0] = rba
->bbx
[1][0];
1601 manifold
[1][1] = rba
->bbx
[0][1];
1602 manifold
[1][2] = pz
;
1603 manifold
[2][0] = rba
->bbx
[1][0];
1604 manifold
[2][1] = rba
->bbx
[1][1];
1605 manifold
[2][2] = pz
;
1606 manifold
[3][0] = rba
->bbx
[0][0];
1607 manifold
[3][1] = rba
->bbx
[1][1];
1608 manifold
[3][2] = pz
;
1611 for( int j
=0; j
<4; j
++ )
1612 m4x3_mulv( rba
->to_world
, manifold
[j
], manifold
[j
] );
1614 vg_line( manifold
[0], manifold
[1], 0xffffffff );
1615 vg_line( manifold
[1], manifold
[2], 0xffffffff );
1616 vg_line( manifold
[2], manifold
[3], 0xffffffff );
1617 vg_line( manifold
[3], manifold
[0], 0xffffffff );
1619 for( int j
=0; j
<4; j
++ )
1621 rb_ct
*ct
= buf
+count
;
1623 v3_copy( manifold
[j
], ct
->co
);
1624 v3_copy( n
, ct
->n
);
1626 float l0
= v3_dot( tri
[0], n
),
1627 l1
= v3_dot( manifold
[j
], n
);
1629 ct
->p
= (l0
-l1
)*0.5f
;
1644 VG_STATIC
int RB_MATRIX_ERROR( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1646 vg_error( "Collision type is unimplemented between types %d and %d\n",
1647 rba
->type
, rbb
->type
);
1652 VG_STATIC
int rb_sphere_capsule( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1654 return rb_capsule_sphere( rbb
, rba
, buf
);
1657 VG_STATIC
int rb_box_capsule( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1659 return rb_capsule_box( rbb
, rba
, buf
);
1662 VG_STATIC
int rb_box_sphere( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1664 return rb_sphere_box( rbb
, rba
, buf
);
1667 VG_STATIC
int rb_scene_box( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1669 return rb_box_scene( rbb
, rba
, buf
);
1672 VG_STATIC
int (*rb_jump_table
[4][4])( rigidbody
*a
, rigidbody
*b
, rb_ct
*buf
) =
1674 /* box */ /* Sphere */ /* Capsule */ /* Mesh */
1675 { RB_MATRIX_ERROR
, rb_box_sphere
, rb_box_capsule
, rb_box_scene
},
1676 { rb_sphere_box
, rb_sphere_sphere
, rb_sphere_capsule
, rb_sphere_scene
},
1677 { rb_capsule_box
, rb_capsule_sphere
, rb_capsule_capsule
, RB_MATRIX_ERROR
},
1678 { rb_scene_box
, RB_MATRIX_ERROR
, RB_MATRIX_ERROR
, RB_MATRIX_ERROR
}
1681 VG_STATIC
int rb_collide( rigidbody
*rba
, rigidbody
*rbb
)
1683 int (*collider_jump
)(rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1684 = rb_jump_table
[rba
->type
][rbb
->type
];
1687 * 12 is the maximum manifold size we can generate, so we are forced to abort
1688 * potentially checking any more.
1690 if( rb_contact_count
+ 12 > vg_list_size(rb_contact_buffer
) )
1692 vg_warn( "Too many contacts made in global collider buffer (%d of %d\n)",
1693 rb_contact_count
, vg_list_size(rb_contact_buffer
) );
1698 * FUTURE: Replace this with a more dedicated broad phase pass
1700 if( box_overlap( rba
->bbx_world
, rbb
->bbx_world
) )
1702 int count
= collider_jump( rba
, rbb
, rb_contact_buffer
+rb_contact_count
);
1703 rb_contact_count
+= count
;
1711 * -----------------------------------------------------------------------------
1713 * -----------------------------------------------------------------------------
1716 VG_STATIC
void rb_solver_reset(void)
1718 rb_contact_count
= 0;
1721 VG_STATIC rb_ct
*rb_global_ct(void)
1723 return rb_contact_buffer
+ rb_contact_count
;
1727 * Initializing things like tangent vectors
1729 VG_STATIC
void rb_presolve_contacts( rb_ct
*buffer
, int len
)
1731 for( int i
=0; i
<len
; i
++ )
1733 rb_ct
*ct
= &buffer
[i
];
1734 ct
->bias
= -0.2f
* k_rb_rate
* vg_minf( 0.0f
, -ct
->p
+k_penetration_slop
);
1735 rb_tangent_basis( ct
->n
, ct
->t
[0], ct
->t
[1] );
1737 ct
->norm_impulse
= 0.0f
;
1738 ct
->tangent_impulse
[0] = 0.0f
;
1739 ct
->tangent_impulse
[1] = 0.0f
;
1741 v3f ra
, rb
, raCn
, rbCn
, raCt
, rbCt
;
1742 v3_sub( ct
->co
, ct
->rba
->co
, ra
);
1743 v3_sub( ct
->co
, ct
->rbb
->co
, rb
);
1744 v3_cross( ra
, ct
->n
, raCn
);
1745 v3_cross( rb
, ct
->n
, rbCn
);
1747 /* orient inverse inertia tensors */
1749 m3x3_mulv( ct
->rba
->iIw
, raCn
, raCnI
);
1750 m3x3_mulv( ct
->rbb
->iIw
, rbCn
, rbCnI
);
1752 ct
->normal_mass
= ct
->rba
->inv_mass
+ ct
->rbb
->inv_mass
;
1753 ct
->normal_mass
+= v3_dot( raCn
, raCnI
);
1754 ct
->normal_mass
+= v3_dot( rbCn
, rbCnI
);
1755 ct
->normal_mass
= 1.0f
/ct
->normal_mass
;
1757 for( int j
=0; j
<2; j
++ )
1760 v3_cross( ct
->t
[j
], ra
, raCt
);
1761 v3_cross( ct
->t
[j
], rb
, rbCt
);
1762 m3x3_mulv( ct
->rba
->iIw
, raCt
, raCtI
);
1763 m3x3_mulv( ct
->rbb
->iIw
, rbCt
, rbCtI
);
1765 ct
->tangent_mass
[j
] = ct
->rba
->inv_mass
+ ct
->rbb
->inv_mass
;
1766 ct
->tangent_mass
[j
] += v3_dot( raCt
, raCtI
);
1767 ct
->tangent_mass
[j
] += v3_dot( rbCt
, rbCtI
);
1768 ct
->tangent_mass
[j
] = 1.0f
/ct
->tangent_mass
[j
];
1771 rb_debug_contact( ct
);
1776 * Creates relative contact velocity vector, and offsets between each body
1778 VG_STATIC
void rb_rcv( rb_ct
*ct
, v3f rv
, v3f da
, v3f db
)
1780 rigidbody
*rba
= ct
->rba
,
1783 v3_sub( ct
->co
, rba
->co
, da
);
1784 v3_sub( ct
->co
, rbb
->co
, db
);
1787 v3_cross( rba
->w
, da
, rva
);
1788 v3_add( rba
->v
, rva
, rva
);
1789 v3_cross( rbb
->w
, db
, rvb
);
1790 v3_add( rbb
->v
, rvb
, rvb
);
1792 v3_sub( rva
, rvb
, rv
);
1796 * Apply impulse to object
1798 VG_STATIC
void rb_linear_impulse( rigidbody
*rb
, v3f delta
, v3f impulse
)
1801 v3_muladds( rb
->v
, impulse
, rb
->inv_mass
, rb
->v
);
1803 /* Angular velocity */
1805 v3_cross( delta
, impulse
, wa
);
1807 m3x3_mulv( rb
->iIw
, wa
, wa
);
1808 v3_add( rb
->w
, wa
, rb
->w
);
1812 * One iteration to solve the contact constraint
1814 VG_STATIC
void rb_solve_contacts( rb_ct
*buf
, int len
)
1816 for( int i
=0; i
<len
; i
++ )
1818 struct contact
*ct
= &buf
[i
];
1819 rigidbody
*rb
= ct
->rba
;
1822 rb_rcv( ct
, rv
, da
, db
);
1825 for( int j
=0; j
<2; j
++ )
1827 float f
= k_friction
* ct
->norm_impulse
,
1828 vt
= v3_dot( rv
, ct
->t
[j
] ),
1829 lambda
= ct
->tangent_mass
[j
] * -vt
;
1831 float temp
= ct
->tangent_impulse
[j
];
1832 ct
->tangent_impulse
[j
] = vg_clampf( temp
+ lambda
, -f
, f
);
1833 lambda
= ct
->tangent_impulse
[j
] - temp
;
1836 v3_muls( ct
->t
[j
], lambda
, impulse
);
1837 rb_linear_impulse( ct
->rba
, da
, impulse
);
1839 v3_muls( ct
->t
[j
], -lambda
, impulse
);
1840 rb_linear_impulse( ct
->rbb
, db
, impulse
);
1844 rb_rcv( ct
, rv
, da
, db
);
1845 float vn
= v3_dot( rv
, ct
->n
),
1846 lambda
= ct
->normal_mass
* (-vn
+ ct
->bias
);
1848 float temp
= ct
->norm_impulse
;
1849 ct
->norm_impulse
= vg_maxf( temp
+ lambda
, 0.0f
);
1850 lambda
= ct
->norm_impulse
- temp
;
1853 v3_muls( ct
->n
, lambda
, impulse
);
1854 rb_linear_impulse( ct
->rba
, da
, impulse
);
1856 v3_muls( ct
->n
, -lambda
, impulse
);
1857 rb_linear_impulse( ct
->rbb
, db
, impulse
);
1862 * -----------------------------------------------------------------------------
1864 * -----------------------------------------------------------------------------
1867 VG_STATIC
void draw_angle_limit( v3f c
, v3f major
, v3f minor
,
1868 float amin
, float amax
, float measured
,
1873 v3_muls( major
, f
, ay
);
1874 v3_muls( minor
, f
, ax
);
1876 for( int x
=0; x
<16; x
++ )
1878 float t0
= (float)x
/ 16.0f
,
1879 t1
= (float)(x
+1) / 16.0f
,
1880 a0
= vg_lerpf( amin
, amax
, t0
),
1881 a1
= vg_lerpf( amin
, amax
, t1
);
1884 v3_muladds( c
, ay
, cosf(a0
), p0
);
1885 v3_muladds( p0
, ax
, sinf(a0
), p0
);
1886 v3_muladds( c
, ay
, cosf(a1
), p1
);
1887 v3_muladds( p1
, ax
, sinf(a1
), p1
);
1889 vg_line( p0
, p1
, colour
);
1892 vg_line( c
, p0
, colour
);
1894 vg_line( c
, p1
, colour
);
1898 v3_muladds( c
, ay
, cosf(measured
)*1.2f
, p2
);
1899 v3_muladds( p2
, ax
, sinf(measured
)*1.2f
, p2
);
1900 vg_line( c
, p2
, colour
);
1903 VG_STATIC
void rb_debug_constraint_limits( rigidbody
*ra
, rigidbody
*rb
, v3f lca
,
1906 v3f ax
, ay
, az
, bx
, by
, bz
;
1907 m3x3_mulv( ra
->to_world
, (v3f
){1.0f
,0.0f
,0.0f
}, ax
);
1908 m3x3_mulv( ra
->to_world
, (v3f
){0.0f
,1.0f
,0.0f
}, ay
);
1909 m3x3_mulv( ra
->to_world
, (v3f
){0.0f
,0.0f
,1.0f
}, az
);
1910 m3x3_mulv( rb
->to_world
, (v3f
){1.0f
,0.0f
,0.0f
}, bx
);
1911 m3x3_mulv( rb
->to_world
, (v3f
){0.0f
,1.0f
,0.0f
}, by
);
1912 m3x3_mulv( rb
->to_world
, (v3f
){0.0f
,0.0f
,1.0f
}, bz
);
1915 px
[0] = v3_dot( ay
, by
);
1916 px
[1] = v3_dot( az
, by
);
1918 py
[0] = v3_dot( az
, bz
);
1919 py
[1] = v3_dot( ax
, bz
);
1921 pz
[0] = v3_dot( ax
, bx
);
1922 pz
[1] = v3_dot( ay
, bx
);
1924 float r0
= atan2f( px
[1], px
[0] ),
1925 r1
= atan2f( py
[1], py
[0] ),
1926 r2
= atan2f( pz
[1], pz
[0] );
1929 m4x3_mulv( ra
->to_world
, lca
, c
);
1930 draw_angle_limit( c
, ay
, az
, limits
[0][0], limits
[1][0], r0
, 0xff0000ff );
1931 draw_angle_limit( c
, az
, ax
, limits
[0][1], limits
[1][1], r1
, 0xff00ff00 );
1932 draw_angle_limit( c
, ax
, ay
, limits
[0][2], limits
[1][2], r2
, 0xffff0000 );
1935 VG_STATIC
void rb_limit_cure( rigidbody
*ra
, rigidbody
*rb
, v3f axis
, float d
)
1939 float avx
= v3_dot( ra
->w
, axis
) - v3_dot( rb
->w
, axis
);
1940 float joint_mass
= rb
->inv_mass
+ ra
->inv_mass
;
1941 joint_mass
= 1.0f
/joint_mass
;
1943 float bias
= (k_limit_bias
* k_rb_rate
) * d
,
1944 lambda
= -(avx
+ bias
) * joint_mass
;
1946 /* Angular velocity */
1948 v3_muls( axis
, lambda
* ra
->inv_mass
, wa
);
1949 v3_muls( axis
, -lambda
* rb
->inv_mass
, wb
);
1951 v3_add( ra
->w
, wa
, ra
->w
);
1952 v3_add( rb
->w
, wb
, rb
->w
);
1956 VG_STATIC
void rb_constraint_limits( rigidbody
*ra
, v3f lca
,
1957 rigidbody
*rb
, v3f lcb
, v3f limits
[2] )
1959 v3f ax
, ay
, az
, bx
, by
, bz
;
1960 m3x3_mulv( ra
->to_world
, (v3f
){1.0f
,0.0f
,0.0f
}, ax
);
1961 m3x3_mulv( ra
->to_world
, (v3f
){0.0f
,1.0f
,0.0f
}, ay
);
1962 m3x3_mulv( ra
->to_world
, (v3f
){0.0f
,0.0f
,1.0f
}, az
);
1963 m3x3_mulv( rb
->to_world
, (v3f
){1.0f
,0.0f
,0.0f
}, bx
);
1964 m3x3_mulv( rb
->to_world
, (v3f
){0.0f
,1.0f
,0.0f
}, by
);
1965 m3x3_mulv( rb
->to_world
, (v3f
){0.0f
,0.0f
,1.0f
}, bz
);
1968 px
[0] = v3_dot( ay
, by
);
1969 px
[1] = v3_dot( az
, by
);
1971 py
[0] = v3_dot( az
, bz
);
1972 py
[1] = v3_dot( ax
, bz
);
1974 pz
[0] = v3_dot( ax
, bx
);
1975 pz
[1] = v3_dot( ay
, bx
);
1977 float r0
= atan2f( px
[1], px
[0] ),
1978 r1
= atan2f( py
[1], py
[0] ),
1979 r2
= atan2f( pz
[1], pz
[0] );
1981 /* calculate angle deltas */
1982 float dx
= 0.0f
, dy
= 0.0f
, dz
= 0.0f
;
1984 if( r0
< limits
[0][0] ) dx
= limits
[0][0] - r0
;
1985 if( r0
> limits
[1][0] ) dx
= limits
[1][0] - r0
;
1986 if( r1
< limits
[0][1] ) dy
= limits
[0][1] - r1
;
1987 if( r1
> limits
[1][1] ) dy
= limits
[1][1] - r1
;
1988 if( r2
< limits
[0][2] ) dz
= limits
[0][2] - r2
;
1989 if( r2
> limits
[1][2] ) dz
= limits
[1][2] - r2
;
1992 m3x3_mulv( ra
->to_world
, lca
, wca
);
1993 m3x3_mulv( rb
->to_world
, lcb
, wcb
);
1995 rb_limit_cure( ra
, rb
, ax
, dx
);
1996 rb_limit_cure( ra
, rb
, ay
, dy
);
1997 rb_limit_cure( ra
, rb
, az
, dz
);
2000 VG_STATIC
void rb_debug_constraint_position( rigidbody
*ra
, v3f lca
,
2001 rigidbody
*rb
, v3f lcb
)
2004 m3x3_mulv( ra
->to_world
, lca
, wca
);
2005 m3x3_mulv( rb
->to_world
, lcb
, wcb
);
2008 v3_add( wca
, ra
->co
, p0
);
2009 v3_add( wcb
, rb
->co
, p1
);
2010 vg_line_pt3( p0
, 0.005f
, 0xffffff00 );
2011 vg_line_pt3( p1
, 0.005f
, 0xffffff00 );
2012 vg_line( p0
, p1
, 0xffffff00 );
2015 VG_STATIC
void rb_constraint_position( rigidbody
*ra
, v3f lca
,
2016 rigidbody
*rb
, v3f lcb
)
2018 /* C = (COa + Ra*LCa) - (COb + Rb*LCb) = 0 */
2020 m3x3_mulv( ra
->to_world
, lca
, wca
);
2021 m3x3_mulv( rb
->to_world
, lcb
, wcb
);
2024 v3_sub( ra
->v
, rb
->v
, rcv
);
2027 v3_cross( ra
->w
, wca
, rcv_Ra
);
2028 v3_cross( rb
->w
, wcb
, rcv_Rb
);
2029 v3_add( rcv_Ra
, rcv
, rcv
);
2030 v3_sub( rcv
, rcv_Rb
, rcv
);
2034 v3_add( wca
, ra
->co
, p0
);
2035 v3_add( wcb
, rb
->co
, p1
);
2036 v3_sub( p1
, p0
, delta
);
2038 float dist2
= v3_length2( delta
);
2040 if( dist2
> 0.00001f
)
2042 float dist
= sqrtf(dist2
);
2043 v3_muls( delta
, 1.0f
/dist
, delta
);
2045 float joint_mass
= rb
->inv_mass
+ ra
->inv_mass
;
2047 v3f raCn
, rbCn
, raCt
, rbCt
;
2048 v3_cross( wca
, delta
, raCn
);
2049 v3_cross( wcb
, delta
, rbCn
);
2051 /* orient inverse inertia tensors */
2053 m3x3_mulv( ra
->iIw
, raCn
, raCnI
);
2054 m3x3_mulv( rb
->iIw
, rbCn
, rbCnI
);
2055 joint_mass
+= v3_dot( raCn
, raCnI
);
2056 joint_mass
+= v3_dot( rbCn
, rbCnI
);
2057 joint_mass
= 1.0f
/joint_mass
;
2059 float vd
= v3_dot( rcv
, delta
),
2060 bias
= -(k_joint_bias
* k_rb_rate
) * dist
,
2061 lambda
= -(vd
+ bias
) * joint_mass
;
2064 v3_muls( delta
, lambda
, impulse
);
2065 rb_linear_impulse( ra
, wca
, impulse
);
2066 v3_muls( delta
, -lambda
, impulse
);
2067 rb_linear_impulse( rb
, wcb
, impulse
);
2070 v3_muladds( ra
->co
, delta
, dist
* k_joint_correction
, ra
->co
);
2071 v3_muladds( rb
->co
, delta
, -dist
* k_joint_correction
, rb
->co
);
2079 VG_STATIC
void rb_effect_simple_bouyency( rigidbody
*ra
, v4f plane
,
2080 float amt
, float drag
)
2083 float depth
= v3_dot( plane
, ra
->co
) - plane
[3],
2084 lambda
= vg_clampf( -depth
, 0.0f
, 1.0f
) * amt
;
2086 v3_muladds( ra
->v
, plane
, lambda
* k_rb_delta
, ra
->v
);
2089 v3_muls( ra
->v
, 1.0f
-(drag
*k_rb_delta
), ra
->v
);
2093 * -----------------------------------------------------------------------------
2094 * BVH implementation, this is ONLY for VG_STATIC rigidbodies, its to slow for
2096 * -----------------------------------------------------------------------------
2099 VG_STATIC
void rb_bh_expand_bound( void *user
, boxf bound
, u32 item_index
)
2101 rigidbody
*rb
= &((rigidbody
*)user
)[ item_index
];
2102 box_concat( bound
, rb
->bbx_world
);
2105 VG_STATIC
float rb_bh_centroid( void *user
, u32 item_index
, int axis
)
2107 rigidbody
*rb
= &((rigidbody
*)user
)[ item_index
];
2108 return (rb
->bbx_world
[axis
][0] + rb
->bbx_world
[1][axis
]) * 0.5f
;
2111 VG_STATIC
void rb_bh_swap( void *user
, u32 ia
, u32 ib
)
2113 rigidbody temp
, *rba
, *rbb
;
2114 rba
= &((rigidbody
*)user
)[ ia
];
2115 rbb
= &((rigidbody
*)user
)[ ib
];
2122 VG_STATIC
void rb_bh_debug( void *user
, u32 item_index
)
2124 rigidbody
*rb
= &((rigidbody
*)user
)[ item_index
];
2125 rb_debug( rb
, 0xff00ffff );
2128 VG_STATIC bh_system bh_system_rigidbodies
=
2130 .expand_bound
= rb_bh_expand_bound
,
2131 .item_centroid
= rb_bh_centroid
,
2132 .item_swap
= rb_bh_swap
,
2133 .item_debug
= rb_bh_debug
,
2137 #endif /* RIGIDBODY_H */