- Home /
Calculate distance between two surfaces?
hmmm... seems like an obvious question that I can't seem to find answered.
Anyhow,
I'm trying to figure out how to calculate the distance between 2 surfaces, not between 2 objects. I suppose the distance could be computed as the difference between positions for regular objects with a known collider/mesh size. But let's say very irregularly shaped meshes.
I think this would amount to a. finding the point on each surface closest to the other surface and then b. getting the distance between those two.
Any tips or directions or thoughts on doing a. would be appreciated. Thanks!
Answer by rule62 · Mar 09, 2011 at 08:33 PM
It looks like this may be the way to go? (comments appreciated!)
EDIT:
Looks like this problem was solved already (though potentially computationally intensive, still).
Linecast and Raycast results in the same function. It's just another way of providing the parameters. Both takes a start point. Linecast takes an end point and Raycast a direction (which is the difference between end point and start point).
Right. Thanks for all the helpful comments. I think I may just go with the raycasting. The other approach is going to be so computationally intense.
Answer by StephanK · Mar 09, 2011 at 07:17 PM
Take a look at Collider.ClosestPointOnBounds
Excellent. Thank you. I was thinking a brute force would be to simply compute distances between mesh vertices of each surface.
Well, as i wrote, this will get you the closest point on the AABB (aka bounds
in unity)
If you are using mesh colliders you could also do raycasts and check which surface(triangle) you hit at which distance.
Answer by Bunny83 · Mar 09, 2011 at 07:58 PM
In general there are three cases.
- Vertex point vs. vertex point
- Vertex point vs. triangle face
- Triangle face vs. triangle face
If your mesh only have quite small triangles in 99% of all cases it will be number "1."
If the mesh is huge (like walls) you will also get the cases "2." and "3."
The question is: for what use and how precise you need the distance because even "1." will be a lot of testing. I think "2." and "3." will even include "1."
ps.
case 2 happens when a point from the second mesh, projected onto the triangleplane of the first mesh, will be inside this triangle.
case 3 is the special case when two faces are parallel and face each other (same face-normal but flipped)
You have to decide what you need and if it's worth to do the calculations or just use some approximation.
Collider.ClosestPointOnBounds is a very rough approximation since you get the closest point on the AABB which would be worse that just use the average bounding-sphere.
edit
In you have an arbitrary mesh (non convex) you will have to test all vertices. But you could try to pre-sort them out so you only test a few vertices. If you compare a lot of distances (just to sort them from far to near or reverse) use Vector3.sqrMagnitude instead of Vector3.magnitude ! Magnitude needs to calculate the square root internally which is very slow (cpu intensive). I guess the closest vertex to vertex distance should do in most cases. But if you have large walls you'll get into trouble because you will always get the distance to the nearest corner of the wall and not the wall surface. But an arbitrary method would be much more complicated (to cover all special cases).
Even with the best algorithm there are cases where you will have muliple equal distances (with non convex meshes). Non convex meshes can somehow intersect each other (like an object that is inside a bucket).
I need something more accurate than bounding spheres/boxes, etc.
This is a very interesting problem. Would be nice if things like oct-tree sorting were built in, but if this is a function you'd be running often, it might be worth implementing that yourself.
Nice link, i think the $$anonymous$$DTree comes in handy.
Your answer
Follow this Question
Related Questions
Check for an instantiated object farthest from player? 1 Answer
How to check if X Coordinate 1 > X Coordinate 2 1 Answer
Passing "best" to the other scene 0 Answers
Vector3.Distance edge of model 3 Answers
Check Between Enemy List 1 Answer