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