Skip to content

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

Merged
SeanCurtis-TRI merged 4 commits into
flexible-collision-library:masterfrom
xuchen-han:sap-update-changing-extents
Sep 2, 2026
Merged

SeanCurtis-TRI merged 4 commits into
flexible-collision-library:masterfrom
xuchen-han:sap-update-changing-extents

Conversation

@xuchen-han

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

Copy link
Copy Markdown
Collaborator

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 #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 #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

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.

@SeanCurtis-TRI SeanCurtis-TRI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SeanCurtis-TRI reviewed 2 files and all commit messages, and made 10 comments.
Reviewable status: 2 of 3 files reviewed, 10 unresolved discussions (waiting on xuchen-han).


test/broadphase/test_broadphase_SaP.cpp line 37 at r1 (raw file):

/** @author Xuchen Han (xuchenhan123@gmail.com) */

/** Tests the sweep-and-prune manager's incremental update. */

nit: This would be better applied to the test as opposed to implying the intended scope of the whole file.


test/broadphase/test_broadphase_SaP.cpp line 80 at r1 (raw file):

}

PairSet Overlaps(BroadPhaseCollisionManager<double>* manager,

nit: The function desperately needs a verb: FindOverlaps(), perhaps?


test/broadphase/test_broadphase_SaP.cpp line 114 at r1 (raw file):

  EXPECT_EQ(Overlaps(&sap, raw), Overlaps(&reference, raw));

  // Yaw every object by a different angle, which grows some intervals and

nit: This is a lie. Because the collision Geometry is a box, for any non-identity rotation, we only get the bounding cube of the bounding sphere of the bounding box of the box (sorry, I just had to write that out for the fun of it). Generally, that means in the first rotation every AABB got bigger. The only way that any of them would subsequently get smaller is if one of these rotations happened to go to 0. It's clear from the constants that that won't be happening any time soon.

Given that, the 12 objects and the 8 steps are really overkill for this test.


include/fcl/broadphase/broadphase_SaP-inl.h line 343 at r1 (raw file):

    // direction taken from "lo" would splice "hi" against itself and break the
    // list. When the width does not change the two directions agree, which is
    // the only case this function used to see.

nit: This is a very claude-ish comment. It's contrasting code that now exists with code that no longer exists. Probably worth making the code stand on its own two feet:

Suggestion:

    // End points can move for two reasons:
    //  1. the object has translated
    //  2. The object's AABB has changed; collision objects will frequently
    //     change AABB's based on orientation.
    //
    // For (1), the endpoints move in rigid lockstep along the axes. For (2),
    // the endpoints can move in independent directions. So, we need to
    // generally compute both endpoints explicitly.

include/fcl/broadphase/broadphase_SaP-inl.h line 344 at r1 (raw file):

    // list. When the width does not change the two directions agree, which is
    // the only case this function used to see.
    int lo_direction; // -1 reverse, 0 nochange, 1 forward

nit: The terms "reverse", "nochange" and "forward" aren't great. "nochange" isn't a "direction". Perhaps "negative" and "positive" might be better for implying movement along an axis.

As for "nochange", that's really just a sentinel value that we never test on. There must be a better designation. Possibly no op?


include/fcl/broadphase/broadphase_SaP-inl.h line 344 at r1 (raw file):

    // list. When the width does not change the two directions agree, which is
    // the only case this function used to see.
    int lo_direction; // -1 reverse, 0 nochange, 1 forward

nit: RAII would be better.


include/fcl/broadphase/broadphase_SaP-inl.h line 345 at r1 (raw file):

    // the only case this function used to see.
    int lo_direction; // -1 reverse, 0 nochange, 1 forward
    if(current->lo->getVal(coord) > new_min[coord])

BTW As long as we're here, I wouldn't mind fixing this horrible formatting.


include/fcl/broadphase/broadphase_SaP-inl.h line 358 at r1 (raw file):

    else hi_direction = 0;

    // The endpoint travelling right moves first, so "lo" stays behind "hi" in

nit: this statement isn't helpful.

  1. Instead of "forward" and "reverse", now we're using "right" (and, presumably, "left"). We should maintain coherent conceptual terminology.
  2. It's not clear why this invariant is important. I was hard-pressed to justify it.
  3. It only gives insight into this single block, and doesn't provide insight into any of the following blocks.

As a curiosity, I think the test conditions aren't right. Consider low and high values l0, h0, and l1, h1 (the cached vs the new). It seems hi should be updated first iff h1 >= l0. Otherwise, update lo first.


include/fcl/broadphase/broadphase_SaP-inl.h line 434 at r1 (raw file):

    else if(lo_direction == 1)
    {
        //then, update the "lo" endpoint of the interval.

nit: This branch isn't covered by test.


include/fcl/broadphase/broadphase_SaP-inl.h line 461 at r1 (raw file):

    if(hi_direction == -1)
    {
        // update hi end point

nit: This branch isn't covered by test.

@SeanCurtis-TRI

Copy link
Copy Markdown
Contributor

BTW I had a bit of a headache reasoning about what was going on here. In addition to determining the test had insufficient coverage, I had a hard time convincing myself there weren't other lurking problems. So, in addition to addressing the various quibbles listed below, I've pushed my own complete and total refactor.

PTAL (if you're curious)

@xuchen-han xuchen-han left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy as long as you (and CI) are happy!

@xuchen-han made 1 comment.
Reviewable status: 1 of 4 files reviewed, 10 unresolved discussions (waiting on SeanCurtis-TRI).

@SeanCurtis-TRI SeanCurtis-TRI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SeanCurtis-TRI reviewed 3 files and all commit messages, made 2 comments, and resolved 10 discussions.
Reviewable status: all files reviewed, 2 unresolved discussions (waiting on xuchen-han).


include/fcl/broadphase/broadphase_SaP.h line 127 at r2 (raw file):

  class FCL_EXPORT isNotValidPair;

  // Callback for updateEndPoint() -- should be one of addToOverlapPairs() or

nit: Oops. Two problems:

  1. Reference to method is out of date during changes.
  2. It's now only used internally, so there's not much point in defining it here.

include/fcl/broadphase/broadphase_SaP.h line 215 at r2 (raw file):

  /// @param head[out] The *current* head of the axi's linked list (will mutate
  ///                  if `this` is the current head).
  void removeFromList(size_t i, EndPoint*& head);

nit; The code more consistently uses coord. So, we'll name this to match.

@SeanCurtis-TRI SeanCurtis-TRI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SeanCurtis-TRI reviewed 2 files and all commit messages, and resolved 2 discussions.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on xuchen-han).

@SeanCurtis-TRI
SeanCurtis-TRI merged commit 0c178c7 into flexible-collision-library:master Sep 2, 2026
6 checks passed
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