Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

CollisionEvent detection fails (following ECS Physics samples)

Discussion in 'Physics for ECS' started by alvinwan, Jul 4, 2020.

  1. alvinwan

    alvinwan

    Joined:
    Jan 21, 2018
    Posts:
    34
    To detect collisions between two objects, I'm following this thread (link), using the format that @Bas-Smit recommends. The recommended approach agrees with the official code samples (link1, link2). However, in a vanilla project, I receive the following error, repeated at every timestep. Am I breaking convention or mis-setting up my entities somehow?

    Code (CSharp):
    1.  
    2. InvalidOperationException: The previously scheduled job System:CollisionEventJob reads from the Unity.Collections.NativeArray`1[Unity.Physics.Velocity] CollisionEventJob.EventReader.m_InputVelocities. You are trying to schedule a new job Solver:ParallelApplyGravityAndCopyInputVelocitiesJob, which writes to the same Unity.Collections.NativeArray`1[Unity.Physics.Velocity] (via ParallelApplyGravityAndCopyInputVelocitiesJob.InputVelocities). To guarantee safety, you must include System:CollisionEventJob as a dependency of the newly sche
    3. Unity.Jobs.LowLevel.Unsafe.JobsUtility.ScheduleParallelFor (Unity.Jobs.LowLevel.Unsafe.JobsUtility+JobScheduleParameters& parameters, System.Int32 arrayLength, System.Int32 innerloopBatchCount) (at <640657a3133d4ea99521a19cbe77e665>:0)
    4. Unity.Jobs.IJobParallelForExtensions.Schedule[T] (T jobData, System.Int32 arrayLength, System.Int32 innerloopBatchCount, Unity.Jobs.JobHandle dependsOn) (at /Users/builduser/buildslave/unity/build/Runtime/Jobs/Managed/IJobParallelFor.cs:52)
    5. Unity.Physics.Solver.ScheduleApplyGravityAndCopyInputVelocitiesJob (Unity.Collections.NativeArray`1[T] motionVelocities, Unity.Collections.NativeArray`1[T] inputVelocities, Unity.Mathematics.float3 gravityAcceleration, Unity.Jobs.JobHandle inputDeps, System.Int32 threadCountHint) (at Library/PackageCache/com.unity.physics@0.4.0-preview.5/Unity.Physics/Dynamics/Solver/Solver.cs:167)
    6. Unity.Physics.Simulation.ScheduleStepJobs (Unity.Physics.SimulationStepInput input, Unity.Physics.SimulationCallbacks callbacksIn, Unity.Jobs.JobHandle inputDeps, System.Int32 threadCountHint) (at Library/PackageCache/com.unity.physics@0.4.0-preview.5/Unity.Physics/Dynamics/Simulation/Simulation.cs:363)
    7. Unity.Physics.Systems.StepPhysicsWorld.OnUpdate () (at Library/PackageCache/com.unity.physics@0.4.0-preview.5/Unity.Physics/ECS/Systems/StepPhysicsWorld.cs:100)
    8. Unity.Entities.SystemBase.Update () (at Library/PackageCache/com.unity.entities@0.11.1-preview.4/Unity.Entities/SystemBase.cs:414)
    9. Unity.Entities.ComponentSystemGroup.UpdateAllSystems () (at Library/PackageCache/com.unity.entities@0.11.1-preview.4/Unity.Entities/ComponentSystemGroup.cs:445)
    10. UnityEngine.Debug:LogException(Exception)
    11. Unity.Debug:LogException(Exception) (at Library/PackageCache/com.unity.entities@0.11.1-preview.4/Unity.Entities/Stubs/Unity/Debug.cs:19)
    12. Unity.Entities.ComponentSystemGroup:UpdateAllSystems() (at Library/PackageCache/com.unity.entities@0.11.1-preview.4/Unity.Entities/ComponentSystemGroup.cs:450)
    13. Unity.Entities.ComponentSystemGroup:OnUpdate() (at Library/PackageCache/com.unity.entities@0.11.1-preview.4/Unity.Entities/ComponentSystemGroup.cs:398)
    14. Unity.Entities.ComponentSystem:Update() (at Library/PackageCache/com.unity.entities@0.11.1-preview.4/Unity.Entities/ComponentSystem.cs:109)
    15. Unity.Entities.DummyDelegateWrapper:TriggerUpdate() (at Library/PackageCache/com.unity.entities@0.11.1-preview.4/Unity.Entities/ScriptBehaviourUpdateOrder.cs:192)
    16.  

    Setup for the vanilla project: There is one dynamic 1x1x1 cube. There is also one static (or kinematic, I tried both) 10x1x10 cube. There is also only one system, and its source is below. I attempted to force all previous physics jobs to finish, per this post (link), but the fix didn't work; source for this attempt is also below.

    Code (CSharp):
    1. using Unity.Entities;
    2. using Unity.Physics;
    3. using Unity.Physics.Systems;
    4.  
    5. // Got basic structure of colliders from https://forum.unity.com/threads/ecs-collisions.857725/
    6. [UpdateAfter(typeof(EndFramePhysicsSystem))]  // Also tried other orderings, for ex https://forum.unity.com/threads/handle-collisions-in-unity-physics-ecs.653644/#post-4526167
    7. public class System : SystemBase
    8. {
    9.     StepPhysicsWorld StepPhysicsWorld => World.GetOrCreateSystem<StepPhysicsWorld>();
    10.     BuildPhysicsWorld BuildPhysicsWorld => World.GetOrCreateSystem<BuildPhysicsWorld>();
    11.  
    12.     struct CollisionEventJob : ICollisionEventsJob
    13.     {
    14.         public void Execute(CollisionEvent evt)
    15.         {
    16.             UnityEngine.Debug.Log(evt);
    17.         }
    18.     }
    19.  
    20.     protected override void OnUpdate()
    21.     {
    22.         Dependency = new CollisionEventJob().Schedule
    23.         (
    24.             StepPhysicsWorld.Simulation,
    25.             ref BuildPhysicsWorld.PhysicsWorld,
    26.             Dependency
    27.         );
    28.     }
    29. }

    Code (CSharp):
    1. using Unity.Entities;
    2. using Unity.Physics;
    3. using Unity.Physics.Systems;
    4.  
    5. // Got basic structure of colliders from https://forum.unity.com/threads/ecs-collisions.857725/
    6. [UpdateAfter(typeof(EndFramePhysicsSystem))]
    7. public class System : SystemBase
    8. {
    9.     EndFramePhysicsSystem EndFramePhysicsSystem => World.GetOrCreateSystem<EndFramePhysicsSystem>();
    10.     StepPhysicsWorld StepPhysicsWorld => World.GetOrCreateSystem<StepPhysicsWorld>();
    11.     BuildPhysicsWorld BuildPhysicsWorld => World.GetOrCreateSystem<BuildPhysicsWorld>();
    12.  
    13.     struct CollisionEventJob : ICollisionEventsJob
    14.     {
    15.         public void Execute(CollisionEvent evt)
    16.         {
    17.             UnityEngine.Debug.Log(evt);
    18.         }
    19.     }
    20.  
    21.     protected override void OnUpdate()
    22.     {
    23.         EndFramePhysicsSystem.GetOutputDependency().Complete();
    24.         Dependency = new CollisionEventJob().Schedule
    25.         (
    26.             StepPhysicsWorld.Simulation,
    27.             ref BuildPhysicsWorld.PhysicsWorld,
    28.             Dependency
    29.         );
    30.     }
    31. }

    To reproduce: I've attached the Unity Project's entire Assets/ folder. Below is also a screenshot of all packages installed + versions. I'm on Unity version 2020.1.0b7.

    Any pointers for where to look would be much appreciated. In case more context is helpful: I'm trying to get the amount of force that one cube hits the other with. Naturally, the first step is to figure out when one cube touches the other, but seems like I'm making mistakes already xD
     

    Attached Files:

    Last edited: Jul 4, 2020
  2. brunocoimbra

    brunocoimbra

    Joined:
    Sep 2, 2015
    Posts:
    677
    That should work:
    Code (CSharp):
    1.     using Unity.Entities;
    2.     using Unity.Physics;
    3.     using Unity.Physics.Systems;
    4.  
    5.     [UpdateAfter(typeof(ExportPhysicsWorld))]
    6.     [UpdateBefore(typeof(EndFramePhysicsSystem>))]
    7.     public class System : SystemBase
    8.     {
    9.         EndFramePhysicsSystem EndFramePhysicsSystem => World.GetOrCreateSystem<EndFramePhysicsSystem>();
    10.         StepPhysicsWorld StepPhysicsWorld => World.GetOrCreateSystem<StepPhysicsWorld>();
    11.         BuildPhysicsWorld BuildPhysicsWorld => World.GetOrCreateSystem<BuildPhysicsWorld>();
    12.    
    13.         struct CollisionEventJob : ICollisionEventsJob
    14.         {
    15.             public void Execute(CollisionEvent evt)
    16.             {
    17.                 UnityEngine.Debug.Log(evt);
    18.             }
    19.         }
    20.    
    21.         protected override void OnUpdate()
    22.         {
    23.             Dependency = new CollisionEventJob().Schedule
    24.             (
    25.                 StepPhysicsWorld.Simulation,
    26.                 ref BuildPhysicsWorld.PhysicsWorld,
    27.                 Dependency
    28.             );
    29.             EndFramePhysicsSystem.AddInputDependency(Dependency);
    30.         }
    31.     }
    32.  
     
    steveeHavok, petarmHavok and alvinwan like this.
  3. alvinwan

    alvinwan

    Joined:
    Jan 21, 2018
    Posts:
    34
    Yess, just for posterity: that worked. thanks @brunocoimbra! I'll read more about those systems/barriers and why that worked.
     
    petarmHavok and brunocoimbra like this.
  4. AdrienIsHere

    AdrienIsHere

    Joined:
    Feb 28, 2020
    Posts:
    4
    The method AddInputDependency() doesn't exists anymore or has been renamed, any clue?
     
  5. brunocoimbra

    brunocoimbra

    Joined:
    Sep 2, 2015
    Posts:
    677
    Are you using the latest physics package?