clean up rigidbody
[carveJwlIkooP6JGAAIwe30JlM.git] / rigidbody.h
1 /*
2 * Resources: Box2D - Erin Catto
3 * qu3e - Randy Gaul
4 */
5
6 #include "common.h"
7 #include "bvh.h"
8 #include "scene.h"
9
10 static void rb_tangent_basis( v3f n, v3f tx, v3f ty );
11 static bh_system bh_system_rigidbodies;
12
13 #ifndef RIGIDBODY_H
14 #define RIGIDBODY_H
15
16 /*
17 * -----------------------------------------------------------------------------
18 * (K)onstants
19 * -----------------------------------------------------------------------------
20 */
21
22 static const float
23 k_rb_rate = 60.0f,
24 k_rb_delta = (1.0f/k_rb_rate),
25 k_friction = 0.6f,
26 k_damp_linear = 0.05f, /* scale velocity 1/(1+x) */
27 k_damp_angular = 0.1f, /* scale angular 1/(1+x) */
28 k_limit_bias = 0.04f,
29 k_joint_bias = 0.08f, /* positional joints */
30 k_joint_correction = 0.01f,
31 k_penetration_slop = 0.01f;
32
33 /*
34 * -----------------------------------------------------------------------------
35 * structure definitions
36 * -----------------------------------------------------------------------------
37 */
38
39 typedef struct rigidbody rigidbody;
40 typedef struct contact rb_ct;
41
42 struct rigidbody
43 {
44 v3f co, v, w;
45 v4f q;
46
47 enum rb_shape
48 {
49 k_rb_shape_box = 0,
50 k_rb_shape_sphere = 1,
51 k_rb_shape_capsule = 2,
52 k_rb_shape_scene = 3
53 }
54 type;
55
56 union
57 {
58 struct rb_sphere
59 {
60 float radius;
61 }
62 sphere;
63
64 struct rb_capsule
65 {
66 float height, radius;
67 }
68 capsule;
69
70 struct rb_scene
71 {
72 scene *pscene;
73 }
74 scene;
75 }
76 inf;
77
78 v3f right, up, forward;
79
80 int is_world;
81
82 boxf bbx, bbx_world;
83 float inv_mass;
84
85 /* inertia model and inverse world tensor */
86 v3f I;
87 m3x3f iI, iIw;
88
89 m4x3f to_world, to_local;
90 };
91
92 static struct contact
93 {
94 rigidbody *rba, *rbb;
95 v3f co, n;
96 v3f t[2];
97 float p, bias, norm_impulse, tangent_impulse[2],
98 normal_mass, tangent_mass[2];
99
100 u32 element_id;
101 }
102 rb_contact_buffer[256];
103 static int rb_contact_count = 0;
104
105 /*
106 * -----------------------------------------------------------------------------
107 * Math Utils
108 * -----------------------------------------------------------------------------
109 */
110
111 static float sphere_volume( float radius )
112 {
113 float r3 = radius*radius*radius;
114 return (4.0f/3.0f) * VG_PIf * r3;
115 }
116
117 static void rb_tangent_basis( v3f n, v3f tx, v3f ty )
118 {
119 /* Compute tangent basis (box2d) */
120 if( fabsf( n[0] ) >= 0.57735027f )
121 {
122 tx[0] = n[1];
123 tx[1] = -n[0];
124 tx[2] = 0.0f;
125 }
126 else
127 {
128 tx[0] = 0.0f;
129 tx[1] = n[2];
130 tx[2] = -n[1];
131 }
132
133 v3_normalize( tx );
134 v3_cross( n, tx, ty );
135 }
136
137 /*
138 * -----------------------------------------------------------------------------
139 * Debugging
140 * -----------------------------------------------------------------------------
141 */
142
143 static void rb_debug_contact( rb_ct *ct )
144 {
145 v3f p1;
146 v3_muladds( ct->co, ct->n, 0.1f, p1 );
147 vg_line_pt3( ct->co, 0.025f, 0xff0000ff );
148 vg_line( ct->co, p1, 0xffffffff );
149 }
150
151 static void debug_sphere( m4x3f m, float radius, u32 colour )
152 {
153 v3f ly = { 0.0f, 0.0f, radius },
154 lx = { 0.0f, radius, 0.0f },
155 lz = { 0.0f, 0.0f, radius };
156
157 for( int i=0; i<16; i++ )
158 {
159 float t = ((float)(i+1) * (1.0f/16.0f)) * VG_PIf * 2.0f,
160 s = sinf(t),
161 c = cosf(t);
162
163 v3f py = { s*radius, 0.0f, c*radius },
164 px = { s*radius, c*radius, 0.0f },
165 pz = { 0.0f, s*radius, c*radius };
166
167 v3f p0, p1, p2, p3, p4, p5;
168 m4x3_mulv( m, py, p0 );
169 m4x3_mulv( m, ly, p1 );
170 m4x3_mulv( m, px, p2 );
171 m4x3_mulv( m, lx, p3 );
172 m4x3_mulv( m, pz, p4 );
173 m4x3_mulv( m, lz, p5 );
174
175 vg_line( p0, p1, colour == 0x00? 0xff00ff00: colour );
176 vg_line( p2, p3, colour == 0x00? 0xff0000ff: colour );
177 vg_line( p4, p5, colour == 0x00? 0xffff0000: colour );
178
179 v3_copy( py, ly );
180 v3_copy( px, lx );
181 v3_copy( pz, lz );
182 }
183 }
184
185 static void debug_capsule( m4x3f m, float radius, float h, u32 colour )
186 {
187 v3f ly = { 0.0f, 0.0f, radius },
188 lx = { 0.0f, radius, 0.0f },
189 lz = { 0.0f, 0.0f, radius };
190
191 float s0 = sinf(0.0f)*radius,
192 c0 = cosf(0.0f)*radius;
193
194 v3f p0, p1, up, right, forward;
195 m3x3_mulv( m, (v3f){0.0f,1.0f,0.0f}, up );
196 m3x3_mulv( m, (v3f){1.0f,0.0f,0.0f}, right );
197 m3x3_mulv( m, (v3f){0.0f,0.0f,-1.0f}, forward );
198 v3_muladds( m[3], up, -h*0.5f+radius, p0 );
199 v3_muladds( m[3], up, h*0.5f-radius, p1 );
200
201 v3f a0, a1, b0, b1;
202 v3_muladds( p0, right, radius, a0 );
203 v3_muladds( p1, right, radius, a1 );
204 v3_muladds( p0, forward, radius, b0 );
205 v3_muladds( p1, forward, radius, b1 );
206 vg_line( a0, a1, colour );
207 vg_line( b0, b1, colour );
208
209 v3_muladds( p0, right, -radius, a0 );
210 v3_muladds( p1, right, -radius, a1 );
211 v3_muladds( p0, forward, -radius, b0 );
212 v3_muladds( p1, forward, -radius, b1 );
213 vg_line( a0, a1, colour );
214 vg_line( b0, b1, colour );
215
216 for( int i=0; i<16; i++ )
217 {
218 float t = ((float)(i+1) * (1.0f/16.0f)) * VG_PIf * 2.0f,
219 s1 = sinf(t)*radius,
220 c1 = cosf(t)*radius;
221
222 v3f e0 = { s0, 0.0f, c0 },
223 e1 = { s1, 0.0f, c1 },
224 e2 = { s0, c0, 0.0f },
225 e3 = { s1, c1, 0.0f },
226 e4 = { 0.0f, c0, s0 },
227 e5 = { 0.0f, c1, s1 };
228
229 m3x3_mulv( m, e0, e0 );
230 m3x3_mulv( m, e1, e1 );
231 m3x3_mulv( m, e2, e2 );
232 m3x3_mulv( m, e3, e3 );
233 m3x3_mulv( m, e4, e4 );
234 m3x3_mulv( m, e5, e5 );
235
236 v3_add( p0, e0, a0 );
237 v3_add( p0, e1, a1 );
238 v3_add( p1, e0, b0 );
239 v3_add( p1, e1, b1 );
240
241 vg_line( a0, a1, colour );
242 vg_line( b0, b1, colour );
243
244 if( c0 < 0.0f )
245 {
246 v3_add( p0, e2, a0 );
247 v3_add( p0, e3, a1 );
248 v3_add( p0, e4, b0 );
249 v3_add( p0, e5, b1 );
250 }
251 else
252 {
253 v3_add( p1, e2, a0 );
254 v3_add( p1, e3, a1 );
255 v3_add( p1, e4, b0 );
256 v3_add( p1, e5, b1 );
257 }
258
259 vg_line( a0, a1, colour );
260 vg_line( b0, b1, colour );
261
262 s0 = s1;
263 c0 = c1;
264 }
265 }
266
267 static void rb_debug( rigidbody *rb, u32 colour )
268 {
269 if( rb->type == k_rb_shape_box )
270 {
271 v3f *box = rb->bbx;
272 vg_line_boxf_transformed( rb->to_world, rb->bbx, colour );
273 }
274 else if( rb->type == k_rb_shape_sphere )
275 {
276 debug_sphere( rb->to_world, rb->inf.sphere.radius, colour );
277 }
278 else if( rb->type == k_rb_shape_capsule )
279 {
280 m4x3f m0, m1;
281 float h = rb->inf.capsule.height,
282 r = rb->inf.capsule.radius;
283
284 debug_capsule( rb->to_world, r, h, colour );
285 }
286 else if( rb->type == k_rb_shape_scene )
287 {
288 vg_line_boxf( rb->bbx, colour );
289 }
290 }
291
292 /*
293 * -----------------------------------------------------------------------------
294 * Integration
295 * -----------------------------------------------------------------------------
296 */
297
298 /*
299 * Update world space bounding box based on local one
300 */
301 static void rb_update_bounds( rigidbody *rb )
302 {
303 box_copy( rb->bbx, rb->bbx_world );
304 m4x3_transform_aabb( rb->to_world, rb->bbx_world );
305 }
306
307 /*
308 * Commit transform to rigidbody. Updates matrices
309 */
310 static void rb_update_transform( rigidbody *rb )
311 {
312 q_normalize( rb->q );
313 q_m3x3( rb->q, rb->to_world );
314 v3_copy( rb->co, rb->to_world[3] );
315
316 m4x3_invert_affine( rb->to_world, rb->to_local );
317
318 m3x3_mulv( rb->to_world, (v3f){1.0f,0.0f, 0.0f}, rb->right );
319 m3x3_mulv( rb->to_world, (v3f){0.0f,1.0f, 0.0f}, rb->up );
320 m3x3_mulv( rb->to_world, (v3f){0.0f,0.0f,-1.0f}, rb->forward );
321
322 m3x3_mul( rb->iI, rb->to_local, rb->iIw );
323 m3x3_mul( rb->to_world, rb->iIw, rb->iIw );
324
325 rb_update_bounds( rb );
326 }
327
328 /*
329 * Initialize rigidbody and calculate masses, inertia
330 */
331 static void rb_init( rigidbody *rb )
332 {
333 float volume = 1.0f;
334
335 if( rb->type == k_rb_shape_box )
336 {
337 v3f dims;
338 v3_sub( rb->bbx[1], rb->bbx[0], dims );
339 volume = dims[0]*dims[1]*dims[2];
340
341 if( !rb->is_world )
342 vg_info( "Box volume: %f\n", volume );
343 }
344 else if( rb->type == k_rb_shape_sphere )
345 {
346 volume = sphere_volume( rb->inf.sphere.radius );
347 v3_fill( rb->bbx[0], -rb->inf.sphere.radius );
348 v3_fill( rb->bbx[1], rb->inf.sphere.radius );
349
350 vg_info( "Sphere volume: %f\n", volume );
351 }
352 else if( rb->type == k_rb_shape_capsule )
353 {
354 float r = rb->inf.capsule.radius,
355 h = rb->inf.capsule.height;
356 volume = sphere_volume( r ) + VG_PIf * r*r * (h - r*2.0f);
357
358 v3_fill( rb->bbx[0], -rb->inf.sphere.radius );
359 v3_fill( rb->bbx[1], rb->inf.sphere.radius );
360 rb->bbx[0][1] = -h;
361 rb->bbx[1][1] = h;
362 }
363 else if( rb->type == k_rb_shape_scene )
364 {
365 rb->is_world = 1;
366 box_copy( rb->inf.scene.pscene->bbx, rb->bbx );
367 }
368
369 if( rb->is_world )
370 {
371 rb->inv_mass = 0.0f;
372 v3_zero( rb->I );
373 m3x3_zero(rb->iI);
374 }
375 else
376 {
377 float mass = 2.0f*volume;
378 rb->inv_mass = 1.0f/mass;
379
380 v3f extent;
381 v3_sub( rb->bbx[1], rb->bbx[0], extent );
382 v3_muls( extent, 0.5f, extent );
383
384 /* local intertia tensor */
385 float scale = 4.0f;
386 float ex2 = scale*extent[0]*extent[0],
387 ey2 = scale*extent[1]*extent[1],
388 ez2 = scale*extent[2]*extent[2];
389
390 rb->I[0] = ((1.0f/12.0f) * mass * (ey2+ez2));
391 rb->I[1] = ((1.0f/12.0f) * mass * (ex2+ez2));
392 rb->I[2] = ((1.0f/12.0f) * mass * (ex2+ey2));
393
394 m3x3_identity( rb->iI );
395 rb->iI[0][0] = rb->I[0];
396 rb->iI[1][1] = rb->I[1];
397 rb->iI[2][2] = rb->I[2];
398 m3x3_inv( rb->iI, rb->iI );
399 }
400
401 v3_zero( rb->v );
402 v3_zero( rb->w );
403
404 rb_update_transform( rb );
405 }
406
407 static void rb_iter( rigidbody *rb )
408 {
409 v3f gravity = { 0.0f, -9.8f, 0.0f };
410 v3_muladds( rb->v, gravity, k_rb_delta, rb->v );
411
412 /* intergrate velocity */
413 v3_muladds( rb->co, rb->v, k_rb_delta, rb->co );
414 v3_lerp( rb->w, (v3f){0.0f,0.0f,0.0f}, 0.0025f, rb->w );
415
416 /* inegrate inertia */
417 if( v3_length2( rb->w ) > 0.0f )
418 {
419 v4f rotation;
420 v3f axis;
421 v3_copy( rb->w, axis );
422
423 float mag = v3_length( axis );
424 v3_divs( axis, mag, axis );
425 q_axis_angle( rotation, axis, mag*k_rb_delta );
426 q_mul( rotation, rb->q, rb->q );
427 }
428
429 /* damping */
430 v3_muls( rb->v, 1.0f/(1.0f+k_rb_delta*k_damp_linear), rb->v );
431 v3_muls( rb->w, 1.0f/(1.0f+k_rb_delta*k_damp_angular), rb->w );
432 }
433
434 /*
435 * -----------------------------------------------------------------------------
436 * Closest point functions
437 * -----------------------------------------------------------------------------
438 */
439
440 /*
441 * These closest point tests were learned from Real-Time Collision Detection by
442 * Christer Ericson
443 */
444 static float closest_segment_segment( v3f p1, v3f q1, v3f p2, v3f q2,
445 float *s, float *t, v3f c1, v3f c2)
446 {
447 v3f d1,d2,r;
448 v3_sub( q1, p1, d1 );
449 v3_sub( q2, p2, d2 );
450 v3_sub( p1, p2, r );
451
452 float a = v3_length2( d1 ),
453 e = v3_length2( d2 ),
454 f = v3_dot( d2, r );
455
456 const float kEpsilon = 0.0001f;
457
458 if( a <= kEpsilon && e <= kEpsilon )
459 {
460 *s = 0.0f;
461 *t = 0.0f;
462 v3_copy( p1, c1 );
463 v3_copy( p2, c2 );
464
465 v3f v0;
466 v3_sub( c1, c2, v0 );
467
468 return v3_length2( v0 );
469 }
470
471 if( a<= kEpsilon )
472 {
473 *s = 0.0f;
474 *t = vg_clampf( f / e, 0.0f, 1.0f );
475 }
476 else
477 {
478 float c = v3_dot( d1, r );
479 if( e <= kEpsilon )
480 {
481 *t = 0.0f;
482 *s = vg_clampf( -c / a, 0.0f, 1.0f );
483 }
484 else
485 {
486 float b = v3_dot(d1,d2),
487 d = a*e-b*b;
488
489 if( d != 0.0f )
490 {
491 *s = vg_clampf((b*f - c*e)/d, 0.0f, 1.0f);
492 }
493 else
494 {
495 *s = 0.0f;
496 }
497
498 *t = (b*(*s)+f) / e;
499
500 if( *t < 0.0f )
501 {
502 *t = 0.0f;
503 *s = vg_clampf( -c / a, 0.0f, 1.0f );
504 }
505 else if( *t > 1.0f )
506 {
507 *t = 1.0f;
508 *s = vg_clampf((b-c)/a,0.0f,1.0f);
509 }
510 }
511 }
512
513 v3_muladds( p1, d1, *s, c1 );
514 v3_muladds( p2, d2, *t, c2 );
515
516 v3f v0;
517 v3_sub( c1, c2, v0 );
518 return v3_length2( v0 );
519 }
520
521 static void closest_point_aabb( v3f p, boxf box, v3f dest )
522 {
523 v3_maxv( p, box[0], dest );
524 v3_minv( dest, box[1], dest );
525 }
526
527 static void closest_point_obb( v3f p, rigidbody *rb, v3f dest )
528 {
529 v3f local;
530 m4x3_mulv( rb->to_local, p, local );
531 closest_point_aabb( local, rb->bbx, local );
532 m4x3_mulv( rb->to_world, local, dest );
533 }
534
535 static float closest_point_segment( v3f a, v3f b, v3f point, v3f dest )
536 {
537 v3f v0, v1;
538 v3_sub( b, a, v0 );
539 v3_sub( point, a, v1 );
540
541 float t = v3_dot( v1, v0 ) / v3_length2(v0);
542 t = vg_clampf(t,0.0f,1.0f);
543 v3_muladds( a, v0, t, dest );
544 return t;
545 }
546
547 static void closest_on_triangle( v3f p, v3f tri[3], v3f dest )
548 {
549 v3f ab, ac, ap;
550 float d1, d2;
551
552 /* Region outside A */
553 v3_sub( tri[1], tri[0], ab );
554 v3_sub( tri[2], tri[0], ac );
555 v3_sub( p, tri[0], ap );
556
557 d1 = v3_dot(ab,ap);
558 d2 = v3_dot(ac,ap);
559 if( d1 <= 0.0f && d2 <= 0.0f )
560 {
561 v3_copy( tri[0], dest );
562 v3_copy( (v3f){INFINITY,INFINITY,INFINITY}, dest );
563 return;
564 }
565
566 /* Region outside B */
567 v3f bp;
568 float d3, d4;
569
570 v3_sub( p, tri[1], bp );
571 d3 = v3_dot( ab, bp );
572 d4 = v3_dot( ac, bp );
573
574 if( d3 >= 0.0f && d4 <= d3 )
575 {
576 v3_copy( tri[1], dest );
577 v3_copy( (v3f){INFINITY,INFINITY,INFINITY}, dest );
578 return;
579 }
580
581 /* Edge region of AB */
582 float vc = d1*d4 - d3*d2;
583 if( vc <= 0.0f && d1 >= 0.0f && d3 <= 0.0f )
584 {
585 float v = d1 / (d1-d3);
586 v3_muladds( tri[0], ab, v, dest );
587 v3_copy( (v3f){INFINITY,INFINITY,INFINITY}, dest );
588 return;
589 }
590
591 /* Region outside C */
592 v3f cp;
593 float d5, d6;
594 v3_sub( p, tri[2], cp );
595 d5 = v3_dot(ab, cp);
596 d6 = v3_dot(ac, cp);
597
598 if( d6 >= 0.0f && d5 <= d6 )
599 {
600 v3_copy( tri[2], dest );
601 v3_copy( (v3f){INFINITY,INFINITY,INFINITY}, dest );
602 return;
603 }
604
605 /* Region of AC */
606 float vb = d5*d2 - d1*d6;
607 if( vb <= 0.0f && d2 >= 0.0f && d6 <= 0.0f )
608 {
609 float w = d2 / (d2-d6);
610 v3_muladds( tri[0], ac, w, dest );
611 v3_copy( (v3f){INFINITY,INFINITY,INFINITY}, dest );
612 return;
613 }
614
615 /* Region of BC */
616 float va = d3*d6 - d5*d4;
617 if( va <= 0.0f && (d4-d3) >= 0.0f && (d5-d6) >= 0.0f )
618 {
619 float w = (d4-d3) / ((d4-d3) + (d5-d6));
620 v3f bc;
621 v3_sub( tri[2], tri[1], bc );
622 v3_muladds( tri[1], bc, w, dest );
623 v3_copy( (v3f){INFINITY,INFINITY,INFINITY}, dest );
624 return;
625 }
626
627 /* P inside region, Q via barycentric coordinates uvw */
628 float d = 1.0f/(va+vb+vc),
629 v = vb*d,
630 w = vc*d;
631
632 v3_muladds( tri[0], ab, v, dest );
633 v3_muladds( dest, ac, w, dest );
634 }
635
636 /* TODO */
637 static void closest_on_triangle_1( v3f p, v3f tri[3], v3f dest )
638 {
639 v3f ab, ac, ap;
640 float d1, d2;
641
642 /* Region outside A */
643 v3_sub( tri[1], tri[0], ab );
644 v3_sub( tri[2], tri[0], ac );
645 v3_sub( p, tri[0], ap );
646
647 d1 = v3_dot(ab,ap);
648 d2 = v3_dot(ac,ap);
649 if( d1 <= 0.0f && d2 <= 0.0f )
650 {
651 v3_copy( tri[0], dest );
652 return;
653 }
654
655 /* Region outside B */
656 v3f bp;
657 float d3, d4;
658
659 v3_sub( p, tri[1], bp );
660 d3 = v3_dot( ab, bp );
661 d4 = v3_dot( ac, bp );
662
663 if( d3 >= 0.0f && d4 <= d3 )
664 {
665 v3_copy( tri[1], dest );
666 return;
667 }
668
669 /* Edge region of AB */
670 float vc = d1*d4 - d3*d2;
671 if( vc <= 0.0f && d1 >= 0.0f && d3 <= 0.0f )
672 {
673 float v = d1 / (d1-d3);
674 v3_muladds( tri[0], ab, v, dest );
675 return;
676 }
677
678 /* Region outside C */
679 v3f cp;
680 float d5, d6;
681 v3_sub( p, tri[2], cp );
682 d5 = v3_dot(ab, cp);
683 d6 = v3_dot(ac, cp);
684
685 if( d6 >= 0.0f && d5 <= d6 )
686 {
687 v3_copy( tri[2], dest );
688 return;
689 }
690
691 /* Region of AC */
692 float vb = d5*d2 - d1*d6;
693 if( vb <= 0.0f && d2 >= 0.0f && d6 <= 0.0f )
694 {
695 float w = d2 / (d2-d6);
696 v3_muladds( tri[0], ac, w, dest );
697 return;
698 }
699
700 /* Region of BC */
701 float va = d3*d6 - d5*d4;
702 if( va <= 0.0f && (d4-d3) >= 0.0f && (d5-d6) >= 0.0f )
703 {
704 float w = (d4-d3) / ((d4-d3) + (d5-d6));
705 v3f bc;
706 v3_sub( tri[2], tri[1], bc );
707 v3_muladds( tri[1], bc, w, dest );
708 return;
709 }
710
711 /* P inside region, Q via barycentric coordinates uvw */
712 float d = 1.0f/(va+vb+vc),
713 v = vb*d,
714 w = vc*d;
715
716 v3_muladds( tri[0], ab, v, dest );
717 v3_muladds( dest, ac, w, dest );
718 }
719
720 /*
721 * -----------------------------------------------------------------------------
722 * Boolean shape overlap functions
723 * -----------------------------------------------------------------------------
724 */
725
726 /*
727 * Project AABB, and triangle interval onto axis to check if they overlap
728 */
729 static int rb_box_triangle_interval( v3f extent, v3f axis, v3f tri[3] )
730 {
731 float
732
733 r = extent[0] * fabsf(axis[0]) +
734 extent[1] * fabsf(axis[1]) +
735 extent[2] * fabsf(axis[2]),
736
737 p0 = v3_dot( axis, tri[0] ),
738 p1 = v3_dot( axis, tri[1] ),
739 p2 = v3_dot( axis, tri[2] ),
740
741 e = vg_maxf(-vg_maxf(p0,vg_maxf(p1,p2)), vg_minf(p0,vg_minf(p1,p2)));
742
743 if( e > r ) return 0;
744 else return 1;
745 }
746
747 /*
748 * Seperating axis test box vs triangle
749 */
750 static int rb_box_triangle_sat( rigidbody *rba, v3f tri_src[3] )
751 {
752 v3f tri[3];
753
754 v3f extent, c;
755 v3_sub( rba->bbx[1], rba->bbx[0], extent );
756 v3_muls( extent, 0.5f, extent );
757 v3_add( rba->bbx[0], extent, c );
758
759 for( int i=0; i<3; i++ )
760 {
761 m4x3_mulv( rba->to_local, tri_src[i], tri[i] );
762 v3_sub( tri[i], c, tri[i] );
763 }
764
765 /* u0, u1, u2 */
766 if(!rb_box_triangle_interval( extent, (v3f){1.0f,0.0f,0.0f}, tri )) return 0;
767 if(!rb_box_triangle_interval( extent, (v3f){0.0f,1.0f,0.0f}, tri )) return 0;
768 if(!rb_box_triangle_interval( extent, (v3f){0.0f,0.0f,1.0f}, tri )) return 0;
769
770 v3f v0,v1,v2,n, e0,e1,e2;
771 v3_sub( tri[1], tri[0], v0 );
772 v3_sub( tri[2], tri[0], v1 );
773 v3_sub( tri[2], tri[1], v2 );
774 v3_normalize( v0 );
775 v3_normalize( v1 );
776 v3_normalize( v2 );
777 v3_cross( v0, v1, n );
778 v3_cross( v0, n, e0 );
779 v3_cross( n, v1, e1 );
780 v3_cross( v2, n, e2 );
781
782 /* normal */
783 if(!rb_box_triangle_interval( extent, n, tri )) return 0;
784
785 v3f axis[9];
786 v3_cross( e0, (v3f){1.0f,0.0f,0.0f}, axis[0] );
787 v3_cross( e0, (v3f){0.0f,1.0f,0.0f}, axis[1] );
788 v3_cross( e0, (v3f){0.0f,0.0f,1.0f}, axis[2] );
789 v3_cross( e1, (v3f){1.0f,0.0f,0.0f}, axis[3] );
790 v3_cross( e1, (v3f){0.0f,1.0f,0.0f}, axis[4] );
791 v3_cross( e1, (v3f){0.0f,0.0f,1.0f}, axis[5] );
792 v3_cross( e2, (v3f){1.0f,0.0f,0.0f}, axis[6] );
793 v3_cross( e2, (v3f){0.0f,1.0f,0.0f}, axis[7] );
794 v3_cross( e2, (v3f){0.0f,0.0f,1.0f}, axis[8] );
795
796 for( int i=0; i<9; i++ )
797 if(!rb_box_triangle_interval( extent, axis[i], tri )) return 0;
798
799 return 1;
800 }
801
802 /*
803 * -----------------------------------------------------------------------------
804 * Collision matrix
805 * -----------------------------------------------------------------------------
806 */
807
808 /*
809 * Contact generators
810 *
811 * These do not automatically allocate contacts, an appropriately sized
812 * buffer must be supplied. The function returns the size of the manifold
813 * which was generated.
814 *
815 * The values set on the contacts are: n, co, p, rba, rbb
816 */
817
818 /*
819 * By collecting the minimum(time) and maximum(time) pairs of points, we
820 * build a reduced and stable exact manifold.
821 *
822 * tx: time at point
823 * rx: minimum distance of these points
824 * dx: the delta between the two points
825 *
826 * pairs will only ammend these if they are creating a collision
827 */
828 typedef struct capsule_manifold capsule_manifold;
829 struct capsule_manifold
830 {
831 float t0, t1;
832 float r0, r1;
833 v3f d0, d1;
834 };
835
836 /*
837 * Expand a line manifold with a new pair. t value is the time along segment
838 * on the oriented object which created this pair.
839 */
840 static void rb_capsule_manifold( v3f pa, v3f pb, float t, float r,
841 capsule_manifold *manifold )
842 {
843 v3f delta;
844 v3_sub( pa, pb, delta );
845
846 if( v3_length2(delta) < r*r )
847 {
848 if( t < manifold->t0 )
849 {
850 v3_copy( delta, manifold->d0 );
851 manifold->t0 = t;
852 manifold->r0 = r;
853 }
854
855 if( t > manifold->t1 )
856 {
857 v3_copy( delta, manifold->d1 );
858 manifold->t1 = t;
859 manifold->r1 = r;
860 }
861 }
862 }
863
864 static void rb_capsule_manifold_init( capsule_manifold *manifold )
865 {
866 manifold->t0 = INFINITY;
867 manifold->t1 = -INFINITY;
868 }
869
870 static int rb_capsule_manifold_done( rigidbody *rba, rigidbody *rbb,
871 capsule_manifold *manifold, rb_ct *buf )
872 {
873 float h = rba->inf.capsule.height,
874 ra = rba->inf.capsule.radius;
875
876 v3f p0, p1;
877 v3_muladds( rba->co, rba->up, -h*0.5f+ra, p0 );
878 v3_muladds( rba->co, rba->up, h*0.5f-ra, p1 );
879
880 int count = 0;
881 if( manifold->t0 <= 1.0f )
882 {
883 rb_ct *ct = buf;
884
885 v3f pa;
886 v3_muls( p0, 1.0f-manifold->t0, pa );
887 v3_muladds( pa, p1, manifold->t0, pa );
888
889 float d = v3_length( manifold->d0 );
890 v3_muls( manifold->d0, 1.0f/d, ct->n );
891 v3_muladds( pa, ct->n, -ra, ct->co );
892
893 ct->p = manifold->r0 - d;
894 ct->rba = rba;
895 ct->rbb = rbb;
896
897 count ++;
898 }
899
900 if( (manifold->t1 >= 0.0f) && (manifold->t0 != manifold->t1) )
901 {
902 rb_ct *ct = buf+count;
903
904 v3f pa;
905 v3_muls( p0, 1.0f-manifold->t1, pa );
906 v3_muladds( pa, p1, manifold->t1, pa );
907
908 float d = v3_length( manifold->d1 );
909 v3_muls( manifold->d1, 1.0f/d, ct->n );
910 v3_muladds( pa, ct->n, -ra, ct->co );
911
912 ct->p = manifold->r1 - d;
913 ct->rba = rba;
914 ct->rbb = rbb;
915
916 count ++;
917 }
918
919 /*
920 * Debugging
921 */
922
923 if( count == 2 )
924 vg_line( buf[0].co, buf[1].co, 0xff0000ff );
925
926 return count;
927 }
928
929 static int rb_capsule_sphere( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
930 {
931 float h = rba->inf.capsule.height,
932 ra = rba->inf.capsule.radius,
933 rb = rbb->inf.sphere.radius;
934
935 v3f p0, p1;
936 v3_muladds( rba->co, rba->up, -h*0.5f+ra, p0 );
937 v3_muladds( rba->co, rba->up, h*0.5f-ra, p1 );
938
939 v3f c, delta;
940 closest_point_segment( p0, p1, rbb->co, c );
941 v3_sub( c, rbb->co, delta );
942
943 float d2 = v3_length2(delta),
944 r = ra + rb;
945
946 if( d2 < r*r )
947 {
948 float d = sqrtf(d2);
949
950 rb_ct *ct = buf;
951 v3_muls( delta, 1.0f/d, ct->n );
952 ct->p = r-d;
953
954 v3f p0, p1;
955 v3_muladds( c, ct->n, -ra, p0 );
956 v3_muladds( rbb->co, ct->n, rb, p1 );
957 v3_add( p0, p1, ct->co );
958 v3_muls( ct->co, 0.5f, ct->co );
959
960 ct->rba = rba;
961 ct->rbb = rbb;
962
963 return 1;
964 }
965
966 return 0;
967 }
968
969 static int rb_capsule_capsule( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
970 {
971 float ha = rba->inf.capsule.height,
972 hb = rbb->inf.capsule.height,
973 ra = rba->inf.capsule.radius,
974 rb = rbb->inf.capsule.radius,
975 r = ra+rb;
976
977 v3f p0, p1, p2, p3;
978 v3_muladds( rba->co, rba->up, -ha*0.5f+ra, p0 );
979 v3_muladds( rba->co, rba->up, ha*0.5f-ra, p1 );
980 v3_muladds( rbb->co, rbb->up, -hb*0.5f+rb, p2 );
981 v3_muladds( rbb->co, rbb->up, hb*0.5f-rb, p3 );
982
983 capsule_manifold manifold;
984 rb_capsule_manifold_init( &manifold );
985
986 v3f pa, pb;
987 float ta, tb;
988 closest_segment_segment( p0, p1, p2, p3, &ta, &tb, pa, pb );
989 rb_capsule_manifold( pa, pb, ta, r, &manifold );
990
991 ta = closest_point_segment( p0, p1, p2, pa );
992 tb = closest_point_segment( p0, p1, p3, pb );
993 rb_capsule_manifold( pa, p2, ta, r, &manifold );
994 rb_capsule_manifold( pb, p3, tb, r, &manifold );
995
996 closest_point_segment( p2, p3, p0, pa );
997 closest_point_segment( p2, p3, p1, pb );
998 rb_capsule_manifold( p0, pa, 0.0f, r, &manifold );
999 rb_capsule_manifold( p1, pb, 1.0f, r, &manifold );
1000
1001 return rb_capsule_manifold_done( rba, rbb, &manifold, buf );
1002 }
1003
1004 /*
1005 * Generates up to two contacts; optimised for the most stable manifold
1006 */
1007 static int rb_capsule_box( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
1008 {
1009 float h = rba->inf.capsule.height,
1010 r = rba->inf.capsule.radius;
1011
1012 /*
1013 * Solving this in symetric local space of the cube saves us some time and a
1014 * couple branches when it comes to the quad stage.
1015 */
1016 v3f centroid;
1017 v3_add( rbb->bbx[0], rbb->bbx[1], centroid );
1018 v3_muls( centroid, 0.5f, centroid );
1019
1020 boxf bbx;
1021 v3_sub( rbb->bbx[0], centroid, bbx[0] );
1022 v3_sub( rbb->bbx[1], centroid, bbx[1] );
1023
1024 v3f pc, p0w, p1w, p0, p1;
1025 v3_muladds( rba->co, rba->up, -h*0.5f+r, p0w );
1026 v3_muladds( rba->co, rba->up, h*0.5f-r, p1w );
1027
1028 m4x3_mulv( rbb->to_local, p0w, p0 );
1029 m4x3_mulv( rbb->to_local, p1w, p1 );
1030 v3_sub( p0, centroid, p0 );
1031 v3_sub( p1, centroid, p1 );
1032 v3_add( p0, p1, pc );
1033 v3_muls( pc, 0.5f, pc );
1034
1035 /*
1036 * Finding an appropriate quad to collide lines with
1037 */
1038 v3f region;
1039 v3_div( pc, bbx[1], region );
1040
1041 v3f quad[4];
1042 if( (fabsf(region[0]) > fabsf(region[1])) &&
1043 (fabsf(region[0]) > fabsf(region[2])) )
1044 {
1045 float px = vg_signf(region[0]) * bbx[1][0];
1046 v3_copy( (v3f){ px, bbx[0][1], bbx[0][2] }, quad[0] );
1047 v3_copy( (v3f){ px, bbx[1][1], bbx[0][2] }, quad[1] );
1048 v3_copy( (v3f){ px, bbx[1][1], bbx[1][2] }, quad[2] );
1049 v3_copy( (v3f){ px, bbx[0][1], bbx[1][2] }, quad[3] );
1050 }
1051 else if( fabsf(region[1]) > fabsf(region[2]) )
1052 {
1053 float py = vg_signf(region[1]) * bbx[1][1];
1054 v3_copy( (v3f){ bbx[0][0], py, bbx[0][2] }, quad[0] );
1055 v3_copy( (v3f){ bbx[1][0], py, bbx[0][2] }, quad[1] );
1056 v3_copy( (v3f){ bbx[1][0], py, bbx[1][2] }, quad[2] );
1057 v3_copy( (v3f){ bbx[0][0], py, bbx[1][2] }, quad[3] );
1058 }
1059 else
1060 {
1061 float pz = vg_signf(region[2]) * bbx[1][2];
1062 v3_copy( (v3f){ bbx[0][0], bbx[0][1], pz }, quad[0] );
1063 v3_copy( (v3f){ bbx[1][0], bbx[0][1], pz }, quad[1] );
1064 v3_copy( (v3f){ bbx[1][0], bbx[1][1], pz }, quad[2] );
1065 v3_copy( (v3f){ bbx[0][0], bbx[1][1], pz }, quad[3] );
1066 }
1067
1068 capsule_manifold manifold;
1069 rb_capsule_manifold_init( &manifold );
1070
1071 v3f c0, c1;
1072 closest_point_aabb( p0, bbx, c0 );
1073 closest_point_aabb( p1, bbx, c1 );
1074
1075 v3f d0, d1, da;
1076 v3_sub( c0, p0, d0 );
1077 v3_sub( c1, p1, d1 );
1078 v3_sub( p1, p0, da );
1079
1080 /* TODO: ? */
1081 v3_normalize(d0);
1082 v3_normalize(d1);
1083 v3_normalize(da);
1084
1085 if( v3_dot( da, d0 ) <= 0.01f )
1086 rb_capsule_manifold( p0, c0, 0.0f, r, &manifold );
1087
1088 if( v3_dot( da, d1 ) >= -0.01f )
1089 rb_capsule_manifold( p1, c1, 1.0f, r, &manifold );
1090
1091 for( int i=0; i<4; i++ )
1092 {
1093 int i0 = i,
1094 i1 = (i+1)%4;
1095
1096 v3f ca, cb;
1097 float ta, tb;
1098 closest_segment_segment( p0, p1, quad[i0], quad[i1], &ta, &tb, ca, cb );
1099 rb_capsule_manifold( ca, cb, ta, r, &manifold );
1100 }
1101
1102 /*
1103 * Create final contacts based on line manifold
1104 */
1105 m3x3_mulv( rbb->to_world, manifold.d0, manifold.d0 );
1106 m3x3_mulv( rbb->to_world, manifold.d1, manifold.d1 );
1107
1108 /*
1109 * Debugging
1110 */
1111
1112 #if 0
1113 for( int i=0; i<4; i++ )
1114 {
1115 v3f q0, q1;
1116 int i0 = i,
1117 i1 = (i+1)%4;
1118
1119 v3_add( quad[i0], centroid, q0 );
1120 v3_add( quad[i1], centroid, q1 );
1121
1122 m4x3_mulv( rbb->to_world, q0, q0 );
1123 m4x3_mulv( rbb->to_world, q1, q1 );
1124
1125 vg_line( q0, q1, 0xffffffff );
1126 }
1127 #endif
1128
1129 return rb_capsule_manifold_done( rba, rbb, &manifold, buf );
1130 }
1131
1132 static int rb_sphere_box( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
1133 {
1134 v3f co, delta;
1135
1136 closest_point_obb( rba->co, rbb, co );
1137 v3_sub( rba->co, co, delta );
1138
1139 float d2 = v3_length2(delta),
1140 r = rba->inf.sphere.radius;
1141
1142 if( d2 <= r*r )
1143 {
1144 float d;
1145
1146 rb_ct *ct = buf;
1147 if( d2 <= 0.0001f )
1148 {
1149 v3_sub( rba->co, rbb->co, delta );
1150
1151 /*
1152 * some extra testing is required to find the best axis to push the
1153 * object back outside the box. Since there isnt a clear seperating
1154 * vector already, especially on really high aspect boxes.
1155 */
1156 float lx = v3_dot( rbb->right, delta ),
1157 ly = v3_dot( rbb->up, delta ),
1158 lz = v3_dot( rbb->forward, delta ),
1159 px = rbb->bbx[1][0] - fabsf(lx),
1160 py = rbb->bbx[1][1] - fabsf(ly),
1161 pz = rbb->bbx[1][2] - fabsf(lz);
1162
1163 if( px < py && px < pz )
1164 v3_muls( rbb->right, vg_signf(lx), ct->n );
1165 else if( py < pz )
1166 v3_muls( rbb->up, vg_signf(ly), ct->n );
1167 else
1168 v3_muls( rbb->forward, vg_signf(lz), ct->n );
1169
1170 v3_muladds( rba->co, ct->n, -r, ct->co );
1171 ct->p = r;
1172 }
1173 else
1174 {
1175 d = sqrtf(d2);
1176 v3_muls( delta, 1.0f/d, ct->n );
1177 ct->p = r-d;
1178 v3_copy( co, ct->co );
1179 }
1180
1181 ct->rba = rba;
1182 ct->rbb = rbb;
1183 return 1;
1184 }
1185
1186 return 0;
1187 }
1188
1189 static int rb_sphere_sphere( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
1190 {
1191 v3f delta;
1192 v3_sub( rba->co, rbb->co, delta );
1193
1194 float d2 = v3_length2(delta),
1195 r = rba->inf.sphere.radius + rbb->inf.sphere.radius;
1196
1197 if( d2 < r*r )
1198 {
1199 float d = sqrtf(d2);
1200
1201 rb_ct *ct = buf;
1202 v3_muls( delta, 1.0f/d, ct->n );
1203
1204 v3f p0, p1;
1205 v3_muladds( rba->co, ct->n,-rba->inf.sphere.radius, p0 );
1206 v3_muladds( rbb->co, ct->n, rbb->inf.sphere.radius, p1 );
1207 v3_add( p0, p1, ct->co );
1208 v3_muls( ct->co, 0.5f, ct->co );
1209 ct->p = r-d;
1210 ct->rba = rba;
1211 ct->rbb = rbb;
1212 return 1;
1213 }
1214
1215 return 0;
1216 }
1217
1218 static int rb_sphere_triangle( rigidbody *rba, rigidbody *rbb,
1219 v3f tri[3], rb_ct *buf )
1220 {
1221 v3f delta, co;
1222
1223 closest_on_triangle( rba->co, tri, co );
1224 v3_sub( rba->co, co, delta );
1225
1226 vg_line( rba->co, co, 0xffff0000 );
1227 vg_line_pt3( rba->co, 0.1f, 0xff00ffff );
1228
1229 float d2 = v3_length2( delta ),
1230 r = rba->inf.sphere.radius;
1231
1232 if( d2 < r*r )
1233 {
1234 rb_ct *ct = buf;
1235
1236 v3f ab, ac, tn;
1237 v3_sub( tri[2], tri[0], ab );
1238 v3_sub( tri[1], tri[0], ac );
1239 v3_cross( ac, ab, tn );
1240 v3_copy( tn, ct->n );
1241 v3_normalize( ct->n );
1242
1243 float d = sqrtf(d2);
1244
1245 v3_copy( co, ct->co );
1246 ct->p = r-d;
1247 ct->rba = rba;
1248 ct->rbb = rbb;
1249 return 1;
1250 }
1251
1252 return 0;
1253 }
1254
1255 static int rb_sphere_scene( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
1256 {
1257 scene *sc = rbb->inf.scene.pscene;
1258
1259 u32 geo[128];
1260 v3f tri[3];
1261 int len = bh_select( &sc->bhtris, rba->bbx_world, geo, 128 );
1262
1263 int count = 0;
1264
1265 for( int i=0; i<len; i++ )
1266 {
1267 u32 *ptri = &sc->indices[ geo[i]*3 ];
1268
1269 for( int j=0; j<3; j++ )
1270 v3_copy( sc->verts[ptri[j]].co, tri[j] );
1271
1272 vg_line(tri[0],tri[1],0xff00ff00 );
1273 vg_line(tri[1],tri[2],0xff00ff00 );
1274 vg_line(tri[2],tri[0],0xff00ff00 );
1275
1276 buf[count].element_id = ptri[0];
1277 count += rb_sphere_triangle( rba, rbb, tri, buf+count );
1278
1279 if( count == 12 )
1280 {
1281 vg_warn( "Exceeding sphere_vs_scene capacity. Geometry too dense!\n" );
1282 return count;
1283 }
1284 }
1285
1286 return count;
1287 }
1288
1289 static int rb_box_scene( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
1290 {
1291 scene *sc = rbb->inf.scene.pscene;
1292
1293 u32 geo[128];
1294 v3f tri[3];
1295 int len = bh_select( &sc->bhtris, rba->bbx_world, geo, 128 );
1296
1297 int count = 0;
1298
1299 for( int i=0; i<len; i++ )
1300 {
1301 u32 *ptri = &sc->indices[ geo[i]*3 ];
1302
1303 for( int j=0; j<3; j++ )
1304 v3_copy( sc->verts[ptri[j]].co, tri[j] );
1305
1306 if( rb_box_triangle_sat( rba, tri ) )
1307 {
1308 vg_line(tri[0],tri[1],0xff50ff00 );
1309 vg_line(tri[1],tri[2],0xff50ff00 );
1310 vg_line(tri[2],tri[0],0xff50ff00 );
1311 }
1312 else
1313 {
1314 vg_line(tri[0],tri[1],0xff0000ff );
1315 vg_line(tri[1],tri[2],0xff0000ff );
1316 vg_line(tri[2],tri[0],0xff0000ff );
1317
1318 continue;
1319 }
1320
1321 v3f v0,v1,n;
1322 v3_sub( tri[1], tri[0], v0 );
1323 v3_sub( tri[2], tri[0], v1 );
1324 v3_cross( v0, v1, n );
1325 v3_normalize( n );
1326
1327 /* find best feature */
1328 float best = v3_dot( rba->right, n );
1329 int axis = 0;
1330
1331 float cy = v3_dot( rba->up, n );
1332 if( fabsf(cy) > fabsf(best) )
1333 {
1334 best = cy;
1335 axis = 1;
1336 }
1337
1338 /* TODO: THIS IS WRONG DIRECTION */
1339 float cz = -v3_dot( rba->forward, n );
1340 if( fabsf(cz) > fabsf(best) )
1341 {
1342 best = cz;
1343 axis = 2;
1344 }
1345
1346 v3f manifold[4];
1347
1348 if( axis == 0 )
1349 {
1350 float px = best > 0.0f? rba->bbx[0][0]: rba->bbx[1][0];
1351 manifold[0][0] = px;
1352 manifold[0][1] = rba->bbx[0][1];
1353 manifold[0][2] = rba->bbx[0][2];
1354 manifold[1][0] = px;
1355 manifold[1][1] = rba->bbx[1][1];
1356 manifold[1][2] = rba->bbx[0][2];
1357 manifold[2][0] = px;
1358 manifold[2][1] = rba->bbx[1][1];
1359 manifold[2][2] = rba->bbx[1][2];
1360 manifold[3][0] = px;
1361 manifold[3][1] = rba->bbx[0][1];
1362 manifold[3][2] = rba->bbx[1][2];
1363 }
1364 else if( axis == 1 )
1365 {
1366 float py = best > 0.0f? rba->bbx[0][1]: rba->bbx[1][1];
1367 manifold[0][0] = rba->bbx[0][0];
1368 manifold[0][1] = py;
1369 manifold[0][2] = rba->bbx[0][2];
1370 manifold[1][0] = rba->bbx[1][0];
1371 manifold[1][1] = py;
1372 manifold[1][2] = rba->bbx[0][2];
1373 manifold[2][0] = rba->bbx[1][0];
1374 manifold[2][1] = py;
1375 manifold[2][2] = rba->bbx[1][2];
1376 manifold[3][0] = rba->bbx[0][0];
1377 manifold[3][1] = py;
1378 manifold[3][2] = rba->bbx[1][2];
1379 }
1380 else
1381 {
1382 float pz = best > 0.0f? rba->bbx[0][2]: rba->bbx[1][2];
1383 manifold[0][0] = rba->bbx[0][0];
1384 manifold[0][1] = rba->bbx[0][1];
1385 manifold[0][2] = pz;
1386 manifold[1][0] = rba->bbx[1][0];
1387 manifold[1][1] = rba->bbx[0][1];
1388 manifold[1][2] = pz;
1389 manifold[2][0] = rba->bbx[1][0];
1390 manifold[2][1] = rba->bbx[1][1];
1391 manifold[2][2] = pz;
1392 manifold[3][0] = rba->bbx[0][0];
1393 manifold[3][1] = rba->bbx[1][1];
1394 manifold[3][2] = pz;
1395 }
1396
1397 for( int j=0; j<4; j++ )
1398 m4x3_mulv( rba->to_world, manifold[j], manifold[j] );
1399
1400 vg_line( manifold[0], manifold[1], 0xffffffff );
1401 vg_line( manifold[1], manifold[2], 0xffffffff );
1402 vg_line( manifold[2], manifold[3], 0xffffffff );
1403 vg_line( manifold[3], manifold[0], 0xffffffff );
1404
1405 for( int j=0; j<4; j++ )
1406 {
1407 rb_ct *ct = buf+count;
1408
1409 v3_copy( manifold[j], ct->co );
1410 v3_copy( n, ct->n );
1411
1412 float l0 = v3_dot( tri[0], n ),
1413 l1 = v3_dot( manifold[j], n );
1414
1415 ct->p = (l0-l1)*0.5f;
1416 if( ct->p < 0.0f )
1417 continue;
1418
1419 ct->rba = rba;
1420 ct->rbb = rbb;
1421 count ++;
1422
1423 if( count >= 12 )
1424 return count;
1425 }
1426 }
1427 return count;
1428 }
1429
1430 static int RB_MATRIX_ERROR( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
1431 {
1432 vg_error( "Collision type is unimplemented between types %d and %d\n",
1433 rba->type, rbb->type );
1434
1435 return 0;
1436 }
1437
1438 static int rb_sphere_capsule( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
1439 {
1440 return rb_capsule_sphere( rbb, rba, buf );
1441 }
1442
1443 static int rb_box_capsule( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
1444 {
1445 return rb_capsule_box( rbb, rba, buf );
1446 }
1447
1448 static int rb_box_sphere( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
1449 {
1450 return rb_sphere_box( rbb, rba, buf );
1451 }
1452
1453 static int rb_scene_box( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
1454 {
1455 return rb_box_scene( rbb, rba, buf );
1456 }
1457
1458 static int (*rb_jump_table[4][4])( rigidbody *a, rigidbody *b, rb_ct *buf ) =
1459 {
1460 /* box */ /* Sphere */ /* Capsule */ /* Mesh */
1461 { RB_MATRIX_ERROR, rb_box_sphere, rb_box_capsule, rb_box_scene },
1462 { rb_sphere_box, rb_sphere_sphere, rb_sphere_capsule, rb_sphere_scene },
1463 { rb_capsule_box, rb_capsule_sphere, rb_capsule_capsule, RB_MATRIX_ERROR },
1464 { rb_scene_box, RB_MATRIX_ERROR, RB_MATRIX_ERROR, RB_MATRIX_ERROR }
1465 };
1466
1467 static int rb_collide( rigidbody *rba, rigidbody *rbb )
1468 {
1469 int (*collider_jump)(rigidbody *rba, rigidbody *rbb, rb_ct *buf )
1470 = rb_jump_table[rba->type][rbb->type];
1471
1472 /*
1473 * 12 is the maximum manifold size we can generate, so we are forced to abort
1474 * potentially checking any more.
1475 */
1476 if( rb_contact_count + 12 > vg_list_size(rb_contact_buffer) )
1477 {
1478 vg_warn( "Too many contacts made in global collider buffer (%d of %d\n)",
1479 rb_contact_count, vg_list_size(rb_contact_buffer) );
1480 return 0;
1481 }
1482
1483 /*
1484 * TODO: Replace this with a more dedicated broad phase pass
1485 */
1486 if( box_overlap( rba->bbx_world, rbb->bbx_world ) )
1487 {
1488 int count = collider_jump( rba, rbb, rb_contact_buffer+rb_contact_count);
1489 rb_contact_count += count;
1490 return count;
1491 }
1492 else
1493 return 0;
1494 }
1495
1496 /*
1497 * -----------------------------------------------------------------------------
1498 * Dynamics
1499 * -----------------------------------------------------------------------------
1500 */
1501
1502 static void rb_solver_reset(void)
1503 {
1504 rb_contact_count = 0;
1505 }
1506
1507 static rb_ct *rb_global_ct(void)
1508 {
1509 return rb_contact_buffer + rb_contact_count;
1510 }
1511
1512 /*
1513 * Initializing things like tangent vectors
1514 */
1515 static void rb_presolve_contacts( rb_ct *buffer, int len )
1516 {
1517 for( int i=0; i<len; i++ )
1518 {
1519 rb_ct *ct = &buffer[i];
1520 ct->bias = -0.2f * k_rb_rate * vg_minf( 0.0f, -ct->p+k_penetration_slop );
1521 rb_tangent_basis( ct->n, ct->t[0], ct->t[1] );
1522
1523 ct->norm_impulse = 0.0f;
1524 ct->tangent_impulse[0] = 0.0f;
1525 ct->tangent_impulse[1] = 0.0f;
1526
1527 v3f ra, rb, raCn, rbCn, raCt, rbCt;
1528 v3_sub( ct->co, ct->rba->co, ra );
1529 v3_sub( ct->co, ct->rbb->co, rb );
1530 v3_cross( ra, ct->n, raCn );
1531 v3_cross( rb, ct->n, rbCn );
1532
1533 /* orient inverse inertia tensors */
1534 v3f raCnI, rbCnI;
1535 m3x3_mulv( ct->rba->iIw, raCn, raCnI );
1536 m3x3_mulv( ct->rbb->iIw, rbCn, rbCnI );
1537
1538 ct->normal_mass = ct->rba->inv_mass + ct->rbb->inv_mass;
1539 ct->normal_mass += v3_dot( raCn, raCnI );
1540 ct->normal_mass += v3_dot( rbCn, rbCnI );
1541 ct->normal_mass = 1.0f/ct->normal_mass;
1542
1543 for( int j=0; j<2; j++ )
1544 {
1545 v3f raCtI, rbCtI;
1546 v3_cross( ct->t[j], ra, raCt );
1547 v3_cross( ct->t[j], rb, rbCt );
1548 m3x3_mulv( ct->rba->iIw, raCt, raCtI );
1549 m3x3_mulv( ct->rbb->iIw, rbCt, rbCtI );
1550
1551 ct->tangent_mass[j] = ct->rba->inv_mass + ct->rbb->inv_mass;
1552 ct->tangent_mass[j] += v3_dot( raCt, raCtI );
1553 ct->tangent_mass[j] += v3_dot( rbCt, rbCtI );
1554 ct->tangent_mass[j] = 1.0f/ct->tangent_mass[j];
1555 }
1556
1557 rb_debug_contact( ct );
1558 }
1559 }
1560
1561 /*
1562 * Creates relative contact velocity vector, and offsets between each body
1563 */
1564 static void rb_rcv( rb_ct *ct, v3f rv, v3f da, v3f db )
1565 {
1566 rigidbody *rba = ct->rba,
1567 *rbb = ct->rbb;
1568
1569 v3_sub( ct->co, rba->co, da );
1570 v3_sub( ct->co, rbb->co, db );
1571
1572 v3f rva, rvb;
1573 v3_cross( rba->w, da, rva );
1574 v3_add( rba->v, rva, rva );
1575 v3_cross( rbb->w, db, rvb );
1576 v3_add( rbb->v, rvb, rvb );
1577
1578 v3_sub( rva, rvb, rv );
1579 }
1580
1581 /*
1582 * Apply impulse to object
1583 */
1584 static void rb_linear_impulse( rigidbody *rb, v3f delta, v3f impulse )
1585 {
1586 /* linear */
1587 v3_muladds( rb->v, impulse, rb->inv_mass, rb->v );
1588
1589 /* Angular velocity */
1590 v3f wa;
1591 v3_cross( delta, impulse, wa );
1592
1593 m3x3_mulv( rb->iIw, wa, wa );
1594 v3_add( rb->w, wa, rb->w );
1595 }
1596
1597 /*
1598 * One iteration to solve the contact constraint
1599 */
1600 static void rb_solve_contacts( rb_ct *buf, int len )
1601 {
1602 for( int i=0; i<len; i++ )
1603 {
1604 struct contact *ct = &buf[i];
1605 rigidbody *rb = ct->rba;
1606
1607 v3f rv, da, db;
1608 rb_rcv( ct, rv, da, db );
1609
1610 /* Friction */
1611 for( int j=0; j<2; j++ )
1612 {
1613 float f = k_friction * ct->norm_impulse,
1614 vt = v3_dot( rv, ct->t[j] ),
1615 lambda = ct->tangent_mass[j] * -vt;
1616
1617 float temp = ct->tangent_impulse[j];
1618 ct->tangent_impulse[j] = vg_clampf( temp + lambda, -f, f );
1619 lambda = ct->tangent_impulse[j] - temp;
1620
1621 v3f impulse;
1622 v3_muls( ct->t[j], lambda, impulse );
1623 rb_linear_impulse( ct->rba, da, impulse );
1624
1625 v3_muls( ct->t[j], -lambda, impulse );
1626 rb_linear_impulse( ct->rbb, db, impulse );
1627 }
1628
1629 /* Normal */
1630 rb_rcv( ct, rv, da, db );
1631 float vn = v3_dot( rv, ct->n ),
1632 lambda = ct->normal_mass * (-vn + ct->bias);
1633
1634 float temp = ct->norm_impulse;
1635 ct->norm_impulse = vg_maxf( temp + lambda, 0.0f );
1636 lambda = ct->norm_impulse - temp;
1637
1638 v3f impulse;
1639 v3_muls( ct->n, lambda, impulse );
1640 rb_linear_impulse( ct->rba, da, impulse );
1641
1642 v3_muls( ct->n, -lambda, impulse );
1643 rb_linear_impulse( ct->rbb, db, impulse );
1644 }
1645 }
1646
1647 /*
1648 * -----------------------------------------------------------------------------
1649 * Constraints
1650 * -----------------------------------------------------------------------------
1651 */
1652
1653 static void draw_angle_limit( v3f c, v3f major, v3f minor,
1654 float amin, float amax, float measured,
1655 u32 colour )
1656 {
1657 float f = 0.05f;
1658 v3f ay, ax;
1659 v3_muls( major, f, ay );
1660 v3_muls( minor, f, ax );
1661
1662 for( int x=0; x<16; x++ )
1663 {
1664 float t0 = (float)x / 16.0f,
1665 t1 = (float)(x+1) / 16.0f,
1666 a0 = vg_lerpf( amin, amax, t0 ),
1667 a1 = vg_lerpf( amin, amax, t1 );
1668
1669 v3f p0, p1;
1670 v3_muladds( c, ay, cosf(a0), p0 );
1671 v3_muladds( p0, ax, sinf(a0), p0 );
1672 v3_muladds( c, ay, cosf(a1), p1 );
1673 v3_muladds( p1, ax, sinf(a1), p1 );
1674
1675 vg_line( p0, p1, colour );
1676
1677 if( x == 0 )
1678 vg_line( c, p0, colour );
1679 if( x == 15 )
1680 vg_line( c, p1, colour );
1681 }
1682
1683 v3f p2;
1684 v3_muladds( c, ay, cosf(measured)*1.2f, p2 );
1685 v3_muladds( p2, ax, sinf(measured)*1.2f, p2 );
1686 vg_line( c, p2, colour );
1687 }
1688
1689 static void rb_debug_constraint_limits( rigidbody *ra, rigidbody *rb, v3f lca,
1690 v3f limits[2] )
1691 {
1692 v3f ax, ay, az, bx, by, bz;
1693 m3x3_mulv( ra->to_world, (v3f){1.0f,0.0f,0.0f}, ax );
1694 m3x3_mulv( ra->to_world, (v3f){0.0f,1.0f,0.0f}, ay );
1695 m3x3_mulv( ra->to_world, (v3f){0.0f,0.0f,1.0f}, az );
1696 m3x3_mulv( rb->to_world, (v3f){1.0f,0.0f,0.0f}, bx );
1697 m3x3_mulv( rb->to_world, (v3f){0.0f,1.0f,0.0f}, by );
1698 m3x3_mulv( rb->to_world, (v3f){0.0f,0.0f,1.0f}, bz );
1699
1700 v2f px, py, pz;
1701 px[0] = v3_dot( ay, by );
1702 px[1] = v3_dot( az, by );
1703
1704 py[0] = v3_dot( az, bz );
1705 py[1] = v3_dot( ax, bz );
1706
1707 pz[0] = v3_dot( ax, bx );
1708 pz[1] = v3_dot( ay, bx );
1709
1710 float r0 = atan2f( px[1], px[0] ),
1711 r1 = atan2f( py[1], py[0] ),
1712 r2 = atan2f( pz[1], pz[0] );
1713
1714 v3f c;
1715 m4x3_mulv( ra->to_world, lca, c );
1716 draw_angle_limit( c, ay, az, limits[0][0], limits[1][0], r0, 0xff0000ff );
1717 draw_angle_limit( c, az, ax, limits[0][1], limits[1][1], r1, 0xff00ff00 );
1718 draw_angle_limit( c, ax, ay, limits[0][2], limits[1][2], r2, 0xffff0000 );
1719 }
1720
1721 static void rb_limit_cure( rigidbody *ra, rigidbody *rb, v3f axis, float d )
1722 {
1723 if( d != 0.0f )
1724 {
1725 float avx = v3_dot( ra->w, axis ) - v3_dot( rb->w, axis );
1726 float joint_mass = rb->inv_mass + ra->inv_mass;
1727 joint_mass = 1.0f/joint_mass;
1728
1729 float bias = (k_limit_bias * k_rb_rate) * d,
1730 lambda = -(avx + bias) * joint_mass;
1731
1732 /* Angular velocity */
1733 v3f wa, wb;
1734 v3_muls( axis, lambda * ra->inv_mass, wa );
1735 v3_muls( axis, -lambda * rb->inv_mass, wb );
1736
1737 v3_add( ra->w, wa, ra->w );
1738 v3_add( rb->w, wb, rb->w );
1739 }
1740 }
1741
1742 static void rb_constraint_limits( rigidbody *ra, v3f lca,
1743 rigidbody *rb, v3f lcb, v3f limits[2] )
1744 {
1745 /* TODO: Code dupe remover */
1746 v3f ax, ay, az, bx, by, bz;
1747 m3x3_mulv( ra->to_world, (v3f){1.0f,0.0f,0.0f}, ax );
1748 m3x3_mulv( ra->to_world, (v3f){0.0f,1.0f,0.0f}, ay );
1749 m3x3_mulv( ra->to_world, (v3f){0.0f,0.0f,1.0f}, az );
1750 m3x3_mulv( rb->to_world, (v3f){1.0f,0.0f,0.0f}, bx );
1751 m3x3_mulv( rb->to_world, (v3f){0.0f,1.0f,0.0f}, by );
1752 m3x3_mulv( rb->to_world, (v3f){0.0f,0.0f,1.0f}, bz );
1753
1754 v2f px, py, pz;
1755 px[0] = v3_dot( ay, by );
1756 px[1] = v3_dot( az, by );
1757
1758 py[0] = v3_dot( az, bz );
1759 py[1] = v3_dot( ax, bz );
1760
1761 pz[0] = v3_dot( ax, bx );
1762 pz[1] = v3_dot( ay, bx );
1763
1764 float r0 = atan2f( px[1], px[0] ),
1765 r1 = atan2f( py[1], py[0] ),
1766 r2 = atan2f( pz[1], pz[0] );
1767
1768 /* calculate angle deltas */
1769 float dx = 0.0f, dy = 0.0f, dz = 0.0f;
1770
1771 if( r0 < limits[0][0] ) dx = limits[0][0] - r0;
1772 if( r0 > limits[1][0] ) dx = limits[1][0] - r0;
1773 if( r1 < limits[0][1] ) dy = limits[0][1] - r1;
1774 if( r1 > limits[1][1] ) dy = limits[1][1] - r1;
1775 if( r2 < limits[0][2] ) dz = limits[0][2] - r2;
1776 if( r2 > limits[1][2] ) dz = limits[1][2] - r2;
1777
1778 v3f wca, wcb;
1779 m3x3_mulv( ra->to_world, lca, wca );
1780 m3x3_mulv( rb->to_world, lcb, wcb );
1781
1782 rb_limit_cure( ra, rb, ax, dx );
1783 rb_limit_cure( ra, rb, ay, dy );
1784 rb_limit_cure( ra, rb, az, dz );
1785 }
1786
1787 static void rb_debug_constraint_position( rigidbody *ra, v3f lca,
1788 rigidbody *rb, v3f lcb )
1789 {
1790 v3f wca, wcb;
1791 m3x3_mulv( ra->to_world, lca, wca );
1792 m3x3_mulv( rb->to_world, lcb, wcb );
1793
1794 v3f p0, p1;
1795 v3_add( wca, ra->co, p0 );
1796 v3_add( wcb, rb->co, p1 );
1797 vg_line_pt3( p0, 0.005f, 0xffffff00 );
1798 vg_line_pt3( p1, 0.005f, 0xffffff00 );
1799 vg_line( p0, p1, 0xffffff00 );
1800 }
1801
1802 static void rb_constraint_position( rigidbody *ra, v3f lca,
1803 rigidbody *rb, v3f lcb )
1804 {
1805 /* C = (COa + Ra*LCa) - (COb + Rb*LCb) = 0 */
1806 v3f wca, wcb;
1807 m3x3_mulv( ra->to_world, lca, wca );
1808 m3x3_mulv( rb->to_world, lcb, wcb );
1809
1810 v3f rcv;
1811 v3_sub( ra->v, rb->v, rcv );
1812
1813 v3f rcv_Ra, rcv_Rb;
1814 v3_cross( ra->w, wca, rcv_Ra );
1815 v3_cross( rb->w, wcb, rcv_Rb );
1816 v3_add( rcv_Ra, rcv, rcv );
1817 v3_sub( rcv, rcv_Rb, rcv );
1818
1819 v3f delta;
1820 v3f p0, p1;
1821 v3_add( wca, ra->co, p0 );
1822 v3_add( wcb, rb->co, p1 );
1823 v3_sub( p1, p0, delta );
1824
1825 float dist2 = v3_length2( delta );
1826
1827 if( dist2 > 0.00001f )
1828 {
1829 float dist = sqrtf(dist2);
1830 v3_muls( delta, 1.0f/dist, delta );
1831
1832 float joint_mass = rb->inv_mass + ra->inv_mass;
1833
1834 v3f raCn, rbCn, raCt, rbCt;
1835 v3_cross( wca, delta, raCn );
1836 v3_cross( wcb, delta, rbCn );
1837
1838 /* orient inverse inertia tensors */
1839 v3f raCnI, rbCnI;
1840 m3x3_mulv( ra->iIw, raCn, raCnI );
1841 m3x3_mulv( rb->iIw, rbCn, rbCnI );
1842 joint_mass += v3_dot( raCn, raCnI );
1843 joint_mass += v3_dot( rbCn, rbCnI );
1844 joint_mass = 1.0f/joint_mass;
1845
1846 float vd = v3_dot( rcv, delta ),
1847 bias = -(k_joint_bias * k_rb_rate) * dist,
1848 lambda = -(vd + bias) * joint_mass;
1849
1850 v3f impulse;
1851 v3_muls( delta, lambda, impulse );
1852 rb_linear_impulse( ra, wca, impulse );
1853 v3_muls( delta, -lambda, impulse );
1854 rb_linear_impulse( rb, wcb, impulse );
1855
1856 /* 'fake' snap */
1857 v3_muladds( ra->co, delta, dist * k_joint_correction, ra->co );
1858 v3_muladds( rb->co, delta, -dist * k_joint_correction, rb->co );
1859 }
1860 }
1861
1862 /*
1863 * -----------------------------------------------------------------------------
1864 * BVH implementation, this is ONLY for static rigidbodies, its to slow for
1865 * realtime use.
1866 * -----------------------------------------------------------------------------
1867 */
1868
1869 static void rb_bh_expand_bound( void *user, boxf bound, u32 item_index )
1870 {
1871 rigidbody *rb = &((rigidbody *)user)[ item_index ];
1872 box_concat( bound, rb->bbx_world );
1873 }
1874
1875 static float rb_bh_centroid( void *user, u32 item_index, int axis )
1876 {
1877 rigidbody *rb = &((rigidbody *)user)[ item_index ];
1878 return (rb->bbx_world[axis][0] + rb->bbx_world[1][axis]) * 0.5f;
1879 }
1880
1881 static void rb_bh_swap( void *user, u32 ia, u32 ib )
1882 {
1883 rigidbody temp, *rba, *rbb;
1884 rba = &((rigidbody *)user)[ ia ];
1885 rbb = &((rigidbody *)user)[ ib ];
1886
1887 temp = *rba;
1888 *rba = *rbb;
1889 *rbb = temp;
1890 }
1891
1892 static void rb_bh_debug( void *user, u32 item_index )
1893 {
1894 rigidbody *rb = &((rigidbody *)user)[ item_index ];
1895 rb_debug( rb, 0xff00ffff );
1896 }
1897
1898 static bh_system bh_system_rigidbodies =
1899 {
1900 .expand_bound = rb_bh_expand_bound,
1901 .item_centroid = rb_bh_centroid,
1902 .item_swap = rb_bh_swap,
1903 .item_debug = rb_bh_debug,
1904 .cast_ray = NULL
1905 };
1906
1907 #endif /* RIGIDBODY_H */