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.

CollisionFilter not taking effect for CollisionWorld.CalculateDistance

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

  1. edalbeci

    edalbeci

    Joined:
    Jan 21, 2018
    Posts:
    36
    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. edalbeci

    edalbeci

    Joined:
    Jan 21, 2018
    Posts:
    36
    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.