2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
6 * Resources: Box2D - Erin Catto
16 VG_STATIC
void rb_tangent_basis( v3f n
, v3f tx
, v3f ty
);
17 VG_STATIC bh_system bh_system_rigidbodies
;
25 * -----------------------------------------------------------------------------
27 * -----------------------------------------------------------------------------
31 k_rb_rate
= (1.0/VG_TIMESTEP_FIXED
),
32 k_rb_delta
= (1.0/k_rb_rate
),
34 k_damp_linear
= 0.1f
, /* scale velocity 1/(1+x) */
35 k_damp_angular
= 0.1f
, /* scale angular 1/(1+x) */
36 k_penetration_slop
= 0.01f
,
37 k_inertia_scale
= 8.0f
,
38 k_phys_baumgarte
= 0.2f
,
43 k_joint_correction
= 0.01f
,
44 k_joint_impulse
= 1.0f
,
45 k_joint_bias
= 0.08f
; /* positional joints */
47 VG_STATIC
void rb_register_cvar(void)
49 vg_var_push( (struct vg_var
){
50 .name
= "k_limit_bias", .data
= &k_limit_bias
,
51 .data_type
= k_var_dtype_f32
, .opt_f32
= {.clamp
= 0}, .persistent
= 1
54 vg_var_push( (struct vg_var
){
55 .name
= "k_joint_bias", .data
= &k_joint_bias
,
56 .data_type
= k_var_dtype_f32
, .opt_f32
= {.clamp
= 0}, .persistent
= 1
59 vg_var_push( (struct vg_var
){
60 .name
= "k_joint_correction", .data
= &k_joint_correction
,
61 .data_type
= k_var_dtype_f32
, .opt_f32
= {.clamp
= 0}, .persistent
= 1
64 vg_var_push( (struct vg_var
){
65 .name
= "k_joint_impulse", .data
= &k_joint_impulse
,
66 .data_type
= k_var_dtype_f32
, .opt_f32
= {.clamp
= 0}, .persistent
= 1
71 * -----------------------------------------------------------------------------
72 * structure definitions
73 * -----------------------------------------------------------------------------
76 typedef struct rigidbody rigidbody
;
77 typedef struct contact rb_ct
;
78 typedef struct rb_sphere rb_sphere
;
79 typedef struct rb_capsule rb_capsule
;
80 typedef struct rb_scene rb_scene
;
105 k_rb_shape_sphere
= 1,
106 k_rb_shape_capsule
= 2,
113 struct rb_sphere sphere
;
114 struct rb_capsule capsule
;
115 struct rb_scene scene
;
119 v3f right
, up
, forward
;
126 /* inertia model and inverse world tensor */
130 m4x3f to_world
, to_local
;
133 VG_STATIC
struct contact
135 rigidbody
*rba
, *rbb
;
138 float p
, bias
, norm_impulse
, tangent_impulse
[2],
139 normal_mass
, tangent_mass
[2];
143 enum contact_type type
;
145 rb_contact_buffer
[256];
146 VG_STATIC
int rb_contact_count
= 0;
148 typedef struct rb_constr_pos rb_constr_pos
;
149 typedef struct rb_constr_swingtwist rb_constr_swingtwist
;
153 rigidbody
*rba
, *rbb
;
157 struct rb_constr_swingtwist
159 rigidbody
*rba
, *rbb
;
161 v4f conevx
, conevy
; /* relative to rba */
162 v3f view_offset
, /* relative to rba */
163 coneva
, conevxb
;/* relative to rbb */
165 int tangent_violation
, axis_violation
;
166 v3f axis
, tangent_axis
, tangent_target
, axis_target
;
169 float tangent_mass
, axis_mass
;
172 struct rb_constr_spring
178 * -----------------------------------------------------------------------------
180 * -----------------------------------------------------------------------------
183 VG_STATIC
float sphere_volume( float radius
)
185 float r3
= radius
*radius
*radius
;
186 return (4.0f
/3.0f
) * VG_PIf
* r3
;
189 VG_STATIC
void rb_tangent_basis( v3f n
, v3f tx
, v3f ty
)
191 /* Compute tangent basis (box2d) */
192 if( fabsf( n
[0] ) >= 0.57735027f
)
206 v3_cross( n
, tx
, ty
);
210 * -----------------------------------------------------------------------------
212 * -----------------------------------------------------------------------------
215 VG_STATIC
void rb_debug_contact( rb_ct
*ct
)
217 if( ct
->type
!= k_contact_type_disabled
)
220 v3_muladds( ct
->co
, ct
->n
, 0.05f
, p1
);
221 vg_line_pt3( ct
->co
, 0.0125f
, 0xff0000ff );
222 vg_line( ct
->co
, p1
, 0xffffffff );
226 VG_STATIC
void debug_sphere( m4x3f m
, float radius
, u32 colour
)
228 v3f ly
= { 0.0f
, 0.0f
, radius
},
229 lx
= { 0.0f
, radius
, 0.0f
},
230 lz
= { 0.0f
, 0.0f
, radius
};
232 for( int i
=0; i
<16; i
++ )
234 float t
= ((float)(i
+1) * (1.0f
/16.0f
)) * VG_PIf
* 2.0f
,
238 v3f py
= { s
*radius
, 0.0f
, c
*radius
},
239 px
= { s
*radius
, c
*radius
, 0.0f
},
240 pz
= { 0.0f
, s
*radius
, c
*radius
};
242 v3f p0
, p1
, p2
, p3
, p4
, p5
;
243 m4x3_mulv( m
, py
, p0
);
244 m4x3_mulv( m
, ly
, p1
);
245 m4x3_mulv( m
, px
, p2
);
246 m4x3_mulv( m
, lx
, p3
);
247 m4x3_mulv( m
, pz
, p4
);
248 m4x3_mulv( m
, lz
, p5
);
250 vg_line( p0
, p1
, colour
== 0x00? 0xff00ff00: colour
);
251 vg_line( p2
, p3
, colour
== 0x00? 0xff0000ff: colour
);
252 vg_line( p4
, p5
, colour
== 0x00? 0xffff0000: colour
);
260 VG_STATIC
void debug_capsule( m4x3f m
, float radius
, float h
, u32 colour
)
262 v3f ly
= { 0.0f
, 0.0f
, radius
},
263 lx
= { 0.0f
, radius
, 0.0f
},
264 lz
= { 0.0f
, 0.0f
, radius
};
266 float s0
= sinf(0.0f
)*radius
,
267 c0
= cosf(0.0f
)*radius
;
269 v3f p0
, p1
, up
, right
, forward
;
270 m3x3_mulv( m
, (v3f
){0.0f
,1.0f
,0.0f
}, up
);
271 m3x3_mulv( m
, (v3f
){1.0f
,0.0f
,0.0f
}, right
);
272 m3x3_mulv( m
, (v3f
){0.0f
,0.0f
,-1.0f
}, forward
);
273 v3_muladds( m
[3], up
, -h
*0.5f
+radius
, p0
);
274 v3_muladds( m
[3], up
, h
*0.5f
-radius
, p1
);
277 v3_muladds( p0
, right
, radius
, a0
);
278 v3_muladds( p1
, right
, radius
, a1
);
279 v3_muladds( p0
, forward
, radius
, b0
);
280 v3_muladds( p1
, forward
, radius
, b1
);
281 vg_line( a0
, a1
, colour
);
282 vg_line( b0
, b1
, colour
);
284 v3_muladds( p0
, right
, -radius
, a0
);
285 v3_muladds( p1
, right
, -radius
, a1
);
286 v3_muladds( p0
, forward
, -radius
, b0
);
287 v3_muladds( p1
, forward
, -radius
, b1
);
288 vg_line( a0
, a1
, colour
);
289 vg_line( b0
, b1
, colour
);
291 for( int i
=0; i
<16; i
++ )
293 float t
= ((float)(i
+1) * (1.0f
/16.0f
)) * VG_PIf
* 2.0f
,
297 v3f e0
= { s0
, 0.0f
, c0
},
298 e1
= { s1
, 0.0f
, c1
},
299 e2
= { s0
, c0
, 0.0f
},
300 e3
= { s1
, c1
, 0.0f
},
301 e4
= { 0.0f
, c0
, s0
},
302 e5
= { 0.0f
, c1
, s1
};
304 m3x3_mulv( m
, e0
, e0
);
305 m3x3_mulv( m
, e1
, e1
);
306 m3x3_mulv( m
, e2
, e2
);
307 m3x3_mulv( m
, e3
, e3
);
308 m3x3_mulv( m
, e4
, e4
);
309 m3x3_mulv( m
, e5
, e5
);
311 v3_add( p0
, e0
, a0
);
312 v3_add( p0
, e1
, a1
);
313 v3_add( p1
, e0
, b0
);
314 v3_add( p1
, e1
, b1
);
316 vg_line( a0
, a1
, colour
);
317 vg_line( b0
, b1
, colour
);
321 v3_add( p0
, e2
, a0
);
322 v3_add( p0
, e3
, a1
);
323 v3_add( p0
, e4
, b0
);
324 v3_add( p0
, e5
, b1
);
328 v3_add( p1
, e2
, a0
);
329 v3_add( p1
, e3
, a1
);
330 v3_add( p1
, e4
, b0
);
331 v3_add( p1
, e5
, b1
);
334 vg_line( a0
, a1
, colour
);
335 vg_line( b0
, b1
, colour
);
342 VG_STATIC
void rb_debug( rigidbody
*rb
, u32 colour
)
344 if( rb
->type
== k_rb_shape_box
)
347 vg_line_boxf_transformed( rb
->to_world
, rb
->bbx
, colour
);
349 else if( rb
->type
== k_rb_shape_sphere
)
351 debug_sphere( rb
->to_world
, rb
->inf
.sphere
.radius
, colour
);
353 else if( rb
->type
== k_rb_shape_capsule
)
356 float h
= rb
->inf
.capsule
.height
,
357 r
= rb
->inf
.capsule
.radius
;
359 debug_capsule( rb
->to_world
, r
, h
, colour
);
361 else if( rb
->type
== k_rb_shape_scene
)
363 vg_line_boxf( rb
->bbx
, colour
);
368 * -----------------------------------------------------------------------------
370 * -----------------------------------------------------------------------------
374 * Update world space bounding box based on local one
376 VG_STATIC
void rb_update_bounds( rigidbody
*rb
)
378 box_copy( rb
->bbx
, rb
->bbx_world
);
379 m4x3_transform_aabb( rb
->to_world
, rb
->bbx_world
);
383 * Commit transform to rigidbody. Updates matrices
385 VG_STATIC
void rb_update_transform( rigidbody
*rb
)
387 q_normalize( rb
->q
);
388 q_m3x3( rb
->q
, rb
->to_world
);
389 v3_copy( rb
->co
, rb
->to_world
[3] );
391 m4x3_invert_affine( rb
->to_world
, rb
->to_local
);
393 m3x3_mulv( rb
->to_world
, (v3f
){1.0f
,0.0f
, 0.0f
}, rb
->right
);
394 m3x3_mulv( rb
->to_world
, (v3f
){0.0f
,1.0f
, 0.0f
}, rb
->up
);
395 m3x3_mulv( rb
->to_world
, (v3f
){0.0f
,0.0f
,-1.0f
}, rb
->forward
);
397 m3x3_mul( rb
->iI
, rb
->to_local
, rb
->iIw
);
398 m3x3_mul( rb
->to_world
, rb
->iIw
, rb
->iIw
);
400 rb_update_bounds( rb
);
404 * Extrapolate rigidbody into a transform based on vg accumulator.
405 * Useful for rendering
407 VG_STATIC
void rb_extrapolate_transform( rigidbody
*rb
, m4x3f transform
)
409 float substep
= vg_clampf( vg
.accumulator
/ k_rb_delta
, 0.0f
, 1.0f
);
414 v3_muladds( rb
->co
, rb
->v
, k_rb_delta
*substep
, co
);
416 if( v3_length2( rb
->w
) > 0.0f
)
420 v3_copy( rb
->w
, axis
);
422 float mag
= v3_length( axis
);
423 v3_divs( axis
, mag
, axis
);
424 q_axis_angle( rotation
, axis
, mag
*k_rb_delta
*substep
);
425 q_mul( rotation
, rb
->q
, q
);
433 q_m3x3( q
, transform
);
434 v3_copy( co
, transform
[3] );
438 * Initialize rigidbody and calculate masses, inertia
440 VG_STATIC
void rb_init( rigidbody
*rb
)
444 if( rb
->type
== k_rb_shape_box
)
447 v3_sub( rb
->bbx
[1], rb
->bbx
[0], dims
);
448 volume
= dims
[0]*dims
[1]*dims
[2];
450 else if( rb
->type
== k_rb_shape_sphere
)
452 volume
= sphere_volume( rb
->inf
.sphere
.radius
);
453 v3_fill( rb
->bbx
[0], -rb
->inf
.sphere
.radius
);
454 v3_fill( rb
->bbx
[1], rb
->inf
.sphere
.radius
);
456 else if( rb
->type
== k_rb_shape_capsule
)
458 float r
= rb
->inf
.capsule
.radius
,
459 h
= rb
->inf
.capsule
.height
;
460 volume
= sphere_volume( r
) + VG_PIf
* r
*r
* (h
- r
*2.0f
);
462 v3_fill( rb
->bbx
[0], -r
);
463 v3_fill( rb
->bbx
[1], r
);
467 else if( rb
->type
== k_rb_shape_scene
)
470 box_copy( rb
->inf
.scene
.bh_scene
->nodes
[0].bbx
, rb
->bbx
);
481 float mass
= 2.0f
*volume
;
482 rb
->inv_mass
= 1.0f
/mass
;
485 v3_sub( rb
->bbx
[1], rb
->bbx
[0], extent
);
486 v3_muls( extent
, 0.5f
, extent
);
488 /* local intertia tensor */
489 float scale
= k_inertia_scale
;
490 float ex2
= scale
*extent
[0]*extent
[0],
491 ey2
= scale
*extent
[1]*extent
[1],
492 ez2
= scale
*extent
[2]*extent
[2];
494 rb
->I
[0] = ((1.0f
/12.0f
) * mass
* (ey2
+ez2
));
495 rb
->I
[1] = ((1.0f
/12.0f
) * mass
* (ex2
+ez2
));
496 rb
->I
[2] = ((1.0f
/12.0f
) * mass
* (ex2
+ey2
));
498 m3x3_identity( rb
->iI
);
499 rb
->iI
[0][0] = rb
->I
[0];
500 rb
->iI
[1][1] = rb
->I
[1];
501 rb
->iI
[2][2] = rb
->I
[2];
502 m3x3_inv( rb
->iI
, rb
->iI
);
508 rb_update_transform( rb
);
511 VG_STATIC
void rb_iter( rigidbody
*rb
)
513 if( !vg_validf( rb
->v
[0] ) ||
514 !vg_validf( rb
->v
[1] ) ||
515 !vg_validf( rb
->v
[2] ) )
517 vg_fatal_exit_loop( "NaN velocity" );
520 v3f gravity
= { 0.0f
, -9.8f
, 0.0f
};
521 v3_muladds( rb
->v
, gravity
, k_rb_delta
, rb
->v
);
523 /* intergrate velocity */
524 v3_muladds( rb
->co
, rb
->v
, k_rb_delta
, rb
->co
);
525 v3_lerp( rb
->w
, (v3f
){0.0f
,0.0f
,0.0f
}, 0.0025f
, rb
->w
);
527 /* inegrate inertia */
528 if( v3_length2( rb
->w
) > 0.0f
)
532 v3_copy( rb
->w
, axis
);
534 float mag
= v3_length( axis
);
535 v3_divs( axis
, mag
, axis
);
536 q_axis_angle( rotation
, axis
, mag
*k_rb_delta
);
537 q_mul( rotation
, rb
->q
, rb
->q
);
541 v3_muls( rb
->v
, 1.0f
/(1.0f
+k_rb_delta
*k_damp_linear
), rb
->v
);
542 v3_muls( rb
->w
, 1.0f
/(1.0f
+k_rb_delta
*k_damp_angular
), rb
->w
);
547 * -----------------------------------------------------------------------------
548 * Boolean shape overlap functions
549 * -----------------------------------------------------------------------------
553 * Project AABB, and triangle interval onto axis to check if they overlap
555 VG_STATIC
int rb_box_triangle_interval( v3f extent
, v3f axis
, v3f tri
[3] )
559 r
= extent
[0] * fabsf(axis
[0]) +
560 extent
[1] * fabsf(axis
[1]) +
561 extent
[2] * fabsf(axis
[2]),
563 p0
= v3_dot( axis
, tri
[0] ),
564 p1
= v3_dot( axis
, tri
[1] ),
565 p2
= v3_dot( axis
, tri
[2] ),
567 e
= vg_maxf(-vg_maxf(p0
,vg_maxf(p1
,p2
)), vg_minf(p0
,vg_minf(p1
,p2
)));
569 if( e
> r
) return 0;
574 * Seperating axis test box vs triangle
576 VG_STATIC
int rb_box_triangle_sat( rigidbody
*rba
, v3f tri_src
[3] )
581 v3_sub( rba
->bbx
[1], rba
->bbx
[0], extent
);
582 v3_muls( extent
, 0.5f
, extent
);
583 v3_add( rba
->bbx
[0], extent
, c
);
585 for( int i
=0; i
<3; i
++ )
587 m4x3_mulv( rba
->to_local
, tri_src
[i
], tri
[i
] );
588 v3_sub( tri
[i
], c
, tri
[i
] );
592 if(!rb_box_triangle_interval( extent
, (v3f
){1.0f
,0.0f
,0.0f
}, tri
)) return 0;
593 if(!rb_box_triangle_interval( extent
, (v3f
){0.0f
,1.0f
,0.0f
}, tri
)) return 0;
594 if(!rb_box_triangle_interval( extent
, (v3f
){0.0f
,0.0f
,1.0f
}, tri
)) return 0;
596 v3f v0
,v1
,v2
,n
, e0
,e1
,e2
;
597 v3_sub( tri
[1], tri
[0], v0
);
598 v3_sub( tri
[2], tri
[0], v1
);
599 v3_sub( tri
[2], tri
[1], v2
);
603 v3_cross( v0
, v1
, n
);
604 v3_cross( v0
, n
, e0
);
605 v3_cross( n
, v1
, e1
);
606 v3_cross( v2
, n
, e2
);
609 if(!rb_box_triangle_interval( extent
, n
, tri
)) return 0;
612 v3_cross( e0
, (v3f
){1.0f
,0.0f
,0.0f
}, axis
[0] );
613 v3_cross( e0
, (v3f
){0.0f
,1.0f
,0.0f
}, axis
[1] );
614 v3_cross( e0
, (v3f
){0.0f
,0.0f
,1.0f
}, axis
[2] );
615 v3_cross( e1
, (v3f
){1.0f
,0.0f
,0.0f
}, axis
[3] );
616 v3_cross( e1
, (v3f
){0.0f
,1.0f
,0.0f
}, axis
[4] );
617 v3_cross( e1
, (v3f
){0.0f
,0.0f
,1.0f
}, axis
[5] );
618 v3_cross( e2
, (v3f
){1.0f
,0.0f
,0.0f
}, axis
[6] );
619 v3_cross( e2
, (v3f
){0.0f
,1.0f
,0.0f
}, axis
[7] );
620 v3_cross( e2
, (v3f
){0.0f
,0.0f
,1.0f
}, axis
[8] );
622 for( int i
=0; i
<9; i
++ )
623 if(!rb_box_triangle_interval( extent
, axis
[i
], tri
)) return 0;
629 * -----------------------------------------------------------------------------
631 * -----------------------------------------------------------------------------
634 VG_STATIC
int rb_manifold_apply_filtered( rb_ct
*man
, int len
)
638 for( int i
=0; i
<len
; i
++ )
642 if( ct
->type
== k_contact_type_disabled
)
652 * Merge two contacts if they are within radius(r) of eachother
654 VG_STATIC
void rb_manifold_contact_weld( rb_ct
*ci
, rb_ct
*cj
, float r
)
656 if( v3_dist2( ci
->co
, cj
->co
) < r
*r
)
658 cj
->type
= k_contact_type_disabled
;
659 ci
->p
= (ci
->p
+ cj
->p
) * 0.5f
;
661 v3_add( ci
->co
, cj
->co
, ci
->co
);
662 v3_muls( ci
->co
, 0.5f
, ci
->co
);
665 v3_sub( ci
->rba
->co
, ci
->co
, delta
);
667 float c0
= v3_dot( ci
->n
, delta
),
668 c1
= v3_dot( cj
->n
, delta
);
670 if( c0
< 0.0f
|| c1
< 0.0f
)
673 ci
->type
= k_contact_type_disabled
;
678 v3_muls( ci
->n
, c0
, n
);
679 v3_muladds( n
, cj
->n
, c1
, n
);
689 VG_STATIC
void rb_manifold_filter_joint_edges( rb_ct
*man
, int len
, float r
)
691 for( int i
=0; i
<len
-1; i
++ )
694 if( ci
->type
!= k_contact_type_edge
)
697 for( int j
=i
+1; j
<len
; j
++ )
700 if( cj
->type
!= k_contact_type_edge
)
703 rb_manifold_contact_weld( ci
, cj
, r
);
709 * Resolve overlapping pairs
713 VG_STATIC
void rb_manifold_filter_pairs( rb_ct
*man
, int len
, float r
)
715 for( int i
=0; i
<len
-1; i
++ )
720 if( ci
->type
== k_contact_type_disabled
) continue;
722 for( int j
=i
+1; j
<len
; j
++ )
726 if( cj
->type
== k_contact_type_disabled
) continue;
728 if( v3_dist2( ci
->co
, cj
->co
) < r
*r
)
730 cj
->type
= k_contact_type_disabled
;
731 v3_add( cj
->n
, ci
->n
, ci
->n
);
739 float n
= 1.0f
/((float)similar
+1.0f
);
740 v3_muls( ci
->n
, n
, ci
->n
);
743 if( v3_length2(ci
->n
) < 0.1f
*0.1f
)
744 ci
->type
= k_contact_type_disabled
;
746 v3_normalize( ci
->n
);
752 * Remove contacts that are facing away from A
754 VG_STATIC
void rb_manifold_filter_backface( rb_ct
*man
, int len
)
756 for( int i
=0; i
<len
; i
++ )
759 if( ct
->type
== k_contact_type_disabled
)
763 v3_sub( ct
->co
, ct
->rba
->co
, delta
);
765 if( v3_dot( delta
, ct
->n
) > -0.001f
)
766 ct
->type
= k_contact_type_disabled
;
771 * Filter out duplicate coplanar results. Good for spheres.
773 VG_STATIC
void rb_manifold_filter_coplanar( rb_ct
*man
, int len
, float w
)
775 for( int i
=0; i
<len
; i
++ )
778 if( ci
->type
== k_contact_type_disabled
||
779 ci
->type
== k_contact_type_edge
)
782 float d1
= v3_dot( ci
->co
, ci
->n
);
784 for( int j
=0; j
<len
; j
++ )
790 if( cj
->type
== k_contact_type_disabled
)
793 float d2
= v3_dot( cj
->co
, ci
->n
),
796 if( fabsf( d
) <= w
)
798 cj
->type
= k_contact_type_disabled
;
805 * -----------------------------------------------------------------------------
807 * -----------------------------------------------------------------------------
813 * These do not automatically allocate contacts, an appropriately sized
814 * buffer must be supplied. The function returns the size of the manifold
815 * which was generated.
817 * The values set on the contacts are: n, co, p, rba, rbb
821 * By collecting the minimum(time) and maximum(time) pairs of points, we
822 * build a reduced and stable exact manifold.
825 * rx: minimum distance of these points
826 * dx: the delta between the two points
828 * pairs will only ammend these if they are creating a collision
830 typedef struct capsule_manifold capsule_manifold
;
831 struct capsule_manifold
839 * Expand a line manifold with a new pair. t value is the time along segment
840 * on the oriented object which created this pair.
842 VG_STATIC
void rb_capsule_manifold( v3f pa
, v3f pb
, float t
, float r
,
843 capsule_manifold
*manifold
)
846 v3_sub( pa
, pb
, delta
);
848 if( v3_length2(delta
) < r
*r
)
850 if( t
< manifold
->t0
)
852 v3_copy( delta
, manifold
->d0
);
857 if( t
> manifold
->t1
)
859 v3_copy( delta
, manifold
->d1
);
866 VG_STATIC
void rb_capsule_manifold_init( capsule_manifold
*manifold
)
868 manifold
->t0
= INFINITY
;
869 manifold
->t1
= -INFINITY
;
872 __attribute__ ((deprecated
))
873 VG_STATIC
int rb_capsule_manifold_done( rigidbody
*rba
, rigidbody
*rbb
,
874 capsule_manifold
*manifold
, rb_ct
*buf
)
876 float h
= rba
->inf
.capsule
.height
,
877 ra
= rba
->inf
.capsule
.radius
;
880 v3_muladds( rba
->co
, rba
->up
, -h
*0.5f
+ra
, p0
);
881 v3_muladds( rba
->co
, rba
->up
, h
*0.5f
-ra
, p1
);
884 if( manifold
->t0
<= 1.0f
)
889 v3_muls( p0
, 1.0f
-manifold
->t0
, pa
);
890 v3_muladds( pa
, p1
, manifold
->t0
, pa
);
892 float d
= v3_length( manifold
->d0
);
893 v3_muls( manifold
->d0
, 1.0f
/d
, ct
->n
);
894 v3_muladds( pa
, ct
->n
, -ra
, ct
->co
);
896 ct
->p
= manifold
->r0
- d
;
899 ct
->type
= k_contact_type_default
;
904 if( (manifold
->t1
>= 0.0f
) && (manifold
->t0
!= manifold
->t1
) )
906 rb_ct
*ct
= buf
+count
;
909 v3_muls( p0
, 1.0f
-manifold
->t1
, pa
);
910 v3_muladds( pa
, p1
, manifold
->t1
, pa
);
912 float d
= v3_length( manifold
->d1
);
913 v3_muls( manifold
->d1
, 1.0f
/d
, ct
->n
);
914 v3_muladds( pa
, ct
->n
, -ra
, ct
->co
);
916 ct
->p
= manifold
->r1
- d
;
919 ct
->type
= k_contact_type_default
;
929 vg_line( buf
[0].co
, buf
[1].co
, 0xff0000ff );
934 VG_STATIC
int rb_capsule__manifold_done( m4x3f mtx
, rb_capsule
*c
,
935 capsule_manifold
*manifold
,
939 v3_muladds( mtx
[3], mtx
[1], -c
->height
*0.5f
+c
->radius
, p0
);
940 v3_muladds( mtx
[3], mtx
[1], c
->height
*0.5f
-c
->radius
, p1
);
943 if( manifold
->t0
<= 1.0f
)
948 v3_muls( p0
, 1.0f
-manifold
->t0
, pa
);
949 v3_muladds( pa
, p1
, manifold
->t0
, pa
);
951 float d
= v3_length( manifold
->d0
);
952 v3_muls( manifold
->d0
, 1.0f
/d
, ct
->n
);
953 v3_muladds( pa
, ct
->n
, -c
->radius
, ct
->co
);
955 ct
->p
= manifold
->r0
- d
;
956 ct
->type
= k_contact_type_default
;
960 if( (manifold
->t1
>= 0.0f
) && (manifold
->t0
!= manifold
->t1
) )
962 rb_ct
*ct
= buf
+count
;
965 v3_muls( p0
, 1.0f
-manifold
->t1
, pa
);
966 v3_muladds( pa
, p1
, manifold
->t1
, pa
);
968 float d
= v3_length( manifold
->d1
);
969 v3_muls( manifold
->d1
, 1.0f
/d
, ct
->n
);
970 v3_muladds( pa
, ct
->n
, -c
->radius
, ct
->co
);
972 ct
->p
= manifold
->r1
- d
;
973 ct
->type
= k_contact_type_default
;
983 vg_line( buf
[0].co
, buf
[1].co
, 0xff0000ff );
988 VG_STATIC
int rb_capsule_sphere( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
990 float h
= rba
->inf
.capsule
.height
,
991 ra
= rba
->inf
.capsule
.radius
,
992 rb
= rbb
->inf
.sphere
.radius
;
995 v3_muladds( rba
->co
, rba
->up
, -h
*0.5f
+ra
, p0
);
996 v3_muladds( rba
->co
, rba
->up
, h
*0.5f
-ra
, p1
);
999 closest_point_segment( p0
, p1
, rbb
->co
, c
);
1000 v3_sub( c
, rbb
->co
, delta
);
1002 float d2
= v3_length2(delta
),
1007 float d
= sqrtf(d2
);
1010 v3_muls( delta
, 1.0f
/d
, ct
->n
);
1014 v3_muladds( c
, ct
->n
, -ra
, p0
);
1015 v3_muladds( rbb
->co
, ct
->n
, rb
, p1
);
1016 v3_add( p0
, p1
, ct
->co
);
1017 v3_muls( ct
->co
, 0.5f
, ct
->co
);
1021 ct
->type
= k_contact_type_default
;
1029 VG_STATIC
int rb_capsule_capsule( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1031 if( !box_overlap( rba
->bbx_world
, rbb
->bbx_world
) )
1034 float ha
= rba
->inf
.capsule
.height
,
1035 hb
= rbb
->inf
.capsule
.height
,
1036 ra
= rba
->inf
.capsule
.radius
,
1037 rb
= rbb
->inf
.capsule
.radius
,
1041 v3_muladds( rba
->co
, rba
->up
, -ha
*0.5f
+ra
, p0
);
1042 v3_muladds( rba
->co
, rba
->up
, ha
*0.5f
-ra
, p1
);
1043 v3_muladds( rbb
->co
, rbb
->up
, -hb
*0.5f
+rb
, p2
);
1044 v3_muladds( rbb
->co
, rbb
->up
, hb
*0.5f
-rb
, p3
);
1046 capsule_manifold manifold
;
1047 rb_capsule_manifold_init( &manifold
);
1051 closest_segment_segment( p0
, p1
, p2
, p3
, &ta
, &tb
, pa
, pb
);
1052 rb_capsule_manifold( pa
, pb
, ta
, r
, &manifold
);
1054 ta
= closest_point_segment( p0
, p1
, p2
, pa
);
1055 tb
= closest_point_segment( p0
, p1
, p3
, pb
);
1056 rb_capsule_manifold( pa
, p2
, ta
, r
, &manifold
);
1057 rb_capsule_manifold( pb
, p3
, tb
, r
, &manifold
);
1059 closest_point_segment( p2
, p3
, p0
, pa
);
1060 closest_point_segment( p2
, p3
, p1
, pb
);
1061 rb_capsule_manifold( p0
, pa
, 0.0f
, r
, &manifold
);
1062 rb_capsule_manifold( p1
, pb
, 1.0f
, r
, &manifold
);
1064 return rb_capsule_manifold_done( rba
, rbb
, &manifold
, buf
);
1068 * Generates up to two contacts; optimised for the most stable manifold
1070 VG_STATIC
int rb_capsule_box( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1072 float h
= rba
->inf
.capsule
.height
,
1073 r
= rba
->inf
.capsule
.radius
;
1076 * Solving this in symetric local space of the cube saves us some time and a
1077 * couple branches when it comes to the quad stage.
1080 v3_add( rbb
->bbx
[0], rbb
->bbx
[1], centroid
);
1081 v3_muls( centroid
, 0.5f
, centroid
);
1084 v3_sub( rbb
->bbx
[0], centroid
, bbx
[0] );
1085 v3_sub( rbb
->bbx
[1], centroid
, bbx
[1] );
1087 v3f pc
, p0w
, p1w
, p0
, p1
;
1088 v3_muladds( rba
->co
, rba
->up
, -h
*0.5f
+r
, p0w
);
1089 v3_muladds( rba
->co
, rba
->up
, h
*0.5f
-r
, p1w
);
1091 m4x3_mulv( rbb
->to_local
, p0w
, p0
);
1092 m4x3_mulv( rbb
->to_local
, p1w
, p1
);
1093 v3_sub( p0
, centroid
, p0
);
1094 v3_sub( p1
, centroid
, p1
);
1095 v3_add( p0
, p1
, pc
);
1096 v3_muls( pc
, 0.5f
, pc
);
1099 * Finding an appropriate quad to collide lines with
1102 v3_div( pc
, bbx
[1], region
);
1105 if( (fabsf(region
[0]) > fabsf(region
[1])) &&
1106 (fabsf(region
[0]) > fabsf(region
[2])) )
1108 float px
= vg_signf(region
[0]) * bbx
[1][0];
1109 v3_copy( (v3f
){ px
, bbx
[0][1], bbx
[0][2] }, quad
[0] );
1110 v3_copy( (v3f
){ px
, bbx
[1][1], bbx
[0][2] }, quad
[1] );
1111 v3_copy( (v3f
){ px
, bbx
[1][1], bbx
[1][2] }, quad
[2] );
1112 v3_copy( (v3f
){ px
, bbx
[0][1], bbx
[1][2] }, quad
[3] );
1114 else if( fabsf(region
[1]) > fabsf(region
[2]) )
1116 float py
= vg_signf(region
[1]) * bbx
[1][1];
1117 v3_copy( (v3f
){ bbx
[0][0], py
, bbx
[0][2] }, quad
[0] );
1118 v3_copy( (v3f
){ bbx
[1][0], py
, bbx
[0][2] }, quad
[1] );
1119 v3_copy( (v3f
){ bbx
[1][0], py
, bbx
[1][2] }, quad
[2] );
1120 v3_copy( (v3f
){ bbx
[0][0], py
, bbx
[1][2] }, quad
[3] );
1124 float pz
= vg_signf(region
[2]) * bbx
[1][2];
1125 v3_copy( (v3f
){ bbx
[0][0], bbx
[0][1], pz
}, quad
[0] );
1126 v3_copy( (v3f
){ bbx
[1][0], bbx
[0][1], pz
}, quad
[1] );
1127 v3_copy( (v3f
){ bbx
[1][0], bbx
[1][1], pz
}, quad
[2] );
1128 v3_copy( (v3f
){ bbx
[0][0], bbx
[1][1], pz
}, quad
[3] );
1131 capsule_manifold manifold
;
1132 rb_capsule_manifold_init( &manifold
);
1135 closest_point_aabb( p0
, bbx
, c0
);
1136 closest_point_aabb( p1
, bbx
, c1
);
1139 v3_sub( c0
, p0
, d0
);
1140 v3_sub( c1
, p1
, d1
);
1141 v3_sub( p1
, p0
, da
);
1147 if( v3_dot( da
, d0
) <= 0.01f
)
1148 rb_capsule_manifold( p0
, c0
, 0.0f
, r
, &manifold
);
1150 if( v3_dot( da
, d1
) >= -0.01f
)
1151 rb_capsule_manifold( p1
, c1
, 1.0f
, r
, &manifold
);
1153 for( int i
=0; i
<4; i
++ )
1160 closest_segment_segment( p0
, p1
, quad
[i0
], quad
[i1
], &ta
, &tb
, ca
, cb
);
1161 rb_capsule_manifold( ca
, cb
, ta
, r
, &manifold
);
1165 * Create final contacts based on line manifold
1167 m3x3_mulv( rbb
->to_world
, manifold
.d0
, manifold
.d0
);
1168 m3x3_mulv( rbb
->to_world
, manifold
.d1
, manifold
.d1
);
1175 for( int i
=0; i
<4; i
++ )
1181 v3_add( quad
[i0
], centroid
, q0
);
1182 v3_add( quad
[i1
], centroid
, q1
);
1184 m4x3_mulv( rbb
->to_world
, q0
, q0
);
1185 m4x3_mulv( rbb
->to_world
, q1
, q1
);
1187 vg_line( q0
, q1
, 0xffffffff );
1191 return rb_capsule_manifold_done( rba
, rbb
, &manifold
, buf
);
1194 VG_STATIC
int rb_sphere_box( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1198 closest_point_obb( rba
->co
, rbb
->bbx
, rbb
->to_world
, rbb
->to_local
, co
);
1199 v3_sub( rba
->co
, co
, delta
);
1201 float d2
= v3_length2(delta
),
1202 r
= rba
->inf
.sphere
.radius
;
1211 v3_sub( rba
->co
, rbb
->co
, delta
);
1214 * some extra testing is required to find the best axis to push the
1215 * object back outside the box. Since there isnt a clear seperating
1216 * vector already, especially on really high aspect boxes.
1218 float lx
= v3_dot( rbb
->right
, delta
),
1219 ly
= v3_dot( rbb
->up
, delta
),
1220 lz
= v3_dot( rbb
->forward
, delta
),
1221 px
= rbb
->bbx
[1][0] - fabsf(lx
),
1222 py
= rbb
->bbx
[1][1] - fabsf(ly
),
1223 pz
= rbb
->bbx
[1][2] - fabsf(lz
);
1225 if( px
< py
&& px
< pz
)
1226 v3_muls( rbb
->right
, vg_signf(lx
), ct
->n
);
1228 v3_muls( rbb
->up
, vg_signf(ly
), ct
->n
);
1230 v3_muls( rbb
->forward
, vg_signf(lz
), ct
->n
);
1232 v3_muladds( rba
->co
, ct
->n
, -r
, ct
->co
);
1238 v3_muls( delta
, 1.0f
/d
, ct
->n
);
1240 v3_copy( co
, ct
->co
);
1245 ct
->type
= k_contact_type_default
;
1252 VG_STATIC
int rb_sphere_sphere( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1255 v3_sub( rba
->co
, rbb
->co
, delta
);
1257 float d2
= v3_length2(delta
),
1258 r
= rba
->inf
.sphere
.radius
+ rbb
->inf
.sphere
.radius
;
1262 float d
= sqrtf(d2
);
1265 v3_muls( delta
, 1.0f
/d
, ct
->n
);
1268 v3_muladds( rba
->co
, ct
->n
,-rba
->inf
.sphere
.radius
, p0
);
1269 v3_muladds( rbb
->co
, ct
->n
, rbb
->inf
.sphere
.radius
, p1
);
1270 v3_add( p0
, p1
, ct
->co
);
1271 v3_muls( ct
->co
, 0.5f
, ct
->co
);
1272 ct
->type
= k_contact_type_default
;
1282 //#define RIGIDBODY_DYNAMIC_MESH_EDGES
1284 __attribute__ ((deprecated
))
1285 VG_STATIC
int rb_sphere_triangle( rigidbody
*rba
, rigidbody
*rbb
,
1286 v3f tri
[3], rb_ct
*buf
)
1290 #ifdef RIGIDBODY_DYNAMIC_MESH_EDGES
1291 closest_on_triangle_1( rba
->co
, tri
, co
);
1293 enum contact_type type
= closest_on_triangle_1( rba
->co
, tri
, co
);
1296 v3_sub( rba
->co
, co
, delta
);
1298 float d2
= v3_length2( delta
),
1299 r
= rba
->inf
.sphere
.radius
;
1306 v3_sub( tri
[2], tri
[0], ab
);
1307 v3_sub( tri
[1], tri
[0], ac
);
1308 v3_cross( ac
, ab
, tn
);
1309 v3_copy( tn
, ct
->n
);
1311 if( v3_length2( ct
->n
) <= 0.00001f
)
1313 vg_error( "Zero area triangle!\n" );
1317 v3_normalize( ct
->n
);
1319 float d
= sqrtf(d2
);
1321 v3_copy( co
, ct
->co
);
1332 VG_STATIC
int rb_sphere__triangle( m4x3f mtxA
, rb_sphere
*b
,
1333 v3f tri
[3], rb_ct
*buf
)
1336 enum contact_type type
= closest_on_triangle_1( mtxA
[3], tri
, co
);
1338 v3_sub( mtxA
[3], co
, delta
);
1340 float d2
= v3_length2( delta
),
1348 v3_sub( tri
[2], tri
[0], ab
);
1349 v3_sub( tri
[1], tri
[0], ac
);
1350 v3_cross( ac
, ab
, tn
);
1351 v3_copy( tn
, ct
->n
);
1353 if( v3_length2( ct
->n
) <= 0.00001f
)
1355 vg_error( "Zero area triangle!\n" );
1359 v3_normalize( ct
->n
);
1361 float d
= sqrtf(d2
);
1363 v3_copy( co
, ct
->co
);
1372 VG_STATIC
void rb_debug_sharp_scene_edges( rigidbody
*rbb
, float sharp_ang
,
1373 boxf box
, u32 colour
)
1375 sharp_ang
= cosf( sharp_ang
);
1377 scene
*sc
= rbb
->inf
.scene
.bh_scene
->user
;
1378 vg_line_boxf( box
, 0xff00ff00 );
1381 bh_iter_init( 0, &it
);
1384 while( bh_next( rbb
->inf
.scene
.bh_scene
, &it
, box
, &idx
) )
1386 u32
*ptri
= &sc
->arrindices
[ idx
*3 ];
1389 for( int j
=0; j
<3; j
++ )
1390 v3_copy( sc
->arrvertices
[ptri
[j
]].co
, tri
[j
] );
1392 for( int j
=0; j
<3; j
++ )
1396 v3_sub( tri
[(j
+1)%3], tri
[j
], edir
);
1398 if( v3_dot( edir
, (v3f
){ 0.5184758473652127f
,
1399 0.2073903389460850f
,
1400 -0.8295613557843402f
} ) < 0.0f
)
1405 bh_iter_init( 0, &jt
);
1408 float const k_r
= 0.02f
;
1409 v3_add( (v3f
){ k_r
, k_r
, k_r
}, tri
[j
], region
[1] );
1410 v3_add( (v3f
){ -k_r
, -k_r
, -k_r
}, tri
[j
], region
[0] );
1413 while( bh_next( rbb
->inf
.scene
.bh_scene
, &jt
, region
, &jdx
) )
1418 u32
*ptrj
= &sc
->arrindices
[ jdx
*3 ];
1421 for( int k
=0; k
<3; k
++ )
1422 v3_copy( sc
->arrvertices
[ptrj
[k
]].co
, trj
[k
] );
1424 for( int k
=0; k
<3; k
++ )
1426 if( v3_dist2( tri
[j
], trj
[k
] ) <= k_r
*k_r
)
1433 if( v3_dist2( tri
[jp1
], trj
[km1
] ) <= k_r
*k_r
)
1436 v3_sub( tri
[jp1
], tri
[j
], b0
);
1437 v3_sub( tri
[jp2
], tri
[j
], b1
);
1438 v3_sub( trj
[km2
], tri
[j
], b2
);
1441 v3_cross( b0
, b1
, cx0
);
1442 v3_cross( b2
, b0
, cx1
);
1444 float polarity
= v3_dot( cx0
, b2
);
1446 if( polarity
< 0.0f
)
1449 vg_line( tri
[j
], tri
[jp1
], 0xff00ff00 );
1450 float ang
= v3_dot(cx0
,cx1
) /
1451 (v3_length(cx0
)*v3_length(cx1
));
1452 if( ang
< sharp_ang
)
1454 vg_line( tri
[j
], tri
[jp1
], 0xff00ff00 );
1466 VG_STATIC
int rb_sphere__scene( m4x3f mtxA
, rb_sphere
*b
,
1467 m4x3f mtxB
, rb_scene
*s
, rb_ct
*buf
)
1469 scene
*sc
= s
->bh_scene
->user
;
1472 bh_iter_init( 0, &it
);
1477 float r
= b
->radius
;
1479 v3_sub( mtxA
[3], (v3f
){ r
,r
,r
}, box
[0] );
1480 v3_add( mtxA
[3], (v3f
){ r
,r
,r
}, box
[1] );
1482 while( bh_next( s
->bh_scene
, &it
, box
, &idx
) )
1484 u32
*ptri
= &sc
->arrindices
[ idx
*3 ];
1487 for( int j
=0; j
<3; j
++ )
1488 v3_copy( sc
->arrvertices
[ptri
[j
]].co
, tri
[j
] );
1490 buf
[ count
].element_id
= ptri
[0];
1492 vg_line( tri
[0],tri
[1],0x70ff6000 );
1493 vg_line( tri
[1],tri
[2],0x70ff6000 );
1494 vg_line( tri
[2],tri
[0],0x70ff6000 );
1496 int contact
= rb_sphere__triangle( mtxA
, b
, tri
, &buf
[count
] );
1501 vg_warn( "Exceeding sphere_vs_scene capacity. Geometry too dense!\n" );
1509 __attribute__ ((deprecated
))
1510 VG_STATIC
int rb_sphere_scene( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1512 scene
*sc
= rbb
->inf
.scene
.bh_scene
->user
;
1515 bh_iter_init( 0, &it
);
1520 while( bh_next( rbb
->inf
.scene
.bh_scene
, &it
, rba
->bbx_world
, &idx
) )
1522 u32
*ptri
= &sc
->arrindices
[ idx
*3 ];
1525 for( int j
=0; j
<3; j
++ )
1526 v3_copy( sc
->arrvertices
[ptri
[j
]].co
, tri
[j
] );
1528 buf
[ count
].element_id
= ptri
[0];
1530 vg_line( tri
[0],tri
[1],0x70ff6000 );
1531 vg_line( tri
[1],tri
[2],0x70ff6000 );
1532 vg_line( tri
[2],tri
[0],0x70ff6000 );
1534 int contact
= rb_sphere_triangle( rba
, rbb
, tri
, buf
+count
);
1539 vg_warn( "Exceeding sphere_vs_scene capacity. Geometry too dense!\n" );
1547 VG_STATIC
int rb_box_scene( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1549 scene
*sc
= rbb
->inf
.scene
.bh_scene
->user
;
1554 bh_iter_init( 0, &it
);
1559 while( bh_next( rbb
->inf
.scene
.bh_scene
, &it
, rba
->bbx_world
, &idx
) )
1561 u32
*ptri
= &sc
->arrindices
[ idx
*3 ];
1563 for( int j
=0; j
<3; j
++ )
1564 v3_copy( sc
->arrvertices
[ptri
[j
]].co
, tri
[j
] );
1566 if( rb_box_triangle_sat( rba
, tri
) )
1568 vg_line(tri
[0],tri
[1],0xff50ff00 );
1569 vg_line(tri
[1],tri
[2],0xff50ff00 );
1570 vg_line(tri
[2],tri
[0],0xff50ff00 );
1574 vg_line(tri
[0],tri
[1],0xff0000ff );
1575 vg_line(tri
[1],tri
[2],0xff0000ff );
1576 vg_line(tri
[2],tri
[0],0xff0000ff );
1582 v3_sub( tri
[1], tri
[0], v0
);
1583 v3_sub( tri
[2], tri
[0], v1
);
1584 v3_cross( v0
, v1
, n
);
1587 /* find best feature */
1588 float best
= v3_dot( rba
->right
, n
);
1591 float cy
= v3_dot( rba
->up
, n
);
1592 if( fabsf(cy
) > fabsf(best
) )
1598 float cz
= -v3_dot( rba
->forward
, n
);
1599 if( fabsf(cz
) > fabsf(best
) )
1609 float px
= best
> 0.0f
? rba
->bbx
[0][0]: rba
->bbx
[1][0];
1610 manifold
[0][0] = px
;
1611 manifold
[0][1] = rba
->bbx
[0][1];
1612 manifold
[0][2] = rba
->bbx
[0][2];
1613 manifold
[1][0] = px
;
1614 manifold
[1][1] = rba
->bbx
[1][1];
1615 manifold
[1][2] = rba
->bbx
[0][2];
1616 manifold
[2][0] = px
;
1617 manifold
[2][1] = rba
->bbx
[1][1];
1618 manifold
[2][2] = rba
->bbx
[1][2];
1619 manifold
[3][0] = px
;
1620 manifold
[3][1] = rba
->bbx
[0][1];
1621 manifold
[3][2] = rba
->bbx
[1][2];
1623 else if( axis
== 1 )
1625 float py
= best
> 0.0f
? rba
->bbx
[0][1]: rba
->bbx
[1][1];
1626 manifold
[0][0] = rba
->bbx
[0][0];
1627 manifold
[0][1] = py
;
1628 manifold
[0][2] = rba
->bbx
[0][2];
1629 manifold
[1][0] = rba
->bbx
[1][0];
1630 manifold
[1][1] = py
;
1631 manifold
[1][2] = rba
->bbx
[0][2];
1632 manifold
[2][0] = rba
->bbx
[1][0];
1633 manifold
[2][1] = py
;
1634 manifold
[2][2] = rba
->bbx
[1][2];
1635 manifold
[3][0] = rba
->bbx
[0][0];
1636 manifold
[3][1] = py
;
1637 manifold
[3][2] = rba
->bbx
[1][2];
1641 float pz
= best
> 0.0f
? rba
->bbx
[0][2]: rba
->bbx
[1][2];
1642 manifold
[0][0] = rba
->bbx
[0][0];
1643 manifold
[0][1] = rba
->bbx
[0][1];
1644 manifold
[0][2] = pz
;
1645 manifold
[1][0] = rba
->bbx
[1][0];
1646 manifold
[1][1] = rba
->bbx
[0][1];
1647 manifold
[1][2] = pz
;
1648 manifold
[2][0] = rba
->bbx
[1][0];
1649 manifold
[2][1] = rba
->bbx
[1][1];
1650 manifold
[2][2] = pz
;
1651 manifold
[3][0] = rba
->bbx
[0][0];
1652 manifold
[3][1] = rba
->bbx
[1][1];
1653 manifold
[3][2] = pz
;
1656 for( int j
=0; j
<4; j
++ )
1657 m4x3_mulv( rba
->to_world
, manifold
[j
], manifold
[j
] );
1659 vg_line( manifold
[0], manifold
[1], 0xffffffff );
1660 vg_line( manifold
[1], manifold
[2], 0xffffffff );
1661 vg_line( manifold
[2], manifold
[3], 0xffffffff );
1662 vg_line( manifold
[3], manifold
[0], 0xffffffff );
1664 for( int j
=0; j
<4; j
++ )
1666 rb_ct
*ct
= buf
+count
;
1668 v3_copy( manifold
[j
], ct
->co
);
1669 v3_copy( n
, ct
->n
);
1671 float l0
= v3_dot( tri
[0], n
),
1672 l1
= v3_dot( manifold
[j
], n
);
1674 ct
->p
= (l0
-l1
)*0.5f
;
1678 ct
->type
= k_contact_type_default
;
1690 VG_STATIC
int rb_capsule__triangle( m4x3f mtxA
, rb_capsule
*c
,
1691 v3f tri
[3], rb_ct
*buf
)
1694 v3_muladds( mtxA
[3], mtxA
[1], -c
->height
*0.5f
+c
->radius
, p0w
);
1695 v3_muladds( mtxA
[3], mtxA
[1], c
->height
*0.5f
-c
->radius
, p1w
);
1697 capsule_manifold manifold
;
1698 rb_capsule_manifold_init( &manifold
);
1701 closest_on_triangle_1( p0w
, tri
, c0
);
1702 closest_on_triangle_1( p1w
, tri
, c1
);
1705 v3_sub( c0
, p0w
, d0
);
1706 v3_sub( c1
, p1w
, d1
);
1707 v3_sub( p1w
, p0w
, da
);
1713 if( v3_dot( da
, d0
) <= 0.01f
)
1714 rb_capsule_manifold( p0w
, c0
, 0.0f
, c
->radius
, &manifold
);
1716 if( v3_dot( da
, d1
) >= -0.01f
)
1717 rb_capsule_manifold( p1w
, c1
, 1.0f
, c
->radius
, &manifold
);
1719 for( int i
=0; i
<3; i
++ )
1726 closest_segment_segment( p0w
, p1w
, tri
[i0
], tri
[i1
], &ta
, &tb
, ca
, cb
);
1727 rb_capsule_manifold( ca
, cb
, ta
, c
->radius
, &manifold
);
1731 v3_sub( tri
[1], tri
[0], v0
);
1732 v3_sub( tri
[2], tri
[0], v1
);
1733 v3_cross( v0
, v1
, n
);
1736 int count
= rb_capsule__manifold_done( mtxA
, c
, &manifold
, buf
);
1737 for( int i
=0; i
<count
; i
++ )
1738 v3_copy( n
, buf
[i
].n
);
1744 * Generates up to two contacts; optimised for the most stable manifold
1746 __attribute__ ((deprecated
))
1747 VG_STATIC
int rb_capsule_triangle( rigidbody
*rba
, rigidbody
*rbb
,
1748 v3f tri
[3], rb_ct
*buf
)
1750 float h
= rba
->inf
.capsule
.height
,
1751 r
= rba
->inf
.capsule
.radius
;
1754 v3_muladds( rba
->co
, rba
->up
, -h
*0.5f
+r
, p0w
);
1755 v3_muladds( rba
->co
, rba
->up
, h
*0.5f
-r
, p1w
);
1757 capsule_manifold manifold
;
1758 rb_capsule_manifold_init( &manifold
);
1761 closest_on_triangle_1( p0w
, tri
, c0
);
1762 closest_on_triangle_1( p1w
, tri
, c1
);
1765 v3_sub( c0
, p0w
, d0
);
1766 v3_sub( c1
, p1w
, d1
);
1767 v3_sub( p1w
, p0w
, da
);
1773 if( v3_dot( da
, d0
) <= 0.01f
)
1774 rb_capsule_manifold( p0w
, c0
, 0.0f
, r
, &manifold
);
1776 if( v3_dot( da
, d1
) >= -0.01f
)
1777 rb_capsule_manifold( p1w
, c1
, 1.0f
, r
, &manifold
);
1779 for( int i
=0; i
<3; i
++ )
1786 closest_segment_segment( p0w
, p1w
, tri
[i0
], tri
[i1
], &ta
, &tb
, ca
, cb
);
1787 rb_capsule_manifold( ca
, cb
, ta
, r
, &manifold
);
1791 v3_sub( tri
[1], tri
[0], v0
);
1792 v3_sub( tri
[2], tri
[0], v1
);
1793 v3_cross( v0
, v1
, n
);
1796 int count
= rb_capsule_manifold_done( rba
, rbb
, &manifold
, buf
);
1797 for( int i
=0; i
<count
; i
++ )
1798 v3_copy( n
, buf
[i
].n
);
1803 /* mtxB is defined only for tradition; it is not used currently */
1804 VG_STATIC
int rb_capsule__scene( m4x3f mtxA
, rb_capsule
*c
,
1805 m4x3f mtxB
, rb_scene
*s
,
1809 bh_iter_init( 0, &it
);
1814 v3_sub( mtxA
[3], (v3f
){ c
->height
, c
->height
, c
->height
}, bbx
[0] );
1815 v3_add( mtxA
[3], (v3f
){ c
->height
, c
->height
, c
->height
}, bbx
[1] );
1817 scene
*sc
= s
->bh_scene
->user
;
1819 while( bh_next( s
->bh_scene
, &it
, bbx
, &idx
) )
1821 u32
*ptri
= &sc
->arrindices
[ idx
*3 ];
1824 for( int j
=0; j
<3; j
++ )
1825 v3_copy( sc
->arrvertices
[ptri
[j
]].co
, tri
[j
] );
1827 buf
[ count
].element_id
= ptri
[0];
1829 int contact
= rb_capsule__triangle( mtxA
, c
, tri
, &buf
[count
] );
1834 vg_warn("Exceeding capsule_vs_scene capacity. Geometry too dense!\n");
1842 __attribute__ ((deprecated
))
1843 VG_STATIC
int rb_capsule_scene( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1845 scene
*sc
= rbb
->inf
.scene
.bh_scene
->user
;
1848 bh_iter_init( 0, &it
);
1853 while( bh_next( rbb
->inf
.scene
.bh_scene
, &it
, rba
->bbx_world
, &idx
) )
1855 u32
*ptri
= &sc
->arrindices
[ idx
*3 ];
1858 for( int j
=0; j
<3; j
++ )
1859 v3_copy( sc
->arrvertices
[ptri
[j
]].co
, tri
[j
] );
1861 buf
[ count
].element_id
= ptri
[0];
1864 vg_line( tri
[0],tri
[1],0x70ff6000 );
1865 vg_line( tri
[1],tri
[2],0x70ff6000 );
1866 vg_line( tri
[2],tri
[0],0x70ff6000 );
1869 int contact
= rb_capsule_triangle( rba
, rbb
, tri
, buf
+count
);
1874 vg_warn("Exceeding capsule_vs_scene capacity. Geometry too dense!\n");
1882 VG_STATIC
int rb_scene_capsule( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1884 return rb_capsule_scene( rbb
, rba
, buf
);
1887 VG_STATIC
int RB_MATRIX_ERROR( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1890 vg_error( "Collision type is unimplemented between types %d and %d\n",
1891 rba
->type
, rbb
->type
);
1897 VG_STATIC
int rb_sphere_capsule( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1899 return rb_capsule_sphere( rbb
, rba
, buf
);
1902 VG_STATIC
int rb_box_capsule( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1904 return rb_capsule_box( rbb
, rba
, buf
);
1907 VG_STATIC
int rb_box_sphere( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1909 return rb_sphere_box( rbb
, rba
, buf
);
1912 VG_STATIC
int rb_scene_box( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1914 return rb_box_scene( rbb
, rba
, buf
);
1917 VG_STATIC
int (*rb_jump_table
[4][4])( rigidbody
*a
, rigidbody
*b
, rb_ct
*buf
) =
1919 /* box */ /* Sphere */ /* Capsule */ /* Mesh */
1920 { RB_MATRIX_ERROR
, rb_box_sphere
, rb_box_capsule
, rb_box_scene
},
1921 { rb_sphere_box
, rb_sphere_sphere
, rb_sphere_capsule
, rb_sphere_scene
},
1922 { rb_capsule_box
, rb_capsule_sphere
, rb_capsule_capsule
, rb_capsule_scene
},
1923 { rb_scene_box
, RB_MATRIX_ERROR
, rb_scene_capsule
, RB_MATRIX_ERROR
}
1926 VG_STATIC
int rb_collide( rigidbody
*rba
, rigidbody
*rbb
)
1928 int (*collider_jump
)(rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1929 = rb_jump_table
[rba
->type
][rbb
->type
];
1932 * 12 is the maximum manifold size we can generate, so we are forced to abort
1933 * potentially checking any more.
1935 if( rb_contact_count
+ 12 > vg_list_size(rb_contact_buffer
) )
1937 vg_warn( "Too many contacts made in global collider buffer (%d of %d\n)",
1938 rb_contact_count
, vg_list_size(rb_contact_buffer
) );
1943 * FUTURE: Replace this with a more dedicated broad phase pass
1945 if( box_overlap( rba
->bbx_world
, rbb
->bbx_world
) )
1947 int count
= collider_jump( rba
, rbb
, rb_contact_buffer
+rb_contact_count
);
1948 rb_contact_count
+= count
;
1956 * -----------------------------------------------------------------------------
1958 * -----------------------------------------------------------------------------
1961 VG_STATIC
void rb_solver_reset(void)
1963 rb_contact_count
= 0;
1966 VG_STATIC rb_ct
*rb_global_ct(void)
1968 return rb_contact_buffer
+ rb_contact_count
;
1971 VG_STATIC
void rb_prepare_contact( rb_ct
*ct
)
1973 ct
->bias
= -0.2f
* k_rb_rate
* vg_minf( 0.0f
, -ct
->p
+k_penetration_slop
);
1974 rb_tangent_basis( ct
->n
, ct
->t
[0], ct
->t
[1] );
1977 ct
->type
= k_contact_type_default
;
1979 ct
->norm_impulse
= 0.0f
;
1980 ct
->tangent_impulse
[0] = 0.0f
;
1981 ct
->tangent_impulse
[1] = 0.0f
;
1985 * Initializing things like tangent vectors
1987 VG_STATIC
void rb_presolve_contacts( rb_ct
*buffer
, int len
)
1989 for( int i
=0; i
<len
; i
++ )
1991 rb_ct
*ct
= &buffer
[i
];
1992 rb_prepare_contact( ct
);
1994 v3f ra
, rb
, raCn
, rbCn
, raCt
, rbCt
;
1995 v3_sub( ct
->co
, ct
->rba
->co
, ra
);
1996 v3_sub( ct
->co
, ct
->rbb
->co
, rb
);
1997 v3_cross( ra
, ct
->n
, raCn
);
1998 v3_cross( rb
, ct
->n
, rbCn
);
2000 /* orient inverse inertia tensors */
2002 m3x3_mulv( ct
->rba
->iIw
, raCn
, raCnI
);
2003 m3x3_mulv( ct
->rbb
->iIw
, rbCn
, rbCnI
);
2005 ct
->normal_mass
= ct
->rba
->inv_mass
+ ct
->rbb
->inv_mass
;
2006 ct
->normal_mass
+= v3_dot( raCn
, raCnI
);
2007 ct
->normal_mass
+= v3_dot( rbCn
, rbCnI
);
2008 ct
->normal_mass
= 1.0f
/ct
->normal_mass
;
2010 for( int j
=0; j
<2; j
++ )
2013 v3_cross( ct
->t
[j
], ra
, raCt
);
2014 v3_cross( ct
->t
[j
], rb
, rbCt
);
2015 m3x3_mulv( ct
->rba
->iIw
, raCt
, raCtI
);
2016 m3x3_mulv( ct
->rbb
->iIw
, rbCt
, rbCtI
);
2018 ct
->tangent_mass
[j
] = ct
->rba
->inv_mass
+ ct
->rbb
->inv_mass
;
2019 ct
->tangent_mass
[j
] += v3_dot( raCt
, raCtI
);
2020 ct
->tangent_mass
[j
] += v3_dot( rbCt
, rbCtI
);
2021 ct
->tangent_mass
[j
] = 1.0f
/ct
->tangent_mass
[j
];
2024 rb_debug_contact( ct
);
2029 * Creates relative contact velocity vector
2031 VG_STATIC
void rb_rcv( rigidbody
*rba
, rigidbody
*rbb
, v3f ra
, v3f rb
, v3f rv
)
2034 v3_cross( rba
->w
, ra
, rva
);
2035 v3_add( rba
->v
, rva
, rva
);
2036 v3_cross( rbb
->w
, rb
, rvb
);
2037 v3_add( rbb
->v
, rvb
, rvb
);
2039 v3_sub( rva
, rvb
, rv
);
2043 * Apply impulse to object
2045 VG_STATIC
void rb_linear_impulse( rigidbody
*rb
, v3f delta
, v3f impulse
)
2048 v3_muladds( rb
->v
, impulse
, rb
->inv_mass
, rb
->v
);
2050 /* Angular velocity */
2052 v3_cross( delta
, impulse
, wa
);
2054 m3x3_mulv( rb
->iIw
, wa
, wa
);
2055 v3_add( rb
->w
, wa
, rb
->w
);
2059 * One iteration to solve the contact constraint
2061 VG_STATIC
void rb_solve_contacts( rb_ct
*buf
, int len
)
2063 for( int i
=0; i
<len
; i
++ )
2065 struct contact
*ct
= &buf
[i
];
2068 v3_sub( ct
->co
, ct
->rba
->co
, ra
);
2069 v3_sub( ct
->co
, ct
->rbb
->co
, rb
);
2070 rb_rcv( ct
->rba
, ct
->rbb
, ra
, rb
, rv
);
2073 for( int j
=0; j
<2; j
++ )
2075 float f
= k_friction
* ct
->norm_impulse
,
2076 vt
= v3_dot( rv
, ct
->t
[j
] ),
2077 lambda
= ct
->tangent_mass
[j
] * -vt
;
2079 float temp
= ct
->tangent_impulse
[j
];
2080 ct
->tangent_impulse
[j
] = vg_clampf( temp
+ lambda
, -f
, f
);
2081 lambda
= ct
->tangent_impulse
[j
] - temp
;
2084 v3_muls( ct
->t
[j
], lambda
, impulse
);
2085 rb_linear_impulse( ct
->rba
, ra
, impulse
);
2087 v3_muls( ct
->t
[j
], -lambda
, impulse
);
2088 rb_linear_impulse( ct
->rbb
, rb
, impulse
);
2092 rb_rcv( ct
->rba
, ct
->rbb
, ra
, rb
, rv
);
2093 float vn
= v3_dot( rv
, ct
->n
),
2094 lambda
= ct
->normal_mass
* (-vn
+ ct
->bias
);
2096 float temp
= ct
->norm_impulse
;
2097 ct
->norm_impulse
= vg_maxf( temp
+ lambda
, 0.0f
);
2098 lambda
= ct
->norm_impulse
- temp
;
2101 v3_muls( ct
->n
, lambda
, impulse
);
2102 rb_linear_impulse( ct
->rba
, ra
, impulse
);
2104 v3_muls( ct
->n
, -lambda
, impulse
);
2105 rb_linear_impulse( ct
->rbb
, rb
, impulse
);
2110 * -----------------------------------------------------------------------------
2112 * -----------------------------------------------------------------------------
2115 VG_STATIC
void rb_debug_position_constraints( rb_constr_pos
*buffer
, int len
)
2117 for( int i
=0; i
<len
; i
++ )
2119 rb_constr_pos
*constr
= &buffer
[i
];
2120 rigidbody
*rba
= constr
->rba
, *rbb
= constr
->rbb
;
2123 m3x3_mulv( rba
->to_world
, constr
->lca
, wca
);
2124 m3x3_mulv( rbb
->to_world
, constr
->lcb
, wcb
);
2127 v3_add( wca
, rba
->co
, p0
);
2128 v3_add( wcb
, rbb
->co
, p1
);
2129 vg_line_pt3( p0
, 0.0025f
, 0xff000000 );
2130 vg_line_pt3( p1
, 0.0025f
, 0xffffffff );
2131 vg_line2( p0
, p1
, 0xff000000, 0xffffffff );
2135 VG_STATIC
void rb_presolve_swingtwist_constraints( rb_constr_swingtwist
*buf
,
2140 for( int i
=0; i
<len
; i
++ )
2142 rb_constr_swingtwist
*st
= &buf
[ i
];
2144 v3f vx
, vy
, va
, vxb
, axis
, center
;
2146 m3x3_mulv( st
->rba
->to_world
, st
->conevx
, vx
);
2147 m3x3_mulv( st
->rbb
->to_world
, st
->conevxb
, vxb
);
2148 m3x3_mulv( st
->rba
->to_world
, st
->conevy
, vy
);
2149 m3x3_mulv( st
->rbb
->to_world
, st
->coneva
, va
);
2150 m4x3_mulv( st
->rba
->to_world
, st
->view_offset
, center
);
2151 v3_cross( vy
, vx
, axis
);
2153 /* Constraint violated ? */
2154 float fx
= v3_dot( vx
, va
), /* projection world */
2155 fy
= v3_dot( vy
, va
),
2156 fn
= v3_dot( va
, axis
),
2158 rx
= st
->conevx
[3], /* elipse radii */
2161 lx
= fx
/rx
, /* projection local (fn==lz) */
2164 st
->tangent_violation
= ((lx
*lx
+ ly
*ly
) > fn
*fn
) || (fn
<= 0.0f
);
2165 if( st
->tangent_violation
)
2167 /* Calculate a good position and the axis to solve on */
2168 v2f closest
, tangent
,
2169 p
= { fx
/fabsf(fn
), fy
/fabsf(fn
) };
2171 closest_point_elipse( p
, (v2f
){rx
,ry
}, closest
);
2172 tangent
[0] = -closest
[1] / (ry
*ry
);
2173 tangent
[1] = closest
[0] / (rx
*rx
);
2174 v2_normalize( tangent
);
2177 v3_muladds( axis
, vx
, closest
[0], v0
);
2178 v3_muladds( v0
, vy
, closest
[1], v0
);
2181 v3_muls( vx
, tangent
[0], v1
);
2182 v3_muladds( v1
, vy
, tangent
[1], v1
);
2184 v3_copy( v0
, st
->tangent_target
);
2185 v3_copy( v1
, st
->tangent_axis
);
2187 /* calculate mass */
2189 m3x3_mulv( st
->rba
->iIw
, st
->tangent_axis
, aIw
);
2190 m3x3_mulv( st
->rbb
->iIw
, st
->tangent_axis
, bIw
);
2191 st
->tangent_mass
= 1.0f
/ (v3_dot( st
->tangent_axis
, aIw
) +
2192 v3_dot( st
->tangent_axis
, bIw
));
2194 float angle
= v3_dot( va
, st
->tangent_target
);
2198 v3_cross( vy
, va
, refaxis
); /* our default rotation */
2199 v3_normalize( refaxis
);
2201 float angle
= v3_dot( refaxis
, vxb
);
2202 st
->axis_violation
= fabsf(angle
) < st
->conet
;
2204 if( st
->axis_violation
)
2207 v3_cross( refaxis
, vxb
, dir_test
);
2209 if( v3_dot(dir_test
, va
) < 0.0f
)
2210 st
->axis_violation
= -st
->axis_violation
;
2212 float newang
= (float)st
->axis_violation
* acosf(st
->conet
-0.0001f
);
2215 v3_cross( va
, refaxis
, refaxis_up
);
2216 v3_muls( refaxis_up
, sinf(newang
), st
->axis_target
);
2217 v3_muladds( st
->axis_target
, refaxis
, -cosf(newang
), st
->axis_target
);
2219 /* calculate mass */
2220 v3_copy( va
, st
->axis
);
2222 m3x3_mulv( st
->rba
->iIw
, st
->axis
, aIw
);
2223 m3x3_mulv( st
->rbb
->iIw
, st
->axis
, bIw
);
2224 st
->axis_mass
= 1.0f
/ (v3_dot( st
->axis
, aIw
) +
2225 v3_dot( st
->axis
, bIw
));
2230 VG_STATIC
void rb_debug_swingtwist_constraints( rb_constr_swingtwist
*buf
,
2235 for( int i
=0; i
<len
; i
++ )
2237 rb_constr_swingtwist
*st
= &buf
[ i
];
2239 v3f vx
, vxb
, vy
, va
, axis
, center
;
2241 m3x3_mulv( st
->rba
->to_world
, st
->conevx
, vx
);
2242 m3x3_mulv( st
->rbb
->to_world
, st
->conevxb
, vxb
);
2243 m3x3_mulv( st
->rba
->to_world
, st
->conevy
, vy
);
2244 m3x3_mulv( st
->rbb
->to_world
, st
->coneva
, va
);
2245 m4x3_mulv( st
->rba
->to_world
, st
->view_offset
, center
);
2246 v3_cross( vy
, vx
, axis
);
2248 float rx
= st
->conevx
[3], /* elipse radii */
2252 v3_muladds( center
, va
, size
, p1
);
2253 vg_line( center
, p1
, 0xffffffff );
2254 vg_line_pt3( p1
, 0.00025f
, 0xffffffff );
2256 if( st
->tangent_violation
)
2258 v3_muladds( center
, st
->tangent_target
, size
, p0
);
2260 vg_line( center
, p0
, 0xff00ff00 );
2261 vg_line_pt3( p0
, 0.00025f
, 0xff00ff00 );
2262 vg_line( p1
, p0
, 0xff000000 );
2265 for( int x
=0; x
<32; x
++ )
2267 float t0
= ((float)x
* (1.0f
/32.0f
)) * VG_TAUf
,
2268 t1
= (((float)x
+1.0f
) * (1.0f
/32.0f
)) * VG_TAUf
,
2275 v3_muladds( axis
, vx
, c0
*rx
, v0
);
2276 v3_muladds( v0
, vy
, s0
*ry
, v0
);
2277 v3_muladds( axis
, vx
, c1
*rx
, v1
);
2278 v3_muladds( v1
, vy
, s1
*ry
, v1
);
2283 v3_muladds( center
, v0
, size
, p0
);
2284 v3_muladds( center
, v1
, size
, p1
);
2286 u32 col0r
= fabsf(c0
) * 255.0f
,
2287 col0g
= fabsf(s0
) * 255.0f
,
2288 col1r
= fabsf(c1
) * 255.0f
,
2289 col1g
= fabsf(s1
) * 255.0f
,
2290 col
= st
->tangent_violation
? 0xff0000ff: 0xff000000,
2291 col0
= col
| (col0r
<<16) | (col0g
<< 8),
2292 col1
= col
| (col1r
<<16) | (col1g
<< 8);
2294 vg_line2( center
, p0
, VG__NONE
, col0
);
2295 vg_line2( p0
, p1
, col0
, col1
);
2299 v3_muladds( center
, va
, size
, p0
);
2300 v3_muladds( p0
, vxb
, size
, p1
);
2302 vg_line( p0
, p1
, 0xff0000ff );
2304 if( st
->axis_violation
)
2306 v3_muladds( p0
, st
->axis_target
, size
*1.25f
, p1
);
2307 vg_line( p0
, p1
, 0xffffff00 );
2308 vg_line_pt3( p1
, 0.0025f
, 0xffffff80 );
2312 v3_cross( vy
, va
, refaxis
); /* our default rotation */
2313 v3_normalize( refaxis
);
2315 v3_cross( va
, refaxis
, refaxis_up
);
2316 float newang
= acosf(st
->conet
-0.0001f
);
2318 v3_muladds( p0
, refaxis_up
, sinf(newang
)*size
, p1
);
2319 v3_muladds( p1
, refaxis
, -cosf(newang
)*size
, p1
);
2320 vg_line( p0
, p1
, 0xff000000 );
2322 v3_muladds( p0
, refaxis_up
, sinf(-newang
)*size
, p1
);
2323 v3_muladds( p1
, refaxis
, -cosf(-newang
)*size
, p1
);
2324 vg_line( p0
, p1
, 0xff404040 );
2329 * Solve a list of positional constraints
2331 VG_STATIC
void rb_solve_position_constraints( rb_constr_pos
*buf
, int len
)
2333 for( int i
=0; i
<len
; i
++ )
2335 rb_constr_pos
*constr
= &buf
[i
];
2336 rigidbody
*rba
= constr
->rba
, *rbb
= constr
->rbb
;
2339 m3x3_mulv( rba
->to_world
, constr
->lca
, wa
);
2340 m3x3_mulv( rbb
->to_world
, constr
->lcb
, wb
);
2342 m3x3f ssra
, ssrat
, ssrb
, ssrbt
;
2344 m3x3_skew_symetric( ssrat
, wa
);
2345 m3x3_skew_symetric( ssrbt
, wb
);
2346 m3x3_transpose( ssrat
, ssra
);
2347 m3x3_transpose( ssrbt
, ssrb
);
2349 v3f b
, b_wa
, b_wb
, b_a
, b_b
;
2350 m3x3_mulv( ssra
, rba
->w
, b_wa
);
2351 m3x3_mulv( ssrb
, rbb
->w
, b_wb
);
2352 v3_add( rba
->v
, b_wa
, b
);
2353 v3_sub( b
, rbb
->v
, b
);
2354 v3_sub( b
, b_wb
, b
);
2355 v3_muls( b
, -1.0f
, b
);
2358 m3x3_diagonal( invMa
, rba
->inv_mass
);
2359 m3x3_diagonal( invMb
, rbb
->inv_mass
);
2362 m3x3_mul( ssra
, rba
->iIw
, ia
);
2363 m3x3_mul( ia
, ssrat
, ia
);
2364 m3x3_mul( ssrb
, rbb
->iIw
, ib
);
2365 m3x3_mul( ib
, ssrbt
, ib
);
2368 m3x3_add( invMa
, ia
, cma
);
2369 m3x3_add( invMb
, ib
, cmb
);
2372 m3x3_add( cma
, cmb
, A
);
2374 /* Solve Ax = b ( A^-1*b = x ) */
2377 m3x3_inv( A
, invA
);
2378 m3x3_mulv( invA
, b
, impulse
);
2380 v3f delta_va
, delta_wa
, delta_vb
, delta_wb
;
2382 m3x3_mul( rba
->iIw
, ssrat
, iwa
);
2383 m3x3_mul( rbb
->iIw
, ssrbt
, iwb
);
2385 m3x3_mulv( invMa
, impulse
, delta_va
);
2386 m3x3_mulv( invMb
, impulse
, delta_vb
);
2387 m3x3_mulv( iwa
, impulse
, delta_wa
);
2388 m3x3_mulv( iwb
, impulse
, delta_wb
);
2390 v3_add( rba
->v
, delta_va
, rba
->v
);
2391 v3_add( rba
->w
, delta_wa
, rba
->w
);
2392 v3_sub( rbb
->v
, delta_vb
, rbb
->v
);
2393 v3_sub( rbb
->w
, delta_wb
, rbb
->w
);
2397 VG_STATIC
void rb_solve_swingtwist_constraints( rb_constr_swingtwist
*buf
,
2402 for( int i
=0; i
<len
; i
++ )
2404 rb_constr_swingtwist
*st
= &buf
[ i
];
2406 if( !st
->axis_violation
)
2409 float rv
= v3_dot( st
->axis
, st
->rbb
->w
) -
2410 v3_dot( st
->axis
, st
->rba
->w
);
2412 if( rv
* (float)st
->axis_violation
> 0.0f
)
2415 v3f impulse
, wa
, wb
;
2416 v3_muls( st
->axis
, rv
*st
->axis_mass
, impulse
);
2417 m3x3_mulv( st
->rba
->iIw
, impulse
, wa
);
2418 v3_add( st
->rba
->w
, wa
, st
->rba
->w
);
2420 v3_muls( impulse
, -1.0f
, impulse
);
2421 m3x3_mulv( st
->rbb
->iIw
, impulse
, wb
);
2422 v3_add( st
->rbb
->w
, wb
, st
->rbb
->w
);
2424 float rv2
= v3_dot( st
->axis
, st
->rbb
->w
) -
2425 v3_dot( st
->axis
, st
->rba
->w
);
2428 for( int i
=0; i
<len
; i
++ )
2430 rb_constr_swingtwist
*st
= &buf
[ i
];
2432 if( !st
->tangent_violation
)
2435 float rv
= v3_dot( st
->tangent_axis
, st
->rbb
->w
) -
2436 v3_dot( st
->tangent_axis
, st
->rba
->w
);
2441 v3f impulse
, wa
, wb
;
2442 v3_muls( st
->tangent_axis
, rv
*st
->tangent_mass
, impulse
);
2443 m3x3_mulv( st
->rba
->iIw
, impulse
, wa
);
2444 v3_add( st
->rba
->w
, wa
, st
->rba
->w
);
2446 v3_muls( impulse
, -1.0f
, impulse
);
2447 m3x3_mulv( st
->rbb
->iIw
, impulse
, wb
);
2448 v3_add( st
->rbb
->w
, wb
, st
->rbb
->w
);
2450 float rv2
= v3_dot( st
->tangent_axis
, st
->rbb
->w
) -
2451 v3_dot( st
->tangent_axis
, st
->rba
->w
);
2455 VG_STATIC
void rb_solve_constr_angle( rigidbody
*rba
, rigidbody
*rbb
,
2458 m3x3f ssra
, ssrb
, ssrat
, ssrbt
;
2461 m3x3_skew_symetric( ssrat
, ra
);
2462 m3x3_skew_symetric( ssrbt
, rb
);
2463 m3x3_transpose( ssrat
, ssra
);
2464 m3x3_transpose( ssrbt
, ssrb
);
2466 m3x3_mul( ssra
, rba
->iIw
, cma
);
2467 m3x3_mul( cma
, ssrat
, cma
);
2468 m3x3_mul( ssrb
, rbb
->iIw
, cmb
);
2469 m3x3_mul( cmb
, ssrbt
, cmb
);
2472 m3x3_add( cma
, cmb
, A
);
2473 m3x3_inv( A
, invA
);
2476 m3x3_mulv( ssra
, rba
->w
, b_wa
);
2477 m3x3_mulv( ssrb
, rbb
->w
, b_wb
);
2478 v3_add( b_wa
, b_wb
, b
);
2482 m3x3_mulv( invA
, b
, impulse
);
2484 v3f delta_wa
, delta_wb
;
2486 m3x3_mul( rba
->iIw
, ssrat
, iwa
);
2487 m3x3_mul( rbb
->iIw
, ssrbt
, iwb
);
2488 m3x3_mulv( iwa
, impulse
, delta_wa
);
2489 m3x3_mulv( iwb
, impulse
, delta_wb
);
2490 v3_add( rba
->w
, delta_wa
, rba
->w
);
2491 v3_sub( rbb
->w
, delta_wb
, rbb
->w
);
2495 * Correct position constraint drift errors
2496 * [ 0.0 <= amt <= 1.0 ]: the correction amount
2498 VG_STATIC
void rb_correct_position_constraints( rb_constr_pos
*buf
, int len
,
2501 for( int i
=0; i
<len
; i
++ )
2503 rb_constr_pos
*constr
= &buf
[i
];
2504 rigidbody
*rba
= constr
->rba
, *rbb
= constr
->rbb
;
2507 m3x3_mulv( rba
->to_world
, constr
->lca
, p0
);
2508 m3x3_mulv( rbb
->to_world
, constr
->lcb
, p1
);
2509 v3_add( rba
->co
, p0
, p0
);
2510 v3_add( rbb
->co
, p1
, p1
);
2511 v3_sub( p1
, p0
, d
);
2513 v3_muladds( rbb
->co
, d
, -1.0f
* amt
, rbb
->co
);
2514 rb_update_transform( rbb
);
2518 VG_STATIC
void rb_correct_swingtwist_constraints( rb_constr_swingtwist
*buf
,
2519 int len
, float amt
)
2521 for( int i
=0; i
<len
; i
++ )
2523 rb_constr_swingtwist
*st
= &buf
[i
];
2525 if( !st
->tangent_violation
)
2529 m3x3_mulv( st
->rbb
->to_world
, st
->coneva
, va
);
2531 float angle
= v3_dot( va
, st
->tangent_target
);
2533 if( fabsf(angle
) < 0.9999f
)
2536 v3_cross( va
, st
->tangent_target
, axis
);
2539 q_axis_angle( correction
, axis
, acosf(angle
) * amt
);
2540 q_mul( correction
, st
->rbb
->q
, st
->rbb
->q
);
2541 rb_update_transform( st
->rbb
);
2545 for( int i
=0; i
<len
; i
++ )
2547 rb_constr_swingtwist
*st
= &buf
[i
];
2549 if( !st
->axis_violation
)
2553 m3x3_mulv( st
->rbb
->to_world
, st
->conevxb
, vxb
);
2555 float angle
= v3_dot( vxb
, st
->axis_target
);
2557 if( fabsf(angle
) < 0.9999f
)
2560 v3_cross( vxb
, st
->axis_target
, axis
);
2563 q_axis_angle( correction
, axis
, acosf(angle
) * amt
);
2564 q_mul( correction
, st
->rbb
->q
, st
->rbb
->q
);
2565 rb_update_transform( st
->rbb
);
2570 VG_STATIC
void rb_correct_contact_constraints( rb_ct
*buf
, int len
, float amt
)
2572 for( int i
=0; i
<len
; i
++ )
2574 rb_ct
*ct
= &buf
[i
];
2575 rigidbody
*rba
= ct
->rba
,
2578 float mass_total
= 1.0f
/ (rba
->inv_mass
+ rbb
->inv_mass
);
2580 v3_muladds( rba
->co
, ct
->n
, -mass_total
* rba
->inv_mass
, rba
->co
);
2581 v3_muladds( rbb
->co
, ct
->n
, mass_total
* rbb
->inv_mass
, rbb
->co
);
2590 VG_STATIC
void rb_effect_simple_bouyency( rigidbody
*ra
, v4f plane
,
2591 float amt
, float drag
)
2594 float depth
= v3_dot( plane
, ra
->co
) - plane
[3],
2595 lambda
= vg_clampf( -depth
, 0.0f
, 1.0f
) * amt
;
2597 v3_muladds( ra
->v
, plane
, lambda
* k_rb_delta
, ra
->v
);
2600 v3_muls( ra
->v
, 1.0f
-(drag
*k_rb_delta
), ra
->v
);
2604 * -----------------------------------------------------------------------------
2605 * BVH implementation, this is ONLY for VG_STATIC rigidbodies, its to slow for
2607 * -----------------------------------------------------------------------------
2610 VG_STATIC
void rb_bh_expand_bound( void *user
, boxf bound
, u32 item_index
)
2612 rigidbody
*rb
= &((rigidbody
*)user
)[ item_index
];
2613 box_concat( bound
, rb
->bbx_world
);
2616 VG_STATIC
float rb_bh_centroid( void *user
, u32 item_index
, int axis
)
2618 rigidbody
*rb
= &((rigidbody
*)user
)[ item_index
];
2619 return (rb
->bbx_world
[axis
][0] + rb
->bbx_world
[1][axis
]) * 0.5f
;
2622 VG_STATIC
void rb_bh_swap( void *user
, u32 ia
, u32 ib
)
2624 rigidbody temp
, *rba
, *rbb
;
2625 rba
= &((rigidbody
*)user
)[ ia
];
2626 rbb
= &((rigidbody
*)user
)[ ib
];
2633 VG_STATIC
void rb_bh_debug( void *user
, u32 item_index
)
2635 rigidbody
*rb
= &((rigidbody
*)user
)[ item_index
];
2636 rb_debug( rb
, 0xff00ffff );
2639 VG_STATIC bh_system bh_system_rigidbodies
=
2641 .expand_bound
= rb_bh_expand_bound
,
2642 .item_centroid
= rb_bh_centroid
,
2643 .item_swap
= rb_bh_swap
,
2644 .item_debug
= rb_bh_debug
,
2648 #endif /* RIGIDBODY_H */