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
0
Question by developer_arni · Dec 21, 2014 at 12:50 PM · mesh

Closest collision point

I am trying to find the nearest collision point on any mesh to a given point in space. I.e. say I have a sphere located at (x1,y1,z1) with radius 0, and I would expand its radius until the first collision occurs.

I need to find the closest collision point on a mesh, such that I can snap to it.

How would I go about it? I could expand a sphere until it hits something, but that is highly expensive if objects are located far away and potentially very inaccurate if many objects are locating very close to each other.

Another approach could be to iterate through all meshes, that are eligible, and find the closest collision point on any triangle and pick the closest one, as described in this post http://answers.unity3d.com/questions/424974/nearest-point-on-mesh.html. Although that just does sound immensely expensive if I have an great number of models with a huge number of vertices, although it would prove perfect accuracy.

Any tips and help is highly appreciated!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Mmmpies · Dec 21, 2014 at 12:58 PM

Depends on the number of game objects you want to check for, if a not that many you could cycle through a list of each object and check the distance.

See Vector3.Distance

This is similar to expanding the collider but if you had 100 objects and the closest was 200 units away then it'd be faster checking which one's closest than expanding the collider till it hits something.

You can use it to find the closest object and act as if triggered.

EDIT:

Sorry for the misunderstanding and for going out immediately after answering! That link appears to be either wrong or down.

EDIT 2:

OK got it. looks like a trailing full stop nearest-point-on-mesh

Hmmm looks complex might have to think about this.

EDIT 3:

OK how's this, increase your collider, let's say 1 unit (1 meter) per frame (store in float IncUnit) and check for collisions. That increase can be anything you like as long as none of your GameObects can slip in between the size you're increasing.

When the collider is triggered set a bool to say HaveInitialCollison = true;

In Update if that bool is true call a function to do the following.

Store these values:

bool BackUp = true //if still colliding

int AmountToDivideBy 2; // Double this with each step

float AmountToMove = IncUnit / AmountToDivideBy; // to check how far to resize the collider.

If BackUp = True then reduce your collider by half it's size and check if a collison is still happening.

If no longer colliding set BackUp to false but increase the collider by AmountToMove instead of reducing. Each time do this:

AmountToDivideBy = AmountToDivideBy * 2;

Keep doing this until these are met:

A: there is a collision still happening

B: are happy that the increments are so small you must be at the closest point.

C: there's only I GameObject being collided with. You'll need some logic to handle this past your maximum AmountToDivideBy.

At this point do whatever check you want from that other page.

It's not ideal but It'll vastly reduce the number of operations being performed on just increasing the collider a fraction from 0 to the first collision.

Basically just jumps forward rapidly then, when collision happens, halves the distance until you are happy that it's close enough and the only collision happening.

Comment
Add comment · Show 1 · 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 developer_arni · Dec 21, 2014 at 01:05 PM 0
Share

Thanks for your answer. This would return which object is closest, not, what would have been, the actual collision if I expanded a sphere, correct? I will try and make it more clear in my question, that I need the closest point of impact.

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

27 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Mesh creation by code not working? 0 Answers

Camera view changing size of mesh 3 Answers

How to assign different colorsets to objects that are using the same mesh? 0 Answers

Procedural cube generation 1 Answer

Edit Mesh Edge to Match Overlapping Object or Obstruction 0 Answers


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