Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Detect rotated cube or rectangle overlap instantly without using OnCollisionEnter

Discussion in 'Scripting' started by robochase, Jun 2, 2014.

  1. robochase

    robochase

    Joined:
    Mar 1, 2014
    Posts:
    244
    Sorry if this should be obvious, but I've looked through the documentation/forum posts quite a bit and there doesn't seem to be an easy way to do this.

    I'm looking for some function like

    bool IsOverlapping(BoxCollider a, BoxCollider b)

    Ideally this magic function will deal with cubes/colliders that are rotated and not axis aligned.

    Yes, I know I can determine this by setting up OnCollisionEnter functions, but that happens to be really inconvenient, as ultimately, I'd be wanting to do something like this in a loop...

    Code (csharp):
    1. // psuedocode
    2. BoxCollider a;
    3. BoxCollider b;
    4.  
    5. for (int i = 0; i < 30; i++)
    6. {
    7.     // move collider a to the next position
    8.     a.transform.position = new Vector3(i*10.0f, 0.0f, 0.0f);
    9.  
    10.     if (IsOverlapping(a, b))
    11.     {
    12.         // do something here.
    13.     }
    14. }
     
  2. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
  3. robochase

    robochase

    Joined:
    Mar 1, 2014
    Posts:
    244
    Bounds is an all encompassing bounding box that doesn't rotate, right? So if you rotate your cube 45 degrees, the bounding box will actually be a bigger volume than the cube, because it's made big enough to encompass the entire rotated cube. Or am I wrong in my understanding here?

    I specifically want to check if 2 rotated cubes are touching.
     
    Siccity likes this.
  4. jc_lvngstn

    jc_lvngstn

    Joined:
    Jul 19, 2006
    Posts:
    1,508
    One way may be to get the position of each corner of the cube (the vertex there). Then for each vertex in cube a, see if that corner point falls in any side of cube b.
    And then vice verce, see if any corner point in cube b falls in any side of cube a.
    Hmm...well actually, that would only work in 2d. :) sorry!
     
  5. jc_lvngstn

    jc_lvngstn

    Joined:
    Jul 19, 2006
    Posts:
    1,508
    Can you just test for poly intersection of the faces?
     
  6. AndreInfante

    AndreInfante

    Joined:
    Oct 26, 2013
    Posts:
    5
    I ended up figuring this out on my own because I couldn't find a good solution and performance of oncollisionenter sucks.

    For anyone curious about this, the simplest way to do it, without getting into scary intersection test math, is just to use the collider.raycast method to check all the edges of each box and see if either crosses into the other.

    So you get each corner of both of your boxes. Then, you take all of the edges in A, and test them as rays against collider B. Then flip around and do the same for B and A. If any of those ray tests returns true, the colliders overlap. If not, they don't. It's 24 ray checks per object pair, which is probably slower than the optimal intersection math, but faster than oncollisionenter in a large world.
     
  7. robochase

    robochase

    Joined:
    Mar 1, 2014
    Posts:
    244
    Hey! A long time has passed since my original post.
    Last year I revisited the problem and I realized that there is now an easy solution to solve this -

    https://docs.unity3d.com/ScriptReference/Physics.CheckBox.html

    Of course it checks against all colliders, but if you can use layer masks to narrow it down quite a bit.