Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Is there a ComputePenetration equivalent?

Discussion in 'Physics for ECS' started by hippocoder, Nov 11, 2019.

  1. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,725
    Prepping to port to UP and want volumetric data that this function so conveniently provides with physx.
     
  2. Rory_Havok

    Rory_Havok

    Joined:
    Jun 25, 2018
    Posts:
    70
    ICollidable.CalculateDistance() returns negative distance in the case of penetration, with an appropriate separation normal. You can call that on a collider, body or world. I think that should be equivalent?
     
    hippocoder and PhilSA like this.
  3. PhilSA

    PhilSA

    Joined:
    Jul 11, 2013
    Posts:
    1,926
    out of curiosity, do ColliderCasts do the same if the shape of the cast is already intersecting something else at its Start pose? Does it return the real normal of the "most penetrating point" as well as the accurate negative fraction with which we could calculate the penetration distance? And what does it do with the hit.Position in that case?
     
    Last edited: Nov 11, 2019
  4. MaxAbernethy

    MaxAbernethy

    Joined:
    Mar 16, 2019
    Posts:
    53
    Hi PhilSA, ColliderCasts with an initial intersection will return a fraction of zero and the same position and normal as if you had done CalculateDistance at the ray start position. The penetration distance is not reported, which is unfortunate because the penetration distance can be very expensive to calculate, and if you call CalculateDistance it would have to be calculated a second time.

    Here is a way to quickly calculate the penetration distance given the information returned from the query. The normal gives you the separating direction, and the position gives you the support point on the hit shape in that direction, so you only need to find the support point on the query shape in the opposite direction. GetSupportingVertex() in ConvexConvexDistance.cs shows how to do that, you just search for the vertex that has the highest dot product with the negative of the normal. Subtract the support point on the hit shape from the support point on the query shape, dot with the hit normal, and subtract the query shape's ConvexRadius to get the distance. It should be a negative number if the shapes are intersecting.

    Hope that's helpful!
     
    steveeHavok, hippocoder, e199 and 2 others like this.
  5. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,725
    Very nice thank you all :)