-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bodies still collide after their removal #230
Comments
I just ran into this issue. It will be a major blocker in my current project. Any hope of a fix soon? If not, I'll probably dig in later in the week and see if I can help. |
Super strange. Also, why do the obstacles slow down? As if it is frozen, but it's not? |
It does seem to freeze for around 80 ticks before it catches up... I'm not sure what causes it to check that entity is actually there after that time |
Another demo: var e = Matter.Engine.create(document.body);
var a = Matter.Bodies.rectangle(400, 400, 100, 60);
var b = Matter.Bodies.rectangle(450, 100, 100, 60);
var c = Matter.Bodies.rectangle(400, 610, 810, 60, {isStatic: true});
Matter.Events.on(e, 'collisionStart', _ => {
_.pairs.forEach(_ => {
if(_.bodyA === a || _.bodyB === a)
Matter.World.remove(e.world, a);
});
});
Matter.World.add(e.world, [a, b, c]);
Matter.Engine.run(e);
But, even in 0.8.0, if you remove |
Thanks for the additional test case @plurry. This should now be fixed in the edge build and should be in a release soon. Tell me if it works for you guys. |
Cool, bodies now get removed like they should. So yes, the original bug is fixed for me. But, a body that's not removed until |
I've created a demo where the issue still exists: |
Thanks for the new example @iamjoshua. I'm having a bit of trouble with this one as when I add your code into my dev build the issue does not seem to occur. I checked that you're using the correct version of matter.js in your demo and it seems to be up to date... weird. |
Very weird. I've tried several variations and have a new clue for you: https://jsfiddle.net/bkr16m07/4/ Removing a body on load, as in some of the previous demos, is no longer an issue with the edge build. However, removing a body after any delay, results in the bug. So my guess would be there is some reference array —perhaps in the rederer?— not getting updated. |
Alright, I've been digging around a bit and I'm still getting familiar with Matter so forgive me if I'm way off. It looks like within Engine.update you're taking Composite.allBodies as the source of bodies for collision detection which is why this is not an issue if the removal of a body occurs immediately. For subsequent updates, broadphase.pairsList is used: Line 148 in 7eb9ac7
However, from what I can tell so far, the pairs list does not get updated upon body removal: The pairsList is only updated if the grid has changed, but to determine if the grid has changed, you're taking the current list of bodies which does not include the removed bodies. |
It definitely seems to be a reference problem. If you update the same demo above (https://jsfiddle.net/bkr16m07/4/) as follows, the issue is resolved:
|
The absolute heart of the issue is within Common.extend. Some function reference isn't getting merged when extending. I'm afraid to break things so I'll stop digging here. An easy fix is to just have Engine use the world provided instead of creating a new one: https://github.com/liabru/matter-js/blob/master/src/core/Engine.js#L75 like so:
I'd submit a pull request but I'm guessing you'll want to resolve the reference issue itself some how. Anyways, I hope that helps. |
Ah, good work! I guess I've never passed a world before, I didn't spot that in your example either. So can you please submit a pull request with it (the comment isn't necessary though)? |
Engine uses provided world; fixes issue #230
This is now in the latest release, thanks guys! |
ccb version still has this problem. |
This was originally reported in #206, but the provided code was said to be unable to be reproduced. I have encountered this again, and have created a Pen to demonstrate it here. To reproduce, simply press the up arrow key and land the block on one of the moving rectangles. You should see that the body on bottom will be removed, but it will still report a collision (see the console logs). In certain cases, I have gotten it to report around 80 collisions after the removal of the body while the block is still suspended in the air.
The text was updated successfully, but these errors were encountered: