Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

181
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda