Compute Penetration with off-center colliders

I’m trying to use the newly exposed ComputePenetration function in Unity 5.6.0b9. It seems to work as expected when the involved colliders are centered on the game object’s origin, but I’m getting strange results when testing with colliders that are not centered in their object’s origin when the object has a rotation and a scale.

The image below shows one of the situations where Physics.ComputePenetration doesn’t seem to work. The yellow line shows the depenetration direction using the center of the collider and the red line shows the depenetration direction using the transform position of the collider (as shown in the Unity Docs).

Find the whole setup in a Unity Package [89850-computepenetrationtest.zip|89850].

Followed a suggestion in the Unity Forums by @recursive and got good results. The idea is this:

  • Save collider centers
  • Set collider centers to zero
  • ComputePenetration with extracted center positions (they are in local space in the collider)
  • Reset collider centers

This should probably be done internally by Physics.ComputePenetration, but it seems to be a good fix for now. I have tested it with all the 3D collider types and it seems to work, with the exception of the TerrainCollider.

Having problems in 2017.1 f0 with this. Not fixed vs tall level colliders which are offset from y = 0