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 Dath · Jun 27, 2015 at 03:31 AM · c#boundsoverlappingintersect

Problem with detecting if instantiated objects are overlapping

Alright, so the title basically sums up the question. I am working with instantiating a bunch of objects along a path which is literally just a stored list of Vector3 positions in the world, which works fine and it comes out looking something like this.

alt text

My issue is that the instantiated objects can be of variable size and shape (and even possibly variable rotation). So I am trying to run checks against the objects to see if they are overlapping and then remove the objects they are overlapping with so that is can look something more like this, so that is looks like a fluid path of objects one right after another close together but not touching (but without the minor overlapping of course. I simply manually placed those):

alt text

Here is my code for checking for doubles (Note: That each instantiated object is added the the list "_objs" before this function is called)

 void RemoveDoubles(){
         for (int j = 0; j < _objs.Count; j++) {
             int k = 0;
             foreach (GameObject obj in _objs) {
                 if (_objs [j] == obj) { 
                     break; 
                 }
                 bool overlapping;
                 Collider col1 = _objs [j].GetComponent<Collider> ();
                 Collider col2 = obj.GetComponent<Collider> ();
                 overlapping = col1.bounds.Intersects (col2.bounds);
                 if (overlapping) {
                     _KillList.Add (obj);
                     Debug.Log("Object " + j + " is overlapping with Object " + k);
                 }
                 k++;
             }
 
             foreach (GameObject obj in _KillList) {
                 _objs.Remove (obj);
                 Destroy (obj);
             }
         }
 
         _objs.Clear ();
         _KillList.Clear ();
     }

The idea is that I check the first object in the list against every other instantiated object in said list. I then add the overlapping objects to another list to be removed from the list and destroyed in the world. However, it seems I am doing something wrong as this is not working as intended and it may just be the heat where I am at but my brain is broken and this one thoroughly has me stumped at the moment.

Also I was wondering If I am simply over complicating this and if anyone has any ideas of an easier, more straightforward way of checking / achieving this.

2015-06-26-1800.png (159.9 kB)
2015-06-26-1803.png (404.8 kB)
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 AlwaysSunny · Jun 27, 2015 at 03:31 AM

Why not address the problem during the placement stage? Doing it after the fact might be a useful skill in other scenarios, but it is entirely wasteful here. Just place the objects so they never overlap in the first place.

  1. Decide the rotation and scale of the next object.

  2. Look at the next position in the position list.

  3. Use Physics.OverlapSphere to see if the space is occupied.

  4. If so, record that position as the last examined, and goto step 2.

  5. When you find a position that won't overlap, place the object.

  6. Record the index of the position used; the following position will be the next examined by step 2.

  7. WaitForFixedUpdate

  8. Go to step 1.

Assuming you want more control, and that all these objects can be represented by boxes, you could use a box collider instead of CheckSphere.

Regardless of the approach, you will probably need to perform this algorithm over N frames in a coroutine with a WaitForFixedUpdate (step 7) because new colliders will not necessarily register their existence until the next frame. If your positions are cleverly spaced according to the average size of these objects, it shouldn't be too big of a problem.

If you don't like the idea that this algorithm executes over time, consider that the alternative is a rather substantial "hitch" or period of nonresponsiveness, which is a much more egregious faux pas in the minds of most users, not to mention spawning dozens or hundreds of unnecessary objects.

If you want "nice" placement on a spline, including control over the rotation along the spline, there are approaches for that that will produce much more aesthetically pleasing results. This involves using a true spline implementation, as opposed to a list of positions. It would also make this placement stage quite trivial, assuming you could make some decisions about average scale, and that the spline is precalculated. These are fairly well documented general spline topics.

If you really want to stick with your approach of placing first, solving for overlap second; the general shape of your approach makes sense, but I didn't proofread it to spot flaws or errors. Your written English description of the solution is sensible enough. Start with the first object and walk out until you find one that doesn't overlap, remove those in between, then continue from that first non-overlapping object. This work could / should also be spaced out over several frames.

Regardless of your selected approach, the most important optimization involves ensuring you are performing as few checks as possible, e.g. by ensuring your spline positions are cleverly spaced. Also, you could skip positions in your list (step 2) until the distance between the last placed object and the next placement test is roughly twice the average radius of your objects.

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Checking to see if BoxCollider intersects with another collider doesn't work. 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

I need help with overlapping sprites and teleporting 0 Answers

Illuminating a 3D object's edges OnMouseOver (script in c#)? 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