Matter js sensor collision sometime not detected on enter collision

hello i made a simple slot machine game with sensor on when an object hit the bottom area. but it seems there are case where sensor is not triggered properly.

below are the code i use to set up the sensor area

this.matter.world.on('collisionstart', (event)=> {
        for (const pair of event.pairs) {
            const bodyA = pair.bodyA;
            const bodyB = pair.bodyB;
            console.log(pair);
            if (!bodyA.isSensor && !bodyB.isSensor) return;
            const sensor = bodyA.isSensor ? bodyA : bodyB;
            console.log(`sensor ${sensor.label} is hit`);
            this.goal();
        }
    });

i’m having hard time debugging these case without any clear reproduction process.

thank you beforehand

Does ‘collisionStart’ always trigger when you don’t use sensors?
If so, maybe you could handle it yourself by pair.isActive = false or something like that.
Or you could check ‘collisionActive’, and if you never received ‘collisionStart’ for that pair, you know you missed it.

yes it does. it prints every pair. but in the case it doesn’t trigger the corellated pair doesn’t print. i am thinking about using set 60hz if that even matter.

trying collision active seems to be more reliable. but for some reason the back of my head says it just one way of another hack. since i’ll be adding flag on stuff.

nvm after hundred of tries there are still some rare case when it happens. even with collisionactive event

in the end the question is more like. how to make matter collision more accurate. and remove any missed collision event

just a heads up this in case someone encountering similar problem.

the problem was on my script below :

if (!bodyA.isSensor && !bodyB.isSensor) return;

which in perfect sense should be :

if (!bodyA.isSensor && !bodyB.isSensor) continue;

i feel so dumb now. maybe it’s related as i’m ill when i write the code beforehand :frowning:

1 Like