Search Unity

  1. Unity 2019.1 beta is now available.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We're looking for insight from anyone who has experience with game testing to help us better Unity. Take our survey here. If chosen to participate you'll be entered into a sweepstake to win an Amazon gift card.
    Dismiss Notice
  4. On February 28th the Feedback website will shut down and be redirected to the Unity forums. See the full post for more information.
    Dismiss Notice
  5. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  6. Unity 2018.3 is now released.
    Dismiss Notice
  7. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Overlap after depenetration?

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

  1. Epiplon

    Epiplon

    Joined:
    Jun 17, 2013
    Posts:
    22
    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:
    1,336
    Epiplon likes this.
  3. Epiplon

    Epiplon

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