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

OverlapBoxNonAlloc vs OverlapBox, most probably a bug

Discussion in 'Physics' started by BrightBit, Mar 5, 2018.

  1. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    264
    I'm getting different results when using Physics.OverlapBoxNonAlloc in comparison to Physics.OverlapBox. OverlapBoxNonAlloc seems to not find colliders every now and then. However, OverlapBox seems to work just fine. I would like to report it as a bug but unfortunately I wasn't able to create a simple scenario where I can reproduce the malfunction. The current project uses SteamVR, requires motion controllers and might be a bit too complicated.

    The file "Hand.cs" in SteamVR originally uses "OverlapBoxNonAlloc" in its "UpdateHovering" method. This method detects if one of my touch controllers hovers over "Interactables", in my case a door knob. I've added some code to this method to change between OverlapBoxNonAlloc and OverlapBox during runtime. Everytime OverlapBoxNonAlloc can't detect the door knob switching to OverlapBox makes the method detect the knob.

    Here's an excerpt:

    Code (CSharp):
    1. private void UpdateHovering()
    2. {
    3.     // some code
    4.  
    5.     // container for results of OverlapBoxNonAlloc
    6.     // needs to be cleared first
    7.     for ( int i = 0; i < fixed.Length; ++i ) fixed[i] = null;
    8.  
    9.     // container for results of OverlapBox
    10.     Collider[] normal = null;
    11.  
    12.     // enables changes between OverlapBoxNonAlloc and OverlapBox at runtime
    13.     if (toggle) normal = Physics.OverlapBox(pos, size,        rotation, -1);
    14.     else         Physics.OverlapBoxNonAlloc(pos, size, fixed, rotation, -1);
    15.  
    16.     foreach (Collider collider in (toggle ? normal : fixed))
    17.     {
    18.         if (collider == null) continue;
    19.  
    20.         Interactable contacting = collider.GetComponentInParent<Interactable>();
    21.  
    22.         // even more code
    23.     }
    24. }
    My system specs:

    Unity Version 2017.3.1f1 (Version 2017.2 also has this issue)
    Oculus Rift Consumer Edition with Motion Controllers
    Windows 10 (64 bit)
    NVIDIA GeForce GTX 980 Ti
    i7-5820K CPU @ 3.3 GHz
    16 GByte Ram
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    Not sure why OverlapBoxNonAlloc doesn't work as intended. But, you might want to use physics layers and put your interactables in their own layer, so that you can use the layer mask to exclude everything else. Also, OverlapBoxNonAlloc returns the number of overlaps, so you don't have to clear fixed[], just get the return value from OverlapBoxNonAlloc and loop over the number of hits.
     
  3. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    264
    Thank you for pointing that out. It's the original code of the SteamVR framework and fixing it didn't help but it's nevertheless good to know.

    I will add some animations to illustrate the different behaviour of those two methods. The red box is the area used by OverlapBox and OverlapBoxNonAlloc to detect colliders touching it. You can see if the methods detect colliders by watching the outline of the black controller. Whenever there's a yellow outline the corresponding method detects the white door knob.

    OverlapBox:



    OverlapBoxNonAlloc:



    As you can see the yellow outline disappears at certain spots while using OverlapBoxNonAlloc.
     
  4. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    264
    I've submitted a bug report now. I will keep you posted.
     
    Meiramz and jj_unity328 like this.
  5. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    264
    Sorry for the delay and sorry for reopening an old thread but some people are still reacting to it.

    Back then, a QA Tester called Giedrius tried to help me find the root cause and even created a simpler test case. However, he was not able to reproduce the issue. To his credit: I also was not able to reproduce the issue all the time. So there are some weird things going on. Unfortunately, the corresponding ticket got eventually closed at some point and I did not react to it as I already moved on to different projects.

    If you still encounter this bug, please open a new bug report.
     
    sirleto likes this.