2 * Resources: Box2D - Erin Catto
10 static void rb_tangent_basis( v3f n
, v3f tx
, v3f ty
);
11 static bh_system bh_system_rigidbodies
;
17 * -----------------------------------------------------------------------------
19 * -----------------------------------------------------------------------------
24 k_rb_delta
= (1.0f
/k_rb_rate
),
26 k_damp_linear
= 0.05f
, /* scale velocity 1/(1+x) */
27 k_damp_angular
= 0.1f
, /* scale angular 1/(1+x) */
29 k_joint_bias
= 0.08f
, /* positional joints */
30 k_joint_correction
= 0.01f
,
31 k_penetration_slop
= 0.01f
,
32 k_inertia_scale
= 4.0f
;
35 * -----------------------------------------------------------------------------
36 * structure definitions
37 * -----------------------------------------------------------------------------
40 typedef struct rigidbody rigidbody
;
41 typedef struct contact rb_ct
;
51 k_rb_shape_sphere
= 1,
52 k_rb_shape_capsule
= 2,
79 v3f right
, up
, forward
;
86 /* inertia model and inverse world tensor */
90 m4x3f to_world
, to_local
;
98 float p
, bias
, norm_impulse
, tangent_impulse
[2],
99 normal_mass
, tangent_mass
[2];
103 rb_contact_buffer
[256];
104 static int rb_contact_count
= 0;
107 * -----------------------------------------------------------------------------
109 * -----------------------------------------------------------------------------
112 static float sphere_volume( float radius
)
114 float r3
= radius
*radius
*radius
;
115 return (4.0f
/3.0f
) * VG_PIf
* r3
;
118 static void rb_tangent_basis( v3f n
, v3f tx
, v3f ty
)
120 /* Compute tangent basis (box2d) */
121 if( fabsf( n
[0] ) >= 0.57735027f
)
135 v3_cross( n
, tx
, ty
);
139 * -----------------------------------------------------------------------------
141 * -----------------------------------------------------------------------------
144 static void rb_debug_contact( rb_ct
*ct
)
147 v3_muladds( ct
->co
, ct
->n
, 0.1f
, p1
);
148 vg_line_pt3( ct
->co
, 0.025f
, 0xff0000ff );
149 vg_line( ct
->co
, p1
, 0xffffffff );
152 static void debug_sphere( m4x3f m
, float radius
, u32 colour
)
154 v3f ly
= { 0.0f
, 0.0f
, radius
},
155 lx
= { 0.0f
, radius
, 0.0f
},
156 lz
= { 0.0f
, 0.0f
, radius
};
158 for( int i
=0; i
<16; i
++ )
160 float t
= ((float)(i
+1) * (1.0f
/16.0f
)) * VG_PIf
* 2.0f
,
164 v3f py
= { s
*radius
, 0.0f
, c
*radius
},
165 px
= { s
*radius
, c
*radius
, 0.0f
},
166 pz
= { 0.0f
, s
*radius
, c
*radius
};
168 v3f p0
, p1
, p2
, p3
, p4
, p5
;
169 m4x3_mulv( m
, py
, p0
);
170 m4x3_mulv( m
, ly
, p1
);
171 m4x3_mulv( m
, px
, p2
);
172 m4x3_mulv( m
, lx
, p3
);
173 m4x3_mulv( m
, pz
, p4
);
174 m4x3_mulv( m
, lz
, p5
);
176 vg_line( p0
, p1
, colour
== 0x00? 0xff00ff00: colour
);
177 vg_line( p2
, p3
, colour
== 0x00? 0xff0000ff: colour
);
178 vg_line( p4
, p5
, colour
== 0x00? 0xffff0000: colour
);
186 static void debug_capsule( m4x3f m
, float radius
, float h
, u32 colour
)
188 v3f ly
= { 0.0f
, 0.0f
, radius
},
189 lx
= { 0.0f
, radius
, 0.0f
},
190 lz
= { 0.0f
, 0.0f
, radius
};
192 float s0
= sinf(0.0f
)*radius
,
193 c0
= cosf(0.0f
)*radius
;
195 v3f p0
, p1
, up
, right
, forward
;
196 m3x3_mulv( m
, (v3f
){0.0f
,1.0f
,0.0f
}, up
);
197 m3x3_mulv( m
, (v3f
){1.0f
,0.0f
,0.0f
}, right
);
198 m3x3_mulv( m
, (v3f
){0.0f
,0.0f
,-1.0f
}, forward
);
199 v3_muladds( m
[3], up
, -h
*0.5f
+radius
, p0
);
200 v3_muladds( m
[3], up
, h
*0.5f
-radius
, p1
);
203 v3_muladds( p0
, right
, radius
, a0
);
204 v3_muladds( p1
, right
, radius
, a1
);
205 v3_muladds( p0
, forward
, radius
, b0
);
206 v3_muladds( p1
, forward
, radius
, b1
);
207 vg_line( a0
, a1
, colour
);
208 vg_line( b0
, b1
, colour
);
210 v3_muladds( p0
, right
, -radius
, a0
);
211 v3_muladds( p1
, right
, -radius
, a1
);
212 v3_muladds( p0
, forward
, -radius
, b0
);
213 v3_muladds( p1
, forward
, -radius
, b1
);
214 vg_line( a0
, a1
, colour
);
215 vg_line( b0
, b1
, colour
);
217 for( int i
=0; i
<16; i
++ )
219 float t
= ((float)(i
+1) * (1.0f
/16.0f
)) * VG_PIf
* 2.0f
,
223 v3f e0
= { s0
, 0.0f
, c0
},
224 e1
= { s1
, 0.0f
, c1
},
225 e2
= { s0
, c0
, 0.0f
},
226 e3
= { s1
, c1
, 0.0f
},
227 e4
= { 0.0f
, c0
, s0
},
228 e5
= { 0.0f
, c1
, s1
};
230 m3x3_mulv( m
, e0
, e0
);
231 m3x3_mulv( m
, e1
, e1
);
232 m3x3_mulv( m
, e2
, e2
);
233 m3x3_mulv( m
, e3
, e3
);
234 m3x3_mulv( m
, e4
, e4
);
235 m3x3_mulv( m
, e5
, e5
);
237 v3_add( p0
, e0
, a0
);
238 v3_add( p0
, e1
, a1
);
239 v3_add( p1
, e0
, b0
);
240 v3_add( p1
, e1
, b1
);
242 vg_line( a0
, a1
, colour
);
243 vg_line( b0
, b1
, colour
);
247 v3_add( p0
, e2
, a0
);
248 v3_add( p0
, e3
, a1
);
249 v3_add( p0
, e4
, b0
);
250 v3_add( p0
, e5
, b1
);
254 v3_add( p1
, e2
, a0
);
255 v3_add( p1
, e3
, a1
);
256 v3_add( p1
, e4
, b0
);
257 v3_add( p1
, e5
, b1
);
260 vg_line( a0
, a1
, colour
);
261 vg_line( b0
, b1
, colour
);
268 static void rb_debug( rigidbody
*rb
, u32 colour
)
270 if( rb
->type
== k_rb_shape_box
)
273 vg_line_boxf_transformed( rb
->to_world
, rb
->bbx
, colour
);
275 else if( rb
->type
== k_rb_shape_sphere
)
277 debug_sphere( rb
->to_world
, rb
->inf
.sphere
.radius
, colour
);
279 else if( rb
->type
== k_rb_shape_capsule
)
282 float h
= rb
->inf
.capsule
.height
,
283 r
= rb
->inf
.capsule
.radius
;
285 debug_capsule( rb
->to_world
, r
, h
, colour
);
287 else if( rb
->type
== k_rb_shape_scene
)
289 vg_line_boxf( rb
->bbx
, colour
);
294 * -----------------------------------------------------------------------------
296 * -----------------------------------------------------------------------------
300 * Update world space bounding box based on local one
302 static void rb_update_bounds( rigidbody
*rb
)
304 box_copy( rb
->bbx
, rb
->bbx_world
);
305 m4x3_transform_aabb( rb
->to_world
, rb
->bbx_world
);
309 * Commit transform to rigidbody. Updates matrices
311 static void rb_update_transform( rigidbody
*rb
)
313 q_normalize( rb
->q
);
314 q_m3x3( rb
->q
, rb
->to_world
);
315 v3_copy( rb
->co
, rb
->to_world
[3] );
317 m4x3_invert_affine( rb
->to_world
, rb
->to_local
);
319 m3x3_mulv( rb
->to_world
, (v3f
){1.0f
,0.0f
, 0.0f
}, rb
->right
);
320 m3x3_mulv( rb
->to_world
, (v3f
){0.0f
,1.0f
, 0.0f
}, rb
->up
);
321 m3x3_mulv( rb
->to_world
, (v3f
){0.0f
,0.0f
,-1.0f
}, rb
->forward
);
323 m3x3_mul( rb
->iI
, rb
->to_local
, rb
->iIw
);
324 m3x3_mul( rb
->to_world
, rb
->iIw
, rb
->iIw
);
326 rb_update_bounds( rb
);
330 * Initialize rigidbody and calculate masses, inertia
332 static void rb_init( rigidbody
*rb
)
336 if( rb
->type
== k_rb_shape_box
)
339 v3_sub( rb
->bbx
[1], rb
->bbx
[0], dims
);
340 volume
= dims
[0]*dims
[1]*dims
[2];
342 else if( rb
->type
== k_rb_shape_sphere
)
344 volume
= sphere_volume( rb
->inf
.sphere
.radius
);
345 v3_fill( rb
->bbx
[0], -rb
->inf
.sphere
.radius
);
346 v3_fill( rb
->bbx
[1], rb
->inf
.sphere
.radius
);
348 else if( rb
->type
== k_rb_shape_capsule
)
350 float r
= rb
->inf
.capsule
.radius
,
351 h
= rb
->inf
.capsule
.height
;
352 volume
= sphere_volume( r
) + VG_PIf
* r
*r
* (h
- r
*2.0f
);
354 v3_fill( rb
->bbx
[0], -rb
->inf
.sphere
.radius
);
355 v3_fill( rb
->bbx
[1], rb
->inf
.sphere
.radius
);
359 else if( rb
->type
== k_rb_shape_scene
)
362 box_copy( rb
->inf
.scene
.pscene
->bbx
, rb
->bbx
);
373 float mass
= 2.0f
*volume
;
374 rb
->inv_mass
= 1.0f
/mass
;
377 v3_sub( rb
->bbx
[1], rb
->bbx
[0], extent
);
378 v3_muls( extent
, 0.5f
, extent
);
380 /* local intertia tensor */
381 float scale
= k_inertia_scale
;
382 float ex2
= scale
*extent
[0]*extent
[0],
383 ey2
= scale
*extent
[1]*extent
[1],
384 ez2
= scale
*extent
[2]*extent
[2];
386 rb
->I
[0] = ((1.0f
/12.0f
) * mass
* (ey2
+ez2
));
387 rb
->I
[1] = ((1.0f
/12.0f
) * mass
* (ex2
+ez2
));
388 rb
->I
[2] = ((1.0f
/12.0f
) * mass
* (ex2
+ey2
));
390 m3x3_identity( rb
->iI
);
391 rb
->iI
[0][0] = rb
->I
[0];
392 rb
->iI
[1][1] = rb
->I
[1];
393 rb
->iI
[2][2] = rb
->I
[2];
394 m3x3_inv( rb
->iI
, rb
->iI
);
400 rb_update_transform( rb
);
403 static void rb_iter( rigidbody
*rb
)
405 v3f gravity
= { 0.0f
, -9.8f
, 0.0f
};
406 v3_muladds( rb
->v
, gravity
, k_rb_delta
, rb
->v
);
408 /* intergrate velocity */
409 v3_muladds( rb
->co
, rb
->v
, k_rb_delta
, rb
->co
);
410 v3_lerp( rb
->w
, (v3f
){0.0f
,0.0f
,0.0f
}, 0.0025f
, rb
->w
);
412 /* inegrate inertia */
413 if( v3_length2( rb
->w
) > 0.0f
)
417 v3_copy( rb
->w
, axis
);
419 float mag
= v3_length( axis
);
420 v3_divs( axis
, mag
, axis
);
421 q_axis_angle( rotation
, axis
, mag
*k_rb_delta
);
422 q_mul( rotation
, rb
->q
, rb
->q
);
426 v3_muls( rb
->v
, 1.0f
/(1.0f
+k_rb_delta
*k_damp_linear
), rb
->v
);
427 v3_muls( rb
->w
, 1.0f
/(1.0f
+k_rb_delta
*k_damp_angular
), rb
->w
);
431 * -----------------------------------------------------------------------------
432 * Closest point functions
433 * -----------------------------------------------------------------------------
437 * These closest point tests were learned from Real-Time Collision Detection by
440 static float closest_segment_segment( v3f p1
, v3f q1
, v3f p2
, v3f q2
,
441 float *s
, float *t
, v3f c1
, v3f c2
)
444 v3_sub( q1
, p1
, d1
);
445 v3_sub( q2
, p2
, d2
);
448 float a
= v3_length2( d1
),
449 e
= v3_length2( d2
),
452 const float kEpsilon
= 0.0001f
;
454 if( a
<= kEpsilon
&& e
<= kEpsilon
)
462 v3_sub( c1
, c2
, v0
);
464 return v3_length2( v0
);
470 *t
= vg_clampf( f
/ e
, 0.0f
, 1.0f
);
474 float c
= v3_dot( d1
, r
);
478 *s
= vg_clampf( -c
/ a
, 0.0f
, 1.0f
);
482 float b
= v3_dot(d1
,d2
),
487 *s
= vg_clampf((b
*f
- c
*e
)/d
, 0.0f
, 1.0f
);
499 *s
= vg_clampf( -c
/ a
, 0.0f
, 1.0f
);
504 *s
= vg_clampf((b
-c
)/a
,0.0f
,1.0f
);
509 v3_muladds( p1
, d1
, *s
, c1
);
510 v3_muladds( p2
, d2
, *t
, c2
);
513 v3_sub( c1
, c2
, v0
);
514 return v3_length2( v0
);
517 static void closest_point_aabb( v3f p
, boxf box
, v3f dest
)
519 v3_maxv( p
, box
[0], dest
);
520 v3_minv( dest
, box
[1], dest
);
523 static void closest_point_obb( v3f p
, rigidbody
*rb
, v3f dest
)
526 m4x3_mulv( rb
->to_local
, p
, local
);
527 closest_point_aabb( local
, rb
->bbx
, local
);
528 m4x3_mulv( rb
->to_world
, local
, dest
);
531 static float closest_point_segment( v3f a
, v3f b
, v3f point
, v3f dest
)
535 v3_sub( point
, a
, v1
);
537 float t
= v3_dot( v1
, v0
) / v3_length2(v0
);
538 t
= vg_clampf(t
,0.0f
,1.0f
);
539 v3_muladds( a
, v0
, t
, dest
);
543 static void closest_on_triangle( v3f p
, v3f tri
[3], v3f dest
)
548 /* Region outside A */
549 v3_sub( tri
[1], tri
[0], ab
);
550 v3_sub( tri
[2], tri
[0], ac
);
551 v3_sub( p
, tri
[0], ap
);
555 if( d1
<= 0.0f
&& d2
<= 0.0f
)
557 v3_copy( tri
[0], dest
);
558 v3_copy( (v3f
){INFINITY
,INFINITY
,INFINITY
}, dest
);
562 /* Region outside B */
566 v3_sub( p
, tri
[1], bp
);
567 d3
= v3_dot( ab
, bp
);
568 d4
= v3_dot( ac
, bp
);
570 if( d3
>= 0.0f
&& d4
<= d3
)
572 v3_copy( tri
[1], dest
);
573 v3_copy( (v3f
){INFINITY
,INFINITY
,INFINITY
}, dest
);
577 /* Edge region of AB */
578 float vc
= d1
*d4
- d3
*d2
;
579 if( vc
<= 0.0f
&& d1
>= 0.0f
&& d3
<= 0.0f
)
581 float v
= d1
/ (d1
-d3
);
582 v3_muladds( tri
[0], ab
, v
, dest
);
583 v3_copy( (v3f
){INFINITY
,INFINITY
,INFINITY
}, dest
);
587 /* Region outside C */
590 v3_sub( p
, tri
[2], cp
);
594 if( d6
>= 0.0f
&& d5
<= d6
)
596 v3_copy( tri
[2], dest
);
597 v3_copy( (v3f
){INFINITY
,INFINITY
,INFINITY
}, dest
);
602 float vb
= d5
*d2
- d1
*d6
;
603 if( vb
<= 0.0f
&& d2
>= 0.0f
&& d6
<= 0.0f
)
605 float w
= d2
/ (d2
-d6
);
606 v3_muladds( tri
[0], ac
, w
, dest
);
607 v3_copy( (v3f
){INFINITY
,INFINITY
,INFINITY
}, dest
);
612 float va
= d3
*d6
- d5
*d4
;
613 if( va
<= 0.0f
&& (d4
-d3
) >= 0.0f
&& (d5
-d6
) >= 0.0f
)
615 float w
= (d4
-d3
) / ((d4
-d3
) + (d5
-d6
));
617 v3_sub( tri
[2], tri
[1], bc
);
618 v3_muladds( tri
[1], bc
, w
, dest
);
619 v3_copy( (v3f
){INFINITY
,INFINITY
,INFINITY
}, dest
);
623 /* P inside region, Q via barycentric coordinates uvw */
624 float d
= 1.0f
/(va
+vb
+vc
),
628 v3_muladds( tri
[0], ab
, v
, dest
);
629 v3_muladds( dest
, ac
, w
, dest
);
632 static void closest_on_triangle_1( v3f p
, v3f tri
[3], v3f dest
)
637 /* Region outside A */
638 v3_sub( tri
[1], tri
[0], ab
);
639 v3_sub( tri
[2], tri
[0], ac
);
640 v3_sub( p
, tri
[0], ap
);
644 if( d1
<= 0.0f
&& d2
<= 0.0f
)
646 v3_copy( tri
[0], dest
);
650 /* Region outside B */
654 v3_sub( p
, tri
[1], bp
);
655 d3
= v3_dot( ab
, bp
);
656 d4
= v3_dot( ac
, bp
);
658 if( d3
>= 0.0f
&& d4
<= d3
)
660 v3_copy( tri
[1], dest
);
664 /* Edge region of AB */
665 float vc
= d1
*d4
- d3
*d2
;
666 if( vc
<= 0.0f
&& d1
>= 0.0f
&& d3
<= 0.0f
)
668 float v
= d1
/ (d1
-d3
);
669 v3_muladds( tri
[0], ab
, v
, dest
);
673 /* Region outside C */
676 v3_sub( p
, tri
[2], cp
);
680 if( d6
>= 0.0f
&& d5
<= d6
)
682 v3_copy( tri
[2], dest
);
687 float vb
= d5
*d2
- d1
*d6
;
688 if( vb
<= 0.0f
&& d2
>= 0.0f
&& d6
<= 0.0f
)
690 float w
= d2
/ (d2
-d6
);
691 v3_muladds( tri
[0], ac
, w
, dest
);
696 float va
= d3
*d6
- d5
*d4
;
697 if( va
<= 0.0f
&& (d4
-d3
) >= 0.0f
&& (d5
-d6
) >= 0.0f
)
699 float w
= (d4
-d3
) / ((d4
-d3
) + (d5
-d6
));
701 v3_sub( tri
[2], tri
[1], bc
);
702 v3_muladds( tri
[1], bc
, w
, dest
);
706 /* P inside region, Q via barycentric coordinates uvw */
707 float d
= 1.0f
/(va
+vb
+vc
),
711 v3_muladds( tri
[0], ab
, v
, dest
);
712 v3_muladds( dest
, ac
, w
, dest
);
716 * -----------------------------------------------------------------------------
717 * Boolean shape overlap functions
718 * -----------------------------------------------------------------------------
722 * Project AABB, and triangle interval onto axis to check if they overlap
724 static int rb_box_triangle_interval( v3f extent
, v3f axis
, v3f tri
[3] )
728 r
= extent
[0] * fabsf(axis
[0]) +
729 extent
[1] * fabsf(axis
[1]) +
730 extent
[2] * fabsf(axis
[2]),
732 p0
= v3_dot( axis
, tri
[0] ),
733 p1
= v3_dot( axis
, tri
[1] ),
734 p2
= v3_dot( axis
, tri
[2] ),
736 e
= vg_maxf(-vg_maxf(p0
,vg_maxf(p1
,p2
)), vg_minf(p0
,vg_minf(p1
,p2
)));
738 if( e
> r
) return 0;
743 * Seperating axis test box vs triangle
745 static int rb_box_triangle_sat( rigidbody
*rba
, v3f tri_src
[3] )
750 v3_sub( rba
->bbx
[1], rba
->bbx
[0], extent
);
751 v3_muls( extent
, 0.5f
, extent
);
752 v3_add( rba
->bbx
[0], extent
, c
);
754 for( int i
=0; i
<3; i
++ )
756 m4x3_mulv( rba
->to_local
, tri_src
[i
], tri
[i
] );
757 v3_sub( tri
[i
], c
, tri
[i
] );
761 if(!rb_box_triangle_interval( extent
, (v3f
){1.0f
,0.0f
,0.0f
}, tri
)) return 0;
762 if(!rb_box_triangle_interval( extent
, (v3f
){0.0f
,1.0f
,0.0f
}, tri
)) return 0;
763 if(!rb_box_triangle_interval( extent
, (v3f
){0.0f
,0.0f
,1.0f
}, tri
)) return 0;
765 v3f v0
,v1
,v2
,n
, e0
,e1
,e2
;
766 v3_sub( tri
[1], tri
[0], v0
);
767 v3_sub( tri
[2], tri
[0], v1
);
768 v3_sub( tri
[2], tri
[1], v2
);
772 v3_cross( v0
, v1
, n
);
773 v3_cross( v0
, n
, e0
);
774 v3_cross( n
, v1
, e1
);
775 v3_cross( v2
, n
, e2
);
778 if(!rb_box_triangle_interval( extent
, n
, tri
)) return 0;
781 v3_cross( e0
, (v3f
){1.0f
,0.0f
,0.0f
}, axis
[0] );
782 v3_cross( e0
, (v3f
){0.0f
,1.0f
,0.0f
}, axis
[1] );
783 v3_cross( e0
, (v3f
){0.0f
,0.0f
,1.0f
}, axis
[2] );
784 v3_cross( e1
, (v3f
){1.0f
,0.0f
,0.0f
}, axis
[3] );
785 v3_cross( e1
, (v3f
){0.0f
,1.0f
,0.0f
}, axis
[4] );
786 v3_cross( e1
, (v3f
){0.0f
,0.0f
,1.0f
}, axis
[5] );
787 v3_cross( e2
, (v3f
){1.0f
,0.0f
,0.0f
}, axis
[6] );
788 v3_cross( e2
, (v3f
){0.0f
,1.0f
,0.0f
}, axis
[7] );
789 v3_cross( e2
, (v3f
){0.0f
,0.0f
,1.0f
}, axis
[8] );
791 for( int i
=0; i
<9; i
++ )
792 if(!rb_box_triangle_interval( extent
, axis
[i
], tri
)) return 0;
798 * -----------------------------------------------------------------------------
800 * -----------------------------------------------------------------------------
806 * These do not automatically allocate contacts, an appropriately sized
807 * buffer must be supplied. The function returns the size of the manifold
808 * which was generated.
810 * The values set on the contacts are: n, co, p, rba, rbb
814 * By collecting the minimum(time) and maximum(time) pairs of points, we
815 * build a reduced and stable exact manifold.
818 * rx: minimum distance of these points
819 * dx: the delta between the two points
821 * pairs will only ammend these if they are creating a collision
823 typedef struct capsule_manifold capsule_manifold
;
824 struct capsule_manifold
832 * Expand a line manifold with a new pair. t value is the time along segment
833 * on the oriented object which created this pair.
835 static void rb_capsule_manifold( v3f pa
, v3f pb
, float t
, float r
,
836 capsule_manifold
*manifold
)
839 v3_sub( pa
, pb
, delta
);
841 if( v3_length2(delta
) < r
*r
)
843 if( t
< manifold
->t0
)
845 v3_copy( delta
, manifold
->d0
);
850 if( t
> manifold
->t1
)
852 v3_copy( delta
, manifold
->d1
);
859 static void rb_capsule_manifold_init( capsule_manifold
*manifold
)
861 manifold
->t0
= INFINITY
;
862 manifold
->t1
= -INFINITY
;
865 static int rb_capsule_manifold_done( rigidbody
*rba
, rigidbody
*rbb
,
866 capsule_manifold
*manifold
, rb_ct
*buf
)
868 float h
= rba
->inf
.capsule
.height
,
869 ra
= rba
->inf
.capsule
.radius
;
872 v3_muladds( rba
->co
, rba
->up
, -h
*0.5f
+ra
, p0
);
873 v3_muladds( rba
->co
, rba
->up
, h
*0.5f
-ra
, p1
);
876 if( manifold
->t0
<= 1.0f
)
881 v3_muls( p0
, 1.0f
-manifold
->t0
, pa
);
882 v3_muladds( pa
, p1
, manifold
->t0
, pa
);
884 float d
= v3_length( manifold
->d0
);
885 v3_muls( manifold
->d0
, 1.0f
/d
, ct
->n
);
886 v3_muladds( pa
, ct
->n
, -ra
, ct
->co
);
888 ct
->p
= manifold
->r0
- d
;
895 if( (manifold
->t1
>= 0.0f
) && (manifold
->t0
!= manifold
->t1
) )
897 rb_ct
*ct
= buf
+count
;
900 v3_muls( p0
, 1.0f
-manifold
->t1
, pa
);
901 v3_muladds( pa
, p1
, manifold
->t1
, pa
);
903 float d
= v3_length( manifold
->d1
);
904 v3_muls( manifold
->d1
, 1.0f
/d
, ct
->n
);
905 v3_muladds( pa
, ct
->n
, -ra
, ct
->co
);
907 ct
->p
= manifold
->r1
- d
;
919 vg_line( buf
[0].co
, buf
[1].co
, 0xff0000ff );
924 static int rb_capsule_sphere( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
926 float h
= rba
->inf
.capsule
.height
,
927 ra
= rba
->inf
.capsule
.radius
,
928 rb
= rbb
->inf
.sphere
.radius
;
931 v3_muladds( rba
->co
, rba
->up
, -h
*0.5f
+ra
, p0
);
932 v3_muladds( rba
->co
, rba
->up
, h
*0.5f
-ra
, p1
);
935 closest_point_segment( p0
, p1
, rbb
->co
, c
);
936 v3_sub( c
, rbb
->co
, delta
);
938 float d2
= v3_length2(delta
),
946 v3_muls( delta
, 1.0f
/d
, ct
->n
);
950 v3_muladds( c
, ct
->n
, -ra
, p0
);
951 v3_muladds( rbb
->co
, ct
->n
, rb
, p1
);
952 v3_add( p0
, p1
, ct
->co
);
953 v3_muls( ct
->co
, 0.5f
, ct
->co
);
964 static int rb_capsule_capsule( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
966 float ha
= rba
->inf
.capsule
.height
,
967 hb
= rbb
->inf
.capsule
.height
,
968 ra
= rba
->inf
.capsule
.radius
,
969 rb
= rbb
->inf
.capsule
.radius
,
973 v3_muladds( rba
->co
, rba
->up
, -ha
*0.5f
+ra
, p0
);
974 v3_muladds( rba
->co
, rba
->up
, ha
*0.5f
-ra
, p1
);
975 v3_muladds( rbb
->co
, rbb
->up
, -hb
*0.5f
+rb
, p2
);
976 v3_muladds( rbb
->co
, rbb
->up
, hb
*0.5f
-rb
, p3
);
978 capsule_manifold manifold
;
979 rb_capsule_manifold_init( &manifold
);
983 closest_segment_segment( p0
, p1
, p2
, p3
, &ta
, &tb
, pa
, pb
);
984 rb_capsule_manifold( pa
, pb
, ta
, r
, &manifold
);
986 ta
= closest_point_segment( p0
, p1
, p2
, pa
);
987 tb
= closest_point_segment( p0
, p1
, p3
, pb
);
988 rb_capsule_manifold( pa
, p2
, ta
, r
, &manifold
);
989 rb_capsule_manifold( pb
, p3
, tb
, r
, &manifold
);
991 closest_point_segment( p2
, p3
, p0
, pa
);
992 closest_point_segment( p2
, p3
, p1
, pb
);
993 rb_capsule_manifold( p0
, pa
, 0.0f
, r
, &manifold
);
994 rb_capsule_manifold( p1
, pb
, 1.0f
, r
, &manifold
);
996 return rb_capsule_manifold_done( rba
, rbb
, &manifold
, buf
);
1000 * Generates up to two contacts; optimised for the most stable manifold
1002 static int rb_capsule_box( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1004 float h
= rba
->inf
.capsule
.height
,
1005 r
= rba
->inf
.capsule
.radius
;
1008 * Solving this in symetric local space of the cube saves us some time and a
1009 * couple branches when it comes to the quad stage.
1012 v3_add( rbb
->bbx
[0], rbb
->bbx
[1], centroid
);
1013 v3_muls( centroid
, 0.5f
, centroid
);
1016 v3_sub( rbb
->bbx
[0], centroid
, bbx
[0] );
1017 v3_sub( rbb
->bbx
[1], centroid
, bbx
[1] );
1019 v3f pc
, p0w
, p1w
, p0
, p1
;
1020 v3_muladds( rba
->co
, rba
->up
, -h
*0.5f
+r
, p0w
);
1021 v3_muladds( rba
->co
, rba
->up
, h
*0.5f
-r
, p1w
);
1023 m4x3_mulv( rbb
->to_local
, p0w
, p0
);
1024 m4x3_mulv( rbb
->to_local
, p1w
, p1
);
1025 v3_sub( p0
, centroid
, p0
);
1026 v3_sub( p1
, centroid
, p1
);
1027 v3_add( p0
, p1
, pc
);
1028 v3_muls( pc
, 0.5f
, pc
);
1031 * Finding an appropriate quad to collide lines with
1034 v3_div( pc
, bbx
[1], region
);
1037 if( (fabsf(region
[0]) > fabsf(region
[1])) &&
1038 (fabsf(region
[0]) > fabsf(region
[2])) )
1040 float px
= vg_signf(region
[0]) * bbx
[1][0];
1041 v3_copy( (v3f
){ px
, bbx
[0][1], bbx
[0][2] }, quad
[0] );
1042 v3_copy( (v3f
){ px
, bbx
[1][1], bbx
[0][2] }, quad
[1] );
1043 v3_copy( (v3f
){ px
, bbx
[1][1], bbx
[1][2] }, quad
[2] );
1044 v3_copy( (v3f
){ px
, bbx
[0][1], bbx
[1][2] }, quad
[3] );
1046 else if( fabsf(region
[1]) > fabsf(region
[2]) )
1048 float py
= vg_signf(region
[1]) * bbx
[1][1];
1049 v3_copy( (v3f
){ bbx
[0][0], py
, bbx
[0][2] }, quad
[0] );
1050 v3_copy( (v3f
){ bbx
[1][0], py
, bbx
[0][2] }, quad
[1] );
1051 v3_copy( (v3f
){ bbx
[1][0], py
, bbx
[1][2] }, quad
[2] );
1052 v3_copy( (v3f
){ bbx
[0][0], py
, bbx
[1][2] }, quad
[3] );
1056 float pz
= vg_signf(region
[2]) * bbx
[1][2];
1057 v3_copy( (v3f
){ bbx
[0][0], bbx
[0][1], pz
}, quad
[0] );
1058 v3_copy( (v3f
){ bbx
[1][0], bbx
[0][1], pz
}, quad
[1] );
1059 v3_copy( (v3f
){ bbx
[1][0], bbx
[1][1], pz
}, quad
[2] );
1060 v3_copy( (v3f
){ bbx
[0][0], bbx
[1][1], pz
}, quad
[3] );
1063 capsule_manifold manifold
;
1064 rb_capsule_manifold_init( &manifold
);
1067 closest_point_aabb( p0
, bbx
, c0
);
1068 closest_point_aabb( p1
, bbx
, c1
);
1071 v3_sub( c0
, p0
, d0
);
1072 v3_sub( c1
, p1
, d1
);
1073 v3_sub( p1
, p0
, da
);
1079 if( v3_dot( da
, d0
) <= 0.01f
)
1080 rb_capsule_manifold( p0
, c0
, 0.0f
, r
, &manifold
);
1082 if( v3_dot( da
, d1
) >= -0.01f
)
1083 rb_capsule_manifold( p1
, c1
, 1.0f
, r
, &manifold
);
1085 for( int i
=0; i
<4; i
++ )
1092 closest_segment_segment( p0
, p1
, quad
[i0
], quad
[i1
], &ta
, &tb
, ca
, cb
);
1093 rb_capsule_manifold( ca
, cb
, ta
, r
, &manifold
);
1097 * Create final contacts based on line manifold
1099 m3x3_mulv( rbb
->to_world
, manifold
.d0
, manifold
.d0
);
1100 m3x3_mulv( rbb
->to_world
, manifold
.d1
, manifold
.d1
);
1107 for( int i
=0; i
<4; i
++ )
1113 v3_add( quad
[i0
], centroid
, q0
);
1114 v3_add( quad
[i1
], centroid
, q1
);
1116 m4x3_mulv( rbb
->to_world
, q0
, q0
);
1117 m4x3_mulv( rbb
->to_world
, q1
, q1
);
1119 vg_line( q0
, q1
, 0xffffffff );
1123 return rb_capsule_manifold_done( rba
, rbb
, &manifold
, buf
);
1126 static int rb_sphere_box( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1130 closest_point_obb( rba
->co
, rbb
, co
);
1131 v3_sub( rba
->co
, co
, delta
);
1133 float d2
= v3_length2(delta
),
1134 r
= rba
->inf
.sphere
.radius
;
1143 v3_sub( rba
->co
, rbb
->co
, delta
);
1146 * some extra testing is required to find the best axis to push the
1147 * object back outside the box. Since there isnt a clear seperating
1148 * vector already, especially on really high aspect boxes.
1150 float lx
= v3_dot( rbb
->right
, delta
),
1151 ly
= v3_dot( rbb
->up
, delta
),
1152 lz
= v3_dot( rbb
->forward
, delta
),
1153 px
= rbb
->bbx
[1][0] - fabsf(lx
),
1154 py
= rbb
->bbx
[1][1] - fabsf(ly
),
1155 pz
= rbb
->bbx
[1][2] - fabsf(lz
);
1157 if( px
< py
&& px
< pz
)
1158 v3_muls( rbb
->right
, vg_signf(lx
), ct
->n
);
1160 v3_muls( rbb
->up
, vg_signf(ly
), ct
->n
);
1162 v3_muls( rbb
->forward
, vg_signf(lz
), ct
->n
);
1164 v3_muladds( rba
->co
, ct
->n
, -r
, ct
->co
);
1170 v3_muls( delta
, 1.0f
/d
, ct
->n
);
1172 v3_copy( co
, ct
->co
);
1183 static int rb_sphere_sphere( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1186 v3_sub( rba
->co
, rbb
->co
, delta
);
1188 float d2
= v3_length2(delta
),
1189 r
= rba
->inf
.sphere
.radius
+ rbb
->inf
.sphere
.radius
;
1193 float d
= sqrtf(d2
);
1196 v3_muls( delta
, 1.0f
/d
, ct
->n
);
1199 v3_muladds( rba
->co
, ct
->n
,-rba
->inf
.sphere
.radius
, p0
);
1200 v3_muladds( rbb
->co
, ct
->n
, rbb
->inf
.sphere
.radius
, p1
);
1201 v3_add( p0
, p1
, ct
->co
);
1202 v3_muls( ct
->co
, 0.5f
, ct
->co
);
1212 static int rb_sphere_triangle( rigidbody
*rba
, rigidbody
*rbb
,
1213 v3f tri
[3], rb_ct
*buf
)
1217 closest_on_triangle( rba
->co
, tri
, co
);
1218 v3_sub( rba
->co
, co
, delta
);
1220 vg_line( rba
->co
, co
, 0xffff0000 );
1221 vg_line_pt3( rba
->co
, 0.1f
, 0xff00ffff );
1223 float d2
= v3_length2( delta
),
1224 r
= rba
->inf
.sphere
.radius
;
1231 v3_sub( tri
[2], tri
[0], ab
);
1232 v3_sub( tri
[1], tri
[0], ac
);
1233 v3_cross( ac
, ab
, tn
);
1234 v3_copy( tn
, ct
->n
);
1235 v3_normalize( ct
->n
);
1237 float d
= sqrtf(d2
);
1239 v3_copy( co
, ct
->co
);
1249 static int rb_sphere_scene( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1251 scene
*sc
= rbb
->inf
.scene
.pscene
;
1255 int len
= bh_select( &sc
->bhtris
, rba
->bbx_world
, geo
, 128 );
1259 for( int i
=0; i
<len
; i
++ )
1261 u32
*ptri
= &sc
->indices
[ geo
[i
]*3 ];
1263 for( int j
=0; j
<3; j
++ )
1264 v3_copy( sc
->verts
[ptri
[j
]].co
, tri
[j
] );
1266 vg_line(tri
[0],tri
[1],0xff00ff00 );
1267 vg_line(tri
[1],tri
[2],0xff00ff00 );
1268 vg_line(tri
[2],tri
[0],0xff00ff00 );
1270 buf
[count
].element_id
= ptri
[0];
1271 count
+= rb_sphere_triangle( rba
, rbb
, tri
, buf
+count
);
1275 vg_warn( "Exceeding sphere_vs_scene capacity. Geometry too dense!\n" );
1283 static int rb_box_scene( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1285 scene
*sc
= rbb
->inf
.scene
.pscene
;
1289 int len
= bh_select( &sc
->bhtris
, rba
->bbx_world
, geo
, 128 );
1293 for( int i
=0; i
<len
; i
++ )
1295 u32
*ptri
= &sc
->indices
[ geo
[i
]*3 ];
1297 for( int j
=0; j
<3; j
++ )
1298 v3_copy( sc
->verts
[ptri
[j
]].co
, tri
[j
] );
1300 if( rb_box_triangle_sat( rba
, tri
) )
1302 vg_line(tri
[0],tri
[1],0xff50ff00 );
1303 vg_line(tri
[1],tri
[2],0xff50ff00 );
1304 vg_line(tri
[2],tri
[0],0xff50ff00 );
1308 vg_line(tri
[0],tri
[1],0xff0000ff );
1309 vg_line(tri
[1],tri
[2],0xff0000ff );
1310 vg_line(tri
[2],tri
[0],0xff0000ff );
1316 v3_sub( tri
[1], tri
[0], v0
);
1317 v3_sub( tri
[2], tri
[0], v1
);
1318 v3_cross( v0
, v1
, n
);
1321 /* find best feature */
1322 float best
= v3_dot( rba
->right
, n
);
1325 float cy
= v3_dot( rba
->up
, n
);
1326 if( fabsf(cy
) > fabsf(best
) )
1332 float cz
= -v3_dot( rba
->forward
, n
);
1333 if( fabsf(cz
) > fabsf(best
) )
1343 float px
= best
> 0.0f
? rba
->bbx
[0][0]: rba
->bbx
[1][0];
1344 manifold
[0][0] = px
;
1345 manifold
[0][1] = rba
->bbx
[0][1];
1346 manifold
[0][2] = rba
->bbx
[0][2];
1347 manifold
[1][0] = px
;
1348 manifold
[1][1] = rba
->bbx
[1][1];
1349 manifold
[1][2] = rba
->bbx
[0][2];
1350 manifold
[2][0] = px
;
1351 manifold
[2][1] = rba
->bbx
[1][1];
1352 manifold
[2][2] = rba
->bbx
[1][2];
1353 manifold
[3][0] = px
;
1354 manifold
[3][1] = rba
->bbx
[0][1];
1355 manifold
[3][2] = rba
->bbx
[1][2];
1357 else if( axis
== 1 )
1359 float py
= best
> 0.0f
? rba
->bbx
[0][1]: rba
->bbx
[1][1];
1360 manifold
[0][0] = rba
->bbx
[0][0];
1361 manifold
[0][1] = py
;
1362 manifold
[0][2] = rba
->bbx
[0][2];
1363 manifold
[1][0] = rba
->bbx
[1][0];
1364 manifold
[1][1] = py
;
1365 manifold
[1][2] = rba
->bbx
[0][2];
1366 manifold
[2][0] = rba
->bbx
[1][0];
1367 manifold
[2][1] = py
;
1368 manifold
[2][2] = rba
->bbx
[1][2];
1369 manifold
[3][0] = rba
->bbx
[0][0];
1370 manifold
[3][1] = py
;
1371 manifold
[3][2] = rba
->bbx
[1][2];
1375 float pz
= best
> 0.0f
? rba
->bbx
[0][2]: rba
->bbx
[1][2];
1376 manifold
[0][0] = rba
->bbx
[0][0];
1377 manifold
[0][1] = rba
->bbx
[0][1];
1378 manifold
[0][2] = pz
;
1379 manifold
[1][0] = rba
->bbx
[1][0];
1380 manifold
[1][1] = rba
->bbx
[0][1];
1381 manifold
[1][2] = pz
;
1382 manifold
[2][0] = rba
->bbx
[1][0];
1383 manifold
[2][1] = rba
->bbx
[1][1];
1384 manifold
[2][2] = pz
;
1385 manifold
[3][0] = rba
->bbx
[0][0];
1386 manifold
[3][1] = rba
->bbx
[1][1];
1387 manifold
[3][2] = pz
;
1390 for( int j
=0; j
<4; j
++ )
1391 m4x3_mulv( rba
->to_world
, manifold
[j
], manifold
[j
] );
1393 vg_line( manifold
[0], manifold
[1], 0xffffffff );
1394 vg_line( manifold
[1], manifold
[2], 0xffffffff );
1395 vg_line( manifold
[2], manifold
[3], 0xffffffff );
1396 vg_line( manifold
[3], manifold
[0], 0xffffffff );
1398 for( int j
=0; j
<4; j
++ )
1400 rb_ct
*ct
= buf
+count
;
1402 v3_copy( manifold
[j
], ct
->co
);
1403 v3_copy( n
, ct
->n
);
1405 float l0
= v3_dot( tri
[0], n
),
1406 l1
= v3_dot( manifold
[j
], n
);
1408 ct
->p
= (l0
-l1
)*0.5f
;
1423 static int RB_MATRIX_ERROR( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1425 vg_error( "Collision type is unimplemented between types %d and %d\n",
1426 rba
->type
, rbb
->type
);
1431 static int rb_sphere_capsule( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1433 return rb_capsule_sphere( rbb
, rba
, buf
);
1436 static int rb_box_capsule( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1438 return rb_capsule_box( rbb
, rba
, buf
);
1441 static int rb_box_sphere( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1443 return rb_sphere_box( rbb
, rba
, buf
);
1446 static int rb_scene_box( rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1448 return rb_box_scene( rbb
, rba
, buf
);
1451 static int (*rb_jump_table
[4][4])( rigidbody
*a
, rigidbody
*b
, rb_ct
*buf
) =
1453 /* box */ /* Sphere */ /* Capsule */ /* Mesh */
1454 { RB_MATRIX_ERROR
, rb_box_sphere
, rb_box_capsule
, rb_box_scene
},
1455 { rb_sphere_box
, rb_sphere_sphere
, rb_sphere_capsule
, rb_sphere_scene
},
1456 { rb_capsule_box
, rb_capsule_sphere
, rb_capsule_capsule
, RB_MATRIX_ERROR
},
1457 { rb_scene_box
, RB_MATRIX_ERROR
, RB_MATRIX_ERROR
, RB_MATRIX_ERROR
}
1460 static int rb_collide( rigidbody
*rba
, rigidbody
*rbb
)
1462 int (*collider_jump
)(rigidbody
*rba
, rigidbody
*rbb
, rb_ct
*buf
)
1463 = rb_jump_table
[rba
->type
][rbb
->type
];
1466 * 12 is the maximum manifold size we can generate, so we are forced to abort
1467 * potentially checking any more.
1469 if( rb_contact_count
+ 12 > vg_list_size(rb_contact_buffer
) )
1471 vg_warn( "Too many contacts made in global collider buffer (%d of %d\n)",
1472 rb_contact_count
, vg_list_size(rb_contact_buffer
) );
1477 * FUTURE: Replace this with a more dedicated broad phase pass
1479 if( box_overlap( rba
->bbx_world
, rbb
->bbx_world
) )
1481 int count
= collider_jump( rba
, rbb
, rb_contact_buffer
+rb_contact_count
);
1482 rb_contact_count
+= count
;
1490 * -----------------------------------------------------------------------------
1492 * -----------------------------------------------------------------------------
1495 static void rb_solver_reset(void)
1497 rb_contact_count
= 0;
1500 static rb_ct
*rb_global_ct(void)
1502 return rb_contact_buffer
+ rb_contact_count
;
1506 * Initializing things like tangent vectors
1508 static void rb_presolve_contacts( rb_ct
*buffer
, int len
)
1510 for( int i
=0; i
<len
; i
++ )
1512 rb_ct
*ct
= &buffer
[i
];
1513 ct
->bias
= -0.2f
* k_rb_rate
* vg_minf( 0.0f
, -ct
->p
+k_penetration_slop
);
1514 rb_tangent_basis( ct
->n
, ct
->t
[0], ct
->t
[1] );
1516 ct
->norm_impulse
= 0.0f
;
1517 ct
->tangent_impulse
[0] = 0.0f
;
1518 ct
->tangent_impulse
[1] = 0.0f
;
1520 v3f ra
, rb
, raCn
, rbCn
, raCt
, rbCt
;
1521 v3_sub( ct
->co
, ct
->rba
->co
, ra
);
1522 v3_sub( ct
->co
, ct
->rbb
->co
, rb
);
1523 v3_cross( ra
, ct
->n
, raCn
);
1524 v3_cross( rb
, ct
->n
, rbCn
);
1526 /* orient inverse inertia tensors */
1528 m3x3_mulv( ct
->rba
->iIw
, raCn
, raCnI
);
1529 m3x3_mulv( ct
->rbb
->iIw
, rbCn
, rbCnI
);
1531 ct
->normal_mass
= ct
->rba
->inv_mass
+ ct
->rbb
->inv_mass
;
1532 ct
->normal_mass
+= v3_dot( raCn
, raCnI
);
1533 ct
->normal_mass
+= v3_dot( rbCn
, rbCnI
);
1534 ct
->normal_mass
= 1.0f
/ct
->normal_mass
;
1536 for( int j
=0; j
<2; j
++ )
1539 v3_cross( ct
->t
[j
], ra
, raCt
);
1540 v3_cross( ct
->t
[j
], rb
, rbCt
);
1541 m3x3_mulv( ct
->rba
->iIw
, raCt
, raCtI
);
1542 m3x3_mulv( ct
->rbb
->iIw
, rbCt
, rbCtI
);
1544 ct
->tangent_mass
[j
] = ct
->rba
->inv_mass
+ ct
->rbb
->inv_mass
;
1545 ct
->tangent_mass
[j
] += v3_dot( raCt
, raCtI
);
1546 ct
->tangent_mass
[j
] += v3_dot( rbCt
, rbCtI
);
1547 ct
->tangent_mass
[j
] = 1.0f
/ct
->tangent_mass
[j
];
1550 rb_debug_contact( ct
);
1555 * Creates relative contact velocity vector, and offsets between each body
1557 static void rb_rcv( rb_ct
*ct
, v3f rv
, v3f da
, v3f db
)
1559 rigidbody
*rba
= ct
->rba
,
1562 v3_sub( ct
->co
, rba
->co
, da
);
1563 v3_sub( ct
->co
, rbb
->co
, db
);
1566 v3_cross( rba
->w
, da
, rva
);
1567 v3_add( rba
->v
, rva
, rva
);
1568 v3_cross( rbb
->w
, db
, rvb
);
1569 v3_add( rbb
->v
, rvb
, rvb
);
1571 v3_sub( rva
, rvb
, rv
);
1575 * Apply impulse to object
1577 static void rb_linear_impulse( rigidbody
*rb
, v3f delta
, v3f impulse
)
1580 v3_muladds( rb
->v
, impulse
, rb
->inv_mass
, rb
->v
);
1582 /* Angular velocity */
1584 v3_cross( delta
, impulse
, wa
);
1586 m3x3_mulv( rb
->iIw
, wa
, wa
);
1587 v3_add( rb
->w
, wa
, rb
->w
);
1591 * One iteration to solve the contact constraint
1593 static void rb_solve_contacts( rb_ct
*buf
, int len
)
1595 for( int i
=0; i
<len
; i
++ )
1597 struct contact
*ct
= &buf
[i
];
1598 rigidbody
*rb
= ct
->rba
;
1601 rb_rcv( ct
, rv
, da
, db
);
1604 for( int j
=0; j
<2; j
++ )
1606 float f
= k_friction
* ct
->norm_impulse
,
1607 vt
= v3_dot( rv
, ct
->t
[j
] ),
1608 lambda
= ct
->tangent_mass
[j
] * -vt
;
1610 float temp
= ct
->tangent_impulse
[j
];
1611 ct
->tangent_impulse
[j
] = vg_clampf( temp
+ lambda
, -f
, f
);
1612 lambda
= ct
->tangent_impulse
[j
] - temp
;
1615 v3_muls( ct
->t
[j
], lambda
, impulse
);
1616 rb_linear_impulse( ct
->rba
, da
, impulse
);
1618 v3_muls( ct
->t
[j
], -lambda
, impulse
);
1619 rb_linear_impulse( ct
->rbb
, db
, impulse
);
1623 rb_rcv( ct
, rv
, da
, db
);
1624 float vn
= v3_dot( rv
, ct
->n
),
1625 lambda
= ct
->normal_mass
* (-vn
+ ct
->bias
);
1627 float temp
= ct
->norm_impulse
;
1628 ct
->norm_impulse
= vg_maxf( temp
+ lambda
, 0.0f
);
1629 lambda
= ct
->norm_impulse
- temp
;
1632 v3_muls( ct
->n
, lambda
, impulse
);
1633 rb_linear_impulse( ct
->rba
, da
, impulse
);
1635 v3_muls( ct
->n
, -lambda
, impulse
);
1636 rb_linear_impulse( ct
->rbb
, db
, impulse
);
1641 * -----------------------------------------------------------------------------
1643 * -----------------------------------------------------------------------------
1646 static void draw_angle_limit( v3f c
, v3f major
, v3f minor
,
1647 float amin
, float amax
, float measured
,
1652 v3_muls( major
, f
, ay
);
1653 v3_muls( minor
, f
, ax
);
1655 for( int x
=0; x
<16; x
++ )
1657 float t0
= (float)x
/ 16.0f
,
1658 t1
= (float)(x
+1) / 16.0f
,
1659 a0
= vg_lerpf( amin
, amax
, t0
),
1660 a1
= vg_lerpf( amin
, amax
, t1
);
1663 v3_muladds( c
, ay
, cosf(a0
), p0
);
1664 v3_muladds( p0
, ax
, sinf(a0
), p0
);
1665 v3_muladds( c
, ay
, cosf(a1
), p1
);
1666 v3_muladds( p1
, ax
, sinf(a1
), p1
);
1668 vg_line( p0
, p1
, colour
);
1671 vg_line( c
, p0
, colour
);
1673 vg_line( c
, p1
, colour
);
1677 v3_muladds( c
, ay
, cosf(measured
)*1.2f
, p2
);
1678 v3_muladds( p2
, ax
, sinf(measured
)*1.2f
, p2
);
1679 vg_line( c
, p2
, colour
);
1682 static void rb_debug_constraint_limits( rigidbody
*ra
, rigidbody
*rb
, v3f lca
,
1685 v3f ax
, ay
, az
, bx
, by
, bz
;
1686 m3x3_mulv( ra
->to_world
, (v3f
){1.0f
,0.0f
,0.0f
}, ax
);
1687 m3x3_mulv( ra
->to_world
, (v3f
){0.0f
,1.0f
,0.0f
}, ay
);
1688 m3x3_mulv( ra
->to_world
, (v3f
){0.0f
,0.0f
,1.0f
}, az
);
1689 m3x3_mulv( rb
->to_world
, (v3f
){1.0f
,0.0f
,0.0f
}, bx
);
1690 m3x3_mulv( rb
->to_world
, (v3f
){0.0f
,1.0f
,0.0f
}, by
);
1691 m3x3_mulv( rb
->to_world
, (v3f
){0.0f
,0.0f
,1.0f
}, bz
);
1694 px
[0] = v3_dot( ay
, by
);
1695 px
[1] = v3_dot( az
, by
);
1697 py
[0] = v3_dot( az
, bz
);
1698 py
[1] = v3_dot( ax
, bz
);
1700 pz
[0] = v3_dot( ax
, bx
);
1701 pz
[1] = v3_dot( ay
, bx
);
1703 float r0
= atan2f( px
[1], px
[0] ),
1704 r1
= atan2f( py
[1], py
[0] ),
1705 r2
= atan2f( pz
[1], pz
[0] );
1708 m4x3_mulv( ra
->to_world
, lca
, c
);
1709 draw_angle_limit( c
, ay
, az
, limits
[0][0], limits
[1][0], r0
, 0xff0000ff );
1710 draw_angle_limit( c
, az
, ax
, limits
[0][1], limits
[1][1], r1
, 0xff00ff00 );
1711 draw_angle_limit( c
, ax
, ay
, limits
[0][2], limits
[1][2], r2
, 0xffff0000 );
1714 static void rb_limit_cure( rigidbody
*ra
, rigidbody
*rb
, v3f axis
, float d
)
1718 float avx
= v3_dot( ra
->w
, axis
) - v3_dot( rb
->w
, axis
);
1719 float joint_mass
= rb
->inv_mass
+ ra
->inv_mass
;
1720 joint_mass
= 1.0f
/joint_mass
;
1722 float bias
= (k_limit_bias
* k_rb_rate
) * d
,
1723 lambda
= -(avx
+ bias
) * joint_mass
;
1725 /* Angular velocity */
1727 v3_muls( axis
, lambda
* ra
->inv_mass
, wa
);
1728 v3_muls( axis
, -lambda
* rb
->inv_mass
, wb
);
1730 v3_add( ra
->w
, wa
, ra
->w
);
1731 v3_add( rb
->w
, wb
, rb
->w
);
1735 static void rb_constraint_limits( rigidbody
*ra
, v3f lca
,
1736 rigidbody
*rb
, v3f lcb
, v3f limits
[2] )
1738 v3f ax
, ay
, az
, bx
, by
, bz
;
1739 m3x3_mulv( ra
->to_world
, (v3f
){1.0f
,0.0f
,0.0f
}, ax
);
1740 m3x3_mulv( ra
->to_world
, (v3f
){0.0f
,1.0f
,0.0f
}, ay
);
1741 m3x3_mulv( ra
->to_world
, (v3f
){0.0f
,0.0f
,1.0f
}, az
);
1742 m3x3_mulv( rb
->to_world
, (v3f
){1.0f
,0.0f
,0.0f
}, bx
);
1743 m3x3_mulv( rb
->to_world
, (v3f
){0.0f
,1.0f
,0.0f
}, by
);
1744 m3x3_mulv( rb
->to_world
, (v3f
){0.0f
,0.0f
,1.0f
}, bz
);
1747 px
[0] = v3_dot( ay
, by
);
1748 px
[1] = v3_dot( az
, by
);
1750 py
[0] = v3_dot( az
, bz
);
1751 py
[1] = v3_dot( ax
, bz
);
1753 pz
[0] = v3_dot( ax
, bx
);
1754 pz
[1] = v3_dot( ay
, bx
);
1756 float r0
= atan2f( px
[1], px
[0] ),
1757 r1
= atan2f( py
[1], py
[0] ),
1758 r2
= atan2f( pz
[1], pz
[0] );
1760 /* calculate angle deltas */
1761 float dx
= 0.0f
, dy
= 0.0f
, dz
= 0.0f
;
1763 if( r0
< limits
[0][0] ) dx
= limits
[0][0] - r0
;
1764 if( r0
> limits
[1][0] ) dx
= limits
[1][0] - r0
;
1765 if( r1
< limits
[0][1] ) dy
= limits
[0][1] - r1
;
1766 if( r1
> limits
[1][1] ) dy
= limits
[1][1] - r1
;
1767 if( r2
< limits
[0][2] ) dz
= limits
[0][2] - r2
;
1768 if( r2
> limits
[1][2] ) dz
= limits
[1][2] - r2
;
1771 m3x3_mulv( ra
->to_world
, lca
, wca
);
1772 m3x3_mulv( rb
->to_world
, lcb
, wcb
);
1774 rb_limit_cure( ra
, rb
, ax
, dx
);
1775 rb_limit_cure( ra
, rb
, ay
, dy
);
1776 rb_limit_cure( ra
, rb
, az
, dz
);
1779 static void rb_debug_constraint_position( rigidbody
*ra
, v3f lca
,
1780 rigidbody
*rb
, v3f lcb
)
1783 m3x3_mulv( ra
->to_world
, lca
, wca
);
1784 m3x3_mulv( rb
->to_world
, lcb
, wcb
);
1787 v3_add( wca
, ra
->co
, p0
);
1788 v3_add( wcb
, rb
->co
, p1
);
1789 vg_line_pt3( p0
, 0.005f
, 0xffffff00 );
1790 vg_line_pt3( p1
, 0.005f
, 0xffffff00 );
1791 vg_line( p0
, p1
, 0xffffff00 );
1794 static void rb_constraint_position( rigidbody
*ra
, v3f lca
,
1795 rigidbody
*rb
, v3f lcb
)
1797 /* C = (COa + Ra*LCa) - (COb + Rb*LCb) = 0 */
1799 m3x3_mulv( ra
->to_world
, lca
, wca
);
1800 m3x3_mulv( rb
->to_world
, lcb
, wcb
);
1803 v3_sub( ra
->v
, rb
->v
, rcv
);
1806 v3_cross( ra
->w
, wca
, rcv_Ra
);
1807 v3_cross( rb
->w
, wcb
, rcv_Rb
);
1808 v3_add( rcv_Ra
, rcv
, rcv
);
1809 v3_sub( rcv
, rcv_Rb
, rcv
);
1813 v3_add( wca
, ra
->co
, p0
);
1814 v3_add( wcb
, rb
->co
, p1
);
1815 v3_sub( p1
, p0
, delta
);
1817 float dist2
= v3_length2( delta
);
1819 if( dist2
> 0.00001f
)
1821 float dist
= sqrtf(dist2
);
1822 v3_muls( delta
, 1.0f
/dist
, delta
);
1824 float joint_mass
= rb
->inv_mass
+ ra
->inv_mass
;
1826 v3f raCn
, rbCn
, raCt
, rbCt
;
1827 v3_cross( wca
, delta
, raCn
);
1828 v3_cross( wcb
, delta
, rbCn
);
1830 /* orient inverse inertia tensors */
1832 m3x3_mulv( ra
->iIw
, raCn
, raCnI
);
1833 m3x3_mulv( rb
->iIw
, rbCn
, rbCnI
);
1834 joint_mass
+= v3_dot( raCn
, raCnI
);
1835 joint_mass
+= v3_dot( rbCn
, rbCnI
);
1836 joint_mass
= 1.0f
/joint_mass
;
1838 float vd
= v3_dot( rcv
, delta
),
1839 bias
= -(k_joint_bias
* k_rb_rate
) * dist
,
1840 lambda
= -(vd
+ bias
) * joint_mass
;
1843 v3_muls( delta
, lambda
, impulse
);
1844 rb_linear_impulse( ra
, wca
, impulse
);
1845 v3_muls( delta
, -lambda
, impulse
);
1846 rb_linear_impulse( rb
, wcb
, impulse
);
1849 v3_muladds( ra
->co
, delta
, dist
* k_joint_correction
, ra
->co
);
1850 v3_muladds( rb
->co
, delta
, -dist
* k_joint_correction
, rb
->co
);
1858 static void rb_effect_simple_bouyency( rigidbody
*ra
, v4f plane
,
1859 float amt
, float drag
)
1862 float depth
= v3_dot( plane
, ra
->co
) - plane
[3],
1863 lambda
= vg_clampf( -depth
, 0.0f
, 1.0f
) * amt
;
1865 v3_muladds( ra
->v
, plane
, lambda
* ktimestep
, ra
->v
);
1868 v3_muls( ra
->v
, 1.0f
-(drag
*ktimestep
), ra
->v
);
1872 * -----------------------------------------------------------------------------
1873 * BVH implementation, this is ONLY for static rigidbodies, its to slow for
1875 * -----------------------------------------------------------------------------
1878 static void rb_bh_expand_bound( void *user
, boxf bound
, u32 item_index
)
1880 rigidbody
*rb
= &((rigidbody
*)user
)[ item_index
];
1881 box_concat( bound
, rb
->bbx_world
);
1884 static float rb_bh_centroid( void *user
, u32 item_index
, int axis
)
1886 rigidbody
*rb
= &((rigidbody
*)user
)[ item_index
];
1887 return (rb
->bbx_world
[axis
][0] + rb
->bbx_world
[1][axis
]) * 0.5f
;
1890 static void rb_bh_swap( void *user
, u32 ia
, u32 ib
)
1892 rigidbody temp
, *rba
, *rbb
;
1893 rba
= &((rigidbody
*)user
)[ ia
];
1894 rbb
= &((rigidbody
*)user
)[ ib
];
1901 static void rb_bh_debug( void *user
, u32 item_index
)
1903 rigidbody
*rb
= &((rigidbody
*)user
)[ item_index
];
1904 rb_debug( rb
, 0xff00ffff );
1907 static bh_system bh_system_rigidbodies
=
1909 .expand_bound
= rb_bh_expand_bound
,
1910 .item_centroid
= rb_bh_centroid
,
1911 .item_swap
= rb_bh_swap
,
1912 .item_debug
= rb_bh_debug
,
1916 #endif /* RIGIDBODY_H */