2 * Resources: Box2D - Erin Catto
9 static void rb_tangent_basis( v3f n
, v3f tx
, v3f ty
);
10 static bh_system bh_system_rigidbodies
;
16 #define k_rb_rate 60.0f
17 #define k_rb_delta (1.0f/k_rb_rate)
19 typedef struct rigidbody rigidbody
;
20 typedef struct contact rb_ct
;
51 v3f right
, up
, forward
;
58 v3f delta
; /* where is the origin of this in relation to a parent body */
59 m4x3f to_world
, to_local
;
62 static rigidbody rb_static_null
=
65 .q
={0.0f
,0.0f
,0.0f
,1.0f
},
72 static void rb_debug( rigidbody
*rb
, u32 colour
);
79 float mass_total
, p
, bias
, norm_impulse
, tangent_impulse
[2];
81 rb_contact_buffer
[256];
82 static int rb_contact_count
= 0;
84 static void rb_update_transform( rigidbody
*rb
)
87 q_m3x3( rb
->q
, rb
->to_world
);
88 v3_copy( rb
->co
, rb
->to_world
[3] );
90 m4x3_invert_affine( rb
->to_world
, rb
->to_local
);
92 box_copy( rb
->bbx
, rb
->bbx_world
);
93 m4x3_transform_aabb( rb
->to_world
, rb
->bbx_world
);
95 m3x3_mulv( rb
->to_world
, (v3f
){1.0f
,0.0f
, 0.0f
}, rb
->right
);
96 m3x3_mulv( rb
->to_world
, (v3f
){0.0f
,1.0f
, 0.0f
}, rb
->up
);
97 m3x3_mulv( rb
->to_world
, (v3f
){0.0f
,0.0f
,-1.0f
}, rb
->forward
);
100 static float sphere_volume( float radius
)
102 float r3
= radius
*radius
*radius
;
103 return (4.0f
/3.0f
) * VG_PIf
* r3
;
106 static void rb_init( rigidbody
*rb
)
110 if( rb
->type
== k_rb_shape_box
)
113 v3_sub( rb
->bbx
[1], rb
->bbx
[0], dims
);
114 volume
= dims
[0]*dims
[1]*dims
[2];
116 else if( rb
->type
== k_rb_shape_sphere
)
118 volume
= sphere_volume( rb
->inf
.sphere
.radius
);
119 v3_fill( rb
->bbx
[0], -rb
->inf
.sphere
.radius
);
120 v3_fill( rb
->bbx
[1], rb
->inf
.sphere
.radius
);
122 else if( rb
->type
== k_rb_shape_capsule
)
124 float r
= rb
->inf
.capsule
.radius
,
125 h
= rb
->inf
.capsule
.height
;
126 volume
= sphere_volume( r
) + VG_PIf
* r
*r
* (h
- r
*2.0f
);
128 v3_fill( rb
->bbx
[0], -rb
->inf
.sphere
.radius
);
129 v3_fill( rb
->bbx
[1], rb
->inf
.sphere
.radius
);
140 rb
->inv_mass
= 1.0f
/(8.0f
*volume
);
146 rb_update_transform( rb
);
149 static void rb_iter( rigidbody
*rb
)
151 v3f gravity
= { 0.0f
, -9.6f
, 0.0f
};
152 v3_muladds( rb
->v
, gravity
, k_rb_delta
, rb
->v
);
154 /* intergrate velocity */
155 v3_muladds( rb
->co
, rb
->v
, k_rb_delta
, rb
->co
);
156 v3_lerp( rb
->w
, (v3f
){0.0f
,0.0f
,0.0f
}, 0.0025f
, rb
->w
);
158 /* inegrate inertia */
159 if( v3_length2( rb
->w
) > 0.0f
)
163 v3_copy( rb
->w
, axis
);
165 float mag
= v3_length( axis
);
166 v3_divs( axis
, mag
, axis
);
167 q_axis_angle( rotation
, axis
, mag
*k_rb_delta
);
168 q_mul( rotation
, rb
->q
, rb
->q
);
172 static void rb_torque( rigidbody
*rb
, v3f axis
, float mag
)
174 v3_muladds( rb
->w
, axis
, mag
*k_rb_delta
, rb
->w
);
177 static void rb_tangent_basis( v3f n
, v3f tx
, v3f ty
)
179 /* Compute tangent basis (box2d) */
180 if( fabsf( n
[0] ) >= 0.57735027f
)
194 v3_cross( n
, tx
, ty
);
197 static void rb_solver_reset(void);
198 static void rb_build_manifold_terrain( rigidbody
*rb
);
199 static void rb_build_manifold_terrain_sphere( rigidbody
*rb
);
200 static void rb_solve_contacts( rb_ct
*buf
, int len
);
201 static void rb_presolve_contacts( rb_ct
*buffer
, int len
);
204 * These closest point tests were learned from Real-Time Collision Detection by
207 static float closest_segment_segment( v3f p1
, v3f q1
, v3f p2
, v3f q2
,
208 float *s
, float *t
, v3f c1
, v3f c2
)
211 v3_sub( q1
, p1
, d1
);
212 v3_sub( q2
, p2
, d2
);
215 float a
= v3_length2( d1
),
216 e
= v3_length2( d2
),
219 const float kEpsilon
= 0.0001f
;
221 if( a
<= kEpsilon
&& e
<= kEpsilon
)
229 v3_sub( c1
, c2
, v0
);
231 return v3_length2( v0
);
237 *t
= vg_clampf( f
/ e
, 0.0f
, 1.0f
);
241 float c
= v3_dot( d1
, r
);
245 *s
= vg_clampf( -c
/ a
, 0.0f
, 1.0f
);
249 float b
= v3_dot(d1
,d2
),
254 *s
= vg_clampf((b
*f
- c
*e
)/d
, 0.0f
, 1.0f
);
266 *s
= vg_clampf( -c
/ a
, 0.0f
, 1.0f
);
271 *s
= vg_clampf((b
-c
)/a
,0.0f
,1.0f
);
276 v3_muladds( p1
, d1
, *s
, c1
);
277 v3_muladds( p2
, d2
, *t
, c2
);
280 v3_sub( c1
, c2
, v0
);
281 return v3_length2( v0
);
284 static void closest_point_aabb( v3f p
, boxf box
, v3f dest
)
286 v3_maxv( p
, box
[0], dest
);
287 v3_minv( dest
, box
[1], dest
);
290 static void closest_point_obb( v3f p
, rigidbody
*rb
, v3f dest
)
293 m4x3_mulv( rb
->to_local
, p
, local
);
294 closest_point_aabb( local
, rb
->bbx
, local
);
295 m4x3_mulv( rb
->to_world
, local
, dest
);
298 static float closest_point_segment( v3f a
, v3f b
, v3f point
, v3f dest
)
302 v3_sub( point
, a
, v1
);
304 float t
= v3_dot( v1
, v0
) / v3_length2(v0
);
305 t
= vg_clampf(t
,0.0f
,1.0f
);
306 v3_muladds( a
, v0
, t
, dest
);
310 static void closest_on_triangle( v3f p
, v3f tri
[3], v3f dest
)
315 /* Region outside A */
316 v3_sub( tri
[1], tri
[0], ab
);
317 v3_sub( tri
[2], tri
[0], ac
);
318 v3_sub( p
, tri
[0], ap
);
322 if( d1
<= 0.0f
&& d2
<= 0.0f
)
324 v3_copy( tri
[0], dest
);
325 v3_copy( (v3f
){INFINITY
,INFINITY
,INFINITY
}, dest
);
329 /* Region outside B */
333 v3_sub( p
, tri
[1], bp
);
334 d3
= v3_dot( ab
, bp
);
335 d4
= v3_dot( ac
, bp
);
337 if( d3
>= 0.0f
&& d4
<= d3
)
339 v3_copy( tri
[1], dest
);
340 v3_copy( (v3f
){INFINITY
,INFINITY
,INFINITY
}, dest
);
344 /* Edge region of AB */
345 float vc
= d1
*d4
- d3
*d2
;
346 if( vc
<= 0.0f
&& d1
>= 0.0f
&& d3
<= 0.0f
)
348 float v
= d1
/ (d1
-d3
);
349 v3_muladds( tri
[0], ab
, v
, dest
);
350 v3_copy( (v3f
){INFINITY
,INFINITY
,INFINITY
}, dest
);
354 /* Region outside C */
357 v3_sub( p
, tri
[2], cp
);
361 if( d6
>= 0.0f
&& d5
<= d6
)
363 v3_copy( tri
[2], dest
);
364 v3_copy( (v3f
){INFINITY
,INFINITY
,INFINITY
}, dest
);
369 float vb
= d5
*d2
- d1
*d6
;
370 if( vb
<= 0.0f
&& d2
>= 0.0f
&& d6
<= 0.0f
)
372 float w
= d2
/ (d2
-d6
);
373 v3_muladds( tri
[0], ac
, w
, dest
);
374 v3_copy( (v3f
){INFINITY
,INFINITY
,INFINITY
}, dest
);
379 float va
= d3
*d6
- d5
*d4
;
380 if( va
<= 0.0f
&& (d4
-d3
) >= 0.0f
&& (d5
-d6
) >= 0.0f
)
382 float w
= (d4
-d3
) / ((d4
-d3
) + (d5
-d6
));
384 v3_sub( tri
[2], tri
[1], bc
);
385 v3_muladds( tri
[1], bc
, w
, dest
);
386 v3_copy( (v3f
){INFINITY
,INFINITY
,INFINITY
}, dest
);
390 /* P inside region, Q via barycentric coordinates uvw */
391 float d
= 1.0f
/(va
+vb
+vc
),
395 v3_muladds( tri
[0], ab
, v
, dest
);
396 v3_muladds( dest
, ac
, w
, dest
);
402 * These do not automatically allocate contacts, an appropriately sized
403 * buffer must be supplied. The function returns the size of the manifold
404 * which was generated.
406 * The values set on the contacts are: n, co, p, rba, rbb
409 static void rb_debug_contact( rb_ct
*ct
)
412 v3_muladds( ct
->co
, ct
->n
, 0.2f
, p1
);
413 vg_line_pt3( ct
->co
, 0.1f
, 0xff0000ff );
414 vg_line( ct
->co
, p1
, 0xffffffff );
418 * By collecting the minimum(time) and maximum(time) pairs of points, we
419 * build a reduced and stable exact manifold.
422 * rx: minimum distance of these points
423 * dx: the delta between the two points
425 * pairs will only ammend these if they are creating a collision
427 typedef struct capsule_manifold capsule_manifold
;
428 struct capsule_manifold
436 * Expand a line manifold with a new pair. t value is the time along segment
437 * on the oriented object which created this pair.
439 static void rb_capsule_manifold( v3f pa
, v3f pb
, float t
, float r
,
440 capsule_manifold
*manifold
)
443 v3_sub( pa
, pb
, delta
);
445 if( v3_length2(delta
) < r
*r
)
447 if( t
< manifold
->t0
)
449 v3_copy( delta
, manifold
->d0
);
454 if( t
> manifold
->t1
)
456 v3_copy( delta
, manifold
->d1
);
463 static void rb_capsule_manifold_init( capsule_manifold
*manifold
)
465 manifold
->t0
= INFINITY
;
466 manifold
->t1
= -INFINITY
;
469 static int rb_capsule_manifold_done( rigidbody
*rba
, rigidbody
*rbb
,
470 capsule_manifold
*manifold
, rb_ct
*buf
)
472 float h
= rba
->inf
.capsule
.height
,
473 ra
= rba
->inf
.capsule
.radius
;
476 v3_muladds( rba
->co
, rba
->up
, -h
*0.5f
+ra
, p0
);
477 v3_muladds( rba
->co
, rba
->up
, h
*0.5f
-ra
, p1
);
480 if( manifold
->t0
<= 1.0f
)
485 v3_muls( p0
, 1.0f
-manifold
->t0
, pa
);
486 v3_muladds( pa
, p1
, manifold
->t0
, pa
);
488 float d
= v3_length( manifold
->d0
);
489 v3_muls( manifold
->d0
, 1.0f
/d
, ct
->n
);
490 v3_muladds( pa
, ct
->n
, -ra
, ct
->co
);
492 ct
->p
= manifold
->r0
- d
;
499 if( (manifold
->t1
>= 0.0f
) && (manifold
->t0
!= manifold
->t1
) )
501 rb_ct
*ct
= buf
+count
;
504 v3_muls( p0
, 1.0f
-manifold
->t1
, pa
);
505 v3_muladds( pa
, p1
, manifold
->t1
, pa
);
507 float d
= v3_length( manifold
->d1
);
508 v3_muls( manifold
->d1
, 1.0f
/d
, ct
->n
);
509 v3_muladds( pa
, ct
->n
, -ra
, ct
->co
);
511 ct
->p
= manifold
->r1
- d
;
523 vg_line( buf
[0].co
, buf
[1].co
, 0xff0000ff );
528 static int rb_capsule_vs_sphere( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
530 float h
= rba
->inf
.capsule
.height
,
531 ra
= rba
->inf
.capsule
.radius
,
532 rb
= rbb
->inf
.sphere
.radius
;
535 v3_muladds( rba
->co
, rba
->up
, -h
*0.5f
+ra
, p0
);
536 v3_muladds( rba
->co
, rba
->up
, h
*0.5f
-ra
, p1
);
539 closest_point_segment( p0
, p1
, rbb
->co
, c
);
540 v3_sub( c
, rbb
->co
, delta
);
542 float d2
= v3_length2(delta
),
550 v3_muls( delta
, 1.0f
/d
, ct
->n
);
554 v3_muladds( c
, ct
->n
, -ra
, p0
);
555 v3_muladds( rbb
->co
, ct
->n
, rb
, p1
);
556 v3_add( p0
, p1
, ct
->co
);
557 v3_muls( ct
->co
, 0.5f
, ct
->co
);
568 static int rb_capsule_vs_capsule( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
570 float ha
= rba
->inf
.capsule
.height
,
571 hb
= rbb
->inf
.capsule
.height
,
572 ra
= rba
->inf
.capsule
.radius
,
573 rb
= rbb
->inf
.capsule
.radius
,
577 v3_muladds( rba
->co
, rba
->up
, -ha
*0.5f
+ra
, p0
);
578 v3_muladds( rba
->co
, rba
->up
, ha
*0.5f
-ra
, p1
);
579 v3_muladds( rbb
->co
, rbb
->up
, -hb
*0.5f
+rb
, p2
);
580 v3_muladds( rbb
->co
, rbb
->up
, hb
*0.5f
-rb
, p3
);
582 capsule_manifold manifold
;
583 rb_capsule_manifold_init( &manifold
);
587 closest_segment_segment( p0
, p1
, p2
, p3
, &ta
, &tb
, pa
, pb
);
588 rb_capsule_manifold( pa
, pb
, ta
, r
, &manifold
);
590 ta
= closest_point_segment( p0
, p1
, p2
, pa
);
591 tb
= closest_point_segment( p0
, p1
, p3
, pb
);
592 rb_capsule_manifold( pa
, p2
, ta
, r
, &manifold
);
593 rb_capsule_manifold( pb
, p3
, tb
, r
, &manifold
);
595 closest_point_segment( p2
, p3
, p0
, pa
);
596 closest_point_segment( p2
, p3
, p1
, pb
);
597 rb_capsule_manifold( p0
, pa
, 0.0f
, r
, &manifold
);
598 rb_capsule_manifold( p1
, pb
, 1.0f
, r
, &manifold
);
600 return rb_capsule_manifold_done( rba
, rbb
, &manifold
, buf
);
604 * Generates up to two contacts; optimised for the most stable manifold
606 static int rb_capsule_vs_box( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
608 float h
= rba
->inf
.capsule
.height
,
609 r
= rba
->inf
.capsule
.radius
;
612 * Solving this in symetric local space of the cube saves us some time and a
613 * couple branches when it comes to the quad stage.
616 v3_add( rbb
->bbx
[0], rbb
->bbx
[1], centroid
);
617 v3_muls( centroid
, 0.5f
, centroid
);
620 v3_sub( rbb
->bbx
[0], centroid
, bbx
[0] );
621 v3_sub( rbb
->bbx
[1], centroid
, bbx
[1] );
623 v3f pc
, p0w
, p1w
, p0
, p1
;
624 v3_muladds( rba
->co
, rba
->up
, -h
*0.5f
+r
, p0w
);
625 v3_muladds( rba
->co
, rba
->up
, h
*0.5f
-r
, p1w
);
627 m4x3_mulv( rbb
->to_local
, p0w
, p0
);
628 m4x3_mulv( rbb
->to_local
, p1w
, p1
);
629 v3_sub( p0
, centroid
, p0
);
630 v3_sub( p1
, centroid
, p1
);
631 v3_add( p0
, p1
, pc
);
632 v3_muls( pc
, 0.5f
, pc
);
635 * Finding an appropriate quad to collide lines with
638 v3_div( pc
, bbx
[1], region
);
641 if( (fabsf(region
[0]) > fabsf(region
[1])) &&
642 (fabsf(region
[0]) > fabsf(region
[2])) )
644 float px
= vg_signf(region
[0]) * bbx
[1][0];
645 v3_copy( (v3f
){ px
, bbx
[0][1], bbx
[0][2] }, quad
[0] );
646 v3_copy( (v3f
){ px
, bbx
[1][1], bbx
[0][2] }, quad
[1] );
647 v3_copy( (v3f
){ px
, bbx
[1][1], bbx
[1][2] }, quad
[2] );
648 v3_copy( (v3f
){ px
, bbx
[0][1], bbx
[1][2] }, quad
[3] );
650 else if( fabsf(region
[1]) > fabsf(region
[2]) )
652 float py
= vg_signf(region
[1]) * bbx
[1][1];
653 v3_copy( (v3f
){ bbx
[0][0], py
, bbx
[0][2] }, quad
[0] );
654 v3_copy( (v3f
){ bbx
[1][0], py
, bbx
[0][2] }, quad
[1] );
655 v3_copy( (v3f
){ bbx
[1][0], py
, bbx
[1][2] }, quad
[2] );
656 v3_copy( (v3f
){ bbx
[0][0], py
, bbx
[1][2] }, quad
[3] );
660 float pz
= vg_signf(region
[2]) * bbx
[1][2];
661 v3_copy( (v3f
){ bbx
[0][0], bbx
[0][1], pz
}, quad
[0] );
662 v3_copy( (v3f
){ bbx
[1][0], bbx
[0][1], pz
}, quad
[1] );
663 v3_copy( (v3f
){ bbx
[1][0], bbx
[1][1], pz
}, quad
[2] );
664 v3_copy( (v3f
){ bbx
[0][0], bbx
[1][1], pz
}, quad
[3] );
667 capsule_manifold manifold
;
668 rb_capsule_manifold_init( &manifold
);
671 closest_point_aabb( p0
, bbx
, c0
);
672 closest_point_aabb( p1
, bbx
, c1
);
675 v3_sub( c0
, p0
, d0
);
676 v3_sub( c1
, p1
, d1
);
677 v3_sub( p1
, p0
, da
);
684 if( v3_dot( da
, d0
) <= 0.01f
)
685 rb_capsule_manifold( p0
, c0
, 0.0f
, r
, &manifold
);
687 if( v3_dot( da
, d1
) >= -0.01f
)
688 rb_capsule_manifold( p1
, c1
, 1.0f
, r
, &manifold
);
690 for( int i
=0; i
<4; i
++ )
697 closest_segment_segment( p0
, p1
, quad
[i0
], quad
[i1
], &ta
, &tb
, ca
, cb
);
698 rb_capsule_manifold( ca
, cb
, ta
, r
, &manifold
);
702 * Create final contacts based on line manifold
704 m3x3_mulv( rbb
->to_world
, manifold
.d0
, manifold
.d0
);
705 m3x3_mulv( rbb
->to_world
, manifold
.d1
, manifold
.d1
);
712 for( int i
=0; i
<4; i
++ )
718 v3_add( quad
[i0
], centroid
, q0
);
719 v3_add( quad
[i1
], centroid
, q1
);
721 m4x3_mulv( rbb
->to_world
, q0
, q0
);
722 m4x3_mulv( rbb
->to_world
, q1
, q1
);
724 vg_line( q0
, q1
, 0xffffffff );
728 return rb_capsule_manifold_done( rba
, rbb
, &manifold
, buf
);
731 static int rb_sphere_vs_box( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
735 closest_point_obb( rba
->co
, rbb
, co
);
736 v3_sub( rba
->co
, co
, delta
);
738 float d2
= v3_length2(delta
),
739 r
= rba
->inf
.sphere
.radius
;
748 v3_sub( rba
->co
, rbb
->co
, delta
);
751 * some extra testing is required to find the best axis to push the
752 * object back outside the box. Since there isnt a clear seperating
753 * vector already, especially on really high aspect boxes.
755 float lx
= v3_dot( rbb
->right
, delta
),
756 ly
= v3_dot( rbb
->up
, delta
),
757 lz
= v3_dot( rbb
->forward
, delta
),
758 px
= rbb
->bbx
[1][0] - fabsf(lx
),
759 py
= rbb
->bbx
[1][1] - fabsf(ly
),
760 pz
= rbb
->bbx
[1][2] - fabsf(lz
);
762 if( px
< py
&& px
< pz
)
763 v3_muls( rbb
->right
, vg_signf(lx
), ct
->n
);
765 v3_muls( rbb
->up
, vg_signf(ly
), ct
->n
);
767 v3_muls( rbb
->forward
, vg_signf(lz
), ct
->n
);
769 v3_muladds( rba
->co
, ct
->n
, -r
, ct
->co
);
775 v3_muls( delta
, 1.0f
/d
, ct
->n
);
777 v3_copy( co
, ct
->co
);
788 static int rb_sphere_vs_sphere( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
791 v3_sub( rba
->co
, rbb
->co
, delta
);
793 float d2
= v3_length2(delta
),
794 r
= rba
->inf
.sphere
.radius
+ rbb
->inf
.sphere
.radius
;
801 v3_muls( delta
, 1.0f
/d
, ct
->n
);
804 v3_muladds( rba
->co
, ct
->n
,-rba
->inf
.sphere
.radius
, p0
);
805 v3_muladds( rbb
->co
, ct
->n
, rbb
->inf
.sphere
.radius
, p1
);
806 v3_add( p0
, p1
, ct
->co
);
807 v3_muls( ct
->co
, 0.5f
, ct
->co
);
817 static int RB_MATRIX_ERROR( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
819 vg_error( "Collision type is unimplemented between types %d and %d\n",
820 rba
->type
, rbb
->type
);
825 static int rb_sphere_vs_capsule( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
827 return rb_capsule_vs_sphere( rbb
, rba
, buf
);
830 static int rb_box_vs_capsule( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
832 return rb_capsule_vs_box( rbb
, rba
, buf
);
835 static int rb_box_vs_sphere( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
837 return rb_sphere_vs_box( rbb
, rba
, buf
);
840 static int (*rb_jump_table
[4][4])( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
842 /* box */ /* Sphere */ /* Capsule */
843 /*box */ { RB_MATRIX_ERROR
, rb_box_vs_sphere
, rb_box_vs_capsule
, RB_MATRIX_ERROR
},
844 /*sphere */ { rb_sphere_vs_box
, rb_sphere_vs_sphere
, rb_sphere_vs_capsule
, RB_MATRIX_ERROR
},
845 /*capsule*/ { rb_capsule_vs_box
,rb_capsule_vs_sphere
,rb_capsule_vs_capsule
,RB_MATRIX_ERROR
},
846 /*mesh */ { RB_MATRIX_ERROR
, RB_MATRIX_ERROR
, RB_MATRIX_ERROR
, RB_MATRIX_ERROR
}
855 * This function does not accept triangle as a dynamic object, it is assumed
856 * to always be static.
858 * The triangle is also assumed to be one sided for better detection
860 static int rb_sphere_vs_triangle( rigidbody
*rba
, v3f tri
[3], rb_ct
*buf
)
864 closest_on_triangle( rba
->co
, tri
, co
);
865 v3_sub( rba
->co
, co
, delta
);
867 float d2
= v3_length2( delta
),
868 r
= rba
->inf
.sphere
.radius
;
873 v3_sub( tri
[1], tri
[0], ab
);
874 v3_sub( tri
[2], tri
[0], ac
);
875 v3_cross( ac
, ab
, tn
);
877 if( v3_dot( delta
, tn
) > 0.0f
)
878 v3_muls( delta
, -1.0f
, delta
);
883 v3_muls( delta
, 1.0f
/d
, ct
->n
);
884 v3_copy( co
, ct
->co
);
887 ct
->rbb
= &rb_static_null
;
896 static int sphere_vs_triangle( v3f c
, float r
, v3f tri
[3],
897 v3f co
, v3f norm
, float *p
)
900 closest_on_triangle( c
, tri
, co
);
902 v3_sub( c
, co
, delta
);
905 float d
= v3_length2( delta
);
909 v3_sub( tri
[1], tri
[0], ab
);
910 v3_sub( tri
[2], tri
[0], ac
);
911 v3_cross( ac
, ab
, tn
);
913 if( v3_dot( delta
, tn
) > 0.0f
)
914 v3_muls( delta
, -1.0f
, delta
);
916 vg_line_pt3( co
, 0.05f
, 0xff00ff00 );
919 v3_muls( delta
, 1.0f
/d
, norm
);
930 static void rb_solver_reset(void)
932 rb_contact_count
= 0;
935 static rb_ct
*rb_global_ct(void)
937 return rb_contact_buffer
+ rb_contact_count
;
940 static struct contact
*rb_start_contact(void)
942 if( rb_contact_count
== vg_list_size(rb_contact_buffer
) )
944 vg_error( "rigidbody: too many contacts generated (%u)\n",
949 return &rb_contact_buffer
[ rb_contact_count
];
952 static void rb_commit_contact( struct contact
*ct
, float p
)
954 ct
->bias
= -0.2f
*k_rb_rate
*vg_minf(0.0f
,-p
+0.04f
);
955 rb_tangent_basis( ct
->n
, ct
->t
[0], ct
->t
[1] );
957 ct
->norm_impulse
= 0.0f
;
958 ct
->tangent_impulse
[0] = 0.0f
;
959 ct
->tangent_impulse
[1] = 0.0f
;
964 static void rb_build_manifold_terrain_sphere( rigidbody
*rb
)
968 int len
= bh_select( &world
.geo
.bhtris
, rb
->bbx_world
, geo
, 256 );
970 for( int i
=0; i
<len
; i
++ )
972 u32
*ptri
= &world
.geo
.indices
[ geo
[i
]*3 ];
974 for( int j
=0; j
<3; j
++ )
975 v3_copy( world
.geo
.verts
[ptri
[j
]].co
, tri
[j
] );
977 vg_line(tri
[0],tri
[1],0xff00ff00 );
978 vg_line(tri
[1],tri
[2],0xff00ff00 );
979 vg_line(tri
[2],tri
[0],0xff00ff00 );
984 for( int j
=0; j
<2; j
++ )
986 if( sphere_vs_triangle( rb
->co
, rb
->inf
.sphere
.radius
, tri
,co
,norm
,&p
))
988 struct contact
*ct
= rb_start_contact();
994 v3_muladds( rb
->co
, norm
, p
, p1
);
995 vg_line( rb
->co
, p1
, 0xffffffff );
998 v3_copy( co
, ct
->co
);
999 v3_copy( norm
, ct
->n
);
1000 rb_commit_contact( ct
, p
);
1008 static void rb_build_manifold_terrain( rigidbody
*rb
)
1012 float *p000
= pts
[0], *p001
= pts
[1], *p010
= pts
[2], *p011
= pts
[3],
1013 *p100
= pts
[4], *p101
= pts
[5], *p110
= pts
[6], *p111
= pts
[7];
1015 p000
[0]=box
[0][0];p000
[1]=box
[0][1];p000
[2]=box
[0][2];
1016 p001
[0]=box
[0][0];p001
[1]=box
[0][1];p001
[2]=box
[1][2];
1017 p010
[0]=box
[0][0];p010
[1]=box
[1][1];p010
[2]=box
[0][2];
1018 p011
[0]=box
[0][0];p011
[1]=box
[1][1];p011
[2]=box
[1][2];
1020 p100
[0]=box
[1][0];p100
[1]=box
[0][1];p100
[2]=box
[0][2];
1021 p101
[0]=box
[1][0];p101
[1]=box
[0][1];p101
[2]=box
[1][2];
1022 p110
[0]=box
[1][0];p110
[1]=box
[1][1];p110
[2]=box
[0][2];
1023 p111
[0]=box
[1][0];p111
[1]=box
[1][1];p111
[2]=box
[1][2];
1025 m4x3_mulv( rb
->to_world
, p000
, p000
);
1026 m4x3_mulv( rb
->to_world
, p001
, p001
);
1027 m4x3_mulv( rb
->to_world
, p010
, p010
);
1028 m4x3_mulv( rb
->to_world
, p011
, p011
);
1029 m4x3_mulv( rb
->to_world
, p100
, p100
);
1030 m4x3_mulv( rb
->to_world
, p101
, p101
);
1031 m4x3_mulv( rb
->to_world
, p110
, p110
);
1032 m4x3_mulv( rb
->to_world
, p111
, p111
);
1036 for( int i
=0; i
<8; i
++ )
1038 float *point
= pts
[i
];
1039 struct contact
*ct
= rb_start_contact();
1047 v3_copy( point
, surface
);
1051 hit
.dist
= INFINITY
;
1052 if( !ray_world( surface
, (v3f
){0.0f
,-1.0f
,0.0f
}, &hit
))
1055 v3_copy( hit
.pos
, surface
);
1057 float p
= vg_minf( surface
[1] - point
[1], 1.0f
);
1061 v3_copy( hit
.normal
, ct
->n
);
1062 v3_add( point
, surface
, ct
->co
);
1063 v3_muls( ct
->co
, 0.5f
, ct
->co
);
1065 rb_commit_contact( ct
, p
);
1074 * Initializing things like tangent vectors
1077 static void rb_presolve_contacts( rb_ct
*buffer
, int len
)
1079 for( int i
=0; i
<len
; i
++ )
1081 rb_ct
*ct
= &buffer
[i
];
1082 ct
->bias
= -0.2f
* k_rb_rate
* vg_minf(0.0f
,-ct
->p
+0.04f
);
1083 rb_tangent_basis( ct
->n
, ct
->t
[0], ct
->t
[1] );
1085 ct
->norm_impulse
= 0.0f
;
1086 ct
->tangent_impulse
[0] = 0.0f
;
1087 ct
->tangent_impulse
[1] = 0.0f
;
1088 ct
->mass_total
= 1.0f
/(ct
->rba
->inv_mass
+ ct
->rbb
->inv_mass
);
1090 rb_debug_contact( ct
);
1095 * Creates relative contact velocity vector, and offsets between each body
1097 static void rb_rcv( rb_ct
*ct
, v3f rv
, v3f da
, v3f db
)
1099 rigidbody
*rba
= ct
->rba
,
1102 v3_sub( ct
->co
, rba
->co
, da
);
1103 v3_sub( ct
->co
, rbb
->co
, db
);
1106 v3_cross( rba
->w
, da
, rva
);
1107 v3_add( rba
->v
, rva
, rva
);
1108 v3_cross( rbb
->w
, db
, rvb
);
1109 v3_add( rbb
->v
, rvb
, rvb
);
1111 v3_sub( rva
, rvb
, rv
);
1115 * Apply regular and angular velocity impulses to objects involved in contact
1117 static void rb_standard_impulse( rb_ct
*ct
, v3f da
, v3f db
, v3f impulse
)
1119 rigidbody
*rba
= ct
->rba
,
1123 v3_muls( impulse
, ct
->mass_total
*rba
->inv_mass
, ia
);
1124 v3_muls( impulse
, -ct
->mass_total
*rbb
->inv_mass
, ib
);
1127 v3_add( rba
->v
, ia
, rba
->v
);
1128 v3_add( rbb
->v
, ib
, rbb
->v
);
1130 /* Angular velocity */
1132 v3_cross( da
, ia
, wa
);
1133 v3_cross( db
, ib
, wb
);
1134 v3_add( rba
->w
, wa
, rba
->w
);
1135 v3_add( rbb
->w
, wb
, rbb
->w
);
1139 * One iteration to solve the contact constraint
1141 static void rb_solve_contacts( rb_ct
*buf
, int len
)
1143 float k_friction
= 0.1f
;
1145 /* Friction Impulse */
1146 for( int i
=0; i
<len
; i
++ )
1148 struct contact
*ct
= &buf
[i
];
1149 rigidbody
*rb
= ct
->rba
;
1152 rb_rcv( ct
, rv
, da
, db
);
1154 for( int j
=0; j
<2; j
++ )
1156 float f
= k_friction
* ct
->norm_impulse
,
1157 vt
= -v3_dot( rv
, ct
->t
[j
] );
1159 float temp
= ct
->tangent_impulse
[j
];
1160 ct
->tangent_impulse
[j
] = vg_clampf( temp
+vt
, -f
, f
);
1161 vt
= ct
->tangent_impulse
[j
] - temp
;
1164 v3_muls( ct
->t
[j
], vt
, impulse
);
1165 rb_standard_impulse( ct
, da
, db
, impulse
);
1169 /* Normal Impulse */
1170 for( int i
=0; i
<len
; i
++ )
1172 struct contact
*ct
= &buf
[i
];
1173 rigidbody
*rba
= ct
->rba
,
1177 rb_rcv( ct
, rv
, da
, db
);
1179 float vn
= -v3_dot( rv
, ct
->n
) + ct
->bias
;
1181 float temp
= ct
->norm_impulse
;
1182 ct
->norm_impulse
= vg_maxf( temp
+ vn
, 0.0f
);
1183 vn
= ct
->norm_impulse
- temp
;
1186 v3_muls( ct
->n
, vn
, impulse
);
1187 rb_standard_impulse( ct
, da
, db
, impulse
);
1192 * The following ventures into not really very sophisticated at all maths
1195 struct rb_angle_limit
1197 rigidbody
*rba
, *rbb
;
1199 float impulse
, bias
;
1202 static int rb_angle_limit_force( rigidbody
*rba
, v3f va
,
1203 rigidbody
*rbb
, v3f vb
,
1207 m3x3_mulv( rba
->to_world
, va
, wva
);
1208 m3x3_mulv( rbb
->to_world
, vb
, wvb
);
1210 float dt
= v3_dot(wva
,wvb
)*0.999f
,
1215 float correction
= max
-ang
;
1218 v3_cross( wva
, wvb
, axis
);
1221 q_axis_angle( rotation
, axis
, -correction
*0.25f
);
1222 q_mul( rotation
, rba
->q
, rba
->q
);
1224 q_axis_angle( rotation
, axis
, correction
*0.25f
);
1225 q_mul( rotation
, rbb
->q
, rbb
->q
);
1233 static void rb_constraint_angle_limit( struct rb_angle_limit
*limit
)
1239 static void rb_constraint_angle( rigidbody
*rba
, v3f va
,
1240 rigidbody
*rbb
, v3f vb
,
1241 float max
, float spring
)
1244 m3x3_mulv( rba
->to_world
, va
, wva
);
1245 m3x3_mulv( rbb
->to_world
, vb
, wvb
);
1247 float dt
= v3_dot(wva
,wvb
)*0.999f
,
1251 v3_cross( wva
, wvb
, axis
);
1252 v3_muladds( rba
->w
, axis
, ang
*spring
*0.5f
, rba
->w
);
1253 v3_muladds( rbb
->w
, axis
, -ang
*spring
*0.5f
, rbb
->w
);
1257 /* TODO: convert max into the dot product value so we dont have to always
1258 * evaluate acosf, only if its greater than the angle specified */
1262 float correction
= max
-ang
;
1265 q_axis_angle( rotation
, axis
, -correction
*0.125f
);
1266 q_mul( rotation
, rba
->q
, rba
->q
);
1268 q_axis_angle( rotation
, axis
, correction
*0.125f
);
1269 q_mul( rotation
, rbb
->q
, rbb
->q
);
1273 static void rb_relative_velocity( rigidbody
*ra
, v3f lca
,
1274 rigidbody
*rb
, v3f lcb
, v3f rcv
)
1277 m3x3_mulv( ra
->to_world
, lca
, wca
);
1278 m3x3_mulv( rb
->to_world
, lcb
, wcb
);
1280 v3_sub( ra
->v
, rb
->v
, rcv
);
1283 v3_cross( ra
->w
, wca
, rcv_Ra
);
1284 v3_cross( rb
->w
, wcb
, rcv_Rb
);
1285 v3_add( rcv_Ra
, rcv
, rcv
);
1286 v3_sub( rcv
, rcv_Rb
, rcv
);
1289 static void rb_constraint_position( rigidbody
*ra
, v3f lca
,
1290 rigidbody
*rb
, v3f lcb
)
1292 /* C = (COa + Ra*LCa) - (COb + Rb*LCb) = 0 */
1294 m3x3_mulv( ra
->to_world
, lca
, wca
);
1295 m3x3_mulv( rb
->to_world
, lcb
, wcb
);
1298 v3_add( wcb
, rb
->co
, delta
);
1299 v3_sub( delta
, wca
, delta
);
1300 v3_sub( delta
, ra
->co
, delta
);
1302 v3_muladds( ra
->co
, delta
, 0.5f
, ra
->co
);
1303 v3_muladds( rb
->co
, delta
, -0.5f
, rb
->co
);
1306 v3_sub( ra
->v
, rb
->v
, rcv
);
1309 v3_cross( ra
->w
, wca
, rcv_Ra
);
1310 v3_cross( rb
->w
, wcb
, rcv_Rb
);
1311 v3_add( rcv_Ra
, rcv
, rcv
);
1312 v3_sub( rcv
, rcv_Rb
, rcv
);
1314 float nm
= 0.5f
/(rb
->inv_mass
+ ra
->inv_mass
);
1316 float mass_a
= 1.0f
/ra
->inv_mass
,
1317 mass_b
= 1.0f
/rb
->inv_mass
,
1318 total_mass
= mass_a
+mass_b
;
1321 v3_muls( rcv
, 1.0f
, impulse
);
1322 v3_muladds( rb
->v
, impulse
, mass_b
/total_mass
, rb
->v
);
1323 v3_cross( wcb
, impulse
, impulse
);
1324 v3_add( impulse
, rb
->w
, rb
->w
);
1326 v3_muls( rcv
, -1.0f
, impulse
);
1327 v3_muladds( ra
->v
, impulse
, mass_a
/total_mass
, ra
->v
);
1328 v3_cross( wca
, impulse
, impulse
);
1329 v3_add( impulse
, ra
->w
, ra
->w
);
1333 * this could be used for spring joints
1334 * its not good for position constraint
1337 v3_muls( delta
, 0.5f
*spring
, impulse
);
1339 v3_add( impulse
, ra
->v
, ra
->v
);
1340 v3_cross( wca
, impulse
, impulse
);
1341 v3_add( impulse
, ra
->w
, ra
->w
);
1343 v3_muls( delta
, -0.5f
*spring
, impulse
);
1345 v3_add( impulse
, rb
->v
, rb
->v
);
1346 v3_cross( wcb
, impulse
, impulse
);
1347 v3_add( impulse
, rb
->w
, rb
->w
);
1351 static void debug_sphere( m4x3f m
, float radius
, u32 colour
)
1353 v3f ly
= { 0.0f
, 0.0f
, radius
},
1354 lx
= { 0.0f
, radius
, 0.0f
},
1355 lz
= { 0.0f
, 0.0f
, radius
};
1357 for( int i
=0; i
<16; i
++ )
1359 float t
= ((float)(i
+1) * (1.0f
/16.0f
)) * VG_PIf
* 2.0f
,
1363 v3f py
= { s
*radius
, 0.0f
, c
*radius
},
1364 px
= { s
*radius
, c
*radius
, 0.0f
},
1365 pz
= { 0.0f
, s
*radius
, c
*radius
};
1367 v3f p0
, p1
, p2
, p3
, p4
, p5
;
1368 m4x3_mulv( m
, py
, p0
);
1369 m4x3_mulv( m
, ly
, p1
);
1370 m4x3_mulv( m
, px
, p2
);
1371 m4x3_mulv( m
, lx
, p3
);
1372 m4x3_mulv( m
, pz
, p4
);
1373 m4x3_mulv( m
, lz
, p5
);
1375 vg_line( p0
, p1
, colour
== 0x00? 0xff00ff00: colour
);
1376 vg_line( p2
, p3
, colour
== 0x00? 0xff0000ff: colour
);
1377 vg_line( p4
, p5
, colour
== 0x00? 0xffff0000: colour
);
1385 static void debug_capsule( m4x3f m
, float radius
, float h
, u32 colour
)
1387 v3f ly
= { 0.0f
, 0.0f
, radius
},
1388 lx
= { 0.0f
, radius
, 0.0f
},
1389 lz
= { 0.0f
, 0.0f
, radius
};
1391 float s0
= sinf(0.0f
)*radius
,
1392 c0
= cosf(0.0f
)*radius
;
1394 v3f p0
, p1
, up
, right
, forward
;
1395 m3x3_mulv( m
, (v3f
){0.0f
,1.0f
,0.0f
}, up
);
1396 m3x3_mulv( m
, (v3f
){1.0f
,0.0f
,0.0f
}, right
);
1397 m3x3_mulv( m
, (v3f
){0.0f
,0.0f
,-1.0f
}, forward
);
1398 v3_muladds( m
[3], up
, -h
*0.5f
+radius
, p0
);
1399 v3_muladds( m
[3], up
, h
*0.5f
-radius
, p1
);
1402 v3_muladds( p0
, right
, radius
, a0
);
1403 v3_muladds( p1
, right
, radius
, a1
);
1404 v3_muladds( p0
, forward
, radius
, b0
);
1405 v3_muladds( p1
, forward
, radius
, b1
);
1406 vg_line( a0
, a1
, colour
);
1407 vg_line( b0
, b1
, colour
);
1409 v3_muladds( p0
, right
, -radius
, a0
);
1410 v3_muladds( p1
, right
, -radius
, a1
);
1411 v3_muladds( p0
, forward
, -radius
, b0
);
1412 v3_muladds( p1
, forward
, -radius
, b1
);
1413 vg_line( a0
, a1
, colour
);
1414 vg_line( b0
, b1
, colour
);
1416 for( int i
=0; i
<16; i
++ )
1418 float t
= ((float)(i
+1) * (1.0f
/16.0f
)) * VG_PIf
* 2.0f
,
1419 s1
= sinf(t
)*radius
,
1420 c1
= cosf(t
)*radius
;
1422 v3f e0
= { s0
, 0.0f
, c0
},
1423 e1
= { s1
, 0.0f
, c1
},
1424 e2
= { s0
, c0
, 0.0f
},
1425 e3
= { s1
, c1
, 0.0f
},
1426 e4
= { 0.0f
, c0
, s0
},
1427 e5
= { 0.0f
, c1
, s1
};
1429 m3x3_mulv( m
, e0
, e0
);
1430 m3x3_mulv( m
, e1
, e1
);
1431 m3x3_mulv( m
, e2
, e2
);
1432 m3x3_mulv( m
, e3
, e3
);
1433 m3x3_mulv( m
, e4
, e4
);
1434 m3x3_mulv( m
, e5
, e5
);
1436 v3_add( p0
, e0
, a0
);
1437 v3_add( p0
, e1
, a1
);
1438 v3_add( p1
, e0
, b0
);
1439 v3_add( p1
, e1
, b1
);
1441 vg_line( a0
, a1
, colour
);
1442 vg_line( b0
, b1
, colour
);
1446 v3_add( p0
, e2
, a0
);
1447 v3_add( p0
, e3
, a1
);
1448 v3_add( p0
, e4
, b0
);
1449 v3_add( p0
, e5
, b1
);
1453 v3_add( p1
, e2
, a0
);
1454 v3_add( p1
, e3
, a1
);
1455 v3_add( p1
, e4
, b0
);
1456 v3_add( p1
, e5
, b1
);
1459 vg_line( a0
, a1
, colour
);
1460 vg_line( b0
, b1
, colour
);
1467 static void rb_debug( rigidbody
*rb
, u32 colour
)
1469 if( rb
->type
== k_rb_shape_box
)
1472 vg_line_boxf_transformed( rb
->to_world
, rb
->bbx
, colour
);
1474 else if( rb
->type
== k_rb_shape_sphere
)
1476 debug_sphere( rb
->to_world
, rb
->inf
.sphere
.radius
, colour
);
1478 else if( rb
->type
== k_rb_shape_capsule
)
1481 float h
= rb
->inf
.capsule
.height
,
1482 r
= rb
->inf
.capsule
.radius
;
1484 debug_capsule( rb
->to_world
, r
, h
, colour
);
1489 * out penetration distance, normal
1491 static int rb_point_in_body( rigidbody
*rb
, v3f pos
, float *pen
, v3f normal
)
1494 m4x3_mulv( rb
->to_local
, pos
, local
);
1496 if( local
[0] > rb
->bbx
[0][0] && local
[0] < rb
->bbx
[1][0] &&
1497 local
[1] > rb
->bbx
[0][1] && local
[1] < rb
->bbx
[1][1] &&
1498 local
[2] > rb
->bbx
[0][2] && local
[2] < rb
->bbx
[1][2] )
1500 v3f area
, com
, comrel
;
1501 v3_add( rb
->bbx
[0], rb
->bbx
[1], com
);
1502 v3_muls( com
, 0.5f
, com
);
1504 v3_sub( rb
->bbx
[1], rb
->bbx
[0], area
);
1505 v3_sub( local
, com
, comrel
);
1506 v3_div( comrel
, area
, comrel
);
1509 float max_mag
= fabsf(comrel
[0]);
1511 if( fabsf(comrel
[1]) > max_mag
)
1514 max_mag
= fabsf(comrel
[1]);
1516 if( fabsf(comrel
[2]) > max_mag
)
1519 max_mag
= fabsf(comrel
[2]);
1523 normal
[axis
] = vg_signf(comrel
[axis
]);
1525 if( normal
[axis
] < 0.0f
)
1526 *pen
= local
[axis
] - rb
->bbx
[0][axis
];
1528 *pen
= rb
->bbx
[1][axis
] - local
[axis
];
1530 m3x3_mulv( rb
->to_world
, normal
, normal
);
1538 * BVH implementation, this is ONLY for static rigidbodies, its to slow for
1542 static void rb_bh_expand_bound( void *user
, boxf bound
, u32 item_index
)
1544 rigidbody
*rb
= &((rigidbody
*)user
)[ item_index
];
1545 box_concat( bound
, rb
->bbx_world
);
1548 static float rb_bh_centroid( void *user
, u32 item_index
, int axis
)
1550 rigidbody
*rb
= &((rigidbody
*)user
)[ item_index
];
1551 return (rb
->bbx_world
[axis
][0] + rb
->bbx_world
[1][axis
]) * 0.5f
;
1554 static void rb_bh_swap( void *user
, u32 ia
, u32 ib
)
1556 rigidbody temp
, *rba
, *rbb
;
1557 rba
= &((rigidbody
*)user
)[ ia
];
1558 rbb
= &((rigidbody
*)user
)[ ib
];
1565 static void rb_bh_debug( void *user
, u32 item_index
)
1567 rigidbody
*rb
= &((rigidbody
*)user
)[ item_index
];
1568 rb_debug( rb
, 0xff00ffff );
1571 static bh_system bh_system_rigidbodies
=
1573 .expand_bound
= rb_bh_expand_bound
,
1574 .item_centroid
= rb_bh_centroid
,
1575 .item_swap
= rb_bh_swap
,
1576 .item_debug
= rb_bh_debug
,
1580 #endif /* RIGIDBODY_H */