#How to do an IBodyPairsJob with Unity Physics 1.3?

1 messages · Page 1 of 1 (latest)

lofty elbow
#

The example that unity uses in the docs doesn't even compile, and when I try to make it work it throws an error.
https://docs.unity3d.com/Packages/com.unity.physics@1.3/manual/simulation-modification.html
This is what I did:

[UpdateInGroup(typeof(PhysicsSimulationGroup))]
[UpdateAfter(typeof(PhysicsCreateBodyPairsGroup))]
[UpdateBefore(typeof(PhysicsCreateContactsGroup))]
public partial struct DisableDynamicDynamicPairsSystem : ISystem
{
    [BurstCompile]
    struct DisableDynamicDynamicPairsJob : IBodyPairsJob
    {
        public int NumDynamicBodies;

        public unsafe void Execute(ref ModifiableBodyPair pair)
        {
            // Disable the pair if it's dynamic-dynamic
            bool isDynamicDynamic = pair.BodyIndexA < NumDynamicBodies && pair.BodyIndexB < NumDynamicBodies;
            if (isDynamicDynamic)
            {
                pair.Disable();
            }
        }
    }

    [BurstCompile]
    public void OnUpdate(ref SystemState state)
    {
        PhysicsWorldSingleton worldSingleton = SystemAPI.GetSingletonRW<PhysicsWorldSingleton>().ValueRW;
        SimulationSingleton simulationSingleton = SystemAPI.GetSingleton<SimulationSingleton>();

        state.Dependency = new DisableDynamicDynamicPairsJob
        {
            NumDynamicBodies = worldSingleton.PhysicsWorld.NumDynamicBodies
        }.Schedule(simulationSingleton, ref worldSingleton.PhysicsWorld,
            state.Dependency);
    }
}

If I add this before the DisableDynamicDynamicPairsJob, it works, but it will still throw ~30 errors when entering playmode.
state.Dependency = simulationSingleton.AsSimulation().StepContext.CreatePhasedDispatchPairsJobHandle;
However, I have no idea what this does and this is definitely not an actual fix, as I have to do an assembly reference to even get access to this function as it is internal to the Unity.Physics assembly

#

The error that is thrown:

Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle.CheckExistsAndThrow (Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle& handle) (at <2c271b216ff84328b73d1d7f2333e7ab>:0)
Unity.Collections.NativeList`1[T].AsDeferredJobArray () (at ./Library/PackageCache/com.unity.collections@d49facba0036/Unity.Collections/NativeList.cs:702)
Unity.Physics.IBodyPairsJobExtensions.ScheduleUnityPhysicsBodyPairsJob[T] (T jobData, Unity.Physics.Simulation simulation, Unity.Physics.PhysicsWorld& world, Unity.Jobs.JobHandle inputDeps) (at ./Library/PackageCache/com.unity.physics@b8c0a578fe9d/Unity.Physics/Dynamics/Simulation/IBodyPairsJob.cs:132)
Unity.Physics.IBodyPairsJobExtensions.Schedule[T] (T jobData, Unity.Physics.SimulationSingleton simulationSingleton, Unity.Physics.PhysicsWorld& world, Unity.Jobs.JobHandle inputDeps) (at ./Library/PackageCache/com.unity.physics@b8c0a578fe9d/Unity.Physics/Dynamics/Simulation/IBodyPairsJob.cs:93)```
#

From just going through the code, it might have to do with the fact that the broadphase isn't set as a dependency to the job, but I don't know how I could do that. Setting the broadphaseSystem as a dependency for the job doesn't seem to work either.

sage mason
#

I think you are missing the require for update on the singletons.
Also to validate if it's a dependency issue just complete the state dependency on the first line of your update method.

#

Also you are querying the number of dynamic bodies in the singleton on the update method. The other systems may not have finished writing to it when you are scheduling your job, I would try to pass the dynamic bodies of the physics world singleton or the physics world itself.

sage mason
#

This is working, no error message and does disable the physics for all dynamic dynamic collisions.

using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Physics;
using Unity.Physics.Systems;
using UnityEngine;

[UpdateInGroup(typeof(PhysicsSimulationGroup))]
[UpdateAfter(typeof(PhysicsCreateBodyPairsGroup))]
[UpdateBefore(typeof(PhysicsCreateContactsGroup))]
public partial struct DisableDynamicDynamicPairsSystem : ISystem
{
    public void OnCreate(ref SystemState state)
    {
        state.RequireForUpdate<PhysicsWorldSingleton>();
        state.RequireForUpdate<SimulationSingleton>();
    }    
    [BurstCompile]
    public void OnUpdate(ref SystemState state)
    {
        PhysicsWorldSingleton worldSingleton = SystemAPI.GetSingleton<PhysicsWorldSingleton>();
        SimulationSingleton simulationSingleton = SystemAPI.GetSingleton<SimulationSingleton>();

        state.Dependency = new DisableDynamicDynamicPairsJob
        {
            DynamicBodies = worldSingleton.PhysicsWorld.DynamicBodies
        }.Schedule(simulationSingleton, ref worldSingleton.PhysicsWorld,
            state.Dependency);
    }        
    [BurstCompile]
    struct DisableDynamicDynamicPairsJob : IBodyPairsJob
    {
        [ReadOnly] public NativeArray<RigidBody> DynamicBodies;

        public void Execute(ref ModifiableBodyPair pair)
        {
            // Disable the pair if it's dynamic-dynamic
            bool isDynamicDynamic = DynamicBodies.Length > pair.BodyIndexA && DynamicBodies.Length > pair.BodyIndexB;
            Debug.Log($"Pair {pair.EntityA.Index}:{pair.EntityA.Version} - {pair.EntityB.Index}:{pair.EntityB.Version} is dynamic-dynamic: {isDynamicDynamic}");
            if (isDynamicDynamic)
            {
                Debug.Log($"Disabling dynamic-dynamic pair {pair.EntityA.Index}:{pair.EntityA.Version} - {pair.EntityB.Index}:{pair.EntityB.Version}");
                pair.Disable();
            }
        }
    }
}
lofty elbow
#

What unity version are you using? This doesn't work for me on 6000.1.10f1

sage mason
#

This was with 6000.2.2f1

lofty elbow
#

Okay I'll try it out there

sage mason
#

I think the version of the entities physics package would matter more than unity version

#

I'm on "com.unity.physics": "1.3.14"

lofty elbow
#

Yeah I'm also using 1.3.14

lofty elbow
#

How did you get it to work? I still get the same errors on a fresh install of 6000.2

sage mason
#

let me try in another project

sage mason
#

I've got a brand new project with just entities graphics, and physics.
The only script I added is the one in this thread (I fixed a mistake in the schedulking, you should pass in worldSingleton.PhysicsWorld.DynamicBodies instead of worldSingleton.PhysicsWorld.Bodies, but either way I don't have any exception).

lofty elbow
#

I still get errors. I did notice that error logging was hidden in the project.

sage mason
#

what errors do you get ?

lofty elbow
sage mason
#

This seems to be throwing because the simulation does not have a valid simulation.StepContext.PhasedDispatchPairs

#

can you try adding .complete() on the dependencies before and after scheduling the job ?

lofty elbow
#

Okay so it seems to only do it when the sub scene is closed.

#

When I open the sub scene it also works now

sage mason
#

yes I can confirm that

lofty elbow
#

And if I create a new scene it's every frame

#

weird

sage mason
#

completing the dependencies before and after the job schedule has no impact on the error so it's not system ordering related

#

I would raise a bug at this stage

lofty elbow
#

Yes that's what I'm going to do