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