Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by rule62 · Mar 09, 2011 at 06:48 PM · distancemeshesbetween

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
-1
Best Answer

Answer by rule62 · Mar 09, 2011 at 08:33 PM

It looks like this may be the way to go? (comments appreciated!)

Physics.Linecast

EDIT:

Looks like this problem was solved already (though potentially computationally intensive, still).

link text

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Mar 09, 2011 at 09:03 PM 0
Share

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).

avatar image rule62 · Mar 09, 2011 at 09:44 PM 0
Share

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.

avatar image
2

Answer by StephanK · Mar 09, 2011 at 07:17 PM

Take a look at Collider.ClosestPointOnBounds

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rule62 · Mar 09, 2011 at 07:59 PM 0
Share

Excellent. Thank you. I was thinking a brute force would be to simply compute distances between mesh vertices of each surface.

avatar image Bunny83 · Mar 09, 2011 at 08:05 PM 0
Share

Well, as i wrote, this will get you the closest point on the AABB (aka bounds in unity)

avatar image StephanK · Mar 09, 2011 at 08:38 PM 0
Share

If you are using mesh colliders you could also do raycasts and check which surface(triangle) you hit at which distance.

avatar image
2

Answer by Bunny83 · Mar 09, 2011 at 07:58 PM

In general there are three cases.

  1. Vertex point vs. vertex point
  2. Vertex point vs. triangle face
  3. 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).

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rule62 · Mar 09, 2011 at 08:29 PM 0
Share

I need something more accurate than bounding spheres/boxes, etc.

avatar image DaveA · Mar 09, 2011 at 10:16 PM 0
Share

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.

avatar image rule62 · Mar 10, 2011 at 02:04 AM 1
Share

http://forum.unity3d.com/threads/34660-Closest-point-on-mesh-collider

avatar image Bunny83 · Mar 10, 2011 at 02:24 AM 0
Share

Nice link, i think the $$anonymous$$DTree comes in handy.

avatar image rule62 · Mar 10, 2011 at 01:20 PM 0
Share

Yeah, I'm curious to try it out and do some trials. :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges