Search Unity

Can anyone give some Example of TriggerEvent?

Discussion in 'Entity Component System' started by ilyaylm, Apr 20, 2019.

  1. ilyaylm

    ilyaylm

    Joined:
    Aug 20, 2015
    Posts:
    19
    I’d read packages. But I cannot still understand howto use TriggerEvent and CollisionEvent. And neither BlockStream too. Plz help.
    Am I miss something in example? I cannot find example scene of Event.
     
  2. TRS6123

    TRS6123

    Joined:
    May 16, 2015
    Posts:
    246
    Code (CSharp):
    1. [BurstCompile]
    2. private struct CollisionJob : IJob
    3. {
    4.     [ReadOnly] public PhysicsWorld PhysicsWorld;
    5.     [ReadOnly] public CollisionEvents CollisionEvents;
    6.  
    7.     public void Execute()
    8.     {
    9.         foreach (CollisionEvent collisionEvent in CollisionEvents)
    10.         {
    11.             Entity entityA = PhysicsWorld.Bodies[collisionEvent.BodyIndices.BodyAIndex].Entity;
    12.             Entity entityB = PhysicsWorld.Bodies[collisionEvent.BodyIndices.BodyBIndex].Entity;
    13.             //your logic here
    14.         }
    15.     }
    16. }
     
    NT_Ninetails, ilyaylm and siggigg like this.
  3. TRS6123

    TRS6123

    Joined:
    May 16, 2015
    Posts:
    246
    You can access the PhysicsWorld from the BuildPhysicsWorld system and both CollisionEvents and TriggerEvents from the Simulation of the StepPhysicsWorld system. Be sure to pass BuildPhysicsWorld.FinalJobHandle and StepPhysicsWorld.FinalSimulationJobHandle as dependencies into the job.
     
    jgp80, ilyaylm and siggigg like this.
  4. ilyaylm

    ilyaylm

    Joined:
    Aug 20, 2015
    Posts:
    19
    This is perfect. Appreciate!
     
    steveeHavok likes this.
  5. thelebaron

    thelebaron

    Joined:
    Jun 2, 2013
    Posts:
    857
    hm I tried this and there don't seem to be any collision events with this. Is this correct?

    Code (CSharp):
    1.  
    2. using Unity.Burst;
    3. using Unity.Collections;
    4. using Unity.Entities;
    5. using Unity.Jobs;
    6. using Unity.Physics;
    7. using Unity.Physics.Systems;
    8. using UnityEngine;
    9.  
    10. namespace DefaultNamespace
    11. {
    12.     public class CollisionSystem: JobComponentSystem
    13.     {
    14.         private BuildPhysicsWorld m_BuildPhysicsWorld;
    15.         private StepPhysicsWorld m_StepPhysicsWorld;
    16.         private EndSimulationEntityCommandBufferSystem m_EndSimulationEntityCommandBufferSystem;
    17.        
    18.         protected override void OnCreate()
    19.         {
    20.             m_BuildPhysicsWorld = Unity.Entities.World.Active.GetOrCreateSystem<BuildPhysicsWorld>();
    21.             m_StepPhysicsWorld = Unity.Entities.World.Active.GetOrCreateSystem<StepPhysicsWorld>();
    22.             m_EndSimulationEntityCommandBufferSystem = Unity.Entities.World.Active.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
    23.         }
    24.  
    25.         //[BurstCompile]
    26.         private struct CollisionJob : IJob
    27.         {
    28.             [ReadOnly] public PhysicsWorld PhysicsWorld;
    29.             [ReadOnly] public CollisionEvents CollisionEvents;
    30.             [ReadOnly] public ComponentDataFromEntity<PhysicsVelocity> PhysicsVelocityData;
    31.             public EntityCommandBuffer EntityCommandBuffer;
    32.    
    33.             public void Execute()
    34.             {
    35.                 foreach (CollisionEvent collisionEvent in CollisionEvents)
    36.                 {
    37.                     Debug.Log("None of this gets logged");
    38.                     Debug.Log(collisionEvent.AccumulatedImpulses.xyz);
    39.                     Debug.Log(collisionEvent.AccumulatedImpulses.xyzw);
    40.                     Entity entityA = PhysicsWorld.Bodies[collisionEvent.BodyIndices.BodyAIndex].Entity;
    41.                     Entity entityB = PhysicsWorld.Bodies[collisionEvent.BodyIndices.BodyBIndex].Entity;
    42.                     //your logic here
    43.                     if(!PhysicsVelocityData.Exists(entityA))
    44.                         EntityCommandBuffer.AddComponent(entityA, new PhysicsVelocity());
    45.                     if(!PhysicsVelocityData.Exists(entityB))
    46.                         EntityCommandBuffer.AddComponent(entityB, new PhysicsVelocity());
    47.                        
    48.                 }
    49.             }
    50.         }
    51.  
    52.         protected override JobHandle OnUpdate(JobHandle inputDeps)
    53.         {
    54.             var combineDependencies = JobHandle.CombineDependencies(inputDeps, m_BuildPhysicsWorld.FinalJobHandle,
    55.                 m_StepPhysicsWorld.FinalSimulationJobHandle);
    56.            
    57.             var collisionJob = new CollisionJob
    58.             {
    59.                 PhysicsWorld = m_BuildPhysicsWorld.PhysicsWorld,
    60.                 CollisionEvents = m_StepPhysicsWorld.Simulation.CollisionEvents,
    61.                 PhysicsVelocityData = GetComponentDataFromEntity<PhysicsVelocity>(),
    62.                 EntityCommandBuffer = m_EndSimulationEntityCommandBufferSystem.CreateCommandBuffer()
    63.             };
    64.  
    65.             var collisionJobHandle = collisionJob.Schedule(combineDependencies);
    66.             return collisionJobHandle;
    67.         }
    68.     }
    69. }
    70.  
     
    phobos2077, Arkade and NT_Ninetails like this.
  6. thelebaron

    thelebaron

    Joined:
    Jun 2, 2013
    Posts:
    857
    After some more investigation, im not sure I understand what a collision event is? My assumption was it was something that occurred when one or more dynamic objects collided with something. However when DrawCollisionEvents is ticked in the physics settings, in any of the demos or my own tests nothing seems to be shown as far as debug gizmos, so I'm not sure if this is something that is not yet implemented or something else that I'm not understanding?
     
  7. TRS6123

    TRS6123

    Joined:
    May 16, 2015
    Posts:
    246
    @thelebaron Did you set "Raises Collision Events" toggle to true in the Physics Shape script?
     
  8. thelebaron

    thelebaron

    Joined:
    Jun 2, 2013
    Posts:
    857
    ah thanks @TRS6123 , that was what I was missing
     
  9. mk1987

    mk1987

    Joined:
    Mar 18, 2019
    Posts:
    53
    Hi, I've started to try and get a grips with DOTS and Physics system and currently using 2019.3.7f1 with Physics 0.3.2. I had tried making my own version of the script @thelebaron made above but I kept getting an error so decided to just copy and paste it in to see what im doing wrong.. With the exact code above im getting the same error for both Trigger and Collisions events. Looking at the online manual the code seems reasonable but for whatever reason the line.. 'stepPhysicsWorld.Simulation.CollisionEvents' is apparently not valid as my '.Simulation' doesnt have a collisionevents extension.

    error CS1061: 'ISimulation' does not contain a definition for 'CollisionEvents' and no accessible extension method 'CollisionEvents' accepting a first argument of type 'ISimulation' could be found (are you missing a using directive or an assembly reference?)

    It doesnt look like its changed for my Physics version, could someone point me in the direction of what im missing/doing wrong?
     
  10. Lieene-Guo

    Lieene-Guo

    Joined:
    Aug 20, 2013
    Posts:
    547
    There is a ITriggerEventsJob in physics...
    Code (CSharp):
    1.         protected override void OnUpdate()
    2.         {
    3.             Dependency = new TriggerEnterOrStayJob()
    4.             {
    5.             }.Schedule(m_StepPhysicsWorldSystem.Simulation, ref m_BuildPhysicsWorldSystem.PhysicsWorld, Dependency);
    6.         }
    7.  
    8.         [BurstCompile]
    9.         struct TriggerEnterOrStayJob : ITriggerEventsJob
    10.         {
    11.             public void Execute(TriggerEvent triggerEvent)
    12.             { }
    13.         }
    Same for ICollisionEventsJob