sat
[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 rb_object rb_object;
76 typedef struct contact rb_ct;
77 typedef struct rb_sphere rb_sphere;
78 typedef struct rb_capsule rb_capsule;
79 typedef struct rb_scene rb_scene;
80
81 struct rb_sphere
82 {
83 float radius;
84 };
85
86 struct rb_capsule
87 {
88 float height, radius;
89 };
90
91 struct rb_scene
92 {
93 bh_tree *bh_scene;
94 };
95
96 struct rigidbody
97 {
98 v3f co, v, w;
99 v4f q;
100
101 boxf bbx, bbx_world;
102 float inv_mass;
103
104 /* inertia model and inverse world tensor */
105 v3f I;
106 m3x3f iI, iIw;
107 m4x3f to_world, to_local;
108 };
109
110 /* simple objects */
111 struct rb_object
112 {
113 rigidbody rb;
114 enum rb_shape{
115 k_rb_shape_box = 0,
116 k_rb_shape_sphere = 1,
117 k_rb_shape_capsule = 2,
118 k_rb_shape_scene = 3
119 }
120 type;
121
122 union
123 {
124 struct rb_sphere sphere;
125 struct rb_capsule capsule;
126 struct rb_scene scene;
127 }
128 inf;
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_object_debug( rb_object *obj, u32 colour )
347 {
348 if( obj->type == k_rb_shape_box ){
349 v3f *box = obj->rb.bbx;
350 vg_line_boxf_transformed( obj->rb.to_world, obj->rb.bbx, colour );
351 }
352 else if( obj->type == k_rb_shape_sphere ){
353 debug_sphere( obj->rb.to_world, obj->inf.sphere.radius, colour );
354 }
355 else if( obj->type == k_rb_shape_capsule ){
356 m4x3f m0, m1;
357 float h = obj->inf.capsule.height,
358 r = obj->inf.capsule.radius;
359
360 debug_capsule( obj->rb.to_world, r, h, colour );
361 }
362 else if( obj->type == k_rb_shape_scene ){
363 vg_line_boxf( obj->rb.bbx, colour );
364 }
365 }
366
367 /*
368 * -----------------------------------------------------------------------------
369 * Integration
370 * -----------------------------------------------------------------------------
371 */
372
373 /*
374 * Update world space bounding box based on local one
375 */
376 VG_STATIC void rb_update_bounds( rigidbody *rb )
377 {
378 box_copy( rb->bbx, rb->bbx_world );
379 m4x3_transform_aabb( rb->to_world, rb->bbx_world );
380 }
381
382 /*
383 * Commit transform to rigidbody. Updates matrices
384 */
385 VG_STATIC void rb_update_transform( rigidbody *rb )
386 {
387 q_normalize( rb->q );
388 q_m3x3( rb->q, rb->to_world );
389 v3_copy( rb->co, rb->to_world[3] );
390
391 m4x3_invert_affine( rb->to_world, rb->to_local );
392
393 #if 0
394 m3x3_mulv( rb->to_world, (v3f){1.0f,0.0f, 0.0f}, rb->right );
395 m3x3_mulv( rb->to_world, (v3f){0.0f,1.0f, 0.0f}, rb->up );
396 m3x3_mulv( rb->to_world, (v3f){0.0f,0.0f,-1.0f}, rb->forward );
397 #endif
398
399 m3x3_mul( rb->iI, rb->to_local, rb->iIw );
400 m3x3_mul( rb->to_world, rb->iIw, rb->iIw );
401
402 rb_update_bounds( rb );
403 }
404
405 /*
406 * Extrapolate rigidbody into a transform based on vg accumulator.
407 * Useful for rendering
408 */
409 VG_STATIC void rb_extrapolate( rigidbody *rb, v3f co, v4f q )
410 {
411 float substep = vg_clampf( vg.accumulator / k_rb_delta, 0.0f, 1.0f );
412
413 v3_muladds( rb->co, rb->v, k_rb_delta*substep, co );
414
415 if( v3_length2( rb->w ) > 0.0f ){
416 v4f rotation;
417 v3f axis;
418 v3_copy( rb->w, axis );
419
420 float mag = v3_length( axis );
421 v3_divs( axis, mag, axis );
422 q_axis_angle( rotation, axis, mag*k_rb_delta*substep );
423 q_mul( rotation, rb->q, q );
424 q_normalize( q );
425 }
426 else{
427 v4_copy( rb->q, q );
428 }
429 }
430
431 /*
432 * Initialize rigidbody and calculate masses, inertia
433 */
434 VG_STATIC void rb_init_object( rb_object *obj )
435 {
436 float volume = 1.0f;
437 int inert = 0;
438
439 if( obj->type == k_rb_shape_box ){
440 v3f dims;
441 v3_sub( obj->rb.bbx[1], obj->rb.bbx[0], dims );
442 volume = dims[0]*dims[1]*dims[2];
443 }
444 else if( obj->type == k_rb_shape_sphere ){
445 volume = sphere_volume( obj->inf.sphere.radius );
446 v3_fill( obj->rb.bbx[0], -obj->inf.sphere.radius );
447 v3_fill( obj->rb.bbx[1], obj->inf.sphere.radius );
448 }
449 else if( obj->type == k_rb_shape_capsule ){
450 float r = obj->inf.capsule.radius,
451 h = obj->inf.capsule.height;
452 volume = sphere_volume( r ) + VG_PIf * r*r * (h - r*2.0f);
453
454 v3_fill( obj->rb.bbx[0], -r );
455 v3_fill( obj->rb.bbx[1], r );
456 obj->rb.bbx[0][1] = -h;
457 obj->rb.bbx[1][1] = h;
458 }
459 else if( obj->type == k_rb_shape_scene ){
460 inert = 1;
461 box_copy( obj->inf.scene.bh_scene->nodes[0].bbx, obj->rb.bbx );
462 }
463
464 if( inert ){
465 obj->rb.inv_mass = 0.0f;
466 v3_zero( obj->rb.I );
467 m3x3_zero( obj->rb.iI );
468 }
469 else{
470 float mass = 2.0f*volume;
471 obj->rb.inv_mass = 1.0f/mass;
472
473 v3f extent;
474 v3_sub( obj->rb.bbx[1], obj->rb.bbx[0], extent );
475 v3_muls( extent, 0.5f, extent );
476
477 /* local intertia tensor */
478 float scale = k_inertia_scale;
479 float ex2 = scale*extent[0]*extent[0],
480 ey2 = scale*extent[1]*extent[1],
481 ez2 = scale*extent[2]*extent[2];
482
483 obj->rb.I[0] = ((1.0f/12.0f) * mass * (ey2+ez2));
484 obj->rb.I[1] = ((1.0f/12.0f) * mass * (ex2+ez2));
485 obj->rb.I[2] = ((1.0f/12.0f) * mass * (ex2+ey2));
486
487 m3x3_identity( obj->rb.iI );
488 obj->rb.iI[0][0] = obj->rb.I[0];
489 obj->rb.iI[1][1] = obj->rb.I[1];
490 obj->rb.iI[2][2] = obj->rb.I[2];
491 m3x3_inv( obj->rb.iI, obj->rb.iI );
492 }
493
494 rb_update_transform( &obj->rb );
495 }
496
497 VG_STATIC void rb_iter( rigidbody *rb )
498 {
499 if( !vg_validf( rb->v[0] ) ||
500 !vg_validf( rb->v[1] ) ||
501 !vg_validf( rb->v[2] ) )
502 {
503 vg_fatal_exit_loop( "NaN velocity" );
504 }
505
506 v3f gravity = { 0.0f, -9.8f, 0.0f };
507 v3_muladds( rb->v, gravity, k_rb_delta, rb->v );
508
509 /* intergrate velocity */
510 v3_muladds( rb->co, rb->v, k_rb_delta, rb->co );
511 v3_lerp( rb->w, (v3f){0.0f,0.0f,0.0f}, 0.0025f, rb->w );
512
513 /* inegrate inertia */
514 if( v3_length2( rb->w ) > 0.0f )
515 {
516 v4f rotation;
517 v3f axis;
518 v3_copy( rb->w, axis );
519
520 float mag = v3_length( axis );
521 v3_divs( axis, mag, axis );
522 q_axis_angle( rotation, axis, mag*k_rb_delta );
523 q_mul( rotation, rb->q, rb->q );
524 }
525
526 /* damping */
527 v3_muls( rb->v, 1.0f/(1.0f+k_rb_delta*k_damp_linear), rb->v );
528 v3_muls( rb->w, 1.0f/(1.0f+k_rb_delta*k_damp_angular), rb->w );
529 }
530
531
532 /*
533 * -----------------------------------------------------------------------------
534 * Boolean shape overlap functions
535 * -----------------------------------------------------------------------------
536 */
537
538 /*
539 * Project AABB, and triangle interval onto axis to check if they overlap
540 */
541 VG_STATIC int rb_box_triangle_interval( v3f extent, v3f axis, v3f tri[3] )
542 {
543 float
544
545 r = extent[0] * fabsf(axis[0]) +
546 extent[1] * fabsf(axis[1]) +
547 extent[2] * fabsf(axis[2]),
548
549 p0 = v3_dot( axis, tri[0] ),
550 p1 = v3_dot( axis, tri[1] ),
551 p2 = v3_dot( axis, tri[2] ),
552
553 e = vg_maxf(-vg_maxf(p0,vg_maxf(p1,p2)), vg_minf(p0,vg_minf(p1,p2)));
554
555 if( e > r ) return 0;
556 else return 1;
557 }
558
559 /*
560 * Seperating axis test box vs triangle
561 */
562 VG_STATIC int rb_box_triangle_sat( v3f extent, v3f center,
563 m4x3f to_local, v3f tri_src[3] )
564 {
565 v3f tri[3];
566
567 for( int i=0; i<3; i++ ){
568 m4x3_mulv( to_local, tri_src[i], tri[i] );
569 v3_sub( tri[i], center, tri[i] );
570 }
571
572 v3f f0,f1,f2,n;
573 v3_sub( tri[1], tri[0], f0 );
574 v3_sub( tri[2], tri[1], f1 );
575 v3_sub( tri[0], tri[2], f2 );
576
577
578 v3f axis[9];
579 v3_cross( (v3f){1.0f,0.0f,0.0f}, f0, axis[0] );
580 v3_cross( (v3f){1.0f,0.0f,0.0f}, f1, axis[1] );
581 v3_cross( (v3f){1.0f,0.0f,0.0f}, f2, axis[2] );
582 v3_cross( (v3f){0.0f,1.0f,0.0f}, f0, axis[3] );
583 v3_cross( (v3f){0.0f,1.0f,0.0f}, f1, axis[4] );
584 v3_cross( (v3f){0.0f,1.0f,0.0f}, f2, axis[5] );
585 v3_cross( (v3f){0.0f,0.0f,1.0f}, f0, axis[6] );
586 v3_cross( (v3f){0.0f,0.0f,1.0f}, f1, axis[7] );
587 v3_cross( (v3f){0.0f,0.0f,1.0f}, f2, axis[8] );
588
589 for( int i=0; i<9; i++ )
590 if(!rb_box_triangle_interval( extent, axis[i], tri )) return 0;
591
592 /* u0, u1, u2 */
593 if(!rb_box_triangle_interval( extent, (v3f){1.0f,0.0f,0.0f}, tri )) return 0;
594 if(!rb_box_triangle_interval( extent, (v3f){0.0f,1.0f,0.0f}, tri )) return 0;
595 if(!rb_box_triangle_interval( extent, (v3f){0.0f,0.0f,1.0f}, tri )) return 0;
596
597 /* normal */
598 v3_cross( f0, f1, n );
599 if(!rb_box_triangle_interval( extent, n, tri )) return 0;
600
601 return 1;
602 }
603
604 /*
605 * -----------------------------------------------------------------------------
606 * Manifold
607 * -----------------------------------------------------------------------------
608 */
609
610 VG_STATIC int rb_manifold_apply_filtered( rb_ct *man, int len )
611 {
612 int k = 0;
613
614 for( int i=0; i<len; i++ ){
615 rb_ct *ct = &man[i];
616
617 if( ct->type == k_contact_type_disabled )
618 continue;
619
620 man[k ++] = man[i];
621 }
622
623 return k;
624 }
625
626 /*
627 * Merge two contacts if they are within radius(r) of eachother
628 */
629 VG_STATIC void rb_manifold_contact_weld( rb_ct *ci, rb_ct *cj, float r )
630 {
631 if( v3_dist2( ci->co, cj->co ) < r*r ){
632 cj->type = k_contact_type_disabled;
633 ci->p = (ci->p + cj->p) * 0.5f;
634
635 v3_add( ci->co, cj->co, ci->co );
636 v3_muls( ci->co, 0.5f, ci->co );
637
638 v3f delta;
639 v3_sub( ci->rba->co, ci->co, delta );
640
641 float c0 = v3_dot( ci->n, delta ),
642 c1 = v3_dot( cj->n, delta );
643
644 if( c0 < 0.0f || c1 < 0.0f ){
645 /* error */
646 ci->type = k_contact_type_disabled;
647 }
648 else{
649 v3f n;
650 v3_muls( ci->n, c0, n );
651 v3_muladds( n, cj->n, c1, n );
652 v3_normalize( n );
653 v3_copy( n, ci->n );
654 }
655 }
656 }
657
658 /*
659 *
660 */
661 VG_STATIC void rb_manifold_filter_joint_edges( rb_ct *man, int len, float r )
662 {
663 for( int i=0; i<len-1; i++ ){
664 rb_ct *ci = &man[i];
665 if( ci->type != k_contact_type_edge )
666 continue;
667
668 for( int j=i+1; j<len; j++ ){
669 rb_ct *cj = &man[j];
670 if( cj->type != k_contact_type_edge )
671 continue;
672
673 rb_manifold_contact_weld( ci, cj, r );
674 }
675 }
676 }
677
678 /*
679 * Resolve overlapping pairs
680 *
681 * TODO: Remove?
682 */
683 VG_STATIC void rb_manifold_filter_pairs( rb_ct *man, int len, float r )
684 {
685 for( int i=0; i<len-1; i++ ){
686 rb_ct *ci = &man[i];
687 int similar = 0;
688
689 if( ci->type == k_contact_type_disabled ) continue;
690
691 for( int j=i+1; j<len; j++ ){
692 rb_ct *cj = &man[j];
693
694 if( cj->type == k_contact_type_disabled ) continue;
695
696 if( v3_dist2( ci->co, cj->co ) < r*r ){
697 cj->type = k_contact_type_disabled;
698 v3_add( cj->n, ci->n, ci->n );
699 ci->p += cj->p;
700 similar ++;
701 }
702 }
703
704 if( similar ){
705 float n = 1.0f/((float)similar+1.0f);
706 v3_muls( ci->n, n, ci->n );
707 ci->p *= n;
708
709 if( v3_length2(ci->n) < 0.1f*0.1f )
710 ci->type = k_contact_type_disabled;
711 else
712 v3_normalize( ci->n );
713 }
714 }
715 }
716
717 /*
718 * Remove contacts that are facing away from A
719 */
720 VG_STATIC void rb_manifold_filter_backface( rb_ct *man, int len )
721 {
722 for( int i=0; i<len; i++ ){
723 rb_ct *ct = &man[i];
724 if( ct->type == k_contact_type_disabled )
725 continue;
726
727 v3f delta;
728 v3_sub( ct->co, ct->rba->co, delta );
729
730 if( v3_dot( delta, ct->n ) > -0.001f )
731 ct->type = k_contact_type_disabled;
732 }
733 }
734
735 /*
736 * Filter out duplicate coplanar results. Good for spheres.
737 */
738 VG_STATIC void rb_manifold_filter_coplanar( rb_ct *man, int len, float w )
739 {
740 for( int i=0; i<len; i++ ){
741 rb_ct *ci = &man[i];
742 if( ci->type == k_contact_type_disabled ||
743 ci->type == k_contact_type_edge )
744 continue;
745
746 float d1 = v3_dot( ci->co, ci->n );
747
748 for( int j=0; j<len; j++ ){
749 if( j == i )
750 continue;
751
752 rb_ct *cj = &man[j];
753 if( cj->type == k_contact_type_disabled )
754 continue;
755
756 float d2 = v3_dot( cj->co, ci->n ),
757 d = d2-d1;
758
759 if( fabsf( d ) <= w ){
760 cj->type = k_contact_type_disabled;
761 }
762 }
763 }
764 }
765
766 /*
767 * -----------------------------------------------------------------------------
768 * Collision matrix
769 * -----------------------------------------------------------------------------
770 */
771
772 /*
773 * Contact generators
774 *
775 * These do not automatically allocate contacts, an appropriately sized
776 * buffer must be supplied. The function returns the size of the manifold
777 * which was generated.
778 *
779 * The values set on the contacts are: n, co, p, rba, rbb
780 */
781
782 /*
783 * By collecting the minimum(time) and maximum(time) pairs of points, we
784 * build a reduced and stable exact manifold.
785 *
786 * tx: time at point
787 * rx: minimum distance of these points
788 * dx: the delta between the two points
789 *
790 * pairs will only ammend these if they are creating a collision
791 */
792 typedef struct capsule_manifold capsule_manifold;
793 struct capsule_manifold
794 {
795 float t0, t1;
796 float r0, r1;
797 v3f d0, d1;
798 };
799
800 /*
801 * Expand a line manifold with a new pair. t value is the time along segment
802 * on the oriented object which created this pair.
803 */
804 VG_STATIC void rb_capsule_manifold( v3f pa, v3f pb, float t, float r,
805 capsule_manifold *manifold )
806 {
807 v3f delta;
808 v3_sub( pa, pb, delta );
809
810 if( v3_length2(delta) < r*r ){
811 if( t < manifold->t0 ){
812 v3_copy( delta, manifold->d0 );
813 manifold->t0 = t;
814 manifold->r0 = r;
815 }
816
817 if( t > manifold->t1 ){
818 v3_copy( delta, manifold->d1 );
819 manifold->t1 = t;
820 manifold->r1 = r;
821 }
822 }
823 }
824
825 VG_STATIC void rb_capsule_manifold_init( capsule_manifold *manifold )
826 {
827 manifold->t0 = INFINITY;
828 manifold->t1 = -INFINITY;
829 }
830
831 VG_STATIC int rb_capsule__manifold_done( m4x3f mtx, rb_capsule *c,
832 capsule_manifold *manifold,
833 rb_ct *buf )
834 {
835 v3f p0, p1;
836 v3_muladds( mtx[3], mtx[1], -c->height*0.5f+c->radius, p0 );
837 v3_muladds( mtx[3], mtx[1], c->height*0.5f-c->radius, p1 );
838
839 int count = 0;
840 if( manifold->t0 <= 1.0f ){
841 rb_ct *ct = buf;
842
843 v3f pa;
844 v3_muls( p0, 1.0f-manifold->t0, pa );
845 v3_muladds( pa, p1, manifold->t0, pa );
846
847 float d = v3_length( manifold->d0 );
848 v3_muls( manifold->d0, 1.0f/d, ct->n );
849 v3_muladds( pa, ct->n, -c->radius, ct->co );
850
851 ct->p = manifold->r0 - d;
852 ct->type = k_contact_type_default;
853 count ++;
854 }
855
856 if( (manifold->t1 >= 0.0f) && (manifold->t0 != manifold->t1) ){
857 rb_ct *ct = buf+count;
858
859 v3f pa;
860 v3_muls( p0, 1.0f-manifold->t1, pa );
861 v3_muladds( pa, p1, manifold->t1, pa );
862
863 float d = v3_length( manifold->d1 );
864 v3_muls( manifold->d1, 1.0f/d, ct->n );
865 v3_muladds( pa, ct->n, -c->radius, ct->co );
866
867 ct->p = manifold->r1 - d;
868 ct->type = k_contact_type_default;
869
870 count ++;
871 }
872
873 /*
874 * Debugging
875 */
876
877 if( count == 2 )
878 vg_line( buf[0].co, buf[1].co, 0xff0000ff );
879
880 return count;
881 }
882
883 VG_STATIC int rb_capsule_sphere( rb_object *obja, rb_object *objb, rb_ct *buf )
884 {
885 rigidbody *rba = &obja->rb, *rbb = &objb->rb;
886 float h = obja->inf.capsule.height,
887 ra = obja->inf.capsule.radius,
888 rb = objb->inf.sphere.radius;
889
890 v3f p0, p1;
891 v3_muladds( rba->co, rba->to_world[1], -h*0.5f+ra, p0 );
892 v3_muladds( rba->co, rba->to_world[1], h*0.5f-ra, p1 );
893
894 v3f c, delta;
895 closest_point_segment( p0, p1, rbb->co, c );
896 v3_sub( c, rbb->co, delta );
897
898 float d2 = v3_length2(delta),
899 r = ra + rb;
900
901 if( d2 < r*r ){
902 float d = sqrtf(d2);
903
904 rb_ct *ct = buf;
905 v3_muls( delta, 1.0f/d, ct->n );
906 ct->p = r-d;
907
908 v3f p0, p1;
909 v3_muladds( c, ct->n, -ra, p0 );
910 v3_muladds( rbb->co, ct->n, rb, p1 );
911 v3_add( p0, p1, ct->co );
912 v3_muls( ct->co, 0.5f, ct->co );
913
914 ct->rba = rba;
915 ct->rbb = rbb;
916 ct->type = k_contact_type_default;
917
918 return 1;
919 }
920
921 return 0;
922 }
923
924 VG_STATIC int rb_capsule__capsule( m4x3f mtxA, rb_capsule *ca,
925 m4x3f mtxB, rb_capsule *cb, rb_ct *buf )
926 {
927 float ha = ca->height,
928 hb = cb->height,
929 ra = ca->radius,
930 rb = cb->radius,
931 r = ra+rb;
932
933 v3f p0, p1, p2, p3;
934 v3_muladds( mtxA[3], mtxA[1], -ha*0.5f+ra, p0 );
935 v3_muladds( mtxA[3], mtxA[1], ha*0.5f-ra, p1 );
936 v3_muladds( mtxB[3], mtxB[1], -hb*0.5f+rb, p2 );
937 v3_muladds( mtxB[3], mtxB[1], hb*0.5f-rb, p3 );
938
939 capsule_manifold manifold;
940 rb_capsule_manifold_init( &manifold );
941
942 v3f pa, pb;
943 float ta, tb;
944 closest_segment_segment( p0, p1, p2, p3, &ta, &tb, pa, pb );
945 rb_capsule_manifold( pa, pb, ta, r, &manifold );
946
947 ta = closest_point_segment( p0, p1, p2, pa );
948 tb = closest_point_segment( p0, p1, p3, pb );
949 rb_capsule_manifold( pa, p2, ta, r, &manifold );
950 rb_capsule_manifold( pb, p3, tb, r, &manifold );
951
952 closest_point_segment( p2, p3, p0, pa );
953 closest_point_segment( p2, p3, p1, pb );
954 rb_capsule_manifold( p0, pa, 0.0f, r, &manifold );
955 rb_capsule_manifold( p1, pb, 1.0f, r, &manifold );
956
957 return rb_capsule__manifold_done( mtxA, ca, &manifold, buf );
958 }
959
960 #if 0
961 /*
962 * Generates up to two contacts; optimised for the most stable manifold
963 */
964 VG_STATIC int rb_capsule_box( rigidbody *rba, rigidbody *rbb, rb_ct *buf )
965 {
966 float h = rba->inf.capsule.height,
967 r = rba->inf.capsule.radius;
968
969 /*
970 * Solving this in symetric local space of the cube saves us some time and a
971 * couple branches when it comes to the quad stage.
972 */
973 v3f centroid;
974 v3_add( rbb->bbx[0], rbb->bbx[1], centroid );
975 v3_muls( centroid, 0.5f, centroid );
976
977 boxf bbx;
978 v3_sub( rbb->bbx[0], centroid, bbx[0] );
979 v3_sub( rbb->bbx[1], centroid, bbx[1] );
980
981 v3f pc, p0w, p1w, p0, p1;
982 v3_muladds( rba->co, rba->up, -h*0.5f+r, p0w );
983 v3_muladds( rba->co, rba->up, h*0.5f-r, p1w );
984
985 m4x3_mulv( rbb->to_local, p0w, p0 );
986 m4x3_mulv( rbb->to_local, p1w, p1 );
987 v3_sub( p0, centroid, p0 );
988 v3_sub( p1, centroid, p1 );
989 v3_add( p0, p1, pc );
990 v3_muls( pc, 0.5f, pc );
991
992 /*
993 * Finding an appropriate quad to collide lines with
994 */
995 v3f region;
996 v3_div( pc, bbx[1], region );
997
998 v3f quad[4];
999 if( (fabsf(region[0]) > fabsf(region[1])) &&
1000 (fabsf(region[0]) > fabsf(region[2])) )
1001 {
1002 float px = vg_signf(region[0]) * bbx[1][0];
1003 v3_copy( (v3f){ px, bbx[0][1], bbx[0][2] }, quad[0] );
1004 v3_copy( (v3f){ px, bbx[1][1], bbx[0][2] }, quad[1] );
1005 v3_copy( (v3f){ px, bbx[1][1], bbx[1][2] }, quad[2] );
1006 v3_copy( (v3f){ px, bbx[0][1], bbx[1][2] }, quad[3] );
1007 }
1008 else if( fabsf(region[1]) > fabsf(region[2]) )
1009 {
1010 float py = vg_signf(region[1]) * bbx[1][1];
1011 v3_copy( (v3f){ bbx[0][0], py, bbx[0][2] }, quad[0] );
1012 v3_copy( (v3f){ bbx[1][0], py, bbx[0][2] }, quad[1] );
1013 v3_copy( (v3f){ bbx[1][0], py, bbx[1][2] }, quad[2] );
1014 v3_copy( (v3f){ bbx[0][0], py, bbx[1][2] }, quad[3] );
1015 }
1016 else
1017 {
1018 float pz = vg_signf(region[2]) * bbx[1][2];
1019 v3_copy( (v3f){ bbx[0][0], bbx[0][1], pz }, quad[0] );
1020 v3_copy( (v3f){ bbx[1][0], bbx[0][1], pz }, quad[1] );
1021 v3_copy( (v3f){ bbx[1][0], bbx[1][1], pz }, quad[2] );
1022 v3_copy( (v3f){ bbx[0][0], bbx[1][1], pz }, quad[3] );
1023 }
1024
1025 capsule_manifold manifold;
1026 rb_capsule_manifold_init( &manifold );
1027
1028 v3f c0, c1;
1029 closest_point_aabb( p0, bbx, c0 );
1030 closest_point_aabb( p1, bbx, c1 );
1031
1032 v3f d0, d1, da;
1033 v3_sub( c0, p0, d0 );
1034 v3_sub( c1, p1, d1 );
1035 v3_sub( p1, p0, da );
1036
1037 v3_normalize(d0);
1038 v3_normalize(d1);
1039 v3_normalize(da);
1040
1041 if( v3_dot( da, d0 ) <= 0.01f )
1042 rb_capsule_manifold( p0, c0, 0.0f, r, &manifold );
1043
1044 if( v3_dot( da, d1 ) >= -0.01f )
1045 rb_capsule_manifold( p1, c1, 1.0f, r, &manifold );
1046
1047 for( int i=0; i<4; i++ )
1048 {
1049 int i0 = i,
1050 i1 = (i+1)%4;
1051
1052 v3f ca, cb;
1053 float ta, tb;
1054 closest_segment_segment( p0, p1, quad[i0], quad[i1], &ta, &tb, ca, cb );
1055 rb_capsule_manifold( ca, cb, ta, r, &manifold );
1056 }
1057
1058 /*
1059 * Create final contacts based on line manifold
1060 */
1061 m3x3_mulv( rbb->to_world, manifold.d0, manifold.d0 );
1062 m3x3_mulv( rbb->to_world, manifold.d1, manifold.d1 );
1063
1064 /*
1065 * Debugging
1066 */
1067
1068 #if 0
1069 for( int i=0; i<4; i++ )
1070 {
1071 v3f q0, q1;
1072 int i0 = i,
1073 i1 = (i+1)%4;
1074
1075 v3_add( quad[i0], centroid, q0 );
1076 v3_add( quad[i1], centroid, q1 );
1077
1078 m4x3_mulv( rbb->to_world, q0, q0 );
1079 m4x3_mulv( rbb->to_world, q1, q1 );
1080
1081 vg_line( q0, q1, 0xffffffff );
1082 }
1083 #endif
1084
1085 return rb_capsule_manifold_done( rba, rbb, &manifold, buf );
1086 }
1087 #endif
1088
1089 VG_STATIC int rb_sphere_box( rb_object *obja, rb_object *objb, rb_ct *buf )
1090 {
1091 v3f co, delta;
1092 rigidbody *rba = &obja->rb, *rbb = &objb->rb;
1093
1094 closest_point_obb( rba->co, rbb->bbx, rbb->to_world, rbb->to_local, co );
1095 v3_sub( rba->co, co, delta );
1096
1097 float d2 = v3_length2(delta),
1098 r = obja->inf.sphere.radius;
1099
1100 if( d2 <= r*r ){
1101 float d;
1102
1103 rb_ct *ct = buf;
1104 if( d2 <= 0.0001f ){
1105 v3_sub( rba->co, rbb->co, delta );
1106
1107 /*
1108 * some extra testing is required to find the best axis to push the
1109 * object back outside the box. Since there isnt a clear seperating
1110 * vector already, especially on really high aspect boxes.
1111 */
1112 float lx = v3_dot( rbb->to_world[0], delta ),
1113 ly = v3_dot( rbb->to_world[1], delta ),
1114 lz = v3_dot( rbb->to_world[2], delta ),
1115 px = rbb->bbx[1][0] - fabsf(lx),
1116 py = rbb->bbx[1][1] - fabsf(ly),
1117 pz = rbb->bbx[1][2] - fabsf(lz);
1118
1119 if( px < py && px < pz )
1120 v3_muls( rbb->to_world[0], vg_signf(lx), ct->n );
1121 else if( py < pz )
1122 v3_muls( rbb->to_world[1], vg_signf(ly), ct->n );
1123 else
1124 v3_muls( rbb->to_world[2], vg_signf(lz), ct->n );
1125
1126 v3_muladds( rba->co, ct->n, -r, ct->co );
1127 ct->p = r;
1128 }
1129 else{
1130 d = sqrtf(d2);
1131 v3_muls( delta, 1.0f/d, ct->n );
1132 ct->p = r-d;
1133 v3_copy( co, ct->co );
1134 }
1135
1136 ct->rba = rba;
1137 ct->rbb = rbb;
1138 ct->type = k_contact_type_default;
1139 return 1;
1140 }
1141
1142 return 0;
1143 }
1144
1145 VG_STATIC int rb_sphere_sphere( rb_object *obja, rb_object *objb, rb_ct *buf )
1146 {
1147 rigidbody *rba = &obja->rb, *rbb = &objb->rb;
1148 v3f delta;
1149 v3_sub( rba->co, rbb->co, delta );
1150
1151 float d2 = v3_length2(delta),
1152 r = obja->inf.sphere.radius + objb->inf.sphere.radius;
1153
1154 if( d2 < r*r ){
1155 float d = sqrtf(d2);
1156
1157 rb_ct *ct = buf;
1158 v3_muls( delta, 1.0f/d, ct->n );
1159
1160 v3f p0, p1;
1161 v3_muladds( rba->co, ct->n,-obja->inf.sphere.radius, p0 );
1162 v3_muladds( rbb->co, ct->n, objb->inf.sphere.radius, p1 );
1163 v3_add( p0, p1, ct->co );
1164 v3_muls( ct->co, 0.5f, ct->co );
1165 ct->type = k_contact_type_default;
1166 ct->p = r-d;
1167 ct->rba = rba;
1168 ct->rbb = rbb;
1169 return 1;
1170 }
1171
1172 return 0;
1173 }
1174
1175 //#define RIGIDBODY_DYNAMIC_MESH_EDGES
1176
1177 #if 0
1178 __attribute__ ((deprecated))
1179 VG_STATIC int rb_sphere_triangle( rigidbody *rba, rigidbody *rbb,
1180 v3f tri[3], rb_ct *buf )
1181 {
1182 v3f delta, co;
1183
1184 #ifdef RIGIDBODY_DYNAMIC_MESH_EDGES
1185 closest_on_triangle_1( rba->co, tri, co );
1186 #else
1187 enum contact_type type = closest_on_triangle_1( rba->co, tri, co );
1188 #endif
1189
1190 v3_sub( rba->co, co, delta );
1191
1192 float d2 = v3_length2( delta ),
1193 r = rba->inf.sphere.radius;
1194
1195 if( d2 < r*r )
1196 {
1197 rb_ct *ct = buf;
1198
1199 v3f ab, ac, tn;
1200 v3_sub( tri[2], tri[0], ab );
1201 v3_sub( tri[1], tri[0], ac );
1202 v3_cross( ac, ab, tn );
1203 v3_copy( tn, ct->n );
1204
1205 if( v3_length2( ct->n ) <= 0.00001f )
1206 {
1207 vg_error( "Zero area triangle!\n" );
1208 return 0;
1209 }
1210
1211 v3_normalize( ct->n );
1212
1213 float d = sqrtf(d2);
1214
1215 v3_copy( co, ct->co );
1216 ct->type = type;
1217 ct->p = r-d;
1218 ct->rba = rba;
1219 ct->rbb = rbb;
1220 return 1;
1221 }
1222
1223 return 0;
1224 }
1225 #endif
1226
1227 VG_STATIC int rb_sphere__triangle( m4x3f mtxA, rb_sphere *b,
1228 v3f tri[3], rb_ct *buf )
1229 {
1230 v3f delta, co;
1231 enum contact_type type = closest_on_triangle_1( mtxA[3], tri, co );
1232
1233 v3_sub( mtxA[3], co, delta );
1234
1235 float d2 = v3_length2( delta ),
1236 r = b->radius;
1237
1238 if( d2 <= r*r ){
1239 rb_ct *ct = buf;
1240
1241 v3f ab, ac, tn;
1242 v3_sub( tri[2], tri[0], ab );
1243 v3_sub( tri[1], tri[0], ac );
1244 v3_cross( ac, ab, tn );
1245 v3_copy( tn, ct->n );
1246
1247 if( v3_length2( ct->n ) <= 0.00001f ){
1248 vg_error( "Zero area triangle!\n" );
1249 return 0;
1250 }
1251
1252 v3_normalize( ct->n );
1253
1254 float d = sqrtf(d2);
1255
1256 v3_copy( co, ct->co );
1257 ct->type = type;
1258 ct->p = r-d;
1259 return 1;
1260 }
1261
1262 return 0;
1263 }
1264
1265 VG_STATIC int rb_sphere__scene( m4x3f mtxA, rb_sphere *b,
1266 m4x3f mtxB, rb_scene *s, rb_ct *buf )
1267 {
1268 scene *sc = s->bh_scene->user;
1269
1270 bh_iter it;
1271 bh_iter_init( 0, &it );
1272 int idx;
1273
1274 int count = 0;
1275
1276 float r = b->radius + 0.1f;
1277 boxf box;
1278 v3_sub( mtxA[3], (v3f){ r,r,r }, box[0] );
1279 v3_add( mtxA[3], (v3f){ r,r,r }, box[1] );
1280
1281 while( bh_next( s->bh_scene, &it, box, &idx ) ){
1282 u32 *ptri = &sc->arrindices[ idx*3 ];
1283 v3f tri[3];
1284
1285 for( int j=0; j<3; j++ )
1286 v3_copy( sc->arrvertices[ptri[j]].co, tri[j] );
1287
1288 buf[ count ].element_id = ptri[0];
1289
1290 vg_line( tri[0],tri[1],0x70ff6000 );
1291 vg_line( tri[1],tri[2],0x70ff6000 );
1292 vg_line( tri[2],tri[0],0x70ff6000 );
1293
1294 int contact = rb_sphere__triangle( mtxA, b, tri, &buf[count] );
1295 count += contact;
1296
1297 if( count == 16 ){
1298 vg_warn( "Exceeding sphere_vs_scene capacity. Geometry too dense!\n" );
1299 return count;
1300 }
1301 }
1302
1303 return count;
1304 }
1305
1306 VG_STATIC int rb_box__scene( m4x3f mtxA, boxf bbx,
1307 m4x3f mtxB, rb_scene *s, rb_ct *buf )
1308 {
1309 #if 1
1310 scene *sc = s->bh_scene->user;
1311 v3f tri[3];
1312
1313 v3f extent, center;
1314 v3_sub( bbx[1], bbx[0], extent );
1315 v3_muls( extent, 0.5f, extent );
1316 v3_add( bbx[0], extent, center );
1317
1318 float r = v3_length(extent);
1319 boxf world_bbx;
1320 v3_fill( world_bbx[0], -r );
1321 v3_fill( world_bbx[1], r );
1322 for( int i=0; i<2; i++ ){
1323 v3_add( center, world_bbx[i], world_bbx[i] );
1324 v3_add( mtxA[3], world_bbx[i], world_bbx[i] );
1325 }
1326
1327 m4x3f to_local;
1328 m4x3_invert_affine( mtxA, to_local );
1329
1330 bh_iter it;
1331 bh_iter_init( 0, &it );
1332 int idx;
1333 int count = 0;
1334
1335 vg_line_boxf( world_bbx, VG__RED );
1336
1337 while( bh_next( s->bh_scene, &it, world_bbx, &idx ) ){
1338 u32 *ptri = &sc->arrindices[ idx*3 ];
1339
1340 for( int j=0; j<3; j++ )
1341 v3_copy( sc->arrvertices[ptri[j]].co, tri[j] );
1342
1343 if( rb_box_triangle_sat( extent, center, to_local, tri ) ){
1344 vg_line(tri[0],tri[1],0xff50ff00 );
1345 vg_line(tri[1],tri[2],0xff50ff00 );
1346 vg_line(tri[2],tri[0],0xff50ff00 );
1347 }
1348 else{
1349 vg_line(tri[0],tri[1],0xff0000ff );
1350 vg_line(tri[1],tri[2],0xff0000ff );
1351 vg_line(tri[2],tri[0],0xff0000ff );
1352 continue;
1353 }
1354
1355 v3f v0,v1,n;
1356 v3_sub( tri[1], tri[0], v0 );
1357 v3_sub( tri[2], tri[0], v1 );
1358 v3_cross( v0, v1, n );
1359 v3_normalize( n );
1360
1361 /* find best feature */
1362 float best = v3_dot( mtxA[0], n );
1363 int axis = 0;
1364
1365 for( int i=1; i<3; i++ ){
1366 float c = v3_dot( mtxA[i], n );
1367
1368 if( fabsf(c) > fabsf(best) ){
1369 best = c;
1370 axis = i;
1371 }
1372 }
1373
1374 v3f manifold[4];
1375
1376 if( axis == 0 ){
1377 float px = best > 0.0f? bbx[0][0]: bbx[1][0];
1378 manifold[0][0] = px;
1379 manifold[0][1] = bbx[0][1];
1380 manifold[0][2] = bbx[0][2];
1381 manifold[1][0] = px;
1382 manifold[1][1] = bbx[1][1];
1383 manifold[1][2] = bbx[0][2];
1384 manifold[2][0] = px;
1385 manifold[2][1] = bbx[1][1];
1386 manifold[2][2] = bbx[1][2];
1387 manifold[3][0] = px;
1388 manifold[3][1] = bbx[0][1];
1389 manifold[3][2] = bbx[1][2];
1390 }
1391 else if( axis == 1 ){
1392 float py = best > 0.0f? bbx[0][1]: bbx[1][1];
1393 manifold[0][0] = bbx[0][0];
1394 manifold[0][1] = py;
1395 manifold[0][2] = bbx[0][2];
1396 manifold[1][0] = bbx[1][0];
1397 manifold[1][1] = py;
1398 manifold[1][2] = bbx[0][2];
1399 manifold[2][0] = bbx[1][0];
1400 manifold[2][1] = py;
1401 manifold[2][2] = bbx[1][2];
1402 manifold[3][0] = bbx[0][0];
1403 manifold[3][1] = py;
1404 manifold[3][2] = bbx[1][2];
1405 }
1406 else{
1407 float pz = best > 0.0f? bbx[0][2]: bbx[1][2];
1408 manifold[0][0] = bbx[0][0];
1409 manifold[0][1] = bbx[0][1];
1410 manifold[0][2] = pz;
1411 manifold[1][0] = bbx[1][0];
1412 manifold[1][1] = bbx[0][1];
1413 manifold[1][2] = pz;
1414 manifold[2][0] = bbx[1][0];
1415 manifold[2][1] = bbx[1][1];
1416 manifold[2][2] = pz;
1417 manifold[3][0] = bbx[0][0];
1418 manifold[3][1] = bbx[1][1];
1419 manifold[3][2] = pz;
1420 }
1421
1422 for( int j=0; j<4; j++ )
1423 m4x3_mulv( mtxA, manifold[j], manifold[j] );
1424
1425 vg_line( manifold[0], manifold[1], 0xffffffff );
1426 vg_line( manifold[1], manifold[2], 0xffffffff );
1427 vg_line( manifold[2], manifold[3], 0xffffffff );
1428 vg_line( manifold[3], manifold[0], 0xffffffff );
1429
1430 for( int j=0; j<4; j++ ){
1431 rb_ct *ct = buf+count;
1432
1433 v3_copy( manifold[j], ct->co );
1434 v3_copy( n, ct->n );
1435
1436 float l0 = v3_dot( tri[0], n ),
1437 l1 = v3_dot( manifold[j], n );
1438
1439 ct->p = (l0-l1)*0.5f;
1440 if( ct->p < 0.0f )
1441 continue;
1442
1443 ct->type = k_contact_type_default;
1444 count ++;
1445
1446 if( count >= 12 )
1447 return count;
1448 }
1449 }
1450 return count;
1451 #else
1452
1453 scene *sc = s->bh_scene->user;
1454 v3f tri[3];
1455
1456 v3f extent, center;
1457 v3_sub( bbx[1], bbx[0], extent );
1458 v3_muls( extent, 0.5f, extent );
1459 v3_add( bbx[0], extent, center );
1460
1461 float r = v3_length(extent);
1462 boxf world_bbx;
1463 v3_fill( world_bbx[0], -r );
1464 v3_fill( world_bbx[1], r );
1465 for( int i=0; i<2; i++ ){
1466 v3_add( center, world_bbx[i], world_bbx[i] );
1467 v3_add( mtxA[3], world_bbx[i], world_bbx[i] );
1468 }
1469
1470 m4x3f to_local;
1471 m4x3_invert_affine( mtxA, to_local );
1472
1473 bh_iter it;
1474 bh_iter_init( 0, &it );
1475 int idx;
1476 int count = 0;
1477
1478 vg_line_boxf( world_bbx, VG__RED );
1479
1480 while( bh_next( s->bh_scene, &it, world_bbx, &idx ) ){
1481 u32 *ptri = &sc->arrindices[ idx*3 ];
1482
1483 for( int j=0; j<3; j++ )
1484 v3_copy( sc->arrvertices[ptri[j]].co, tri[j] );
1485
1486 vg_line( tri[0],tri[1],VG__BLACK );
1487 vg_line( tri[1],tri[2],VG__BLACK );
1488 vg_line( tri[2],tri[0],VG__BLACK );
1489
1490 v3f clip[2][8];
1491 u32 clip_length = 0;
1492 }
1493
1494 #endif
1495 }
1496
1497 VG_STATIC int rb_capsule__triangle( m4x3f mtxA, rb_capsule *c,
1498 v3f tri[3], rb_ct *buf )
1499 {
1500 v3f pc, p0w, p1w;
1501 v3_muladds( mtxA[3], mtxA[1], -c->height*0.5f+c->radius, p0w );
1502 v3_muladds( mtxA[3], mtxA[1], c->height*0.5f-c->radius, p1w );
1503
1504 capsule_manifold manifold;
1505 rb_capsule_manifold_init( &manifold );
1506
1507 v3f c0, c1;
1508 closest_on_triangle_1( p0w, tri, c0 );
1509 closest_on_triangle_1( p1w, tri, c1 );
1510
1511 v3f d0, d1, da;
1512 v3_sub( c0, p0w, d0 );
1513 v3_sub( c1, p1w, d1 );
1514 v3_sub( p1w, p0w, da );
1515
1516 v3_normalize(d0);
1517 v3_normalize(d1);
1518 v3_normalize(da);
1519
1520 if( v3_dot( da, d0 ) <= 0.01f )
1521 rb_capsule_manifold( p0w, c0, 0.0f, c->radius, &manifold );
1522
1523 if( v3_dot( da, d1 ) >= -0.01f )
1524 rb_capsule_manifold( p1w, c1, 1.0f, c->radius, &manifold );
1525
1526 for( int i=0; i<3; i++ ){
1527 int i0 = i,
1528 i1 = (i+1)%3;
1529
1530 v3f ca, cb;
1531 float ta, tb;
1532 closest_segment_segment( p0w, p1w, tri[i0], tri[i1], &ta, &tb, ca, cb );
1533 rb_capsule_manifold( ca, cb, ta, c->radius, &manifold );
1534 }
1535
1536 v3f v0, v1, n;
1537 v3_sub( tri[1], tri[0], v0 );
1538 v3_sub( tri[2], tri[0], v1 );
1539 v3_cross( v0, v1, n );
1540 v3_normalize( n );
1541
1542 int count = rb_capsule__manifold_done( mtxA, c, &manifold, buf );
1543 for( int i=0; i<count; i++ )
1544 v3_copy( n, buf[i].n );
1545
1546 return count;
1547 }
1548
1549 /* mtxB is defined only for tradition; it is not used currently */
1550 VG_STATIC int rb_capsule__scene( m4x3f mtxA, rb_capsule *c,
1551 m4x3f mtxB, rb_scene *s,
1552 rb_ct *buf )
1553 {
1554 bh_iter it;
1555 bh_iter_init( 0, &it );
1556 int idx;
1557 int count = 0;
1558
1559 boxf bbx;
1560 v3_sub( mtxA[3], (v3f){ c->height, c->height, c->height }, bbx[0] );
1561 v3_add( mtxA[3], (v3f){ c->height, c->height, c->height }, bbx[1] );
1562
1563 scene *sc = s->bh_scene->user;
1564
1565 while( bh_next( s->bh_scene, &it, bbx, &idx ) ){
1566 u32 *ptri = &sc->arrindices[ idx*3 ];
1567 v3f tri[3];
1568
1569 for( int j=0; j<3; j++ )
1570 v3_copy( sc->arrvertices[ptri[j]].co, tri[j] );
1571
1572 buf[ count ].element_id = ptri[0];
1573
1574 int contact = rb_capsule__triangle( mtxA, c, tri, &buf[count] );
1575 count += contact;
1576
1577 if( count >= 16 ){
1578 vg_warn("Exceeding capsule_vs_scene capacity. Geometry too dense!\n");
1579 return count;
1580 }
1581 }
1582
1583 return count;
1584 }
1585
1586 VG_STATIC int rb_global_has_space( void )
1587 {
1588 if( rb_contact_count + 16 > vg_list_size(rb_contact_buffer) )
1589 return 0;
1590
1591 return 1;
1592 }
1593
1594 VG_STATIC rb_ct *rb_global_buffer( void )
1595 {
1596 return &rb_contact_buffer[ rb_contact_count ];
1597 }
1598
1599 /*
1600 * -----------------------------------------------------------------------------
1601 * Dynamics
1602 * -----------------------------------------------------------------------------
1603 */
1604
1605 VG_STATIC void rb_solver_reset(void)
1606 {
1607 rb_contact_count = 0;
1608 }
1609
1610 VG_STATIC rb_ct *rb_global_ct(void)
1611 {
1612 return rb_contact_buffer + rb_contact_count;
1613 }
1614
1615 VG_STATIC void rb_prepare_contact( rb_ct *ct, float timestep )
1616 {
1617 ct->bias = -0.2f * (timestep*3600.0f)
1618 * vg_minf( 0.0f, -ct->p+k_penetration_slop );
1619
1620 rb_tangent_basis( ct->n, ct->t[0], ct->t[1] );
1621 ct->norm_impulse = 0.0f;
1622 ct->tangent_impulse[0] = 0.0f;
1623 ct->tangent_impulse[1] = 0.0f;
1624 }
1625
1626 /* calculate total move. manifold should belong to ONE object only */
1627 VG_STATIC void rb_depenetrate( rb_ct *manifold, int len, v3f dt )
1628 {
1629 v3_zero( dt );
1630
1631 for( int j=0; j<7; j++ )
1632 {
1633 for( int i=0; i<len; i++ )
1634 {
1635 struct contact *ct = &manifold[i];
1636
1637 float resolved_amt = v3_dot( ct->n, dt ),
1638 remaining = (ct->p-k_penetration_slop) - resolved_amt,
1639 apply = vg_maxf( remaining, 0.0f ) * 0.4f;
1640
1641 v3_muladds( dt, ct->n, apply, dt );
1642 }
1643 }
1644 }
1645
1646 /*
1647 * Initializing things like tangent vectors
1648 */
1649 VG_STATIC void rb_presolve_contacts( rb_ct *buffer, int len )
1650 {
1651 for( int i=0; i<len; i++ ){
1652 rb_ct *ct = &buffer[i];
1653 rb_prepare_contact( ct, k_rb_delta );
1654
1655 v3f ra, rb, raCn, rbCn, raCt, rbCt;
1656 v3_sub( ct->co, ct->rba->co, ra );
1657 v3_sub( ct->co, ct->rbb->co, rb );
1658 v3_cross( ra, ct->n, raCn );
1659 v3_cross( rb, ct->n, rbCn );
1660
1661 /* orient inverse inertia tensors */
1662 v3f raCnI, rbCnI;
1663 m3x3_mulv( ct->rba->iIw, raCn, raCnI );
1664 m3x3_mulv( ct->rbb->iIw, rbCn, rbCnI );
1665
1666 ct->normal_mass = ct->rba->inv_mass + ct->rbb->inv_mass;
1667 ct->normal_mass += v3_dot( raCn, raCnI );
1668 ct->normal_mass += v3_dot( rbCn, rbCnI );
1669 ct->normal_mass = 1.0f/ct->normal_mass;
1670
1671 for( int j=0; j<2; j++ ){
1672 v3f raCtI, rbCtI;
1673 v3_cross( ct->t[j], ra, raCt );
1674 v3_cross( ct->t[j], rb, rbCt );
1675 m3x3_mulv( ct->rba->iIw, raCt, raCtI );
1676 m3x3_mulv( ct->rbb->iIw, rbCt, rbCtI );
1677
1678 ct->tangent_mass[j] = ct->rba->inv_mass + ct->rbb->inv_mass;
1679 ct->tangent_mass[j] += v3_dot( raCt, raCtI );
1680 ct->tangent_mass[j] += v3_dot( rbCt, rbCtI );
1681 ct->tangent_mass[j] = 1.0f/ct->tangent_mass[j];
1682 }
1683
1684 rb_debug_contact( ct );
1685 }
1686 }
1687
1688 /*
1689 * Creates relative contact velocity vector
1690 */
1691 VG_STATIC void rb_rcv( rigidbody *rba, rigidbody *rbb, v3f ra, v3f rb, v3f rv )
1692 {
1693 v3f rva, rvb;
1694 v3_cross( rba->w, ra, rva );
1695 v3_add( rba->v, rva, rva );
1696 v3_cross( rbb->w, rb, rvb );
1697 v3_add( rbb->v, rvb, rvb );
1698
1699 v3_sub( rva, rvb, rv );
1700 }
1701
1702 VG_STATIC void rb_contact_restitution( rb_ct *ct, float cr )
1703 {
1704 v3f rv, ra, rb;
1705 v3_sub( ct->co, ct->rba->co, ra );
1706 v3_sub( ct->co, ct->rbb->co, rb );
1707 rb_rcv( ct->rba, ct->rbb, ra, rb, rv );
1708
1709 float v = v3_dot( rv, ct->n );
1710
1711 if( v < -1.0f ){
1712 ct->bias += -cr * v;
1713 }
1714 }
1715
1716 /*
1717 * Apply impulse to object
1718 */
1719 VG_STATIC void rb_linear_impulse( rigidbody *rb, v3f delta, v3f impulse )
1720 {
1721 /* linear */
1722 v3_muladds( rb->v, impulse, rb->inv_mass, rb->v );
1723
1724 /* Angular velocity */
1725 v3f wa;
1726 v3_cross( delta, impulse, wa );
1727
1728 m3x3_mulv( rb->iIw, wa, wa );
1729 v3_add( rb->w, wa, rb->w );
1730 }
1731
1732 /*
1733 * One iteration to solve the contact constraint
1734 */
1735 VG_STATIC void rb_solve_contacts( rb_ct *buf, int len )
1736 {
1737 for( int i=0; i<len; i++ ){
1738 struct contact *ct = &buf[i];
1739
1740 v3f rv, ra, rb;
1741 v3_sub( ct->co, ct->rba->co, ra );
1742 v3_sub( ct->co, ct->rbb->co, rb );
1743 rb_rcv( ct->rba, ct->rbb, ra, rb, rv );
1744
1745 /* Friction */
1746 for( int j=0; j<2; j++ ){
1747 float f = k_friction * ct->norm_impulse,
1748 vt = v3_dot( rv, ct->t[j] ),
1749 lambda = ct->tangent_mass[j] * -vt;
1750
1751 float temp = ct->tangent_impulse[j];
1752 ct->tangent_impulse[j] = vg_clampf( temp + lambda, -f, f );
1753 lambda = ct->tangent_impulse[j] - temp;
1754
1755 v3f impulse;
1756 v3_muls( ct->t[j], lambda, impulse );
1757 rb_linear_impulse( ct->rba, ra, impulse );
1758
1759 v3_muls( ct->t[j], -lambda, impulse );
1760 rb_linear_impulse( ct->rbb, rb, impulse );
1761 }
1762
1763 /* Normal */
1764 rb_rcv( ct->rba, ct->rbb, ra, rb, rv );
1765 float vn = v3_dot( rv, ct->n ),
1766 lambda = ct->normal_mass * (-vn + ct->bias);
1767
1768 float temp = ct->norm_impulse;
1769 ct->norm_impulse = vg_maxf( temp + lambda, 0.0f );
1770 lambda = ct->norm_impulse - temp;
1771
1772 v3f impulse;
1773 v3_muls( ct->n, lambda, impulse );
1774 rb_linear_impulse( ct->rba, ra, impulse );
1775
1776 v3_muls( ct->n, -lambda, impulse );
1777 rb_linear_impulse( ct->rbb, rb, impulse );
1778 }
1779 }
1780
1781 /*
1782 * -----------------------------------------------------------------------------
1783 * Constraints
1784 * -----------------------------------------------------------------------------
1785 */
1786
1787 VG_STATIC void rb_debug_position_constraints( rb_constr_pos *buffer, int len )
1788 {
1789 for( int i=0; i<len; i++ ){
1790 rb_constr_pos *constr = &buffer[i];
1791 rigidbody *rba = constr->rba, *rbb = constr->rbb;
1792
1793 v3f wca, wcb;
1794 m3x3_mulv( rba->to_world, constr->lca, wca );
1795 m3x3_mulv( rbb->to_world, constr->lcb, wcb );
1796
1797 v3f p0, p1;
1798 v3_add( wca, rba->co, p0 );
1799 v3_add( wcb, rbb->co, p1 );
1800 vg_line_pt3( p0, 0.0025f, 0xff000000 );
1801 vg_line_pt3( p1, 0.0025f, 0xffffffff );
1802 vg_line2( p0, p1, 0xff000000, 0xffffffff );
1803 }
1804 }
1805
1806 VG_STATIC void rb_presolve_swingtwist_constraints( rb_constr_swingtwist *buf,
1807 int len )
1808 {
1809 float size = 0.12f;
1810
1811 for( int i=0; i<len; i++ ){
1812 rb_constr_swingtwist *st = &buf[ i ];
1813
1814 v3f vx, vy, va, vxb, axis, center;
1815
1816 m3x3_mulv( st->rba->to_world, st->conevx, vx );
1817 m3x3_mulv( st->rbb->to_world, st->conevxb, vxb );
1818 m3x3_mulv( st->rba->to_world, st->conevy, vy );
1819 m3x3_mulv( st->rbb->to_world, st->coneva, va );
1820 m4x3_mulv( st->rba->to_world, st->view_offset, center );
1821 v3_cross( vy, vx, axis );
1822
1823 /* Constraint violated ? */
1824 float fx = v3_dot( vx, va ), /* projection world */
1825 fy = v3_dot( vy, va ),
1826 fn = v3_dot( va, axis ),
1827
1828 rx = st->conevx[3], /* elipse radii */
1829 ry = st->conevy[3],
1830
1831 lx = fx/rx, /* projection local (fn==lz) */
1832 ly = fy/ry;
1833
1834 st->tangent_violation = ((lx*lx + ly*ly) > fn*fn) || (fn <= 0.0f);
1835 if( st->tangent_violation ){
1836 /* Calculate a good position and the axis to solve on */
1837 v2f closest, tangent,
1838 p = { fx/fabsf(fn), fy/fabsf(fn) };
1839
1840 closest_point_elipse( p, (v2f){rx,ry}, closest );
1841 tangent[0] = -closest[1] / (ry*ry);
1842 tangent[1] = closest[0] / (rx*rx);
1843 v2_normalize( tangent );
1844
1845 v3f v0, v1;
1846 v3_muladds( axis, vx, closest[0], v0 );
1847 v3_muladds( v0, vy, closest[1], v0 );
1848 v3_normalize( v0 );
1849
1850 v3_muls( vx, tangent[0], v1 );
1851 v3_muladds( v1, vy, tangent[1], v1 );
1852
1853 v3_copy( v0, st->tangent_target );
1854 v3_copy( v1, st->tangent_axis );
1855
1856 /* calculate mass */
1857 v3f aIw, bIw;
1858 m3x3_mulv( st->rba->iIw, st->tangent_axis, aIw );
1859 m3x3_mulv( st->rbb->iIw, st->tangent_axis, bIw );
1860 st->tangent_mass = 1.0f / (v3_dot( st->tangent_axis, aIw ) +
1861 v3_dot( st->tangent_axis, bIw ));
1862
1863 float angle = v3_dot( va, st->tangent_target );
1864 }
1865
1866 v3f refaxis;
1867 v3_cross( vy, va, refaxis ); /* our default rotation */
1868 v3_normalize( refaxis );
1869
1870 float angle = v3_dot( refaxis, vxb );
1871 st->axis_violation = fabsf(angle) < st->conet;
1872
1873 if( st->axis_violation ){
1874 v3f dir_test;
1875 v3_cross( refaxis, vxb, dir_test );
1876
1877 if( v3_dot(dir_test, va) < 0.0f )
1878 st->axis_violation = -st->axis_violation;
1879
1880 float newang = (float)st->axis_violation * acosf(st->conet-0.0001f);
1881
1882 v3f refaxis_up;
1883 v3_cross( va, refaxis, refaxis_up );
1884 v3_muls( refaxis_up, sinf(newang), st->axis_target );
1885 v3_muladds( st->axis_target, refaxis, -cosf(newang), st->axis_target );
1886
1887 /* calculate mass */
1888 v3_copy( va, st->axis );
1889 v3f aIw, bIw;
1890 m3x3_mulv( st->rba->iIw, st->axis, aIw );
1891 m3x3_mulv( st->rbb->iIw, st->axis, bIw );
1892 st->axis_mass = 1.0f / (v3_dot( st->axis, aIw ) +
1893 v3_dot( st->axis, bIw ));
1894 }
1895 }
1896 }
1897
1898 VG_STATIC void rb_debug_swingtwist_constraints( rb_constr_swingtwist *buf,
1899 int len )
1900 {
1901 float size = 0.12f;
1902
1903 for( int i=0; i<len; i++ ){
1904 rb_constr_swingtwist *st = &buf[ i ];
1905
1906 v3f vx, vxb, vy, va, axis, center;
1907
1908 m3x3_mulv( st->rba->to_world, st->conevx, vx );
1909 m3x3_mulv( st->rbb->to_world, st->conevxb, vxb );
1910 m3x3_mulv( st->rba->to_world, st->conevy, vy );
1911 m3x3_mulv( st->rbb->to_world, st->coneva, va );
1912 m4x3_mulv( st->rba->to_world, st->view_offset, center );
1913 v3_cross( vy, vx, axis );
1914
1915 float rx = st->conevx[3], /* elipse radii */
1916 ry = st->conevy[3];
1917
1918 v3f p0, p1;
1919 v3_muladds( center, va, size, p1 );
1920 vg_line( center, p1, 0xffffffff );
1921 vg_line_pt3( p1, 0.00025f, 0xffffffff );
1922
1923 if( st->tangent_violation ){
1924 v3_muladds( center, st->tangent_target, size, p0 );
1925
1926 vg_line( center, p0, 0xff00ff00 );
1927 vg_line_pt3( p0, 0.00025f, 0xff00ff00 );
1928 vg_line( p1, p0, 0xff000000 );
1929 }
1930
1931 for( int x=0; x<32; x++ ){
1932 float t0 = ((float)x * (1.0f/32.0f)) * VG_TAUf,
1933 t1 = (((float)x+1.0f) * (1.0f/32.0f)) * VG_TAUf,
1934 c0 = cosf( t0 ),
1935 s0 = sinf( t0 ),
1936 c1 = cosf( t1 ),
1937 s1 = sinf( t1 );
1938
1939 v3f v0, v1;
1940 v3_muladds( axis, vx, c0*rx, v0 );
1941 v3_muladds( v0, vy, s0*ry, v0 );
1942 v3_muladds( axis, vx, c1*rx, v1 );
1943 v3_muladds( v1, vy, s1*ry, v1 );
1944
1945 v3_normalize( v0 );
1946 v3_normalize( v1 );
1947
1948 v3_muladds( center, v0, size, p0 );
1949 v3_muladds( center, v1, size, p1 );
1950
1951 u32 col0r = fabsf(c0) * 255.0f,
1952 col0g = fabsf(s0) * 255.0f,
1953 col1r = fabsf(c1) * 255.0f,
1954 col1g = fabsf(s1) * 255.0f,
1955 col = st->tangent_violation? 0xff0000ff: 0xff000000,
1956 col0 = col | (col0r<<16) | (col0g << 8),
1957 col1 = col | (col1r<<16) | (col1g << 8);
1958
1959 vg_line2( center, p0, VG__NONE, col0 );
1960 vg_line2( p0, p1, col0, col1 );
1961 }
1962
1963 /* Draw twist */
1964 v3_muladds( center, va, size, p0 );
1965 v3_muladds( p0, vxb, size, p1 );
1966
1967 vg_line( p0, p1, 0xff0000ff );
1968
1969 if( st->axis_violation ){
1970 v3_muladds( p0, st->axis_target, size*1.25f, p1 );
1971 vg_line( p0, p1, 0xffffff00 );
1972 vg_line_pt3( p1, 0.0025f, 0xffffff80 );
1973 }
1974
1975 v3f refaxis;
1976 v3_cross( vy, va, refaxis ); /* our default rotation */
1977 v3_normalize( refaxis );
1978 v3f refaxis_up;
1979 v3_cross( va, refaxis, refaxis_up );
1980 float newang = acosf(st->conet-0.0001f);
1981
1982 v3_muladds( p0, refaxis_up, sinf(newang)*size, p1 );
1983 v3_muladds( p1, refaxis, -cosf(newang)*size, p1 );
1984 vg_line( p0, p1, 0xff000000 );
1985
1986 v3_muladds( p0, refaxis_up, sinf(-newang)*size, p1 );
1987 v3_muladds( p1, refaxis, -cosf(-newang)*size, p1 );
1988 vg_line( p0, p1, 0xff404040 );
1989 }
1990 }
1991
1992 /*
1993 * Solve a list of positional constraints
1994 */
1995 VG_STATIC void rb_solve_position_constraints( rb_constr_pos *buf, int len )
1996 {
1997 for( int i=0; i<len; i++ ){
1998 rb_constr_pos *constr = &buf[i];
1999 rigidbody *rba = constr->rba, *rbb = constr->rbb;
2000
2001 v3f wa, wb;
2002 m3x3_mulv( rba->to_world, constr->lca, wa );
2003 m3x3_mulv( rbb->to_world, constr->lcb, wb );
2004
2005 m3x3f ssra, ssrat, ssrb, ssrbt;
2006
2007 m3x3_skew_symetric( ssrat, wa );
2008 m3x3_skew_symetric( ssrbt, wb );
2009 m3x3_transpose( ssrat, ssra );
2010 m3x3_transpose( ssrbt, ssrb );
2011
2012 v3f b, b_wa, b_wb, b_a, b_b;
2013 m3x3_mulv( ssra, rba->w, b_wa );
2014 m3x3_mulv( ssrb, rbb->w, b_wb );
2015 v3_add( rba->v, b_wa, b );
2016 v3_sub( b, rbb->v, b );
2017 v3_sub( b, b_wb, b );
2018 v3_muls( b, -1.0f, b );
2019
2020 m3x3f invMa, invMb;
2021 m3x3_diagonal( invMa, rba->inv_mass );
2022 m3x3_diagonal( invMb, rbb->inv_mass );
2023
2024 m3x3f ia, ib;
2025 m3x3_mul( ssra, rba->iIw, ia );
2026 m3x3_mul( ia, ssrat, ia );
2027 m3x3_mul( ssrb, rbb->iIw, ib );
2028 m3x3_mul( ib, ssrbt, ib );
2029
2030 m3x3f cma, cmb;
2031 m3x3_add( invMa, ia, cma );
2032 m3x3_add( invMb, ib, cmb );
2033
2034 m3x3f A;
2035 m3x3_add( cma, cmb, A );
2036
2037 /* Solve Ax = b ( A^-1*b = x ) */
2038 v3f impulse;
2039 m3x3f invA;
2040 m3x3_inv( A, invA );
2041 m3x3_mulv( invA, b, impulse );
2042
2043 v3f delta_va, delta_wa, delta_vb, delta_wb;
2044 m3x3f iwa, iwb;
2045 m3x3_mul( rba->iIw, ssrat, iwa );
2046 m3x3_mul( rbb->iIw, ssrbt, iwb );
2047
2048 m3x3_mulv( invMa, impulse, delta_va );
2049 m3x3_mulv( invMb, impulse, delta_vb );
2050 m3x3_mulv( iwa, impulse, delta_wa );
2051 m3x3_mulv( iwb, impulse, delta_wb );
2052
2053 v3_add( rba->v, delta_va, rba->v );
2054 v3_add( rba->w, delta_wa, rba->w );
2055 v3_sub( rbb->v, delta_vb, rbb->v );
2056 v3_sub( rbb->w, delta_wb, rbb->w );
2057 }
2058 }
2059
2060 VG_STATIC void rb_solve_swingtwist_constraints( rb_constr_swingtwist *buf,
2061 int len )
2062 {
2063 float size = 0.12f;
2064
2065 for( int i=0; i<len; i++ ){
2066 rb_constr_swingtwist *st = &buf[ i ];
2067
2068 if( !st->axis_violation )
2069 continue;
2070
2071 float rv = v3_dot( st->axis, st->rbb->w ) -
2072 v3_dot( st->axis, st->rba->w );
2073
2074 if( rv * (float)st->axis_violation > 0.0f )
2075 continue;
2076
2077 v3f impulse, wa, wb;
2078 v3_muls( st->axis, rv*st->axis_mass, impulse );
2079 m3x3_mulv( st->rba->iIw, impulse, wa );
2080 v3_add( st->rba->w, wa, st->rba->w );
2081
2082 v3_muls( impulse, -1.0f, impulse );
2083 m3x3_mulv( st->rbb->iIw, impulse, wb );
2084 v3_add( st->rbb->w, wb, st->rbb->w );
2085
2086 float rv2 = v3_dot( st->axis, st->rbb->w ) -
2087 v3_dot( st->axis, st->rba->w );
2088 }
2089
2090 for( int i=0; i<len; i++ ){
2091 rb_constr_swingtwist *st = &buf[ i ];
2092
2093 if( !st->tangent_violation )
2094 continue;
2095
2096 float rv = v3_dot( st->tangent_axis, st->rbb->w ) -
2097 v3_dot( st->tangent_axis, st->rba->w );
2098
2099 if( rv > 0.0f )
2100 continue;
2101
2102 v3f impulse, wa, wb;
2103 v3_muls( st->tangent_axis, rv*st->tangent_mass, impulse );
2104 m3x3_mulv( st->rba->iIw, impulse, wa );
2105 v3_add( st->rba->w, wa, st->rba->w );
2106
2107 v3_muls( impulse, -1.0f, impulse );
2108 m3x3_mulv( st->rbb->iIw, impulse, wb );
2109 v3_add( st->rbb->w, wb, st->rbb->w );
2110
2111 float rv2 = v3_dot( st->tangent_axis, st->rbb->w ) -
2112 v3_dot( st->tangent_axis, st->rba->w );
2113 }
2114 }
2115
2116 VG_STATIC void rb_solve_constr_angle( rigidbody *rba, rigidbody *rbb,
2117 v3f ra, v3f rb )
2118 {
2119 m3x3f ssra, ssrb, ssrat, ssrbt;
2120 m3x3f cma, cmb;
2121
2122 m3x3_skew_symetric( ssrat, ra );
2123 m3x3_skew_symetric( ssrbt, rb );
2124 m3x3_transpose( ssrat, ssra );
2125 m3x3_transpose( ssrbt, ssrb );
2126
2127 m3x3_mul( ssra, rba->iIw, cma );
2128 m3x3_mul( cma, ssrat, cma );
2129 m3x3_mul( ssrb, rbb->iIw, cmb );
2130 m3x3_mul( cmb, ssrbt, cmb );
2131
2132 m3x3f A, invA;
2133 m3x3_add( cma, cmb, A );
2134 m3x3_inv( A, invA );
2135
2136 v3f b_wa, b_wb, b;
2137 m3x3_mulv( ssra, rba->w, b_wa );
2138 m3x3_mulv( ssrb, rbb->w, b_wb );
2139 v3_add( b_wa, b_wb, b );
2140 v3_negate( b, b );
2141
2142 v3f impulse;
2143 m3x3_mulv( invA, b, impulse );
2144
2145 v3f delta_wa, delta_wb;
2146 m3x3f iwa, iwb;
2147 m3x3_mul( rba->iIw, ssrat, iwa );
2148 m3x3_mul( rbb->iIw, ssrbt, iwb );
2149 m3x3_mulv( iwa, impulse, delta_wa );
2150 m3x3_mulv( iwb, impulse, delta_wb );
2151 v3_add( rba->w, delta_wa, rba->w );
2152 v3_sub( rbb->w, delta_wb, rbb->w );
2153 }
2154
2155 /*
2156 * Correct position constraint drift errors
2157 * [ 0.0 <= amt <= 1.0 ]: the correction amount
2158 */
2159 VG_STATIC void rb_correct_position_constraints( rb_constr_pos *buf, int len,
2160 float amt )
2161 {
2162 for( int i=0; i<len; i++ ){
2163 rb_constr_pos *constr = &buf[i];
2164 rigidbody *rba = constr->rba, *rbb = constr->rbb;
2165
2166 v3f p0, p1, d;
2167 m3x3_mulv( rba->to_world, constr->lca, p0 );
2168 m3x3_mulv( rbb->to_world, constr->lcb, p1 );
2169 v3_add( rba->co, p0, p0 );
2170 v3_add( rbb->co, p1, p1 );
2171 v3_sub( p1, p0, d );
2172
2173 v3_muladds( rbb->co, d, -1.0f * amt, rbb->co );
2174 rb_update_transform( rbb );
2175 }
2176 }
2177
2178 VG_STATIC void rb_correct_swingtwist_constraints( rb_constr_swingtwist *buf,
2179 int len, float amt )
2180 {
2181 for( int i=0; i<len; i++ ){
2182 rb_constr_swingtwist *st = &buf[i];
2183
2184 if( !st->tangent_violation )
2185 continue;
2186
2187 v3f va;
2188 m3x3_mulv( st->rbb->to_world, st->coneva, va );
2189
2190 float angle = v3_dot( va, st->tangent_target );
2191
2192 if( fabsf(angle) < 0.9999f ){
2193 v3f axis;
2194 v3_cross( va, st->tangent_target, axis );
2195
2196 v4f correction;
2197 q_axis_angle( correction, axis, acosf(angle) * amt );
2198 q_mul( correction, st->rbb->q, st->rbb->q );
2199 rb_update_transform( st->rbb );
2200 }
2201 }
2202
2203 for( int i=0; i<len; i++ ){
2204 rb_constr_swingtwist *st = &buf[i];
2205
2206 if( !st->axis_violation )
2207 continue;
2208
2209 v3f vxb;
2210 m3x3_mulv( st->rbb->to_world, st->conevxb, vxb );
2211
2212 float angle = v3_dot( vxb, st->axis_target );
2213
2214 if( fabsf(angle) < 0.9999f ){
2215 v3f axis;
2216 v3_cross( vxb, st->axis_target, axis );
2217
2218 v4f correction;
2219 q_axis_angle( correction, axis, acosf(angle) * amt );
2220 q_mul( correction, st->rbb->q, st->rbb->q );
2221 rb_update_transform( st->rbb );
2222 }
2223 }
2224 }
2225
2226 VG_STATIC void rb_correct_contact_constraints( rb_ct *buf, int len, float amt )
2227 {
2228 for( int i=0; i<len; i++ ){
2229 rb_ct *ct = &buf[i];
2230 rigidbody *rba = ct->rba,
2231 *rbb = ct->rbb;
2232
2233 float mass_total = 1.0f / (rba->inv_mass + rbb->inv_mass);
2234
2235 v3_muladds( rba->co, ct->n, -mass_total * rba->inv_mass, rba->co );
2236 v3_muladds( rbb->co, ct->n, mass_total * rbb->inv_mass, rbb->co );
2237 }
2238 }
2239
2240
2241 /*
2242 * Effectors
2243 */
2244
2245 VG_STATIC void rb_effect_simple_bouyency( rigidbody *ra, v4f plane,
2246 float amt, float drag )
2247 {
2248 /* float */
2249 float depth = v3_dot( plane, ra->co ) - plane[3],
2250 lambda = vg_clampf( -depth, 0.0f, 1.0f ) * amt;
2251
2252 v3_muladds( ra->v, plane, lambda * k_rb_delta, ra->v );
2253
2254 if( depth < 0.0f )
2255 v3_muls( ra->v, 1.0f-(drag*k_rb_delta), ra->v );
2256 }
2257
2258 /* apply a spring&dampener force to match ra(worldspace) on rigidbody, to
2259 * rt(worldspace)
2260 */
2261 VG_STATIC void rb_effect_spring_target_vector( rigidbody *rba, v3f ra, v3f rt,
2262 float spring, float dampening,
2263 float timestep )
2264 {
2265 float d = v3_dot( rt, ra );
2266 float a = vg_signf( d ) * acosf( vg_clampf( d, -1.0f, 1.0f ) );
2267
2268 v3f axis;
2269 v3_cross( rt, ra, axis );
2270
2271 float Fs = -a * spring,
2272 Fd = -v3_dot( rba->w, axis ) * dampening;
2273
2274 v3_muladds( rba->w, axis, (Fs+Fd) * timestep, rba->w );
2275 }
2276
2277 #endif /* RIGIDBODY_H */