2 * Resources: Box2D - Erin Catto
10 static void rb_tangent_basis( v3f n
, v3f tx
, v3f ty
);
11 static bh_system bh_system_rigidbodies
;
17 #define k_rb_rate 60.0f
18 #define k_rb_delta (1.0f/k_rb_rate)
20 typedef struct rigidbody rigidbody
;
21 typedef struct contact rb_ct
;
31 k_rb_shape_sphere
= 1,
32 k_rb_shape_capsule
= 2,
59 v3f right
, up
, forward
;
66 /* inertia model and inverse world tensor */
70 m4x3f to_world
, to_local
;
75 * Impulses on static objects get re-routed here
77 static rigidbody rb_static_null
=
80 .q
={0.0f
,0.0f
,0.0f
,1.0f
},
88 static void rb_debug( rigidbody
*rb
, u32 colour
);
95 float p
, bias
, norm_impulse
, tangent_impulse
[2],
96 normal_mass
, tangent_mass
[2];
100 rb_contact_buffer
[256];
101 static int rb_contact_count
= 0;
103 static void rb_update_bounds( rigidbody
*rb
)
105 box_copy( rb
->bbx
, rb
->bbx_world
);
106 m4x3_transform_aabb( rb
->to_world
, rb
->bbx_world
);
109 static void rb_update_transform( rigidbody
*rb
)
111 q_normalize( rb
->q
);
112 q_m3x3( rb
->q
, rb
->to_world
);
113 v3_copy( rb
->co
, rb
->to_world
[3] );
115 m4x3_invert_affine( rb
->to_world
, rb
->to_local
);
117 m3x3_mulv( rb
->to_world
, (v3f
){1.0f
,0.0f
, 0.0f
}, rb
->right
);
118 m3x3_mulv( rb
->to_world
, (v3f
){0.0f
,1.0f
, 0.0f
}, rb
->up
);
119 m3x3_mulv( rb
->to_world
, (v3f
){0.0f
,0.0f
,-1.0f
}, rb
->forward
);
121 m3x3_mul( rb
->iI
, rb
->to_local
, rb
->iIw
);
122 m3x3_mul( rb
->to_world
, rb
->iIw
, rb
->iIw
);
124 rb_update_bounds( rb
);
127 static float sphere_volume( float radius
)
129 float r3
= radius
*radius
*radius
;
130 return (4.0f
/3.0f
) * VG_PIf
* r3
;
133 static void rb_init( rigidbody
*rb
)
137 if( rb
->type
== k_rb_shape_box
)
140 v3_sub( rb
->bbx
[1], rb
->bbx
[0], dims
);
141 volume
= dims
[0]*dims
[1]*dims
[2];
144 vg_info( "Box volume: %f\n", volume
);
146 else if( rb
->type
== k_rb_shape_sphere
)
148 volume
= sphere_volume( rb
->inf
.sphere
.radius
);
149 v3_fill( rb
->bbx
[0], -rb
->inf
.sphere
.radius
);
150 v3_fill( rb
->bbx
[1], rb
->inf
.sphere
.radius
);
152 vg_info( "Sphere volume: %f\n", volume
);
154 else if( rb
->type
== k_rb_shape_capsule
)
156 float r
= rb
->inf
.capsule
.radius
,
157 h
= rb
->inf
.capsule
.height
;
158 volume
= sphere_volume( r
) + VG_PIf
* r
*r
* (h
- r
*2.0f
);
160 v3_fill( rb
->bbx
[0], -rb
->inf
.sphere
.radius
);
161 v3_fill( rb
->bbx
[1], rb
->inf
.sphere
.radius
);
165 else if( rb
->type
== k_rb_shape_scene
)
168 box_copy( rb
->inf
.scene
.pscene
->bbx
, rb
->bbx
);
179 float mass
= 2.0f
*volume
;
180 rb
->inv_mass
= 1.0f
/mass
;
183 v3_sub( rb
->bbx
[1], rb
->bbx
[0], extent
);
184 v3_muls( extent
, 0.5f
, extent
);
186 /* local intertia tensor */
187 float ex2
= 4.0f
*extent
[0]*extent
[0],
188 ey2
= 4.0f
*extent
[1]*extent
[1],
189 ez2
= 4.0f
*extent
[2]*extent
[2];
191 rb
->I
[0] = ((1.0f
/12.0f
) * mass
* (ey2
+ez2
));
192 rb
->I
[1] = ((1.0f
/12.0f
) * mass
* (ex2
+ez2
));
193 rb
->I
[2] = ((1.0f
/12.0f
) * mass
* (ex2
+ey2
));
195 m3x3_identity( rb
->iI
);
196 rb
->iI
[0][0] = rb
->I
[0];
197 rb
->iI
[1][1] = rb
->I
[1];
198 rb
->iI
[2][2] = rb
->I
[2];
199 m3x3_inv( rb
->iI
, rb
->iI
);
205 rb_update_transform( rb
);
208 static void rb_iter( rigidbody
*rb
)
210 v3f gravity
= { 0.0f
, -9.8f
, 0.0f
};
211 v3_muladds( rb
->v
, gravity
, k_rb_delta
, rb
->v
);
213 /* intergrate velocity */
214 v3_muladds( rb
->co
, rb
->v
, k_rb_delta
, rb
->co
);
215 v3_lerp( rb
->w
, (v3f
){0.0f
,0.0f
,0.0f
}, 0.0025f
, rb
->w
);
217 /* inegrate inertia */
218 if( v3_length2( rb
->w
) > 0.0f
)
222 v3_copy( rb
->w
, axis
);
224 float mag
= v3_length( axis
);
225 v3_divs( axis
, mag
, axis
);
226 q_axis_angle( rotation
, axis
, mag
*k_rb_delta
);
227 q_mul( rotation
, rb
->q
, rb
->q
);
231 static void rb_torque( rigidbody
*rb
, v3f axis
, float mag
)
233 v3_muladds( rb
->w
, axis
, mag
*k_rb_delta
, rb
->w
);
236 static void rb_tangent_basis( v3f n
, v3f tx
, v3f ty
)
238 /* Compute tangent basis (box2d) */
239 if( fabsf( n
[0] ) >= 0.57735027f
)
253 v3_cross( n
, tx
, ty
);
256 static void rb_solver_reset(void);
258 static void rb_build_manifold_terrain( rigidbody
*rb
);
259 static void rb_build_manifold_terrain_sphere( rigidbody
*rb
);
261 static void rb_solve_contacts( rb_ct
*buf
, int len
);
262 static void rb_presolve_contacts( rb_ct
*buffer
, int len
);
265 * These closest point tests were learned from Real-Time Collision Detection by
268 static float closest_segment_segment( v3f p1
, v3f q1
, v3f p2
, v3f q2
,
269 float *s
, float *t
, v3f c1
, v3f c2
)
272 v3_sub( q1
, p1
, d1
);
273 v3_sub( q2
, p2
, d2
);
276 float a
= v3_length2( d1
),
277 e
= v3_length2( d2
),
280 const float kEpsilon
= 0.0001f
;
282 if( a
<= kEpsilon
&& e
<= kEpsilon
)
290 v3_sub( c1
, c2
, v0
);
292 return v3_length2( v0
);
298 *t
= vg_clampf( f
/ e
, 0.0f
, 1.0f
);
302 float c
= v3_dot( d1
, r
);
306 *s
= vg_clampf( -c
/ a
, 0.0f
, 1.0f
);
310 float b
= v3_dot(d1
,d2
),
315 *s
= vg_clampf((b
*f
- c
*e
)/d
, 0.0f
, 1.0f
);
327 *s
= vg_clampf( -c
/ a
, 0.0f
, 1.0f
);
332 *s
= vg_clampf((b
-c
)/a
,0.0f
,1.0f
);
337 v3_muladds( p1
, d1
, *s
, c1
);
338 v3_muladds( p2
, d2
, *t
, c2
);
341 v3_sub( c1
, c2
, v0
);
342 return v3_length2( v0
);
345 static void closest_point_aabb( v3f p
, boxf box
, v3f dest
)
347 v3_maxv( p
, box
[0], dest
);
348 v3_minv( dest
, box
[1], dest
);
351 static void closest_point_obb( v3f p
, rigidbody
*rb
, v3f dest
)
354 m4x3_mulv( rb
->to_local
, p
, local
);
355 closest_point_aabb( local
, rb
->bbx
, local
);
356 m4x3_mulv( rb
->to_world
, local
, dest
);
359 static float closest_point_segment( v3f a
, v3f b
, v3f point
, v3f dest
)
363 v3_sub( point
, a
, v1
);
365 float t
= v3_dot( v1
, v0
) / v3_length2(v0
);
366 t
= vg_clampf(t
,0.0f
,1.0f
);
367 v3_muladds( a
, v0
, t
, dest
);
371 static void closest_on_triangle( v3f p
, v3f tri
[3], v3f dest
)
376 /* Region outside A */
377 v3_sub( tri
[1], tri
[0], ab
);
378 v3_sub( tri
[2], tri
[0], ac
);
379 v3_sub( p
, tri
[0], ap
);
383 if( d1
<= 0.0f
&& d2
<= 0.0f
)
385 v3_copy( tri
[0], dest
);
386 v3_copy( (v3f
){INFINITY
,INFINITY
,INFINITY
}, dest
);
390 /* Region outside B */
394 v3_sub( p
, tri
[1], bp
);
395 d3
= v3_dot( ab
, bp
);
396 d4
= v3_dot( ac
, bp
);
398 if( d3
>= 0.0f
&& d4
<= d3
)
400 v3_copy( tri
[1], dest
);
401 v3_copy( (v3f
){INFINITY
,INFINITY
,INFINITY
}, dest
);
405 /* Edge region of AB */
406 float vc
= d1
*d4
- d3
*d2
;
407 if( vc
<= 0.0f
&& d1
>= 0.0f
&& d3
<= 0.0f
)
409 float v
= d1
/ (d1
-d3
);
410 v3_muladds( tri
[0], ab
, v
, dest
);
411 v3_copy( (v3f
){INFINITY
,INFINITY
,INFINITY
}, dest
);
415 /* Region outside C */
418 v3_sub( p
, tri
[2], cp
);
422 if( d6
>= 0.0f
&& d5
<= d6
)
424 v3_copy( tri
[2], dest
);
425 v3_copy( (v3f
){INFINITY
,INFINITY
,INFINITY
}, dest
);
430 float vb
= d5
*d2
- d1
*d6
;
431 if( vb
<= 0.0f
&& d2
>= 0.0f
&& d6
<= 0.0f
)
433 float w
= d2
/ (d2
-d6
);
434 v3_muladds( tri
[0], ac
, w
, dest
);
435 v3_copy( (v3f
){INFINITY
,INFINITY
,INFINITY
}, dest
);
440 float va
= d3
*d6
- d5
*d4
;
441 if( va
<= 0.0f
&& (d4
-d3
) >= 0.0f
&& (d5
-d6
) >= 0.0f
)
443 float w
= (d4
-d3
) / ((d4
-d3
) + (d5
-d6
));
445 v3_sub( tri
[2], tri
[1], bc
);
446 v3_muladds( tri
[1], bc
, w
, dest
);
447 v3_copy( (v3f
){INFINITY
,INFINITY
,INFINITY
}, dest
);
451 /* P inside region, Q via barycentric coordinates uvw */
452 float d
= 1.0f
/(va
+vb
+vc
),
456 v3_muladds( tri
[0], ab
, v
, dest
);
457 v3_muladds( dest
, ac
, w
, dest
);
461 static void closest_on_triangle_1( v3f p
, v3f tri
[3], v3f dest
)
466 /* Region outside A */
467 v3_sub( tri
[1], tri
[0], ab
);
468 v3_sub( tri
[2], tri
[0], ac
);
469 v3_sub( p
, tri
[0], ap
);
473 if( d1
<= 0.0f
&& d2
<= 0.0f
)
475 v3_copy( tri
[0], dest
);
479 /* Region outside B */
483 v3_sub( p
, tri
[1], bp
);
484 d3
= v3_dot( ab
, bp
);
485 d4
= v3_dot( ac
, bp
);
487 if( d3
>= 0.0f
&& d4
<= d3
)
489 v3_copy( tri
[1], dest
);
493 /* Edge region of AB */
494 float vc
= d1
*d4
- d3
*d2
;
495 if( vc
<= 0.0f
&& d1
>= 0.0f
&& d3
<= 0.0f
)
497 float v
= d1
/ (d1
-d3
);
498 v3_muladds( tri
[0], ab
, v
, dest
);
502 /* Region outside C */
505 v3_sub( p
, tri
[2], cp
);
509 if( d6
>= 0.0f
&& d5
<= d6
)
511 v3_copy( tri
[2], dest
);
516 float vb
= d5
*d2
- d1
*d6
;
517 if( vb
<= 0.0f
&& d2
>= 0.0f
&& d6
<= 0.0f
)
519 float w
= d2
/ (d2
-d6
);
520 v3_muladds( tri
[0], ac
, w
, dest
);
525 float va
= d3
*d6
- d5
*d4
;
526 if( va
<= 0.0f
&& (d4
-d3
) >= 0.0f
&& (d5
-d6
) >= 0.0f
)
528 float w
= (d4
-d3
) / ((d4
-d3
) + (d5
-d6
));
530 v3_sub( tri
[2], tri
[1], bc
);
531 v3_muladds( tri
[1], bc
, w
, dest
);
535 /* P inside region, Q via barycentric coordinates uvw */
536 float d
= 1.0f
/(va
+vb
+vc
),
540 v3_muladds( tri
[0], ab
, v
, dest
);
541 v3_muladds( dest
, ac
, w
, dest
);
544 static int rb_intersect_planes( v4f p0
, v4f p1
, v4f p2
, v3f p
)
547 v3_cross( p1
, p2
, u
);
548 float d
= v3_dot( p0
, u
);
550 if( fabsf(d
) < 0.0001f
)
553 v3_muls( u
, p0
[3], p
);
556 v3_muls( p1
, p2
[3], v0
);
557 v3_muladds( v0
, p2
, -p1
[3], v0
);
558 v3_cross( p0
, v0
, v1
);
560 v3_muls( p
, 1.0f
/d
, p
);
565 int rb_intersect_planes_1( v4f a
, v4f b
, v4f c
, v3f p
)
567 float const epsilon
= 0.001;
575 if( d
< epsilon
&& d
> -epsilon
) return 0;
581 v3_muls( bc
, -a
[3], p
);
582 v3_muladds( p
, ca
, -b
[3], p
);
583 v3_muladds( p
, ab
, -c
[3], p
);
593 * These do not automatically allocate contacts, an appropriately sized
594 * buffer must be supplied. The function returns the size of the manifold
595 * which was generated.
597 * The values set on the contacts are: n, co, p, rba, rbb
600 static void rb_debug_contact( rb_ct
*ct
)
603 v3_muladds( ct
->co
, ct
->n
, 0.1f
, p1
);
604 vg_line_pt3( ct
->co
, 0.025f
, 0xff0000ff );
605 vg_line( ct
->co
, p1
, 0xffffffff );
609 * By collecting the minimum(time) and maximum(time) pairs of points, we
610 * build a reduced and stable exact manifold.
613 * rx: minimum distance of these points
614 * dx: the delta between the two points
616 * pairs will only ammend these if they are creating a collision
618 typedef struct capsule_manifold capsule_manifold
;
619 struct capsule_manifold
627 * Expand a line manifold with a new pair. t value is the time along segment
628 * on the oriented object which created this pair.
630 static void rb_capsule_manifold( v3f pa
, v3f pb
, float t
, float r
,
631 capsule_manifold
*manifold
)
634 v3_sub( pa
, pb
, delta
);
636 if( v3_length2(delta
) < r
*r
)
638 if( t
< manifold
->t0
)
640 v3_copy( delta
, manifold
->d0
);
645 if( t
> manifold
->t1
)
647 v3_copy( delta
, manifold
->d1
);
654 static void rb_capsule_manifold_init( capsule_manifold
*manifold
)
656 manifold
->t0
= INFINITY
;
657 manifold
->t1
= -INFINITY
;
660 static int rb_capsule_manifold_done( rigidbody
*rba
, rigidbody
*rbb
,
661 capsule_manifold
*manifold
, rb_ct
*buf
)
663 float h
= rba
->inf
.capsule
.height
,
664 ra
= rba
->inf
.capsule
.radius
;
667 v3_muladds( rba
->co
, rba
->up
, -h
*0.5f
+ra
, p0
);
668 v3_muladds( rba
->co
, rba
->up
, h
*0.5f
-ra
, p1
);
671 if( manifold
->t0
<= 1.0f
)
676 v3_muls( p0
, 1.0f
-manifold
->t0
, pa
);
677 v3_muladds( pa
, p1
, manifold
->t0
, pa
);
679 float d
= v3_length( manifold
->d0
);
680 v3_muls( manifold
->d0
, 1.0f
/d
, ct
->n
);
681 v3_muladds( pa
, ct
->n
, -ra
, ct
->co
);
683 ct
->p
= manifold
->r0
- d
;
690 if( (manifold
->t1
>= 0.0f
) && (manifold
->t0
!= manifold
->t1
) )
692 rb_ct
*ct
= buf
+count
;
695 v3_muls( p0
, 1.0f
-manifold
->t1
, pa
);
696 v3_muladds( pa
, p1
, manifold
->t1
, pa
);
698 float d
= v3_length( manifold
->d1
);
699 v3_muls( manifold
->d1
, 1.0f
/d
, ct
->n
);
700 v3_muladds( pa
, ct
->n
, -ra
, ct
->co
);
702 ct
->p
= manifold
->r1
- d
;
714 vg_line( buf
[0].co
, buf
[1].co
, 0xff0000ff );
719 static int rb_capsule_vs_sphere( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
721 float h
= rba
->inf
.capsule
.height
,
722 ra
= rba
->inf
.capsule
.radius
,
723 rb
= rbb
->inf
.sphere
.radius
;
726 v3_muladds( rba
->co
, rba
->up
, -h
*0.5f
+ra
, p0
);
727 v3_muladds( rba
->co
, rba
->up
, h
*0.5f
-ra
, p1
);
730 closest_point_segment( p0
, p1
, rbb
->co
, c
);
731 v3_sub( c
, rbb
->co
, delta
);
733 float d2
= v3_length2(delta
),
741 v3_muls( delta
, 1.0f
/d
, ct
->n
);
745 v3_muladds( c
, ct
->n
, -ra
, p0
);
746 v3_muladds( rbb
->co
, ct
->n
, rb
, p1
);
747 v3_add( p0
, p1
, ct
->co
);
748 v3_muls( ct
->co
, 0.5f
, ct
->co
);
759 static int rb_capsule_vs_capsule( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
761 float ha
= rba
->inf
.capsule
.height
,
762 hb
= rbb
->inf
.capsule
.height
,
763 ra
= rba
->inf
.capsule
.radius
,
764 rb
= rbb
->inf
.capsule
.radius
,
768 v3_muladds( rba
->co
, rba
->up
, -ha
*0.5f
+ra
, p0
);
769 v3_muladds( rba
->co
, rba
->up
, ha
*0.5f
-ra
, p1
);
770 v3_muladds( rbb
->co
, rbb
->up
, -hb
*0.5f
+rb
, p2
);
771 v3_muladds( rbb
->co
, rbb
->up
, hb
*0.5f
-rb
, p3
);
773 capsule_manifold manifold
;
774 rb_capsule_manifold_init( &manifold
);
778 closest_segment_segment( p0
, p1
, p2
, p3
, &ta
, &tb
, pa
, pb
);
779 rb_capsule_manifold( pa
, pb
, ta
, r
, &manifold
);
781 ta
= closest_point_segment( p0
, p1
, p2
, pa
);
782 tb
= closest_point_segment( p0
, p1
, p3
, pb
);
783 rb_capsule_manifold( pa
, p2
, ta
, r
, &manifold
);
784 rb_capsule_manifold( pb
, p3
, tb
, r
, &manifold
);
786 closest_point_segment( p2
, p3
, p0
, pa
);
787 closest_point_segment( p2
, p3
, p1
, pb
);
788 rb_capsule_manifold( p0
, pa
, 0.0f
, r
, &manifold
);
789 rb_capsule_manifold( p1
, pb
, 1.0f
, r
, &manifold
);
791 return rb_capsule_manifold_done( rba
, rbb
, &manifold
, buf
);
795 * Generates up to two contacts; optimised for the most stable manifold
797 static int rb_capsule_vs_box( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
799 float h
= rba
->inf
.capsule
.height
,
800 r
= rba
->inf
.capsule
.radius
;
803 * Solving this in symetric local space of the cube saves us some time and a
804 * couple branches when it comes to the quad stage.
807 v3_add( rbb
->bbx
[0], rbb
->bbx
[1], centroid
);
808 v3_muls( centroid
, 0.5f
, centroid
);
811 v3_sub( rbb
->bbx
[0], centroid
, bbx
[0] );
812 v3_sub( rbb
->bbx
[1], centroid
, bbx
[1] );
814 v3f pc
, p0w
, p1w
, p0
, p1
;
815 v3_muladds( rba
->co
, rba
->up
, -h
*0.5f
+r
, p0w
);
816 v3_muladds( rba
->co
, rba
->up
, h
*0.5f
-r
, p1w
);
818 m4x3_mulv( rbb
->to_local
, p0w
, p0
);
819 m4x3_mulv( rbb
->to_local
, p1w
, p1
);
820 v3_sub( p0
, centroid
, p0
);
821 v3_sub( p1
, centroid
, p1
);
822 v3_add( p0
, p1
, pc
);
823 v3_muls( pc
, 0.5f
, pc
);
826 * Finding an appropriate quad to collide lines with
829 v3_div( pc
, bbx
[1], region
);
832 if( (fabsf(region
[0]) > fabsf(region
[1])) &&
833 (fabsf(region
[0]) > fabsf(region
[2])) )
835 float px
= vg_signf(region
[0]) * bbx
[1][0];
836 v3_copy( (v3f
){ px
, bbx
[0][1], bbx
[0][2] }, quad
[0] );
837 v3_copy( (v3f
){ px
, bbx
[1][1], bbx
[0][2] }, quad
[1] );
838 v3_copy( (v3f
){ px
, bbx
[1][1], bbx
[1][2] }, quad
[2] );
839 v3_copy( (v3f
){ px
, bbx
[0][1], bbx
[1][2] }, quad
[3] );
841 else if( fabsf(region
[1]) > fabsf(region
[2]) )
843 float py
= vg_signf(region
[1]) * bbx
[1][1];
844 v3_copy( (v3f
){ bbx
[0][0], py
, bbx
[0][2] }, quad
[0] );
845 v3_copy( (v3f
){ bbx
[1][0], py
, bbx
[0][2] }, quad
[1] );
846 v3_copy( (v3f
){ bbx
[1][0], py
, bbx
[1][2] }, quad
[2] );
847 v3_copy( (v3f
){ bbx
[0][0], py
, bbx
[1][2] }, quad
[3] );
851 float pz
= vg_signf(region
[2]) * bbx
[1][2];
852 v3_copy( (v3f
){ bbx
[0][0], bbx
[0][1], pz
}, quad
[0] );
853 v3_copy( (v3f
){ bbx
[1][0], bbx
[0][1], pz
}, quad
[1] );
854 v3_copy( (v3f
){ bbx
[1][0], bbx
[1][1], pz
}, quad
[2] );
855 v3_copy( (v3f
){ bbx
[0][0], bbx
[1][1], pz
}, quad
[3] );
858 capsule_manifold manifold
;
859 rb_capsule_manifold_init( &manifold
);
862 closest_point_aabb( p0
, bbx
, c0
);
863 closest_point_aabb( p1
, bbx
, c1
);
866 v3_sub( c0
, p0
, d0
);
867 v3_sub( c1
, p1
, d1
);
868 v3_sub( p1
, p0
, da
);
875 if( v3_dot( da
, d0
) <= 0.01f
)
876 rb_capsule_manifold( p0
, c0
, 0.0f
, r
, &manifold
);
878 if( v3_dot( da
, d1
) >= -0.01f
)
879 rb_capsule_manifold( p1
, c1
, 1.0f
, r
, &manifold
);
881 for( int i
=0; i
<4; i
++ )
888 closest_segment_segment( p0
, p1
, quad
[i0
], quad
[i1
], &ta
, &tb
, ca
, cb
);
889 rb_capsule_manifold( ca
, cb
, ta
, r
, &manifold
);
893 * Create final contacts based on line manifold
895 m3x3_mulv( rbb
->to_world
, manifold
.d0
, manifold
.d0
);
896 m3x3_mulv( rbb
->to_world
, manifold
.d1
, manifold
.d1
);
903 for( int i
=0; i
<4; i
++ )
909 v3_add( quad
[i0
], centroid
, q0
);
910 v3_add( quad
[i1
], centroid
, q1
);
912 m4x3_mulv( rbb
->to_world
, q0
, q0
);
913 m4x3_mulv( rbb
->to_world
, q1
, q1
);
915 vg_line( q0
, q1
, 0xffffffff );
919 return rb_capsule_manifold_done( rba
, rbb
, &manifold
, buf
);
922 static int rb_sphere_vs_box( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
926 closest_point_obb( rba
->co
, rbb
, co
);
927 v3_sub( rba
->co
, co
, delta
);
929 float d2
= v3_length2(delta
),
930 r
= rba
->inf
.sphere
.radius
;
939 v3_sub( rba
->co
, rbb
->co
, delta
);
942 * some extra testing is required to find the best axis to push the
943 * object back outside the box. Since there isnt a clear seperating
944 * vector already, especially on really high aspect boxes.
946 float lx
= v3_dot( rbb
->right
, delta
),
947 ly
= v3_dot( rbb
->up
, delta
),
948 lz
= v3_dot( rbb
->forward
, delta
),
949 px
= rbb
->bbx
[1][0] - fabsf(lx
),
950 py
= rbb
->bbx
[1][1] - fabsf(ly
),
951 pz
= rbb
->bbx
[1][2] - fabsf(lz
);
953 if( px
< py
&& px
< pz
)
954 v3_muls( rbb
->right
, vg_signf(lx
), ct
->n
);
956 v3_muls( rbb
->up
, vg_signf(ly
), ct
->n
);
958 v3_muls( rbb
->forward
, vg_signf(lz
), ct
->n
);
960 v3_muladds( rba
->co
, ct
->n
, -r
, ct
->co
);
966 v3_muls( delta
, 1.0f
/d
, ct
->n
);
968 v3_copy( co
, ct
->co
);
979 static int rb_sphere_vs_sphere( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
982 v3_sub( rba
->co
, rbb
->co
, delta
);
984 float d2
= v3_length2(delta
),
985 r
= rba
->inf
.sphere
.radius
+ rbb
->inf
.sphere
.radius
;
992 v3_muls( delta
, 1.0f
/d
, ct
->n
);
995 v3_muladds( rba
->co
, ct
->n
,-rba
->inf
.sphere
.radius
, p0
);
996 v3_muladds( rbb
->co
, ct
->n
, rbb
->inf
.sphere
.radius
, p1
);
997 v3_add( p0
, p1
, ct
->co
);
998 v3_muls( ct
->co
, 0.5f
, ct
->co
);
1008 /* TODO: these guys */
1010 static int rb_capsule_vs_scene( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1014 int len
= bh_select( &rbb
->inf
.scene
.pscene
->bhtris
,
1015 rba
->bbx_world
, geo
, 128 );
1020 static int rb_sphere_vs_triangle( rigidbody
*rba
, rigidbody
*rbb
,
1021 v3f tri
[3], rb_ct
*buf
)
1025 closest_on_triangle( rba
->co
, tri
, co
);
1026 v3_sub( rba
->co
, co
, delta
);
1028 vg_line( rba
->co
, co
, 0xffff0000 );
1029 vg_line_pt3( rba
->co
, 0.1f
, 0xff00ffff );
1031 float d2
= v3_length2( delta
),
1032 r
= rba
->inf
.sphere
.radius
;
1039 v3_sub( tri
[2], tri
[0], ab
);
1040 v3_sub( tri
[1], tri
[0], ac
);
1041 v3_cross( ac
, ab
, tn
);
1042 v3_copy( tn
, ct
->n
);
1043 v3_normalize( ct
->n
);
1045 float d
= sqrtf(d2
);
1047 v3_copy( co
, ct
->co
);
1057 static int rb_sphere_vs_scene( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1059 scene
*sc
= rbb
->inf
.scene
.pscene
;
1063 int len
= bh_select( &sc
->bhtris
, rba
->bbx_world
, geo
, 128 );
1067 for( int i
=0; i
<len
; i
++ )
1069 u32
*ptri
= &sc
->indices
[ geo
[i
]*3 ];
1071 for( int j
=0; j
<3; j
++ )
1072 v3_copy( sc
->verts
[ptri
[j
]].co
, tri
[j
] );
1074 vg_line(tri
[0],tri
[1],0xff00ff00 );
1075 vg_line(tri
[1],tri
[2],0xff00ff00 );
1076 vg_line(tri
[2],tri
[0],0xff00ff00 );
1078 buf
[count
].element_id
= ptri
[0];
1079 count
+= rb_sphere_vs_triangle( rba
, rbb
, tri
, buf
+count
);
1083 vg_warn( "Exceeding sphere_vs_scene capacity. Geometry too dense!\n" );
1091 static float rb_box_plane_interval( rigidbody
*rba
, v4f p
)
1093 /* TODO: Make boxes COG aligned as is every other shape.
1094 * or create COG vector.
1095 * TODO: Make forward actually point in the right fucking direction. */
1097 v3_sub( rba
->bbx
[1], rba
->bbx
[0], e
);
1098 v3_muls( e
, 0.5f
, e
);
1099 v3_add( rba
->bbx
[0], e
, c
);
1100 m4x3_mulv( rba
->to_world
, c
, c
);
1103 e
[0]*fabsf( v3_dot(p
, rba
->right
)) +
1104 e
[1]*fabsf( v3_dot(p
, rba
->up
)) +
1105 e
[2]*fabsf(-v3_dot(p
, rba
->forward
)),
1106 s
= v3_dot( p
, c
) - p
[3];
1111 static int rb_box_triangle_interval( v3f extent
, v3f axis
, v3f tri
[3] )
1115 r
= extent
[0] * fabsf(axis
[0]) +
1116 extent
[1] * fabsf(axis
[1]) +
1117 extent
[2] * fabsf(axis
[2]),
1119 p0
= v3_dot( axis
, tri
[0] ),
1120 p1
= v3_dot( axis
, tri
[1] ),
1121 p2
= v3_dot( axis
, tri
[2] ),
1123 e
= vg_maxf(-vg_maxf(p0
,vg_maxf(p1
,p2
)), vg_minf(p0
,vg_minf(p1
,p2
)));
1125 if( e
> r
) return 0;
1129 static int rb_box_triangle_sat( rigidbody
*rba
, v3f tri_src
[3] )
1134 v3_sub( rba
->bbx
[1], rba
->bbx
[0], extent
);
1135 v3_muls( extent
, 0.5f
, extent
);
1136 v3_add( rba
->bbx
[0], extent
, c
);
1138 for( int i
=0; i
<3; i
++ )
1140 m4x3_mulv( rba
->to_local
, tri_src
[i
], tri
[i
] );
1141 v3_sub( tri
[i
], c
, tri
[i
] );
1145 if(!rb_box_triangle_interval( extent
, (v3f
){1.0f
,0.0f
,0.0f
}, tri
)) return 0;
1146 if(!rb_box_triangle_interval( extent
, (v3f
){0.0f
,1.0f
,0.0f
}, tri
)) return 0;
1147 if(!rb_box_triangle_interval( extent
, (v3f
){0.0f
,0.0f
,1.0f
}, tri
)) return 0;
1149 v3f v0
,v1
,v2
,n
, e0
,e1
,e2
;
1150 v3_sub( tri
[1], tri
[0], v0
);
1151 v3_sub( tri
[2], tri
[0], v1
);
1152 v3_sub( tri
[2], tri
[1], v2
);
1156 v3_cross( v0
, v1
, n
);
1157 v3_cross( v0
, n
, e0
);
1158 v3_cross( n
, v1
, e1
);
1159 v3_cross( v2
, n
, e2
);
1162 if(!rb_box_triangle_interval( extent
, n
, tri
)) return 0;
1165 v3_cross( e0
, (v3f
){1.0f
,0.0f
,0.0f
}, axis
[0] );
1166 v3_cross( e0
, (v3f
){0.0f
,1.0f
,0.0f
}, axis
[1] );
1167 v3_cross( e0
, (v3f
){0.0f
,0.0f
,1.0f
}, axis
[2] );
1168 v3_cross( e1
, (v3f
){1.0f
,0.0f
,0.0f
}, axis
[3] );
1169 v3_cross( e1
, (v3f
){0.0f
,1.0f
,0.0f
}, axis
[4] );
1170 v3_cross( e1
, (v3f
){0.0f
,0.0f
,1.0f
}, axis
[5] );
1171 v3_cross( e2
, (v3f
){1.0f
,0.0f
,0.0f
}, axis
[6] );
1172 v3_cross( e2
, (v3f
){0.0f
,1.0f
,0.0f
}, axis
[7] );
1173 v3_cross( e2
, (v3f
){0.0f
,0.0f
,1.0f
}, axis
[8] );
1175 for( int i
=0; i
<9; i
++ )
1176 if(!rb_box_triangle_interval( extent
, axis
[i
], tri
)) return 0;
1181 static int rb_box_vs_scene( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1183 scene
*sc
= rbb
->inf
.scene
.pscene
;
1187 int len
= bh_select( &sc
->bhtris
, rba
->bbx_world
, geo
, 128 );
1191 for( int i
=0; i
<len
; i
++ )
1193 u32
*ptri
= &sc
->indices
[ geo
[i
]*3 ];
1195 for( int j
=0; j
<3; j
++ )
1196 v3_copy( sc
->verts
[ptri
[j
]].co
, tri
[j
] );
1198 if( rb_box_triangle_sat( rba
, tri
) )
1200 vg_line(tri
[0],tri
[1],0xff50ff00 );
1201 vg_line(tri
[1],tri
[2],0xff50ff00 );
1202 vg_line(tri
[2],tri
[0],0xff50ff00 );
1206 vg_line(tri
[0],tri
[1],0xff0000ff );
1207 vg_line(tri
[1],tri
[2],0xff0000ff );
1208 vg_line(tri
[2],tri
[0],0xff0000ff );
1214 v3_sub( tri
[1], tri
[0], v0
);
1215 v3_sub( tri
[2], tri
[0], v1
);
1216 v3_cross( v0
, v1
, n
);
1219 /* find best feature */
1220 float best
= v3_dot( rba
->right
, n
);
1223 float cy
= v3_dot( rba
->up
, n
);
1224 if( fabsf(cy
) > fabsf(best
) )
1230 /* TODO: THIS IS WRONG DIRECTION */
1231 float cz
= -v3_dot( rba
->forward
, n
);
1232 if( fabsf(cz
) > fabsf(best
) )
1242 float px
= best
> 0.0f
? rba
->bbx
[0][0]: rba
->bbx
[1][0];
1243 manifold
[0][0] = px
;
1244 manifold
[0][1] = rba
->bbx
[0][1];
1245 manifold
[0][2] = rba
->bbx
[0][2];
1246 manifold
[1][0] = px
;
1247 manifold
[1][1] = rba
->bbx
[1][1];
1248 manifold
[1][2] = rba
->bbx
[0][2];
1249 manifold
[2][0] = px
;
1250 manifold
[2][1] = rba
->bbx
[1][1];
1251 manifold
[2][2] = rba
->bbx
[1][2];
1252 manifold
[3][0] = px
;
1253 manifold
[3][1] = rba
->bbx
[0][1];
1254 manifold
[3][2] = rba
->bbx
[1][2];
1256 else if( axis
== 1 )
1258 float py
= best
> 0.0f
? rba
->bbx
[0][1]: rba
->bbx
[1][1];
1259 manifold
[0][0] = rba
->bbx
[0][0];
1260 manifold
[0][1] = py
;
1261 manifold
[0][2] = rba
->bbx
[0][2];
1262 manifold
[1][0] = rba
->bbx
[1][0];
1263 manifold
[1][1] = py
;
1264 manifold
[1][2] = rba
->bbx
[0][2];
1265 manifold
[2][0] = rba
->bbx
[1][0];
1266 manifold
[2][1] = py
;
1267 manifold
[2][2] = rba
->bbx
[1][2];
1268 manifold
[3][0] = rba
->bbx
[0][0];
1269 manifold
[3][1] = py
;
1270 manifold
[3][2] = rba
->bbx
[1][2];
1274 float pz
= best
> 0.0f
? rba
->bbx
[0][2]: rba
->bbx
[1][2];
1275 manifold
[0][0] = rba
->bbx
[0][0];
1276 manifold
[0][1] = rba
->bbx
[0][1];
1277 manifold
[0][2] = pz
;
1278 manifold
[1][0] = rba
->bbx
[1][0];
1279 manifold
[1][1] = rba
->bbx
[0][1];
1280 manifold
[1][2] = pz
;
1281 manifold
[2][0] = rba
->bbx
[1][0];
1282 manifold
[2][1] = rba
->bbx
[1][1];
1283 manifold
[2][2] = pz
;
1284 manifold
[3][0] = rba
->bbx
[0][0];
1285 manifold
[3][1] = rba
->bbx
[1][1];
1286 manifold
[3][2] = pz
;
1289 for( int j
=0; j
<4; j
++ )
1290 m4x3_mulv( rba
->to_world
, manifold
[j
], manifold
[j
] );
1292 vg_line( manifold
[0], manifold
[1], 0xffffffff );
1293 vg_line( manifold
[1], manifold
[2], 0xffffffff );
1294 vg_line( manifold
[2], manifold
[3], 0xffffffff );
1295 vg_line( manifold
[3], manifold
[0], 0xffffffff );
1297 for( int j
=0; j
<4; j
++ )
1299 rb_ct
*ct
= buf
+count
;
1301 v3_copy( manifold
[j
], ct
->co
);
1302 v3_copy( n
, ct
->n
);
1304 float l0
= v3_dot( tri
[0], n
),
1305 l1
= v3_dot( manifold
[j
], n
);
1307 ct
->p
= (l0
-l1
)*0.5f
;
1322 static int RB_MATRIX_ERROR( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1324 vg_error( "Collision type is unimplemented between types %d and %d\n",
1325 rba
->type
, rbb
->type
);
1330 static int rb_sphere_vs_capsule( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1332 return rb_capsule_vs_sphere( rbb
, rba
, buf
);
1335 static int rb_box_vs_capsule( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1337 return rb_capsule_vs_box( rbb
, rba
, buf
);
1340 static int rb_box_vs_sphere( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1342 return rb_sphere_vs_box( rbb
, rba
, buf
);
1345 static int rb_scene_vs_box( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1347 return rb_box_vs_scene( rbb
, rba
, buf
);
1350 static int (*rb_jump_table
[4][4])( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1352 /* box */ /* Sphere */ /* Capsule */ /* Mesh */
1353 /*box */ { RB_MATRIX_ERROR
, rb_box_vs_sphere
, rb_box_vs_capsule
, rb_box_vs_scene
},
1354 /*sphere */ { rb_sphere_vs_box
, rb_sphere_vs_sphere
, rb_sphere_vs_capsule
, rb_sphere_vs_scene
},
1355 /*capsule*/ { rb_capsule_vs_box
,rb_capsule_vs_sphere
,rb_capsule_vs_capsule
,RB_MATRIX_ERROR
},
1356 /*mesh */ { rb_scene_vs_box
, RB_MATRIX_ERROR
, RB_MATRIX_ERROR
, RB_MATRIX_ERROR
}
1359 static int rb_collide( rigidbody
*rba
, rigidbody
*rbb
)
1361 int (*collider_jump
)(rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1362 = rb_jump_table
[rba
->type
][rbb
->type
];
1365 * 12 is the maximum manifold size we can generate, so we are forced to abort
1366 * potentially checking any more.
1368 if( rb_contact_count
+ 12 > vg_list_size(rb_contact_buffer
) )
1370 vg_warn( "Too many contacts made in global collider buffer (%d of %d\n)",
1371 rb_contact_count
, vg_list_size(rb_contact_buffer
) );
1376 * TODO: Replace this with a more dedicated broad phase pass
1378 if( box_overlap( rba
->bbx_world
, rbb
->bbx_world
) )
1380 int count
= collider_jump( rba
, rbb
, rb_contact_buffer
+rb_contact_count
);
1381 rb_contact_count
+= count
;
1394 * This function does not accept triangle as a dynamic object, it is assumed
1395 * to always be static.
1397 * The triangle is also assumed to be one sided for better detection
1399 static int rb_sphere_vs_triangle( rigidbody
*rba
, v3f tri
[3], rb_ct
*buf
)
1403 closest_on_triangle( rba
->co
, tri
, co
);
1404 v3_sub( rba
->co
, co
, delta
);
1406 float d2
= v3_length2( delta
),
1407 r
= rba
->inf
.sphere
.radius
;
1412 v3_sub( tri
[1], tri
[0], ab
);
1413 v3_sub( tri
[2], tri
[0], ac
);
1414 v3_cross( ac
, ab
, tn
);
1416 if( v3_dot( delta
, tn
) > 0.0f
)
1417 v3_muls( delta
, -1.0f
, delta
);
1419 float d
= sqrtf(d2
);
1422 v3_muls( delta
, 1.0f
/d
, ct
->n
);
1423 v3_copy( co
, ct
->co
);
1426 ct
->rbb
= &rb_static_null
;
1433 static int sphere_vs_triangle( v3f c
, float r
, v3f tri
[3],
1434 v3f co
, v3f norm
, float *p
)
1437 closest_on_triangle( c
, tri
, co
);
1439 v3_sub( c
, co
, delta
);
1442 float d
= v3_length2( delta
);
1446 v3_sub( tri
[1], tri
[0], ab
);
1447 v3_sub( tri
[2], tri
[0], ac
);
1448 v3_cross( ac
, ab
, tn
);
1450 if( v3_dot( delta
, tn
) > 0.0f
)
1451 v3_muls( delta
, -1.0f
, delta
);
1453 vg_line_pt3( co
, 0.05f
, 0xff00ff00 );
1456 v3_muls( delta
, 1.0f
/d
, norm
);
1468 static void rb_solver_reset(void)
1470 rb_contact_count
= 0;
1473 static rb_ct
*rb_global_ct(void)
1475 return rb_contact_buffer
+ rb_contact_count
;
1479 static struct contact
*rb_start_contact(void)
1481 if( rb_contact_count
== vg_list_size(rb_contact_buffer
) )
1483 vg_error( "rigidbody: too many contacts generated (%u)\n",
1488 return &rb_contact_buffer
[ rb_contact_count
];
1491 static void rb_commit_contact( struct contact
*ct
, float p
)
1493 ct
->bias
= -0.2f
*k_rb_rate
*vg_minf(0.0f
,-p
+0.04f
);
1494 rb_tangent_basis( ct
->n
, ct
->t
[0], ct
->t
[1] );
1496 ct
->norm_impulse
= 0.0f
;
1497 ct
->tangent_impulse
[0] = 0.0f
;
1498 ct
->tangent_impulse
[1] = 0.0f
;
1500 rb_contact_count
++;
1503 static void rb_build_manifold_terrain_sphere( rigidbody
*rb
)
1507 int len
= bh_select( &world
.geo
.bhtris
, rb
->bbx_world
, geo
, 256 );
1509 for( int i
=0; i
<len
; i
++ )
1511 u32
*ptri
= &world
.geo
.indices
[ geo
[i
]*3 ];
1513 for( int j
=0; j
<3; j
++ )
1514 v3_copy( world
.geo
.verts
[ptri
[j
]].co
, tri
[j
] );
1516 vg_line(tri
[0],tri
[1],0xff00ff00 );
1517 vg_line(tri
[1],tri
[2],0xff00ff00 );
1518 vg_line(tri
[2],tri
[0],0xff00ff00 );
1523 for( int j
=0; j
<2; j
++ )
1525 if( sphere_vs_triangle( rb
->co
, rb
->inf
.sphere
.radius
, tri
,co
,norm
,&p
))
1527 struct contact
*ct
= rb_start_contact();
1533 v3_muladds( rb
->co
, norm
, p
, p1
);
1534 vg_line( rb
->co
, p1
, 0xffffffff );
1537 v3_copy( co
, ct
->co
);
1538 v3_copy( norm
, ct
->n
);
1539 rb_commit_contact( ct
, p
);
1546 static void rb_build_manifold_terrain( rigidbody
*rb
)
1550 float *p000
= pts
[0], *p001
= pts
[1], *p010
= pts
[2], *p011
= pts
[3],
1551 *p100
= pts
[4], *p101
= pts
[5], *p110
= pts
[6], *p111
= pts
[7];
1553 p000
[0]=box
[0][0];p000
[1]=box
[0][1];p000
[2]=box
[0][2];
1554 p001
[0]=box
[0][0];p001
[1]=box
[0][1];p001
[2]=box
[1][2];
1555 p010
[0]=box
[0][0];p010
[1]=box
[1][1];p010
[2]=box
[0][2];
1556 p011
[0]=box
[0][0];p011
[1]=box
[1][1];p011
[2]=box
[1][2];
1558 p100
[0]=box
[1][0];p100
[1]=box
[0][1];p100
[2]=box
[0][2];
1559 p101
[0]=box
[1][0];p101
[1]=box
[0][1];p101
[2]=box
[1][2];
1560 p110
[0]=box
[1][0];p110
[1]=box
[1][1];p110
[2]=box
[0][2];
1561 p111
[0]=box
[1][0];p111
[1]=box
[1][1];p111
[2]=box
[1][2];
1563 m4x3_mulv( rb
->to_world
, p000
, p000
);
1564 m4x3_mulv( rb
->to_world
, p001
, p001
);
1565 m4x3_mulv( rb
->to_world
, p010
, p010
);
1566 m4x3_mulv( rb
->to_world
, p011
, p011
);
1567 m4x3_mulv( rb
->to_world
, p100
, p100
);
1568 m4x3_mulv( rb
->to_world
, p101
, p101
);
1569 m4x3_mulv( rb
->to_world
, p110
, p110
);
1570 m4x3_mulv( rb
->to_world
, p111
, p111
);
1574 for( int i
=0; i
<8; i
++ )
1576 float *point
= pts
[i
];
1577 struct contact
*ct
= rb_start_contact();
1585 v3_copy( point
, surface
);
1589 hit
.dist
= INFINITY
;
1590 if( !ray_world( surface
, (v3f
){0.0f
,-1.0f
,0.0f
}, &hit
))
1593 v3_copy( hit
.pos
, surface
);
1595 float p
= vg_minf( surface
[1] - point
[1], 1.0f
);
1599 v3_copy( hit
.normal
, ct
->n
);
1600 v3_add( point
, surface
, ct
->co
);
1601 v3_muls( ct
->co
, 0.5f
, ct
->co
);
1603 rb_commit_contact( ct
, p
);
1613 * Initializing things like tangent vectors
1616 static void rb_presolve_contacts( rb_ct
*buffer
, int len
)
1618 for( int i
=0; i
<len
; i
++ )
1620 rb_ct
*ct
= &buffer
[i
];
1621 ct
->bias
= -0.2f
* k_rb_rate
* vg_minf(0.0f
,-ct
->p
+0.01f
);
1622 rb_tangent_basis( ct
->n
, ct
->t
[0], ct
->t
[1] );
1624 ct
->norm_impulse
= 0.0f
;
1625 ct
->tangent_impulse
[0] = 0.0f
;
1626 ct
->tangent_impulse
[1] = 0.0f
;
1628 v3f ra
, rb
, raCn
, rbCn
, raCt
, rbCt
;
1629 v3_sub( ct
->co
, ct
->rba
->co
, ra
);
1630 v3_sub( ct
->co
, ct
->rbb
->co
, rb
);
1631 v3_cross( ra
, ct
->n
, raCn
);
1632 v3_cross( rb
, ct
->n
, rbCn
);
1634 /* orient inverse inertia tensors */
1636 m3x3_mulv( ct
->rba
->iIw
, raCn
, raCnI
);
1637 m3x3_mulv( ct
->rbb
->iIw
, rbCn
, rbCnI
);
1639 ct
->normal_mass
= ct
->rba
->inv_mass
+ ct
->rbb
->inv_mass
;
1640 ct
->normal_mass
+= v3_dot( raCn
, raCnI
);
1641 ct
->normal_mass
+= v3_dot( rbCn
, rbCnI
);
1642 ct
->normal_mass
= 1.0f
/ct
->normal_mass
;
1644 for( int j
=0; j
<2; j
++ )
1647 v3_cross( ct
->t
[j
], ra
, raCt
);
1648 v3_cross( ct
->t
[j
], rb
, rbCt
);
1649 m3x3_mulv( ct
->rba
->iIw
, raCt
, raCtI
);
1650 m3x3_mulv( ct
->rbb
->iIw
, rbCt
, rbCtI
);
1652 ct
->tangent_mass
[j
] = ct
->rba
->inv_mass
+ ct
->rbb
->inv_mass
;
1653 ct
->tangent_mass
[j
] += v3_dot( raCt
, raCtI
);
1654 ct
->tangent_mass
[j
] += v3_dot( rbCt
, rbCtI
);
1655 ct
->tangent_mass
[j
] = 1.0f
/ct
->tangent_mass
[j
];
1658 rb_debug_contact( ct
);
1663 * Creates relative contact velocity vector, and offsets between each body
1665 static void rb_rcv( rb_ct
*ct
, v3f rv
, v3f da
, v3f db
)
1667 rigidbody
*rba
= ct
->rba
,
1670 v3_sub( ct
->co
, rba
->co
, da
);
1671 v3_sub( ct
->co
, rbb
->co
, db
);
1674 v3_cross( rba
->w
, da
, rva
);
1675 v3_add( rba
->v
, rva
, rva
);
1676 v3_cross( rbb
->w
, db
, rvb
);
1677 v3_add( rbb
->v
, rvb
, rvb
);
1679 v3_sub( rva
, rvb
, rv
);
1683 * Apply regular and angular velocity impulses to objects involved in contact
1685 static void rb_standard_impulse( rb_ct
*ct
, v3f da
, v3f db
, v3f impulse
)
1687 rigidbody
*rba
= ct
->rba
,
1690 v3_muladds( rba
->v
, impulse
, rba
->inv_mass
, rba
->v
);
1691 v3_muladds( rbb
->v
, impulse
, -rbb
->inv_mass
, rbb
->v
);
1693 /* Angular velocity */
1695 v3_cross( da
, impulse
, wa
);
1696 v3_negate( impulse
, invim
);
1697 v3_cross( db
, invim
, wb
);
1699 m3x3_mulv( ct
->rba
->iIw
, wa
, wa
);
1700 m3x3_mulv( ct
->rbb
->iIw
, wb
, wb
);
1701 v3_add( rba
->w
, wa
, rba
->w
);
1702 v3_add( rbb
->w
, wb
, rbb
->w
);
1706 * One iteration to solve the contact constraint
1708 static void rb_solve_contacts( rb_ct
*buf
, int len
)
1710 float k_friction
= 0.2f
;
1712 for( int i
=0; i
<len
; i
++ )
1714 struct contact
*ct
= &buf
[i
];
1715 rigidbody
*rb
= ct
->rba
;
1718 rb_rcv( ct
, rv
, da
, db
);
1721 for( int j
=0; j
<2; j
++ )
1723 float f
= k_friction
* ct
->norm_impulse
,
1724 vt
= v3_dot( rv
, ct
->t
[j
] ),
1725 lambda
= ct
->tangent_mass
[j
] * -vt
;
1727 float temp
= ct
->tangent_impulse
[j
];
1728 ct
->tangent_impulse
[j
] = vg_clampf( temp
+ lambda
, -f
, f
);
1729 lambda
= ct
->tangent_impulse
[j
] - temp
;
1732 v3_muls( ct
->t
[j
], lambda
, impulse
);
1733 rb_standard_impulse( ct
, da
, db
, impulse
);
1737 rb_rcv( ct
, rv
, da
, db
);
1738 float vn
= v3_dot( rv
, ct
->n
),
1739 lambda
= ct
->normal_mass
* (-vn
+ ct
->bias
);
1741 float temp
= ct
->norm_impulse
;
1742 ct
->norm_impulse
= vg_maxf( temp
+ lambda
, 0.0f
);
1743 lambda
= ct
->norm_impulse
- temp
;
1746 v3_muls( ct
->n
, lambda
, impulse
);
1747 rb_standard_impulse( ct
, da
, db
, impulse
);
1752 * The following ventures into not really very sophisticated at all maths
1755 struct rb_angle_limit
1757 rigidbody
*rba
, *rbb
;
1759 float impulse
, bias
;
1762 static int rb_angle_limit_force( rigidbody
*rba
, v3f va
,
1763 rigidbody
*rbb
, v3f vb
,
1767 m3x3_mulv( rba
->to_world
, va
, wva
);
1768 m3x3_mulv( rbb
->to_world
, vb
, wvb
);
1770 float dt
= v3_dot(wva
,wvb
)*0.999f
,
1775 float correction
= max
-ang
;
1778 v3_cross( wva
, wvb
, axis
);
1781 q_axis_angle( rotation
, axis
, -correction
*0.25f
);
1782 q_mul( rotation
, rba
->q
, rba
->q
);
1784 q_axis_angle( rotation
, axis
, correction
*0.25f
);
1785 q_mul( rotation
, rbb
->q
, rbb
->q
);
1793 static void rb_constraint_angle_limit( struct rb_angle_limit
*limit
)
1798 static void rb_constraint_angle( rigidbody
*rba
, v3f va
,
1799 rigidbody
*rbb
, v3f vb
,
1800 float max
, float spring
)
1803 m3x3_mulv( rba
->to_world
, va
, wva
);
1804 m3x3_mulv( rbb
->to_world
, vb
, wvb
);
1806 float dt
= v3_dot(wva
,wvb
)*0.999f
,
1810 v3_cross( wva
, wvb
, axis
);
1811 v3_muladds( rba
->w
, axis
, ang
*spring
*0.5f
, rba
->w
);
1812 v3_muladds( rbb
->w
, axis
, -ang
*spring
*0.5f
, rbb
->w
);
1816 /* TODO: convert max into the dot product value so we dont have to always
1817 * evaluate acosf, only if its greater than the angle specified */
1821 float correction
= max
-ang
;
1824 q_axis_angle( rotation
, axis
, -correction
*0.125f
);
1825 q_mul( rotation
, rba
->q
, rba
->q
);
1827 q_axis_angle( rotation
, axis
, correction
*0.125f
);
1828 q_mul( rotation
, rbb
->q
, rbb
->q
);
1832 static void rb_relative_velocity( rigidbody
*ra
, v3f lca
,
1833 rigidbody
*rb
, v3f lcb
, v3f rcv
)
1836 m3x3_mulv( ra
->to_world
, lca
, wca
);
1837 m3x3_mulv( rb
->to_world
, lcb
, wcb
);
1839 v3_sub( ra
->v
, rb
->v
, rcv
);
1842 v3_cross( ra
->w
, wca
, rcv_Ra
);
1843 v3_cross( rb
->w
, wcb
, rcv_Rb
);
1844 v3_add( rcv_Ra
, rcv
, rcv
);
1845 v3_sub( rcv
, rcv_Rb
, rcv
);
1848 static void rb_constraint_position( rigidbody
*ra
, v3f lca
,
1849 rigidbody
*rb
, v3f lcb
)
1851 /* C = (COa + Ra*LCa) - (COb + Rb*LCb) = 0 */
1853 m3x3_mulv( ra
->to_world
, lca
, wca
);
1854 m3x3_mulv( rb
->to_world
, lcb
, wcb
);
1857 v3_add( wcb
, rb
->co
, delta
);
1858 v3_sub( delta
, wca
, delta
);
1859 v3_sub( delta
, ra
->co
, delta
);
1861 v3_muladds( ra
->co
, delta
, 0.5f
, ra
->co
);
1862 v3_muladds( rb
->co
, delta
, -0.5f
, rb
->co
);
1865 v3_sub( ra
->v
, rb
->v
, rcv
);
1868 v3_cross( ra
->w
, wca
, rcv_Ra
);
1869 v3_cross( rb
->w
, wcb
, rcv_Rb
);
1870 v3_add( rcv_Ra
, rcv
, rcv
);
1871 v3_sub( rcv
, rcv_Rb
, rcv
);
1873 float nm
= 0.5f
/(rb
->inv_mass
+ ra
->inv_mass
);
1875 float mass_a
= 1.0f
/ra
->inv_mass
,
1876 mass_b
= 1.0f
/rb
->inv_mass
,
1877 total_mass
= mass_a
+mass_b
;
1880 v3_muls( rcv
, 1.0f
, impulse
);
1881 v3_muladds( rb
->v
, impulse
, mass_b
/total_mass
, rb
->v
);
1882 v3_cross( wcb
, impulse
, impulse
);
1883 v3_add( impulse
, rb
->w
, rb
->w
);
1885 v3_muls( rcv
, -1.0f
, impulse
);
1886 v3_muladds( ra
->v
, impulse
, mass_a
/total_mass
, ra
->v
);
1887 v3_cross( wca
, impulse
, impulse
);
1888 v3_add( impulse
, ra
->w
, ra
->w
);
1892 * this could be used for spring joints
1893 * its not good for position constraint
1896 v3_muls( delta
, 0.5f
*spring
, impulse
);
1898 v3_add( impulse
, ra
->v
, ra
->v
);
1899 v3_cross( wca
, impulse
, impulse
);
1900 v3_add( impulse
, ra
->w
, ra
->w
);
1902 v3_muls( delta
, -0.5f
*spring
, impulse
);
1904 v3_add( impulse
, rb
->v
, rb
->v
);
1905 v3_cross( wcb
, impulse
, impulse
);
1906 v3_add( impulse
, rb
->w
, rb
->w
);
1910 static void debug_sphere( m4x3f m
, float radius
, u32 colour
)
1912 v3f ly
= { 0.0f
, 0.0f
, radius
},
1913 lx
= { 0.0f
, radius
, 0.0f
},
1914 lz
= { 0.0f
, 0.0f
, radius
};
1916 for( int i
=0; i
<16; i
++ )
1918 float t
= ((float)(i
+1) * (1.0f
/16.0f
)) * VG_PIf
* 2.0f
,
1922 v3f py
= { s
*radius
, 0.0f
, c
*radius
},
1923 px
= { s
*radius
, c
*radius
, 0.0f
},
1924 pz
= { 0.0f
, s
*radius
, c
*radius
};
1926 v3f p0
, p1
, p2
, p3
, p4
, p5
;
1927 m4x3_mulv( m
, py
, p0
);
1928 m4x3_mulv( m
, ly
, p1
);
1929 m4x3_mulv( m
, px
, p2
);
1930 m4x3_mulv( m
, lx
, p3
);
1931 m4x3_mulv( m
, pz
, p4
);
1932 m4x3_mulv( m
, lz
, p5
);
1934 vg_line( p0
, p1
, colour
== 0x00? 0xff00ff00: colour
);
1935 vg_line( p2
, p3
, colour
== 0x00? 0xff0000ff: colour
);
1936 vg_line( p4
, p5
, colour
== 0x00? 0xffff0000: colour
);
1944 static void debug_capsule( m4x3f m
, float radius
, float h
, u32 colour
)
1946 v3f ly
= { 0.0f
, 0.0f
, radius
},
1947 lx
= { 0.0f
, radius
, 0.0f
},
1948 lz
= { 0.0f
, 0.0f
, radius
};
1950 float s0
= sinf(0.0f
)*radius
,
1951 c0
= cosf(0.0f
)*radius
;
1953 v3f p0
, p1
, up
, right
, forward
;
1954 m3x3_mulv( m
, (v3f
){0.0f
,1.0f
,0.0f
}, up
);
1955 m3x3_mulv( m
, (v3f
){1.0f
,0.0f
,0.0f
}, right
);
1956 m3x3_mulv( m
, (v3f
){0.0f
,0.0f
,-1.0f
}, forward
);
1957 v3_muladds( m
[3], up
, -h
*0.5f
+radius
, p0
);
1958 v3_muladds( m
[3], up
, h
*0.5f
-radius
, p1
);
1961 v3_muladds( p0
, right
, radius
, a0
);
1962 v3_muladds( p1
, right
, radius
, a1
);
1963 v3_muladds( p0
, forward
, radius
, b0
);
1964 v3_muladds( p1
, forward
, radius
, b1
);
1965 vg_line( a0
, a1
, colour
);
1966 vg_line( b0
, b1
, colour
);
1968 v3_muladds( p0
, right
, -radius
, a0
);
1969 v3_muladds( p1
, right
, -radius
, a1
);
1970 v3_muladds( p0
, forward
, -radius
, b0
);
1971 v3_muladds( p1
, forward
, -radius
, b1
);
1972 vg_line( a0
, a1
, colour
);
1973 vg_line( b0
, b1
, colour
);
1975 for( int i
=0; i
<16; i
++ )
1977 float t
= ((float)(i
+1) * (1.0f
/16.0f
)) * VG_PIf
* 2.0f
,
1978 s1
= sinf(t
)*radius
,
1979 c1
= cosf(t
)*radius
;
1981 v3f e0
= { s0
, 0.0f
, c0
},
1982 e1
= { s1
, 0.0f
, c1
},
1983 e2
= { s0
, c0
, 0.0f
},
1984 e3
= { s1
, c1
, 0.0f
},
1985 e4
= { 0.0f
, c0
, s0
},
1986 e5
= { 0.0f
, c1
, s1
};
1988 m3x3_mulv( m
, e0
, e0
);
1989 m3x3_mulv( m
, e1
, e1
);
1990 m3x3_mulv( m
, e2
, e2
);
1991 m3x3_mulv( m
, e3
, e3
);
1992 m3x3_mulv( m
, e4
, e4
);
1993 m3x3_mulv( m
, e5
, e5
);
1995 v3_add( p0
, e0
, a0
);
1996 v3_add( p0
, e1
, a1
);
1997 v3_add( p1
, e0
, b0
);
1998 v3_add( p1
, e1
, b1
);
2000 vg_line( a0
, a1
, colour
);
2001 vg_line( b0
, b1
, colour
);
2005 v3_add( p0
, e2
, a0
);
2006 v3_add( p0
, e3
, a1
);
2007 v3_add( p0
, e4
, b0
);
2008 v3_add( p0
, e5
, b1
);
2012 v3_add( p1
, e2
, a0
);
2013 v3_add( p1
, e3
, a1
);
2014 v3_add( p1
, e4
, b0
);
2015 v3_add( p1
, e5
, b1
);
2018 vg_line( a0
, a1
, colour
);
2019 vg_line( b0
, b1
, colour
);
2026 static void rb_debug( rigidbody
*rb
, u32 colour
)
2028 if( rb
->type
== k_rb_shape_box
)
2031 vg_line_boxf_transformed( rb
->to_world
, rb
->bbx
, colour
);
2033 else if( rb
->type
== k_rb_shape_sphere
)
2035 debug_sphere( rb
->to_world
, rb
->inf
.sphere
.radius
, colour
);
2037 else if( rb
->type
== k_rb_shape_capsule
)
2040 float h
= rb
->inf
.capsule
.height
,
2041 r
= rb
->inf
.capsule
.radius
;
2043 debug_capsule( rb
->to_world
, r
, h
, colour
);
2045 else if( rb
->type
== k_rb_shape_scene
)
2047 vg_line_boxf( rb
->bbx
, colour
);
2053 * out penetration distance, normal
2055 static int rb_point_in_body( rigidbody
*rb
, v3f pos
, float *pen
, v3f normal
)
2058 m4x3_mulv( rb
->to_local
, pos
, local
);
2060 if( local
[0] > rb
->bbx
[0][0] && local
[0] < rb
->bbx
[1][0] &&
2061 local
[1] > rb
->bbx
[0][1] && local
[1] < rb
->bbx
[1][1] &&
2062 local
[2] > rb
->bbx
[0][2] && local
[2] < rb
->bbx
[1][2] )
2064 v3f area
, com
, comrel
;
2065 v3_add( rb
->bbx
[0], rb
->bbx
[1], com
);
2066 v3_muls( com
, 0.5f
, com
);
2068 v3_sub( rb
->bbx
[1], rb
->bbx
[0], area
);
2069 v3_sub( local
, com
, comrel
);
2070 v3_div( comrel
, area
, comrel
);
2073 float max_mag
= fabsf(comrel
[0]);
2075 if( fabsf(comrel
[1]) > max_mag
)
2078 max_mag
= fabsf(comrel
[1]);
2080 if( fabsf(comrel
[2]) > max_mag
)
2083 max_mag
= fabsf(comrel
[2]);
2087 normal
[axis
] = vg_signf(comrel
[axis
]);
2089 if( normal
[axis
] < 0.0f
)
2090 *pen
= local
[axis
] - rb
->bbx
[0][axis
];
2092 *pen
= rb
->bbx
[1][axis
] - local
[axis
];
2094 m3x3_mulv( rb
->to_world
, normal
, normal
);
2102 * BVH implementation, this is ONLY for static rigidbodies, its to slow for
2106 static void rb_bh_expand_bound( void *user
, boxf bound
, u32 item_index
)
2108 rigidbody
*rb
= &((rigidbody
*)user
)[ item_index
];
2109 box_concat( bound
, rb
->bbx_world
);
2112 static float rb_bh_centroid( void *user
, u32 item_index
, int axis
)
2114 rigidbody
*rb
= &((rigidbody
*)user
)[ item_index
];
2115 return (rb
->bbx_world
[axis
][0] + rb
->bbx_world
[1][axis
]) * 0.5f
;
2118 static void rb_bh_swap( void *user
, u32 ia
, u32 ib
)
2120 rigidbody temp
, *rba
, *rbb
;
2121 rba
= &((rigidbody
*)user
)[ ia
];
2122 rbb
= &((rigidbody
*)user
)[ ib
];
2129 static void rb_bh_debug( void *user
, u32 item_index
)
2131 rigidbody
*rb
= &((rigidbody
*)user
)[ item_index
];
2132 rb_debug( rb
, 0xff00ffff );
2135 static bh_system bh_system_rigidbodies
=
2137 .expand_bound
= rb_bh_expand_bound
,
2138 .item_centroid
= rb_bh_centroid
,
2139 .item_swap
= rb_bh_swap
,
2140 .item_debug
= rb_bh_debug
,
2146 #endif /* RIGIDBODY_H */