piqnt / planck.js

2D JavaScript Physics Engine
http://piqnt.com/planck.js/
MIT License
4.87k stars 236 forks source link

Why does displacement cause multiple begin and end contact events? #234

Closed quinton-ashley closed 1 year ago

quinton-ashley commented 1 year ago

Please take a look at my "colliding bug" example on this page: https://p5play.org/learn/sprite.html?page=9

Why does the bug occur? I think planck should not end contact since the block and platform are moving at the same speed.

zOadT commented 1 year ago

Hey! Contact events aren't really meant to represent a state of "touching" but more to inform the engine that two bodies are colliding and need to be separated (and the user to be able to modify this behaviour). If this ist about checking if a player can jump you can take a look at https://www.iforce2d.net/b2dtut/jumpability. The second point in advantages is describing a slight variation of the behaviour you see, quote:

Secondly, as the player moves around, especially on slopes the main body tends to bounce slightly on the ground which causes a large number of begin/end events to occur in quick succession. If we were using the main body to determine jumpability, it's possible that the user could try to jump just when the main body is off the ground for a few milliseconds, even though it appears to be on the ground, and that would just be annoying huh? Using a sensor like this will cause a smoother and more reliable contact

quinton-ashley commented 1 year ago

Ah I probably should've mentioned that I'm familiar with that approach. I actually referenced that tutorial in my platformer tutorial: https://openprocessing.org/sketch/1869796

But I'm still wondering if there's another way to check if it's just displacement occurring during endContact vs a true end to the colliders touching?

shakiba commented 1 year ago

This is a very good discussion, however given that this is same behavior as Box2D, we won't be able to change it in Planck.

quinton-ashley commented 1 year ago

@shakiba It'd be really nice if you are able to fix the error to implement the changes under a flag like planck.accurateBouncing = true which would be false by default for compatibility.

Have you verified the error actually does occur in the latest version of c++ Box2D? I think it should be fixed there as well, I'll make an issue there.

shakiba commented 1 year ago

Looking at the article shared above, it seems to be the case in Box2D as well, it mentioned there the reason is small repeating bounces.

When the box is on a moving platform, the box moves because the platform repeatedly collides with the box as it moves up, and each collision bounces the box away and upward a little, and then it falls down again because of gravity.

quinton-ashley commented 1 year ago

Ah okay so it is on purpose then to handle that scenario. I see.