Saturday night: Implementing DOTS Unity.Physics OnCollisionEnter() only for certain IComponentData


Decided to try creating a "tag" `ComponentType` `StickCause` that indicates a physics entity should stick to something.

How to filter query my `CollisionSystem` to only fire my `CollisionJob` for collisions where there's also a `StickCause` ?  This feels like the thing I'm supposed to do.  This blog suggests it's the right way to address this and this documentation suggests I'd maybe pass the query to `Schedule()` but the `ICollisionEventJobExtensions.Schedule()` doesn't look to take the same info and it calls an `internal` method.  I guess I could duplicate that code and do it myself (assuming nothing internal ends up being called).  That feels rather too hacky though.
Note "filtering" is a different but also potentially viable approach -- what I was talking about is termed "Querying".

So maybe I should supply the `StickCause` for each `Entity` to the `Job` and if there isn't one, I operate?  Maybe as a some sort of 'list' (e.g. a `NativeArray`) of all `StickCause` Entities and correlate entity index into that?  Maybe?  Ah, `GetComponentDataFromEntity<T>()` does the job.  That works.  So now I'm doing this for all entities and this code is called for all of them.

Could I also do this with a Physics.Material "Custom Flags" or "Collision Filter" ?   Maybe but I suspect I'll wish to add some data to the `StickCause` and do stuff with it later so prefer this approach.

Well, what I have may not be ideal approach from a performance point of view but maybe OK for now until I figure better approach.  (I'll hope to update this post if/when I find a better answer.)

So in summary I added this to the Job:

[ReadOnly] public ComponentDataFromEntity<stickcause> stickCauses;

Populated it in my system's `OnUpdate()` along with the other data:

stickCauses = GetComponentDataFromEntity<stickcause>(true),

And check for presence in the Job:

Entity entityA = PhysicsWorld.Bodies[collisionEvent.BodyIndices.BodyAIndex].Entity;
if (stickCauses.Exists(entityA))
  Debug.LogFormat("{0} is a StickCause and hit something", entityA);

Next, on to trying to get them to "connect"!

p.s. if you know the right answer, please comment/tweet me and I'll update and credit :)

<- PREV | NEXT ->

Leave a comment

Log in with itch.io to leave a comment.