switch to entity list
[carveJwlIkooP6JGAAIwe30JlM.git] / rigidbody.h
1 /*
2 * Copyright (C) 2021-2023 Mt.ZERO Software, Harry Godden - All Rights Reserved
3 */
4
5 /*
6 * Resources: Box2D - Erin Catto
7 * qu3e - Randy Gaul
8 */
9
10 #include "vg/vg_console.h"
11 #include "bvh.h"
12 #include "scene.h"
13
14 #include <math.h>
15
16 static bh_system bh_system_rigidbodies;
17
18 #ifndef RIGIDBODY_H
19 #define RIGIDBODY_H
20
21 /*
22 * -----------------------------------------------------------------------------
23 * (K)onstants
24 * -----------------------------------------------------------------------------
25 */
26
27 static const float
28 k_rb_rate = (1.0/VG_TIMESTEP_FIXED),
29 k_rb_delta = (1.0/k_rb_rate),
30 k_friction = 0.4f,
31 k_damp_linear = 0.1f, /* scale velocity 1/(1+x) */
32 k_damp_angular = 0.1f, /* scale angular 1/(1+x) */
33 k_penetration_slop = 0.01f,
34 k_inertia_scale = 8.0f,
35 k_phys_baumgarte = 0.2f,
36 k_gravity = 9.6f;
37
38 static float
39 k_limit_bias = 0.02f,
40 k_joint_correction = 0.01f,
41 k_joint_impulse = 1.0f,
42 k_joint_bias = 0.08f; /* positional joints */
43
44 static void rb_register_cvar(void){
45 VG_VAR_F32( k_limit_bias, flags=VG_VAR_CHEAT );
46 VG_VAR_F32( k_joint_bias, flags=VG_VAR_CHEAT );
47 VG_VAR_F32( k_joint_correction, flags=VG_VAR_CHEAT );
48 VG_VAR_F32( k_joint_impulse, flags=VG_VAR_CHEAT );
49 }
50
51 /*
52 * -----------------------------------------------------------------------------
53 * structure definitions
54 * -----------------------------------------------------------------------------
55 */
56
57 typedef struct rigidbody rigidbody;
58 typedef struct rb_object rb_object;
59 typedef struct contact rb_ct;
60 typedef struct rb_sphere rb_sphere;
61 typedef struct rb_capsule rb_capsule;
62 typedef struct rb_scene rb_scene;
63
64 struct rb_sphere{
65 float radius;
66 };
67
68 struct rb_capsule{
69 float height, radius;
70 };
71
72 struct rb_scene{
73 bh_tree *bh_scene;
74 };
75
76 struct rigidbody{
77 v3f co, v, w;
78 v4f q;
79
80 boxf bbx, bbx_world;
81 float inv_mass;
82
83 /* inertia model and inverse world tensor */
84 v3f I;
85 m3x3f iI, iIw;
86 m4x3f to_world, to_local;
87 };
88
89 /* simple objects */
90 struct rb_object{
91 rigidbody rb;
92 enum rb_shape{
93 k_rb_shape_box = 0,
94 k_rb_shape_sphere = 1,
95 k_rb_shape_capsule = 2,
96 k_rb_shape_scene = 3
97 }
98 type;
99
100 union{
101 struct rb_sphere sphere;
102 struct rb_capsule capsule;
103 struct rb_scene scene;
104 }
105 inf;
106 };
107
108 VG_STATIC struct contact{
109 rigidbody *rba, *rbb;
110 v3f co, n;
111 v3f t[2];
112 float p, bias, norm_impulse, tangent_impulse[2],
113 normal_mass, tangent_mass[2];
114
115 u32 element_id;
116
117 enum contact_type type;
118 }
119 rb_contact_buffer[256];
120 VG_STATIC int rb_contact_count = 0;
121
122 typedef struct rb_constr_pos rb_constr_pos;
123 typedef struct rb_constr_swingtwist rb_constr_swingtwist;
124
125 struct rb_constr_pos{
126 rigidbody *rba, *rbb;
127 v3f lca, lcb;
128 };
129
130 struct rb_constr_swingtwist{
131 rigidbody *rba, *rbb;
132
133 v4f conevx, conevy; /* relative to rba */
134 v3f view_offset, /* relative to rba */
135 coneva, conevxb;/* relative to rbb */
136
137 int tangent_violation, axis_violation;
138 v3f axis, tangent_axis, tangent_target, axis_target;
139
140 float conet;
141 float tangent_mass, axis_mass;
142 };
143
144 /*
145 * -----------------------------------------------------------------------------
146 * Debugging
147 * -----------------------------------------------------------------------------
148 */
149
150 VG_STATIC void rb_debug_contact( rb_ct *ct ){
151 v3f p1;
152 v3_muladds( ct->co, ct->n, 0.05f, p1 );
153
154 if( ct->type == k_contact_type_default ){
155 vg_line_point( ct->co, 0.0125f, 0xff0000ff );
156 vg_line( ct->co, p1, 0xffffffff );
157 }
158 else if( ct->type == k_contact_type_edge ){
159 vg_line_point( ct->co, 0.0125f, 0xff00ffc0 );
160 vg_line( ct->co, p1, 0xffffffff );
161 }
162 }
163
164
165 VG_STATIC void rb_object_debug( rb_object *obj, u32 colour ){
166 if( obj->type == k_rb_shape_box ){
167 v3f *box = obj->rb.bbx;
168 vg_line_boxf_transformed( obj->rb.to_world, obj->rb.bbx, colour );
169 }
170 else if( obj->type == k_rb_shape_sphere ){
171 vg_line_sphere( obj->rb.to_world, obj->inf.sphere.radius, colour );
172 }
173 else if( obj->type == k_rb_shape_capsule ){
174 m4x3f m0, m1;
175 float h = obj->inf.capsule.height,
176 r = obj->inf.capsule.radius;
177
178 vg_line_capsule( obj->rb.to_world, r, h, colour );
179 }
180 else if( obj->type == k_rb_shape_scene ){
181 vg_line_boxf( obj->rb.bbx, colour );
182 }
183 }
184
185 /*
186 * -----------------------------------------------------------------------------
187 * Integration
188 * -----------------------------------------------------------------------------
189 */
190
191 /*
192 * Update world space bounding box based on local one
193 */
194 VG_STATIC void rb_update_bounds( rigidbody *rb ){
195 box_init_inf( rb->bbx_world );
196 m4x3_expand_aabb_aabb( rb->to_world, rb->bbx_world, rb->bbx );
197 }
198
199 /*
200 * Commit transform to rigidbody. Updates matrices
201 */
202 VG_STATIC void rb_update_transform( rigidbody *rb )
203 {
204 q_normalize( rb->q );
205 q_m3x3( rb->q, rb->to_world );
206 v3_copy( rb->co, rb->to_world[3] );
207
208 m4x3_invert_affine( rb->to_world, rb->to_local );
209 m3x3_mul( rb->iI, rb->to_local, rb->iIw );
210 m3x3_mul( rb->to_world, rb->iIw, rb->iIw );
211
212 rb_update_bounds( rb );
213 }
214
215 /*
216 * Extrapolate rigidbody into a transform based on vg accumulator.
217 * Useful for rendering
218 */
219 VG_STATIC void rb_extrapolate( rigidbody *rb, v3f co, v4f q )
220 {
221 float substep = vg.time_fixed_extrapolate;
222 v3_muladds( rb->co, rb->v, k_rb_delta*substep, co );
223
224 if( v3_length2( rb->w ) > 0.0f ){
225 v4f rotation;
226 v3f axis;
227 v3_copy( rb->w, axis );
228
229 float mag = v3_length( axis );
230 v3_divs( axis, mag, axis );
231 q_axis_angle( rotation, axis, mag*k_rb_delta*substep );
232 q_mul( rotation, rb->q, q );
233 q_normalize( q );
234 }
235 else{
236 v4_copy( rb->q, q );
237 }
238 }
239
240 /*
241 * Initialize rigidbody and calculate masses, inertia
242 */
243 VG_STATIC void rb_init_object( rb_object *obj ){
244 float volume = 1.0f;
245 int inert = 0;
246
247 if( obj->type == k_rb_shape_box ){
248 v3f dims;
249 v3_sub( obj->rb.bbx[1], obj->rb.bbx[0], dims );
250 volume = dims[0]*dims[1]*dims[2];
251 }
252 else if( obj->type == k_rb_shape_sphere ){
253 volume = vg_sphere_volume( obj->inf.sphere.radius );
254 v3_fill( obj->rb.bbx[0], -obj->inf.sphere.radius );
255 v3_fill( obj->rb.bbx[1], obj->inf.sphere.radius );
256 }
257 else if( obj->type == k_rb_shape_capsule ){
258 float r = obj->inf.capsule.radius,
259 h = obj->inf.capsule.height;
260 volume = vg_sphere_volume( r ) + VG_PIf * r*r * (h - r*2.0f);
261
262 v3_fill( obj->rb.bbx[0], -r );
263 v3_fill( obj->rb.bbx[1], r );
264 obj->rb.bbx[0][1] = -h;
265 obj->rb.bbx[1][1] = h;
266 }
267 else if( obj->type == k_rb_shape_scene ){
268 inert = 1;
269 box_copy( obj->inf.scene.bh_scene->nodes[0].bbx, obj->rb.bbx );
270 }
271
272 if( inert ){
273 obj->rb.inv_mass = 0.0f;
274 v3_zero( obj->rb.I );
275 m3x3_zero( obj->rb.iI );
276 }
277 else{
278 float mass = 2.0f*volume;
279 obj->rb.inv_mass = 1.0f/mass;
280
281 v3f extent;
282 v3_sub( obj->rb.bbx[1], obj->rb.bbx[0], extent );
283 v3_muls( extent, 0.5f, extent );
284
285 /* local intertia tensor */
286 float scale = k_inertia_scale;
287 float ex2 = scale*extent[0]*extent[0],
288 ey2 = scale*extent[1]*extent[1],
289 ez2 = scale*extent[2]*extent[2];
290
291 obj->rb.I[0] = ((1.0f/12.0f) * mass * (ey2+ez2));
292 obj->rb.I[1] = ((1.0f/12.0f) * mass * (ex2+ez2));
293 obj->rb.I[2] = ((1.0f/12.0f) * mass * (ex2+ey2));
294
295 m3x3_identity( obj->rb.iI );
296 obj->rb.iI[0][0] = obj->rb.I[0];
297 obj->rb.iI[1][1] = obj->rb.I[1];
298 obj->rb.iI[2][2] = obj->rb.I[2];
299 m3x3_inv( obj->rb.iI, obj->rb.iI );
300 }
301
302 rb_update_transform( &obj->rb );
303 }
304
305 VG_STATIC void rb_iter( rigidbody *rb ){
306 if( !vg_validf( rb->v[0] ) ||
307 !vg_validf( rb->v[1] ) ||
308 !vg_validf( rb->v[2] ) )
309 {
310 vg_fatal_error( "NaN velocity" );
311 }
312
313 v3f gravity = { 0.0f, -9.8f, 0.0f };
314 v3_muladds( rb->v, gravity, k_rb_delta, rb->v );
315
316 /* intergrate velocity */
317 v3_muladds( rb->co, rb->v, k_rb_delta, rb->co );
318 v3_lerp( rb->w, (v3f){0.0f,0.0f,0.0f}, 0.0025f, rb->w );
319
320 /* inegrate inertia */
321 if( v3_length2( rb->w ) > 0.0f )
322 {
323 v4f rotation;
324 v3f axis;
325 v3_copy( rb->w, axis );
326
327 float mag = v3_length( axis );
328 v3_divs( axis, mag, axis );
329 q_axis_angle( rotation, axis, mag*k_rb_delta );
330 q_mul( rotation, rb->q, rb->q );
331 }
332
333 /* damping */
334 v3_muls( rb->v, 1.0f/(1.0f+k_rb_delta*k_damp_linear), rb->v );
335 v3_muls( rb->w, 1.0f/(1.0f+k_rb_delta*k_damp_angular), rb->w );
336 }
337
338
339 /*
340 * -----------------------------------------------------------------------------
341 * Boolean shape overlap functions
342 * -----------------------------------------------------------------------------
343 */
344
345 /*
346 * Project AABB, and triangle interval onto axis to check if they overlap
347 */
348 VG_STATIC int rb_box_triangle_interval( v3f extent, v3f axis, v3f tri[3] ){
349 float
350
351 r = extent[0] * fabsf(axis[0]) +
352 extent[1] * fabsf(axis[1]) +
353 extent[2] * fabsf(axis[2]),
354
355 p0 = v3_dot( axis, tri[0] ),
356 p1 = v3_dot( axis, tri[1] ),
357 p2 = v3_dot( axis, tri[2] ),
358
359 e = vg_maxf(-vg_maxf(p0,vg_maxf(p1,p2)), vg_minf(p0,vg_minf(p1,p2)));
360
361 if( e > r ) return 0;
362 else return 1;
363 }
364
365 /*
366 * Seperating axis test box vs triangle
367 */
368 VG_STATIC int rb_box_triangle_sat( v3f extent, v3f center,
369 m4x3f to_local, v3f tri_src[3] ){
370 v3f tri[3];
371
372 for( int i=0; i<3; i++ ){
373 m4x3_mulv( to_local, tri_src[i], tri[i] );
374 v3_sub( tri[i], center, tri[i] );
375 }
376
377 v3f f0,f1,f2,n;
378 v3_sub( tri[1], tri[0], f0 );
379 v3_sub( tri[2], tri[1], f1 );
380 v3_sub( tri[0], tri[2], f2 );
381
382
383 v3f axis[9];
384 v3_cross( (v3f){1.0f,0.0f,0.0f}, f0, axis[0] );
385 v3_cross( (v3f){1.0f,0.0f,0.0f}, f1, axis[1] );
386 v3_cross( (v3f){1.0f,0.0f,0.0f}, f2, axis[2] );
387 v3_cross( (v3f){0.0f,1.0f,0.0f}, f0, axis[3] );
388 v3_cross( (v3f){0.0f,1.0f,0.0f}, f1, axis[4] );
389 v3_cross( (v3f){0.0f,1.0f,0.0f}, f2, axis[5] );
390 v3_cross( (v3f){0.0f,0.0f,1.0f}, f0, axis[6] );
391 v3_cross( (v3f){0.0f,0.0f,1.0f}, f1, axis[7] );
392 v3_cross( (v3f){0.0f,0.0f,1.0f}, f2, axis[8] );
393
394 for( int i=0; i<9; i++ )
395 if(!rb_box_triangle_interval( extent, axis[i], tri )) return 0;
396
397 /* u0, u1, u2 */
398 if(!rb_box_triangle_interval( extent, (v3f){1.0f,0.0f,0.0f}, tri )) return 0;
399 if(!rb_box_triangle_interval( extent, (v3f){0.0f,1.0f,0.0f}, tri )) return 0;
400 if(!rb_box_triangle_interval( extent, (v3f){0.0f,0.0f,1.0f}, tri )) return 0;
401
402 /* normal */
403 v3_cross( f0, f1, n );
404 if(!rb_box_triangle_interval( extent, n, tri )) return 0;
405
406 return 1;
407 }
408
409 /*
410 * -----------------------------------------------------------------------------
411 * Manifold
412 * -----------------------------------------------------------------------------
413 */
414
415 VG_STATIC int rb_manifold_apply_filtered( rb_ct *man, int len ){
416 int k = 0;
417
418 for( int i=0; i<len; i++ ){
419 rb_ct *ct = &man[i];
420
421 if( ct->type == k_contact_type_disabled )
422 continue;
423
424 man[k ++] = man[i];
425 }
426
427 return k;
428 }
429
430 /*
431 * Merge two contacts if they are within radius(r) of eachother
432 */
433 VG_STATIC void rb_manifold_contact_weld( rb_ct *ci, rb_ct *cj, float r ){
434 if( v3_dist2( ci->co, cj->co ) < r*r ){
435 cj->type = k_contact_type_disabled;
436 ci->p = (ci->p + cj->p) * 0.5f;
437
438 v3_add( ci->co, cj->co, ci->co );
439 v3_muls( ci->co, 0.5f, ci->co );
440
441 v3f delta;
442 v3_sub( ci->rba->co, ci->co, delta );
443
444 float c0 = v3_dot( ci->n, delta ),
445 c1 = v3_dot( cj->n, delta );
446
447 if( c0 < 0.0f || c1 < 0.0f ){
448 /* error */
449 ci->type = k_contact_type_disabled;
450 }
451 else{
452 v3f n;
453 v3_muls( ci->n, c0, n );
454 v3_muladds( n, cj->n, c1, n );
455 v3_normalize( n );
456 v3_copy( n, ci->n );
457 }
458 }
459 }
460
461 /*
462 *
463 */
464 VG_STATIC void rb_manifold_filter_joint_edges( rb_ct *man, int len, float r ){
465 for( int i=0; i<len-1; i++ ){
466 rb_ct *ci = &man[i];
467 if( ci->type != k_contact_type_edge )
468 continue;
469
470 for( int j=i+1; j<len; j++ ){
471 rb_ct *cj = &man[j];
472 if( cj->type != k_contact_type_edge )
473 continue;
474
475 rb_manifold_contact_weld( ci, cj, r );
476 }
477 }
478 }
479
480 /*
481 * Resolve overlapping pairs
482 */
483 VG_STATIC void rb_manifold_filter_pairs( rb_ct *man, int len, float r ){
484 for( int i=0; i<len-1; i++ ){
485 rb_ct *ci = &man[i];
486 int similar = 0;
487
488 if( ci->type == k_contact_type_disabled ) continue;
489
490 for( int j=i+1; j<len; j++ ){
491 rb_ct *cj = &man[j];
492
493 if( cj->type == k_contact_type_disabled ) continue;
494
495 if( v3_dist2( ci->co, cj->co ) < r*r ){
496 cj->type = k_contact_type_disabled;
497 v3_add( cj->n, ci->n, ci->n );
498 ci->p += cj->p;
499 similar ++;
500 }
501 }
502
503 if( similar ){
504 float n = 1.0f/((float)similar+1.0f);
505 v3_muls( ci->n, n, ci->n );
506 ci->p *= n;
507
508 if( v3_length2(ci->n) < 0.1f*0.1f )
509 ci->type = k_contact_type_disabled;
510 else
511 v3_normalize( ci->n );
512 }
513 }
514 }
515
516 /*
517 * Remove contacts that are facing away from A
518 */
519 VG_STATIC void rb_manifold_filter_backface( rb_ct *man, int len ){
520 for( int i=0; i<len; i++ ){
521 rb_ct *ct = &man[i];
522 if( ct->type == k_contact_type_disabled )
523 continue;
524
525 v3f delta;
526 v3_sub( ct->co, ct->rba->co, delta );
527
528 if( v3_dot( delta, ct->n ) > -0.001f )
529 ct->type = k_contact_type_disabled;
530 }
531 }
532
533 /*
534 * Filter out duplicate coplanar results. Good for spheres.
535 */
536 VG_STATIC void rb_manifold_filter_coplanar( rb_ct *man, int len, float w ){
537 for( int i=0; i<len; i++ ){
538 rb_ct *ci = &man[i];
539 if( ci->type == k_contact_type_disabled ||
540 ci->type == k_contact_type_edge )
541 continue;
542
543 float d1 = v3_dot( ci->co, ci->n );
544
545 for( int j=0; j<len; j++ ){
546 if( j == i )
547 continue;
548
549 rb_ct *cj = &man[j];
550 if( cj->type == k_contact_type_disabled )
551 continue;
552
553 float d2 = v3_dot( cj->co, ci->n ),
554 d = d2-d1;
555
556 if( fabsf( d ) <= w ){
557 cj->type = k_contact_type_disabled;
558 }
559 }
560 }
561 }
562
563 /*
564 * -----------------------------------------------------------------------------
565 * Collision matrix
566 * -----------------------------------------------------------------------------
567 */
568
569 /*
570 * Contact generators
571 *
572 * These do not automatically allocate contacts, an appropriately sized
573 * buffer must be supplied. The function returns the size of the manifold
574 * which was generated.
575 *
576 * The values set on the contacts are: n, co, p, rba, rbb
577 */
578
579 /*
580 * By collecting the minimum(time) and maximum(time) pairs of points, we
581 * build a reduced and stable exact manifold.
582 *
583 * tx: time at point
584 * rx: minimum distance of these points
585 * dx: the delta between the two points
586 *
587 * pairs will only ammend these if they are creating a collision
588 */
589 typedef struct capsule_manifold capsule_manifold;
590 struct capsule_manifold{
591 float t0, t1;
592 float r0, r1;
593 v3f d0, d1;
594 };
595
596 /*
597 * Expand a line manifold with a new pair. t value is the time along segment
598 * on the oriented object which created this pair.
599 */
600 VG_STATIC void rb_capsule_manifold( v3f pa, v3f pb, float t, float r,
601 capsule_manifold *manifold ){
602 v3f delta;
603 v3_sub( pa, pb, delta );
604
605 if( v3_length2(delta) < r*r ){
606 if( t < manifold->t0 ){
607 v3_copy( delta, manifold->d0 );
608 manifold->t0 = t;
609 manifold->r0 = r;
610 }
611
612 if( t > manifold->t1 ){
613 v3_copy( delta, manifold->d1 );
614 manifold->t1 = t;
615 manifold->r1 = r;
616 }
617 }
618 }
619
620 VG_STATIC void rb_capsule_manifold_init( capsule_manifold *manifold ){
621 manifold->t0 = INFINITY;
622 manifold->t1 = -INFINITY;
623 }
624
625 VG_STATIC int rb_capsule__manifold_done( m4x3f mtx, rb_capsule *c,
626 capsule_manifold *manifold,
627 rb_ct *buf ){
628 v3f p0, p1;
629 v3_muladds( mtx[3], mtx[1], -c->height*0.5f+c->radius, p0 );
630 v3_muladds( mtx[3], mtx[1], c->height*0.5f-c->radius, p1 );
631
632 int count = 0;
633 if( manifold->t0 <= 1.0f ){
634 rb_ct *ct = buf;
635
636 v3f pa;
637 v3_muls( p0, 1.0f-manifold->t0, pa );
638 v3_muladds( pa, p1, manifold->t0, pa );
639
640 float d = v3_length( manifold->d0 );
641 v3_muls( manifold->d0, 1.0f/d, ct->n );
642 v3_muladds( pa, ct->n, -c->radius, ct->co );
643
644 ct->p = manifold->r0 - d;
645 ct->type = k_contact_type_default;
646 count ++;
647 }
648
649 if( (manifold->t1 >= 0.0f) && (manifold->t0 != manifold->t1) ){
650 rb_ct *ct = buf+count;
651
652 v3f pa;
653 v3_muls( p0, 1.0f-manifold->t1, pa );
654 v3_muladds( pa, p1, manifold->t1, pa );
655
656 float d = v3_length( manifold->d1 );
657 v3_muls( manifold->d1, 1.0f/d, ct->n );
658 v3_muladds( pa, ct->n, -c->radius, ct->co );
659
660 ct->p = manifold->r1 - d;
661 ct->type = k_contact_type_default;
662
663 count ++;
664 }
665
666 /*
667 * Debugging
668 */
669
670 if( count == 2 )
671 vg_line( buf[0].co, buf[1].co, 0xff0000ff );
672
673 return count;
674 }
675
676 VG_STATIC int rb_capsule_sphere( rb_object *obja, rb_object *objb, rb_ct *buf ){
677 rigidbody *rba = &obja->rb, *rbb = &objb->rb;
678 float h = obja->inf.capsule.height,
679 ra = obja->inf.capsule.radius,
680 rb = objb->inf.sphere.radius;
681
682 v3f p0, p1;
683 v3_muladds( rba->co, rba->to_world[1], -h*0.5f+ra, p0 );
684 v3_muladds( rba->co, rba->to_world[1], h*0.5f-ra, p1 );
685
686 v3f c, delta;
687 closest_point_segment( p0, p1, rbb->co, c );
688 v3_sub( c, rbb->co, delta );
689
690 float d2 = v3_length2(delta),
691 r = ra + rb;
692
693 if( d2 < r*r ){
694 float d = sqrtf(d2);
695
696 rb_ct *ct = buf;
697 v3_muls( delta, 1.0f/d, ct->n );
698 ct->p = r-d;
699
700 v3f p0, p1;
701 v3_muladds( c, ct->n, -ra, p0 );
702 v3_muladds( rbb->co, ct->n, rb, p1 );
703 v3_add( p0, p1, ct->co );
704 v3_muls( ct->co, 0.5f, ct->co );
705
706 ct->rba = rba;
707 ct->rbb = rbb;
708 ct->type = k_contact_type_default;
709
710 return 1;
711 }
712
713 return 0;
714 }
715
716 VG_STATIC int rb_capsule__capsule( m4x3f mtxA, rb_capsule *ca,
717 m4x3f mtxB, rb_capsule *cb, rb_ct *buf ){
718 float ha = ca->height,
719 hb = cb->height,
720 ra = ca->radius,
721 rb = cb->radius,
722 r = ra+rb;
723
724 v3f p0, p1, p2, p3;
725 v3_muladds( mtxA[3], mtxA[1], -ha*0.5f+ra, p0 );
726 v3_muladds( mtxA[3], mtxA[1], ha*0.5f-ra, p1 );
727 v3_muladds( mtxB[3], mtxB[1], -hb*0.5f+rb, p2 );
728 v3_muladds( mtxB[3], mtxB[1], hb*0.5f-rb, p3 );
729
730 capsule_manifold manifold;
731 rb_capsule_manifold_init( &manifold );
732
733 v3f pa, pb;
734 float ta, tb;
735 closest_segment_segment( p0, p1, p2, p3, &ta, &tb, pa, pb );
736 rb_capsule_manifold( pa, pb, ta, r, &manifold );
737
738 ta = closest_point_segment( p0, p1, p2, pa );
739 tb = closest_point_segment( p0, p1, p3, pb );
740 rb_capsule_manifold( pa, p2, ta, r, &manifold );
741 rb_capsule_manifold( pb, p3, tb, r, &manifold );
742
743 closest_point_segment( p2, p3, p0, pa );
744 closest_point_segment( p2, p3, p1, pb );
745 rb_capsule_manifold( p0, pa, 0.0f, r, &manifold );
746 rb_capsule_manifold( p1, pb, 1.0f, r, &manifold );
747
748 return rb_capsule__manifold_done( mtxA, ca, &manifold, buf );
749 }
750
751 VG_STATIC int rb_sphere_box( rb_object *obja, rb_object *objb, rb_ct *buf ){
752 v3f co, delta;
753 rigidbody *rba = &obja->rb, *rbb = &objb->rb;
754
755 closest_point_obb( rba->co, rbb->bbx, rbb->to_world, rbb->to_local, co );
756 v3_sub( rba->co, co, delta );
757
758 float d2 = v3_length2(delta),
759 r = obja->inf.sphere.radius;
760
761 if( d2 <= r*r ){
762 float d;
763
764 rb_ct *ct = buf;
765 if( d2 <= 0.0001f ){
766 v3_sub( rba->co, rbb->co, delta );
767
768 /*
769 * some extra testing is required to find the best axis to push the
770 * object back outside the box. Since there isnt a clear seperating
771 * vector already, especially on really high aspect boxes.
772 */
773 float lx = v3_dot( rbb->to_world[0], delta ),
774 ly = v3_dot( rbb->to_world[1], delta ),
775 lz = v3_dot( rbb->to_world[2], delta ),
776 px = rbb->bbx[1][0] - fabsf(lx),
777 py = rbb->bbx[1][1] - fabsf(ly),
778 pz = rbb->bbx[1][2] - fabsf(lz);
779
780 if( px < py && px < pz )
781 v3_muls( rbb->to_world[0], vg_signf(lx), ct->n );
782 else if( py < pz )
783 v3_muls( rbb->to_world[1], vg_signf(ly), ct->n );
784 else
785 v3_muls( rbb->to_world[2], vg_signf(lz), ct->n );
786
787 v3_muladds( rba->co, ct->n, -r, ct->co );
788 ct->p = r;
789 }
790 else{
791 d = sqrtf(d2);
792 v3_muls( delta, 1.0f/d, ct->n );
793 ct->p = r-d;
794 v3_copy( co, ct->co );
795 }
796
797 ct->rba = rba;
798 ct->rbb = rbb;
799 ct->type = k_contact_type_default;
800 return 1;
801 }
802
803 return 0;
804 }
805
806 VG_STATIC int rb_sphere_sphere( rb_object *obja, rb_object *objb, rb_ct *buf ){
807 rigidbody *rba = &obja->rb, *rbb = &objb->rb;
808 v3f delta;
809 v3_sub( rba->co, rbb->co, delta );
810
811 float d2 = v3_length2(delta),
812 r = obja->inf.sphere.radius + objb->inf.sphere.radius;
813
814 if( d2 < r*r ){
815 float d = sqrtf(d2);
816
817 rb_ct *ct = buf;
818 v3_muls( delta, 1.0f/d, ct->n );
819
820 v3f p0, p1;
821 v3_muladds( rba->co, ct->n,-obja->inf.sphere.radius, p0 );
822 v3_muladds( rbb->co, ct->n, objb->inf.sphere.radius, p1 );
823 v3_add( p0, p1, ct->co );
824 v3_muls( ct->co, 0.5f, ct->co );
825 ct->type = k_contact_type_default;
826 ct->p = r-d;
827 ct->rba = rba;
828 ct->rbb = rbb;
829 return 1;
830 }
831
832 return 0;
833 }
834
835 VG_STATIC int rb_sphere__triangle( m4x3f mtxA, rb_sphere *b,
836 v3f tri[3], rb_ct *buf ){
837 v3f delta, co;
838 enum contact_type type = closest_on_triangle_1( mtxA[3], tri, co );
839
840 v3_sub( mtxA[3], co, delta );
841
842 float d2 = v3_length2( delta ),
843 r = b->radius;
844
845 if( d2 <= r*r ){
846 rb_ct *ct = buf;
847
848 v3f ab, ac, tn;
849 v3_sub( tri[2], tri[0], ab );
850 v3_sub( tri[1], tri[0], ac );
851 v3_cross( ac, ab, tn );
852 v3_copy( tn, ct->n );
853
854 if( v3_length2( ct->n ) <= 0.00001f ){
855 vg_error( "Zero area triangle!\n" );
856 return 0;
857 }
858
859 v3_normalize( ct->n );
860
861 float d = sqrtf(d2);
862
863 v3_copy( co, ct->co );
864 ct->type = type;
865 ct->p = r-d;
866 return 1;
867 }
868
869 return 0;
870 }
871
872 VG_STATIC int rb_sphere__scene( m4x3f mtxA, rb_sphere *b,
873 m4x3f mtxB, rb_scene *s, rb_ct *buf ){
874 scene_context *sc = s->bh_scene->user;
875
876 int count = 0;
877
878 float r = b->radius + 0.1f;
879 boxf box;
880 v3_sub( mtxA[3], (v3f){ r,r,r }, box[0] );
881 v3_add( mtxA[3], (v3f){ r,r,r }, box[1] );
882
883 bh_iter it;
884 i32 idx;
885 bh_iter_init_box( 0, &it, box );
886
887 while( bh_next( s->bh_scene, &it, &idx ) ){
888 u32 *ptri = &sc->arrindices[ idx*3 ];
889 v3f tri[3];
890
891 for( int j=0; j<3; j++ )
892 v3_copy( sc->arrvertices[ptri[j]].co, tri[j] );
893
894 buf[ count ].element_id = ptri[0];
895
896 vg_line( tri[0],tri[1],0x70ff6000 );
897 vg_line( tri[1],tri[2],0x70ff6000 );
898 vg_line( tri[2],tri[0],0x70ff6000 );
899
900 int contact = rb_sphere__triangle( mtxA, b, tri, &buf[count] );
901 count += contact;
902
903 if( count == 16 ){
904 vg_warn( "Exceeding sphere_vs_scene capacity. Geometry too dense!\n" );
905 return count;
906 }
907 }
908
909 return count;
910 }
911
912 VG_STATIC int rb_box__scene( m4x3f mtxA, boxf bbx,
913 m4x3f mtxB, rb_scene *s, rb_ct *buf ){
914 scene_context *sc = s->bh_scene->user;
915 v3f tri[3];
916
917 v3f extent, center;
918 v3_sub( bbx[1], bbx[0], extent );
919 v3_muls( extent, 0.5f, extent );
920 v3_add( bbx[0], extent, center );
921
922 float r = v3_length(extent);
923 boxf world_bbx;
924 v3_fill( world_bbx[0], -r );
925 v3_fill( world_bbx[1], r );
926 for( int i=0; i<2; i++ ){
927 v3_add( center, world_bbx[i], world_bbx[i] );
928 v3_add( mtxA[3], world_bbx[i], world_bbx[i] );
929 }
930
931 m4x3f to_local;
932 m4x3_invert_affine( mtxA, to_local );
933
934 bh_iter it;
935 bh_iter_init_box( 0, &it, world_bbx );
936 int idx;
937 int count = 0;
938
939 vg_line_boxf( world_bbx, VG__RED );
940
941 while( bh_next( s->bh_scene, &it, &idx ) ){
942 u32 *ptri = &sc->arrindices[ idx*3 ];
943
944 for( int j=0; j<3; j++ )
945 v3_copy( sc->arrvertices[ptri[j]].co, tri[j] );
946
947 if( rb_box_triangle_sat( extent, center, to_local, tri ) ){
948 vg_line(tri[0],tri[1],0xff50ff00 );
949 vg_line(tri[1],tri[2],0xff50ff00 );
950 vg_line(tri[2],tri[0],0xff50ff00 );
951 }
952 else{
953 vg_line(tri[0],tri[1],0xff0000ff );
954 vg_line(tri[1],tri[2],0xff0000ff );
955 vg_line(tri[2],tri[0],0xff0000ff );
956 continue;
957 }
958
959 v3f v0,v1,n;
960 v3_sub( tri[1], tri[0], v0 );
961 v3_sub( tri[2], tri[0], v1 );
962 v3_cross( v0, v1, n );
963 v3_normalize( n );
964
965 /* find best feature */
966 float best = v3_dot( mtxA[0], n );
967 int axis = 0;
968
969 for( int i=1; i<3; i++ ){
970 float c = v3_dot( mtxA[i], n );
971
972 if( fabsf(c) > fabsf(best) ){
973 best = c;
974 axis = i;
975 }
976 }
977
978 v3f manifold[4];
979
980 if( axis == 0 ){
981 float px = best > 0.0f? bbx[0][0]: bbx[1][0];
982 manifold[0][0] = px;
983 manifold[0][1] = bbx[0][1];
984 manifold[0][2] = bbx[0][2];
985 manifold[1][0] = px;
986 manifold[1][1] = bbx[1][1];
987 manifold[1][2] = bbx[0][2];
988 manifold[2][0] = px;
989 manifold[2][1] = bbx[1][1];
990 manifold[2][2] = bbx[1][2];
991 manifold[3][0] = px;
992 manifold[3][1] = bbx[0][1];
993 manifold[3][2] = bbx[1][2];
994 }
995 else if( axis == 1 ){
996 float py = best > 0.0f? bbx[0][1]: bbx[1][1];
997 manifold[0][0] = bbx[0][0];
998 manifold[0][1] = py;
999 manifold[0][2] = bbx[0][2];
1000 manifold[1][0] = bbx[1][0];
1001 manifold[1][1] = py;
1002 manifold[1][2] = bbx[0][2];
1003 manifold[2][0] = bbx[1][0];
1004 manifold[2][1] = py;
1005 manifold[2][2] = bbx[1][2];
1006 manifold[3][0] = bbx[0][0];
1007 manifold[3][1] = py;
1008 manifold[3][2] = bbx[1][2];
1009 }
1010 else{
1011 float pz = best > 0.0f? bbx[0][2]: bbx[1][2];
1012 manifold[0][0] = bbx[0][0];
1013 manifold[0][1] = bbx[0][1];
1014 manifold[0][2] = pz;
1015 manifold[1][0] = bbx[1][0];
1016 manifold[1][1] = bbx[0][1];
1017 manifold[1][2] = pz;
1018 manifold[2][0] = bbx[1][0];
1019 manifold[2][1] = bbx[1][1];
1020 manifold[2][2] = pz;
1021 manifold[3][0] = bbx[0][0];
1022 manifold[3][1] = bbx[1][1];
1023 manifold[3][2] = pz;
1024 }
1025
1026 for( int j=0; j<4; j++ )
1027 m4x3_mulv( mtxA, manifold[j], manifold[j] );
1028
1029 vg_line( manifold[0], manifold[1], 0xffffffff );
1030 vg_line( manifold[1], manifold[2], 0xffffffff );
1031 vg_line( manifold[2], manifold[3], 0xffffffff );
1032 vg_line( manifold[3], manifold[0], 0xffffffff );
1033
1034 for( int j=0; j<4; j++ ){
1035 rb_ct *ct = buf+count;
1036
1037 v3_copy( manifold[j], ct->co );
1038 v3_copy( n, ct->n );
1039
1040 float l0 = v3_dot( tri[0], n ),
1041 l1 = v3_dot( manifold[j], n );
1042
1043 ct->p = (l0-l1)*0.5f;
1044 if( ct->p < 0.0f )
1045 continue;
1046
1047 ct->type = k_contact_type_default;
1048 count ++;
1049
1050 if( count >= 12 )
1051 return count;
1052 }
1053 }
1054 return count;
1055 }
1056
1057 VG_STATIC int rb_capsule__triangle( m4x3f mtxA, rb_capsule *c,
1058 v3f tri[3], rb_ct *buf ){
1059 v3f pc, p0w, p1w;
1060 v3_muladds( mtxA[3], mtxA[1], -c->height*0.5f+c->radius, p0w );
1061 v3_muladds( mtxA[3], mtxA[1], c->height*0.5f-c->radius, p1w );
1062
1063 capsule_manifold manifold;
1064 rb_capsule_manifold_init( &manifold );
1065
1066 v3f c0, c1;
1067 closest_on_triangle_1( p0w, tri, c0 );
1068 closest_on_triangle_1( p1w, tri, c1 );
1069
1070 v3f d0, d1, da;
1071 v3_sub( c0, p0w, d0 );
1072 v3_sub( c1, p1w, d1 );
1073 v3_sub( p1w, p0w, da );
1074
1075 v3_normalize(d0);
1076 v3_normalize(d1);
1077 v3_normalize(da);
1078
1079 if( v3_dot( da, d0 ) <= 0.01f )
1080 rb_capsule_manifold( p0w, c0, 0.0f, c->radius, &manifold );
1081
1082 if( v3_dot( da, d1 ) >= -0.01f )
1083 rb_capsule_manifold( p1w, c1, 1.0f, c->radius, &manifold );
1084
1085 for( int i=0; i<3; i++ ){
1086 int i0 = i,
1087 i1 = (i+1)%3;
1088
1089 v3f ca, cb;
1090 float ta, tb;
1091 closest_segment_segment( p0w, p1w, tri[i0], tri[i1], &ta, &tb, ca, cb );
1092 rb_capsule_manifold( ca, cb, ta, c->radius, &manifold );
1093 }
1094
1095 v3f v0, v1, n;
1096 v3_sub( tri[1], tri[0], v0 );
1097 v3_sub( tri[2], tri[0], v1 );
1098 v3_cross( v0, v1, n );
1099
1100 if( v3_length2( n ) <= 0.00001f ){
1101 vg_error( "Zero area triangle!\n" );
1102 return 0;
1103 }
1104
1105 v3_normalize( n );
1106
1107 int count = rb_capsule__manifold_done( mtxA, c, &manifold, buf );
1108 for( int i=0; i<count; i++ )
1109 v3_copy( n, buf[i].n );
1110
1111 return count;
1112 }
1113
1114 /* mtxB is defined only for tradition; it is not used currently */
1115 VG_STATIC int rb_capsule__scene( m4x3f mtxA, rb_capsule *c,
1116 m4x3f mtxB, rb_scene *s,
1117 rb_ct *buf ){
1118 int count = 0;
1119
1120 boxf bbx;
1121 v3_sub( mtxA[3], (v3f){ c->height, c->height, c->height }, bbx[0] );
1122 v3_add( mtxA[3], (v3f){ c->height, c->height, c->height }, bbx[1] );
1123
1124 scene_context *sc = s->bh_scene->user;
1125
1126 bh_iter it;
1127 bh_iter_init_box( 0, &it, bbx );
1128 i32 idx;
1129 while( bh_next( s->bh_scene, &it, &idx ) ){
1130 u32 *ptri = &sc->arrindices[ idx*3 ];
1131 v3f tri[3];
1132
1133 for( int j=0; j<3; j++ )
1134 v3_copy( sc->arrvertices[ptri[j]].co, tri[j] );
1135
1136 buf[ count ].element_id = ptri[0];
1137
1138 int contact = rb_capsule__triangle( mtxA, c, tri, &buf[count] );
1139 count += contact;
1140
1141 if( count >= 16 ){
1142 vg_warn("Exceeding capsule_vs_scene capacity. Geometry too dense!\n");
1143 return count;
1144 }
1145 }
1146
1147 return count;
1148 }
1149
1150 VG_STATIC int rb_global_has_space( void ){
1151 if( rb_contact_count + 16 > vg_list_size(rb_contact_buffer) )
1152 return 0;
1153
1154 return 1;
1155 }
1156
1157 VG_STATIC rb_ct *rb_global_buffer( void ){
1158 return &rb_contact_buffer[ rb_contact_count ];
1159 }
1160
1161 /*
1162 * -----------------------------------------------------------------------------
1163 * Dynamics
1164 * -----------------------------------------------------------------------------
1165 */
1166
1167 VG_STATIC void rb_solver_reset(void){
1168 rb_contact_count = 0;
1169 }
1170
1171 VG_STATIC rb_ct *rb_global_ct(void){
1172 return rb_contact_buffer + rb_contact_count;
1173 }
1174
1175 VG_STATIC void rb_prepare_contact( rb_ct *ct, float timestep ){
1176 ct->bias = -0.2f * (timestep*3600.0f)
1177 * vg_minf( 0.0f, -ct->p+k_penetration_slop );
1178
1179 v3_tangent_basis( ct->n, ct->t[0], ct->t[1] );
1180 ct->norm_impulse = 0.0f;
1181 ct->tangent_impulse[0] = 0.0f;
1182 ct->tangent_impulse[1] = 0.0f;
1183 }
1184
1185 /* calculate total move. manifold should belong to ONE object only */
1186 VG_STATIC void rb_depenetrate( rb_ct *manifold, int len, v3f dt ){
1187 v3_zero( dt );
1188
1189 for( int j=0; j<7; j++ )
1190 {
1191 for( int i=0; i<len; i++ )
1192 {
1193 struct contact *ct = &manifold[i];
1194
1195 float resolved_amt = v3_dot( ct->n, dt ),
1196 remaining = (ct->p-k_penetration_slop) - resolved_amt,
1197 apply = vg_maxf( remaining, 0.0f ) * 0.4f;
1198
1199 v3_muladds( dt, ct->n, apply, dt );
1200 }
1201 }
1202 }
1203
1204 /*
1205 * Initializing things like tangent vectors
1206 */
1207 VG_STATIC void rb_presolve_contacts( rb_ct *buffer, int len ){
1208 for( int i=0; i<len; i++ ){
1209 rb_ct *ct = &buffer[i];
1210 rb_prepare_contact( ct, k_rb_delta );
1211
1212 v3f ra, rb, raCn, rbCn, raCt, rbCt;
1213 v3_sub( ct->co, ct->rba->co, ra );
1214 v3_sub( ct->co, ct->rbb->co, rb );
1215 v3_cross( ra, ct->n, raCn );
1216 v3_cross( rb, ct->n, rbCn );
1217
1218 /* orient inverse inertia tensors */
1219 v3f raCnI, rbCnI;
1220 m3x3_mulv( ct->rba->iIw, raCn, raCnI );
1221 m3x3_mulv( ct->rbb->iIw, rbCn, rbCnI );
1222
1223 ct->normal_mass = ct->rba->inv_mass + ct->rbb->inv_mass;
1224 ct->normal_mass += v3_dot( raCn, raCnI );
1225 ct->normal_mass += v3_dot( rbCn, rbCnI );
1226 ct->normal_mass = 1.0f/ct->normal_mass;
1227
1228 for( int j=0; j<2; j++ ){
1229 v3f raCtI, rbCtI;
1230 v3_cross( ct->t[j], ra, raCt );
1231 v3_cross( ct->t[j], rb, rbCt );
1232 m3x3_mulv( ct->rba->iIw, raCt, raCtI );
1233 m3x3_mulv( ct->rbb->iIw, rbCt, rbCtI );
1234
1235 ct->tangent_mass[j] = ct->rba->inv_mass + ct->rbb->inv_mass;
1236 ct->tangent_mass[j] += v3_dot( raCt, raCtI );
1237 ct->tangent_mass[j] += v3_dot( rbCt, rbCtI );
1238 ct->tangent_mass[j] = 1.0f/ct->tangent_mass[j];
1239 }
1240
1241 rb_debug_contact( ct );
1242 }
1243 }
1244
1245 /*
1246 * Creates relative contact velocity vector
1247 */
1248 VG_STATIC void rb_rcv( rigidbody *rba, rigidbody *rbb, v3f ra, v3f rb, v3f rv ){
1249 v3f rva, rvb;
1250 v3_cross( rba->w, ra, rva );
1251 v3_add( rba->v, rva, rva );
1252 v3_cross( rbb->w, rb, rvb );
1253 v3_add( rbb->v, rvb, rvb );
1254
1255 v3_sub( rva, rvb, rv );
1256 }
1257
1258 VG_STATIC void rb_contact_restitution( rb_ct *ct, float cr ){
1259 v3f rv, ra, rb;
1260 v3_sub( ct->co, ct->rba->co, ra );
1261 v3_sub( ct->co, ct->rbb->co, rb );
1262 rb_rcv( ct->rba, ct->rbb, ra, rb, rv );
1263
1264 float v = v3_dot( rv, ct->n );
1265
1266 if( v < -1.0f ){
1267 ct->bias += -cr * v;
1268 }
1269 }
1270
1271 /*
1272 * Apply impulse to object
1273 */
1274 VG_STATIC void rb_linear_impulse( rigidbody *rb, v3f delta, v3f impulse ){
1275 /* linear */
1276 v3_muladds( rb->v, impulse, rb->inv_mass, rb->v );
1277
1278 /* Angular velocity */
1279 v3f wa;
1280 v3_cross( delta, impulse, wa );
1281
1282 m3x3_mulv( rb->iIw, wa, wa );
1283 v3_add( rb->w, wa, rb->w );
1284 }
1285
1286 /*
1287 * One iteration to solve the contact constraint
1288 */
1289 VG_STATIC void rb_solve_contacts( rb_ct *buf, int len ){
1290 for( int i=0; i<len; i++ ){
1291 struct contact *ct = &buf[i];
1292
1293 v3f rv, ra, rb;
1294 v3_sub( ct->co, ct->rba->co, ra );
1295 v3_sub( ct->co, ct->rbb->co, rb );
1296 rb_rcv( ct->rba, ct->rbb, ra, rb, rv );
1297
1298 /* Friction */
1299 for( int j=0; j<2; j++ ){
1300 float f = k_friction * ct->norm_impulse,
1301 vt = v3_dot( rv, ct->t[j] ),
1302 lambda = ct->tangent_mass[j] * -vt;
1303
1304 float temp = ct->tangent_impulse[j];
1305 ct->tangent_impulse[j] = vg_clampf( temp + lambda, -f, f );
1306 lambda = ct->tangent_impulse[j] - temp;
1307
1308 v3f impulse;
1309 v3_muls( ct->t[j], lambda, impulse );
1310 rb_linear_impulse( ct->rba, ra, impulse );
1311
1312 v3_muls( ct->t[j], -lambda, impulse );
1313 rb_linear_impulse( ct->rbb, rb, impulse );
1314 }
1315
1316 /* Normal */
1317 rb_rcv( ct->rba, ct->rbb, ra, rb, rv );
1318 float vn = v3_dot( rv, ct->n ),
1319 lambda = ct->normal_mass * (-vn + ct->bias);
1320
1321 float temp = ct->norm_impulse;
1322 ct->norm_impulse = vg_maxf( temp + lambda, 0.0f );
1323 lambda = ct->norm_impulse - temp;
1324
1325 v3f impulse;
1326 v3_muls( ct->n, lambda, impulse );
1327 rb_linear_impulse( ct->rba, ra, impulse );
1328
1329 v3_muls( ct->n, -lambda, impulse );
1330 rb_linear_impulse( ct->rbb, rb, impulse );
1331 }
1332 }
1333
1334 /*
1335 * -----------------------------------------------------------------------------
1336 * Constraints
1337 * -----------------------------------------------------------------------------
1338 */
1339
1340 VG_STATIC void rb_debug_position_constraints( rb_constr_pos *buffer, int len ){
1341 for( int i=0; i<len; i++ ){
1342 rb_constr_pos *constr = &buffer[i];
1343 rigidbody *rba = constr->rba, *rbb = constr->rbb;
1344
1345 v3f wca, wcb;
1346 m3x3_mulv( rba->to_world, constr->lca, wca );
1347 m3x3_mulv( rbb->to_world, constr->lcb, wcb );
1348
1349 v3f p0, p1;
1350 v3_add( wca, rba->co, p0 );
1351 v3_add( wcb, rbb->co, p1 );
1352 vg_line_point( p0, 0.0025f, 0xff000000 );
1353 vg_line_point( p1, 0.0025f, 0xffffffff );
1354 vg_line2( p0, p1, 0xff000000, 0xffffffff );
1355 }
1356 }
1357
1358 VG_STATIC void rb_presolve_swingtwist_constraints( rb_constr_swingtwist *buf,
1359 int len ){
1360 float size = 0.12f;
1361
1362 for( int i=0; i<len; i++ ){
1363 rb_constr_swingtwist *st = &buf[ i ];
1364
1365 v3f vx, vy, va, vxb, axis, center;
1366
1367 m3x3_mulv( st->rba->to_world, st->conevx, vx );
1368 m3x3_mulv( st->rbb->to_world, st->conevxb, vxb );
1369 m3x3_mulv( st->rba->to_world, st->conevy, vy );
1370 m3x3_mulv( st->rbb->to_world, st->coneva, va );
1371 m4x3_mulv( st->rba->to_world, st->view_offset, center );
1372 v3_cross( vy, vx, axis );
1373
1374 /* Constraint violated ? */
1375 float fx = v3_dot( vx, va ), /* projection world */
1376 fy = v3_dot( vy, va ),
1377 fn = v3_dot( va, axis ),
1378
1379 rx = st->conevx[3], /* elipse radii */
1380 ry = st->conevy[3],
1381
1382 lx = fx/rx, /* projection local (fn==lz) */
1383 ly = fy/ry;
1384
1385 st->tangent_violation = ((lx*lx + ly*ly) > fn*fn) || (fn <= 0.0f);
1386 if( st->tangent_violation ){
1387 /* Calculate a good position and the axis to solve on */
1388 v2f closest, tangent,
1389 p = { fx/fabsf(fn), fy/fabsf(fn) };
1390
1391 closest_point_elipse( p, (v2f){rx,ry}, closest );
1392 tangent[0] = -closest[1] / (ry*ry);
1393 tangent[1] = closest[0] / (rx*rx);
1394 v2_normalize( tangent );
1395
1396 v3f v0, v1;
1397 v3_muladds( axis, vx, closest[0], v0 );
1398 v3_muladds( v0, vy, closest[1], v0 );
1399 v3_normalize( v0 );
1400
1401 v3_muls( vx, tangent[0], v1 );
1402 v3_muladds( v1, vy, tangent[1], v1 );
1403
1404 v3_copy( v0, st->tangent_target );
1405 v3_copy( v1, st->tangent_axis );
1406
1407 /* calculate mass */
1408 v3f aIw, bIw;
1409 m3x3_mulv( st->rba->iIw, st->tangent_axis, aIw );
1410 m3x3_mulv( st->rbb->iIw, st->tangent_axis, bIw );
1411 st->tangent_mass = 1.0f / (v3_dot( st->tangent_axis, aIw ) +
1412 v3_dot( st->tangent_axis, bIw ));
1413
1414 float angle = v3_dot( va, st->tangent_target );
1415 }
1416
1417 v3f refaxis;
1418 v3_cross( vy, va, refaxis ); /* our default rotation */
1419 v3_normalize( refaxis );
1420
1421 float angle = v3_dot( refaxis, vxb );
1422 st->axis_violation = fabsf(angle) < st->conet;
1423
1424 if( st->axis_violation ){
1425 v3f dir_test;
1426 v3_cross( refaxis, vxb, dir_test );
1427
1428 if( v3_dot(dir_test, va) < 0.0f )
1429 st->axis_violation = -st->axis_violation;
1430
1431 float newang = (float)st->axis_violation * acosf(st->conet-0.0001f);
1432
1433 v3f refaxis_up;
1434 v3_cross( va, refaxis, refaxis_up );
1435 v3_muls( refaxis_up, sinf(newang), st->axis_target );
1436 v3_muladds( st->axis_target, refaxis, -cosf(newang), st->axis_target );
1437
1438 /* calculate mass */
1439 v3_copy( va, st->axis );
1440 v3f aIw, bIw;
1441 m3x3_mulv( st->rba->iIw, st->axis, aIw );
1442 m3x3_mulv( st->rbb->iIw, st->axis, bIw );
1443 st->axis_mass = 1.0f / (v3_dot( st->axis, aIw ) +
1444 v3_dot( st->axis, bIw ));
1445 }
1446 }
1447 }
1448
1449 VG_STATIC void rb_debug_swingtwist_constraints( rb_constr_swingtwist *buf,
1450 int len ){
1451 float size = 0.12f;
1452
1453 for( int i=0; i<len; i++ ){
1454 rb_constr_swingtwist *st = &buf[ i ];
1455
1456 v3f vx, vxb, vy, va, axis, center;
1457
1458 m3x3_mulv( st->rba->to_world, st->conevx, vx );
1459 m3x3_mulv( st->rbb->to_world, st->conevxb, vxb );
1460 m3x3_mulv( st->rba->to_world, st->conevy, vy );
1461 m3x3_mulv( st->rbb->to_world, st->coneva, va );
1462 m4x3_mulv( st->rba->to_world, st->view_offset, center );
1463 v3_cross( vy, vx, axis );
1464
1465 float rx = st->conevx[3], /* elipse radii */
1466 ry = st->conevy[3];
1467
1468 v3f p0, p1;
1469 v3_muladds( center, va, size, p1 );
1470 vg_line( center, p1, 0xffffffff );
1471 vg_line_point( p1, 0.00025f, 0xffffffff );
1472
1473 if( st->tangent_violation ){
1474 v3_muladds( center, st->tangent_target, size, p0 );
1475
1476 vg_line( center, p0, 0xff00ff00 );
1477 vg_line_point( p0, 0.00025f, 0xff00ff00 );
1478 vg_line( p1, p0, 0xff000000 );
1479 }
1480
1481 for( int x=0; x<32; x++ ){
1482 float t0 = ((float)x * (1.0f/32.0f)) * VG_TAUf,
1483 t1 = (((float)x+1.0f) * (1.0f/32.0f)) * VG_TAUf,
1484 c0 = cosf( t0 ),
1485 s0 = sinf( t0 ),
1486 c1 = cosf( t1 ),
1487 s1 = sinf( t1 );
1488
1489 v3f v0, v1;
1490 v3_muladds( axis, vx, c0*rx, v0 );
1491 v3_muladds( v0, vy, s0*ry, v0 );
1492 v3_muladds( axis, vx, c1*rx, v1 );
1493 v3_muladds( v1, vy, s1*ry, v1 );
1494
1495 v3_normalize( v0 );
1496 v3_normalize( v1 );
1497
1498 v3_muladds( center, v0, size, p0 );
1499 v3_muladds( center, v1, size, p1 );
1500
1501 u32 col0r = fabsf(c0) * 255.0f,
1502 col0g = fabsf(s0) * 255.0f,
1503 col1r = fabsf(c1) * 255.0f,
1504 col1g = fabsf(s1) * 255.0f,
1505 col = st->tangent_violation? 0xff0000ff: 0xff000000,
1506 col0 = col | (col0r<<16) | (col0g << 8),
1507 col1 = col | (col1r<<16) | (col1g << 8);
1508
1509 vg_line2( center, p0, VG__NONE, col0 );
1510 vg_line2( p0, p1, col0, col1 );
1511 }
1512
1513 /* Draw twist */
1514 v3_muladds( center, va, size, p0 );
1515 v3_muladds( p0, vxb, size, p1 );
1516
1517 vg_line( p0, p1, 0xff0000ff );
1518
1519 if( st->axis_violation ){
1520 v3_muladds( p0, st->axis_target, size*1.25f, p1 );
1521 vg_line( p0, p1, 0xffffff00 );
1522 vg_line_point( p1, 0.0025f, 0xffffff80 );
1523 }
1524
1525 v3f refaxis;
1526 v3_cross( vy, va, refaxis ); /* our default rotation */
1527 v3_normalize( refaxis );
1528 v3f refaxis_up;
1529 v3_cross( va, refaxis, refaxis_up );
1530 float newang = acosf(st->conet-0.0001f);
1531
1532 v3_muladds( p0, refaxis_up, sinf(newang)*size, p1 );
1533 v3_muladds( p1, refaxis, -cosf(newang)*size, p1 );
1534 vg_line( p0, p1, 0xff000000 );
1535
1536 v3_muladds( p0, refaxis_up, sinf(-newang)*size, p1 );
1537 v3_muladds( p1, refaxis, -cosf(-newang)*size, p1 );
1538 vg_line( p0, p1, 0xff404040 );
1539 }
1540 }
1541
1542 /*
1543 * Solve a list of positional constraints
1544 */
1545 VG_STATIC void rb_solve_position_constraints( rb_constr_pos *buf, int len ){
1546 for( int i=0; i<len; i++ ){
1547 rb_constr_pos *constr = &buf[i];
1548 rigidbody *rba = constr->rba, *rbb = constr->rbb;
1549
1550 v3f wa, wb;
1551 m3x3_mulv( rba->to_world, constr->lca, wa );
1552 m3x3_mulv( rbb->to_world, constr->lcb, wb );
1553
1554 m3x3f ssra, ssrat, ssrb, ssrbt;
1555
1556 m3x3_skew_symetric( ssrat, wa );
1557 m3x3_skew_symetric( ssrbt, wb );
1558 m3x3_transpose( ssrat, ssra );
1559 m3x3_transpose( ssrbt, ssrb );
1560
1561 v3f b, b_wa, b_wb, b_a, b_b;
1562 m3x3_mulv( ssra, rba->w, b_wa );
1563 m3x3_mulv( ssrb, rbb->w, b_wb );
1564 v3_add( rba->v, b_wa, b );
1565 v3_sub( b, rbb->v, b );
1566 v3_sub( b, b_wb, b );
1567 v3_muls( b, -1.0f, b );
1568
1569 m3x3f invMa, invMb;
1570 m3x3_diagonal( invMa, rba->inv_mass );
1571 m3x3_diagonal( invMb, rbb->inv_mass );
1572
1573 m3x3f ia, ib;
1574 m3x3_mul( ssra, rba->iIw, ia );
1575 m3x3_mul( ia, ssrat, ia );
1576 m3x3_mul( ssrb, rbb->iIw, ib );
1577 m3x3_mul( ib, ssrbt, ib );
1578
1579 m3x3f cma, cmb;
1580 m3x3_add( invMa, ia, cma );
1581 m3x3_add( invMb, ib, cmb );
1582
1583 m3x3f A;
1584 m3x3_add( cma, cmb, A );
1585
1586 /* Solve Ax = b ( A^-1*b = x ) */
1587 v3f impulse;
1588 m3x3f invA;
1589 m3x3_inv( A, invA );
1590 m3x3_mulv( invA, b, impulse );
1591
1592 v3f delta_va, delta_wa, delta_vb, delta_wb;
1593 m3x3f iwa, iwb;
1594 m3x3_mul( rba->iIw, ssrat, iwa );
1595 m3x3_mul( rbb->iIw, ssrbt, iwb );
1596
1597 m3x3_mulv( invMa, impulse, delta_va );
1598 m3x3_mulv( invMb, impulse, delta_vb );
1599 m3x3_mulv( iwa, impulse, delta_wa );
1600 m3x3_mulv( iwb, impulse, delta_wb );
1601
1602 v3_add( rba->v, delta_va, rba->v );
1603 v3_add( rba->w, delta_wa, rba->w );
1604 v3_sub( rbb->v, delta_vb, rbb->v );
1605 v3_sub( rbb->w, delta_wb, rbb->w );
1606 }
1607 }
1608
1609 VG_STATIC void rb_solve_swingtwist_constraints( rb_constr_swingtwist *buf,
1610 int len ){
1611 float size = 0.12f;
1612
1613 for( int i=0; i<len; i++ ){
1614 rb_constr_swingtwist *st = &buf[ i ];
1615
1616 if( !st->axis_violation )
1617 continue;
1618
1619 float rv = v3_dot( st->axis, st->rbb->w ) -
1620 v3_dot( st->axis, st->rba->w );
1621
1622 if( rv * (float)st->axis_violation > 0.0f )
1623 continue;
1624
1625 v3f impulse, wa, wb;
1626 v3_muls( st->axis, rv*st->axis_mass, impulse );
1627 m3x3_mulv( st->rba->iIw, impulse, wa );
1628 v3_add( st->rba->w, wa, st->rba->w );
1629
1630 v3_muls( impulse, -1.0f, impulse );
1631 m3x3_mulv( st->rbb->iIw, impulse, wb );
1632 v3_add( st->rbb->w, wb, st->rbb->w );
1633
1634 float rv2 = v3_dot( st->axis, st->rbb->w ) -
1635 v3_dot( st->axis, st->rba->w );
1636 }
1637
1638 for( int i=0; i<len; i++ ){
1639 rb_constr_swingtwist *st = &buf[ i ];
1640
1641 if( !st->tangent_violation )
1642 continue;
1643
1644 float rv = v3_dot( st->tangent_axis, st->rbb->w ) -
1645 v3_dot( st->tangent_axis, st->rba->w );
1646
1647 if( rv > 0.0f )
1648 continue;
1649
1650 v3f impulse, wa, wb;
1651 v3_muls( st->tangent_axis, rv*st->tangent_mass, impulse );
1652 m3x3_mulv( st->rba->iIw, impulse, wa );
1653 v3_add( st->rba->w, wa, st->rba->w );
1654
1655 v3_muls( impulse, -1.0f, impulse );
1656 m3x3_mulv( st->rbb->iIw, impulse, wb );
1657 v3_add( st->rbb->w, wb, st->rbb->w );
1658
1659 float rv2 = v3_dot( st->tangent_axis, st->rbb->w ) -
1660 v3_dot( st->tangent_axis, st->rba->w );
1661 }
1662 }
1663
1664 VG_STATIC void rb_solve_constr_angle( rigidbody *rba, rigidbody *rbb,
1665 v3f ra, v3f rb ){
1666 m3x3f ssra, ssrb, ssrat, ssrbt;
1667 m3x3f cma, cmb;
1668
1669 m3x3_skew_symetric( ssrat, ra );
1670 m3x3_skew_symetric( ssrbt, rb );
1671 m3x3_transpose( ssrat, ssra );
1672 m3x3_transpose( ssrbt, ssrb );
1673
1674 m3x3_mul( ssra, rba->iIw, cma );
1675 m3x3_mul( cma, ssrat, cma );
1676 m3x3_mul( ssrb, rbb->iIw, cmb );
1677 m3x3_mul( cmb, ssrbt, cmb );
1678
1679 m3x3f A, invA;
1680 m3x3_add( cma, cmb, A );
1681 m3x3_inv( A, invA );
1682
1683 v3f b_wa, b_wb, b;
1684 m3x3_mulv( ssra, rba->w, b_wa );
1685 m3x3_mulv( ssrb, rbb->w, b_wb );
1686 v3_add( b_wa, b_wb, b );
1687 v3_negate( b, b );
1688
1689 v3f impulse;
1690 m3x3_mulv( invA, b, impulse );
1691
1692 v3f delta_wa, delta_wb;
1693 m3x3f iwa, iwb;
1694 m3x3_mul( rba->iIw, ssrat, iwa );
1695 m3x3_mul( rbb->iIw, ssrbt, iwb );
1696 m3x3_mulv( iwa, impulse, delta_wa );
1697 m3x3_mulv( iwb, impulse, delta_wb );
1698 v3_add( rba->w, delta_wa, rba->w );
1699 v3_sub( rbb->w, delta_wb, rbb->w );
1700 }
1701
1702 /*
1703 * Correct position constraint drift errors
1704 * [ 0.0 <= amt <= 1.0 ]: the correction amount
1705 */
1706 VG_STATIC void rb_correct_position_constraints( rb_constr_pos *buf, int len,
1707 float amt ){
1708 for( int i=0; i<len; i++ ){
1709 rb_constr_pos *constr = &buf[i];
1710 rigidbody *rba = constr->rba, *rbb = constr->rbb;
1711
1712 v3f p0, p1, d;
1713 m3x3_mulv( rba->to_world, constr->lca, p0 );
1714 m3x3_mulv( rbb->to_world, constr->lcb, p1 );
1715 v3_add( rba->co, p0, p0 );
1716 v3_add( rbb->co, p1, p1 );
1717 v3_sub( p1, p0, d );
1718
1719 v3_muladds( rbb->co, d, -1.0f * amt, rbb->co );
1720 rb_update_transform( rbb );
1721 }
1722 }
1723
1724 VG_STATIC void rb_correct_swingtwist_constraints( rb_constr_swingtwist *buf,
1725 int len, float amt ){
1726 for( int i=0; i<len; i++ ){
1727 rb_constr_swingtwist *st = &buf[i];
1728
1729 if( !st->tangent_violation )
1730 continue;
1731
1732 v3f va;
1733 m3x3_mulv( st->rbb->to_world, st->coneva, va );
1734
1735 float angle = v3_dot( va, st->tangent_target );
1736
1737 if( fabsf(angle) < 0.9999f ){
1738 v3f axis;
1739 v3_cross( va, st->tangent_target, axis );
1740
1741 v4f correction;
1742 q_axis_angle( correction, axis, acosf(angle) * amt );
1743 q_mul( correction, st->rbb->q, st->rbb->q );
1744 rb_update_transform( st->rbb );
1745 }
1746 }
1747
1748 for( int i=0; i<len; i++ ){
1749 rb_constr_swingtwist *st = &buf[i];
1750
1751 if( !st->axis_violation )
1752 continue;
1753
1754 v3f vxb;
1755 m3x3_mulv( st->rbb->to_world, st->conevxb, vxb );
1756
1757 float angle = v3_dot( vxb, st->axis_target );
1758
1759 if( fabsf(angle) < 0.9999f ){
1760 v3f axis;
1761 v3_cross( vxb, st->axis_target, axis );
1762
1763 v4f correction;
1764 q_axis_angle( correction, axis, acosf(angle) * amt );
1765 q_mul( correction, st->rbb->q, st->rbb->q );
1766 rb_update_transform( st->rbb );
1767 }
1768 }
1769 }
1770
1771 VG_STATIC void rb_correct_contact_constraints( rb_ct *buf, int len, float amt ){
1772 for( int i=0; i<len; i++ ){
1773 rb_ct *ct = &buf[i];
1774 rigidbody *rba = ct->rba,
1775 *rbb = ct->rbb;
1776
1777 float mass_total = 1.0f / (rba->inv_mass + rbb->inv_mass);
1778
1779 v3_muladds( rba->co, ct->n, -mass_total * rba->inv_mass, rba->co );
1780 v3_muladds( rbb->co, ct->n, mass_total * rbb->inv_mass, rbb->co );
1781 }
1782 }
1783
1784
1785 /*
1786 * Effectors
1787 */
1788
1789 VG_STATIC void rb_effect_simple_bouyency( rigidbody *ra, v4f plane,
1790 float amt, float drag ){
1791 /* float */
1792 float depth = v3_dot( plane, ra->co ) - plane[3],
1793 lambda = vg_clampf( -depth, 0.0f, 1.0f ) * amt;
1794
1795 v3_muladds( ra->v, plane, lambda * k_rb_delta, ra->v );
1796
1797 if( depth < 0.0f )
1798 v3_muls( ra->v, 1.0f-(drag*k_rb_delta), ra->v );
1799 }
1800
1801 /* apply a spring&dampener force to match ra(worldspace) on rigidbody, to
1802 * rt(worldspace)
1803 */
1804 VG_STATIC void rb_effect_spring_target_vector( rigidbody *rba, v3f ra, v3f rt,
1805 float spring, float dampening,
1806 float timestep ){
1807 float d = v3_dot( rt, ra );
1808 float a = acosf( vg_clampf( d, -1.0f, 1.0f ) );
1809
1810 v3f axis;
1811 v3_cross( rt, ra, axis );
1812
1813 float Fs = -a * spring,
1814 Fd = -v3_dot( rba->w, axis ) * dampening;
1815
1816 v3_muladds( rba->w, axis, (Fs+Fd) * timestep, rba->w );
1817 }
1818
1819 #endif /* RIGIDBODY_H */