Find all triangles inside given bounds

I’d like to find all MeshCollider triangles inside a Sphere or a Box.

Unfortunately:

  • Physics.SphereCastAll() returns only
    first hit, so only one triangle.
  • Physics.RaycastAll() is not a volume
    query.

I want to analyze geometry around the character, so I need to limit the number of triangles to process.
The physics engine has all the necessary data to answer such a query very quickly, but is there a way to do it using the provided interface?

You probably know, you could just look through the vertices, and determine if each one is in the box or not. (Couple lines of code.)

“The physics engine has all the necessary data to answer such a query very quickly”

Sounds vaguely sensible to me, but the answers to that type of question can be surprising !

I don’t necessarily see that PhysX would Know all the tris in a box. Just as you say, raycasting is not a volume query.


Aside: should one do spatial hashing?

In a word: no. I’ve never found it to be necessary.

Think about how incredibly fast it is to just check points in a box. (Even if you deliberately write the code AS BAD AS POSSIBLE, it’s only gonna be two or three times slower than if you write the code as efficiently as possible! heh!!)

And don’t forget - there are only a trivial amount of verts in video game meshes.

Quite simply, do it first with a couple of lines of code. If, incredibly, you find you need spatial hashing - just bolt it on LATER.

I take it back - once we had to use a spatial hash because of a weird algorithm that iteratively had to search various meshes many many times - so someone stuck in some hashing to speed it up a bit. ie, we replaced the couple of lines of code with some lame class that hashed it’s way around space.

Don’t forget during every everyday FRAME, for goodness sake, your 3D stack is performing miracles of computation, just to bring up the “click to continue” on the screen!