Skip to content

Commit

Permalink
Collision: fix partition algorithm
Browse files Browse the repository at this point in the history
- Partition sort had a degenerate case where 1 element in the array would always be assigned to the disabled bucket, regardless of if it passed the partitioning test.
- The fix also makes the sort logic generally simpler as a side benefit.
  • Loading branch information
sturnclaw authored and Webster Sheets committed Jan 23, 2024
1 parent 46d6f0d commit c747d5d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/collider/BVHTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,17 +305,17 @@ uint32_t SingleBVHTree::Partition(SortKey *keys, uint32_t numKeys, const AABBd &

// Simple O(n) sort algorithm to sort all objects according to side of pivot
uint32_t startIdx = 0;
uint32_t endIdx = numKeys - 1;
uint32_t endIdx = numKeys;

// It is ~10% faster to sort the indices than to sort the whole AABB array
// (cache hit rate vs. memory bandwidth)
// Sorting in general is extremely fast.
while (startIdx <= endIdx && endIdx) {
while (startIdx < endIdx) {
if (keys[startIdx].center[axis] < pivot) {
startIdx++;
} else {
std::swap(keys[startIdx], keys[endIdx]);
endIdx--;
std::swap(keys[startIdx], keys[endIdx]);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/collider/CollisionSpace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,14 +236,14 @@ uint32_t CollisionSpace::SortEnabledGeoms(std::vector<Geom *> &geoms)
// Simple O(n) sort algorithm
// Sort geoms according to enabled state (group all enabled geoms at start of array)
uint32_t startIdx = 0;
uint32_t endIdx = geoms.size() - 1;
uint32_t endIdx = geoms.size();

while (startIdx <= endIdx && endIdx) {
while (startIdx < endIdx) {
if (geoms[startIdx]->IsEnabled()) {
startIdx++;
} else {
std::swap(geoms[startIdx], geoms[endIdx]);
endIdx--;
std::swap(geoms[startIdx], geoms[endIdx]);
}
}

Expand Down

0 comments on commit c747d5d

Please sign in to comment.