Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Check if area within bounds is completely occupied..

Discussion in 'Scripting' started by sygaki786, Mar 2, 2015.

  1. sygaki786

    sygaki786

    Joined:
    Jan 26, 2014
    Posts:
    142
    How do I check if the area within certain bounds is completely occupied with 3d gameobjects such that there is no space between them?

    Thanks
    Syed
     
  2. lineupthesky

    lineupthesky

    Joined:
    Jan 31, 2015
    Posts:
    92
    This actually depends on the "bounds" you are talking about. If you can give more information, we'd be more helpful. But I think you need this to do some check like " if any more of this object can fit over there, can I spawn more ? " or something like that, if that's the case, there are couple of ways to do that :

    Considering that none of the objects get through each other,

    *Manually or Semi-Manually :
    • - Determining how many game objects will fit into an area. First you need try how much can that "area" of yours take, and then determine a number. Use that number for your checks.
    • - If those bounds may differ from level to level, or place to place, then you need a fraction or let's say multiplier. Again, just try to keep that area's scale within fully numbers, I mean integers, and then try how many gameobject fits there and create a ratio over that & use that ratio to determine the maximum object count that the others bounds will use.

    *Use some little math, create some virtual volume variables :
    • - If that area of yours can take objects in X-Y-Z axes, you can think it as a sphere or a cube. I'll go with the sphere one, the total volume value of a sphere is 4/3 * PI * (r*r*r) , so you already know that area's radius right ? If so, do this calculation over your code ( Unity has a variable called Mathf.PI ) and there you'll find your maximum volume / capacity. Then before putting any more gameobjects in that are, you can check if it'll fit, by first calculating the volume of the object which tries to get into the area, and than decreasing it from total volume. After the first object, you shall always check if the new object's volume is smaller than the capacity.

    Hope I was able to help, if I knew your situation better, I would come up with better ideas.
     
    sygaki786 likes this.
  3. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    That is almost certainly not what you want to test, because for most practical examples the answer will always be "false" (unless the objects are allowed to overlap).

    You're dealing with floating-point numbers, so the empty space between two objects will pretty much never be exactly zero.

    Even ignoring precision issues, most objects can't pack perfectly into a 3D space. For example, if your objects are spheres (and your area is larger than a single sphere), then even if they're touching at one point, they'll both curve away from that point of contact, creating empty space around the edges.

    In principle, you could exactly fill an area with axis-aligned box colliders, but even then it would be really hard.

    You probably want to test something else, like "are there more than 5 objects in this area?" or "is more than 75% of the space in this area occupied?" or "is there enough empty space in this area for me to create another widget here?"
     
    sygaki786 likes this.
  4. sygaki786

    sygaki786

    Joined:
    Jan 26, 2014
    Posts:
    142
    Thanks for the help folks! I have enough to get started.