Skip to content

Place each sweep-and-prune endpoint independently - #2

Draft
xuchen-han wants to merge 5 commits into
masterfrom
sap-update-changing-extents
Draft

xuchen-han wants to merge 5 commits into
masterfrom
sap-update-changing-extents

Conversation

@xuchen-han

@xuchen-han xuchen-han commented Aug 28, 2026 •

Copy link
Copy Markdown
Owner

Context

The broadphase is the first stage of collision detection. It finds the pairs of
objects that are near each other.

SaPCollisionManager does this with sweep and prune. For each of the three
axes, it gives every object one interval. An interval has two endpoints, lo
and hi. The manager holds all endpoints in a sorted list, one list per axis.

When an object moves, update_() moves that object's two endpoints to their
new places in the list. It does not sort the whole list again.

The problem

update_() calculates one direction, and it uses only the lo endpoint:

if(current->lo->getVal(coord) > new_min[coord])      direction = -1;
else if(current->lo->getVal(coord) < new_min[coord]) direction = 1;
else direction = 0;

It then moves both endpoints in that direction.

This is correct only while the interval keeps its width. In that case lo and
hi always move together. If the width changes, the two endpoints move in
opposite directions. The single direction is then wrong for one of them.

What breaks

Look at the branch for direction == -1, with a hi endpoint that must move to
the right. The loop that walks to the left stops immediately:

temp = current->hi;
while(temp->getVal(coord) > new_max[coord]) { ...; temp = temp->prev[coord]; }

temp is still current->hi. The code after the loop puts that node after
itself. It sets hi->prev = hi and hi->next = hi.

The node now points to itself and the list is broken. Each later walk gives a
wrong result, or it never stops. The branch for direction == 1 has the same
fault on the lo endpoint.

I put a temporary assert at that splice and it fired:
dir=-1 but hi must move RIGHT (coord=2): hi=-435.94 new_max=-434.62.

Master has this fault today

A collision object cannot replace its geometry. The width of an AABB can
therefore change only inside computeAABB().

computeAABB() has two paths. For an object with no rotation, it uses the
object's local AABB. For a rotated object, it uses a cube around the bounding
sphere. The two boxes have different widths. An object that starts or stops
being axis-aligned changes the width of its AABB. The new test does exactly
this, and it fails on master.

On master the result is a wrong answer: sweep and prune loses pairs that are
really overlapping.

I found the fault while I worked on flexible-collision-library#664. That change makes the AABB depend on
the orientation, so the width changes at every step. The result is an endless
loop instead of a wrong answer, and that loop stops CI with a timeout. This fix
does not need flexible-collision-library#664 and corrects the fault on its own.

The correction

Each endpoint now calculates its own direction. The endpoint that moves to the
right moves first. This keeps lo behind hi after each step.

The four blocks of code that place an endpoint do not change. Only the choice
of block and the order change. If the width does not change, the two directions
agree, the same blocks run in the same order, and the behavior stays the same as
before.

Each block now runs only when its own endpoint must move. Each loop therefore
makes at least one step, and the bad splice cannot happen again.

Tests

test/broadphase/test_broadphase_SaP.cpp is new. It puts twelve long boxes into
a SaPCollisionManager and a NaiveCollisionManager. It turns the boxes about
the z axis eight times. After each turn the two managers must report the same
pairs.

Without the correction the test fails at the first turn. Sweep and prune reports
25 pairs, and the reference reports more than 60.

I built this branch from source and ran these tests:

  • the new test passes, and it fails with the old update_;
  • test_fcl_broadphase_collision_1, _2, _distance and
    test_broadphase_dynamic_AABB_tree pass, which is 18 tests;
  • all of these tests also pass with the old computeAABB(), so the correction
    is safe by itself.

This change is Reviewable

xuchen-han and others added 5 commits August 27, 2026 22:47
update_() took one direction from the interval's "lo" endpoint and used it to
place both endpoints, which only holds while an object's AABB keeps its width.
When the width changes, "lo" and "hi" travel in opposite directions, the walk
for "hi" stops on the endpoint being moved, and the splice links that node to
itself. The lists are then cut, so the manager misses overlaps or walks a cycle
forever. An AABB changes width whenever an object leaves or returns to an
identity rotation, because computeAABB() bounds an unrotated geometry by its
local box and a rotated one by the cube around its bounding sphere.

Give each endpoint its own direction, and move the one travelling right first
so that "lo" stays behind "hi" at every intermediate step. When the width does
not change the two directions agree and the behaviour is unchanged.
1) Windows test recovery would occasionally fail after successful
   completion of build. The default time is five seconds. We've explicitly
   extended it to 20 seconds to give the flakes a bit more time to
   settle out.
2) The github action jobs (mac and linux) no longer stop evaluation if
   any other test (including appveyor) fail.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants