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