#Min Distance between a moving point and a mesh

1 messages · Page 1 of 1 (latest)

pallid wharf
#

Hi, everyone. I've a point that I move in the scene and I would like to know the distance, the shortest one (so no between mesh centers), between this point and a mesh. I've tried Collider.ClosestPoint but the problem is that my mesh collider cannot be set to convex because to create the convex collider Unity does some approximations that generates false distances (for example, distance 0 when clearly it is not). Has anyone some ideas how to compute this distance?

fresh walrus
pallid wharf
#

I need a precise distance algorithm. I can code without problems, but I really need a precise measure because I've to realize a sort of medical simulation

fresh walrus
#

In case you want to implement the algorithm yourself, we would better start with any constraints that you have for it such as precision (whether distance to closest vertex is sufficient for example) and performance (complexity of the meshes, any time/frame rate/check rate limits)

fresh walrus
#

The algorithm would generally look something like following. You would just loop through the triangles or vertices (if such precision is fine) and keep track of the closest one. Countless point to triangle distance algorithms can be found online to help with that: https://stackoverflow.com/questions/2924795/fastest-way-to-compute-point-to-triangle-distance-in-3d. If you need frequent checks and/or high resolution meshes, you would want to create some sort of partitioning structure such as octree to avoid checking against triangles that could not be closer than the closest found triangle based on it's AABB. There are definitely considerations to be done to which order you would traverse the structure to quickly find reasonable upper boundaries for the distance but the idea would be something like that

pallid wharf
#

This is my mesh, it has 21,000 vertices. I didn't model it on blender and I cannot modify it more than I already did. It should be a close mesh (the hole that is visible bottom right is not a real hole in mesh but more like a hole in a donut). Given the structure, I don't know if even a script that divides the mesh in multiple convex ones would work. I need also efficiency since the point is moving smoothly in the space. Hence, I think that iterating each frame through vertices it isn't a suitable solution. At the moment I am considering two solutions:

  • algotithm based on octree or KDtree as you said
  • I can define many capsule colliders to cover all the mesh, identify the closest one and then use closest point. I know it is an approximation but I think it's more easy and I can achieve a good approximation if I define correctly the colliders. In the end it is what the script of "convexification" would do
    What do you think?
fresh walrus
# pallid wharf This is my mesh, it has 21,000 vertices. I didn't model it on blender and I cann...

Either one could work. Making an optimized own algorithm isn't trivial at all though. If this is the only mesh you need (or you don't have too many), I would probably just make the collision shape out of primitives like capsule colliders and iterate through them to find the closest one.

A third option that I just now thought of was that if you know the bounds of the points you want the distance from, you could generate a 3d texture of the distance field which you could then simply sample (potenatially with trilinear sampling) when needed. This would obviously be super fast to sample, but does require knowing some bound for the points, loses bit of precision and requires some space to store. Unity has their own tool for generating such SDF it seems like: https://docs.unity3d.com/Packages/com.unity.visualeffectgraph@12.0/manual/sdf-bake-tool-window.html. There is also a realtime SDF API for doing that but that seems to be bit compromised in quality and sensitive to what kind of mesh you can feed it

pallid wharf
#

Ok thank you for your time. You've been very helpful🫶