Search Unity

  1. Click here to receive a gift with your purchase of Unity Pro or Unity Enterprise.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Physics Callback Sample?

Discussion in 'Data Oriented Technology Stack' started by KlashnikovKid, Jun 3, 2019.

  1. KlashnikovKid

    KlashnikovKid

    Joined:
    Feb 1, 2013
    Posts:
    9
    Starting to play with the new DOTS physics package. I've got a good handle on raycasting and letting rigidbodies and colliders simulate/resolve. Nothing too crazy so far.

    However, now I'm trying to figure out how to implement some kind of simple collision callback (OnCollisionEnter behavior). I understand the API is pretty undefined in this area at the moment but I believe the docs still say this is possible using some kind delegate.

    https://docs.unity3d.com/Packages/com.unity.physics@0.0/manual/simulation_modification.html

    I've been poking around the samples to see if I could make sense of anything. No luck so far but hoping maybe someone out there could point me to a simple example. Something like an entity receiving a hit and updating a component accordingly for example.

    Thanks!
     
  2. Arkade

    Arkade

    Joined:
    Oct 11, 2012
    Posts:
    621
    I used this for a pretty good start (both the "this doesn't work" code and the "remember to set Raises Collision Events").
    https://forum.unity.com/threads/can-anyone-give-some-example-of-triggerevent.664732/#post-4452577

    I'm successfully getting OnCollisionEnter()-like behaviour which is what I wanted.

    I also found this which purports to provide the stay and leave parts:
    https://forum.unity.com/threads/handle-collisions-in-unity-physics-ecs.653644/#post-4526167

    That said, I have a feeling there might be better ways to achieve this and maybe (though I'm not sure) this was before other APIs might have been available (e.g. the one above and maybe using EntityQuery + change filters). I've not investigated further.

    HTH
    p.s. They all know more than me -- I'm only day 2 into trying this stuff! GL and keen to see what you conclude :)
     
  3. KlashnikovKid

    KlashnikovKid

    Joined:
    Feb 1, 2013
    Posts:
    9
    Thank you!!!! This looks promising. I'll share what I get working in this thread once I have some success.
     
  4. KlashnikovKid

    KlashnikovKid

    Joined:
    Feb 1, 2013
    Posts:
    9
    Well, I think I'm close going from the samples. Just trying to get something to compile. It looks like the CollisionEvents accessor from the StepPhysicsWorld is gone. So need to find that. Also I noticed the namespace for the collision events is now Unity.Physics.LowLevel

    Code (CSharp):
    1.  
    2. using Unity.Collections;
    3. using Unity.Entities;
    4. using Unity.Jobs;
    5. using Unity.Physics;
    6. using Unity.Physics.LowLevel;
    7. using Unity.Physics.Systems;
    8.  
    9. public class TestCollisionSystem : JobComponentSystem
    10. {
    11.     protected override JobHandle OnUpdate(JobHandle inputDeps)
    12.     {
    13.         var buildPhysicsWorld = World.Active.GetOrCreateSystem<BuildPhysicsWorld>();
    14.         var stepPhysicsWorld = World.Active.GetOrCreateSystem<StepPhysicsWorld>();
    15.  
    16.         var dependencies = JobHandle.CombineDependencies(inputDeps, buildPhysicsWorld.FinalJobHandle, stepPhysicsWorld.FinalSimulationJobHandle);
    17.  
    18.         return new TestCollisionSystemJob()
    19.         {
    20.             PhysicsWorld = buildPhysicsWorld.PhysicsWorld,
    21.             CollisionEvents = stepPhysicsWorld.Simulation.CollisionEvents // This has been moved. Can't find
    22.         }.Schedule(dependencies);
    23.     }
    24.  
    25.     struct TestCollisionSystemJob : IJob
    26.     {
    27.         [ReadOnly] public PhysicsWorld PhysicsWorld;
    28.         [ReadOnly] public CollisionEvents CollisionEvents;
    29.  
    30.         public void Execute()
    31.         {
    32.             foreach (var collisionEvent in CollisionEvents)
    33.             {
    34.                 Entity entityA = PhysicsWorld.Bodies[collisionEvent.BodyIndices.BodyAIndex].Entity;
    35.                 Entity entityB = PhysicsWorld.Bodies[collisionEvent.BodyIndices.BodyBIndex].Entity;
    36.                 // logic here
    37.             }
    38.         }
    39.     }
    40. }
    41.  
     
  5. KlashnikovKid

    KlashnikovKid

    Joined:
    Feb 1, 2013
    Posts:
    9
    Well, my previous post with a code sample has been awaiting moderation since last night... No idea why... (Tried to delete and repost with no luck..)

    I don't think these approaches are doable anymore. The collision events accessor off the StepPhysicsWorld system is no longer available. I'm guessing we have to figure out how to use the callback delegate mentioned in the last page of the docs.
     
  6. Arkade

    Arkade

    Joined:
    Oct 11, 2012
    Posts:
    621
    Oh? ...ah, you're talking about the second link's approach. Good to know that door's shut.

    Yeah I'm using the
    ICollisionEventsJob
    approach atm with Unity 2019.1.4f1 and Unity.Physics preview-0.1.0. Mine's working... well, when I get things right! (Just investigating why placing an EntityQuery on Joints in OnCreate() disables my System!)

    EDIT: Realised instead of creating the query with the
    JobComponentSystem
    -based
    GetEntityQuery()
    , I had to use
    EntityManager.CreateEntityQuery()
    so it's not considered as part of the system's requirements! (prevented from running by
    ShouldRunSystem()
    .)

    Is that this doc you're referring to? If not, any chance for a link?

    Re. code samples, maybe post a Gist? (though like you said, obsoleted API etc)

    Btw although I'm only on page 4 as yet of the giant Unity.Physics discussion thread, there are gems hidden in there.
     
    Last edited: Jun 4, 2019
  7. KlashnikovKid

    KlashnikovKid

    Joined:
    Feb 1, 2013
    Posts:
    9
    Yeah, I think I'm a version up with the physics package. Can't check at the moment. (Stupid dayjob)

    Yup, from the doc... And I'm guessing this is why the CollisionEvents accessor is gone... Now if we can only find a sample of someone doing this...

    https://docs.unity3d.com/Packages/com.unity.physics@0.0/manual/simulation_modification.html
    And yeaaaaah, I tried to work through some of that thread. I'll have to go back at it for goodies.
     
unityunity