Let’s say you have a number of small physics objects on the screen and one is being moved fairly rapidly. Let’s say that these are small objects and one of them is being moved with a touch joint. Let’s also say that when these objects collide with each other, they will get connected by a stretchy joint (something which allows them to move apart) and when they stop colliding, that joint will get removed.
So, in short: objects collide, collision listeners fire, joint is created, very soon after the objects stop colliding, then joint is removed.
Now, as we all know, during the collision event the bodies cannot be affected because the physics engine locks the Box2D world - this means that during the began and ended phases we cannot add the joint to either body.
So, as per the usual recommendation, we need to use a timer(.performWithDelay) to wait a moment and then add the joint.
The problem with this is that if the two bodies collide and separate quite quickly the first timer may not fire fast enough, causing the ended timer to fire first (due to race conditions.) This could cause a joint to be created and not be removed. The problem gets worse if the collisions can happen between many objects, because then the addition and removal of joints needs to be managed in a list, during locked began and ended phases, with non-guaranteed timers.
So, while I’m working on my solution (which, frankly, I thought I’d solve a number of times already and been proven wrong, again)…
What’s your solution?
Have you noticed a problem with my logic?
Or maybe: is this a new problem to you?
Thanks,
Matt