Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

174
Visualizações
matter.js: collisionStart triggered many times for one collision

I am working on a game app using React native and Matter.js.

I am trying to implement a system that adds points every time a bullet hits a target.

In order to do this, I am trying to use collisionStart to detect the collision.

However, even though the bullet and target collide only once, the event seems to be triggered 41 times.

This is the code:

Matter.Events.on(engine, 'collisionStart', (event) => {
    let pairs = event.pairs
    for (const pair of pairs) {
      if (pair.bodyA.label === 'bullet' && pair.bodyB.label === 'Worm') {
        console.log("target hit");
      }
    }
  })

In the end, I'm planning to replace console.log with something that adds points. At the current moment, one collision seems like it would trigger the add points 41 times, which is obviously not ideal.

Any ideas what is happening here and how I can get this to trigger only once for one collision?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Try next example. I take it from my own project [you need little adaptd from ts to js]:

    Matter.Events.on(this.starter.getEngine(), "collisionStart", function (event) {
      root.collisionCheck(event, true);
    });
 public collisionCheck(event, ground: boolean) {

    const myInstance = this;

    const pairs = event.pairs;
    for (let i = 0, j = pairs.length; i !== j; ++i) {
      const pair = pairs[i];
      if (pair.activeContacts) {
        if (pair.bodyA.label === "bullet" && pair.bodyB.label === "Worm") {
          const collectitem = pair.bodyA;
          this.playerDie(collectitem);
        } else if (pair.bodyB.label === "bullet" && pair.bodyA.label === "Worm") {
          const collectitem = pair.bodyB;
          this.playerDie(collectitem);
        }

         // ....
        }
      }

  }

 public destroyBody = (destroyBody) => {
    try {
    Matter.Composite.remove(this.getWorld(), destroyBody);
    } catch(err) {
      console.log(err)
    }
  }

If you still have same problem , we can adapt also with flag PREVENT_DOUBLE_BY_1_SECOUND for example.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda