liabru / matter-js

a 2D rigid body physics engine for the web ▲● ■
MIT License
16.85k stars 1.97k forks source link

collisionStart firing multiple times #366

Open dxu opened 7 years ago

dxu commented 7 years ago

Hi! I wrote up a small plugin for adding collision events to bodies, but I'm looking to use this elsewhere, and I'm noticing that the 'collisionStart' event is being fired a lot of times compared what I would expect is a single collision between two boxes.

You can run the example included in the repo, but essentially I have two squares, BoxA and BoxB, falling onto the ground at different heights. I expected that there would be 3 collisionStart's (and 3 corresponding collisionEnd's), while there might be a bunch of collisionActive's:

  1. BoxA hits Ground
  2. BoxB hits BoxA (BoxB is above BoxA)
  3. BoxB falls over, hits Ground.

But instead I get something along the lines of 10-20 collisionStarts and Ends.

untitled

Is this intentional / expected? Am I doing something wrong?

Thanks for the great work you've done with this engine! It's really awesome, and I'm looking forward to playing with it further!

dxu commented 7 years ago

ah, never mind. I noticed that there is a unique pairID for collisions between two objects, so I can differentiate collisions based off that, I'm guessing?

So are new collisionStarts just any time there is a collision between two objects with a new contact point?

liabru commented 7 years ago

I've not yet had chance to look into this - did you resolve it or was it a misunderstanding? Collision start should only really fire once for each pair, until the body moves out of contact.

dxu commented 7 years ago

Oh, sorry! I thought I was just misunderstanding how the collisions were supposed to work. So if a box lands on its corner (assuming 0 restitution), and then falls flat onto the ground, collisionStart should only fire once in that case?

I'll reopen in case I'm misunderstanding my misunderstanding.

liabru commented 7 years ago

Yeah it should fire once, then multiple collisionActive then finally collisionEnd. Can you share your test case?

dxu commented 7 years ago

Here were the examples I was testing with, I console.log the events.

Only collisionStart Only collisionEnd Only collisionActive All events

I get ~9-15 collisionEnd events (seems to be inconsistent across refreshes), ~12-17 collisionStart events

liabru commented 7 years ago

Thanks for this, will check it out.

gokadi commented 2 years ago

Hi! I faced the same issue. Are there any updates?
"matter-js": "^0.17.1",

The behaviour is the same as @dxu provided above. Although two bodies already started collision (in my case moving through each other), for some reason I receive several groups of (1 collisionStart, lots of collisionActive and 1 collisionEnd) events, but I assume it is expected as 1 group of these events.

liabru commented 2 years ago

@gokadi can you check Matter.version is it 0.18.0?

If so can you try out the alpha build file included this PR? https://github.com/liabru/matter-js/pull/1079

Be great to know if it resolves, here's the discussion on it https://github.com/liabru/matter-js/issues/1077

RandomGamingDev commented 5 months ago

Hi! I faced the same issue. Are there any updates? The same issue is happening to me on 0.19.0