Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

ModifyBroadphasePairsSystem How to One Shot rather than Continuous Update

Discussion in 'Physics Previews' started by Antypodish, Aug 14, 2019.

  1. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,780
    So I am checking up ModifyBroadphasePairsSystem
    EntityComponentSystemSamples/UnityPhysicsSamples/Assets/Demos/5. Modify/Scripts/
    ModifyBroadphasePairsBehaviour.cs

    Copied Script
    Code (CSharp):
    1. using Unity.Physics;
    2. using Unity.Physics.Systems;
    3. using Unity.Collections;
    4. using Unity.Entities;
    5. using Unity.Jobs;
    6. using UnityEngine;
    7. using System;
    8. using Unity.Burst;
    9.  
    10. //<todo.eoin.usermod Rename to ModifyOverlappingBodyPairsComponentData?
    11. public struct ModifyBroadphasePairs : IComponentData { }
    12.  
    13. public class ModifyBroadphasePairsBehaviour : MonoBehaviour, IConvertGameObjectToEntity
    14. {
    15.     void IConvertGameObjectToEntity.Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    16.     {
    17.         dstManager.AddComponentData(entity, new ModifyBroadphasePairs());
    18.     }
    19. }
    20.  
    21. // A system which configures the simulation step to disable certain broad phase pairs
    22. [UpdateBefore(typeof(StepPhysicsWorld))]
    23. public class ModifyBroadphasePairsSystem : JobComponentSystem
    24. {
    25.     EntityQuery m_PairModifierGroup;
    26.  
    27.     BuildPhysicsWorld m_PhysicsWorld;
    28.     StepPhysicsWorld m_StepPhysicsWorld;
    29.  
    30.     protected override void OnCreate()
    31.     {
    32.         m_PhysicsWorld = World.GetOrCreateSystem<BuildPhysicsWorld>();
    33.         m_StepPhysicsWorld = World.GetOrCreateSystem<StepPhysicsWorld>();
    34.  
    35.         m_PairModifierGroup = GetEntityQuery(new EntityQueryDesc
    36.         {
    37.             All = new ComponentType[] { typeof(ModifyBroadphasePairs) }
    38.         });
    39.     }
    40.  
    41.     protected override JobHandle OnUpdate(JobHandle inputDeps)
    42.     {
    43.         if (m_PairModifierGroup.CalculateLength() == 0)
    44.         {
    45.             return inputDeps;
    46.         }
    47.  
    48.         if( m_StepPhysicsWorld.Simulation.Type == SimulationType.NoPhysics )
    49.         {
    50.             return inputDeps;
    51.         }
    52.  
    53.         // Add a custom callback to the simulation, which will inject our custom job after the body pairs have been created
    54.         SimulationCallbacks.Callback callback = (ref ISimulation simulation, ref PhysicsWorld world, JobHandle inDeps) =>
    55.         {
    56.             inDeps.Complete(); //<todo Needed to initialize our modifier
    57.  
    58.             return new DisablePairsJob
    59.             {
    60.                 Bodies = m_PhysicsWorld.PhysicsWorld.Bodies,
    61.                 Motions = m_PhysicsWorld.PhysicsWorld.MotionVelocities
    62.             }.Schedule(simulation, ref world, inputDeps);
    63.         };
    64.         m_StepPhysicsWorld.EnqueueCallback(SimulationCallbacks.Phase.PostCreateDispatchPairs, callback);
    65.  
    66.         return inputDeps;
    67.     }
    68.  
    69.     [BurstCompile]
    70.     struct DisablePairsJob : IBodyPairsJob
    71.     {
    72.         public NativeSlice<RigidBody> Bodies;
    73.         [ReadOnly] public NativeSlice<MotionVelocity> Motions;
    74.  
    75.         public unsafe void Execute(ref ModifiableBodyPair pair)
    76.         {
    77.             // Disable the pair if a box collides with a static object
    78.             int indexA = pair.BodyIndices.BodyAIndex;
    79.             int indexB = pair.BodyIndices.BodyBIndex;
    80.             if ((Bodies[indexA].Collider != null && Bodies[indexA].Collider->Type == ColliderType.Box && indexB >= Motions.Length)
    81.                 || (Bodies[indexB].Collider != null && Bodies[indexB].Collider->Type == ColliderType.Box && indexA >= Motions.Length))
    82.             {
    83.                 pair.Disable();
    84.             }
    85.         }
    86.     }
    87. }

    The thing is that disabling collision pairs, happens every update. It doesn't makes sense for me. I would think I just set it once, like OneShot.
    I tried to execute job only once. But it looks like, disabling must happen continuously.

    This feels very wasteful for thousands of entities. What is other alternative, to disable pairs only once, or filter group of entities?

    Would it be CollisionFilter like in case of
    UnityPhysicsSamples/Assets/Tests/JointTest/LimitedHingeDemo.cs
    or
    UnityPhysicsSamples/Assets/Tests/JointTest/BoxCapsuleDemo.cs
    or
    UnityPhysicsSamples/Assets/Demos/6. Use Cases/RaycastCar/Scripts/VehicleMechanics.cs
    If so, I still need investigate this options.

    I would like to filter groups of multiple entities, so the entities in each group, don't collide with each other.


    Edit:
    Ok, I think I have figured out things, or two.

    BoxCapsuleDemo allowed me to test basics of.
    Code (CSharp):
    1. private static CollisionFilter filter ( uint layer, uint disabled )
    I see it also operates on bitmask, in similar manner as classic Unity OOP.

    Regarding groups, I found Compound demo, which may be also what I am looking for.
     
    Last edited: Aug 14, 2019
  2. elcionap

    elcionap

    Joined:
    Jan 11, 2016
    Posts:
    138
    @Antypodish just an honestly question, any reason why you starting posting about the DOTS Physics here? I'm asking only because I have never witnessed an answer from the DOTS Physics team here and I'm worried that you may be missing some help from them.

    []'s
     
    Antypodish likes this.
  3. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,780
    I did crosslink this post and my latest related threads, to DOTS forum, as they have as well just single thread about new experimental DOTS physics. However, single thread easily hides older discussed subjects. But experimental physics preview full forum is here. So it makes more sense, to keep topics in own threads, in designated forum. There is single pined thread, indicating PhysX. But information does not excludes DOTS. Specially there ara other physics topics here, which are DOTS related. So that my reason for posting here.