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
;
23 * -----------------------------------------------------------------------------
25 * -----------------------------------------------------------------------------
29 k_rb_rate
= (1.0/VG_TIMESTEP_FIXED
),
30 k_rb_delta
= (1.0/k_rb_rate
),
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
,
41 k_joint_correction
= 0.01f
,
42 k_joint_impulse
= 1.0f
,
43 k_joint_bias
= 0.08f
; /* positional joints */
45 VG_STATIC
void rb_register_cvar(void)
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
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
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
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
69 * -----------------------------------------------------------------------------
70 * structure definitions
71 * -----------------------------------------------------------------------------
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
;
104 /* inertia model and inverse world tensor */
107 m4x3f to_world
, to_local
;
116 k_rb_shape_sphere
= 1,
117 k_rb_shape_capsule
= 2,
124 struct rb_sphere sphere
;
125 struct rb_capsule capsule
;
126 struct rb_scene scene
;
131 VG_STATIC
struct contact
133 rigidbody
*rba
, *rbb
;
136 float p
, bias
, norm_impulse
, tangent_impulse
[2],
137 normal_mass
, tangent_mass
[2];
141 enum contact_type type
;
143 rb_contact_buffer
[256];
144 VG_STATIC
int rb_contact_count
= 0;
146 typedef struct rb_constr_pos rb_constr_pos
;
147 typedef struct rb_constr_swingtwist rb_constr_swingtwist
;
151 rigidbody
*rba
, *rbb
;
155 struct rb_constr_swingtwist
157 rigidbody
*rba
, *rbb
;
159 v4f conevx
, conevy
; /* relative to rba */
160 v3f view_offset
, /* relative to rba */
161 coneva
, conevxb
;/* relative to rbb */
163 int tangent_violation
, axis_violation
;
164 v3f axis
, tangent_axis
, tangent_target
, axis_target
;
167 float tangent_mass
, axis_mass
;
170 struct rb_constr_spring
176 * -----------------------------------------------------------------------------
178 * -----------------------------------------------------------------------------
181 VG_STATIC
float sphere_volume( float radius
)
183 float r3
= radius
*radius
*radius
;
184 return (4.0f
/3.0f
) * VG_PIf
* r3
;
187 VG_STATIC
void rb_tangent_basis( v3f n
, v3f tx
, v3f ty
)
189 /* Compute tangent basis (box2d) */
190 if( fabsf( n
[0] ) >= 0.57735027f
)
204 v3_cross( n
, tx
, ty
);
208 * -----------------------------------------------------------------------------
210 * -----------------------------------------------------------------------------
213 VG_STATIC
void rb_debug_contact( rb_ct
*ct
)
216 v3_muladds( ct
->co
, ct
->n
, 0.05f
, p1
);
218 if( ct
->type
== k_contact_type_default
)
220 vg_line_pt3( ct
->co
, 0.0125f
, 0xff0000ff );
221 vg_line( ct
->co
, p1
, 0xffffffff );
223 else if( ct
->type
== k_contact_type_edge
)
225 vg_line_pt3( ct
->co
, 0.0125f
, 0xff00ffc0 );
226 vg_line( ct
->co
, p1
, 0xffffffff );
230 VG_STATIC
void debug_sphere( m4x3f m
, float radius
, u32 colour
)
232 v3f ly
= { 0.0f
, 0.0f
, radius
},
233 lx
= { 0.0f
, radius
, 0.0f
},
234 lz
= { 0.0f
, 0.0f
, radius
};
236 for( int i
=0; i
<16; i
++ )
238 float t
= ((float)(i
+1) * (1.0f
/16.0f
)) * VG_PIf
* 2.0f
,
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
};
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
);
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
);
264 VG_STATIC
void debug_capsule( m4x3f m
, float radius
, float h
, u32 colour
)
266 v3f ly
= { 0.0f
, 0.0f
, radius
},
267 lx
= { 0.0f
, radius
, 0.0f
},
268 lz
= { 0.0f
, 0.0f
, radius
};
270 float s0
= sinf(0.0f
)*radius
,
271 c0
= cosf(0.0f
)*radius
;
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
);
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
);
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
);
295 for( int i
=0; i
<16; i
++ )
297 float t
= ((float)(i
+1) * (1.0f
/16.0f
)) * VG_PIf
* 2.0f
,
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
};
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
);
315 v3_add( p0
, e0
, a0
);
316 v3_add( p0
, e1
, a1
);
317 v3_add( p1
, e0
, b0
);
318 v3_add( p1
, e1
, b1
);
320 vg_line( a0
, a1
, colour
);
321 vg_line( b0
, b1
, colour
);
325 v3_add( p0
, e2
, a0
);
326 v3_add( p0
, e3
, a1
);
327 v3_add( p0
, e4
, b0
);
328 v3_add( p0
, e5
, b1
);
332 v3_add( p1
, e2
, a0
);
333 v3_add( p1
, e3
, a1
);
334 v3_add( p1
, e4
, b0
);
335 v3_add( p1
, e5
, b1
);
338 vg_line( a0
, a1
, colour
);
339 vg_line( b0
, b1
, colour
);
346 VG_STATIC
void rb_object_debug( rb_object
*obj
, u32 colour
)
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
);
352 else if( obj
->type
== k_rb_shape_sphere
){
353 debug_sphere( obj
->rb
.to_world
, obj
->inf
.sphere
.radius
, colour
);
355 else if( obj
->type
== k_rb_shape_capsule
){
357 float h
= obj
->inf
.capsule
.height
,
358 r
= obj
->inf
.capsule
.radius
;
360 debug_capsule( obj
->rb
.to_world
, r
, h
, colour
);
362 else if( obj
->type
== k_rb_shape_scene
){
363 vg_line_boxf( obj
->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
);
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
);
399 m3x3_mul( rb
->iI
, rb
->to_local
, rb
->iIw
);
400 m3x3_mul( rb
->to_world
, rb
->iIw
, rb
->iIw
);
402 rb_update_bounds( rb
);
406 * Extrapolate rigidbody into a transform based on vg accumulator.
407 * Useful for rendering
409 VG_STATIC
void rb_extrapolate( rigidbody
*rb
, v3f co
, v4f q
)
411 float substep
= vg_clampf( vg
.accumulator
/ k_rb_delta
, 0.0f
, 1.0f
);
413 v3_muladds( rb
->co
, rb
->v
, k_rb_delta
*substep
, co
);
415 if( v3_length2( rb
->w
) > 0.0f
){
418 v3_copy( rb
->w
, axis
);
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
);
432 * Initialize rigidbody and calculate masses, inertia
434 VG_STATIC
void rb_init_object( rb_object
*obj
)
439 if( obj
->type
== k_rb_shape_box
){
441 v3_sub( obj
->rb
.bbx
[1], obj
->rb
.bbx
[0], dims
);
442 volume
= dims
[0]*dims
[1]*dims
[2];
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
);
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
);
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
;
459 else if( obj
->type
== k_rb_shape_scene
){
461 box_copy( obj
->inf
.scene
.bh_scene
->nodes
[0].bbx
, obj
->rb
.bbx
);
465 obj
->rb
.inv_mass
= 0.0f
;
466 v3_zero( obj
->rb
.I
);
467 m3x3_zero( obj
->rb
.iI
);
470 float mass
= 2.0f
*volume
;
471 obj
->rb
.inv_mass
= 1.0f
/mass
;
474 v3_sub( obj
->rb
.bbx
[1], obj
->rb
.bbx
[0], extent
);
475 v3_muls( extent
, 0.5f
, extent
);
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];
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
));
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
);
494 rb_update_transform( &obj
->rb
);
497 VG_STATIC
void rb_iter( rigidbody
*rb
)
499 if( !vg_validf( rb
->v
[0] ) ||
500 !vg_validf( rb
->v
[1] ) ||
501 !vg_validf( rb
->v
[2] ) )
503 vg_fatal_exit_loop( "NaN velocity" );
506 v3f gravity
= { 0.0f
, -9.8f
, 0.0f
};
507 v3_muladds( rb
->v
, gravity
, k_rb_delta
, rb
->v
);
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
);
513 /* inegrate inertia */
514 if( v3_length2( rb
->w
) > 0.0f
)
518 v3_copy( rb
->w
, axis
);
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
);
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
);
533 * -----------------------------------------------------------------------------
534 * Boolean shape overlap functions
535 * -----------------------------------------------------------------------------
539 * Project AABB, and triangle interval onto axis to check if they overlap
541 VG_STATIC
int rb_box_triangle_interval( v3f extent
, v3f axis
, v3f tri
[3] )
545 r
= extent
[0] * fabsf(axis
[0]) +
546 extent
[1] * fabsf(axis
[1]) +
547 extent
[2] * fabsf(axis
[2]),
549 p0
= v3_dot( axis
, tri
[0] ),
550 p1
= v3_dot( axis
, tri
[1] ),
551 p2
= v3_dot( axis
, tri
[2] ),
553 e
= vg_maxf(-vg_maxf(p0
,vg_maxf(p1
,p2
)), vg_minf(p0
,vg_minf(p1
,p2
)));
555 if( e
> r
) return 0;
560 * Seperating axis test box vs triangle
562 VG_STATIC
int rb_box_triangle_sat( v3f extent
, v3f center
,
563 m4x3f to_local
, v3f tri_src
[3] )
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
] );
573 if(!rb_box_triangle_interval( extent
, (v3f
){1.0f
,0.0f
,0.0f
}, tri
)) return 0;
574 if(!rb_box_triangle_interval( extent
, (v3f
){0.0f
,1.0f
,0.0f
}, tri
)) return 0;
575 if(!rb_box_triangle_interval( extent
, (v3f
){0.0f
,0.0f
,1.0f
}, tri
)) return 0;
577 v3f v0
,v1
,v2
,n
, e0
,e1
,e2
;
578 v3_sub( tri
[1], tri
[0], v0
);
579 v3_sub( tri
[2], tri
[0], v1
);
580 v3_sub( tri
[2], tri
[1], v2
);
584 v3_cross( v0
, v1
, n
);
585 v3_cross( v0
, n
, e0
);
586 v3_cross( n
, v1
, e1
);
587 v3_cross( v2
, n
, e2
);
590 if(!rb_box_triangle_interval( extent
, n
, tri
)) return 0;
593 v3_cross( e0
, (v3f
){1.0f
,0.0f
,0.0f
}, axis
[0] );
594 v3_cross( e0
, (v3f
){0.0f
,1.0f
,0.0f
}, axis
[1] );
595 v3_cross( e0
, (v3f
){0.0f
,0.0f
,1.0f
}, axis
[2] );
596 v3_cross( e1
, (v3f
){1.0f
,0.0f
,0.0f
}, axis
[3] );
597 v3_cross( e1
, (v3f
){0.0f
,1.0f
,0.0f
}, axis
[4] );
598 v3_cross( e1
, (v3f
){0.0f
,0.0f
,1.0f
}, axis
[5] );
599 v3_cross( e2
, (v3f
){1.0f
,0.0f
,0.0f
}, axis
[6] );
600 v3_cross( e2
, (v3f
){0.0f
,1.0f
,0.0f
}, axis
[7] );
601 v3_cross( e2
, (v3f
){0.0f
,0.0f
,1.0f
}, axis
[8] );
603 for( int i
=0; i
<9; i
++ )
604 if(!rb_box_triangle_interval( extent
, axis
[i
], tri
)) return 0;
610 * -----------------------------------------------------------------------------
612 * -----------------------------------------------------------------------------
615 VG_STATIC
int rb_manifold_apply_filtered( rb_ct
*man
, int len
)
619 for( int i
=0; i
<len
; i
++ ){
622 if( ct
->type
== k_contact_type_disabled
)
632 * Merge two contacts if they are within radius(r) of eachother
634 VG_STATIC
void rb_manifold_contact_weld( rb_ct
*ci
, rb_ct
*cj
, float r
)
636 if( v3_dist2( ci
->co
, cj
->co
) < r
*r
){
637 cj
->type
= k_contact_type_disabled
;
638 ci
->p
= (ci
->p
+ cj
->p
) * 0.5f
;
640 v3_add( ci
->co
, cj
->co
, ci
->co
);
641 v3_muls( ci
->co
, 0.5f
, ci
->co
);
644 v3_sub( ci
->rba
->co
, ci
->co
, delta
);
646 float c0
= v3_dot( ci
->n
, delta
),
647 c1
= v3_dot( cj
->n
, delta
);
649 if( c0
< 0.0f
|| c1
< 0.0f
){
651 ci
->type
= k_contact_type_disabled
;
655 v3_muls( ci
->n
, c0
, n
);
656 v3_muladds( n
, cj
->n
, c1
, n
);
666 VG_STATIC
void rb_manifold_filter_joint_edges( rb_ct
*man
, int len
, float r
)
668 for( int i
=0; i
<len
-1; i
++ ){
670 if( ci
->type
!= k_contact_type_edge
)
673 for( int j
=i
+1; j
<len
; j
++ ){
675 if( cj
->type
!= k_contact_type_edge
)
678 rb_manifold_contact_weld( ci
, cj
, r
);
684 * Resolve overlapping pairs
688 VG_STATIC
void rb_manifold_filter_pairs( rb_ct
*man
, int len
, float r
)
690 for( int i
=0; i
<len
-1; i
++ ){
694 if( ci
->type
== k_contact_type_disabled
) continue;
696 for( int j
=i
+1; j
<len
; j
++ ){
699 if( cj
->type
== k_contact_type_disabled
) continue;
701 if( v3_dist2( ci
->co
, cj
->co
) < r
*r
){
702 cj
->type
= k_contact_type_disabled
;
703 v3_add( cj
->n
, ci
->n
, ci
->n
);
710 float n
= 1.0f
/((float)similar
+1.0f
);
711 v3_muls( ci
->n
, n
, ci
->n
);
714 if( v3_length2(ci
->n
) < 0.1f
*0.1f
)
715 ci
->type
= k_contact_type_disabled
;
717 v3_normalize( ci
->n
);
723 * Remove contacts that are facing away from A
725 VG_STATIC
void rb_manifold_filter_backface( rb_ct
*man
, int len
)
727 for( int i
=0; i
<len
; i
++ ){
729 if( ct
->type
== k_contact_type_disabled
)
733 v3_sub( ct
->co
, ct
->rba
->co
, delta
);
735 if( v3_dot( delta
, ct
->n
) > -0.001f
)
736 ct
->type
= k_contact_type_disabled
;
741 * Filter out duplicate coplanar results. Good for spheres.
743 VG_STATIC
void rb_manifold_filter_coplanar( rb_ct
*man
, int len
, float w
)
745 for( int i
=0; i
<len
; i
++ ){
747 if( ci
->type
== k_contact_type_disabled
||
748 ci
->type
== k_contact_type_edge
)
751 float d1
= v3_dot( ci
->co
, ci
->n
);
753 for( int j
=0; j
<len
; j
++ ){
758 if( cj
->type
== k_contact_type_disabled
)
761 float d2
= v3_dot( cj
->co
, ci
->n
),
764 if( fabsf( d
) <= w
){
765 cj
->type
= k_contact_type_disabled
;
772 * -----------------------------------------------------------------------------
774 * -----------------------------------------------------------------------------
780 * These do not automatically allocate contacts, an appropriately sized
781 * buffer must be supplied. The function returns the size of the manifold
782 * which was generated.
784 * The values set on the contacts are: n, co, p, rba, rbb
788 * By collecting the minimum(time) and maximum(time) pairs of points, we
789 * build a reduced and stable exact manifold.
792 * rx: minimum distance of these points
793 * dx: the delta between the two points
795 * pairs will only ammend these if they are creating a collision
797 typedef struct capsule_manifold capsule_manifold
;
798 struct capsule_manifold
806 * Expand a line manifold with a new pair. t value is the time along segment
807 * on the oriented object which created this pair.
809 VG_STATIC
void rb_capsule_manifold( v3f pa
, v3f pb
, float t
, float r
,
810 capsule_manifold
*manifold
)
813 v3_sub( pa
, pb
, delta
);
815 if( v3_length2(delta
) < r
*r
){
816 if( t
< manifold
->t0
){
817 v3_copy( delta
, manifold
->d0
);
822 if( t
> manifold
->t1
){
823 v3_copy( delta
, manifold
->d1
);
830 VG_STATIC
void rb_capsule_manifold_init( capsule_manifold
*manifold
)
832 manifold
->t0
= INFINITY
;
833 manifold
->t1
= -INFINITY
;
836 VG_STATIC
int rb_capsule__manifold_done( m4x3f mtx
, rb_capsule
*c
,
837 capsule_manifold
*manifold
,
841 v3_muladds( mtx
[3], mtx
[1], -c
->height
*0.5f
+c
->radius
, p0
);
842 v3_muladds( mtx
[3], mtx
[1], c
->height
*0.5f
-c
->radius
, p1
);
845 if( manifold
->t0
<= 1.0f
){
849 v3_muls( p0
, 1.0f
-manifold
->t0
, pa
);
850 v3_muladds( pa
, p1
, manifold
->t0
, pa
);
852 float d
= v3_length( manifold
->d0
);
853 v3_muls( manifold
->d0
, 1.0f
/d
, ct
->n
);
854 v3_muladds( pa
, ct
->n
, -c
->radius
, ct
->co
);
856 ct
->p
= manifold
->r0
- d
;
857 ct
->type
= k_contact_type_default
;
861 if( (manifold
->t1
>= 0.0f
) && (manifold
->t0
!= manifold
->t1
) ){
862 rb_ct
*ct
= buf
+count
;
865 v3_muls( p0
, 1.0f
-manifold
->t1
, pa
);
866 v3_muladds( pa
, p1
, manifold
->t1
, pa
);
868 float d
= v3_length( manifold
->d1
);
869 v3_muls( manifold
->d1
, 1.0f
/d
, ct
->n
);
870 v3_muladds( pa
, ct
->n
, -c
->radius
, ct
->co
);
872 ct
->p
= manifold
->r1
- d
;
873 ct
->type
= k_contact_type_default
;
883 vg_line( buf
[0].co
, buf
[1].co
, 0xff0000ff );
888 VG_STATIC
int rb_capsule_sphere( rb_object
*obja
, rb_object
*objb
, rb_ct
*buf
)
890 rigidbody
*rba
= &obja
->rb
, *rbb
= &objb
->rb
;
891 float h
= obja
->inf
.capsule
.height
,
892 ra
= obja
->inf
.capsule
.radius
,
893 rb
= objb
->inf
.sphere
.radius
;
896 v3_muladds( rba
->co
, rba
->to_world
[1], -h
*0.5f
+ra
, p0
);
897 v3_muladds( rba
->co
, rba
->to_world
[1], h
*0.5f
-ra
, p1
);
900 closest_point_segment( p0
, p1
, rbb
->co
, c
);
901 v3_sub( c
, rbb
->co
, delta
);
903 float d2
= v3_length2(delta
),
910 v3_muls( delta
, 1.0f
/d
, ct
->n
);
914 v3_muladds( c
, ct
->n
, -ra
, p0
);
915 v3_muladds( rbb
->co
, ct
->n
, rb
, p1
);
916 v3_add( p0
, p1
, ct
->co
);
917 v3_muls( ct
->co
, 0.5f
, ct
->co
);
921 ct
->type
= k_contact_type_default
;
929 VG_STATIC
int rb_capsule__capsule( m4x3f mtxA
, rb_capsule
*ca
,
930 m4x3f mtxB
, rb_capsule
*cb
, rb_ct
*buf
)
932 float ha
= ca
->height
,
939 v3_muladds( mtxA
[3], mtxA
[1], -ha
*0.5f
+ra
, p0
);
940 v3_muladds( mtxA
[3], mtxA
[1], ha
*0.5f
-ra
, p1
);
941 v3_muladds( mtxB
[3], mtxB
[1], -hb
*0.5f
+rb
, p2
);
942 v3_muladds( mtxB
[3], mtxB
[1], hb
*0.5f
-rb
, p3
);
944 capsule_manifold manifold
;
945 rb_capsule_manifold_init( &manifold
);
949 closest_segment_segment( p0
, p1
, p2
, p3
, &ta
, &tb
, pa
, pb
);
950 rb_capsule_manifold( pa
, pb
, ta
, r
, &manifold
);
952 ta
= closest_point_segment( p0
, p1
, p2
, pa
);
953 tb
= closest_point_segment( p0
, p1
, p3
, pb
);
954 rb_capsule_manifold( pa
, p2
, ta
, r
, &manifold
);
955 rb_capsule_manifold( pb
, p3
, tb
, r
, &manifold
);
957 closest_point_segment( p2
, p3
, p0
, pa
);
958 closest_point_segment( p2
, p3
, p1
, pb
);
959 rb_capsule_manifold( p0
, pa
, 0.0f
, r
, &manifold
);
960 rb_capsule_manifold( p1
, pb
, 1.0f
, r
, &manifold
);
962 return rb_capsule__manifold_done( mtxA
, ca
, &manifold
, buf
);
967 * Generates up to two contacts; optimised for the most stable manifold
969 VG_STATIC
int rb_capsule_box( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
971 float h
= rba
->inf
.capsule
.height
,
972 r
= rba
->inf
.capsule
.radius
;
975 * Solving this in symetric local space of the cube saves us some time and a
976 * couple branches when it comes to the quad stage.
979 v3_add( rbb
->bbx
[0], rbb
->bbx
[1], centroid
);
980 v3_muls( centroid
, 0.5f
, centroid
);
983 v3_sub( rbb
->bbx
[0], centroid
, bbx
[0] );
984 v3_sub( rbb
->bbx
[1], centroid
, bbx
[1] );
986 v3f pc
, p0w
, p1w
, p0
, p1
;
987 v3_muladds( rba
->co
, rba
->up
, -h
*0.5f
+r
, p0w
);
988 v3_muladds( rba
->co
, rba
->up
, h
*0.5f
-r
, p1w
);
990 m4x3_mulv( rbb
->to_local
, p0w
, p0
);
991 m4x3_mulv( rbb
->to_local
, p1w
, p1
);
992 v3_sub( p0
, centroid
, p0
);
993 v3_sub( p1
, centroid
, p1
);
994 v3_add( p0
, p1
, pc
);
995 v3_muls( pc
, 0.5f
, pc
);
998 * Finding an appropriate quad to collide lines with
1001 v3_div( pc
, bbx
[1], region
);
1004 if( (fabsf(region
[0]) > fabsf(region
[1])) &&
1005 (fabsf(region
[0]) > fabsf(region
[2])) )
1007 float px
= vg_signf(region
[0]) * bbx
[1][0];
1008 v3_copy( (v3f
){ px
, bbx
[0][1], bbx
[0][2] }, quad
[0] );
1009 v3_copy( (v3f
){ px
, bbx
[1][1], bbx
[0][2] }, quad
[1] );
1010 v3_copy( (v3f
){ px
, bbx
[1][1], bbx
[1][2] }, quad
[2] );
1011 v3_copy( (v3f
){ px
, bbx
[0][1], bbx
[1][2] }, quad
[3] );
1013 else if( fabsf(region
[1]) > fabsf(region
[2]) )
1015 float py
= vg_signf(region
[1]) * bbx
[1][1];
1016 v3_copy( (v3f
){ bbx
[0][0], py
, bbx
[0][2] }, quad
[0] );
1017 v3_copy( (v3f
){ bbx
[1][0], py
, bbx
[0][2] }, quad
[1] );
1018 v3_copy( (v3f
){ bbx
[1][0], py
, bbx
[1][2] }, quad
[2] );
1019 v3_copy( (v3f
){ bbx
[0][0], py
, bbx
[1][2] }, quad
[3] );
1023 float pz
= vg_signf(region
[2]) * bbx
[1][2];
1024 v3_copy( (v3f
){ bbx
[0][0], bbx
[0][1], pz
}, quad
[0] );
1025 v3_copy( (v3f
){ bbx
[1][0], bbx
[0][1], pz
}, quad
[1] );
1026 v3_copy( (v3f
){ bbx
[1][0], bbx
[1][1], pz
}, quad
[2] );
1027 v3_copy( (v3f
){ bbx
[0][0], bbx
[1][1], pz
}, quad
[3] );
1030 capsule_manifold manifold
;
1031 rb_capsule_manifold_init( &manifold
);
1034 closest_point_aabb( p0
, bbx
, c0
);
1035 closest_point_aabb( p1
, bbx
, c1
);
1038 v3_sub( c0
, p0
, d0
);
1039 v3_sub( c1
, p1
, d1
);
1040 v3_sub( p1
, p0
, da
);
1046 if( v3_dot( da
, d0
) <= 0.01f
)
1047 rb_capsule_manifold( p0
, c0
, 0.0f
, r
, &manifold
);
1049 if( v3_dot( da
, d1
) >= -0.01f
)
1050 rb_capsule_manifold( p1
, c1
, 1.0f
, r
, &manifold
);
1052 for( int i
=0; i
<4; i
++ )
1059 closest_segment_segment( p0
, p1
, quad
[i0
], quad
[i1
], &ta
, &tb
, ca
, cb
);
1060 rb_capsule_manifold( ca
, cb
, ta
, r
, &manifold
);
1064 * Create final contacts based on line manifold
1066 m3x3_mulv( rbb
->to_world
, manifold
.d0
, manifold
.d0
);
1067 m3x3_mulv( rbb
->to_world
, manifold
.d1
, manifold
.d1
);
1074 for( int i
=0; i
<4; i
++ )
1080 v3_add( quad
[i0
], centroid
, q0
);
1081 v3_add( quad
[i1
], centroid
, q1
);
1083 m4x3_mulv( rbb
->to_world
, q0
, q0
);
1084 m4x3_mulv( rbb
->to_world
, q1
, q1
);
1086 vg_line( q0
, q1
, 0xffffffff );
1090 return rb_capsule_manifold_done( rba
, rbb
, &manifold
, buf
);
1094 VG_STATIC
int rb_sphere_box( rb_object
*obja
, rb_object
*objb
, rb_ct
*buf
)
1097 rigidbody
*rba
= &obja
->rb
, *rbb
= &objb
->rb
;
1099 closest_point_obb( rba
->co
, rbb
->bbx
, rbb
->to_world
, rbb
->to_local
, co
);
1100 v3_sub( rba
->co
, co
, delta
);
1102 float d2
= v3_length2(delta
),
1103 r
= obja
->inf
.sphere
.radius
;
1109 if( d2
<= 0.0001f
){
1110 v3_sub( rba
->co
, rbb
->co
, delta
);
1113 * some extra testing is required to find the best axis to push the
1114 * object back outside the box. Since there isnt a clear seperating
1115 * vector already, especially on really high aspect boxes.
1117 float lx
= v3_dot( rbb
->to_world
[0], delta
),
1118 ly
= v3_dot( rbb
->to_world
[1], delta
),
1119 lz
= v3_dot( rbb
->to_world
[2], delta
),
1120 px
= rbb
->bbx
[1][0] - fabsf(lx
),
1121 py
= rbb
->bbx
[1][1] - fabsf(ly
),
1122 pz
= rbb
->bbx
[1][2] - fabsf(lz
);
1124 if( px
< py
&& px
< pz
)
1125 v3_muls( rbb
->to_world
[0], vg_signf(lx
), ct
->n
);
1127 v3_muls( rbb
->to_world
[1], vg_signf(ly
), ct
->n
);
1129 v3_muls( rbb
->to_world
[2], vg_signf(lz
), ct
->n
);
1131 v3_muladds( rba
->co
, ct
->n
, -r
, ct
->co
);
1136 v3_muls( delta
, 1.0f
/d
, ct
->n
);
1138 v3_copy( co
, ct
->co
);
1143 ct
->type
= k_contact_type_default
;
1150 VG_STATIC
int rb_sphere_sphere( rb_object
*obja
, rb_object
*objb
, rb_ct
*buf
)
1152 rigidbody
*rba
= &obja
->rb
, *rbb
= &objb
->rb
;
1154 v3_sub( rba
->co
, rbb
->co
, delta
);
1156 float d2
= v3_length2(delta
),
1157 r
= obja
->inf
.sphere
.radius
+ objb
->inf
.sphere
.radius
;
1160 float d
= sqrtf(d2
);
1163 v3_muls( delta
, 1.0f
/d
, ct
->n
);
1166 v3_muladds( rba
->co
, ct
->n
,-obja
->inf
.sphere
.radius
, p0
);
1167 v3_muladds( rbb
->co
, ct
->n
, objb
->inf
.sphere
.radius
, p1
);
1168 v3_add( p0
, p1
, ct
->co
);
1169 v3_muls( ct
->co
, 0.5f
, ct
->co
);
1170 ct
->type
= k_contact_type_default
;
1180 //#define RIGIDBODY_DYNAMIC_MESH_EDGES
1183 __attribute__ ((deprecated
))
1184 VG_STATIC
int rb_sphere_triangle( rigidbody
*rba
, rigidbody
*rbb
,
1185 v3f tri
[3], rb_ct
*buf
)
1189 #ifdef RIGIDBODY_DYNAMIC_MESH_EDGES
1190 closest_on_triangle_1( rba
->co
, tri
, co
);
1192 enum contact_type type
= closest_on_triangle_1( rba
->co
, tri
, co
);
1195 v3_sub( rba
->co
, co
, delta
);
1197 float d2
= v3_length2( delta
),
1198 r
= rba
->inf
.sphere
.radius
;
1205 v3_sub( tri
[2], tri
[0], ab
);
1206 v3_sub( tri
[1], tri
[0], ac
);
1207 v3_cross( ac
, ab
, tn
);
1208 v3_copy( tn
, ct
->n
);
1210 if( v3_length2( ct
->n
) <= 0.00001f
)
1212 vg_error( "Zero area triangle!\n" );
1216 v3_normalize( ct
->n
);
1218 float d
= sqrtf(d2
);
1220 v3_copy( co
, ct
->co
);
1232 VG_STATIC
int rb_sphere__triangle( m4x3f mtxA
, rb_sphere
*b
,
1233 v3f tri
[3], rb_ct
*buf
)
1236 enum contact_type type
= closest_on_triangle_1( mtxA
[3], tri
, co
);
1238 v3_sub( mtxA
[3], co
, delta
);
1240 float d2
= v3_length2( delta
),
1247 v3_sub( tri
[2], tri
[0], ab
);
1248 v3_sub( tri
[1], tri
[0], ac
);
1249 v3_cross( ac
, ab
, tn
);
1250 v3_copy( tn
, ct
->n
);
1252 if( v3_length2( ct
->n
) <= 0.00001f
){
1253 vg_error( "Zero area triangle!\n" );
1257 v3_normalize( ct
->n
);
1259 float d
= sqrtf(d2
);
1261 v3_copy( co
, ct
->co
);
1270 VG_STATIC
int rb_sphere__scene( m4x3f mtxA
, rb_sphere
*b
,
1271 m4x3f mtxB
, rb_scene
*s
, rb_ct
*buf
)
1273 scene
*sc
= s
->bh_scene
->user
;
1276 bh_iter_init( 0, &it
);
1281 float r
= b
->radius
+ 0.1f
;
1283 v3_sub( mtxA
[3], (v3f
){ r
,r
,r
}, box
[0] );
1284 v3_add( mtxA
[3], (v3f
){ r
,r
,r
}, box
[1] );
1286 while( bh_next( s
->bh_scene
, &it
, box
, &idx
) ){
1287 u32
*ptri
= &sc
->arrindices
[ idx
*3 ];
1290 for( int j
=0; j
<3; j
++ )
1291 v3_copy( sc
->arrvertices
[ptri
[j
]].co
, tri
[j
] );
1293 buf
[ count
].element_id
= ptri
[0];
1295 vg_line( tri
[0],tri
[1],0x70ff6000 );
1296 vg_line( tri
[1],tri
[2],0x70ff6000 );
1297 vg_line( tri
[2],tri
[0],0x70ff6000 );
1299 int contact
= rb_sphere__triangle( mtxA
, b
, tri
, &buf
[count
] );
1303 vg_warn( "Exceeding sphere_vs_scene capacity. Geometry too dense!\n" );
1311 VG_STATIC
int rb_box__scene( m4x3f mtxA
, boxf bbx
,
1312 m4x3f mtxB
, rb_scene
*s
, rb_ct
*buf
)
1314 scene
*sc
= s
->bh_scene
->user
;
1318 v3_sub( bbx
[1], bbx
[0], extent
);
1319 v3_muls( extent
, 0.5f
, extent
);
1320 v3_add( bbx
[0], extent
, center
);
1322 float r
= v3_length(extent
);
1324 v3_fill( world_bbx
[0], -r
);
1325 v3_fill( world_bbx
[1], r
);
1326 for( int i
=0; i
<2; i
++ ){
1327 v3_add( center
, world_bbx
[i
], world_bbx
[i
] );
1328 v3_add( mtxA
[3], world_bbx
[i
], world_bbx
[i
] );
1332 m4x3_invert_affine( mtxA
, to_local
);
1335 bh_iter_init( 0, &it
);
1339 vg_line_boxf( world_bbx
, VG__RED
);
1341 while( bh_next( s
->bh_scene
, &it
, world_bbx
, &idx
) ){
1342 u32
*ptri
= &sc
->arrindices
[ idx
*3 ];
1344 for( int j
=0; j
<3; j
++ )
1345 v3_copy( sc
->arrvertices
[ptri
[j
]].co
, tri
[j
] );
1347 if( rb_box_triangle_sat( extent
, center
, to_local
, tri
) ){
1348 vg_line(tri
[0],tri
[1],0xff50ff00 );
1349 vg_line(tri
[1],tri
[2],0xff50ff00 );
1350 vg_line(tri
[2],tri
[0],0xff50ff00 );
1353 vg_line(tri
[0],tri
[1],0xff0000ff );
1354 vg_line(tri
[1],tri
[2],0xff0000ff );
1355 vg_line(tri
[2],tri
[0],0xff0000ff );
1360 v3_sub( tri
[1], tri
[0], v0
);
1361 v3_sub( tri
[2], tri
[0], v1
);
1362 v3_cross( v0
, v1
, n
);
1365 /* find best feature */
1366 float best
= v3_dot( mtxA
[0], n
);
1369 for( int i
=1; i
<3; i
++ ){
1370 float c
= v3_dot( mtxA
[i
], n
);
1372 if( fabsf(c
) > fabsf(best
) ){
1381 float px
= best
> 0.0f
? bbx
[0][0]: bbx
[1][0];
1382 manifold
[0][0] = px
;
1383 manifold
[0][1] = bbx
[0][1];
1384 manifold
[0][2] = bbx
[0][2];
1385 manifold
[1][0] = px
;
1386 manifold
[1][1] = bbx
[1][1];
1387 manifold
[1][2] = bbx
[0][2];
1388 manifold
[2][0] = px
;
1389 manifold
[2][1] = bbx
[1][1];
1390 manifold
[2][2] = bbx
[1][2];
1391 manifold
[3][0] = px
;
1392 manifold
[3][1] = bbx
[0][1];
1393 manifold
[3][2] = bbx
[1][2];
1395 else if( axis
== 1 ){
1396 float py
= best
> 0.0f
? bbx
[0][1]: bbx
[1][1];
1397 manifold
[0][0] = bbx
[0][0];
1398 manifold
[0][1] = py
;
1399 manifold
[0][2] = bbx
[0][2];
1400 manifold
[1][0] = bbx
[1][0];
1401 manifold
[1][1] = py
;
1402 manifold
[1][2] = bbx
[0][2];
1403 manifold
[2][0] = bbx
[1][0];
1404 manifold
[2][1] = py
;
1405 manifold
[2][2] = bbx
[1][2];
1406 manifold
[3][0] = bbx
[0][0];
1407 manifold
[3][1] = py
;
1408 manifold
[3][2] = bbx
[1][2];
1411 float pz
= best
> 0.0f
? bbx
[0][2]: bbx
[1][2];
1412 manifold
[0][0] = bbx
[0][0];
1413 manifold
[0][1] = bbx
[0][1];
1414 manifold
[0][2] = pz
;
1415 manifold
[1][0] = bbx
[1][0];
1416 manifold
[1][1] = bbx
[0][1];
1417 manifold
[1][2] = pz
;
1418 manifold
[2][0] = bbx
[1][0];
1419 manifold
[2][1] = bbx
[1][1];
1420 manifold
[2][2] = pz
;
1421 manifold
[3][0] = bbx
[0][0];
1422 manifold
[3][1] = bbx
[1][1];
1423 manifold
[3][2] = pz
;
1426 for( int j
=0; j
<4; j
++ )
1427 m4x3_mulv( mtxA
, manifold
[j
], manifold
[j
] );
1429 vg_line( manifold
[0], manifold
[1], 0xffffffff );
1430 vg_line( manifold
[1], manifold
[2], 0xffffffff );
1431 vg_line( manifold
[2], manifold
[3], 0xffffffff );
1432 vg_line( manifold
[3], manifold
[0], 0xffffffff );
1434 for( int j
=0; j
<4; j
++ ){
1435 rb_ct
*ct
= buf
+count
;
1437 v3_copy( manifold
[j
], ct
->co
);
1438 v3_copy( n
, ct
->n
);
1440 float l0
= v3_dot( tri
[0], n
),
1441 l1
= v3_dot( manifold
[j
], n
);
1443 ct
->p
= (l0
-l1
)*0.5f
;
1447 ct
->type
= k_contact_type_default
;
1457 VG_STATIC
int rb_capsule__triangle( m4x3f mtxA
, rb_capsule
*c
,
1458 v3f tri
[3], rb_ct
*buf
)
1461 v3_muladds( mtxA
[3], mtxA
[1], -c
->height
*0.5f
+c
->radius
, p0w
);
1462 v3_muladds( mtxA
[3], mtxA
[1], c
->height
*0.5f
-c
->radius
, p1w
);
1464 capsule_manifold manifold
;
1465 rb_capsule_manifold_init( &manifold
);
1468 closest_on_triangle_1( p0w
, tri
, c0
);
1469 closest_on_triangle_1( p1w
, tri
, c1
);
1472 v3_sub( c0
, p0w
, d0
);
1473 v3_sub( c1
, p1w
, d1
);
1474 v3_sub( p1w
, p0w
, da
);
1480 if( v3_dot( da
, d0
) <= 0.01f
)
1481 rb_capsule_manifold( p0w
, c0
, 0.0f
, c
->radius
, &manifold
);
1483 if( v3_dot( da
, d1
) >= -0.01f
)
1484 rb_capsule_manifold( p1w
, c1
, 1.0f
, c
->radius
, &manifold
);
1486 for( int i
=0; i
<3; i
++ ){
1492 closest_segment_segment( p0w
, p1w
, tri
[i0
], tri
[i1
], &ta
, &tb
, ca
, cb
);
1493 rb_capsule_manifold( ca
, cb
, ta
, c
->radius
, &manifold
);
1497 v3_sub( tri
[1], tri
[0], v0
);
1498 v3_sub( tri
[2], tri
[0], v1
);
1499 v3_cross( v0
, v1
, n
);
1502 int count
= rb_capsule__manifold_done( mtxA
, c
, &manifold
, buf
);
1503 for( int i
=0; i
<count
; i
++ )
1504 v3_copy( n
, buf
[i
].n
);
1509 /* mtxB is defined only for tradition; it is not used currently */
1510 VG_STATIC
int rb_capsule__scene( m4x3f mtxA
, rb_capsule
*c
,
1511 m4x3f mtxB
, rb_scene
*s
,
1515 bh_iter_init( 0, &it
);
1520 v3_sub( mtxA
[3], (v3f
){ c
->height
, c
->height
, c
->height
}, bbx
[0] );
1521 v3_add( mtxA
[3], (v3f
){ c
->height
, c
->height
, c
->height
}, bbx
[1] );
1523 scene
*sc
= s
->bh_scene
->user
;
1525 while( bh_next( s
->bh_scene
, &it
, bbx
, &idx
) ){
1526 u32
*ptri
= &sc
->arrindices
[ idx
*3 ];
1529 for( int j
=0; j
<3; j
++ )
1530 v3_copy( sc
->arrvertices
[ptri
[j
]].co
, tri
[j
] );
1532 buf
[ count
].element_id
= ptri
[0];
1534 int contact
= rb_capsule__triangle( mtxA
, c
, tri
, &buf
[count
] );
1538 vg_warn("Exceeding capsule_vs_scene capacity. Geometry too dense!\n");
1546 VG_STATIC
int rb_global_has_space( void )
1548 if( rb_contact_count
+ 16 > vg_list_size(rb_contact_buffer
) )
1554 VG_STATIC rb_ct
*rb_global_buffer( void )
1556 return &rb_contact_buffer
[ rb_contact_count
];
1560 * -----------------------------------------------------------------------------
1562 * -----------------------------------------------------------------------------
1565 VG_STATIC
void rb_solver_reset(void)
1567 rb_contact_count
= 0;
1570 VG_STATIC rb_ct
*rb_global_ct(void)
1572 return rb_contact_buffer
+ rb_contact_count
;
1575 VG_STATIC
void rb_prepare_contact( rb_ct
*ct
, float timestep
)
1577 ct
->bias
= -0.2f
* (timestep
*3600.0f
)
1578 * vg_minf( 0.0f
, -ct
->p
+k_penetration_slop
);
1580 rb_tangent_basis( ct
->n
, ct
->t
[0], ct
->t
[1] );
1581 ct
->norm_impulse
= 0.0f
;
1582 ct
->tangent_impulse
[0] = 0.0f
;
1583 ct
->tangent_impulse
[1] = 0.0f
;
1586 /* calculate total move. manifold should belong to ONE object only */
1587 VG_STATIC
void rb_depenetrate( rb_ct
*manifold
, int len
, v3f dt
)
1591 for( int j
=0; j
<7; j
++ )
1593 for( int i
=0; i
<len
; i
++ )
1595 struct contact
*ct
= &manifold
[i
];
1597 float resolved_amt
= v3_dot( ct
->n
, dt
),
1598 remaining
= (ct
->p
-k_penetration_slop
) - resolved_amt
,
1599 apply
= vg_maxf( remaining
, 0.0f
) * 0.4f
;
1601 v3_muladds( dt
, ct
->n
, apply
, dt
);
1607 * Initializing things like tangent vectors
1609 VG_STATIC
void rb_presolve_contacts( rb_ct
*buffer
, int len
)
1611 for( int i
=0; i
<len
; i
++ ){
1612 rb_ct
*ct
= &buffer
[i
];
1613 rb_prepare_contact( ct
, k_rb_delta
);
1615 v3f ra
, rb
, raCn
, rbCn
, raCt
, rbCt
;
1616 v3_sub( ct
->co
, ct
->rba
->co
, ra
);
1617 v3_sub( ct
->co
, ct
->rbb
->co
, rb
);
1618 v3_cross( ra
, ct
->n
, raCn
);
1619 v3_cross( rb
, ct
->n
, rbCn
);
1621 /* orient inverse inertia tensors */
1623 m3x3_mulv( ct
->rba
->iIw
, raCn
, raCnI
);
1624 m3x3_mulv( ct
->rbb
->iIw
, rbCn
, rbCnI
);
1626 ct
->normal_mass
= ct
->rba
->inv_mass
+ ct
->rbb
->inv_mass
;
1627 ct
->normal_mass
+= v3_dot( raCn
, raCnI
);
1628 ct
->normal_mass
+= v3_dot( rbCn
, rbCnI
);
1629 ct
->normal_mass
= 1.0f
/ct
->normal_mass
;
1631 for( int j
=0; j
<2; j
++ ){
1633 v3_cross( ct
->t
[j
], ra
, raCt
);
1634 v3_cross( ct
->t
[j
], rb
, rbCt
);
1635 m3x3_mulv( ct
->rba
->iIw
, raCt
, raCtI
);
1636 m3x3_mulv( ct
->rbb
->iIw
, rbCt
, rbCtI
);
1638 ct
->tangent_mass
[j
] = ct
->rba
->inv_mass
+ ct
->rbb
->inv_mass
;
1639 ct
->tangent_mass
[j
] += v3_dot( raCt
, raCtI
);
1640 ct
->tangent_mass
[j
] += v3_dot( rbCt
, rbCtI
);
1641 ct
->tangent_mass
[j
] = 1.0f
/ct
->tangent_mass
[j
];
1644 rb_debug_contact( ct
);
1649 * Creates relative contact velocity vector
1651 VG_STATIC
void rb_rcv( rigidbody
*rba
, rigidbody
*rbb
, v3f ra
, v3f rb
, v3f rv
)
1654 v3_cross( rba
->w
, ra
, rva
);
1655 v3_add( rba
->v
, rva
, rva
);
1656 v3_cross( rbb
->w
, rb
, rvb
);
1657 v3_add( rbb
->v
, rvb
, rvb
);
1659 v3_sub( rva
, rvb
, rv
);
1663 * Apply impulse to object
1665 VG_STATIC
void rb_linear_impulse( rigidbody
*rb
, v3f delta
, v3f impulse
)
1668 v3_muladds( rb
->v
, impulse
, rb
->inv_mass
, rb
->v
);
1670 /* Angular velocity */
1672 v3_cross( delta
, impulse
, wa
);
1674 m3x3_mulv( rb
->iIw
, wa
, wa
);
1675 v3_add( rb
->w
, wa
, rb
->w
);
1679 * One iteration to solve the contact constraint
1681 VG_STATIC
void rb_solve_contacts( rb_ct
*buf
, int len
)
1683 for( int i
=0; i
<len
; i
++ ){
1684 struct contact
*ct
= &buf
[i
];
1687 v3_sub( ct
->co
, ct
->rba
->co
, ra
);
1688 v3_sub( ct
->co
, ct
->rbb
->co
, rb
);
1689 rb_rcv( ct
->rba
, ct
->rbb
, ra
, rb
, rv
);
1692 for( int j
=0; j
<2; j
++ ){
1693 float f
= k_friction
* ct
->norm_impulse
,
1694 vt
= v3_dot( rv
, ct
->t
[j
] ),
1695 lambda
= ct
->tangent_mass
[j
] * -vt
;
1697 float temp
= ct
->tangent_impulse
[j
];
1698 ct
->tangent_impulse
[j
] = vg_clampf( temp
+ lambda
, -f
, f
);
1699 lambda
= ct
->tangent_impulse
[j
] - temp
;
1702 v3_muls( ct
->t
[j
], lambda
, impulse
);
1703 rb_linear_impulse( ct
->rba
, ra
, impulse
);
1705 v3_muls( ct
->t
[j
], -lambda
, impulse
);
1706 rb_linear_impulse( ct
->rbb
, rb
, impulse
);
1710 rb_rcv( ct
->rba
, ct
->rbb
, ra
, rb
, rv
);
1711 float vn
= v3_dot( rv
, ct
->n
),
1712 lambda
= ct
->normal_mass
* (-vn
+ ct
->bias
);
1714 float temp
= ct
->norm_impulse
;
1715 ct
->norm_impulse
= vg_maxf( temp
+ lambda
, 0.0f
);
1716 lambda
= ct
->norm_impulse
- temp
;
1719 v3_muls( ct
->n
, lambda
, impulse
);
1720 rb_linear_impulse( ct
->rba
, ra
, impulse
);
1722 v3_muls( ct
->n
, -lambda
, impulse
);
1723 rb_linear_impulse( ct
->rbb
, rb
, impulse
);
1728 * -----------------------------------------------------------------------------
1730 * -----------------------------------------------------------------------------
1733 VG_STATIC
void rb_debug_position_constraints( rb_constr_pos
*buffer
, int len
)
1735 for( int i
=0; i
<len
; i
++ ){
1736 rb_constr_pos
*constr
= &buffer
[i
];
1737 rigidbody
*rba
= constr
->rba
, *rbb
= constr
->rbb
;
1740 m3x3_mulv( rba
->to_world
, constr
->lca
, wca
);
1741 m3x3_mulv( rbb
->to_world
, constr
->lcb
, wcb
);
1744 v3_add( wca
, rba
->co
, p0
);
1745 v3_add( wcb
, rbb
->co
, p1
);
1746 vg_line_pt3( p0
, 0.0025f
, 0xff000000 );
1747 vg_line_pt3( p1
, 0.0025f
, 0xffffffff );
1748 vg_line2( p0
, p1
, 0xff000000, 0xffffffff );
1752 VG_STATIC
void rb_presolve_swingtwist_constraints( rb_constr_swingtwist
*buf
,
1757 for( int i
=0; i
<len
; i
++ ){
1758 rb_constr_swingtwist
*st
= &buf
[ i
];
1760 v3f vx
, vy
, va
, vxb
, axis
, center
;
1762 m3x3_mulv( st
->rba
->to_world
, st
->conevx
, vx
);
1763 m3x3_mulv( st
->rbb
->to_world
, st
->conevxb
, vxb
);
1764 m3x3_mulv( st
->rba
->to_world
, st
->conevy
, vy
);
1765 m3x3_mulv( st
->rbb
->to_world
, st
->coneva
, va
);
1766 m4x3_mulv( st
->rba
->to_world
, st
->view_offset
, center
);
1767 v3_cross( vy
, vx
, axis
);
1769 /* Constraint violated ? */
1770 float fx
= v3_dot( vx
, va
), /* projection world */
1771 fy
= v3_dot( vy
, va
),
1772 fn
= v3_dot( va
, axis
),
1774 rx
= st
->conevx
[3], /* elipse radii */
1777 lx
= fx
/rx
, /* projection local (fn==lz) */
1780 st
->tangent_violation
= ((lx
*lx
+ ly
*ly
) > fn
*fn
) || (fn
<= 0.0f
);
1781 if( st
->tangent_violation
){
1782 /* Calculate a good position and the axis to solve on */
1783 v2f closest
, tangent
,
1784 p
= { fx
/fabsf(fn
), fy
/fabsf(fn
) };
1786 closest_point_elipse( p
, (v2f
){rx
,ry
}, closest
);
1787 tangent
[0] = -closest
[1] / (ry
*ry
);
1788 tangent
[1] = closest
[0] / (rx
*rx
);
1789 v2_normalize( tangent
);
1792 v3_muladds( axis
, vx
, closest
[0], v0
);
1793 v3_muladds( v0
, vy
, closest
[1], v0
);
1796 v3_muls( vx
, tangent
[0], v1
);
1797 v3_muladds( v1
, vy
, tangent
[1], v1
);
1799 v3_copy( v0
, st
->tangent_target
);
1800 v3_copy( v1
, st
->tangent_axis
);
1802 /* calculate mass */
1804 m3x3_mulv( st
->rba
->iIw
, st
->tangent_axis
, aIw
);
1805 m3x3_mulv( st
->rbb
->iIw
, st
->tangent_axis
, bIw
);
1806 st
->tangent_mass
= 1.0f
/ (v3_dot( st
->tangent_axis
, aIw
) +
1807 v3_dot( st
->tangent_axis
, bIw
));
1809 float angle
= v3_dot( va
, st
->tangent_target
);
1813 v3_cross( vy
, va
, refaxis
); /* our default rotation */
1814 v3_normalize( refaxis
);
1816 float angle
= v3_dot( refaxis
, vxb
);
1817 st
->axis_violation
= fabsf(angle
) < st
->conet
;
1819 if( st
->axis_violation
){
1821 v3_cross( refaxis
, vxb
, dir_test
);
1823 if( v3_dot(dir_test
, va
) < 0.0f
)
1824 st
->axis_violation
= -st
->axis_violation
;
1826 float newang
= (float)st
->axis_violation
* acosf(st
->conet
-0.0001f
);
1829 v3_cross( va
, refaxis
, refaxis_up
);
1830 v3_muls( refaxis_up
, sinf(newang
), st
->axis_target
);
1831 v3_muladds( st
->axis_target
, refaxis
, -cosf(newang
), st
->axis_target
);
1833 /* calculate mass */
1834 v3_copy( va
, st
->axis
);
1836 m3x3_mulv( st
->rba
->iIw
, st
->axis
, aIw
);
1837 m3x3_mulv( st
->rbb
->iIw
, st
->axis
, bIw
);
1838 st
->axis_mass
= 1.0f
/ (v3_dot( st
->axis
, aIw
) +
1839 v3_dot( st
->axis
, bIw
));
1844 VG_STATIC
void rb_debug_swingtwist_constraints( rb_constr_swingtwist
*buf
,
1849 for( int i
=0; i
<len
; i
++ ){
1850 rb_constr_swingtwist
*st
= &buf
[ i
];
1852 v3f vx
, vxb
, vy
, va
, axis
, center
;
1854 m3x3_mulv( st
->rba
->to_world
, st
->conevx
, vx
);
1855 m3x3_mulv( st
->rbb
->to_world
, st
->conevxb
, vxb
);
1856 m3x3_mulv( st
->rba
->to_world
, st
->conevy
, vy
);
1857 m3x3_mulv( st
->rbb
->to_world
, st
->coneva
, va
);
1858 m4x3_mulv( st
->rba
->to_world
, st
->view_offset
, center
);
1859 v3_cross( vy
, vx
, axis
);
1861 float rx
= st
->conevx
[3], /* elipse radii */
1865 v3_muladds( center
, va
, size
, p1
);
1866 vg_line( center
, p1
, 0xffffffff );
1867 vg_line_pt3( p1
, 0.00025f
, 0xffffffff );
1869 if( st
->tangent_violation
){
1870 v3_muladds( center
, st
->tangent_target
, size
, p0
);
1872 vg_line( center
, p0
, 0xff00ff00 );
1873 vg_line_pt3( p0
, 0.00025f
, 0xff00ff00 );
1874 vg_line( p1
, p0
, 0xff000000 );
1877 for( int x
=0; x
<32; x
++ ){
1878 float t0
= ((float)x
* (1.0f
/32.0f
)) * VG_TAUf
,
1879 t1
= (((float)x
+1.0f
) * (1.0f
/32.0f
)) * VG_TAUf
,
1886 v3_muladds( axis
, vx
, c0
*rx
, v0
);
1887 v3_muladds( v0
, vy
, s0
*ry
, v0
);
1888 v3_muladds( axis
, vx
, c1
*rx
, v1
);
1889 v3_muladds( v1
, vy
, s1
*ry
, v1
);
1894 v3_muladds( center
, v0
, size
, p0
);
1895 v3_muladds( center
, v1
, size
, p1
);
1897 u32 col0r
= fabsf(c0
) * 255.0f
,
1898 col0g
= fabsf(s0
) * 255.0f
,
1899 col1r
= fabsf(c1
) * 255.0f
,
1900 col1g
= fabsf(s1
) * 255.0f
,
1901 col
= st
->tangent_violation
? 0xff0000ff: 0xff000000,
1902 col0
= col
| (col0r
<<16) | (col0g
<< 8),
1903 col1
= col
| (col1r
<<16) | (col1g
<< 8);
1905 vg_line2( center
, p0
, VG__NONE
, col0
);
1906 vg_line2( p0
, p1
, col0
, col1
);
1910 v3_muladds( center
, va
, size
, p0
);
1911 v3_muladds( p0
, vxb
, size
, p1
);
1913 vg_line( p0
, p1
, 0xff0000ff );
1915 if( st
->axis_violation
){
1916 v3_muladds( p0
, st
->axis_target
, size
*1.25f
, p1
);
1917 vg_line( p0
, p1
, 0xffffff00 );
1918 vg_line_pt3( p1
, 0.0025f
, 0xffffff80 );
1922 v3_cross( vy
, va
, refaxis
); /* our default rotation */
1923 v3_normalize( refaxis
);
1925 v3_cross( va
, refaxis
, refaxis_up
);
1926 float newang
= acosf(st
->conet
-0.0001f
);
1928 v3_muladds( p0
, refaxis_up
, sinf(newang
)*size
, p1
);
1929 v3_muladds( p1
, refaxis
, -cosf(newang
)*size
, p1
);
1930 vg_line( p0
, p1
, 0xff000000 );
1932 v3_muladds( p0
, refaxis_up
, sinf(-newang
)*size
, p1
);
1933 v3_muladds( p1
, refaxis
, -cosf(-newang
)*size
, p1
);
1934 vg_line( p0
, p1
, 0xff404040 );
1939 * Solve a list of positional constraints
1941 VG_STATIC
void rb_solve_position_constraints( rb_constr_pos
*buf
, int len
)
1943 for( int i
=0; i
<len
; i
++ ){
1944 rb_constr_pos
*constr
= &buf
[i
];
1945 rigidbody
*rba
= constr
->rba
, *rbb
= constr
->rbb
;
1948 m3x3_mulv( rba
->to_world
, constr
->lca
, wa
);
1949 m3x3_mulv( rbb
->to_world
, constr
->lcb
, wb
);
1951 m3x3f ssra
, ssrat
, ssrb
, ssrbt
;
1953 m3x3_skew_symetric( ssrat
, wa
);
1954 m3x3_skew_symetric( ssrbt
, wb
);
1955 m3x3_transpose( ssrat
, ssra
);
1956 m3x3_transpose( ssrbt
, ssrb
);
1958 v3f b
, b_wa
, b_wb
, b_a
, b_b
;
1959 m3x3_mulv( ssra
, rba
->w
, b_wa
);
1960 m3x3_mulv( ssrb
, rbb
->w
, b_wb
);
1961 v3_add( rba
->v
, b_wa
, b
);
1962 v3_sub( b
, rbb
->v
, b
);
1963 v3_sub( b
, b_wb
, b
);
1964 v3_muls( b
, -1.0f
, b
);
1967 m3x3_diagonal( invMa
, rba
->inv_mass
);
1968 m3x3_diagonal( invMb
, rbb
->inv_mass
);
1971 m3x3_mul( ssra
, rba
->iIw
, ia
);
1972 m3x3_mul( ia
, ssrat
, ia
);
1973 m3x3_mul( ssrb
, rbb
->iIw
, ib
);
1974 m3x3_mul( ib
, ssrbt
, ib
);
1977 m3x3_add( invMa
, ia
, cma
);
1978 m3x3_add( invMb
, ib
, cmb
);
1981 m3x3_add( cma
, cmb
, A
);
1983 /* Solve Ax = b ( A^-1*b = x ) */
1986 m3x3_inv( A
, invA
);
1987 m3x3_mulv( invA
, b
, impulse
);
1989 v3f delta_va
, delta_wa
, delta_vb
, delta_wb
;
1991 m3x3_mul( rba
->iIw
, ssrat
, iwa
);
1992 m3x3_mul( rbb
->iIw
, ssrbt
, iwb
);
1994 m3x3_mulv( invMa
, impulse
, delta_va
);
1995 m3x3_mulv( invMb
, impulse
, delta_vb
);
1996 m3x3_mulv( iwa
, impulse
, delta_wa
);
1997 m3x3_mulv( iwb
, impulse
, delta_wb
);
1999 v3_add( rba
->v
, delta_va
, rba
->v
);
2000 v3_add( rba
->w
, delta_wa
, rba
->w
);
2001 v3_sub( rbb
->v
, delta_vb
, rbb
->v
);
2002 v3_sub( rbb
->w
, delta_wb
, rbb
->w
);
2006 VG_STATIC
void rb_solve_swingtwist_constraints( rb_constr_swingtwist
*buf
,
2011 for( int i
=0; i
<len
; i
++ ){
2012 rb_constr_swingtwist
*st
= &buf
[ i
];
2014 if( !st
->axis_violation
)
2017 float rv
= v3_dot( st
->axis
, st
->rbb
->w
) -
2018 v3_dot( st
->axis
, st
->rba
->w
);
2020 if( rv
* (float)st
->axis_violation
> 0.0f
)
2023 v3f impulse
, wa
, wb
;
2024 v3_muls( st
->axis
, rv
*st
->axis_mass
, impulse
);
2025 m3x3_mulv( st
->rba
->iIw
, impulse
, wa
);
2026 v3_add( st
->rba
->w
, wa
, st
->rba
->w
);
2028 v3_muls( impulse
, -1.0f
, impulse
);
2029 m3x3_mulv( st
->rbb
->iIw
, impulse
, wb
);
2030 v3_add( st
->rbb
->w
, wb
, st
->rbb
->w
);
2032 float rv2
= v3_dot( st
->axis
, st
->rbb
->w
) -
2033 v3_dot( st
->axis
, st
->rba
->w
);
2036 for( int i
=0; i
<len
; i
++ ){
2037 rb_constr_swingtwist
*st
= &buf
[ i
];
2039 if( !st
->tangent_violation
)
2042 float rv
= v3_dot( st
->tangent_axis
, st
->rbb
->w
) -
2043 v3_dot( st
->tangent_axis
, st
->rba
->w
);
2048 v3f impulse
, wa
, wb
;
2049 v3_muls( st
->tangent_axis
, rv
*st
->tangent_mass
, impulse
);
2050 m3x3_mulv( st
->rba
->iIw
, impulse
, wa
);
2051 v3_add( st
->rba
->w
, wa
, st
->rba
->w
);
2053 v3_muls( impulse
, -1.0f
, impulse
);
2054 m3x3_mulv( st
->rbb
->iIw
, impulse
, wb
);
2055 v3_add( st
->rbb
->w
, wb
, st
->rbb
->w
);
2057 float rv2
= v3_dot( st
->tangent_axis
, st
->rbb
->w
) -
2058 v3_dot( st
->tangent_axis
, st
->rba
->w
);
2062 VG_STATIC
void rb_solve_constr_angle( rigidbody
*rba
, rigidbody
*rbb
,
2065 m3x3f ssra
, ssrb
, ssrat
, ssrbt
;
2068 m3x3_skew_symetric( ssrat
, ra
);
2069 m3x3_skew_symetric( ssrbt
, rb
);
2070 m3x3_transpose( ssrat
, ssra
);
2071 m3x3_transpose( ssrbt
, ssrb
);
2073 m3x3_mul( ssra
, rba
->iIw
, cma
);
2074 m3x3_mul( cma
, ssrat
, cma
);
2075 m3x3_mul( ssrb
, rbb
->iIw
, cmb
);
2076 m3x3_mul( cmb
, ssrbt
, cmb
);
2079 m3x3_add( cma
, cmb
, A
);
2080 m3x3_inv( A
, invA
);
2083 m3x3_mulv( ssra
, rba
->w
, b_wa
);
2084 m3x3_mulv( ssrb
, rbb
->w
, b_wb
);
2085 v3_add( b_wa
, b_wb
, b
);
2089 m3x3_mulv( invA
, b
, impulse
);
2091 v3f delta_wa
, delta_wb
;
2093 m3x3_mul( rba
->iIw
, ssrat
, iwa
);
2094 m3x3_mul( rbb
->iIw
, ssrbt
, iwb
);
2095 m3x3_mulv( iwa
, impulse
, delta_wa
);
2096 m3x3_mulv( iwb
, impulse
, delta_wb
);
2097 v3_add( rba
->w
, delta_wa
, rba
->w
);
2098 v3_sub( rbb
->w
, delta_wb
, rbb
->w
);
2102 * Correct position constraint drift errors
2103 * [ 0.0 <= amt <= 1.0 ]: the correction amount
2105 VG_STATIC
void rb_correct_position_constraints( rb_constr_pos
*buf
, int len
,
2108 for( int i
=0; i
<len
; i
++ ){
2109 rb_constr_pos
*constr
= &buf
[i
];
2110 rigidbody
*rba
= constr
->rba
, *rbb
= constr
->rbb
;
2113 m3x3_mulv( rba
->to_world
, constr
->lca
, p0
);
2114 m3x3_mulv( rbb
->to_world
, constr
->lcb
, p1
);
2115 v3_add( rba
->co
, p0
, p0
);
2116 v3_add( rbb
->co
, p1
, p1
);
2117 v3_sub( p1
, p0
, d
);
2119 v3_muladds( rbb
->co
, d
, -1.0f
* amt
, rbb
->co
);
2120 rb_update_transform( rbb
);
2124 VG_STATIC
void rb_correct_swingtwist_constraints( rb_constr_swingtwist
*buf
,
2125 int len
, float amt
)
2127 for( int i
=0; i
<len
; i
++ ){
2128 rb_constr_swingtwist
*st
= &buf
[i
];
2130 if( !st
->tangent_violation
)
2134 m3x3_mulv( st
->rbb
->to_world
, st
->coneva
, va
);
2136 float angle
= v3_dot( va
, st
->tangent_target
);
2138 if( fabsf(angle
) < 0.9999f
){
2140 v3_cross( va
, st
->tangent_target
, axis
);
2143 q_axis_angle( correction
, axis
, acosf(angle
) * amt
);
2144 q_mul( correction
, st
->rbb
->q
, st
->rbb
->q
);
2145 rb_update_transform( st
->rbb
);
2149 for( int i
=0; i
<len
; i
++ ){
2150 rb_constr_swingtwist
*st
= &buf
[i
];
2152 if( !st
->axis_violation
)
2156 m3x3_mulv( st
->rbb
->to_world
, st
->conevxb
, vxb
);
2158 float angle
= v3_dot( vxb
, st
->axis_target
);
2160 if( fabsf(angle
) < 0.9999f
){
2162 v3_cross( vxb
, st
->axis_target
, axis
);
2165 q_axis_angle( correction
, axis
, acosf(angle
) * amt
);
2166 q_mul( correction
, st
->rbb
->q
, st
->rbb
->q
);
2167 rb_update_transform( st
->rbb
);
2172 VG_STATIC
void rb_correct_contact_constraints( rb_ct
*buf
, int len
, float amt
)
2174 for( int i
=0; i
<len
; i
++ ){
2175 rb_ct
*ct
= &buf
[i
];
2176 rigidbody
*rba
= ct
->rba
,
2179 float mass_total
= 1.0f
/ (rba
->inv_mass
+ rbb
->inv_mass
);
2181 v3_muladds( rba
->co
, ct
->n
, -mass_total
* rba
->inv_mass
, rba
->co
);
2182 v3_muladds( rbb
->co
, ct
->n
, mass_total
* rbb
->inv_mass
, rbb
->co
);
2191 VG_STATIC
void rb_effect_simple_bouyency( rigidbody
*ra
, v4f plane
,
2192 float amt
, float drag
)
2195 float depth
= v3_dot( plane
, ra
->co
) - plane
[3],
2196 lambda
= vg_clampf( -depth
, 0.0f
, 1.0f
) * amt
;
2198 v3_muladds( ra
->v
, plane
, lambda
* k_rb_delta
, ra
->v
);
2201 v3_muls( ra
->v
, 1.0f
-(drag
*k_rb_delta
), ra
->v
);
2204 /* apply a spring&dampener force to match ra(worldspace) on rigidbody, to
2207 VG_STATIC
void rb_effect_spring_target_vector( rigidbody
*rba
, v3f ra
, v3f rt
,
2208 float spring
, float dampening
,
2211 float d
= v3_dot( rt
, ra
);
2212 float a
= vg_signf( d
) * acosf( vg_clampf( d
, -1.0f
, 1.0f
) );
2215 v3_cross( rt
, ra
, axis
);
2217 float Fs
= -a
* spring
,
2218 Fd
= -v3_dot( rba
->w
, axis
) * dampening
;
2220 v3_muladds( rba
->w
, axis
, (Fs
+Fd
) * timestep
, rba
->w
);
2223 #endif /* RIGIDBODY_H */