Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Clonkex · Nov 03, 2015 at 02:50 AM · physicscollision detectiontriggersboundingbox

Fastest way to check for bounding box partially or fully inside another bounding box?

So I've got tonnes of objects (let's say 200 - not actually sure how many there will be on average). I also have tonnes of bounding boxes (likely also 200). I need to know if one or more of those objects has partially or fully entered one of the bounding boxes. This probably means testing each of the 200 object against each of the bounding boxes (40,000 tests). That's quite a lot. (Incidentally, it may never reach that amount of objects, but there will definitely be that many bounding boxes, possibly more, so I feel like 200x200 is a good approximation).

So here's my question: What would be the fastest way to find which of the 200 objects are inside (even partially) which of the 200 bounding boxes?

And: If the best solution does indeed involve 40,000 tests, what would be the easiest and/or best way to split the tests over multiple frames?

Additional Information:

The objects will have various collision shapes attached and be rigidbodies. (Just ignore the fact that I'm running 200 rigidbodies - I have ways to deal with the performance in that respect) I can add box colliders or triggers or whatever is needed to both the objects and the bounding boxes. The bounding boxes themselves are actually parent GameObjects with various meshes and physics stuff as children.

Comment
Add comment · Show 2
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 MadDevil · Nov 03, 2015 at 04:38 AM 0
Share

I think the best way would be to add script to single object, which checks for itself for the bounding conditions.

avatar image Clonkex MadDevil · Nov 03, 2015 at 05:59 AM 0
Share

Yeah that's what I'm thinking. Right now I'm going to just add trigger colliders to every object and give them their own scripts and see how that goes.

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by JoshuaMcKenzie · Nov 03, 2015 at 07:49 AM

I believe you should look at this problem differently. Don't try to test if each of the 200 rigid bodies is inside any of the 200 boundaries. Its better to use the system Unity already has for you.

So You have two types of objects, never mind how many there are of each, just that there are two types. And one type will overlap with the other. I have no idea what you have specifically called them but I will simply label these two different groups as "Zones" and "Residents" where a resident can either partially or fully enter a zone. I would use this labels as the name of their collision layer.

The residents will have the Rigid bodies with an attached Box Collider (or what ever shape you want), to represent its bounding volume. The Zones will likewise also have a Collider (with the most valid shape) but set as a Trigger.

Then go to your edit->project settings-> Physics and make sure that Zones and Residents only interact with each other and nothing else, this is mostly an optimization but it also helps in preventing any unexpected triggering events. its here that you'll also make sure that Zones can't interact with other zones and the same with Residents and other residents

Finally make a monobehavior script for the zone that uses an OnTriggerEnter, OnTriggerStay, and OnTriggerExit to detect when a resident enters, stays in, or leaves a zone.

also if you want to know if a resident is fully inside a zone you can use this function

 bool FullyContains(Collider resident)
     {
         Collider zone = GetComponent<Collider>();
         if(zone == null)
         {
             return false;
         }

         return zone.bounds.Contains(resident.bounds.max) && zone.bounds.Contains(resident.bounds.min);
     }

Now you are no longer constantly checking every frame if 200 objects are messing with a separate groups of another 200 objects, your code is now event driven and will only mess with the objects that it needs to mess with at that specific time.

if you're curious about Colliders being used as Triggers you can refer to this tutorial Colliders as Triggers

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 Clonkex · Nov 03, 2015 at 08:57 AM 0
Share

Excellent answer, thank you! :D This really helps cement the idea in my $$anonymous$$d :)

avatar image Clonkex · Nov 03, 2015 at 09:09 AM 0
Share

Incidentally "Zones" and "Residents" were well-chosen. Oh and I just noticed the bonus bit about checking whether the Residents are fully inside a Zone... and that's brilliant! At some point in the future I may need to do a check like that and I'd been wondering how I was going to do it :D

avatar image Clonkex · Nov 03, 2015 at 09:35 AM 0
Share

Also, since both the Zones and the Residents may be spread out by quite a long way (in the order of tens or hundreds of thousands of units), I guess the physics engine will optimise the collision testing quite a bit in its broadphase, assu$$anonymous$$g the physics engine is actually responsible for the trigger collision testing.

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

38 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Problem with TriggerEnter/Exit Execution Order 0 Answers

How do you avoid collisions when teleporting CharacterControllers? 0 Answers

Did triggers inside loaded gameobjects break in a recent update? 0 Answers

Clarification on updates, physics events order and frequency? 1 Answer

Collision Detection Randomly Ignores Hits 2 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