matter-js
matter-js copied to clipboard
body.isSensor = true; not setting non-collide if bodies are currently touching?
I'm working on adapting matter.js to a side scroller.
http://codepen.io/lilgreenland/pen/ozXNWZ?editors=0010
I made the player from sensor parts and regular body parts. I made the part of the player called headSensor a sensor when crouched and not a sensor when standing. This mostly works fine to allow the player to enter small areas.
headSensor.isSensor = true;
headSensor.isSensor = false;
The problem is that the headSensor will still collide with a body that is already touching the headSensor when I switch the head to a sensor. When the player collides with something new the headSensor will generally fix it's self.
Any ideas?
(To see what I am talking about try the game. Press T for testing mode, run over to the cave on the left, and crouch while up against the cave entrance. The headSensor is in blue.)
I worked around the issue by making the headSensor always a sensor and adding a body to the player that I translate when the player crouches to change the effective height of the player.
Was this ever resolved or investigated further? I ran in this exact issue with a one-way platform system where a body standing on terrain should fall through with an input. So I set the corresponding platform body to isSensor = true
but the bodies will keep interacting as if nothing ever changed. This only happens if they touched the moment the switch happened. The only workaround I found was to use the collisionfilter to disable collisions temporarily. That works even if the bodies have been touching.
Just ran into this myself - a good work around seems to be as @eduke2k mentions above.
If you're sticking to colliders that you've toggled off (like in my case "doors") then just toggle the collisionFilter
value as well.
E.g.
function toggleDoor(doorBody, isOpen) {
doorBody.isSensor = isOpen;
doorBody.render.visible = !isOpen;
doorBody.collisionFilter.mask = isOpen ? 0 : 0x0001;
}