collision system slow

Hi

I have this code that works but its quite slow…
if i remove the job.complete it gives errors when i play with some dependency problems
Does anyone know what am i doing wrong or is it just slow ?

There is only like 10-20 explosive entities but there are a lot of blocks like maybe 1-2k
if i commend out this code then the fps goes back to normal

[AlwaysSynchronizeSystem]
[UpdateAfter(typeof(EndFramePhysicsSystem))]
public class ExplosivesCollisionSystem : JobComponentSystem
{
    private BuildPhysicsWorld buildPhysicsWorld;
    private StepPhysicsWorld stepPhysicsWorld;

    protected override void OnCreate()
    {
        base.OnCreate();
        buildPhysicsWorld = World.GetOrCreateSystem<BuildPhysicsWorld>();
        stepPhysicsWorld = World.GetOrCreateSystem<StepPhysicsWorld>();
    }

    [BurstCompile]
    struct DetectCollisionsSystemJob : ICollisionEventsJob
    {
        public ComponentDataFromEntity<ExplosivesComponent> explosivesGroup;
        [ReadOnly] public ComponentDataFromEntity<BlockComponent> blocksGroup;

        public void Execute(CollisionEvent collisionEvent)
        {
            Entity entityA = collisionEvent.Entities.EntityA;
            Entity entityB = collisionEvent.Entities.EntityB;

            bool isBlockA = blocksGroup.Exists(entityA);
            bool isExplosivesA = explosivesGroup.Exists(entityA);
            bool isBlockB = blocksGroup.Exists(entityB);
            bool isExplosivesB = explosivesGroup.Exists(entityB);

            if (isExplosivesA && isBlockB)
            {
                ExplosivesComponent modifiedData = explosivesGroup[entityA];
                if (!modifiedData.DetectedCollision)
                {
                    modifiedData.DetectedCollision = true;
                    explosivesGroup[entityA] = modifiedData;
                }
            }

            if (isBlockA && isExplosivesB)
            {
                ExplosivesComponent modifiedData = explosivesGroup[entityB];
                if (!modifiedData.DetectedCollision)
                {
                    modifiedData.DetectedCollision = true;
                    explosivesGroup[entityB] = modifiedData;
                }
            }
        }
    }

    protected override JobHandle OnUpdate(JobHandle inputDependencies)
    {
        JobHandle jobHandle = new DetectCollisionsSystemJob
        {
            explosivesGroup = GetComponentDataFromEntity<ExplosivesComponent>(false),
            blocksGroup = GetComponentDataFromEntity<BlockComponent>(true),
        }.Schedule(stepPhysicsWorld.Simulation, ref buildPhysicsWorld.PhysicsWorld, inputDependencies);

        jobHandle.Complete();

        return jobHandle;
    }
}

You want to UpdateAfter ExportPhysicsWorld and UpdateBefore EndFramePhysicsSystem. Additionally, you want to add your jobHandle to the EndFramePhysicsSystem HandlesToWaitFor list.

this thread is duplicated, has been answered. thanks!!!

If you’re inheriting SystemBase and using Entities.ForEach(), how do you "add your jobHandle to EndFramePhysicsSystem HandlesToWaitFor list? Sorry noob here… I don’t even know how to get a job handle for a ForEach() loop…

So… this is my theory… I “think” SystemBase + Entities.ForEach automatically adds the dependency so we don’t need to call EndFramePhysicsSystem.HandlesToWaitFor.Add().

When I added this above my class declaration, it seems to have stopped the random crashing (crossing my fingers).

[UpdateAfter(typeof(StepPhysicsWorld)), UpdateBefore((typeof(EndFramePhysicsSystem)))]

var dependency = Entities.ForeEach().Schedule();

Quoting an official answer on this: https://discussions.unity.com/t/777008/9

SystemBase only handles Dependencies automatically for the Entities.ForEach, for any IJobChunk or custom Job (like ICollisionEventsJob) you need to handle manually.

protected override void OnUpdate()
{
    Dependency = new MyCollisionEventsJob
    {
        /*params*/
    }.Schedule(_stepPhysicsWorld.Simulation, ref _buildPhysicsWorld.PhysicsWorld, Dependency);

    _endFramePhysicsSystem.HandlesToWaitFor.Add(Dependency);
}

so my final code based on this is as follows
if anyone disagress, please say so :slight_smile:

[UpdateAfter(typeof(StepPhysicsWorld)), UpdateBefore((typeof(EndFramePhysicsSystem)))]
public class ExplosivesCollisionSystem : SystemBase
{
    private BuildPhysicsWorld buildPhysicsWorld;
    private StepPhysicsWorld stepPhysicsWorld;
    private EndFramePhysicsSystem endFramePhysicsSystem;

    protected override void OnCreate()
    {
        base.OnCreate();
        buildPhysicsWorld = World.GetOrCreateSystem<BuildPhysicsWorld>();
        stepPhysicsWorld = World.GetOrCreateSystem<StepPhysicsWorld>();
        endFramePhysicsSystem = World.GetOrCreateSystem<EndFramePhysicsSystem>();

    }

    [BurstCompile]
    struct DetectCollisionsSystemJob : ICollisionEventsJob
    {
        public ComponentDataFromEntity<ExplosivesComponent> explosivesGroup;
        [ReadOnly] public ComponentDataFromEntity<BlockComponentData> blocksGroup;

        public void Execute(CollisionEvent collisionEvent)
        {
            Entity entityA = collisionEvent.Entities.EntityA;
            Entity entityB = collisionEvent.Entities.EntityB;

            bool isBlockA = blocksGroup.Exists(entityA);
            bool isExplosivesA = explosivesGroup.Exists(entityA);
            bool isBlockB = blocksGroup.Exists(entityB);
            bool isExplosivesB = explosivesGroup.Exists(entityB);

            if (isExplosivesA && isBlockB)
            {
                ExplosivesComponent modifiedData = explosivesGroup[entityA];
                if (!modifiedData.DetectedCollision)
                {
                    modifiedData.DetectedCollision = true;
                    explosivesGroup[entityA] = modifiedData;
                }
            }

            if (isBlockA && isExplosivesB)
            {
                ExplosivesComponent modifiedData = explosivesGroup[entityB];
                if (!modifiedData.DetectedCollision)
                {
                    modifiedData.DetectedCollision = true;
                    explosivesGroup[entityB] = modifiedData;
                }
            }
        }
    }

    protected override void OnUpdate()
    {
        Dependency = new DetectCollisionsSystemJob
        {
            explosivesGroup = GetComponentDataFromEntity<ExplosivesComponent>(false),
            blocksGroup = GetComponentDataFromEntity<BlockComponentData>(true),

        }.Schedule(stepPhysicsWorld.Simulation, ref buildPhysicsWorld.PhysicsWorld, Dependency);

        endFramePhysicsSystem.HandlesToWaitFor.Add(Dependency);
    }
}