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

ComputePenetration, what am I doing wrong?

Discussion in 'Physics' started by YondernautsGames, Jul 27, 2018.

  1. YondernautsGames

    YondernautsGames

    Joined:
    Nov 24, 2014
    Posts:
    352
    Hi all.

    I'm having an issue, that PhysicsCompute penetration always seems to return false, and I can't see why. I'm getting colliders with an overlap test and immediately checking them. Here's the code:

    Code (CSharp):
    1.  
    2. Vector3 GetDepenetrationVector()
    3. {
    4.     Collider[] hits = new Collider[8];
    5.     Vector3 position = transform.position + characterController.center;
    6.     Vector3 offset = new Vector3(0f, (characterController.height * 0.5f) - characterController.radius, 0f);
    7.     int contacts = Physics.OverlapCapsuleNonAlloc(
    8.         position - offset, position + offset, characterController.radius, hits
    9.     );
    10.     if (contacts == 0)
    11.         return Vector3.zero;
    12.  
    13.     // Check hit
    14.     Vector3 result = Vector3.zero;
    15.     for (int i = 0; i < contacts; ++i)
    16.     {
    17.         // Calculate overlap
    18.         Collider c = hits[i];
    19.         Vector3 overlap;
    20.         float distance;
    21.         bool overlapped = Physics.ComputePenetration(
    22.             characterController, transform.position, Quaternion.identity,
    23.             c, c.transform.position, c.transform.rotation,
    24.             out overlap, out distance
    25.         );
    26.  
    27.         Debug.Log("Checking against " + c.name + ", overlapped: " + overlapped);
    28.  
    29.         if (overlapped)
    30.             result += overlap * distance;
    31.     }
    32.  
    33.     // Return result
    34.     return result;
    35. }
    36.  
    I am using a CharacterController as the first collider (on the object this behaviour is attached to).

    I've tried using position, position + center, etc. I've also tried casting the other collider to a box in a known test situation and using position + center for that as well. I've tried transform.rotation for the character collider, but assume that's not necessary.

    Also, I should point out that in the scenario I use it for, the other colliders are always a simple collider (capsule, box or sphere) and always has a rigidbody.

    No matter what I do, the debug log always says that ComputePenetration returned false.

    Any help would be appreciated.
     
  2. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    I would suggest doing a bug report as it's not certain there are no bugs with this.
     
  3. PandaArcade

    PandaArcade

    Joined:
    Jan 2, 2017
    Posts:
    128
    @Survivalist-Games did you ever submit a bug report for this? I'm having similar problems with ComputePenetration intermittently incorrectly returning false :(
     
  4. YondernautsGames

    YondernautsGames

    Joined:
    Nov 24, 2014
    Posts:
    352
    I didn't actually. I got distracted with other tasks.

    I can do that before the end of the week, unless you have a test case you can set do that with yourself?

    Edit: Also I'm not using anything near the latest version so it might be better doing a bug report with that if it's still a problem. I'm developing an asset, so I wanted to find a workaround that works in the version I'm using as that's the minimum version I plan to support
     
  5. PandaArcade

    PandaArcade

    Joined:
    Jan 2, 2017
    Posts:
    128
    SparrowGS likes this.
  6. YondernautsGames

    YondernautsGames

    Joined:
    Nov 24, 2014
    Posts:
    352
    You are very correct there, however I have only just got back to the coding and testing side of things now after a harrowing time contracting, writing docs and sorting websites. Anyhow, I've built my repro case and tested it in different versions and it seems to be fixed in 2018.3. It was quite different to your problem. In my case it was weirdness with the character controller itself so I've found a workaround for earlier versions.
     
  7. PandaArcade

    PandaArcade

    Joined:
    Jan 2, 2017
    Posts:
    128
    Happy to hear your problem has been fixed! :D