Search Unity

Feedback [Feature Request] Test a 3D collider against a scene

Discussion in 'Physics' started by cwhelmi, Jun 4, 2021.

  1. cwhelmi

    cwhelmi

    Joined:
    May 12, 2013
    Posts:
    6
    Hi,

    I've seen this topic mentioned in a few old threads, but in the interest of not necro-posting I thought I'd start a new one. After looking through the API for a while I was unable to find a reasonable solution, so hopefully this isn't something I'm just overlooking.

    The problem:
    I'm developing a physics-based first person game. The player can pick up various items and drop them again. When the player picks up an item the rigidbody is set to kinematic and collisions are disabled so they can walk around without hitting stuff. Simple enough so far. But, when the player drops the item I would like to check if it would be intersecting anything first. This is to prevent the rigidbody from ending up clipped inside something and then flinging off into oblivion. Should be easy enough to check, right?

    Unfortunately there does not appear to be a way to do this with the current API. There are methods for checking individual primitives like Physics.CheckBox or Physics.OverlapCapsule, but there is no corresponding method for checking a mesh collider. (Even a convex one!) Moreover, these are static methods which means to test for some arbitrary collider (items could be any shape) you first must determine which collider component is attached, then for each case feed all the parameters of that collider into one of these functions. Ergonomically this is terrible. And again, this doesn't work for mesh colliders.

    Another potential method I saw mentioned was Rigidbody.SweepTest, but as far as I can tell it doesn't find a hit if the colliders are overlapping, much like the other sweep tests.

    Suggestions:
    1. Add methods to BoxCollider, etc. that correspond to the existing static checks in Physics. Something like IsTouching, Overlap, OverlapNonAlloc. This would be a nice improvement to ergonomics and doesn't have to be tied to anything else.
    2. Implement similar methods to the above that allow for testing a given mesh collider. Even if it were limited to convex colliders it would be a huge improvement.
    3. Do the above but make them abstract methods that can be called for an arbitrary Collider regardless of the specific type. The 2D physics API already does this with Collider2D.IsTouchingLayers and Collider2D.OverlapCollider. I don't think I need to explain why this sort of polymorphism is super useful.
    I would love to hear people's thoughts on all of this. If there's anything I overlooked please let me know. I would also be very interested in hearing if there is some technical reason why these things are not already implemented.

    Cheers!
     
    tjmaul and Edy like this.
  2. tjmaul

    tjmaul

    Joined:
    Aug 29, 2018
    Posts:
    307
    I have a similar problem and right now I’m solving it by moving a copy of the item some distance away and then I do a Rigidbody SweepTest to see where it intersects for the first time. This also gave me some headaches and I’m also only using this as a workaround. Especially with some additional layers for other purposes, this really kills easy-to-read code, so I feel your pain.

    something that I’ll look into next is ComputePenetration. If you want to use this against a scene, you have to do something like a boxOverlap first to get all the potentially involved colliders and then use that information with compute penentration. Admittedly still not super elegant but maybe at least more readable, logical and predictable. I hope this helps :)
     
    Last edited: Jun 10, 2021
    cwhelmi likes this.
  3. cwhelmi

    cwhelmi

    Joined:
    May 12, 2013
    Posts:
    6
    Thanks for the suggestion on ComputePenetration. I was aware of the function but I didn't think to use it for binary collision detection. I suppose Collider.bounds -> Physics.OverlapBox -> Physics.ComputePenetration would be the most straightforward way to do it. Seems extremely wasteful but whatever, I guess I'll try it and see how bad the performance impact is.

    I definitely don't like the Rigidbody SweepTest because moving the object to perform the test seems like it would lead to all sorts of buggy corner cases. Definitely a problem with no clean solution which should be trivial if we had the API for it.
     
    tjmaul likes this.
unityunity