Continuous collision detection
A moving-body code advances its bodies by a time step and needs the instant at which two of them first touch inside that step. Body–body distance answers at one instant. Sampling the step with it can miss a contact: a thin or fast body passes through the other between two samples (tunnelling). Issue #73 (roadmap #68, WP4) adds the time of first contact with a guarantee, in the module fossil_ccd.
Design and reference (issue #73)
The llm-wiki bundles have no source on body contact: every "collision" there is a collision term of a kinetic or Riemann solver. The method is conservative advancement (gk: Mirtich 1996; Zhang, Redon, Lee & Kim 2006, C2A; the FCL and Bullet libraries). Statements from general knowledge are marked (gk). CPU only: one query per pair is a sequence of distance queries, and the device gains nothing there.
At a glance
A square slides 0.18 above a floor and turns 45 degrees on its way into a wall. Frame k calls curve%time_of_impact(..., max_iterations=k), whose toi is the k-th iterate. The plain bound is held back by the floor gap and needs 25 steps; directional=.true. sees that the sliding does not close that gap and needs 5; both stop at s = 0.7728 (anim_ccd.f90).
The motion over a step
A step takes each body from a pose P0 = (R0, t0) at s = 0 to a pose P1 = (R1, t1) at s = 1, along the motion of pose_interpolate (moving bodies, issue #41), the same motion as the swept cut-cell geometry:
- the rotation R(s) = Q(s) R0, where Q(s) turns about the fixed unit axis k of R1 R0ᵀ by the angle s θ;
- the translation t(s) = t0 + s Δ, with Δ = t1 − t0.
A body point x_b (body frame) is at x(s) = Q(s) R0 x_b + t(s). The rotation turns about the axis through the body-frame origin, which moves with t(s). This is not Chasles' screw, whose axis is fixed in space. In 2-D, Q(s) is the in-plane rotation by s θ, with θ ∈ (−π, π].
The speed bound
The velocity is dx/ds = θ k × y + Δ with y = Q(s) R0 x_b. Q(s) turns about k, so |k × y| = ρ(x_b), the distance of R0 x_b to the axis through the body origin, does not depend on s. Hence, for every s,
|dx/ds| ≤ |Δ| + |θ| ρ(x_b).
ρ is a convex function of x_b (a distance to a line), so its maximum over the body (the hull of the vertices) is reached at a vertex. The bound of a body over the step is
μ = |Δ| + |θ| max_v ρ_v,
and no point of the body moves farther than μ |s2 − s1| between s1 and s2. In 2-D, ρ_v = |x_v| (the axis is the body origin). A body whose vertices lie far from its own frame origin has a large μ when it turns: it is its real motion, not slack. Measured in step 2: the bound is attained at the vertex farthest from the axis.
The end poses alone do not fix the motion: they fix it together with the body frame. With the frame origin at the rectangle's centre every vertex has ρ = 0.5385 and μ = 2.934; with the origin 1.2 to its left, between the same two placements, ρ reaches 1.7117 and μ = 5.945 (fig_ref_collision_bound.f90). Put the body frame at the centre of a body that turns, or accept twice the steps.
Conservative advancement
The distance d(s) between bodies A and B is Lipschitz: each point of A moves at most μ_A δ, each point of B at most μ_B δ, so
d(s + δ) ≥ d(s) − (μ_A + μ_B) δ.
From s_k with d_k = d(s_k) > tol, the step
s_{k+1} = min(1, s_k + (d_k − η tol) / (μ_A + μ_B)), η = 1/2,
keeps d ≥ η tol on [s_k, s_{k+1}]. The loop stops when d_k ≤ tol (contact at s_k), or at s = 1 with d(1) > tol (no contact over the step).
Guarantee (proved by the Lipschitz bound, up to the rounding of the distance query):
- d ≥ η tol on [0, toi], so toi is no later than the first time the bodies come within η tol, and a fortiori no later than the first contact (d = 0);
- d(toi) ≤ tol for a contact;
- no tunnelling, whatever the speed or the thickness: every step is bounded by the distance it starts from;
- at most ⌈(μ_A + μ_B) / ((1 − η) tol)⌉ iterations, since every step with d_k > tol advances at least (1 − η) tol / (μ_A + μ_B).
The distance comes from distance_to(..., pose_self=, pose_other=) (#67) at the interpolated poses: the trees are built once in the body frames, and the result is bit-identical to moving the bodies. Its rounding is a few ulps of the body size, far below the default tolerance.
| Status | Meaning | toi |
|---|---|---|
CCD_CONTACT | the bodies come within tolerance | the first iterate with d ≤ tol |
CCD_SEPARATE | no contact over [0, 1] | 1 (d(1) > tol) |
CCD_INITIAL_CONTACT | d(0) ≤ tol | 0 |
CCD_NOT_CONVERGED | the iteration cap (max_iterations, default 1000) was hit | the last iterate, still a certified lower bound on the contact time |
The tolerance is optional and defaults to 1e-6 of the larger bounding-box diagonal of the two bodies (the convention of deviation). Grazing approaches, where the bodies close slowly, converge slowly: that is what the cap is for.
A square of side 0.4 moves by (1.9, 2) and turns a quarter towards a wall, μ = 3.203 for a closing speed of about 2: curve%time_of_impact takes s = 0.5308, 0.7209, 0.8034, ... and stops at contact after 21 steps at s = 0.8764. Each Lipschitz line stays under d(s). Each step multiplies the distance by about 1 − c/μ, c the closing speed: here 1.7, 0.609, 0.264, 0.122, a little under one half per step (fig_ref_collision_advance.f90).
The directional bound (C2A)
The plain bound assumes the worst direction of motion. Along the unit witness direction n of a convex pair, the separating plane gives d ≥ n · (x_b − x_a), whose closing rate is bounded by the motion projected on n (Mirtich) (gk). This holds for convex sets only: a body that is not convex can close around the other from a side the plane does not see. FOSSIL's bodies are not convex, so the bound is applied per triangle pair (segment pair in 2-D), each of them convex, inside the descent of the two trees, as C2A does with per-node bounds (gk):
- a triangle pair at distance d_ij with witness direction n has the closing rate c_ij = n · (Δ_A − Δ_B) + |θ_A| ρ_A(T_a) + |θ_B| ρ_B(T_b), with ρ(T) the largest ρ of its vertices and n pointing from T_a to T_b (the gap along a fixed n bounds the pair's distance from below at every s, and its rate is at least −c_ij);
- its safe step is (d_ij − η tol) / c_ij (unbounded if c_ij ≤ 0), and the step is the minimum over all pairs;
- a node pair whose plain-bound step (box distance over the node μ) is already above the best is pruned.
With it the loop takes the larger of the plain and the directional step, both certified. A directional step costs far more than a plain one: on large bodies it evaluates tens of thousands of facet pairs exactly (see Measured). And the plain bound cannot tell tangential motion from approach: at a gap just above the tolerance it advances by about (gap − tol/2)/μ per step until the cap.
The default: adaptive (issue #75)
directional chooses between the two:
directional | Steps |
|---|---|
| absent (default) | plain, until the plain loop stalls; then the larger of the two for the rest of the loop |
.true. | the larger of the two from the first step: never more steps than the plain bound |
.false. | plain only |
A step stalls when it advances s by less than 1 % of the remaining interval 1 − s_k and closes less than 1 % of the margin d_k − tol/2. Eight stalled steps in a row switch the loop to the directional step (CCD_STALL_STEPS = 8, CCD_STALL_FRACTION = 0.01, exported by fossil). The second condition is what keeps an approach plain: near contact its steps become short as well, but each closes the margin by the ratio of the closing speed to μ, while a body sliding along another keeps its margin. Both steps are certified, so the switch keeps every guarantee (d ≥ tol/2 on [0, toi], d(toi) ≤ tol, no tunnelling), and a loop that never stalls is the plain loop bit for bit, at the plain cost. curve%time_of_impact, first_impact and first_impact_2d pass directional through and default to the same rule.
Several bodies
first_impact(surfaces, poses0, poses1, ...) gives the earliest contact among N bodies and its pair:
- broad phase: the box of each body at s = 0 grown by μ contains it over the whole step; grown by tol/2 more, two boxes that do not overlap keep their bodies farther than the tolerance, so only pairs of overlapping boxes go on;
- every remaining pair has a lower bound on its time of impact, max(0, box distance at s = 0 − tol) / (μ_A + μ_B) (the first advancement step, or 0 when the bodies may start within the tolerance); pairs run in the order of these bounds, and the search stops when the next bound exceeds the best contact found;
- ties go to the lexicographically smallest pair, so the result equals the brute force over all pairs, bit for bit.
Of the 10 pairs, 8 have overlapping grown boxes. first_impact_2d runs time_of_impact on 7 of them: the last candidate's lower bound exceeds the contact already found, at s = 0.91342 between squares 1 and 2 (fig_ref_collision_first.f90).
Example
The cookbook recipe When do my moving bodies first touch? shows one contact and a thin plate crossed within a step. This program measures the bound, the case the plain bound cannot finish, and several bodies.
program ref_collision
use fossil, only : surface_stl_object, rigid_pose_t, pose_identity, pose_from_axis_angle, motion_bound_3d, first_impact
use fossil_facet_object, only : facet_object
use penf, only : I4P, R8P
implicit none
real(R8P), parameter :: PI = acos(-1._R8P), TOL = 1.e-6_R8P
character(14), parameter :: STATUS_NAME(4) = ['contact ', 'separate ', 'initial ', 'not converged ']
type(surface_stl_object) :: a, b, centred, cubes(4)
type(rigid_pose_t) :: a0, a1, rest, p0(4), p1(4)
type(facet_object), pointer :: f
real(R8P), allocatable :: x(:), y(:), z(:)
real(R8P) :: toi, d
integer(I4P) :: i, k, status, steps, pairs, pair(2), tested
call a%load_from_file(file_name='cube-01.stl', guess_format=.true.) ! [0, 1]^3: the frame origin at a corner
allocate(x(0), y(0), z(0))
do i = 1, a%get_facets_number()
f => a%facet_at(i)
x = [x, (f%vertex(k)%x, k = 1, 3)] ; y = [y, (f%vertex(k)%y, k = 1, 3)] ; z = [z, (f%vertex(k)%z, k = 1, 3)]
enddo
a0 = pose_identity()
a1 = pose_from_axis_angle(axis=[0._R8P, 0._R8P, 1._R8P], angle=PI / 2, translation=[1._R8P, 0._R8P, 0._R8P])
print '(A,F8.5)', 'mu, frame origin at a corner: ', motion_bound_3d(a0, a1, x, y, z)
print '(A,F8.5)', 'mu, frame origin at the centre: ', motion_bound_3d(a0, a1, x - 0.5_R8P, y - 0.5_R8P, z - 0.5_R8P)
call b%load_from_file(file_name='cube-01.stl', guess_format=.true.) ! b rests on [0, 1]^3
rest = pose_identity()
a0 = pose_identity() ; a0%t = [-2._R8P, 0._R8P, 1._R8P + 4 * TOL] ! a slides over b's top face, 4 tol above it
a1 = a0 ; a1%t(1) = 2._R8P
call a%time_of_impact(b, a0, a1, rest, rest, toi, status, tolerance=TOL, distance=d, iterations=steps, &
directional=.false.) ! the plain bound only
print '(3A,F9.6,A,I0,A,ES10.3)', 'plain: ', STATUS_NAME(status), ' toi ', toi, ', steps ', steps, &
', distance ', d
call a%time_of_impact(b, a0, a1, rest, rest, toi, status, tolerance=TOL, distance=d, iterations=steps, &
directional=.true., pairs=pairs) ! the larger step from the first
print '(3A,F9.6,A,I0,A,ES10.3,A,I0)', 'directional: ', STATUS_NAME(status), ' toi ', toi, ', steps ', steps, &
', distance ', d, ', pairs ', pairs
call a%time_of_impact(b, a0, a1, rest, rest, toi, status, tolerance=TOL, distance=d, iterations=steps, &
pairs=pairs) ! default: adaptive
print '(3A,F9.6,A,I0,A,ES10.3,A,I0)', 'default: ', STATUS_NAME(status), ' toi ', toi, ', steps ', steps, &
', distance ', d, ', pairs ', pairs
call centred%load_from_file(file_name='cube-01.stl', guess_format=.true.)
call centred%translate(x=-0.5_R8P, y=-0.5_R8P, z=-0.5_R8P) ; call centred%analyze ! frame origin at the centre
cubes = centred
do i = 1, 4 ! four cubes on a row, 3 apart; 1 and 2 close in on each other, 3 turns in place, 4 drifts away
p0(i) = pose_identity() ; p0(i)%t = [3._R8P * (i - 1), 0._R8P, 0._R8P] ; p1(i) = p0(i)
enddo
p1(1)%t(1) = 1.5_R8P ; p1(2)%t(1) = 1.6_R8P
p1(3) = pose_from_axis_angle(axis=[1._R8P, 1._R8P, 0._R8P], angle=PI, translation=p0(3)%t)
p1(4)%t(1) = 12._R8P
call first_impact(cubes, p0, p1, toi, pair, status, tolerance=TOL, tested=tested)
print '(3A,2I2,A,F9.6,A,I0,A)', 'first impact: ', STATUS_NAME(status), ' pair', pair, ', toi ', toi, &
', ', tested, ' of 6 pairs tested'
endprogram ref_collision$ ref_collision
mu, frame origin at a corner: 3.22144
mu, frame origin at the centre: 2.11072
plain: not converged toi 0.250874, steps 1000, distance 4.000E-06
directional: separate toi 1.000000, steps 11, distance 1.000E+00, pairs 2662
default: separate toi 1.000000, steps 18, distance 1.000E+00, pairs 2430
first impact: contact pair 1 2, toi 0.689655, 3 of 6 pairs tested- The bound. cube-01 spans [0, 1]³, so its frame origin is a corner. For a quarter turn about z and a shift of 1,
motion_bound_3dgives μ = 1 + (π/2) √2 = 3.22144; the same vertices about the centre give 1 + (π/2)(√2/2) = 2.11072. - Sliding. The cube slides by 4 over the top face of the resting one, 4 tol above it. The plain bound cannot tell the sliding from an approach: from s = 0.25, where the moving cube reaches the resting one, each step advances by (4 − 1/2) tol / μ = 8.75e-7, and the loop stops at the cap, 1000 steps, with
CCD_NOT_CONVERGEDand s = 0.250874, a certified lower bound on any contact. Withdirectional=.true.the separating planes of the facet pairs see that the gap does not close: 11 steps and 2662 exact pair evaluations reach s = 1,CCD_SEPARATE. The default gets there too: its plain steps stall from s = 0.25 on, it switches after eight of them and reaches s = 1 in 18 steps and 2430 pair evaluations. - Several bodies. Four unit cubes 3 apart, their frames at their centres: 1 and 2 close in at 1.5 and 1.4 per step, 3 turns in place, 4 moves away.
first_impactfinds cubes 1 and 2 at s = 0.689655 = 2/2.9 (the gap 2 over the closing speed 2.9) and runstime_of_impacton 3 of the 6 pairs.
API
| Routine | Arguments |
|---|---|
motion_bound_3d(p0, p1, x, y, z) / motion_bound_2d(p0, p1, u, v) | μ of a vertex set |
ccd_advance(evaluator, mu, tolerance, toi, status, distance, iterations, max_iterations, directional) | the loop over an abstract distance evaluator (ccd_evaluator_t); directional absent: adaptive |
surface%time_of_impact(other, pose_self0, pose_self1, pose_other0, pose_other1, toi, status, ...) | optional: tolerance, distance, point_self, point_other, facet_self, facet_other, iterations, max_iterations, directional, pairs (exact pair evaluations of the directional steps) |
curve%time_of_impact(...) | the 2-D twin (segment_self, segment_other) |
first_impact(surfaces, poses0, poses1, toi, pair, status, tolerance, directional, tested) / first_impact_2d | several bodies (tested: pairs that ran time_of_impact) |
Proved and measured
Proved: the speed bound, the Lipschitz bound, the guarantee and the iteration bound above (up to the rounding of the distance query); the directional step per convex pair.
Measured (fossil_test_ccd_*, fossil_val_ccd, tolerance 1e-6):
| Case | Plain steps | Directional steps | Note |
|---|---|---|---|
| translation head-on (cubes, squares) | 1 | 1 | toi in [(1 − tol)/3, 1/3] |
| oblique, 45° to a face | 12 | 1 | |
| quarter turn onto a post | 25 | 25 | toi 1.1e-6 below the analytic angle |
| cube vs cube, turning and translating | 11 | 9-10 | within 1e-7 of the bisected brute force |
| bunny (5 000 facets) vs dragon (2 000), 8 steps | 9-35 | about half | plain 0.57 s, directional 7.8 s in all; 5e4-2e5 exact pairs per query; the default never stalls: plain toi, status and steps bit for bit, 0.56 s |
| sliding past a face at a gap of 2-256 tol | 1000 (cap) | 7 | plain: CCD_NOT_CONVERGED, a certified lower bound; the default: 14-15 steps, CCD_SEPARATE |
| a plate 1e-3 thick crossed within one step | 1 | 1 | apart at both end poses |
Every result keeps d ≥ tol/2 at the samples of [0, toi] and d(toi) ≤ tol. Six spheres over 10 sub-steps: first_impact equals the loop over all pairs bit for bit, testing 78 of 150 pairs.
Left out
- Exact CCD of straight vertex trajectories (vertex-face and edge-edge coplanarity cubics, Brochu, Edwards & Bridson 2012) (gk): it fits deforming meshes, not rigid rotation.
- Penetration depth and contact response: the solver's.
- Deformable bodies and self-collision over time.
- Device kernels.