Repository navigation
Replace Quadtree with more accurate implementation - #926
Conversation
| 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)); |
There was a problem hiding this comment.
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.
| { | ||
| 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); |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
I just realized I never went back and renamed this - open to suggestions for a better method name here 😆
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
| Remove(item); | ||
| return Insert(item); |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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?
|
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. |
|
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. |
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.
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.
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.
In-game comparison
Old
New