Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Space partitioning optimization #3

@aevyrie

Description

@aevyrie

Further accelerate ray casting performance with some BVH/space partitioning

Thoughts on how to accomplish

Using bounding volumes, place entities into an octree. Maybe instead of a bounding sphere, find the oriented bounding box (OBB). This seems much better suited because we can simply apply the entity's GlobalTransform to the bounding box, and it will be exactly correct. We only need to recompute the OBB if the mesh changes. Checking if an OBB fits into an octree cell is trivial, we can just test each of the 8 vertices of the OBB to verify that they are within the 6 planes (faces) that bound a cell.

Placing entities in an octree

  1. Starting at the top level, recurse through the octree until the entity's bounding volume no longer fits into a node
  2. Place the entity into the last node it fit into
  3. Repeat for all entities (startup) and all changed entities (subsequent frames)

Ray casting into the octree

When casting a ray, now there will be two options - get all intersections, or just the topmost. If we are only finding the topmost intersection, we can speed up a ray cast by:

  1. Start in the topmost node that the ray's origin coordinate located in. (level 0)
  2. Recurse into the octree using the given coordinate, checking which child node it's located in, until the leaf node is reached. Record any entities that are found along the way.
  3. Check for intersection with all of the entities, first checking their boundary volumes before checking the meshes.
  4. Sort intersections and return the topmost intersection if one was found - all done!
  5. Go up a level at a time from the leaf node, until you find a node that has another (untested) child(ren)
  6. Test if the ray will intersect any of these child cells (up to 8-1 = 7) in this node. If there were no intersections, go to step 5.
  7. Go into the nearest intersected cell and repeat from step 2 using the intersection point plus some tiny epsilon in the ray direction, so we don't test cells we've already looked in.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions