ComputePenetration does not detect overlapping colliders but OverlapBox does

Hi,

I must be failing to comprehend the inputs to ComputePenetration because I rarely see it return true when OverlapBox does. I have an object with a box collider and I want to detect collisions against mesh colliders (not necessarily convex). So I do something like this:

BoxCollider box = GetComponent<BoxCollider>();
Vector3 halfExtents = 0.5f * Footprint.Measure(gameObject);
Collider[] overlapping = Physics.OverlapBox(transform.TransformPoint(box.center), halfExtents, transform.rotation, PlayspaceManager.spatialLayerMask);

overlapping[ ] is populated with the mesh colliders I expect. But then when I iterate over each of them and do this: Footprint.Measure() is a custom function that measures the size of a box collider in world (not local!) units.

foreach (Collider c in overlapping)
{
  Vector3 direction;
  float distance;
  bool result = Physics.ComputePenetration(box, transform.position, transform.rotation, c, 
    c.transform.position, c.transform.rotation, out direction, out distance);
  //...
}

“result” is false almost all the time. How exactly is this functioning posing the colliders to perform the penetration test? Shouldn’t it be perfectly consistent with Physics.OverlapBox?

Thank you,

Bart

What I see here is that you’re calling OverlapBox and ComputePenetration with different parameters. OverlapBox uses halfExtents, while ComputePenetration uses the box extents. Thus ComputePenetration may be called with objects that are not necessarily in contact.

At first sight, you might get coherent result by replacing “halfExtents” with “box.size”, but I’m not entirely sure. Anyways it looks like some calculation mistake in your side.

1 Like

Physics.OverlapBox requires you to give it the halfExtents and it will calculate the full size itself, so he is correct in giving it the halfExtent (though I dont know if hes calculating the halfExtents correctly since he didnt show the code).

Maybe your box gameobject is scaled and its causing weird results?
Im not at a computer so I cant test if I get the issue on my side.

Right. What I meant is that he is passing the variable halfExtents (a custom-calculated value) to OverlapBox, while ComputePenetration uses the box collider itself. Most probably the issue has to do with the way halfExtents is calculated.

1 Like