to the workers of the world
[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 #include <math.h>
15
16 VG_STATIC void rb_tangent_basis( v3f n, v3f tx, v3f ty );
17 VG_STATIC bh_system bh_system_rigidbodies;
18
19 #ifndef RIGIDBODY_H
20 #define RIGIDBODY_H
21
22 /*
23 * -----------------------------------------------------------------------------
24 * (K)onstants
25 * -----------------------------------------------------------------------------
26 */
27
28 VG_STATIC const float
29 k_rb_rate = (1.0/VG_TIMESTEP_FIXED),
30 k_rb_delta = (1.0/k_rb_rate),
31 k_friction = 0.4f,
32 k_damp_linear = 0.1f, /* scale velocity 1/(1+x) */
33 k_damp_angular = 0.1f, /* scale angular 1/(1+x) */
34 k_penetration_slop = 0.01f,
35 k_inertia_scale = 8.0f,
36 k_phys_baumgarte = 0.2f,
37 k_gravity = 9.6f;
38
39 VG_STATIC float
40 k_limit_bias = 0.02f,
41 k_joint_correction = 0.01f,
42 k_joint_impulse = 1.0f,
43 k_joint_bias = 0.08f; /* positional joints */
44
45 VG_STATIC void rb_register_cvar(void)
46 {
47 vg_var_push( (struct vg_var){
48 .name = "k_limit_bias", .data = &k_limit_bias,
49 .data_type = k_var_dtype_f32, .opt_f32 = {.clamp = 0}, .persistent = 1
50 });
51
52 vg_var_push( (struct vg_var){
53 .name = "k_joint_bias", .data = &k_joint_bias,
54 .data_type = k_var_dtype_f32, .opt_f32 = {.clamp = 0}, .persistent = 1
55 });
56
57 vg_var_push( (struct vg_var){
58 .name = "k_joint_correction", .data = &k_joint_correction,
59 .data_type = k_var_dtype_f32, .opt_f32 = {.clamp = 0}, .persistent = 1
60 });
61
62 vg_var_push( (struct vg_var){
63 .name = "k_joint_impulse", .data = &k_joint_impulse,
64 .data_type = k_var_dtype_f32, .opt_f32 = {.clamp = 0}, .persistent = 1
65 });
66 }
67
68 /*
69 * -----------------------------------------------------------------------------
70 * structure definitions
71 * -----------------------------------------------------------------------------
72 */
73
74 typedef struct rigidbody rigidbody;
75 typedef struct contact rb_ct;
76 typedef struct rb_sphere rb_sphere;
77 typedef struct rb_capsule rb_capsule;
78 typedef struct rb_scene rb_scene;
79
80 struct rb_sphere
81 {
82 float radius;
83 };
84
85 struct rb_capsule
86 {
87 float height, radius;
88 };
89
90 struct rb_scene
91 {
92 bh_tree *bh_scene;
93 };
94
95 struct rigidbody
96 {
97 v3f co, v, w;
98 v4f q;
99
100 enum rb_shape
101 {
102 k_rb_shape_box = 0,
103 k_rb_shape_sphere = 1,
104 k_rb_shape_capsule = 2,
105 k_rb_shape_scene = 3
106 }
107 type;
108
109 union
110 {
111 struct rb_sphere sphere;
112 struct rb_capsule capsule;
113 struct rb_scene scene;
114 }
115 inf;
116
117 v3f right, up, forward;
118
119 int is_world;
120
121 boxf bbx, bbx_world;
122 float inv_mass;
123
124 /* inertia model and inverse world tensor */
125 v3f I;
126 m3x3f iI, iIw;
127
128 m4x3f to_world, to_local;
129 };
130
131 VG_STATIC struct contact
132 {
133 rigidbody *rba, *rbb;
134 v3f co, n;
135 v3f t[2];
136 float p, bias, norm_impulse, tangent_impulse[2],
137 normal_mass, tangent_mass[2];
138
139 u32 element_id;
140
141 enum contact_type type;
142 }
143 rb_contact_buffer[256];
144 VG_STATIC int rb_contact_count = 0;
145
146 typedef struct rb_constr_pos rb_constr_pos;
147 typedef struct rb_constr_swingtwist rb_constr_swingtwist;
148
149 struct rb_constr_pos
150 {
151 rigidbody *rba, *rbb;
152 v3f lca, lcb;
153 };
154
155 struct rb_constr_swingtwist
156 {
157 rigidbody *rba, *rbb;
158
159 v4f conevx, conevy; /* relative to rba */
160 v3f view_offset, /* relative to rba */
161 coneva, conevxb;/* relative to rbb */
162
163 int tangent_violation, axis_violation;
164 v3f axis, tangent_axis, tangent_target, axis_target;
165
166 float conet;
167 float tangent_mass, axis_mass;
168 };
169
170 struct rb_constr_spring
171 {
172 int nothing;
173 };
174
175 /*
176 * -----------------------------------------------------------------------------
177 * Math Utils
178 * -----------------------------------------------------------------------------
179 */
180
181 VG_STATIC float sphere_volume( float radius )
182 {
183 float r3 = radius*radius*radius;
184 return (4.0f/3.0f) * VG_PIf * r3;
185 }
186
187 VG_STATIC void rb_tangent_basis( v3f n, v3f tx, v3f ty )
188 {
189 /* Compute tangent basis (box2d) */
190 if( fabsf( n[0] ) >= 0.57735027f )
191 {
192 tx[0] = n[1];
193 tx[1] = -n[0];
194 tx[2] = 0.0f;
195 }
196 else
197 {
198 tx[0] = 0.0f;
199 tx[1] = n[2];
200 tx[2] = -n[1];
201 }
202
203 v3_normalize( tx );
204 v3_cross( n, tx, ty );
205 }
206
207 /*
208 * -----------------------------------------------------------------------------
209 * Debugging
210 * -----------------------------------------------------------------------------
211 */
212
213 VG_STATIC void rb_debug_contact( rb_ct *ct )
214 {
215 v3f p1;
216 v3_muladds( ct->co, ct->n, 0.05f, p1 );
217
218 if( ct->type == k_contact_type_default )
219 {
220 vg_line_pt3( ct->co, 0.0125f, 0xff0000ff );
221 vg_line( ct->co, p1, 0xffffffff );
222 }
223 else if( ct->type == k_contact_type_edge )
224 {
225 vg_line_pt3( ct->co, 0.0125f, 0xff00ffc0 );
226 vg_line( ct->co, p1, 0xffffffff );
227 }
228 }
229
230 VG_STATIC void debug_sphere( m4x3f m, float radius, u32 colour )
231 {
232 v3f ly = { 0.0f, 0.0f, radius },
233 lx = { 0.0f, radius, 0.0f },
234 lz = { 0.0f, 0.0f, radius };
235
236 for( int i=0; i<16; i++ )
237 {
238 float t = ((float)(i+1) * (1.0f/16.0f)) * VG_PIf * 2.0f,
239 s = sinf(t),
240 c = cosf(t);
241
242 v3f py = { s*radius, 0.0f, c*radius },
243 px = { s*radius, c*radius, 0.0f },
244 pz = { 0.0f, s*radius, c*radius };
245
246 v3f p0, p1, p2, p3, p4, p5;
247 m4x3_mulv( m, py, p0 );
248 m4x3_mulv( m, ly, p1 );
249 m4x3_mulv( m, px, p2 );
250 m4x3_mulv( m, lx, p3 );
251 m4x3_mulv( m, pz, p4 );
252 m4x3_mulv( m, lz, p5 );
253
254 vg_line( p0, p1, colour == 0x00? 0xff00ff00: colour );
255 vg_line( p2, p3, colour == 0x00? 0xff0000ff: colour );
256 vg_line( p4, p5, colour == 0x00? 0xffff0000: colour );
257
258 v3_copy( py, ly );
259 v3_copy( px, lx );
260 v3_copy( pz, lz );
261 }
262 }
263
264 VG_STATIC void debug_capsule( m4x3f m, float radius, float h, u32 colour )
265 {
266 v3f ly = { 0.0f, 0.0f, radius },
267 lx = { 0.0f, radius, 0.0f },
268 lz = { 0.0f, 0.0f, radius };
269
270 float s0 = sinf(0.0f)*radius,
271 c0 = cosf(0.0f)*radius;
272
273 v3f p0, p1, up, right, forward;
274 m3x3_mulv( m, (v3f){0.0f,1.0f,0.0f}, up );
275 m3x3_mulv( m, (v3f){1.0f,0.0f,0.0f}, right );
276 m3x3_mulv( m, (v3f){0.0f,0.0f,-1.0f}, forward );
277 v3_muladds( m[3], up, -h*0.5f+radius, p0 );
278 v3_muladds( m[3], up, h*0.5f-radius, p1 );
279
280 v3f a0, a1, b0, b1;
281 v3_muladds( p0, right, radius, a0 );
282 v3_muladds( p1, right, radius, a1 );
283 v3_muladds( p0, forward, radius, b0 );
284 v3_muladds( p1, forward, radius, b1 );
285 vg_line( a0, a1, colour );
286 vg_line( b0, b1, colour );
287
288 v3_muladds( p0, right, -radius, a0 );
289 v3_muladds( p1, right, -radius, a1 );
290 v3_muladds( p0, forward, -radius, b0 );
291 v3_muladds( p1, forward, -radius, b1 );
292 vg_line( a0, a1, colour );
293 vg_line( b0, b1, colour );
294
295 for( int i=0; i<16; i++ )
296 {
297 float t = ((float)(i+1) * (1.0f/16.0f)) * VG_PIf * 2.0f,
298 s1 = sinf(t)*radius,
299 c1 = cosf(t)*radius;
300
301 v3f e0 = { s0, 0.0f, c0 },
302 e1 = { s1, 0.0f, c1 },
303 e2 = { s0, c0, 0.0f },
304 e3 = { s1, c1, 0.0f },
305 e4 = { 0.0f, c0, s0 },
306 e5 = { 0.0f, c1, s1 };
307
308 m3x3_mulv( m, e0, e0 );
309 m3x3_mulv( m, e1, e1 );
310 m3x3_mulv( m, e2, e2 );
311 m3x3_mulv( m, e3, e3 );
312 m3x3_mulv( m, e4, e4 );
313 m3x3_mulv( m, e5, e5 );
314
315 v3_add( p0, e0, a0 );
316 v3_add( p0, e1, a1 );
317 v3_add( p1, e0, b0 );
318 v3_add( p1, e1, b1 );
319
320 vg_line( a0, a1, colour );
321 vg_line( b0, b1, colour );
322
323 if( c0 < 0.0f )
324 {
325 v3_add( p0, e2, a0 );
326 v3_add( p0, e3, a1 );
327 v3_add( p0, e4, b0 );
328 v3_add( p0, e5, b1 );
329 }
330 else
331 {
332 v3_add( p1, e2, a0 );
333 v3_add( p1, e3, a1 );
334 v3_add( p1, e4, b0 );
335 v3_add( p1, e5, b1 );
336 }
337
338 vg_line( a0, a1, colour );
339 vg_line( b0, b1, colour );
340
341 s0 = s1;
342 c0 = c1;
343 }
344 }
345
346 VG_STATIC void rb_debug( rigidbody *rb, u32 colour )
347 {
348 if( rb->type == k_rb_shape_box )
349 {
350 v3f *box = rb->bbx;
351 vg_line_boxf_transformed( rb->to_world, rb->bbx, colour );
352 }
353 else if( rb->type == k_rb_shape_sphere )
354 {
355 debug_sphere( rb->to_world, rb->inf.sphere.radius, colour );
356 }
357 else if( rb->type == k_rb_shape_capsule )
358 {
359 m4x3f m0, m1;
360 float h = rb->inf.capsule.height,
361 r = rb->inf.capsule.radius;
362
363 debug_capsule( rb->to_world, r, h, colour );
364 }
365 else if( rb->type == k_rb_shape_scene )
366 {
367 vg_line_boxf( rb->bbx, colour );
368 }
369 }
370
371 /*
372 * -----------------------------------------------------------------------------
373 * Integration
374 * -----------------------------------------------------------------------------
375 */
376
377 /*
378 * Update world space bounding box based on local one
379 */
380 VG_STATIC void rb_update_bounds( rigidbody *rb )
381 {
382 box_copy( rb->bbx, rb->bbx_world );
383 m4x3_transform_aabb( rb->to_world, rb->bbx_world );
384 }
385
386 /*
387 * Commit transform to rigidbody. Updates matrices
388 */
389 VG_STATIC void rb_update_transform( rigidbody *rb )
390 {
391 q_normalize( rb->q );
392 q_m3x3( rb->q, rb->to_world );
393 v3_copy( rb->co, rb->to_world[3] );
394
395 m4x3_invert_affine( rb->to_world, rb->to_local );
396
397 m3x3_mulv( rb->to_world, (v3f){1.0f,0.0f, 0.0f}, rb->right );
398 m3x3_mulv( rb->to_world, (v3f){0.0f,1.0f, 0.0f}, rb->up );
399 m3x3_mulv( rb->to_world, (v3f){0.0f,0.0f,-1.0f}, rb->forward );
400
401 m3x3_mul( rb->iI, rb->to_local, rb->iIw );
402 m3x3_mul( rb->to_world, rb->iIw, rb->iIw );
403
404 rb_update_bounds( rb );
405 }
406
407 /*
408 * Extrapolate rigidbody into a transform based on vg accumulator.
409 * Useful for rendering
410 */
411 #if 0
412 __attribute__ ((deprecated))
413 VG_STATIC void rb_extrapolate_transform( rigidbody *rb, m4x3f transform )
414 {
415 float substep = vg_clampf( vg.accumulator / k_rb_delta, 0.0f, 1.0f );
416
417 v3f co;
418 v4f q;
419
420 v3_muladds( rb->co, rb->v, k_rb_delta*substep, co );
421
422 if( v3_length2( rb->w ) > 0.0f )
423 {
424 v4f rotation;
425 v3f axis;
426 v3_copy( rb->w, axis );
427
428 float mag = v3_length( axis );
429 v3_divs( axis, mag, axis );
430 q_axis_angle( rotation, axis, mag*k_rb_delta*substep );
431 q_mul( rotation, rb->q, q );
432 q_normalize( q );
433 }
434 else
435 {
436 v4_copy( rb->q, q );
437 }
438
439 q_m3x3( q, transform );
440 v3_copy( co, transform[3] );
441 }
442 #endif
443
444 VG_STATIC void rb_extrapolate( rigidbody *rb, v3f co, v4f q )
445 {
446 float substep = vg_clampf( vg.accumulator / k_rb_delta, 0.0f, 1.0f );
447
448 v3_muladds( rb->co, rb->v, k_rb_delta*substep, co );
449
450 if( v3_length2( rb->w ) > 0.0f )
451 {
452 v4f rotation;
453 v3f axis;
454 v3_copy( rb->w, axis );
455
456 float mag = v3_length( axis );
457 v3_divs( axis, mag, axis );
458 q_axis_angle( rotation, axis, mag*k_rb_delta*substep );
459 q_mul( rotation, rb->q, q );
460 q_normalize( q );
461 }
462 else
463 {
464 v4_copy( rb->q, q );
465 }
466 }
467
468 /*
469 * Initialize rigidbody and calculate masses, inertia
470 */
471 VG_STATIC void rb_init( rigidbody *rb )
472 {
473 float volume = 1.0f;
474
475 if( rb->type == k_rb_shape_box )
476 {
477 v3f dims;
478 v3_sub( rb->bbx[1], rb->bbx[0], dims );
479 volume = dims[0]*dims[1]*dims[2];
480 }
481 else if( rb->type == k_rb_shape_sphere )
482 {
483 volume = sphere_volume( rb->inf.sphere.radius );
484 v3_fill( rb->bbx[0], -rb->inf.sphere.radius );
485 v3_fill( rb->bbx[1], rb->inf.sphere.radius );
486 }
487 else if( rb->type == k_rb_shape_capsule )
488 {
489 float r = rb->inf.capsule.radius,
490 h = rb->inf.capsule.height;
491 volume = sphere_volume( r ) + VG_PIf * r*r * (h - r*2.0f);
492
493 v3_fill( rb->bbx[0], -r );
494 v3_fill( rb->bbx[1], r );
495 rb->bbx[0][1] = -h;
496 rb->bbx[1][1] = h;
497 }
498 else if( rb->type == k_rb_shape_scene )
499 {
500 rb->is_world = 1;
501 box_copy( rb->inf.scene.bh_scene->nodes[0].bbx, rb->bbx );
502 }
503
504 if( rb->is_world )
505 {
506 rb->inv_mass = 0.0f;
507 v3_zero( rb->I );
508 m3x3_zero(rb->iI);
509 }
510 else
511 {
512 float mass = 2.0f*volume;
513 rb->inv_mass = 1.0f/mass;
514
515 v3f extent;
516 v3_sub( rb->bbx[1], rb->bbx[0], extent );
517 v3_muls( extent, 0.5f, extent );
518
519 /* local intertia tensor */
520 float scale = k_inertia_scale;
521 float ex2 = scale*extent[0]*extent[0],
522 ey2 = scale*extent[1]*extent[1],
523 ez2 = scale*extent[2]*extent[2];
524
525 rb->I[0] = ((1.0f/12.0f) * mass * (ey2+ez2));
526 rb->I[1] = ((1.0f/12.0f) * mass * (ex2+ez2));
527 rb->I[2] = ((1.0f/12.0f) * mass * (ex2+ey2));
528
529 m3x3_identity( rb->iI );
530 rb->iI[0][0] = rb->I[0];
531 rb->iI[1][1] = rb->I[1];
532 rb->iI[2][2] = rb->I[2];
533 m3x3_inv( rb->iI, rb->iI );
534 }
535
536 v3_zero( rb->v );
537 v3_zero( rb->w );
538
539 rb_update_transform( rb );
540 }
541
542 VG_STATIC void rb_iter( rigidbody *rb )
543 {
544 if( !vg_validf( rb->v[0] ) ||
545 !vg_validf( rb->v[1] ) ||
546 !vg_validf( rb->v[2] ) )
547 {
548 vg_fatal_exit_loop( "NaN velocity" );
549 }
550
551 v3f gravity = { 0.0f, -9.8f, 0.0f };
552 v3_muladds( rb->v, gravity, k_rb_delta, rb->v );
553
554 /* intergrate velocity */
555 v3_muladds( rb->co, rb->v, k_rb_delta, rb->co );
556 v3_lerp( rb->w, (v3f){0.0f,0.0f,0.0f}, 0.0025f, rb->w );
557
558 /* inegrate inertia */
559 if( v3_length2( rb->w ) > 0.0f )
560 {
561 v4f rotation;
562 v3f axis;
563 v3_copy( rb->w, axis );
564
565 float mag = v3_length( axis );
566 v3_divs( axis, mag, axis );
567 q_axis_angle( rotation, axis, mag*k_rb_delta );
568 q_mul( rotation, rb->q, rb->q );
569 }
570
571 /* damping */
572 v3_muls( rb->v, 1.0f/(1.0f+k_rb_delta*k_damp_linear), rb->v );
573 v3_muls( rb->w, 1.0f/(1.0f+k_rb_delta*k_damp_angular), rb->w );
574 }
575
576
577 /*
578 * -----------------------------------------------------------------------------
579 * Boolean shape overlap functions
580 * -----------------------------------------------------------------------------
581 */
582
583 /*
584 * Project AABB, and triangle interval onto axis to check if they overlap
585 */
586 VG_STATIC int rb_box_triangle_interval( v3f extent, v3f axis, v3f tri[3] )
587 {
588 float
589
590 r = extent[0] * fabsf(axis[0]) +
591 extent[1] * fabsf(axis[1]) +
592 extent[2] * fabsf(axis[2]),
593
594 p0 = v3_dot( axis, tri[0] ),
595 p1 = v3_dot( axis, tri[1] ),
596 p2 = v3_dot( axis, tri[2] ),
597
598 e = vg_maxf(-vg_maxf(p0,vg_maxf(p1,p2)), vg_minf(p0,vg_minf(p1,p2)));
599
600 if( e > r ) return 0;
601 else return 1;
602 }
603
604 /*
605 * Seperating axis test box vs triangle
606 */
607 VG_STATIC int rb_box_triangle_sat( v3f extent, v3f center,
608 m4x3f to_local, v3f tri_src[3] )
609 {
610 v3f tri[3];
611
612 for( int i=0; i<3; i++ ){
613 m4x3_mulv( to_local, tri_src[i], tri[i] );
614 v3_sub( tri[i], center, tri[i] );
615 }
616
617 /* u0, u1, u2 */
618 if(!rb_box_triangle_interval( extent, (v3f){1.0f,0.0f,0.0f}, tri )) return 0;
619 if(!rb_box_triangle_interval( extent, (v3f){0.0f,1.0f,0.0f}, tri )) return 0;
620 if(!rb_box_triangle_interval( extent, (v3f){0.0f,0.0f,1.0f}, tri )) return 0;
621
622 v3f v0,v1,v2,n, e0,e1,e2;
623 v3_sub( tri[1], tri[0], v0 );
624 v3_sub( tri[2], tri[0], v1 );
625 v3_sub( tri[2], tri[1], v2 );
626 v3_normalize( v0 );
627 v3_normalize( v1 );
628 v3_normalize( v2 );
629 v3_cross( v0, v1, n );
630 v3_cross( v0, n, e0 );
631 v3_cross( n, v1, e1 );
632 v3_cross( v2, n, e2 );
633
634 /* normal */
635 if(!rb_box_triangle_interval( extent, n, tri )) return 0;
636
637 v3f axis[9];
638 v3_cross( e0, (v3f){1.0f,0.0f,0.0f}, axis[0] );
639 v3_cross( e0, (v3f){0.0f,1.0f,0.0f}, axis[1] );
640 v3_cross( e0, (v3f){0.0f,0.0f,1.0f}, axis[2] );
641 v3_cross( e1, (v3f){1.0f,0.0f,0.0f}, axis[3] );
642 v3_cross( e1, (v3f){0.0f,1.0f,0.0f}, axis[4] );
643 v3_cross( e1, (v3f){0.0f,0.0f,1.0f}, axis[5] );
644 v3_cross( e2, (v3f){1.0f,0.0f,0.0f}, axis[6] );
645 v3_cross( e2, (v3f){0.0f,1.0f,0.0f}, axis[7] );
646 v3_cross( e2, (v3f){0.0f,0.0f,1.0f}, axis[8] );
647
648 for( int i=0; i<9; i++ )
649 if(!rb_box_triangle_interval( extent, axis[i], tri )) return 0;
650
651 return 1;
652 }
653
654 /*
655 * -----------------------------------------------------------------------------
656 * Manifold
657 * -----------------------------------------------------------------------------
658 */
659
660 VG_STATIC int rb_manifold_apply_filtered( rb_ct *man, int len )
661 {
662 int k = 0;
663
664 for( int i=0; i<len; i++ )
665 {
666 rb_ct *ct = &man[i];
667
668 if( ct->type == k_contact_type_disabled )
669 continue;
670
671 man[k ++] = man[i];
672 }
673
674 return k;
675 }
676
677 /*
678 * Merge two contacts if they are within radius(r) of eachother
679 */
680 VG_STATIC void rb_manifold_contact_weld( rb_ct *ci, rb_ct *cj, float r )
681 {
682 if( v3_dist2( ci->co, cj->co ) < r*r )
683 {
684 cj->type = k_contact_type_disabled;
685 ci->p = (ci->p + cj->p) * 0.5f;
686
687 v3_add( ci->co, cj->co, ci->co );
688 v3_muls( ci->co, 0.5f, ci->co );
689
690 v3f delta;
691 v3_sub( ci->rba->co, ci->co, delta );
692
693 float c0 = v3_dot( ci->n, delta ),
694 c1 = v3_dot( cj->n, delta );
695
696 if( c0 < 0.0f || c1 < 0.0f )
697 {
698 /* error */
699 ci->type = k_contact_type_disabled;
700 }
701 else
702 {
703 v3f n;
704 v3_muls( ci->n, c0, n );
705 v3_muladds( n, cj->n, c1, n );
706 v3_normalize( n );
707 v3_copy( n, ci->n );
708 }
709 }
710 }
711
712 /*
713 *
714 */
715 VG_STATIC void rb_manifold_filter_joint_edges( rb_ct *man, int len, float r )
716 {
717 for( int i=0; i<len-1; i++ )
718 {
719 rb_ct *ci = &man[i];
720 if( ci->type != k_contact_type_edge )
721 continue;
722
723 for( int j=i+1; j<len; j++ )
724 {
725 rb_ct *cj = &man[j];
726 if( cj->type != k_contact_type_edge )
727 continue;
728
729 rb_manifold_contact_weld( ci, cj, r );
730 }
731 }
732 }
733
734 /*
735 * Resolve overlapping pairs
736 *
737 * TODO: Remove?
738 */
739 VG_STATIC void rb_manifold_filter_pairs( rb_ct *man, int len, float r )
740 {
741 for( int i=0; i<len-1; i++ )
742 {
743 rb_ct *ci = &man[i];
744 int similar = 0;
745
746 if( ci->type == k_contact_type_disabled ) continue;
747
748 for( int j=i+1; j<len; j++ )
749 {
750 rb_ct *cj = &man[j];
751
752 if( cj->type == k_contact_type_disabled ) continue;
753
754 if( v3_dist2( ci->co, cj->co ) < r*r )
755 {
756 cj->type = k_contact_type_disabled;
757 v3_add( cj->n, ci->n, ci->n );
758 ci->p += cj->p;
759 similar ++;
760 }
761 }
762
763 if( similar )
764 {
765 float n = 1.0f/((float)similar+1.0f);
766 v3_muls( ci->n, n, ci->n );
767 ci->p *= n;
768
769 if( v3_length2(ci->n) < 0.1f*0.1f )
770 ci->type = k_contact_type_disabled;
771 else
772 v3_normalize( ci->n );
773 }
774 }
775 }
776
777 /*
778 * Remove contacts that are facing away from A
779 */
780 VG_STATIC void rb_manifold_filter_backface( rb_ct *man, int len )
781 {
782 for( int i=0; i<len; i++ )
783 {
784 rb_ct *ct = &man[i];
785 if( ct->type == k_contact_type_disabled )
786 continue;
787
788 v3f delta;
789 v3_sub( ct->co, ct->rba->co, delta );
790
791 if( v3_dot( delta, ct->n ) > -0.001f )
792 ct->type = k_contact_type_disabled;
793 }
794 }
795
796 /*
797 * Filter out duplicate coplanar results. Good for spheres.
798 */
799 VG_STATIC void rb_manifold_filter_coplanar( rb_ct *man, int len, float w )
800 {
801 for( int i=0; i<len; i++ )
802 {
803 rb_ct *ci = &man[i];
804 if( ci->type == k_contact_type_disabled ||
805 ci->type == k_contact_type_edge )
806 continue;
807
808 float d1 = v3_dot( ci->co, ci->n );
809
810 for( int j=0; j<len; j++ )
811 {
812 if( j == i )
813 continue;
814
815 rb_ct *cj = &man[j];
816 if( cj->type == k_contact_type_disabled )
817 continue;
818
819 float d2 = v3_dot( cj->co, ci->n ),
820 d = d2-d1;
821
822 if( fabsf( d ) <= w )
823 {
824 cj->type = k_contact_type_disabled;
825 }
826 }
827 }
828 }
829
830 /*
831 * -----------------------------------------------------------------------------
832 * Collision matrix
833 * -----------------------------------------------------------------------------
834 */
835
836 /*
837 * Contact generators
838 *
839 * These do not automatically allocate contacts, an appropriately sized
840 * buffer must be supplied. The function returns the size of the manifold
841 * which was generated.
842 *
843 * The values set on the contacts are: n, co, p, rba, rbb
844 */
845
846 /*
847 * By collecting the minimum(time) and maximum(time) pairs of points, we
848 * build a reduced and stable exact manifold.
849 *
850 * tx: time at point
851 * rx: minimum distance of these points
852 * dx: the delta between the two points
853 *
854 * pairs will only ammend these if they are creating a collision
855 */
856 typedef struct capsule_manifold capsule_manifold;
857 struct capsule_manifold
858 {
859 float t0, t1;
860 float r0, r1;
861 v3f d0, d1;
862 };
863
864 /*
865 * Expand a line manifold with a new pair. t value is the time along segment
866 * on the oriented object which created this pair.
867 */
868 VG_STATIC void rb_capsule_manifold( v3f pa, v3f pb, float t, float r,
869 capsule_manifold *manifold )
870 {
871 v3f delta;
872 v3_sub( pa, pb, delta );
873
874 if( v3_length2(delta) < r*r )
875 {
876 if( t < manifold->t0 )
877 {
878 v3_copy( delta, manifold->d0 );
879 manifold->t0 = t;
880 manifold->r0 = r;
881 }
882
883 if( t > manifold->t1 )
884 {
885 v3_copy( delta, manifold->d1 );
886 manifold->t1 = t;
887 manifold->r1 = r;
888 }
889 }
890 }
891
892 VG_STATIC void rb_capsule_manifold_init( capsule_manifold *manifold )
893 {
894 manifold->t0 = INFINITY;
895 manifold->t1 = -INFINITY;
896 }
897
898 #if 0
899 __attribute__ ((deprecated))
900 VG_STATIC int rb_capsule_manifold_done( rigidbody *rba, rigidbody *rbb,
901 capsule_manifold *manifold, rb_ct *buf )
902 {
903 float h = rba->inf.capsule.height,
904 ra = rba->inf.capsule.radius;
905
906 v3f p0, p1;
907 v3_muladds( rba->co, rba->up, -h*0.5f+ra, p0 );
908 v3_muladds( rba->co, rba->up, h*0.5f-ra, p1 );
909
910 int count = 0;
911 if( manifold->t0 <= 1.0f )
912 {
913 rb_ct *ct = buf;
914
915 v3f pa;
916 v3_muls( p0, 1.0f-manifold->t0, pa );
917 v3_muladds( pa, p1, manifold->t0, pa );
918
919 float d = v3_length( manifold->d0 );
920 v3_muls( manifold->d0, 1.0f/d, ct->n );
921 v3_muladds( pa, ct->n, -ra, ct->co );
922
923 ct->p = manifold->r0 - d;
924 ct->rba = rba;
925 ct->rbb = rbb;
926 ct->type = k_contact_type_default;
927
928 count ++;
929 }
930
931 if( (manifold->t1 >= 0.0f) && (manifold->t0 != manifold->t1) )
932 {
933 rb_ct *ct = buf+count;
934
935 v3f pa;
936 v3_muls( p0, 1.0f-manifold->t1, pa );
937 v3_muladds( pa, p1, manifold->t1, pa );
938
939 float d = v3_length( manifold->d1 );
940 v3_muls( manifold->d1, 1.0f/d, ct->n );
941 v3_muladds( pa, ct->n, -ra, ct->co );
942
943 ct->p = manifold->r1 - d;
944 ct->rba = rba;
945 ct->rbb = rbb;
946 ct->type = k_contact_type_default;
947
948 count ++;
949 }
950
951 /*
952 * Debugging
953 */
954
955 if( count == 2 )
956 vg_line( buf[0].co, buf[1].co, 0xff0000ff );
957
958 return count;
959 }
960 #endif
961
962 VG_STATIC int rb_capsule__manifold_done( m4x3f mtx, rb_capsule *c,
963 capsule_manifold *manifold,
964 rb_ct *buf )
965 {
966 v3f p0, p1;
967 v3_muladds( mtx[3], mtx[1], -c->height*0.5f+c->radius, p0 );
968 v3_muladds( mtx[3], mtx[1], c->height*0.5f-c->radius, p1 );
969
970 int count = 0;
971 if( manifold->t0 <= 1.0f )
972 {
973 rb_ct *ct = buf;
974
975 v3f pa;
976 v3_muls( p0, 1.0f-manifold->t0, pa );
977 v3_muladds( pa, p1, manifold->t0, pa );
978
979 float d = v3_length( manifold->d0 );
980 v3_muls( manifold->d0, 1.0f/d, ct->n );
981 v3_muladds( pa, ct->n, -c->radius, ct->co );
982
983 ct->p = manifold->r0 - d;
984 ct->type = k_contact_type_default;
985 count ++;
986 }
987
988 if( (manifold->t1 >= 0.0f) && (manifold->t0 != manifold->t1) )
989 {
990 rb_ct *ct = buf+count;
991
992 v3f pa;
993 v3_muls( p0, 1.0f-manifold->t1, pa );
994 v3_muladds( pa, p1, manifold->t1, pa );
995
996 float d = v3_length( manifold->d1 );
997 v3_muls( manifold->d1, 1.0f/d, ct->n );
998 v3_muladds( pa, ct->n, -c->radius, ct->co );
999
1000 ct->p = manifold->r1 - d;
1001 ct->type = k_contact_type_default;
1002
1003 count ++;
1004 }
1005
1006 /*
1007 * Debugging
1008 */
1009
1010 if( count == 2 )
1011 vg_line( buf[0].co, buf[1].co, 0xff0000ff );
1012
1013 return count;
1014 }
1015
1016 VG_STATIC int rb_capsule_sphere( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
1017 {
1018 float h = rba->inf.capsule.height,
1019 ra = rba->inf.capsule.radius,
1020 rb = rbb->inf.sphere.radius;
1021
1022 v3f p0, p1;
1023 v3_muladds( rba->co, rba->up, -h*0.5f+ra, p0 );
1024 v3_muladds( rba->co, rba->up, h*0.5f-ra, p1 );
1025
1026 v3f c, delta;
1027 closest_point_segment( p0, p1, rbb->co, c );
1028 v3_sub( c, rbb->co, delta );
1029
1030 float d2 = v3_length2(delta),
1031 r = ra + rb;
1032
1033 if( d2 < r*r )
1034 {
1035 float d = sqrtf(d2);
1036
1037 rb_ct *ct = buf;
1038 v3_muls( delta, 1.0f/d, ct->n );
1039 ct->p = r-d;
1040
1041 v3f p0, p1;
1042 v3_muladds( c, ct->n, -ra, p0 );
1043 v3_muladds( rbb->co, ct->n, rb, p1 );
1044 v3_add( p0, p1, ct->co );
1045 v3_muls( ct->co, 0.5f, ct->co );
1046
1047 ct->rba = rba;
1048 ct->rbb = rbb;
1049 ct->type = k_contact_type_default;
1050
1051 return 1;
1052 }
1053
1054 return 0;
1055 }
1056
1057 VG_STATIC int rb_capsule__capsule( m4x3f mtxA, rb_capsule *ca,
1058 m4x3f mtxB, rb_capsule *cb, rb_ct *buf )
1059 {
1060 float ha = ca->height,
1061 hb = cb->height,
1062 ra = ca->radius,
1063 rb = cb->radius,
1064 r = ra+rb;
1065
1066 v3f p0, p1, p2, p3;
1067 v3_muladds( mtxA[3], mtxA[1], -ha*0.5f+ra, p0 );
1068 v3_muladds( mtxA[3], mtxA[1], ha*0.5f-ra, p1 );
1069 v3_muladds( mtxB[3], mtxB[1], -hb*0.5f+rb, p2 );
1070 v3_muladds( mtxB[3], mtxB[1], hb*0.5f-rb, p3 );
1071
1072 capsule_manifold manifold;
1073 rb_capsule_manifold_init( &manifold );
1074
1075 v3f pa, pb;
1076 float ta, tb;
1077 closest_segment_segment( p0, p1, p2, p3, &ta, &tb, pa, pb );
1078 rb_capsule_manifold( pa, pb, ta, r, &manifold );
1079
1080 ta = closest_point_segment( p0, p1, p2, pa );
1081 tb = closest_point_segment( p0, p1, p3, pb );
1082 rb_capsule_manifold( pa, p2, ta, r, &manifold );
1083 rb_capsule_manifold( pb, p3, tb, r, &manifold );
1084
1085 closest_point_segment( p2, p3, p0, pa );
1086 closest_point_segment( p2, p3, p1, pb );
1087 rb_capsule_manifold( p0, pa, 0.0f, r, &manifold );
1088 rb_capsule_manifold( p1, pb, 1.0f, r, &manifold );
1089
1090 return rb_capsule__manifold_done( mtxA, ca, &manifold, buf );
1091 }
1092
1093 #if 0
1094 /*
1095 * Generates up to two contacts; optimised for the most stable manifold
1096 */
1097 VG_STATIC int rb_capsule_box( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
1098 {
1099 float h = rba->inf.capsule.height,
1100 r = rba->inf.capsule.radius;
1101
1102 /*
1103 * Solving this in symetric local space of the cube saves us some time and a
1104 * couple branches when it comes to the quad stage.
1105 */
1106 v3f centroid;
1107 v3_add( rbb->bbx[0], rbb->bbx[1], centroid );
1108 v3_muls( centroid, 0.5f, centroid );
1109
1110 boxf bbx;
1111 v3_sub( rbb->bbx[0], centroid, bbx[0] );
1112 v3_sub( rbb->bbx[1], centroid, bbx[1] );
1113
1114 v3f pc, p0w, p1w, p0, p1;
1115 v3_muladds( rba->co, rba->up, -h*0.5f+r, p0w );
1116 v3_muladds( rba->co, rba->up, h*0.5f-r, p1w );
1117
1118 m4x3_mulv( rbb->to_local, p0w, p0 );
1119 m4x3_mulv( rbb->to_local, p1w, p1 );
1120 v3_sub( p0, centroid, p0 );
1121 v3_sub( p1, centroid, p1 );
1122 v3_add( p0, p1, pc );
1123 v3_muls( pc, 0.5f, pc );
1124
1125 /*
1126 * Finding an appropriate quad to collide lines with
1127 */
1128 v3f region;
1129 v3_div( pc, bbx[1], region );
1130
1131 v3f quad[4];
1132 if( (fabsf(region[0]) > fabsf(region[1])) &&
1133 (fabsf(region[0]) > fabsf(region[2])) )
1134 {
1135 float px = vg_signf(region[0]) * bbx[1][0];
1136 v3_copy( (v3f){ px, bbx[0][1], bbx[0][2] }, quad[0] );
1137 v3_copy( (v3f){ px, bbx[1][1], bbx[0][2] }, quad[1] );
1138 v3_copy( (v3f){ px, bbx[1][1], bbx[1][2] }, quad[2] );
1139 v3_copy( (v3f){ px, bbx[0][1], bbx[1][2] }, quad[3] );
1140 }
1141 else if( fabsf(region[1]) > fabsf(region[2]) )
1142 {
1143 float py = vg_signf(region[1]) * bbx[1][1];
1144 v3_copy( (v3f){ bbx[0][0], py, bbx[0][2] }, quad[0] );
1145 v3_copy( (v3f){ bbx[1][0], py, bbx[0][2] }, quad[1] );
1146 v3_copy( (v3f){ bbx[1][0], py, bbx[1][2] }, quad[2] );
1147 v3_copy( (v3f){ bbx[0][0], py, bbx[1][2] }, quad[3] );
1148 }
1149 else
1150 {
1151 float pz = vg_signf(region[2]) * bbx[1][2];
1152 v3_copy( (v3f){ bbx[0][0], bbx[0][1], pz }, quad[0] );
1153 v3_copy( (v3f){ bbx[1][0], bbx[0][1], pz }, quad[1] );
1154 v3_copy( (v3f){ bbx[1][0], bbx[1][1], pz }, quad[2] );
1155 v3_copy( (v3f){ bbx[0][0], bbx[1][1], pz }, quad[3] );
1156 }
1157
1158 capsule_manifold manifold;
1159 rb_capsule_manifold_init( &manifold );
1160
1161 v3f c0, c1;
1162 closest_point_aabb( p0, bbx, c0 );
1163 closest_point_aabb( p1, bbx, c1 );
1164
1165 v3f d0, d1, da;
1166 v3_sub( c0, p0, d0 );
1167 v3_sub( c1, p1, d1 );
1168 v3_sub( p1, p0, da );
1169
1170 v3_normalize(d0);
1171 v3_normalize(d1);
1172 v3_normalize(da);
1173
1174 if( v3_dot( da, d0 ) <= 0.01f )
1175 rb_capsule_manifold( p0, c0, 0.0f, r, &manifold );
1176
1177 if( v3_dot( da, d1 ) >= -0.01f )
1178 rb_capsule_manifold( p1, c1, 1.0f, r, &manifold );
1179
1180 for( int i=0; i<4; i++ )
1181 {
1182 int i0 = i,
1183 i1 = (i+1)%4;
1184
1185 v3f ca, cb;
1186 float ta, tb;
1187 closest_segment_segment( p0, p1, quad[i0], quad[i1], &ta, &tb, ca, cb );
1188 rb_capsule_manifold( ca, cb, ta, r, &manifold );
1189 }
1190
1191 /*
1192 * Create final contacts based on line manifold
1193 */
1194 m3x3_mulv( rbb->to_world, manifold.d0, manifold.d0 );
1195 m3x3_mulv( rbb->to_world, manifold.d1, manifold.d1 );
1196
1197 /*
1198 * Debugging
1199 */
1200
1201 #if 0
1202 for( int i=0; i<4; i++ )
1203 {
1204 v3f q0, q1;
1205 int i0 = i,
1206 i1 = (i+1)%4;
1207
1208 v3_add( quad[i0], centroid, q0 );
1209 v3_add( quad[i1], centroid, q1 );
1210
1211 m4x3_mulv( rbb->to_world, q0, q0 );
1212 m4x3_mulv( rbb->to_world, q1, q1 );
1213
1214 vg_line( q0, q1, 0xffffffff );
1215 }
1216 #endif
1217
1218 return rb_capsule_manifold_done( rba, rbb, &manifold, buf );
1219 }
1220 #endif
1221
1222 VG_STATIC int rb_sphere_box( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
1223 {
1224 v3f co, delta;
1225
1226 closest_point_obb( rba->co, rbb->bbx, rbb->to_world, rbb->to_local, co );
1227 v3_sub( rba->co, co, delta );
1228
1229 float d2 = v3_length2(delta),
1230 r = rba->inf.sphere.radius;
1231
1232 if( d2 <= r*r )
1233 {
1234 float d;
1235
1236 rb_ct *ct = buf;
1237 if( d2 <= 0.0001f )
1238 {
1239 v3_sub( rba->co, rbb->co, delta );
1240
1241 /*
1242 * some extra testing is required to find the best axis to push the
1243 * object back outside the box. Since there isnt a clear seperating
1244 * vector already, especially on really high aspect boxes.
1245 */
1246 float lx = v3_dot( rbb->right, delta ),
1247 ly = v3_dot( rbb->up, delta ),
1248 lz = v3_dot( rbb->forward, delta ),
1249 px = rbb->bbx[1][0] - fabsf(lx),
1250 py = rbb->bbx[1][1] - fabsf(ly),
1251 pz = rbb->bbx[1][2] - fabsf(lz);
1252
1253 if( px < py && px < pz )
1254 v3_muls( rbb->right, vg_signf(lx), ct->n );
1255 else if( py < pz )
1256 v3_muls( rbb->up, vg_signf(ly), ct->n );
1257 else
1258 v3_muls( rbb->forward, vg_signf(lz), ct->n );
1259
1260 v3_muladds( rba->co, ct->n, -r, ct->co );
1261 ct->p = r;
1262 }
1263 else
1264 {
1265 d = sqrtf(d2);
1266 v3_muls( delta, 1.0f/d, ct->n );
1267 ct->p = r-d;
1268 v3_copy( co, ct->co );
1269 }
1270
1271 ct->rba = rba;
1272 ct->rbb = rbb;
1273 ct->type = k_contact_type_default;
1274 return 1;
1275 }
1276
1277 return 0;
1278 }
1279
1280 VG_STATIC int rb_sphere_sphere( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
1281 {
1282 v3f delta;
1283 v3_sub( rba->co, rbb->co, delta );
1284
1285 float d2 = v3_length2(delta),
1286 r = rba->inf.sphere.radius + rbb->inf.sphere.radius;
1287
1288 if( d2 < r*r )
1289 {
1290 float d = sqrtf(d2);
1291
1292 rb_ct *ct = buf;
1293 v3_muls( delta, 1.0f/d, ct->n );
1294
1295 v3f p0, p1;
1296 v3_muladds( rba->co, ct->n,-rba->inf.sphere.radius, p0 );
1297 v3_muladds( rbb->co, ct->n, rbb->inf.sphere.radius, p1 );
1298 v3_add( p0, p1, ct->co );
1299 v3_muls( ct->co, 0.5f, ct->co );
1300 ct->type = k_contact_type_default;
1301 ct->p = r-d;
1302 ct->rba = rba;
1303 ct->rbb = rbb;
1304 return 1;
1305 }
1306
1307 return 0;
1308 }
1309
1310 //#define RIGIDBODY_DYNAMIC_MESH_EDGES
1311
1312 #if 0
1313 __attribute__ ((deprecated))
1314 VG_STATIC int rb_sphere_triangle( rigidbody *rba, rigidbody *rbb,
1315 v3f tri[3], rb_ct *buf )
1316 {
1317 v3f delta, co;
1318
1319 #ifdef RIGIDBODY_DYNAMIC_MESH_EDGES
1320 closest_on_triangle_1( rba->co, tri, co );
1321 #else
1322 enum contact_type type = closest_on_triangle_1( rba->co, tri, co );
1323 #endif
1324
1325 v3_sub( rba->co, co, delta );
1326
1327 float d2 = v3_length2( delta ),
1328 r = rba->inf.sphere.radius;
1329
1330 if( d2 < r*r )
1331 {
1332 rb_ct *ct = buf;
1333
1334 v3f ab, ac, tn;
1335 v3_sub( tri[2], tri[0], ab );
1336 v3_sub( tri[1], tri[0], ac );
1337 v3_cross( ac, ab, tn );
1338 v3_copy( tn, ct->n );
1339
1340 if( v3_length2( ct->n ) <= 0.00001f )
1341 {
1342 vg_error( "Zero area triangle!\n" );
1343 return 0;
1344 }
1345
1346 v3_normalize( ct->n );
1347
1348 float d = sqrtf(d2);
1349
1350 v3_copy( co, ct->co );
1351 ct->type = type;
1352 ct->p = r-d;
1353 ct->rba = rba;
1354 ct->rbb = rbb;
1355 return 1;
1356 }
1357
1358 return 0;
1359 }
1360 #endif
1361
1362 VG_STATIC int rb_sphere__triangle( m4x3f mtxA, rb_sphere *b,
1363 v3f tri[3], rb_ct *buf )
1364 {
1365 v3f delta, co;
1366 enum contact_type type = closest_on_triangle_1( mtxA[3], tri, co );
1367
1368 v3_sub( mtxA[3], co, delta );
1369
1370 float d2 = v3_length2( delta ),
1371 r = b->radius;
1372
1373 if( d2 <= r*r )
1374 {
1375 rb_ct *ct = buf;
1376
1377 v3f ab, ac, tn;
1378 v3_sub( tri[2], tri[0], ab );
1379 v3_sub( tri[1], tri[0], ac );
1380 v3_cross( ac, ab, tn );
1381 v3_copy( tn, ct->n );
1382
1383 if( v3_length2( ct->n ) <= 0.00001f )
1384 {
1385 vg_error( "Zero area triangle!\n" );
1386 return 0;
1387 }
1388
1389 v3_normalize( ct->n );
1390
1391 float d = sqrtf(d2);
1392
1393 v3_copy( co, ct->co );
1394 ct->type = type;
1395 ct->p = r-d;
1396 return 1;
1397 }
1398
1399 return 0;
1400 }
1401
1402 VG_STATIC void rb_debug_sharp_scene_edges( rigidbody *rbb, float sharp_ang,
1403 boxf box, u32 colour )
1404 {
1405 sharp_ang = cosf( sharp_ang );
1406
1407 scene *sc = rbb->inf.scene.bh_scene->user;
1408 vg_line_boxf( box, 0xff00ff00 );
1409
1410 bh_iter it;
1411 bh_iter_init( 0, &it );
1412 int idx;
1413
1414 while( bh_next( rbb->inf.scene.bh_scene, &it, box, &idx ) )
1415 {
1416 u32 *ptri = &sc->arrindices[ idx*3 ];
1417 v3f tri[3];
1418
1419 for( int j=0; j<3; j++ )
1420 v3_copy( sc->arrvertices[ptri[j]].co, tri[j] );
1421
1422 for( int j=0; j<3; j++ )
1423 {
1424 #if 0
1425 v3f edir;
1426 v3_sub( tri[(j+1)%3], tri[j], edir );
1427
1428 if( v3_dot( edir, (v3f){ 0.5184758473652127f,
1429 0.2073903389460850f,
1430 -0.8295613557843402f } ) < 0.0f )
1431 continue;
1432 #endif
1433
1434 bh_iter jt;
1435 bh_iter_init( 0, &jt );
1436
1437 boxf region;
1438 float const k_r = 0.02f;
1439 v3_add( (v3f){ k_r, k_r, k_r }, tri[j], region[1] );
1440 v3_add( (v3f){ -k_r, -k_r, -k_r }, tri[j], region[0] );
1441
1442 int jdx;
1443 while( bh_next( rbb->inf.scene.bh_scene, &jt, region, &jdx ) )
1444 {
1445 if( idx <= jdx )
1446 continue;
1447
1448 u32 *ptrj = &sc->arrindices[ jdx*3 ];
1449 v3f trj[3];
1450
1451 for( int k=0; k<3; k++ )
1452 v3_copy( sc->arrvertices[ptrj[k]].co, trj[k] );
1453
1454 for( int k=0; k<3; k++ )
1455 {
1456 if( v3_dist2( tri[j], trj[k] ) <= k_r*k_r )
1457 {
1458 int jp1 = (j+1)%3,
1459 jp2 = (j+2)%3,
1460 km1 = (k+3-1)%3,
1461 km2 = (k+3-2)%3;
1462
1463 if( v3_dist2( tri[jp1], trj[km1] ) <= k_r*k_r )
1464 {
1465 v3f b0, b1, b2;
1466 v3_sub( tri[jp1], tri[j], b0 );
1467 v3_sub( tri[jp2], tri[j], b1 );
1468 v3_sub( trj[km2], tri[j], b2 );
1469
1470 v3f cx0, cx1;
1471 v3_cross( b0, b1, cx0 );
1472 v3_cross( b2, b0, cx1 );
1473
1474 float polarity = v3_dot( cx0, b2 );
1475
1476 if( polarity < 0.0f )
1477 {
1478 #if 0
1479 vg_line( tri[j], tri[jp1], 0xff00ff00 );
1480 float ang = v3_dot(cx0,cx1) /
1481 (v3_length(cx0)*v3_length(cx1));
1482 if( ang < sharp_ang )
1483 {
1484 vg_line( tri[j], tri[jp1], 0xff00ff00 );
1485 }
1486 #endif
1487 }
1488 }
1489 }
1490 }
1491 }
1492 }
1493 }
1494 }
1495
1496 VG_STATIC int rb_sphere__scene( m4x3f mtxA, rb_sphere *b,
1497 m4x3f mtxB, rb_scene *s, rb_ct *buf )
1498 {
1499 scene *sc = s->bh_scene->user;
1500
1501 bh_iter it;
1502 bh_iter_init( 0, &it );
1503 int idx;
1504
1505 int count = 0;
1506
1507 float r = b->radius + 0.1f;
1508 boxf box;
1509 v3_sub( mtxA[3], (v3f){ r,r,r }, box[0] );
1510 v3_add( mtxA[3], (v3f){ r,r,r }, box[1] );
1511
1512 while( bh_next( s->bh_scene, &it, box, &idx ) )
1513 {
1514 u32 *ptri = &sc->arrindices[ idx*3 ];
1515 v3f tri[3];
1516
1517 for( int j=0; j<3; j++ )
1518 v3_copy( sc->arrvertices[ptri[j]].co, tri[j] );
1519
1520 buf[ count ].element_id = ptri[0];
1521
1522 vg_line( tri[0],tri[1],0x70ff6000 );
1523 vg_line( tri[1],tri[2],0x70ff6000 );
1524 vg_line( tri[2],tri[0],0x70ff6000 );
1525
1526 int contact = rb_sphere__triangle( mtxA, b, tri, &buf[count] );
1527 count += contact;
1528
1529 if( count == 16 )
1530 {
1531 vg_warn( "Exceeding sphere_vs_scene capacity. Geometry too dense!\n" );
1532 return count;
1533 }
1534 }
1535
1536 return count;
1537 }
1538
1539 #if 0
1540 __attribute__ ((deprecated))
1541 VG_STATIC int rb_sphere_scene( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
1542 {
1543 scene *sc = rbb->inf.scene.bh_scene->user;
1544
1545 bh_iter it;
1546 bh_iter_init( 0, &it );
1547 int idx;
1548
1549 int count = 0;
1550
1551 while( bh_next( rbb->inf.scene.bh_scene, &it, rba->bbx_world, &idx ) )
1552 {
1553 u32 *ptri = &sc->arrindices[ idx*3 ];
1554 v3f tri[3];
1555
1556 for( int j=0; j<3; j++ )
1557 v3_copy( sc->arrvertices[ptri[j]].co, tri[j] );
1558
1559 buf[ count ].element_id = ptri[0];
1560
1561 vg_line( tri[0],tri[1],0x70ff6000 );
1562 vg_line( tri[1],tri[2],0x70ff6000 );
1563 vg_line( tri[2],tri[0],0x70ff6000 );
1564
1565 int contact = rb_sphere_triangle( rba, rbb, tri, buf+count );
1566 count += contact;
1567
1568 if( count == 16 )
1569 {
1570 vg_warn( "Exceeding sphere_vs_scene capacity. Geometry too dense!\n" );
1571 return count;
1572 }
1573 }
1574
1575 return count;
1576 }
1577 #endif
1578
1579 VG_STATIC int rb_box__scene( m4x3f mtxA, boxf bbx,
1580 m4x3f mtxB, rb_scene *s, rb_ct *buf )
1581 {
1582 scene *sc = s->bh_scene->user;
1583 v3f tri[3];
1584
1585 v3f extent, center;
1586 v3_sub( bbx[1], bbx[0], extent );
1587 v3_muls( extent, 0.5f, extent );
1588 v3_add( bbx[0], extent, center );
1589
1590 float r = v3_length(extent);
1591 boxf world_bbx;
1592 v3_fill( world_bbx[0], -r );
1593 v3_fill( world_bbx[1], r );
1594 for( int i=0; i<2; i++ ){
1595 v3_add( center, world_bbx[i], world_bbx[i] );
1596 v3_add( mtxA[3], world_bbx[i], world_bbx[i] );
1597 }
1598
1599 m4x3f to_local;
1600 m4x3_invert_affine( mtxA, to_local );
1601
1602 bh_iter it;
1603 bh_iter_init( 0, &it );
1604 int idx;
1605 int count = 0;
1606
1607 vg_line_boxf( world_bbx, VG__RED );
1608
1609 while( bh_next( s->bh_scene, &it, world_bbx, &idx ) ){
1610 u32 *ptri = &sc->arrindices[ idx*3 ];
1611
1612 for( int j=0; j<3; j++ )
1613 v3_copy( sc->arrvertices[ptri[j]].co, tri[j] );
1614
1615 if( rb_box_triangle_sat( extent, center, to_local, tri ) ){
1616 vg_line(tri[0],tri[1],0xff50ff00 );
1617 vg_line(tri[1],tri[2],0xff50ff00 );
1618 vg_line(tri[2],tri[0],0xff50ff00 );
1619 }
1620 else{
1621 vg_line(tri[0],tri[1],0xff0000ff );
1622 vg_line(tri[1],tri[2],0xff0000ff );
1623 vg_line(tri[2],tri[0],0xff0000ff );
1624 continue;
1625 }
1626
1627 v3f v0,v1,n;
1628 v3_sub( tri[1], tri[0], v0 );
1629 v3_sub( tri[2], tri[0], v1 );
1630 v3_cross( v0, v1, n );
1631 v3_normalize( n );
1632
1633 /* find best feature */
1634 float best = v3_dot( mtxA[0], n );
1635 int axis = 0;
1636
1637 for( int i=1; i<3; i++ ){
1638 float c = v3_dot( mtxA[i], n );
1639
1640 if( fabsf(c) > fabsf(best) ){
1641 best = c;
1642 axis = i;
1643 }
1644 }
1645
1646 v3f manifold[4];
1647
1648 if( axis == 0 ){
1649 float px = best > 0.0f? bbx[0][0]: bbx[1][0];
1650 manifold[0][0] = px;
1651 manifold[0][1] = bbx[0][1];
1652 manifold[0][2] = bbx[0][2];
1653 manifold[1][0] = px;
1654 manifold[1][1] = bbx[1][1];
1655 manifold[1][2] = bbx[0][2];
1656 manifold[2][0] = px;
1657 manifold[2][1] = bbx[1][1];
1658 manifold[2][2] = bbx[1][2];
1659 manifold[3][0] = px;
1660 manifold[3][1] = bbx[0][1];
1661 manifold[3][2] = bbx[1][2];
1662 }
1663 else if( axis == 1 ){
1664 float py = best > 0.0f? bbx[0][1]: bbx[1][1];
1665 manifold[0][0] = bbx[0][0];
1666 manifold[0][1] = py;
1667 manifold[0][2] = bbx[0][2];
1668 manifold[1][0] = bbx[1][0];
1669 manifold[1][1] = py;
1670 manifold[1][2] = bbx[0][2];
1671 manifold[2][0] = bbx[1][0];
1672 manifold[2][1] = py;
1673 manifold[2][2] = bbx[1][2];
1674 manifold[3][0] = bbx[0][0];
1675 manifold[3][1] = py;
1676 manifold[3][2] = bbx[1][2];
1677 }
1678 else{
1679 float pz = best > 0.0f? bbx[0][2]: bbx[1][2];
1680 manifold[0][0] = bbx[0][0];
1681 manifold[0][1] = bbx[0][1];
1682 manifold[0][2] = pz;
1683 manifold[1][0] = bbx[1][0];
1684 manifold[1][1] = bbx[0][1];
1685 manifold[1][2] = pz;
1686 manifold[2][0] = bbx[1][0];
1687 manifold[2][1] = bbx[1][1];
1688 manifold[2][2] = pz;
1689 manifold[3][0] = bbx[0][0];
1690 manifold[3][1] = bbx[1][1];
1691 manifold[3][2] = pz;
1692 }
1693
1694 for( int j=0; j<4; j++ )
1695 m4x3_mulv( mtxA, manifold[j], manifold[j] );
1696
1697 vg_line( manifold[0], manifold[1], 0xffffffff );
1698 vg_line( manifold[1], manifold[2], 0xffffffff );
1699 vg_line( manifold[2], manifold[3], 0xffffffff );
1700 vg_line( manifold[3], manifold[0], 0xffffffff );
1701
1702 for( int j=0; j<4; j++ ){
1703 rb_ct *ct = buf+count;
1704
1705 v3_copy( manifold[j], ct->co );
1706 v3_copy( n, ct->n );
1707
1708 float l0 = v3_dot( tri[0], n ),
1709 l1 = v3_dot( manifold[j], n );
1710
1711 ct->p = (l0-l1)*0.5f;
1712 if( ct->p < 0.0f )
1713 continue;
1714
1715 ct->type = k_contact_type_default;
1716 count ++;
1717
1718 if( count >= 12 )
1719 return count;
1720 }
1721 }
1722 return count;
1723 }
1724
1725 #if 0
1726 __attribute__ ((deprecated))
1727 VG_STATIC int rb_box_scene( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
1728 {
1729 scene *sc = rbb->inf.scene.bh_scene->user;
1730
1731 v3f tri[3];
1732
1733 bh_iter it;
1734 bh_iter_init( 0, &it );
1735 int idx;
1736
1737 int count = 0;
1738
1739 while( bh_next( rbb->inf.scene.bh_scene, &it, rba->bbx_world, &idx ) )
1740 {
1741 u32 *ptri = &sc->arrindices[ idx*3 ];
1742
1743 for( int j=0; j<3; j++ )
1744 v3_copy( sc->arrvertices[ptri[j]].co, tri[j] );
1745
1746 if( rb_box_triangle_sat( rba, tri ) )
1747 {
1748 vg_line(tri[0],tri[1],0xff50ff00 );
1749 vg_line(tri[1],tri[2],0xff50ff00 );
1750 vg_line(tri[2],tri[0],0xff50ff00 );
1751 }
1752 else
1753 {
1754 vg_line(tri[0],tri[1],0xff0000ff );
1755 vg_line(tri[1],tri[2],0xff0000ff );
1756 vg_line(tri[2],tri[0],0xff0000ff );
1757
1758 continue;
1759 }
1760
1761 v3f v0,v1,n;
1762 v3_sub( tri[1], tri[0], v0 );
1763 v3_sub( tri[2], tri[0], v1 );
1764 v3_cross( v0, v1, n );
1765 v3_normalize( n );
1766
1767 /* find best feature */
1768 float best = v3_dot( rba->right, n );
1769 int axis = 0;
1770
1771 float cy = v3_dot( rba->up, n );
1772 if( fabsf(cy) > fabsf(best) )
1773 {
1774 best = cy;
1775 axis = 1;
1776 }
1777
1778 float cz = -v3_dot( rba->forward, n );
1779 if( fabsf(cz) > fabsf(best) )
1780 {
1781 best = cz;
1782 axis = 2;
1783 }
1784
1785 v3f manifold[4];
1786
1787 if( axis == 0 )
1788 {
1789 float px = best > 0.0f? rba->bbx[0][0]: rba->bbx[1][0];
1790 manifold[0][0] = px;
1791 manifold[0][1] = rba->bbx[0][1];
1792 manifold[0][2] = rba->bbx[0][2];
1793 manifold[1][0] = px;
1794 manifold[1][1] = rba->bbx[1][1];
1795 manifold[1][2] = rba->bbx[0][2];
1796 manifold[2][0] = px;
1797 manifold[2][1] = rba->bbx[1][1];
1798 manifold[2][2] = rba->bbx[1][2];
1799 manifold[3][0] = px;
1800 manifold[3][1] = rba->bbx[0][1];
1801 manifold[3][2] = rba->bbx[1][2];
1802 }
1803 else if( axis == 1 )
1804 {
1805 float py = best > 0.0f? rba->bbx[0][1]: rba->bbx[1][1];
1806 manifold[0][0] = rba->bbx[0][0];
1807 manifold[0][1] = py;
1808 manifold[0][2] = rba->bbx[0][2];
1809 manifold[1][0] = rba->bbx[1][0];
1810 manifold[1][1] = py;
1811 manifold[1][2] = rba->bbx[0][2];
1812 manifold[2][0] = rba->bbx[1][0];
1813 manifold[2][1] = py;
1814 manifold[2][2] = rba->bbx[1][2];
1815 manifold[3][0] = rba->bbx[0][0];
1816 manifold[3][1] = py;
1817 manifold[3][2] = rba->bbx[1][2];
1818 }
1819 else
1820 {
1821 float pz = best > 0.0f? rba->bbx[0][2]: rba->bbx[1][2];
1822 manifold[0][0] = rba->bbx[0][0];
1823 manifold[0][1] = rba->bbx[0][1];
1824 manifold[0][2] = pz;
1825 manifold[1][0] = rba->bbx[1][0];
1826 manifold[1][1] = rba->bbx[0][1];
1827 manifold[1][2] = pz;
1828 manifold[2][0] = rba->bbx[1][0];
1829 manifold[2][1] = rba->bbx[1][1];
1830 manifold[2][2] = pz;
1831 manifold[3][0] = rba->bbx[0][0];
1832 manifold[3][1] = rba->bbx[1][1];
1833 manifold[3][2] = pz;
1834 }
1835
1836 for( int j=0; j<4; j++ )
1837 m4x3_mulv( rba->to_world, manifold[j], manifold[j] );
1838
1839 vg_line( manifold[0], manifold[1], 0xffffffff );
1840 vg_line( manifold[1], manifold[2], 0xffffffff );
1841 vg_line( manifold[2], manifold[3], 0xffffffff );
1842 vg_line( manifold[3], manifold[0], 0xffffffff );
1843
1844 for( int j=0; j<4; j++ )
1845 {
1846 rb_ct *ct = buf+count;
1847
1848 v3_copy( manifold[j], ct->co );
1849 v3_copy( n, ct->n );
1850
1851 float l0 = v3_dot( tri[0], n ),
1852 l1 = v3_dot( manifold[j], n );
1853
1854 ct->p = (l0-l1)*0.5f;
1855 if( ct->p < 0.0f )
1856 continue;
1857
1858 ct->type = k_contact_type_default;
1859 ct->rba = rba;
1860 ct->rbb = rbb;
1861 count ++;
1862
1863 if( count >= 12 )
1864 return count;
1865 }
1866 }
1867 return count;
1868 }
1869 #endif
1870
1871 VG_STATIC int rb_capsule__triangle( m4x3f mtxA, rb_capsule *c,
1872 v3f tri[3], rb_ct *buf )
1873 {
1874 v3f pc, p0w, p1w;
1875 v3_muladds( mtxA[3], mtxA[1], -c->height*0.5f+c->radius, p0w );
1876 v3_muladds( mtxA[3], mtxA[1], c->height*0.5f-c->radius, p1w );
1877
1878 capsule_manifold manifold;
1879 rb_capsule_manifold_init( &manifold );
1880
1881 v3f c0, c1;
1882 closest_on_triangle_1( p0w, tri, c0 );
1883 closest_on_triangle_1( p1w, tri, c1 );
1884
1885 v3f d0, d1, da;
1886 v3_sub( c0, p0w, d0 );
1887 v3_sub( c1, p1w, d1 );
1888 v3_sub( p1w, p0w, da );
1889
1890 v3_normalize(d0);
1891 v3_normalize(d1);
1892 v3_normalize(da);
1893
1894 if( v3_dot( da, d0 ) <= 0.01f )
1895 rb_capsule_manifold( p0w, c0, 0.0f, c->radius, &manifold );
1896
1897 if( v3_dot( da, d1 ) >= -0.01f )
1898 rb_capsule_manifold( p1w, c1, 1.0f, c->radius, &manifold );
1899
1900 for( int i=0; i<3; i++ )
1901 {
1902 int i0 = i,
1903 i1 = (i+1)%3;
1904
1905 v3f ca, cb;
1906 float ta, tb;
1907 closest_segment_segment( p0w, p1w, tri[i0], tri[i1], &ta, &tb, ca, cb );
1908 rb_capsule_manifold( ca, cb, ta, c->radius, &manifold );
1909 }
1910
1911 v3f v0, v1, n;
1912 v3_sub( tri[1], tri[0], v0 );
1913 v3_sub( tri[2], tri[0], v1 );
1914 v3_cross( v0, v1, n );
1915 v3_normalize( n );
1916
1917 int count = rb_capsule__manifold_done( mtxA, c, &manifold, buf );
1918 for( int i=0; i<count; i++ )
1919 v3_copy( n, buf[i].n );
1920
1921 return count;
1922 }
1923
1924 /*
1925 * Generates up to two contacts; optimised for the most stable manifold
1926 */
1927 #if 0
1928 __attribute__ ((deprecated))
1929 VG_STATIC int rb_capsule_triangle( rigidbody *rba, rigidbody *rbb,
1930 v3f tri[3], rb_ct *buf )
1931 {
1932 float h = rba->inf.capsule.height,
1933 r = rba->inf.capsule.radius;
1934
1935 v3f pc, p0w, p1w;
1936 v3_muladds( rba->co, rba->up, -h*0.5f+r, p0w );
1937 v3_muladds( rba->co, rba->up, h*0.5f-r, p1w );
1938
1939 capsule_manifold manifold;
1940 rb_capsule_manifold_init( &manifold );
1941
1942 v3f c0, c1;
1943 closest_on_triangle_1( p0w, tri, c0 );
1944 closest_on_triangle_1( p1w, tri, c1 );
1945
1946 v3f d0, d1, da;
1947 v3_sub( c0, p0w, d0 );
1948 v3_sub( c1, p1w, d1 );
1949 v3_sub( p1w, p0w, da );
1950
1951 v3_normalize(d0);
1952 v3_normalize(d1);
1953 v3_normalize(da);
1954
1955 if( v3_dot( da, d0 ) <= 0.01f )
1956 rb_capsule_manifold( p0w, c0, 0.0f, r, &manifold );
1957
1958 if( v3_dot( da, d1 ) >= -0.01f )
1959 rb_capsule_manifold( p1w, c1, 1.0f, r, &manifold );
1960
1961 for( int i=0; i<3; i++ )
1962 {
1963 int i0 = i,
1964 i1 = (i+1)%3;
1965
1966 v3f ca, cb;
1967 float ta, tb;
1968 closest_segment_segment( p0w, p1w, tri[i0], tri[i1], &ta, &tb, ca, cb );
1969 rb_capsule_manifold( ca, cb, ta, r, &manifold );
1970 }
1971
1972 v3f v0, v1, n;
1973 v3_sub( tri[1], tri[0], v0 );
1974 v3_sub( tri[2], tri[0], v1 );
1975 v3_cross( v0, v1, n );
1976 v3_normalize( n );
1977
1978 int count = rb_capsule_manifold_done( rba, rbb, &manifold, buf );
1979 for( int i=0; i<count; i++ )
1980 v3_copy( n, buf[i].n );
1981
1982 return count;
1983 }
1984 #endif
1985
1986 /* mtxB is defined only for tradition; it is not used currently */
1987 VG_STATIC int rb_capsule__scene( m4x3f mtxA, rb_capsule *c,
1988 m4x3f mtxB, rb_scene *s,
1989 rb_ct *buf )
1990 {
1991 bh_iter it;
1992 bh_iter_init( 0, &it );
1993 int idx;
1994 int count = 0;
1995
1996 boxf bbx;
1997 v3_sub( mtxA[3], (v3f){ c->height, c->height, c->height }, bbx[0] );
1998 v3_add( mtxA[3], (v3f){ c->height, c->height, c->height }, bbx[1] );
1999
2000 scene *sc = s->bh_scene->user;
2001
2002 while( bh_next( s->bh_scene, &it, bbx, &idx ) )
2003 {
2004 u32 *ptri = &sc->arrindices[ idx*3 ];
2005 v3f tri[3];
2006
2007 for( int j=0; j<3; j++ )
2008 v3_copy( sc->arrvertices[ptri[j]].co, tri[j] );
2009
2010 buf[ count ].element_id = ptri[0];
2011
2012 int contact = rb_capsule__triangle( mtxA, c, tri, &buf[count] );
2013 count += contact;
2014
2015 if( count >= 16 )
2016 {
2017 vg_warn("Exceeding capsule_vs_scene capacity. Geometry too dense!\n");
2018 return count;
2019 }
2020 }
2021
2022 return count;
2023 }
2024
2025 #if 0
2026 __attribute__ ((deprecated))
2027 VG_STATIC int rb_capsule_scene( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
2028 {
2029 scene *sc = rbb->inf.scene.bh_scene->user;
2030
2031 bh_iter it;
2032 bh_iter_init( 0, &it );
2033 int idx;
2034
2035 int count = 0;
2036
2037 while( bh_next( rbb->inf.scene.bh_scene, &it, rba->bbx_world, &idx ) )
2038 {
2039 u32 *ptri = &sc->arrindices[ idx*3 ];
2040 v3f tri[3];
2041
2042 for( int j=0; j<3; j++ )
2043 v3_copy( sc->arrvertices[ptri[j]].co, tri[j] );
2044
2045 buf[ count ].element_id = ptri[0];
2046
2047 #if 0
2048 vg_line( tri[0],tri[1],0x70ff6000 );
2049 vg_line( tri[1],tri[2],0x70ff6000 );
2050 vg_line( tri[2],tri[0],0x70ff6000 );
2051 #endif
2052
2053 int contact = rb_capsule_triangle( rba, rbb, tri, buf+count );
2054 count += contact;
2055
2056 if( count == 16 )
2057 {
2058 vg_warn("Exceeding capsule_vs_scene capacity. Geometry too dense!\n");
2059 return count;
2060 }
2061 }
2062
2063 return count;
2064 }
2065
2066 VG_STATIC int rb_scene_capsule( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
2067 {
2068 return rb_capsule_scene( rbb, rba, buf );
2069 }
2070 #endif
2071
2072 VG_STATIC int RB_MATRIX_ERROR( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
2073 {
2074 #if 0
2075 vg_error( "Collision type is unimplemented between types %d and %d\n",
2076 rba->type, rbb->type );
2077 #endif
2078
2079 return 0;
2080 }
2081
2082 VG_STATIC int rb_sphere_capsule( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
2083 {
2084 return rb_capsule_sphere( rbb, rba, buf );
2085 }
2086
2087 #if 0
2088 VG_STATIC int rb_box_capsule( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
2089 {
2090 return rb_capsule_box( rbb, rba, buf );
2091 }
2092 #endif
2093
2094 VG_STATIC int rb_box_sphere( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
2095 {
2096 return rb_sphere_box( rbb, rba, buf );
2097 }
2098
2099 #if 0
2100 VG_STATIC int rb_scene_box( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
2101 {
2102 return rb_box_scene( rbb, rba, buf );
2103 }
2104 #endif
2105
2106 #if 0
2107 VG_STATIC int (*rb_jump_table[4][4])( rigidbody *a, rigidbody *b, rb_ct *buf ) =
2108 {
2109 /* box */ /* Sphere */ /* Capsule */ /* Mesh */
2110 { RB_MATRIX_ERROR, rb_box_sphere, rb_box_capsule, rb_box_scene },
2111 { rb_sphere_box, rb_sphere_sphere, rb_sphere_capsule, rb_sphere_scene },
2112 { rb_capsule_box, rb_capsule_sphere, rb_capsule_capsule, rb_capsule_scene },
2113 { rb_scene_box, RB_MATRIX_ERROR, rb_scene_capsule, RB_MATRIX_ERROR }
2114 };
2115
2116 VG_STATIC int rb_collide( rigidbody *rba, rigidbody *rbb )
2117 {
2118 int (*collider_jump)(rigidbody *rba, rigidbody *rbb, rb_ct *buf )
2119 = rb_jump_table[rba->type][rbb->type];
2120
2121 /*
2122 * 12 is the maximum manifold size we can generate, so we are forced to abort
2123 * potentially checking any more.
2124 */
2125 if( rb_contact_count + 12 > vg_list_size(rb_contact_buffer) )
2126 {
2127 vg_warn( "Too many contacts made in global collider buffer (%d of %d\n)",
2128 rb_contact_count, vg_list_size(rb_contact_buffer) );
2129 return 0;
2130 }
2131
2132 /*
2133 * FUTURE: Replace this with a more dedicated broad phase pass
2134 */
2135 if( box_overlap( rba->bbx_world, rbb->bbx_world ) )
2136 {
2137 int count = collider_jump( rba, rbb, rb_contact_buffer+rb_contact_count);
2138 rb_contact_count += count;
2139 return count;
2140 }
2141 else
2142 return 0;
2143 }
2144 #endif
2145
2146 VG_STATIC int rb_global_has_space( void )
2147 {
2148 if( rb_contact_count + 16 > vg_list_size(rb_contact_buffer) )
2149 return 0;
2150
2151 return 1;
2152 }
2153
2154 VG_STATIC rb_ct *rb_global_buffer( void )
2155 {
2156 return &rb_contact_buffer[ rb_contact_count ];
2157 }
2158
2159 /*
2160 * -----------------------------------------------------------------------------
2161 * Dynamics
2162 * -----------------------------------------------------------------------------
2163 */
2164
2165 VG_STATIC void rb_solver_reset(void)
2166 {
2167 rb_contact_count = 0;
2168 }
2169
2170 VG_STATIC rb_ct *rb_global_ct(void)
2171 {
2172 return rb_contact_buffer + rb_contact_count;
2173 }
2174
2175 VG_STATIC void rb_prepare_contact( rb_ct *ct, float timestep )
2176 {
2177 ct->bias = -0.2f * (timestep*3600.0f)
2178 * vg_minf( 0.0f, -ct->p+k_penetration_slop );
2179
2180 rb_tangent_basis( ct->n, ct->t[0], ct->t[1] );
2181 ct->norm_impulse = 0.0f;
2182 ct->tangent_impulse[0] = 0.0f;
2183 ct->tangent_impulse[1] = 0.0f;
2184 }
2185
2186 /* calculate total move. manifold should belong to ONE object only */
2187 VG_STATIC void rb_depenetrate( rb_ct *manifold, int len, v3f dt )
2188 {
2189 v3_zero( dt );
2190
2191 for( int j=0; j<7; j++ )
2192 {
2193 for( int i=0; i<len; i++ )
2194 {
2195 struct contact *ct = &manifold[i];
2196
2197 float resolved_amt = v3_dot( ct->n, dt ),
2198 remaining = (ct->p-k_penetration_slop) - resolved_amt,
2199 apply = vg_maxf( remaining, 0.0f ) * 0.4f;
2200
2201 v3_muladds( dt, ct->n, apply, dt );
2202 }
2203 }
2204 }
2205
2206 /*
2207 * Initializing things like tangent vectors
2208 */
2209 VG_STATIC void rb_presolve_contacts( rb_ct *buffer, int len )
2210 {
2211 for( int i=0; i<len; i++ ){
2212 rb_ct *ct = &buffer[i];
2213 rb_prepare_contact( ct, k_rb_delta );
2214
2215 v3f ra, rb, raCn, rbCn, raCt, rbCt;
2216 v3_sub( ct->co, ct->rba->co, ra );
2217 v3_sub( ct->co, ct->rbb->co, rb );
2218 v3_cross( ra, ct->n, raCn );
2219 v3_cross( rb, ct->n, rbCn );
2220
2221 /* orient inverse inertia tensors */
2222 v3f raCnI, rbCnI;
2223 m3x3_mulv( ct->rba->iIw, raCn, raCnI );
2224 m3x3_mulv( ct->rbb->iIw, rbCn, rbCnI );
2225
2226 ct->normal_mass = ct->rba->inv_mass + ct->rbb->inv_mass;
2227 ct->normal_mass += v3_dot( raCn, raCnI );
2228 ct->normal_mass += v3_dot( rbCn, rbCnI );
2229 ct->normal_mass = 1.0f/ct->normal_mass;
2230
2231 for( int j=0; j<2; j++ ){
2232 v3f raCtI, rbCtI;
2233 v3_cross( ct->t[j], ra, raCt );
2234 v3_cross( ct->t[j], rb, rbCt );
2235 m3x3_mulv( ct->rba->iIw, raCt, raCtI );
2236 m3x3_mulv( ct->rbb->iIw, rbCt, rbCtI );
2237
2238 ct->tangent_mass[j] = ct->rba->inv_mass + ct->rbb->inv_mass;
2239 ct->tangent_mass[j] += v3_dot( raCt, raCtI );
2240 ct->tangent_mass[j] += v3_dot( rbCt, rbCtI );
2241 ct->tangent_mass[j] = 1.0f/ct->tangent_mass[j];
2242 }
2243
2244 rb_debug_contact( ct );
2245 }
2246 }
2247
2248 /*
2249 * Creates relative contact velocity vector
2250 */
2251 VG_STATIC void rb_rcv( rigidbody *rba, rigidbody *rbb, v3f ra, v3f rb, v3f rv )
2252 {
2253 v3f rva, rvb;
2254 v3_cross( rba->w, ra, rva );
2255 v3_add( rba->v, rva, rva );
2256 v3_cross( rbb->w, rb, rvb );
2257 v3_add( rbb->v, rvb, rvb );
2258
2259 v3_sub( rva, rvb, rv );
2260 }
2261
2262 /*
2263 * Apply impulse to object
2264 */
2265 VG_STATIC void rb_linear_impulse( rigidbody *rb, v3f delta, v3f impulse )
2266 {
2267 /* linear */
2268 v3_muladds( rb->v, impulse, rb->inv_mass, rb->v );
2269
2270 /* Angular velocity */
2271 v3f wa;
2272 v3_cross( delta, impulse, wa );
2273
2274 m3x3_mulv( rb->iIw, wa, wa );
2275 v3_add( rb->w, wa, rb->w );
2276 }
2277
2278 /*
2279 * One iteration to solve the contact constraint
2280 */
2281 VG_STATIC void rb_solve_contacts( rb_ct *buf, int len )
2282 {
2283 for( int i=0; i<len; i++ ){
2284 struct contact *ct = &buf[i];
2285
2286 v3f rv, ra, rb;
2287 v3_sub( ct->co, ct->rba->co, ra );
2288 v3_sub( ct->co, ct->rbb->co, rb );
2289 rb_rcv( ct->rba, ct->rbb, ra, rb, rv );
2290
2291 /* Friction */
2292 for( int j=0; j<2; j++ ){
2293 float f = k_friction * ct->norm_impulse,
2294 vt = v3_dot( rv, ct->t[j] ),
2295 lambda = ct->tangent_mass[j] * -vt;
2296
2297 float temp = ct->tangent_impulse[j];
2298 ct->tangent_impulse[j] = vg_clampf( temp + lambda, -f, f );
2299 lambda = ct->tangent_impulse[j] - temp;
2300
2301 v3f impulse;
2302 v3_muls( ct->t[j], lambda, impulse );
2303 rb_linear_impulse( ct->rba, ra, impulse );
2304
2305 v3_muls( ct->t[j], -lambda, impulse );
2306 rb_linear_impulse( ct->rbb, rb, impulse );
2307 }
2308
2309 /* Normal */
2310 rb_rcv( ct->rba, ct->rbb, ra, rb, rv );
2311 float vn = v3_dot( rv, ct->n ),
2312 lambda = ct->normal_mass * (-vn + ct->bias);
2313
2314 float temp = ct->norm_impulse;
2315 ct->norm_impulse = vg_maxf( temp + lambda, 0.0f );
2316 lambda = ct->norm_impulse - temp;
2317
2318 v3f impulse;
2319 v3_muls( ct->n, lambda, impulse );
2320 rb_linear_impulse( ct->rba, ra, impulse );
2321
2322 v3_muls( ct->n, -lambda, impulse );
2323 rb_linear_impulse( ct->rbb, rb, impulse );
2324 }
2325 }
2326
2327 /*
2328 * -----------------------------------------------------------------------------
2329 * Constraints
2330 * -----------------------------------------------------------------------------
2331 */
2332
2333 VG_STATIC void rb_debug_position_constraints( rb_constr_pos *buffer, int len )
2334 {
2335 for( int i=0; i<len; i++ )
2336 {
2337 rb_constr_pos *constr = &buffer[i];
2338 rigidbody *rba = constr->rba, *rbb = constr->rbb;
2339
2340 v3f wca, wcb;
2341 m3x3_mulv( rba->to_world, constr->lca, wca );
2342 m3x3_mulv( rbb->to_world, constr->lcb, wcb );
2343
2344 v3f p0, p1;
2345 v3_add( wca, rba->co, p0 );
2346 v3_add( wcb, rbb->co, p1 );
2347 vg_line_pt3( p0, 0.0025f, 0xff000000 );
2348 vg_line_pt3( p1, 0.0025f, 0xffffffff );
2349 vg_line2( p0, p1, 0xff000000, 0xffffffff );
2350 }
2351 }
2352
2353 VG_STATIC void rb_presolve_swingtwist_constraints( rb_constr_swingtwist *buf,
2354 int len )
2355 {
2356 float size = 0.12f;
2357
2358 for( int i=0; i<len; i++ )
2359 {
2360 rb_constr_swingtwist *st = &buf[ i ];
2361
2362 v3f vx, vy, va, vxb, axis, center;
2363
2364 m3x3_mulv( st->rba->to_world, st->conevx, vx );
2365 m3x3_mulv( st->rbb->to_world, st->conevxb, vxb );
2366 m3x3_mulv( st->rba->to_world, st->conevy, vy );
2367 m3x3_mulv( st->rbb->to_world, st->coneva, va );
2368 m4x3_mulv( st->rba->to_world, st->view_offset, center );
2369 v3_cross( vy, vx, axis );
2370
2371 /* Constraint violated ? */
2372 float fx = v3_dot( vx, va ), /* projection world */
2373 fy = v3_dot( vy, va ),
2374 fn = v3_dot( va, axis ),
2375
2376 rx = st->conevx[3], /* elipse radii */
2377 ry = st->conevy[3],
2378
2379 lx = fx/rx, /* projection local (fn==lz) */
2380 ly = fy/ry;
2381
2382 st->tangent_violation = ((lx*lx + ly*ly) > fn*fn) || (fn <= 0.0f);
2383 if( st->tangent_violation )
2384 {
2385 /* Calculate a good position and the axis to solve on */
2386 v2f closest, tangent,
2387 p = { fx/fabsf(fn), fy/fabsf(fn) };
2388
2389 closest_point_elipse( p, (v2f){rx,ry}, closest );
2390 tangent[0] = -closest[1] / (ry*ry);
2391 tangent[1] = closest[0] / (rx*rx);
2392 v2_normalize( tangent );
2393
2394 v3f v0, v1;
2395 v3_muladds( axis, vx, closest[0], v0 );
2396 v3_muladds( v0, vy, closest[1], v0 );
2397 v3_normalize( v0 );
2398
2399 v3_muls( vx, tangent[0], v1 );
2400 v3_muladds( v1, vy, tangent[1], v1 );
2401
2402 v3_copy( v0, st->tangent_target );
2403 v3_copy( v1, st->tangent_axis );
2404
2405 /* calculate mass */
2406 v3f aIw, bIw;
2407 m3x3_mulv( st->rba->iIw, st->tangent_axis, aIw );
2408 m3x3_mulv( st->rbb->iIw, st->tangent_axis, bIw );
2409 st->tangent_mass = 1.0f / (v3_dot( st->tangent_axis, aIw ) +
2410 v3_dot( st->tangent_axis, bIw ));
2411
2412 float angle = v3_dot( va, st->tangent_target );
2413 }
2414
2415 v3f refaxis;
2416 v3_cross( vy, va, refaxis ); /* our default rotation */
2417 v3_normalize( refaxis );
2418
2419 float angle = v3_dot( refaxis, vxb );
2420 st->axis_violation = fabsf(angle) < st->conet;
2421
2422 if( st->axis_violation )
2423 {
2424 v3f dir_test;
2425 v3_cross( refaxis, vxb, dir_test );
2426
2427 if( v3_dot(dir_test, va) < 0.0f )
2428 st->axis_violation = -st->axis_violation;
2429
2430 float newang = (float)st->axis_violation * acosf(st->conet-0.0001f);
2431
2432 v3f refaxis_up;
2433 v3_cross( va, refaxis, refaxis_up );
2434 v3_muls( refaxis_up, sinf(newang), st->axis_target );
2435 v3_muladds( st->axis_target, refaxis, -cosf(newang), st->axis_target );
2436
2437 /* calculate mass */
2438 v3_copy( va, st->axis );
2439 v3f aIw, bIw;
2440 m3x3_mulv( st->rba->iIw, st->axis, aIw );
2441 m3x3_mulv( st->rbb->iIw, st->axis, bIw );
2442 st->axis_mass = 1.0f / (v3_dot( st->axis, aIw ) +
2443 v3_dot( st->axis, bIw ));
2444 }
2445 }
2446 }
2447
2448 VG_STATIC void rb_debug_swingtwist_constraints( rb_constr_swingtwist *buf,
2449 int len )
2450 {
2451 float size = 0.12f;
2452
2453 for( int i=0; i<len; i++ )
2454 {
2455 rb_constr_swingtwist *st = &buf[ i ];
2456
2457 v3f vx, vxb, vy, va, axis, center;
2458
2459 m3x3_mulv( st->rba->to_world, st->conevx, vx );
2460 m3x3_mulv( st->rbb->to_world, st->conevxb, vxb );
2461 m3x3_mulv( st->rba->to_world, st->conevy, vy );
2462 m3x3_mulv( st->rbb->to_world, st->coneva, va );
2463 m4x3_mulv( st->rba->to_world, st->view_offset, center );
2464 v3_cross( vy, vx, axis );
2465
2466 float rx = st->conevx[3], /* elipse radii */
2467 ry = st->conevy[3];
2468
2469 v3f p0, p1;
2470 v3_muladds( center, va, size, p1 );
2471 vg_line( center, p1, 0xffffffff );
2472 vg_line_pt3( p1, 0.00025f, 0xffffffff );
2473
2474 if( st->tangent_violation )
2475 {
2476 v3_muladds( center, st->tangent_target, size, p0 );
2477
2478 vg_line( center, p0, 0xff00ff00 );
2479 vg_line_pt3( p0, 0.00025f, 0xff00ff00 );
2480 vg_line( p1, p0, 0xff000000 );
2481 }
2482
2483 for( int x=0; x<32; x++ )
2484 {
2485 float t0 = ((float)x * (1.0f/32.0f)) * VG_TAUf,
2486 t1 = (((float)x+1.0f) * (1.0f/32.0f)) * VG_TAUf,
2487 c0 = cosf( t0 ),
2488 s0 = sinf( t0 ),
2489 c1 = cosf( t1 ),
2490 s1 = sinf( t1 );
2491
2492 v3f v0, v1;
2493 v3_muladds( axis, vx, c0*rx, v0 );
2494 v3_muladds( v0, vy, s0*ry, v0 );
2495 v3_muladds( axis, vx, c1*rx, v1 );
2496 v3_muladds( v1, vy, s1*ry, v1 );
2497
2498 v3_normalize( v0 );
2499 v3_normalize( v1 );
2500
2501 v3_muladds( center, v0, size, p0 );
2502 v3_muladds( center, v1, size, p1 );
2503
2504 u32 col0r = fabsf(c0) * 255.0f,
2505 col0g = fabsf(s0) * 255.0f,
2506 col1r = fabsf(c1) * 255.0f,
2507 col1g = fabsf(s1) * 255.0f,
2508 col = st->tangent_violation? 0xff0000ff: 0xff000000,
2509 col0 = col | (col0r<<16) | (col0g << 8),
2510 col1 = col | (col1r<<16) | (col1g << 8);
2511
2512 vg_line2( center, p0, VG__NONE, col0 );
2513 vg_line2( p0, p1, col0, col1 );
2514 }
2515
2516 /* Draw twist */
2517 v3_muladds( center, va, size, p0 );
2518 v3_muladds( p0, vxb, size, p1 );
2519
2520 vg_line( p0, p1, 0xff0000ff );
2521
2522 if( st->axis_violation )
2523 {
2524 v3_muladds( p0, st->axis_target, size*1.25f, p1 );
2525 vg_line( p0, p1, 0xffffff00 );
2526 vg_line_pt3( p1, 0.0025f, 0xffffff80 );
2527 }
2528
2529 v3f refaxis;
2530 v3_cross( vy, va, refaxis ); /* our default rotation */
2531 v3_normalize( refaxis );
2532 v3f refaxis_up;
2533 v3_cross( va, refaxis, refaxis_up );
2534 float newang = acosf(st->conet-0.0001f);
2535
2536 v3_muladds( p0, refaxis_up, sinf(newang)*size, p1 );
2537 v3_muladds( p1, refaxis, -cosf(newang)*size, p1 );
2538 vg_line( p0, p1, 0xff000000 );
2539
2540 v3_muladds( p0, refaxis_up, sinf(-newang)*size, p1 );
2541 v3_muladds( p1, refaxis, -cosf(-newang)*size, p1 );
2542 vg_line( p0, p1, 0xff404040 );
2543 }
2544 }
2545
2546 /*
2547 * Solve a list of positional constraints
2548 */
2549 VG_STATIC void rb_solve_position_constraints( rb_constr_pos *buf, int len )
2550 {
2551 for( int i=0; i<len; i++ )
2552 {
2553 rb_constr_pos *constr = &buf[i];
2554 rigidbody *rba = constr->rba, *rbb = constr->rbb;
2555
2556 v3f wa, wb;
2557 m3x3_mulv( rba->to_world, constr->lca, wa );
2558 m3x3_mulv( rbb->to_world, constr->lcb, wb );
2559
2560 m3x3f ssra, ssrat, ssrb, ssrbt;
2561
2562 m3x3_skew_symetric( ssrat, wa );
2563 m3x3_skew_symetric( ssrbt, wb );
2564 m3x3_transpose( ssrat, ssra );
2565 m3x3_transpose( ssrbt, ssrb );
2566
2567 v3f b, b_wa, b_wb, b_a, b_b;
2568 m3x3_mulv( ssra, rba->w, b_wa );
2569 m3x3_mulv( ssrb, rbb->w, b_wb );
2570 v3_add( rba->v, b_wa, b );
2571 v3_sub( b, rbb->v, b );
2572 v3_sub( b, b_wb, b );
2573 v3_muls( b, -1.0f, b );
2574
2575 m3x3f invMa, invMb;
2576 m3x3_diagonal( invMa, rba->inv_mass );
2577 m3x3_diagonal( invMb, rbb->inv_mass );
2578
2579 m3x3f ia, ib;
2580 m3x3_mul( ssra, rba->iIw, ia );
2581 m3x3_mul( ia, ssrat, ia );
2582 m3x3_mul( ssrb, rbb->iIw, ib );
2583 m3x3_mul( ib, ssrbt, ib );
2584
2585 m3x3f cma, cmb;
2586 m3x3_add( invMa, ia, cma );
2587 m3x3_add( invMb, ib, cmb );
2588
2589 m3x3f A;
2590 m3x3_add( cma, cmb, A );
2591
2592 /* Solve Ax = b ( A^-1*b = x ) */
2593 v3f impulse;
2594 m3x3f invA;
2595 m3x3_inv( A, invA );
2596 m3x3_mulv( invA, b, impulse );
2597
2598 v3f delta_va, delta_wa, delta_vb, delta_wb;
2599 m3x3f iwa, iwb;
2600 m3x3_mul( rba->iIw, ssrat, iwa );
2601 m3x3_mul( rbb->iIw, ssrbt, iwb );
2602
2603 m3x3_mulv( invMa, impulse, delta_va );
2604 m3x3_mulv( invMb, impulse, delta_vb );
2605 m3x3_mulv( iwa, impulse, delta_wa );
2606 m3x3_mulv( iwb, impulse, delta_wb );
2607
2608 v3_add( rba->v, delta_va, rba->v );
2609 v3_add( rba->w, delta_wa, rba->w );
2610 v3_sub( rbb->v, delta_vb, rbb->v );
2611 v3_sub( rbb->w, delta_wb, rbb->w );
2612 }
2613 }
2614
2615 VG_STATIC void rb_solve_swingtwist_constraints( rb_constr_swingtwist *buf,
2616 int len )
2617 {
2618 float size = 0.12f;
2619
2620 for( int i=0; i<len; i++ )
2621 {
2622 rb_constr_swingtwist *st = &buf[ i ];
2623
2624 if( !st->axis_violation )
2625 continue;
2626
2627 float rv = v3_dot( st->axis, st->rbb->w ) -
2628 v3_dot( st->axis, st->rba->w );
2629
2630 if( rv * (float)st->axis_violation > 0.0f )
2631 continue;
2632
2633 v3f impulse, wa, wb;
2634 v3_muls( st->axis, rv*st->axis_mass, impulse );
2635 m3x3_mulv( st->rba->iIw, impulse, wa );
2636 v3_add( st->rba->w, wa, st->rba->w );
2637
2638 v3_muls( impulse, -1.0f, impulse );
2639 m3x3_mulv( st->rbb->iIw, impulse, wb );
2640 v3_add( st->rbb->w, wb, st->rbb->w );
2641
2642 float rv2 = v3_dot( st->axis, st->rbb->w ) -
2643 v3_dot( st->axis, st->rba->w );
2644 }
2645
2646 for( int i=0; i<len; i++ )
2647 {
2648 rb_constr_swingtwist *st = &buf[ i ];
2649
2650 if( !st->tangent_violation )
2651 continue;
2652
2653 float rv = v3_dot( st->tangent_axis, st->rbb->w ) -
2654 v3_dot( st->tangent_axis, st->rba->w );
2655
2656 if( rv > 0.0f )
2657 continue;
2658
2659 v3f impulse, wa, wb;
2660 v3_muls( st->tangent_axis, rv*st->tangent_mass, impulse );
2661 m3x3_mulv( st->rba->iIw, impulse, wa );
2662 v3_add( st->rba->w, wa, st->rba->w );
2663
2664 v3_muls( impulse, -1.0f, impulse );
2665 m3x3_mulv( st->rbb->iIw, impulse, wb );
2666 v3_add( st->rbb->w, wb, st->rbb->w );
2667
2668 float rv2 = v3_dot( st->tangent_axis, st->rbb->w ) -
2669 v3_dot( st->tangent_axis, st->rba->w );
2670 }
2671 }
2672
2673 VG_STATIC void rb_solve_constr_angle( rigidbody *rba, rigidbody *rbb,
2674 v3f ra, v3f rb )
2675 {
2676 m3x3f ssra, ssrb, ssrat, ssrbt;
2677 m3x3f cma, cmb;
2678
2679 m3x3_skew_symetric( ssrat, ra );
2680 m3x3_skew_symetric( ssrbt, rb );
2681 m3x3_transpose( ssrat, ssra );
2682 m3x3_transpose( ssrbt, ssrb );
2683
2684 m3x3_mul( ssra, rba->iIw, cma );
2685 m3x3_mul( cma, ssrat, cma );
2686 m3x3_mul( ssrb, rbb->iIw, cmb );
2687 m3x3_mul( cmb, ssrbt, cmb );
2688
2689 m3x3f A, invA;
2690 m3x3_add( cma, cmb, A );
2691 m3x3_inv( A, invA );
2692
2693 v3f b_wa, b_wb, b;
2694 m3x3_mulv( ssra, rba->w, b_wa );
2695 m3x3_mulv( ssrb, rbb->w, b_wb );
2696 v3_add( b_wa, b_wb, b );
2697 v3_negate( b, b );
2698
2699 v3f impulse;
2700 m3x3_mulv( invA, b, impulse );
2701
2702 v3f delta_wa, delta_wb;
2703 m3x3f iwa, iwb;
2704 m3x3_mul( rba->iIw, ssrat, iwa );
2705 m3x3_mul( rbb->iIw, ssrbt, iwb );
2706 m3x3_mulv( iwa, impulse, delta_wa );
2707 m3x3_mulv( iwb, impulse, delta_wb );
2708 v3_add( rba->w, delta_wa, rba->w );
2709 v3_sub( rbb->w, delta_wb, rbb->w );
2710 }
2711
2712 /*
2713 * Correct position constraint drift errors
2714 * [ 0.0 <= amt <= 1.0 ]: the correction amount
2715 */
2716 VG_STATIC void rb_correct_position_constraints( rb_constr_pos *buf, int len,
2717 float amt )
2718 {
2719 for( int i=0; i<len; i++ )
2720 {
2721 rb_constr_pos *constr = &buf[i];
2722 rigidbody *rba = constr->rba, *rbb = constr->rbb;
2723
2724 v3f p0, p1, d;
2725 m3x3_mulv( rba->to_world, constr->lca, p0 );
2726 m3x3_mulv( rbb->to_world, constr->lcb, p1 );
2727 v3_add( rba->co, p0, p0 );
2728 v3_add( rbb->co, p1, p1 );
2729 v3_sub( p1, p0, d );
2730
2731 v3_muladds( rbb->co, d, -1.0f * amt, rbb->co );
2732 rb_update_transform( rbb );
2733 }
2734 }
2735
2736 VG_STATIC void rb_correct_swingtwist_constraints( rb_constr_swingtwist *buf,
2737 int len, float amt )
2738 {
2739 for( int i=0; i<len; i++ )
2740 {
2741 rb_constr_swingtwist *st = &buf[i];
2742
2743 if( !st->tangent_violation )
2744 continue;
2745
2746 v3f va;
2747 m3x3_mulv( st->rbb->to_world, st->coneva, va );
2748
2749 float angle = v3_dot( va, st->tangent_target );
2750
2751 if( fabsf(angle) < 0.9999f )
2752 {
2753 v3f axis;
2754 v3_cross( va, st->tangent_target, axis );
2755
2756 v4f correction;
2757 q_axis_angle( correction, axis, acosf(angle) * amt );
2758 q_mul( correction, st->rbb->q, st->rbb->q );
2759 rb_update_transform( st->rbb );
2760 }
2761 }
2762
2763 for( int i=0; i<len; i++ )
2764 {
2765 rb_constr_swingtwist *st = &buf[i];
2766
2767 if( !st->axis_violation )
2768 continue;
2769
2770 v3f vxb;
2771 m3x3_mulv( st->rbb->to_world, st->conevxb, vxb );
2772
2773 float angle = v3_dot( vxb, st->axis_target );
2774
2775 if( fabsf(angle) < 0.9999f )
2776 {
2777 v3f axis;
2778 v3_cross( vxb, st->axis_target, axis );
2779
2780 v4f correction;
2781 q_axis_angle( correction, axis, acosf(angle) * amt );
2782 q_mul( correction, st->rbb->q, st->rbb->q );
2783 rb_update_transform( st->rbb );
2784 }
2785 }
2786 }
2787
2788 VG_STATIC void rb_correct_contact_constraints( rb_ct *buf, int len, float amt )
2789 {
2790 for( int i=0; i<len; i++ )
2791 {
2792 rb_ct *ct = &buf[i];
2793 rigidbody *rba = ct->rba,
2794 *rbb = ct->rbb;
2795
2796 float mass_total = 1.0f / (rba->inv_mass + rbb->inv_mass);
2797
2798 v3_muladds( rba->co, ct->n, -mass_total * rba->inv_mass, rba->co );
2799 v3_muladds( rbb->co, ct->n, mass_total * rbb->inv_mass, rbb->co );
2800 }
2801 }
2802
2803
2804 /*
2805 * Effectors
2806 */
2807
2808 VG_STATIC void rb_effect_simple_bouyency( rigidbody *ra, v4f plane,
2809 float amt, float drag )
2810 {
2811 /* float */
2812 float depth = v3_dot( plane, ra->co ) - plane[3],
2813 lambda = vg_clampf( -depth, 0.0f, 1.0f ) * amt;
2814
2815 v3_muladds( ra->v, plane, lambda * k_rb_delta, ra->v );
2816
2817 if( depth < 0.0f )
2818 v3_muls( ra->v, 1.0f-(drag*k_rb_delta), ra->v );
2819 }
2820
2821 /* apply a spring&dampener force to match ra(worldspace) on rigidbody, to
2822 * rt(worldspace)
2823 */
2824 VG_STATIC void rb_effect_spring_target_vector( rigidbody *rba, v3f ra, v3f rt,
2825 float spring, float dampening,
2826 float timestep )
2827 {
2828 float d = v3_dot( rt, ra );
2829 float a = vg_signf( d ) * acosf( vg_clampf( d, -1.0f, 1.0f ) );
2830
2831 v3f axis;
2832 v3_cross( rt, ra, axis );
2833
2834 float Fs = -a * spring,
2835 Fd = -v3_dot( rba->w, axis ) * dampening;
2836
2837 v3_muladds( rba->w, axis, (Fs+Fd) * timestep, rba->w );
2838 }
2839
2840 /*
2841 * -----------------------------------------------------------------------------
2842 * BVH implementation, this is ONLY for VG_STATIC rigidbodies, its to slow for
2843 * realtime use.
2844 * -----------------------------------------------------------------------------
2845 */
2846
2847 VG_STATIC void rb_bh_expand_bound( void *user, boxf bound, u32 item_index )
2848 {
2849 rigidbody *rb = &((rigidbody *)user)[ item_index ];
2850 box_concat( bound, rb->bbx_world );
2851 }
2852
2853 VG_STATIC float rb_bh_centroid( void *user, u32 item_index, int axis )
2854 {
2855 rigidbody *rb = &((rigidbody *)user)[ item_index ];
2856 return (rb->bbx_world[axis][0] + rb->bbx_world[1][axis]) * 0.5f;
2857 }
2858
2859 VG_STATIC void rb_bh_swap( void *user, u32 ia, u32 ib )
2860 {
2861 rigidbody temp, *rba, *rbb;
2862 rba = &((rigidbody *)user)[ ia ];
2863 rbb = &((rigidbody *)user)[ ib ];
2864
2865 temp = *rba;
2866 *rba = *rbb;
2867 *rbb = temp;
2868 }
2869
2870 VG_STATIC void rb_bh_debug( void *user, u32 item_index )
2871 {
2872 rigidbody *rb = &((rigidbody *)user)[ item_index ];
2873 rb_debug( rb, 0xff00ffff );
2874 }
2875
2876 VG_STATIC bh_system bh_system_rigidbodies =
2877 {
2878 .expand_bound = rb_bh_expand_bound,
2879 .item_centroid = rb_bh_centroid,
2880 .item_swap = rb_bh_swap,
2881 .item_debug = rb_bh_debug,
2882 .cast_ray = NULL
2883 };
2884
2885 #endif /* RIGIDBODY_H */