Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

CollisionFilter not taking effect for CollisionWorld.CalculateDistance

Discussion in 'Physics for ECS' started by alvinwan, Jan 15, 2020.

  1. alvinwan

    alvinwan

    Joined:
    Jan 21, 2018
    Posts:
    34
    tl;dr collision filter does not seem to take effect.

    I've setup a minimal Unity project with just 1 cube and 1 script:

    1. Cube is in layer 8. (Fig 1)
    2. I have one `TestComponentSystem.cs` that calculates distance to the entity closest to the cube. Despite a CollisionFilter that says to ignore the cube's layer 8, the closest hit is always the cube itself. I'd expect the calculate distance function to return no entities. (full file source in Fig 2)

    Question: Am I misusing the CollisionFilter? Is my group index set incorrectly?

    Fig 1: Cube has standard cube GO components + physics body, physics shape, convert to entity.


    Fig 2: source code for `TestComponentSystem`.

    Code (CSharp):
    1.  
    2. using Unity.Entities;
    3. using Unity.NetCode;
    4. using Unity.Mathematics;
    5. using Unity.Physics;
    6. using Unity.Transforms;
    7. using Unity.Collections;
    8. using Unity.Physics.Systems;
    9. using Unity.Physics.Extensions;
    10.  
    11. public class TestComponentSystem : ComponentSystem
    12. {
    13.     BuildPhysicsWorld physicsWorldSystem;
    14.  
    15.     protected override void OnCreate()
    16.     {
    17.         physicsWorldSystem = World.GetOrCreateSystem<BuildPhysicsWorld>();
    18.         base.OnCreate();
    19.     }
    20.  
    21.     protected void FindClosestTarget()
    22.     {
    23.         Entities.ForEach((Entity entity, ref Translation trans, ref PhysicsCollider col) =>
    24.         {
    25.             CollisionWorld collisionWorld = physicsWorldSystem.PhysicsWorld.CollisionWorld;
    26.  
    27.             DistanceHit closestHit;
    28.  
    29.  
    30.             CollisionFilter filter = new CollisionFilter
    31.             {
    32.                 BelongsTo = ~(1u << 8),
    33.                 CollidesWith = ~(1u << 8),
    34.                 GroupIndex = 0
    35.             };
    36.  
    37.             PointDistanceInput input = new PointDistanceInput
    38.             {
    39.                 Position = trans.Value,
    40.                 MaxDistance = 10,
    41.                 Filter = filter
    42.             };
    43.  
    44.             if (collisionWorld.CalculateDistance(input, out closestHit))
    45.             {
    46.                 Entity other = collisionWorld.Bodies[closestHit.RigidBodyIndex].Entity;
    47.                 UnityEngine.Debug.Log(other);
    48.             }
    49.         });
    50.     }
    51.  
    52.     protected override void OnUpdate()
    53.     {
    54.         FindClosestTarget();
    55.     }
    56. }

     
    Last edited: Jan 15, 2020
  2. alvinwan

    alvinwan

    Joined:
    Jan 21, 2018
    Posts:
    34
    This is resolved now. I got confused by the overloaded "layers" name. The "layers" that the documentation refers to is not the "traditional" Unity layer. In the physics shape component UI, the "Collision Filter" drop down has a BelongsTo field that contains physics-specific layers.


    As for understanding group index, the code sample in that section explains more concisely: https://docs.unity3d.com/Packages/com.unity.physics@0.2/manual/collision_queries.html#filtering
     
    steveeHavok likes this.