Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Batching queries

Discussion in 'Physics for ECS' started by Tony_Max, Nov 5, 2019.

  1. Tony_Max

    Tony_Max

    Joined:
    Feb 7, 2017
    Posts:
    349
    Maybe i asking stupid question as always, so sorry

    In my project NPC must be able to see another NPC'c. To implement vision i use unity physics distance query. But different NPC'c belongs to different physics categories (layers). Some time NPC may need to "see" 0 layer for one algorithm, sometime 1 layer for another, sometime both.

    I see 2 possible approaches:
    1. Make just one physics query inside some "VisionSystem" and setting the collision filter from outside of that system depending on what NPC need to see. As a result i have raw Entity collection that i need to sort (cause as i say entities will be used for different algorithms) using ComponentDataFromEntity.Exists()
    2. Make physics query in every algorithm that need too "see" entities and have no centralized "VisionSystem".
    As far as I can judge from testing, centralized query is more perfomant. But the more important reason why i want to use 1st approach is i have no need to write boilerplates with injecting all physics instances that i need like collision world and so on. Also, all the code for vision is in one place.

    Is there a way to somehow sort result hits by physics categories? (using collectors?)
     
    Last edited: Nov 5, 2019
  2. Tony_Max

    Tony_Max

    Joined:
    Feb 7, 2017
    Posts:
    349
    I see now, that i can access CollisionFilter data through RigidBody.Collider*, but it requires unsafe code(
     
  3. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    481
    Yeah, you can do this sort of thing to minimally make up unsafe sections
    Code (CSharp):
    1.             CollisionFilter filter = CollisionFilter.Default;
    2.             unsafe { filter = MyPhysicsColliderComponent.ColliderPtr->Filter; }
    3.             // use filter
    4.  
     
    Tony_Max likes this.