Search Unity

Determine if a collidercast hit a triangle backface?

Discussion in 'Physics for ECS' started by PhilSA, Feb 21, 2020.

  1. PhilSA

    PhilSA

    Joined:
    Jul 11, 2013
    Posts:
    1,926
    Is it possible to detect if a ColliderCastHit represents a hit on a triangle backface? Or to make queries ignore backfaces?

    I think I'd need that feature for a ColliderCast-based collision detection algorithm. In this picture, the yellow lines represent all hit normals that were detected by ColliderCasts this frame, as a character is sliding along the red wall towards the image's left. As you can see, one of the hits is from the backface of the wall section that faces the left, and the detection of that hit throws off my character's movement

     
  2. MaxAbernethy

    MaxAbernethy

    Joined:
    Mar 16, 2019
    Posts:
    53
    Hello, I think you need to:
    1) get the shape that was hit using ColliderCastHit.RigidBodyIndex and ColliderCastHit.ColliderKey and check if it's a triangle
    2) calculate its front face normal using whatever winding you prefer
    3) dot it with ColliderCastHit.SurfaceNormal, if the dot is negative then throw out the hit.

    To make the query ignore these completely, eg. if you're doing a closest hit cast and you need it to ignore back face hits so that it can give you the closest front face hit, then I think you would need to implement an ICollector and do this stuff in AddHit().
     
    pal_trefall and PhilSA like this.
  3. PhilSA

    PhilSA

    Joined:
    Jul 11, 2013
    Posts:
    1,926
    Awesome, I had no idea CollliderKey could be used to get the triangle. Seems like a good solution!

    What exactly does ColliderKey represent? A unique identifier for every triangle of a collider? (And all hits on primitives have the same colliderkey?)
     
  4. MaxAbernethy

    MaxAbernethy

    Joined:
    Mar 16, 2019
    Posts:
    53
    ColliderKey uniquely identifies a leaf ConvexCollider in an ICompositeCollider, of which there are MeshCollider, CompoundCollider, and TerrainCollider. For a MeshCollider or TerrainCollider, each triangle has its own ColliderKey. For a CompoundCollider containing, say, a BoxCollider and a terrain and a mesh, there would be one ColliderKey for the box and one for each triangle in the terrain and mesh. ICompositeCollider::GetLeaf() fetches the leaf collider for you.

    If you cast hits a body whose root Collider is not an ICompositeCollider, then no ColliderKey is needed, so it's set to ColliderKey.Empty.
     
    florianhanke and PhilSA like this.
  5. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    481
    Worth mentioning that we have a Collector refractor in the works that will give you the collider key and the body index in the Collector.AddHit function to make this sort of filtering much much easier.
     
    florianhanke and PhilSA like this.