help with disabling contacts

we're working on a 2D game where objects are allowed to overlap initially, and then as soon as they become non-overlapping they will from that point forward be allowed to collide with each other. this is so that if you spawn an object in the middle of an existing object, they won't pop apart but rather peacefully coexist until they naturally separate and then collide in the future as expected.

i've implemented this by having a bit field that tells you the current collision status between any 2 objects. then, in my b2ContactListener::PreSolve method i just enable or disable the contact according to the status of the relevant bit. this works great most of the time but i've found that it doesn't successfully disable the contact 100% of the time.

the problem seems to be this: imagine you have 3 circles, A, B and C, floating in space in a row. each circle overlaps the circle next to it just slightly so that there are 2 contacts - 1 between circles A and B, and 1 between circles B and C. all my collision bits are clear so both contacts are constantly being disabled - this allows the circles to remain overlapped as expected. now, all 3 circles become stationary and so all 3 bodies go to sleep. because the bodies are sleeping, the contacts are no longer updated. at this point, both contacts become enabled again at the top of b2World::SolveTOI when all contacts in the world get re-enabled. the circles are still overlapped however because the bodies are sleeping and so they don't participate in b2World::Solve.

now, consider what happens as you pull circle C away from circles A and B so that circles B and C become separated. circle C wakes up because you're moving it and so the contact between circles B and C is now being updated again. at this point there are 2 things that can happen at the moment circles B and C separate depending on the order the contacts are evaluated inside b2ContactManager::Collide. in the good case, the contact between B and C is updated first which will wake up circle B when it sees that it is no longer touching inside b2Contact::Update. the contact between A and B would then be allowed to update second because now body B is awake and it would disable itself according to my collision bits. when things happen in this order, everything behaves as expected. in the bad case, the contact between A and B is processed first and it is not updated at all because both A and B are sleeping. because the contact is not updated, it is left in an enabled state because all contacts are continually re-enabled at the top of b2World::SolveTOI. then, the contact between B and C is updated, it wakes up body B as before, and now body B will participate in b2World::Solve while its contact with body A is enabled. this causes circles A and B to lurch apart for a single Tick until the next frame in which the contact between A and B will be updated and disable itself.

i'd love to get some recommendations from people more familiar with box2d as to how i might be able to deal with this issue. perhaps there's a better way to manage the collision response between all of my items that doesn't involve constantly disabling the contacts? maybe there's something i can do to avoid the race condition when my contacts are getting updated?

thanks so much for any and all feedback!
I looked at the latest C++ code. b2Contact::e_enabledFlag is only applied in b2Contact::Update which calls your listener after setting the flag. Maybe the problem is that the b2Contact constructor enables the contact. Try changing that.
sorry, i should have clarified we're using release 2.1.2 of box2d. i've grabbed the latest code and the conditions under which contacts are enabled do appear to be quite different. i'll do some tests and see if the issue i'm seeing is resolved using the latest.

in general, would you recommend sticking to major release versions for use in large projects or are the day to day svn snapshots usually just as good?
Generally you should look at the submission comments. If I say that the code is unstable, then you should hold off. Otherwise, the code is generally stable. I am quick to fix any crash bugs.
thanks, upgrading to the latest code did in fact solve the problem we were having.

Original thread: https://box2d.org/forum/viewtopic.php?t=5844