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 v3f delta
; /* where is the origin of this in relation to a parent body
67 TODO: Move this somewhere other than rigidbody struct
68 it is only used by character.h's ragdoll
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 mass_total
, p
, bias
, norm_impulse
, tangent_impulse
[2];
97 rb_contact_buffer
[256];
98 static int rb_contact_count
= 0;
100 static void rb_update_bounds( rigidbody
*rb
)
102 box_copy( rb
->bbx
, rb
->bbx_world
);
103 m4x3_transform_aabb( rb
->to_world
, rb
->bbx_world
);
106 static void rb_update_transform( rigidbody
*rb
)
108 q_normalize( rb
->q
);
109 q_m3x3( rb
->q
, rb
->to_world
);
110 v3_copy( rb
->co
, rb
->to_world
[3] );
112 m4x3_invert_affine( rb
->to_world
, rb
->to_local
);
114 m3x3_mulv( rb
->to_world
, (v3f
){1.0f
,0.0f
, 0.0f
}, rb
->right
);
115 m3x3_mulv( rb
->to_world
, (v3f
){0.0f
,1.0f
, 0.0f
}, rb
->up
);
116 m3x3_mulv( rb
->to_world
, (v3f
){0.0f
,0.0f
,-1.0f
}, rb
->forward
);
118 rb_update_bounds( rb
);
121 static float sphere_volume( float radius
)
123 float r3
= radius
*radius
*radius
;
124 return (4.0f
/3.0f
) * VG_PIf
* r3
;
127 static void rb_init( rigidbody
*rb
)
131 if( rb
->type
== k_rb_shape_box
)
134 v3_sub( rb
->bbx
[1], rb
->bbx
[0], dims
);
135 volume
= dims
[0]*dims
[1]*dims
[2];
137 else if( rb
->type
== k_rb_shape_sphere
)
139 volume
= sphere_volume( rb
->inf
.sphere
.radius
);
140 v3_fill( rb
->bbx
[0], -rb
->inf
.sphere
.radius
);
141 v3_fill( rb
->bbx
[1], rb
->inf
.sphere
.radius
);
143 else if( rb
->type
== k_rb_shape_capsule
)
145 float r
= rb
->inf
.capsule
.radius
,
146 h
= rb
->inf
.capsule
.height
;
147 volume
= sphere_volume( r
) + VG_PIf
* r
*r
* (h
- r
*2.0f
);
149 v3_fill( rb
->bbx
[0], -rb
->inf
.sphere
.radius
);
150 v3_fill( rb
->bbx
[1], rb
->inf
.sphere
.radius
);
154 else if( rb
->type
== k_rb_shape_scene
)
157 box_copy( rb
->inf
.scene
.pscene
->bbx
, rb
->bbx
);
166 rb
->inv_mass
= 1.0f
/(8.0f
*volume
);
172 rb_update_transform( rb
);
175 static void rb_iter( rigidbody
*rb
)
177 v3f gravity
= { 0.0f
, -9.6f
, 0.0f
};
178 v3_muladds( rb
->v
, gravity
, k_rb_delta
, rb
->v
);
180 /* intergrate velocity */
181 v3_muladds( rb
->co
, rb
->v
, k_rb_delta
, rb
->co
);
182 v3_lerp( rb
->w
, (v3f
){0.0f
,0.0f
,0.0f
}, 0.0025f
, rb
->w
);
184 /* inegrate inertia */
185 if( v3_length2( rb
->w
) > 0.0f
)
189 v3_copy( rb
->w
, axis
);
191 float mag
= v3_length( axis
);
192 v3_divs( axis
, mag
, axis
);
193 q_axis_angle( rotation
, axis
, mag
*k_rb_delta
);
194 q_mul( rotation
, rb
->q
, rb
->q
);
198 static void rb_torque( rigidbody
*rb
, v3f axis
, float mag
)
200 v3_muladds( rb
->w
, axis
, mag
*k_rb_delta
, rb
->w
);
203 static void rb_tangent_basis( v3f n
, v3f tx
, v3f ty
)
205 /* Compute tangent basis (box2d) */
206 if( fabsf( n
[0] ) >= 0.57735027f
)
220 v3_cross( n
, tx
, ty
);
223 static void rb_solver_reset(void);
225 static void rb_build_manifold_terrain( rigidbody
*rb
);
226 static void rb_build_manifold_terrain_sphere( rigidbody
*rb
);
228 static void rb_solve_contacts( rb_ct
*buf
, int len
);
229 static void rb_presolve_contacts( rb_ct
*buffer
, int len
);
232 * These closest point tests were learned from Real-Time Collision Detection by
235 static float closest_segment_segment( v3f p1
, v3f q1
, v3f p2
, v3f q2
,
236 float *s
, float *t
, v3f c1
, v3f c2
)
239 v3_sub( q1
, p1
, d1
);
240 v3_sub( q2
, p2
, d2
);
243 float a
= v3_length2( d1
),
244 e
= v3_length2( d2
),
247 const float kEpsilon
= 0.0001f
;
249 if( a
<= kEpsilon
&& e
<= kEpsilon
)
257 v3_sub( c1
, c2
, v0
);
259 return v3_length2( v0
);
265 *t
= vg_clampf( f
/ e
, 0.0f
, 1.0f
);
269 float c
= v3_dot( d1
, r
);
273 *s
= vg_clampf( -c
/ a
, 0.0f
, 1.0f
);
277 float b
= v3_dot(d1
,d2
),
282 *s
= vg_clampf((b
*f
- c
*e
)/d
, 0.0f
, 1.0f
);
294 *s
= vg_clampf( -c
/ a
, 0.0f
, 1.0f
);
299 *s
= vg_clampf((b
-c
)/a
,0.0f
,1.0f
);
304 v3_muladds( p1
, d1
, *s
, c1
);
305 v3_muladds( p2
, d2
, *t
, c2
);
308 v3_sub( c1
, c2
, v0
);
309 return v3_length2( v0
);
312 static void closest_point_aabb( v3f p
, boxf box
, v3f dest
)
314 v3_maxv( p
, box
[0], dest
);
315 v3_minv( dest
, box
[1], dest
);
318 static void closest_point_obb( v3f p
, rigidbody
*rb
, v3f dest
)
321 m4x3_mulv( rb
->to_local
, p
, local
);
322 closest_point_aabb( local
, rb
->bbx
, local
);
323 m4x3_mulv( rb
->to_world
, local
, dest
);
326 static float closest_point_segment( v3f a
, v3f b
, v3f point
, v3f dest
)
330 v3_sub( point
, a
, v1
);
332 float t
= v3_dot( v1
, v0
) / v3_length2(v0
);
333 t
= vg_clampf(t
,0.0f
,1.0f
);
334 v3_muladds( a
, v0
, t
, dest
);
338 static void closest_on_triangle( v3f p
, v3f tri
[3], v3f dest
)
343 /* Region outside A */
344 v3_sub( tri
[1], tri
[0], ab
);
345 v3_sub( tri
[2], tri
[0], ac
);
346 v3_sub( p
, tri
[0], ap
);
350 if( d1
<= 0.0f
&& d2
<= 0.0f
)
352 v3_copy( tri
[0], dest
);
353 v3_copy( (v3f
){INFINITY
,INFINITY
,INFINITY
}, dest
);
357 /* Region outside B */
361 v3_sub( p
, tri
[1], bp
);
362 d3
= v3_dot( ab
, bp
);
363 d4
= v3_dot( ac
, bp
);
365 if( d3
>= 0.0f
&& d4
<= d3
)
367 v3_copy( tri
[1], dest
);
368 v3_copy( (v3f
){INFINITY
,INFINITY
,INFINITY
}, dest
);
372 /* Edge region of AB */
373 float vc
= d1
*d4
- d3
*d2
;
374 if( vc
<= 0.0f
&& d1
>= 0.0f
&& d3
<= 0.0f
)
376 float v
= d1
/ (d1
-d3
);
377 v3_muladds( tri
[0], ab
, v
, dest
);
378 v3_copy( (v3f
){INFINITY
,INFINITY
,INFINITY
}, dest
);
382 /* Region outside C */
385 v3_sub( p
, tri
[2], cp
);
389 if( d6
>= 0.0f
&& d5
<= d6
)
391 v3_copy( tri
[2], dest
);
392 v3_copy( (v3f
){INFINITY
,INFINITY
,INFINITY
}, dest
);
397 float vb
= d5
*d2
- d1
*d6
;
398 if( vb
<= 0.0f
&& d2
>= 0.0f
&& d6
<= 0.0f
)
400 float w
= d2
/ (d2
-d6
);
401 v3_muladds( tri
[0], ac
, w
, dest
);
402 v3_copy( (v3f
){INFINITY
,INFINITY
,INFINITY
}, dest
);
407 float va
= d3
*d6
- d5
*d4
;
408 if( va
<= 0.0f
&& (d4
-d3
) >= 0.0f
&& (d5
-d6
) >= 0.0f
)
410 float w
= (d4
-d3
) / ((d4
-d3
) + (d5
-d6
));
412 v3_sub( tri
[2], tri
[1], bc
);
413 v3_muladds( tri
[1], bc
, w
, dest
);
414 v3_copy( (v3f
){INFINITY
,INFINITY
,INFINITY
}, dest
);
418 /* P inside region, Q via barycentric coordinates uvw */
419 float d
= 1.0f
/(va
+vb
+vc
),
423 v3_muladds( tri
[0], ab
, v
, dest
);
424 v3_muladds( dest
, ac
, w
, dest
);
430 * These do not automatically allocate contacts, an appropriately sized
431 * buffer must be supplied. The function returns the size of the manifold
432 * which was generated.
434 * The values set on the contacts are: n, co, p, rba, rbb
437 static void rb_debug_contact( rb_ct
*ct
)
440 v3_muladds( ct
->co
, ct
->n
, 0.2f
, p1
);
441 vg_line_pt3( ct
->co
, 0.1f
, 0xff0000ff );
442 vg_line( ct
->co
, p1
, 0xffffffff );
446 * By collecting the minimum(time) and maximum(time) pairs of points, we
447 * build a reduced and stable exact manifold.
450 * rx: minimum distance of these points
451 * dx: the delta between the two points
453 * pairs will only ammend these if they are creating a collision
455 typedef struct capsule_manifold capsule_manifold
;
456 struct capsule_manifold
464 * Expand a line manifold with a new pair. t value is the time along segment
465 * on the oriented object which created this pair.
467 static void rb_capsule_manifold( v3f pa
, v3f pb
, float t
, float r
,
468 capsule_manifold
*manifold
)
471 v3_sub( pa
, pb
, delta
);
473 if( v3_length2(delta
) < r
*r
)
475 if( t
< manifold
->t0
)
477 v3_copy( delta
, manifold
->d0
);
482 if( t
> manifold
->t1
)
484 v3_copy( delta
, manifold
->d1
);
491 static void rb_capsule_manifold_init( capsule_manifold
*manifold
)
493 manifold
->t0
= INFINITY
;
494 manifold
->t1
= -INFINITY
;
497 static int rb_capsule_manifold_done( rigidbody
*rba
, rigidbody
*rbb
,
498 capsule_manifold
*manifold
, rb_ct
*buf
)
500 float h
= rba
->inf
.capsule
.height
,
501 ra
= rba
->inf
.capsule
.radius
;
504 v3_muladds( rba
->co
, rba
->up
, -h
*0.5f
+ra
, p0
);
505 v3_muladds( rba
->co
, rba
->up
, h
*0.5f
-ra
, p1
);
508 if( manifold
->t0
<= 1.0f
)
513 v3_muls( p0
, 1.0f
-manifold
->t0
, pa
);
514 v3_muladds( pa
, p1
, manifold
->t0
, pa
);
516 float d
= v3_length( manifold
->d0
);
517 v3_muls( manifold
->d0
, 1.0f
/d
, ct
->n
);
518 v3_muladds( pa
, ct
->n
, -ra
, ct
->co
);
520 ct
->p
= manifold
->r0
- d
;
527 if( (manifold
->t1
>= 0.0f
) && (manifold
->t0
!= manifold
->t1
) )
529 rb_ct
*ct
= buf
+count
;
532 v3_muls( p0
, 1.0f
-manifold
->t1
, pa
);
533 v3_muladds( pa
, p1
, manifold
->t1
, pa
);
535 float d
= v3_length( manifold
->d1
);
536 v3_muls( manifold
->d1
, 1.0f
/d
, ct
->n
);
537 v3_muladds( pa
, ct
->n
, -ra
, ct
->co
);
539 ct
->p
= manifold
->r1
- d
;
551 vg_line( buf
[0].co
, buf
[1].co
, 0xff0000ff );
556 static int rb_capsule_vs_sphere( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
558 float h
= rba
->inf
.capsule
.height
,
559 ra
= rba
->inf
.capsule
.radius
,
560 rb
= rbb
->inf
.sphere
.radius
;
563 v3_muladds( rba
->co
, rba
->up
, -h
*0.5f
+ra
, p0
);
564 v3_muladds( rba
->co
, rba
->up
, h
*0.5f
-ra
, p1
);
567 closest_point_segment( p0
, p1
, rbb
->co
, c
);
568 v3_sub( c
, rbb
->co
, delta
);
570 float d2
= v3_length2(delta
),
578 v3_muls( delta
, 1.0f
/d
, ct
->n
);
582 v3_muladds( c
, ct
->n
, -ra
, p0
);
583 v3_muladds( rbb
->co
, ct
->n
, rb
, p1
);
584 v3_add( p0
, p1
, ct
->co
);
585 v3_muls( ct
->co
, 0.5f
, ct
->co
);
596 static int rb_capsule_vs_capsule( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
598 float ha
= rba
->inf
.capsule
.height
,
599 hb
= rbb
->inf
.capsule
.height
,
600 ra
= rba
->inf
.capsule
.radius
,
601 rb
= rbb
->inf
.capsule
.radius
,
605 v3_muladds( rba
->co
, rba
->up
, -ha
*0.5f
+ra
, p0
);
606 v3_muladds( rba
->co
, rba
->up
, ha
*0.5f
-ra
, p1
);
607 v3_muladds( rbb
->co
, rbb
->up
, -hb
*0.5f
+rb
, p2
);
608 v3_muladds( rbb
->co
, rbb
->up
, hb
*0.5f
-rb
, p3
);
610 capsule_manifold manifold
;
611 rb_capsule_manifold_init( &manifold
);
615 closest_segment_segment( p0
, p1
, p2
, p3
, &ta
, &tb
, pa
, pb
);
616 rb_capsule_manifold( pa
, pb
, ta
, r
, &manifold
);
618 ta
= closest_point_segment( p0
, p1
, p2
, pa
);
619 tb
= closest_point_segment( p0
, p1
, p3
, pb
);
620 rb_capsule_manifold( pa
, p2
, ta
, r
, &manifold
);
621 rb_capsule_manifold( pb
, p3
, tb
, r
, &manifold
);
623 closest_point_segment( p2
, p3
, p0
, pa
);
624 closest_point_segment( p2
, p3
, p1
, pb
);
625 rb_capsule_manifold( p0
, pa
, 0.0f
, r
, &manifold
);
626 rb_capsule_manifold( p1
, pb
, 1.0f
, r
, &manifold
);
628 return rb_capsule_manifold_done( rba
, rbb
, &manifold
, buf
);
632 * Generates up to two contacts; optimised for the most stable manifold
634 static int rb_capsule_vs_box( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
636 float h
= rba
->inf
.capsule
.height
,
637 r
= rba
->inf
.capsule
.radius
;
640 * Solving this in symetric local space of the cube saves us some time and a
641 * couple branches when it comes to the quad stage.
644 v3_add( rbb
->bbx
[0], rbb
->bbx
[1], centroid
);
645 v3_muls( centroid
, 0.5f
, centroid
);
648 v3_sub( rbb
->bbx
[0], centroid
, bbx
[0] );
649 v3_sub( rbb
->bbx
[1], centroid
, bbx
[1] );
651 v3f pc
, p0w
, p1w
, p0
, p1
;
652 v3_muladds( rba
->co
, rba
->up
, -h
*0.5f
+r
, p0w
);
653 v3_muladds( rba
->co
, rba
->up
, h
*0.5f
-r
, p1w
);
655 m4x3_mulv( rbb
->to_local
, p0w
, p0
);
656 m4x3_mulv( rbb
->to_local
, p1w
, p1
);
657 v3_sub( p0
, centroid
, p0
);
658 v3_sub( p1
, centroid
, p1
);
659 v3_add( p0
, p1
, pc
);
660 v3_muls( pc
, 0.5f
, pc
);
663 * Finding an appropriate quad to collide lines with
666 v3_div( pc
, bbx
[1], region
);
669 if( (fabsf(region
[0]) > fabsf(region
[1])) &&
670 (fabsf(region
[0]) > fabsf(region
[2])) )
672 float px
= vg_signf(region
[0]) * bbx
[1][0];
673 v3_copy( (v3f
){ px
, bbx
[0][1], bbx
[0][2] }, quad
[0] );
674 v3_copy( (v3f
){ px
, bbx
[1][1], bbx
[0][2] }, quad
[1] );
675 v3_copy( (v3f
){ px
, bbx
[1][1], bbx
[1][2] }, quad
[2] );
676 v3_copy( (v3f
){ px
, bbx
[0][1], bbx
[1][2] }, quad
[3] );
678 else if( fabsf(region
[1]) > fabsf(region
[2]) )
680 float py
= vg_signf(region
[1]) * bbx
[1][1];
681 v3_copy( (v3f
){ bbx
[0][0], py
, bbx
[0][2] }, quad
[0] );
682 v3_copy( (v3f
){ bbx
[1][0], py
, bbx
[0][2] }, quad
[1] );
683 v3_copy( (v3f
){ bbx
[1][0], py
, bbx
[1][2] }, quad
[2] );
684 v3_copy( (v3f
){ bbx
[0][0], py
, bbx
[1][2] }, quad
[3] );
688 float pz
= vg_signf(region
[2]) * bbx
[1][2];
689 v3_copy( (v3f
){ bbx
[0][0], bbx
[0][1], pz
}, quad
[0] );
690 v3_copy( (v3f
){ bbx
[1][0], bbx
[0][1], pz
}, quad
[1] );
691 v3_copy( (v3f
){ bbx
[1][0], bbx
[1][1], pz
}, quad
[2] );
692 v3_copy( (v3f
){ bbx
[0][0], bbx
[1][1], pz
}, quad
[3] );
695 capsule_manifold manifold
;
696 rb_capsule_manifold_init( &manifold
);
699 closest_point_aabb( p0
, bbx
, c0
);
700 closest_point_aabb( p1
, bbx
, c1
);
703 v3_sub( c0
, p0
, d0
);
704 v3_sub( c1
, p1
, d1
);
705 v3_sub( p1
, p0
, da
);
712 if( v3_dot( da
, d0
) <= 0.01f
)
713 rb_capsule_manifold( p0
, c0
, 0.0f
, r
, &manifold
);
715 if( v3_dot( da
, d1
) >= -0.01f
)
716 rb_capsule_manifold( p1
, c1
, 1.0f
, r
, &manifold
);
718 for( int i
=0; i
<4; i
++ )
725 closest_segment_segment( p0
, p1
, quad
[i0
], quad
[i1
], &ta
, &tb
, ca
, cb
);
726 rb_capsule_manifold( ca
, cb
, ta
, r
, &manifold
);
730 * Create final contacts based on line manifold
732 m3x3_mulv( rbb
->to_world
, manifold
.d0
, manifold
.d0
);
733 m3x3_mulv( rbb
->to_world
, manifold
.d1
, manifold
.d1
);
740 for( int i
=0; i
<4; i
++ )
746 v3_add( quad
[i0
], centroid
, q0
);
747 v3_add( quad
[i1
], centroid
, q1
);
749 m4x3_mulv( rbb
->to_world
, q0
, q0
);
750 m4x3_mulv( rbb
->to_world
, q1
, q1
);
752 vg_line( q0
, q1
, 0xffffffff );
756 return rb_capsule_manifold_done( rba
, rbb
, &manifold
, buf
);
759 static int rb_sphere_vs_box( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
763 closest_point_obb( rba
->co
, rbb
, co
);
764 v3_sub( rba
->co
, co
, delta
);
766 float d2
= v3_length2(delta
),
767 r
= rba
->inf
.sphere
.radius
;
776 v3_sub( rba
->co
, rbb
->co
, delta
);
779 * some extra testing is required to find the best axis to push the
780 * object back outside the box. Since there isnt a clear seperating
781 * vector already, especially on really high aspect boxes.
783 float lx
= v3_dot( rbb
->right
, delta
),
784 ly
= v3_dot( rbb
->up
, delta
),
785 lz
= v3_dot( rbb
->forward
, delta
),
786 px
= rbb
->bbx
[1][0] - fabsf(lx
),
787 py
= rbb
->bbx
[1][1] - fabsf(ly
),
788 pz
= rbb
->bbx
[1][2] - fabsf(lz
);
790 if( px
< py
&& px
< pz
)
791 v3_muls( rbb
->right
, vg_signf(lx
), ct
->n
);
793 v3_muls( rbb
->up
, vg_signf(ly
), ct
->n
);
795 v3_muls( rbb
->forward
, vg_signf(lz
), ct
->n
);
797 v3_muladds( rba
->co
, ct
->n
, -r
, ct
->co
);
803 v3_muls( delta
, 1.0f
/d
, ct
->n
);
805 v3_copy( co
, ct
->co
);
816 static int rb_sphere_vs_sphere( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
819 v3_sub( rba
->co
, rbb
->co
, delta
);
821 float d2
= v3_length2(delta
),
822 r
= rba
->inf
.sphere
.radius
+ rbb
->inf
.sphere
.radius
;
829 v3_muls( delta
, 1.0f
/d
, ct
->n
);
832 v3_muladds( rba
->co
, ct
->n
,-rba
->inf
.sphere
.radius
, p0
);
833 v3_muladds( rbb
->co
, ct
->n
, rbb
->inf
.sphere
.radius
, p1
);
834 v3_add( p0
, p1
, ct
->co
);
835 v3_muls( ct
->co
, 0.5f
, ct
->co
);
845 /* TODO: these guys */
847 static int rb_capsule_vs_scene( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
851 int len
= bh_select( &rbb
->inf
.scene
.pscene
->bhtris
,
852 rba
->bbx_world
, geo
, 128 );
857 static int rb_sphere_vs_triangle( rigidbody
*rba
, rigidbody
*rbb
,
858 v3f tri
[3], rb_ct
*buf
)
862 closest_on_triangle( rba
->co
, tri
, co
);
863 v3_sub( rba
->co
, co
, delta
);
865 vg_line( rba
->co
, co
, 0xffff0000 );
866 vg_line_pt3( rba
->co
, 0.1f
, 0xff00ffff );
868 float d2
= v3_length2( delta
),
869 r
= rba
->inf
.sphere
.radius
;
876 v3_sub( tri
[2], tri
[0], ab
);
877 v3_sub( tri
[1], tri
[0], ac
);
878 v3_cross( ac
, ab
, tn
);
879 v3_copy( tn
, ct
->n
);
880 v3_normalize( ct
->n
);
884 v3_copy( co
, ct
->co
);
894 static int rb_sphere_vs_scene( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
896 scene
*sc
= rbb
->inf
.scene
.pscene
;
900 int len
= bh_select( &sc
->bhtris
, rba
->bbx_world
, geo
, 128 );
904 for( int i
=0; i
<len
; i
++ )
906 u32
*ptri
= &sc
->indices
[ geo
[i
]*3 ];
908 for( int j
=0; j
<3; j
++ )
909 v3_copy( sc
->verts
[ptri
[j
]].co
, tri
[j
] );
911 vg_line(tri
[0],tri
[1],0xff00ff00 );
912 vg_line(tri
[1],tri
[2],0xff00ff00 );
913 vg_line(tri
[2],tri
[0],0xff00ff00 );
915 count
+= rb_sphere_vs_triangle( rba
, rbb
, tri
, buf
+count
);
919 vg_warn( "Exceeding sphere_vs_scene capacity. Geometry too dense!\n" );
927 static int RB_MATRIX_ERROR( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
929 vg_error( "Collision type is unimplemented between types %d and %d\n",
930 rba
->type
, rbb
->type
);
935 static int rb_sphere_vs_capsule( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
937 return rb_capsule_vs_sphere( rbb
, rba
, buf
);
940 static int rb_box_vs_capsule( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
942 return rb_capsule_vs_box( rbb
, rba
, buf
);
945 static int rb_box_vs_sphere( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
947 return rb_sphere_vs_box( rbb
, rba
, buf
);
950 static int (*rb_jump_table
[4][4])( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
952 /* box */ /* Sphere */ /* Capsule */
953 /*box */ { RB_MATRIX_ERROR
, rb_box_vs_sphere
, rb_box_vs_capsule
, RB_MATRIX_ERROR
},
954 /*sphere */ { rb_sphere_vs_box
, rb_sphere_vs_sphere
, rb_sphere_vs_capsule
, rb_sphere_vs_scene
},
955 /*capsule*/ { rb_capsule_vs_box
,rb_capsule_vs_sphere
,rb_capsule_vs_capsule
,RB_MATRIX_ERROR
},
956 /*mesh */ { RB_MATRIX_ERROR
, RB_MATRIX_ERROR
, RB_MATRIX_ERROR
, RB_MATRIX_ERROR
}
959 static int rb_collide( rigidbody
*rba
, rigidbody
*rbb
)
961 int (*collider_jump
)(rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
962 = rb_jump_table
[rba
->type
][rbb
->type
];
965 * 12 is the maximum manifold size we can generate, so we are forced to abort
966 * potentially checking any more.
968 if( rb_contact_count
+ 12 > vg_list_size(rb_contact_buffer
) )
970 vg_warn( "Too many contacts made in global collider buffer (%d of %d\n)",
971 rb_contact_count
, vg_list_size(rb_contact_buffer
) );
976 * TODO: Replace this with a more dedicated broad phase pass
978 if( box_overlap( rba
->bbx_world
, rbb
->bbx_world
) )
980 int count
= collider_jump( rba
, rbb
, rb_contact_buffer
+rb_contact_count
);
981 rb_contact_count
+= count
;
994 * This function does not accept triangle as a dynamic object, it is assumed
995 * to always be static.
997 * The triangle is also assumed to be one sided for better detection
999 static int rb_sphere_vs_triangle( rigidbody
*rba
, v3f tri
[3], rb_ct
*buf
)
1003 closest_on_triangle( rba
->co
, tri
, co
);
1004 v3_sub( rba
->co
, co
, delta
);
1006 float d2
= v3_length2( delta
),
1007 r
= rba
->inf
.sphere
.radius
;
1012 v3_sub( tri
[1], tri
[0], ab
);
1013 v3_sub( tri
[2], tri
[0], ac
);
1014 v3_cross( ac
, ab
, tn
);
1016 if( v3_dot( delta
, tn
) > 0.0f
)
1017 v3_muls( delta
, -1.0f
, delta
);
1019 float d
= sqrtf(d2
);
1022 v3_muls( delta
, 1.0f
/d
, ct
->n
);
1023 v3_copy( co
, ct
->co
);
1026 ct
->rbb
= &rb_static_null
;
1033 static int sphere_vs_triangle( v3f c
, float r
, v3f tri
[3],
1034 v3f co
, v3f norm
, float *p
)
1037 closest_on_triangle( c
, tri
, co
);
1039 v3_sub( c
, co
, delta
);
1042 float d
= v3_length2( delta
);
1046 v3_sub( tri
[1], tri
[0], ab
);
1047 v3_sub( tri
[2], tri
[0], ac
);
1048 v3_cross( ac
, ab
, tn
);
1050 if( v3_dot( delta
, tn
) > 0.0f
)
1051 v3_muls( delta
, -1.0f
, delta
);
1053 vg_line_pt3( co
, 0.05f
, 0xff00ff00 );
1056 v3_muls( delta
, 1.0f
/d
, norm
);
1068 static void rb_solver_reset(void)
1070 rb_contact_count
= 0;
1073 static rb_ct
*rb_global_ct(void)
1075 return rb_contact_buffer
+ rb_contact_count
;
1079 static struct contact
*rb_start_contact(void)
1081 if( rb_contact_count
== vg_list_size(rb_contact_buffer
) )
1083 vg_error( "rigidbody: too many contacts generated (%u)\n",
1088 return &rb_contact_buffer
[ rb_contact_count
];
1091 static void rb_commit_contact( struct contact
*ct
, float p
)
1093 ct
->bias
= -0.2f
*k_rb_rate
*vg_minf(0.0f
,-p
+0.04f
);
1094 rb_tangent_basis( ct
->n
, ct
->t
[0], ct
->t
[1] );
1096 ct
->norm_impulse
= 0.0f
;
1097 ct
->tangent_impulse
[0] = 0.0f
;
1098 ct
->tangent_impulse
[1] = 0.0f
;
1100 rb_contact_count
++;
1103 static void rb_build_manifold_terrain_sphere( rigidbody
*rb
)
1107 int len
= bh_select( &world
.geo
.bhtris
, rb
->bbx_world
, geo
, 256 );
1109 for( int i
=0; i
<len
; i
++ )
1111 u32
*ptri
= &world
.geo
.indices
[ geo
[i
]*3 ];
1113 for( int j
=0; j
<3; j
++ )
1114 v3_copy( world
.geo
.verts
[ptri
[j
]].co
, tri
[j
] );
1116 vg_line(tri
[0],tri
[1],0xff00ff00 );
1117 vg_line(tri
[1],tri
[2],0xff00ff00 );
1118 vg_line(tri
[2],tri
[0],0xff00ff00 );
1123 for( int j
=0; j
<2; j
++ )
1125 if( sphere_vs_triangle( rb
->co
, rb
->inf
.sphere
.radius
, tri
,co
,norm
,&p
))
1127 struct contact
*ct
= rb_start_contact();
1133 v3_muladds( rb
->co
, norm
, p
, p1
);
1134 vg_line( rb
->co
, p1
, 0xffffffff );
1137 v3_copy( co
, ct
->co
);
1138 v3_copy( norm
, ct
->n
);
1139 rb_commit_contact( ct
, p
);
1146 static void rb_build_manifold_terrain( rigidbody
*rb
)
1150 float *p000
= pts
[0], *p001
= pts
[1], *p010
= pts
[2], *p011
= pts
[3],
1151 *p100
= pts
[4], *p101
= pts
[5], *p110
= pts
[6], *p111
= pts
[7];
1153 p000
[0]=box
[0][0];p000
[1]=box
[0][1];p000
[2]=box
[0][2];
1154 p001
[0]=box
[0][0];p001
[1]=box
[0][1];p001
[2]=box
[1][2];
1155 p010
[0]=box
[0][0];p010
[1]=box
[1][1];p010
[2]=box
[0][2];
1156 p011
[0]=box
[0][0];p011
[1]=box
[1][1];p011
[2]=box
[1][2];
1158 p100
[0]=box
[1][0];p100
[1]=box
[0][1];p100
[2]=box
[0][2];
1159 p101
[0]=box
[1][0];p101
[1]=box
[0][1];p101
[2]=box
[1][2];
1160 p110
[0]=box
[1][0];p110
[1]=box
[1][1];p110
[2]=box
[0][2];
1161 p111
[0]=box
[1][0];p111
[1]=box
[1][1];p111
[2]=box
[1][2];
1163 m4x3_mulv( rb
->to_world
, p000
, p000
);
1164 m4x3_mulv( rb
->to_world
, p001
, p001
);
1165 m4x3_mulv( rb
->to_world
, p010
, p010
);
1166 m4x3_mulv( rb
->to_world
, p011
, p011
);
1167 m4x3_mulv( rb
->to_world
, p100
, p100
);
1168 m4x3_mulv( rb
->to_world
, p101
, p101
);
1169 m4x3_mulv( rb
->to_world
, p110
, p110
);
1170 m4x3_mulv( rb
->to_world
, p111
, p111
);
1174 for( int i
=0; i
<8; i
++ )
1176 float *point
= pts
[i
];
1177 struct contact
*ct
= rb_start_contact();
1185 v3_copy( point
, surface
);
1189 hit
.dist
= INFINITY
;
1190 if( !ray_world( surface
, (v3f
){0.0f
,-1.0f
,0.0f
}, &hit
))
1193 v3_copy( hit
.pos
, surface
);
1195 float p
= vg_minf( surface
[1] - point
[1], 1.0f
);
1199 v3_copy( hit
.normal
, ct
->n
);
1200 v3_add( point
, surface
, ct
->co
);
1201 v3_muls( ct
->co
, 0.5f
, ct
->co
);
1203 rb_commit_contact( ct
, p
);
1213 * Initializing things like tangent vectors
1216 static void rb_presolve_contacts( rb_ct
*buffer
, int len
)
1218 for( int i
=0; i
<len
; i
++ )
1220 rb_ct
*ct
= &buffer
[i
];
1221 ct
->bias
= -0.2f
* k_rb_rate
* vg_minf(0.0f
,-ct
->p
+0.04f
);
1222 rb_tangent_basis( ct
->n
, ct
->t
[0], ct
->t
[1] );
1224 ct
->norm_impulse
= 0.0f
;
1225 ct
->tangent_impulse
[0] = 0.0f
;
1226 ct
->tangent_impulse
[1] = 0.0f
;
1227 ct
->mass_total
= 1.0f
/(ct
->rba
->inv_mass
+ ct
->rbb
->inv_mass
);
1229 rb_debug_contact( ct
);
1234 * Creates relative contact velocity vector, and offsets between each body
1236 static void rb_rcv( rb_ct
*ct
, v3f rv
, v3f da
, v3f db
)
1238 rigidbody
*rba
= ct
->rba
,
1241 v3_sub( ct
->co
, rba
->co
, da
);
1242 v3_sub( ct
->co
, rbb
->co
, db
);
1245 v3_cross( rba
->w
, da
, rva
);
1246 v3_add( rba
->v
, rva
, rva
);
1247 v3_cross( rbb
->w
, db
, rvb
);
1248 v3_add( rbb
->v
, rvb
, rvb
);
1250 v3_sub( rva
, rvb
, rv
);
1254 * Apply regular and angular velocity impulses to objects involved in contact
1256 static void rb_standard_impulse( rb_ct
*ct
, v3f da
, v3f db
, v3f impulse
)
1258 rigidbody
*rba
= ct
->rba
,
1262 v3_muls( impulse
, ct
->mass_total
*rba
->inv_mass
, ia
);
1263 v3_muls( impulse
, -ct
->mass_total
*rbb
->inv_mass
, ib
);
1266 v3_add( rba
->v
, ia
, rba
->v
);
1267 v3_add( rbb
->v
, ib
, rbb
->v
);
1269 /* Angular velocity */
1271 v3_cross( da
, ia
, wa
);
1272 v3_cross( db
, ib
, wb
);
1273 v3_add( rba
->w
, wa
, rba
->w
);
1274 v3_add( rbb
->w
, wb
, rbb
->w
);
1278 * One iteration to solve the contact constraint
1280 static void rb_solve_contacts( rb_ct
*buf
, int len
)
1282 float k_friction
= 0.1f
;
1284 /* Friction Impulse */
1285 for( int i
=0; i
<len
; i
++ )
1287 struct contact
*ct
= &buf
[i
];
1288 rigidbody
*rb
= ct
->rba
;
1291 rb_rcv( ct
, rv
, da
, db
);
1293 for( int j
=0; j
<2; j
++ )
1295 float f
= k_friction
* ct
->norm_impulse
,
1296 vt
= -v3_dot( rv
, ct
->t
[j
] );
1298 float temp
= ct
->tangent_impulse
[j
];
1299 ct
->tangent_impulse
[j
] = vg_clampf( temp
+vt
, -f
, f
);
1300 vt
= ct
->tangent_impulse
[j
] - temp
;
1303 v3_muls( ct
->t
[j
], vt
, impulse
);
1304 rb_standard_impulse( ct
, da
, db
, impulse
);
1308 /* Normal Impulse */
1309 for( int i
=0; i
<len
; i
++ )
1311 struct contact
*ct
= &buf
[i
];
1312 rigidbody
*rba
= ct
->rba
,
1316 rb_rcv( ct
, rv
, da
, db
);
1318 float vn
= -v3_dot( rv
, ct
->n
) + ct
->bias
;
1320 float temp
= ct
->norm_impulse
;
1321 ct
->norm_impulse
= vg_maxf( temp
+ vn
, 0.0f
);
1322 vn
= ct
->norm_impulse
- temp
;
1325 v3_muls( ct
->n
, vn
, impulse
);
1326 rb_standard_impulse( ct
, da
, db
, impulse
);
1331 * The following ventures into not really very sophisticated at all maths
1334 struct rb_angle_limit
1336 rigidbody
*rba
, *rbb
;
1338 float impulse
, bias
;
1341 static int rb_angle_limit_force( rigidbody
*rba
, v3f va
,
1342 rigidbody
*rbb
, v3f vb
,
1346 m3x3_mulv( rba
->to_world
, va
, wva
);
1347 m3x3_mulv( rbb
->to_world
, vb
, wvb
);
1349 float dt
= v3_dot(wva
,wvb
)*0.999f
,
1354 float correction
= max
-ang
;
1357 v3_cross( wva
, wvb
, axis
);
1360 q_axis_angle( rotation
, axis
, -correction
*0.25f
);
1361 q_mul( rotation
, rba
->q
, rba
->q
);
1363 q_axis_angle( rotation
, axis
, correction
*0.25f
);
1364 q_mul( rotation
, rbb
->q
, rbb
->q
);
1372 static void rb_constraint_angle_limit( struct rb_angle_limit
*limit
)
1377 static void rb_constraint_angle( rigidbody
*rba
, v3f va
,
1378 rigidbody
*rbb
, v3f vb
,
1379 float max
, float spring
)
1382 m3x3_mulv( rba
->to_world
, va
, wva
);
1383 m3x3_mulv( rbb
->to_world
, vb
, wvb
);
1385 float dt
= v3_dot(wva
,wvb
)*0.999f
,
1389 v3_cross( wva
, wvb
, axis
);
1390 v3_muladds( rba
->w
, axis
, ang
*spring
*0.5f
, rba
->w
);
1391 v3_muladds( rbb
->w
, axis
, -ang
*spring
*0.5f
, rbb
->w
);
1395 /* TODO: convert max into the dot product value so we dont have to always
1396 * evaluate acosf, only if its greater than the angle specified */
1400 float correction
= max
-ang
;
1403 q_axis_angle( rotation
, axis
, -correction
*0.125f
);
1404 q_mul( rotation
, rba
->q
, rba
->q
);
1406 q_axis_angle( rotation
, axis
, correction
*0.125f
);
1407 q_mul( rotation
, rbb
->q
, rbb
->q
);
1411 static void rb_relative_velocity( rigidbody
*ra
, v3f lca
,
1412 rigidbody
*rb
, v3f lcb
, v3f rcv
)
1415 m3x3_mulv( ra
->to_world
, lca
, wca
);
1416 m3x3_mulv( rb
->to_world
, lcb
, wcb
);
1418 v3_sub( ra
->v
, rb
->v
, rcv
);
1421 v3_cross( ra
->w
, wca
, rcv_Ra
);
1422 v3_cross( rb
->w
, wcb
, rcv_Rb
);
1423 v3_add( rcv_Ra
, rcv
, rcv
);
1424 v3_sub( rcv
, rcv_Rb
, rcv
);
1427 static void rb_constraint_position( rigidbody
*ra
, v3f lca
,
1428 rigidbody
*rb
, v3f lcb
)
1430 /* C = (COa + Ra*LCa) - (COb + Rb*LCb) = 0 */
1432 m3x3_mulv( ra
->to_world
, lca
, wca
);
1433 m3x3_mulv( rb
->to_world
, lcb
, wcb
);
1436 v3_add( wcb
, rb
->co
, delta
);
1437 v3_sub( delta
, wca
, delta
);
1438 v3_sub( delta
, ra
->co
, delta
);
1440 v3_muladds( ra
->co
, delta
, 0.5f
, ra
->co
);
1441 v3_muladds( rb
->co
, delta
, -0.5f
, rb
->co
);
1444 v3_sub( ra
->v
, rb
->v
, rcv
);
1447 v3_cross( ra
->w
, wca
, rcv_Ra
);
1448 v3_cross( rb
->w
, wcb
, rcv_Rb
);
1449 v3_add( rcv_Ra
, rcv
, rcv
);
1450 v3_sub( rcv
, rcv_Rb
, rcv
);
1452 float nm
= 0.5f
/(rb
->inv_mass
+ ra
->inv_mass
);
1454 float mass_a
= 1.0f
/ra
->inv_mass
,
1455 mass_b
= 1.0f
/rb
->inv_mass
,
1456 total_mass
= mass_a
+mass_b
;
1459 v3_muls( rcv
, 1.0f
, impulse
);
1460 v3_muladds( rb
->v
, impulse
, mass_b
/total_mass
, rb
->v
);
1461 v3_cross( wcb
, impulse
, impulse
);
1462 v3_add( impulse
, rb
->w
, rb
->w
);
1464 v3_muls( rcv
, -1.0f
, impulse
);
1465 v3_muladds( ra
->v
, impulse
, mass_a
/total_mass
, ra
->v
);
1466 v3_cross( wca
, impulse
, impulse
);
1467 v3_add( impulse
, ra
->w
, ra
->w
);
1471 * this could be used for spring joints
1472 * its not good for position constraint
1475 v3_muls( delta
, 0.5f
*spring
, impulse
);
1477 v3_add( impulse
, ra
->v
, ra
->v
);
1478 v3_cross( wca
, impulse
, impulse
);
1479 v3_add( impulse
, ra
->w
, ra
->w
);
1481 v3_muls( delta
, -0.5f
*spring
, impulse
);
1483 v3_add( impulse
, rb
->v
, rb
->v
);
1484 v3_cross( wcb
, impulse
, impulse
);
1485 v3_add( impulse
, rb
->w
, rb
->w
);
1489 static void debug_sphere( m4x3f m
, float radius
, u32 colour
)
1491 v3f ly
= { 0.0f
, 0.0f
, radius
},
1492 lx
= { 0.0f
, radius
, 0.0f
},
1493 lz
= { 0.0f
, 0.0f
, radius
};
1495 for( int i
=0; i
<16; i
++ )
1497 float t
= ((float)(i
+1) * (1.0f
/16.0f
)) * VG_PIf
* 2.0f
,
1501 v3f py
= { s
*radius
, 0.0f
, c
*radius
},
1502 px
= { s
*radius
, c
*radius
, 0.0f
},
1503 pz
= { 0.0f
, s
*radius
, c
*radius
};
1505 v3f p0
, p1
, p2
, p3
, p4
, p5
;
1506 m4x3_mulv( m
, py
, p0
);
1507 m4x3_mulv( m
, ly
, p1
);
1508 m4x3_mulv( m
, px
, p2
);
1509 m4x3_mulv( m
, lx
, p3
);
1510 m4x3_mulv( m
, pz
, p4
);
1511 m4x3_mulv( m
, lz
, p5
);
1513 vg_line( p0
, p1
, colour
== 0x00? 0xff00ff00: colour
);
1514 vg_line( p2
, p3
, colour
== 0x00? 0xff0000ff: colour
);
1515 vg_line( p4
, p5
, colour
== 0x00? 0xffff0000: colour
);
1523 static void debug_capsule( m4x3f m
, float radius
, float h
, u32 colour
)
1525 v3f ly
= { 0.0f
, 0.0f
, radius
},
1526 lx
= { 0.0f
, radius
, 0.0f
},
1527 lz
= { 0.0f
, 0.0f
, radius
};
1529 float s0
= sinf(0.0f
)*radius
,
1530 c0
= cosf(0.0f
)*radius
;
1532 v3f p0
, p1
, up
, right
, forward
;
1533 m3x3_mulv( m
, (v3f
){0.0f
,1.0f
,0.0f
}, up
);
1534 m3x3_mulv( m
, (v3f
){1.0f
,0.0f
,0.0f
}, right
);
1535 m3x3_mulv( m
, (v3f
){0.0f
,0.0f
,-1.0f
}, forward
);
1536 v3_muladds( m
[3], up
, -h
*0.5f
+radius
, p0
);
1537 v3_muladds( m
[3], up
, h
*0.5f
-radius
, p1
);
1540 v3_muladds( p0
, right
, radius
, a0
);
1541 v3_muladds( p1
, right
, radius
, a1
);
1542 v3_muladds( p0
, forward
, radius
, b0
);
1543 v3_muladds( p1
, forward
, radius
, b1
);
1544 vg_line( a0
, a1
, colour
);
1545 vg_line( b0
, b1
, colour
);
1547 v3_muladds( p0
, right
, -radius
, a0
);
1548 v3_muladds( p1
, right
, -radius
, a1
);
1549 v3_muladds( p0
, forward
, -radius
, b0
);
1550 v3_muladds( p1
, forward
, -radius
, b1
);
1551 vg_line( a0
, a1
, colour
);
1552 vg_line( b0
, b1
, colour
);
1554 for( int i
=0; i
<16; i
++ )
1556 float t
= ((float)(i
+1) * (1.0f
/16.0f
)) * VG_PIf
* 2.0f
,
1557 s1
= sinf(t
)*radius
,
1558 c1
= cosf(t
)*radius
;
1560 v3f e0
= { s0
, 0.0f
, c0
},
1561 e1
= { s1
, 0.0f
, c1
},
1562 e2
= { s0
, c0
, 0.0f
},
1563 e3
= { s1
, c1
, 0.0f
},
1564 e4
= { 0.0f
, c0
, s0
},
1565 e5
= { 0.0f
, c1
, s1
};
1567 m3x3_mulv( m
, e0
, e0
);
1568 m3x3_mulv( m
, e1
, e1
);
1569 m3x3_mulv( m
, e2
, e2
);
1570 m3x3_mulv( m
, e3
, e3
);
1571 m3x3_mulv( m
, e4
, e4
);
1572 m3x3_mulv( m
, e5
, e5
);
1574 v3_add( p0
, e0
, a0
);
1575 v3_add( p0
, e1
, a1
);
1576 v3_add( p1
, e0
, b0
);
1577 v3_add( p1
, e1
, b1
);
1579 vg_line( a0
, a1
, colour
);
1580 vg_line( b0
, b1
, colour
);
1584 v3_add( p0
, e2
, a0
);
1585 v3_add( p0
, e3
, a1
);
1586 v3_add( p0
, e4
, b0
);
1587 v3_add( p0
, e5
, b1
);
1591 v3_add( p1
, e2
, a0
);
1592 v3_add( p1
, e3
, a1
);
1593 v3_add( p1
, e4
, b0
);
1594 v3_add( p1
, e5
, b1
);
1597 vg_line( a0
, a1
, colour
);
1598 vg_line( b0
, b1
, colour
);
1605 static void rb_debug( rigidbody
*rb
, u32 colour
)
1607 if( rb
->type
== k_rb_shape_box
)
1610 vg_line_boxf_transformed( rb
->to_world
, rb
->bbx
, colour
);
1612 else if( rb
->type
== k_rb_shape_sphere
)
1614 debug_sphere( rb
->to_world
, rb
->inf
.sphere
.radius
, colour
);
1616 else if( rb
->type
== k_rb_shape_capsule
)
1619 float h
= rb
->inf
.capsule
.height
,
1620 r
= rb
->inf
.capsule
.radius
;
1622 debug_capsule( rb
->to_world
, r
, h
, colour
);
1624 else if( rb
->type
== k_rb_shape_scene
)
1626 vg_line_boxf( rb
->bbx
, colour
);
1632 * out penetration distance, normal
1634 static int rb_point_in_body( rigidbody
*rb
, v3f pos
, float *pen
, v3f normal
)
1637 m4x3_mulv( rb
->to_local
, pos
, local
);
1639 if( local
[0] > rb
->bbx
[0][0] && local
[0] < rb
->bbx
[1][0] &&
1640 local
[1] > rb
->bbx
[0][1] && local
[1] < rb
->bbx
[1][1] &&
1641 local
[2] > rb
->bbx
[0][2] && local
[2] < rb
->bbx
[1][2] )
1643 v3f area
, com
, comrel
;
1644 v3_add( rb
->bbx
[0], rb
->bbx
[1], com
);
1645 v3_muls( com
, 0.5f
, com
);
1647 v3_sub( rb
->bbx
[1], rb
->bbx
[0], area
);
1648 v3_sub( local
, com
, comrel
);
1649 v3_div( comrel
, area
, comrel
);
1652 float max_mag
= fabsf(comrel
[0]);
1654 if( fabsf(comrel
[1]) > max_mag
)
1657 max_mag
= fabsf(comrel
[1]);
1659 if( fabsf(comrel
[2]) > max_mag
)
1662 max_mag
= fabsf(comrel
[2]);
1666 normal
[axis
] = vg_signf(comrel
[axis
]);
1668 if( normal
[axis
] < 0.0f
)
1669 *pen
= local
[axis
] - rb
->bbx
[0][axis
];
1671 *pen
= rb
->bbx
[1][axis
] - local
[axis
];
1673 m3x3_mulv( rb
->to_world
, normal
, normal
);
1681 * BVH implementation, this is ONLY for static rigidbodies, its to slow for
1685 static void rb_bh_expand_bound( void *user
, boxf bound
, u32 item_index
)
1687 rigidbody
*rb
= &((rigidbody
*)user
)[ item_index
];
1688 box_concat( bound
, rb
->bbx_world
);
1691 static float rb_bh_centroid( void *user
, u32 item_index
, int axis
)
1693 rigidbody
*rb
= &((rigidbody
*)user
)[ item_index
];
1694 return (rb
->bbx_world
[axis
][0] + rb
->bbx_world
[1][axis
]) * 0.5f
;
1697 static void rb_bh_swap( void *user
, u32 ia
, u32 ib
)
1699 rigidbody temp
, *rba
, *rbb
;
1700 rba
= &((rigidbody
*)user
)[ ia
];
1701 rbb
= &((rigidbody
*)user
)[ ib
];
1708 static void rb_bh_debug( void *user
, u32 item_index
)
1710 rigidbody
*rb
= &((rigidbody
*)user
)[ item_index
];
1711 rb_debug( rb
, 0xff00ffff );
1714 static bh_system bh_system_rigidbodies
=
1716 .expand_bound
= rb_bh_expand_bound
,
1717 .item_centroid
= rb_bh_centroid
,
1718 .item_swap
= rb_bh_swap
,
1719 .item_debug
= rb_bh_debug
,
1725 #endif /* RIGIDBODY_H */