Skip to content

Fix min node queryray closest#14

Open
Ph0t0nX wants to merge 3 commits into
PhilSA:mainfrom
Ph0t0nX:fix-min-node-queryray-closest
Open

Fix min node queryray closest#14
Ph0t0nX wants to merge 3 commits into
PhilSA:mainfrom
Ph0t0nX:fix-min-node-queryray-closest

Conversation

@Ph0t0nX

@Ph0t0nX Ph0t0nX commented May 17, 2026

Copy link
Copy Markdown

This PR has two main things:

  1. I made a small change in BVHPrecomputedHierarchyJob.Execute() that fixes the error where: if there are 2 or 3 nodes inserted in the tree, only one of them gets added. I verified it with a gizmo showing all bvh nodes in the scene - before the fix with 3 entities, only one showed a box around it, and after the fix all 3 entities had boxes around them. I tested with 1, 2, and 4 as well.

  2. I added 3 functions. 2 versions of QueryRayClosest(), and one QueryRayAny(). In order to make QueryRayClosest() more efficient, I had to make a tiny modification to AABB.IntersectRay() -- I needed the tEntry parameter. I overloaded it so that all calls to IntersectRay() simply call the new IntersectRay() and ignore the tEntry. Using the tEntry allowed me to keep shrinking the ray and prune out a lot of the ray-AABB tests, so it's faster than running a normal QueryRay() and using a collector. I tested it for a while using my selection/hover code in my game and it seemed to run fine - selecting entities in front of other entities.

Disclosure: I am not a BVH expert and I used Claude. I looked over the code as best I could and tested with it, but it's possible that there is a mistake that slipped through. I apologize for that, but I have so many things on my platet right now and I don't have a week to study the BVH implementation. The good news is that aside from the really tiny 2-3 node fix and the tiny change to AABB.IntersectRay() (which I can verify as correct), it's just 3 new functions which can't break anything existing so the risk is low. I will be using QueryRayClosest() for all my projectiles and QueryRayAny() for my turret LOS tests soon, so it'll be getting a lot of testing.

Ph0t0nX added 3 commits May 16, 2026 22:54
… (SortedNodes.Length < BVHUtils.NbLeavesPerNode)" to "if (SortedNodes.Length <= 1)" . This fixes an issue where if the tree had 2 or 3 nodes, only one node was getting included. Now the early-out only triggers for 0 or 1 nodes, and lets the existing padding code handle cases with 2 and 3 nodes.
… have not tested QueryRayAny() yet, but QueryRayClosest() seems to work as expected. It uses pruning so that every subsequent ray-AABB test uses a shrunken length, and IntersectsRay rejects any AABB whose entry is past it. This required a small change in the AABB struct's IntersectRay() function.
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.

1 participant