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