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