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