Search Unity

Physics.ComputePenetration doesn't work properly with Concave Mesh Colliders

Discussion in 'Physics' started by Awarets, Aug 29, 2019.

  1. Awarets

    Awarets

    Joined:
    Jan 5, 2019
    Posts:
    1
    Instead of slightly shifting the collider horizontally like it should do logically, it instead moves it significantly further upward. (Which in practice causes the character to violently jerk upward)

    Physics.ComputePenetration is supposed to "compute the minimal translation required to separate the given colliders apart", so why is this occurring?

    Visualization:

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class PenetrationCalculation : MonoBehaviour
    7. {
    8.     public Collider colliderToTestAgainst;
    9.  
    10.     private Vector3 depenetrationVisualizationStart;
    11.     private Vector3 depenetrationVisualizationEnd;
    12.  
    13.     private void Start()
    14.     {
    15.         TestPenetration();
    16.     }
    17.  
    18.     private void LateUpdate()
    19.     {
    20.         Debug.DrawLine(
    21.             depenetrationVisualizationStart,
    22.             depenetrationVisualizationEnd,
    23.             Color.red);
    24.     }
    25.  
    26.     void TestPenetration()
    27.     {
    28.         Physics.ComputePenetration(
    29.             GetComponent<Collider>(),
    30.             transform.position,
    31.             transform.rotation,
    32.             colliderToTestAgainst,
    33.             colliderToTestAgainst.transform.position,
    34.             colliderToTestAgainst.transform.rotation,
    35.             out Vector3 depenetrationDirection,
    36.             out float depenetrationDistance);
    37.  
    38.         depenetrationVisualizationStart = transform.position;
    39.         depenetrationVisualizationEnd = transform.position + (depenetrationDirection * depenetrationDistance);
    40.         print("Direction: " + depenetrationDirection.ToString("00.00000") + "\nDistance: " + depenetrationDistance);      
    41.     }
    42. }
    43.  
    Project Folder (Using version 2019.2.3f1, the latest version as of posting)
     
  2. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    89
    I'm surprised it even works with concave colliders. Try with convex, and stay with convex, concave is good only for static geometry.