Skip to content

Replace Quadtree with more accurate implementation - #926

Merged
charliefoxtwo merged 2 commits into
OpenSAGE:masterfrom
charliefoxtwo:bugfix/quadtree-the-reckoning
Aug 1, 2024
Merged

charliefoxtwo merged 2 commits into
OpenSAGE:masterfrom
charliefoxtwo:bugfix/quadtree-the-reckoning

Conversation

@charliefoxtwo

Copy link
Copy Markdown
Contributor

Supersedes #925 (ported over tests)

Implementation

After benchmark testing with Orthogonal Arrays, the minimum node geometric size and max items per node parameters were tweaked for better performance.

Additionally, an overlap parameter was added. This scales the subgrids by the provided factor, such that each subgrid overlaps its neighbors. This helps prevent hotspots on cell borders from piling up in the parent nodes.

This new implementation persists the existing behavior that two entities whose borders perfectly touch are not considered colliding. If that is not the desired behavior we may wish to address that separately.

This also switches to using an interface in the method signatures instead of the class name itself, partially because that made it easier for me to flip back and forth between testing and partially so that if someone else ever wants to burn this quadtree with fire and replace it with their implementation that's even better, it will be easier for them as well :)

Benchmarks vs previous implementation

Insert

I would have expected the performance here to be similar, so it's interesting the new quadtree is so much faster.

Method MaxItemSize InsertedItems Mean Error StdDev Ratio
InsertQuadtree 15 5000 839.493 us 8.9584 us 8.3797 us 1.000
InsertNewTree 15 5000 3.346 us 0.0241 us 0.0226 us 0.004

Update

I'm not quite sure why the old quadtree updates were so slow. This benchmark also isn't actually recording the performance of a single update, but rather multiple.

Method TotalItems MovingItems Mean Error StdDev Ratio
UpdateQuadtree 15000 10000 3,205.934 ms 11.2854 ms 10.0043 ms 1.000
UpdateNewTree 15000 10000 4.007 ms 0.0544 ms 0.0455 ms 0.001

Query

Here it's no surprise that the new quadtree is slower - many items were missed in the previous implementation, so it's not really an apples-to-apples comparison.

Method ItemCount ItemSize AreaSideLength Mean Error StdDev Ratio RatioSD
QueryAreaQuadtree 3000 10 100 406.1 ns 2.62 ns 2.05 ns 1.00 0.00
QueryAreaNewTree 3000 10 100 1,634.2 ns 10.37 ns 9.19 ns 4.02 0.04

In-game comparison

Old

New

Comment on lines +30 to +33
var x = (float)(bounds.X + random.NextDouble() * bounds.Width);
var y = (float)(bounds.Y + random.NextDouble() * bounds.Height);
var width = (float)(random.NextDouble() * Math.Min(bounds.Width - x, maxSize.Width));
var height = (float)(random.NextDouble() * Math.Min(bounds.Height - y, maxSize.Height));

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I replaced this (specifically width/height) to attempt to better distribute the sizes of the items generated. Previously this would generate any size and then cap it to max size, which means that a majority of items would be max size.

Comment on lines +426 to +438
{
if (item1.Id == item2.Id)
{
continue;
}

if (!collisions.ContainsKey(item2.Id))
{
collisions[item2.Id] = [];
}

collisions[item1.Id].Add(item2.Id);
collisions[item2.Id].Add(item1.Id);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

If nothing else, we should make sure this is correct. As long as this is correct, then if the fuzzing passes we can feel confident that our quadtree implementation is correct.


namespace OpenSage.DataStructures;

public interface ICollidable

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is just split into a separate file (previously was nested in Quadtree)

}
}

private static bool CollidesForReal(T item, Collider collider, T? searcher, bool twoDimensional, bool hasToCollideWithSearcher)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I just realized I never went back and renamed this - open to suggestions for a better method name here 😆

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can you describe in one sentence what the method does? I've read the code but I'm not 100% sure - is it a precise collision test?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ah, that's it - CollidesPrecise should do the trick.

Yeah, this was ported over from the original implementation, but basically the quadtree does most of its checks on the GameObject's RoughCollider (so, an AABB), and then if that matches then this precise version is called which checks all the actual collidables against each other.

Comment on lines +57 to +58
Remove(item);
return Insert(item);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This was just copied over from the existing implementation, but there's probably room for optimization here. If we can determine that the node the item is in still fully captures the item, then we don't actually need to do anything here.

}
}

private static bool CollidesForReal(T item, Collider collider, T? searcher, bool twoDimensional, bool hasToCollideWithSearcher)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can you describe in one sentence what the method does? I've read the code but I'm not 100% sure - is it a precise collision test?

@tgjones

tgjones commented Aug 1, 2024

Copy link
Copy Markdown
Collaborator

This is awesome!

Often when the benchmark results are wildly different from expectations, it's worth double checking that the benchmarks actually measure what you think they do 😄 But assuming they do, then this looks like a very nice speedup on insert and update, with an expected slowdown for query since it's returning more data.

@charliefoxtwo

Copy link
Copy Markdown
Contributor Author

More benchmarks here couldn't hurt (remove isn't benchmarked at all currently), that's for sure. Since the new quadtree has an identical public signature to the old one, they used the exact same code in the benchmark methods, so I'm pretty comfortable saying they should have been testing the same exact same thing.

I bet this could be further optimized for even better performance as well - but I'm not super experienced in that area.

@charliefoxtwo
charliefoxtwo merged commit a99a71a into OpenSAGE:master Aug 1, 2024
@charliefoxtwo
charliefoxtwo deleted the bugfix/quadtree-the-reckoning branch August 1, 2024 22:01
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