- Home /
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?
Answer by Fattie · Jun 19, 2012 at 08:37 PM
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 !
http://answers.unity3d.com/questions/193695/in-unity-is-there-a-fast-way-to-find-nearby-triang.html
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!
I've searched PhysX SD$$anonymous$$ and there seem to be no solution there either. Fortunately I've found octree implementation on Unity forums that looks promising for my purposes. Here's the link: http://forum.unity3d.com/threads/134554-A-solution-for-accurate-raycasting-without-mesh-colliders
I'm accepting this as an answer, cause building your own spatial tree, as suggested in the linked question, seems to be the only way.
Hi Zbyl ...
for sure, but don't forget ... computers are incredibly fast these days Heh !!!
I do this sort of nonsense on a daily basis and I've never found the need to do any spatial hashing...
It's incredibly fast to just traverse ("use a for loop") any mesh you'd see in a video game.
So I'd encourage you to make a separate subsection of your project that has - err -- a for loop! and if you ever have any performance problems, have someone bolt in some spatial hashing at a later date.
I hope it helps in some way !!