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