Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How to pass PhysicsWorld to CollisionEventJob to get collisionDetails?

Discussion in 'Physics for ECS' started by Balphagore, Aug 18, 2022.

  1. Balphagore

    Balphagore

    Joined:
    Jul 18, 2019
    Posts:
    81
    Right now I'm trying to do this:
    Code (CSharp):
    1. using Unity.Burst;
    2. using Unity.Entities;
    3. using Unity.Jobs;
    4. using Unity.Physics;
    5. using Unity.Physics.Systems;
    6. using UnityEngine;
    7.  
    8. [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    9. [UpdateAfter(typeof(ExportPhysicsWorld))]
    10. [UpdateBefore(typeof(EndFramePhysicsSystem))]
    11. public partial class SphereCollisionSystem : SystemBase
    12. {
    13.     BuildPhysicsWorld buildPhysicsWorldSystem;
    14.     StepPhysicsWorld stepPhysicsWorldSystem;
    15.  
    16.     protected override void OnCreate()
    17.     {
    18.         buildPhysicsWorldSystem = World.DefaultGameObjectInjectionWorld.GetOrCreateSystem<BuildPhysicsWorld>();
    19.         stepPhysicsWorldSystem = World.DefaultGameObjectInjectionWorld.GetOrCreateSystem<StepPhysicsWorld>();
    20.     }
    21.  
    22.     [BurstCompile]
    23.     private struct CollisionEventJob : ICollisionEventsJob
    24.     {
    25.         public PhysicsWorld physicsWorld;
    26.         public void Execute(CollisionEvent collisionEvent)
    27.         {
    28.             Debug.Log("Collision");
    29.             CollisionEvent.Details collisionDetails = collisionEvent.CalculateDetails(ref physicsWorld);
    30.             Debug.Log(string.Format("{0}", collisionDetails));
    31.         }
    32.     }
    33.  
    34.     protected override void OnStartRunning()
    35.     {
    36.         this.RegisterPhysicsRuntimeSystemReadOnly();
    37.     }
    38.  
    39.     protected override void OnUpdate()
    40.     {
    41.         JobHandle collisionJobHandle = new CollisionEventJob()
    42.         {
    43.             physicsWorld = buildPhysicsWorldSystem.PhysicsWorld
    44.         }.Schedule(stepPhysicsWorldSystem.Simulation, this.Dependency);
    45.         collisionJobHandle.Complete();
    46.     }
    47. }
    But in response I get an error:


    Code (CSharp):
    1. InvalidOperationException: The previously scheduled job PhysicsWorldExporter:ExportDynamicBodiesJob reads from the Unity.Collections.NativeArray`1[Unity.Physics.MotionData] ExportDynamicBodiesJob.JobData.MotionDatas. You are trying to schedule a new job SphereCollisionSystem:CollisionEventJob, which writes to the same Unity.Collections.NativeArray`1[Unity.Physics.MotionData] (via CollisionEventJob.UserJobData.physicsWorld.DynamicsWorld.m_MotionDatas). To guarantee safety, you must include PhysicsWorldExporter:ExportDynamicBodiesJob as a dependency of the newly scheduled job.
    2. Unity.Jobs.LowLevel.Unsafe.JobsUtility.Schedule (Unity.Jobs.LowLevel.Unsafe.JobsUtility+JobScheduleParameters& parameters) (at <c6b52566f59b49fc861a7812a1ea2f6b>:0)
    3. Unity.Physics.ICollisionEventJobExtensions.ScheduleUnityPhysicsCollisionEventsJob[T] (T jobData, Unity.Physics.ISimulation simulation, Unity.Jobs.JobHandle inputDeps) (at Library/PackageCache/com.unity.physics@0.51.1-preview.21/Unity.Physics/Dynamics/Simulation/ICollisionEventsJob.cs:83)
    4. Unity.Physics.ICollisionEventJobExtensions.Schedule[T] (T jobData, Unity.Physics.ISimulation simulation, Unity.Jobs.JobHandle inputDeps) (at Library/PackageCache/com.unity.physics@0.51.1-preview.21/Unity.Physics/Dynamics/Simulation/ICollisionEventsJob.cs:45)
    5. SphereCollisionSystem.OnUpdate () (at Assets/Scripts/Systems/SphereCollisionSystem.cs:41)
    6. Unity.Entities.SystemBase.Update () (at Library/PackageCache/com.unity.entities@0.51.1-preview.21/Unity.Entities/SystemBase.cs:409)
    7. Unity.Entities.ComponentSystemGroup.UpdateAllSystems () (at Library/PackageCache/com.unity.entities@0.51.1-preview.21/Unity.Entities/ComponentSystemGroup.cs:583)
    8. UnityEngine.Debug:LogException(Exception)
    9. Unity.Debug:LogException(Exception) (at Library/PackageCache/com.unity.entities@0.51.1-preview.21/Unity.Entities/Stubs/Unity/Debug.cs:19)
    10. Unity.Entities.ComponentSystemGroup:UpdateAllSystems() (at Library/PackageCache/com.unity.entities@0.51.1-preview.21/Unity.Entities/ComponentSystemGroup.cs:588)
    11. Unity.Entities.ComponentSystemGroup:OnUpdate() (at Library/PackageCache/com.unity.entities@0.51.1-preview.21/Unity.Entities/ComponentSystemGroup.cs:529)
    12. Unity.Entities.ComponentSystem:Update() (at Library/PackageCache/com.unity.entities@0.51.1-preview.21/Unity.Entities/ComponentSystem.cs:114)
    13. Unity.Entities.ComponentSystemGroup:UpdateAllSystems() (at Library/PackageCache/com.unity.entities@0.51.1-preview.21/Unity.Entities/ComponentSystemGroup.cs:583)
    14. Unity.Entities.ComponentSystemGroup:OnUpdate() (at Library/PackageCache/com.unity.entities@0.51.1-preview.21/Unity.Entities/ComponentSystemGroup.cs:523)
    15. Unity.Entities.ComponentSystem:Update() (at Library/PackageCache/com.unity.entities@0.51.1-preview.21/Unity.Entities/ComponentSystem.cs:114)
    16. Unity.Entities.DummyDelegateWrapper:TriggerUpdate() (at Library/PackageCache/com.unity.entities@0.51.1-preview.21/Unity.Entities/ScriptBehaviourUpdateOrder.cs:426)
    17.  
     
  2. TRS6123

    TRS6123

    Joined:
    May 16, 2015
    Posts:
    246
    I'm pretty sure you need to add [ReadOnly] to the physicsWorld field of your job
     
    Balphagore likes this.
  3. Balphagore

    Balphagore

    Joined:
    Jul 18, 2019
    Posts:
    81
    Oh yes, it helped, thanks)