Collision Trigger Layer sample project does not work

I am trying to get Trigger Layer to work but the sample project here by yaustar doesnt seem to work properly.

The issue I am having is that the Trigger Layer seems to “trigger” regardless of which layer it is set to activate for. For example, in the sample project, if you set the “Orange Trigger” to only trigger for “Group A” and “Group C”, it will still trigger for all A/B/C groups and the console log still shows “Box Red” “Box Blue” “Box Yellow” despite “Box Red” being set to “Group B” “Mask B” only.
Screenshot 2023-06-29 173056

Rigidbody collisions seems to work fine, Triggers are the only thing that seems to be the problem. I’ve done further experiments to see why this happens and all I figured out was that, if the rigidbody that the trigger is colliding with has a “pc.BODYGROUP_TRIGGER” flag, it will call the triggerenter regardless, and if it doesnt, it won’t seem to call the triggerenter even if the other groupmasks match.

Can someone help me on how to properly fix this so that, the “Orange Trigger” only runs the triggerenter code when it collides with the correct group? I rather not have to resort to something like entity name or tag string compares.

1 Like

Hi @caciopepe,

Hmm, I’m not sure if this project used to have the result you expect in the past regarding the trigger response. @LeXXik do you any idea this?

Here is the Bullet (Ammo.js underlying engine) on the subject: Bullet Collision Filtering — Panda3D Manual

Hmm, Ammo. My favorite :smiley: Been a while since I used it.

It was forever ago when I looked at that project of @yaustar and, if I remember correctly, there was some issue in Ammo/engine to change the filters of a body after it was added to the world. Can’t recall what it was.

I remember the way I solved it was to have my own script that would create a trigger. Currently you can change the group and mask of a rigidbody component (ok, kinda, but not really, it just destroys old body and creates a new one with new filters), but the engine doesn’t allow to do that for the trigger. Those values are hardcoded.

So, for the rigidbody, you can do something like:

const rb = this.entity.rigidbody; = pc.BODYGROUP_USER_1;

And then for the trigger, you would set its filter like:

systems.rigidbody.addBody(body, pc.BODYGROUP_TRIGGER, pc.BODYGROUP_USER_1 ^ pc.BODYGROUP_TRIGGER);

You can check out how the engine creates a trigger and do it in a similar way. Yes, its a bit more painful, though, since you would also have to handle the lifecycle of the trigger you created, e.g. destroying it, when not needed, etc. Another option is to override Collision System Component so that it uses your custom Trigger class.

Ideally, the engine should provide an API for that, so you are welcome to create a feature request at the repo.

1 Like

Thank you for the answers. I guess that kind of explains what the “var flags = 4;” line in the trigger-layer.js code was trying to do (was trying to set the BODYFLAG_NORESPONSE_OBJECT flag I assume). I think I can get this to work now.

@caciopepe Thank you for providing this project as an example. Absolutely instrumental. Thanks again. -lev

1 Like