Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

178
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!