matter-js icon indicating copy to clipboard operation
matter-js copied to clipboard

Technique used in manipulation demo is insufficient to wake sleeping bodies

Open lostfictions opened this issue 4 years ago • 3 comments

Hey there, thanks for this library! I really appreciate how well-structured and documented everything is.

I saw in #532 (and in other issues) that to allow programmatically-moved bodies to wake sleeping bodies, their velocity should manually be set as well as their position. Unfortunately, it doesn't actually seem like this is sufficient. I've made a repro case here by changing the manipulation demo a bit: https://codesandbox.io/s/elegant-ellis-kihf9?file=/src/index.js

The dynamic box on top of the programmatically-moved static box should fall asleep, at which point no more collisions will happen (and it'll just hang in space). The T-shaped compound on the right will also happily pass through any sleeping bodies it collides with.

image

Does something else need to be done? It's not clear to me if there's any way for non-dynamic bodies to even manually disable sleeping on other bodies, since no collision events ever fire.

lostfictions avatar May 16 '20 01:05 lostfictions

Looks like this condition at the beginning of Detector.collisions is preventing static-on-sleeping collisions from being recognized:

https://github.com/liabru/matter-js/blob/7894b4b44d41077200cfe209c01b35b595db90f4/src/collision/Detector.js#L38-L39

I changed that to the following:

-  if ((bodyA.isStatic || bodyA.isSleeping) && (bodyB.isStatic || bodyB.isSleeping))
+  if ((bodyA.isSleeping && bodyB.isSleeping) || (bodyA.isStatic && bodyB.isStatic))

and that was enough to allow collision events to fire between a programmatically-moved static body and a dynamic sleeping body, though it doesn't wake the sleeping body (but at that point it's easy to wake it in the collision event). It didn't seem to cause any spurious events if a body comes to rest against, say, a static floor and falls asleep, though I didn't test much more thoroughly. Happy to submit a PR if this is a desirable fix!

lostfictions avatar May 16 '20 03:05 lostfictions

Hey @liabru, I proposed (what I think is) a relatively non-disruptive solution to this issue in #877 -- any chance you could give feedback on that?

lostfictions avatar Sep 29 '21 03:09 lostfictions

Is there a final solution to this problem?

Recently I developed a project with creating multiple bodies, they are not static. And after each few seconds there will be a new body(isStatic) was manipulated to hit those non-static bodies, you know, like a snooker game.

But because I created a lot of bodies and they keep colliding each others,so I was thinking to improve performance by using enableSleeping, then I have the same problem that when a new body start to hit those sleeping bodies,it just stoped and overlapped on them. It seems that setting different collision filter on them.

how can i solve this problem?

bill-shuzai avatar Jan 16 '23 09:01 bill-shuzai