Search Unity

Overlap after depenetration?

Discussion in 'Physics' started by Epiplon, Sep 7, 2018.

  1. Epiplon

    Epiplon

    Joined:
    Jun 17, 2013
    Posts:
    52
    So, I was running a few tests with
    Physics.ComputePenetration
    and
    Overlap
    functions, mainly for making custom collision detection and noticed something.

    Why does Overlap functions return True when colliders are really close but not touching?

    My basic setup was to create a CapsuleCollider and a BoxCollider. The capsule would move against the box and fix the collision. Then, right after that, the object with the box collider would run their Update and check for anything that might be overlapping. This was done setting the order of the scripts in the Script Execution Order settings. Turns out the Capsule was overlapping anyway.

    This is the depenetration Update:
    Code (CSharp):
    1. void Update()
    2.     {
    3.         transform.position += transform.forward * Time.deltaTime * 2f;
    4.  
    5.         var n = PhysicsExtensions.OverlapCapsuleNonAlloc(capsule, colliders, layerMask);
    6.         for (int i = 0; i < n; i++)
    7.         {
    8.             var collider = colliders[i];
    9.  
    10.             if (Physics.ComputePenetration(capsule, transform.position, transform.rotation,
    11.                 collider, collider.transform.position, collider.transform.rotation,
    12.                 out penetrationDir, out penetrationDist))
    13.             {
    14.                 transform.position += penetrationDir * penetrationDist;
    15.             }
    16.         }
    17.     }
    And this is one from the box (named Wall in the demo):
    Code (CSharp):
    1. void Update()
    2.     {
    3.         c_count = PhysicsExtensions.OverlapBoxNonAlloc(boxCollider, overlapColliders, playerLayer,
    4.             QueryTriggerInteraction.Ignore);
    5.     }
    I set them up with different layers in order to avoid any confusion.

    If anyone wants to test it, here is the link:
    https://1drv.ms/u/s!AhuKiONNB5NIh8RJVYAv9FHV14xmTg
     
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    esmith1234 and Epiplon like this.
  3. Epiplon

    Epiplon

    Joined:
    Jun 17, 2013
    Posts:
    52
    Oh, that's right. It makes sense.