About sensors and collision listener (add and remove)

I think the trick is not to rely on the comings and goings of the add/remove within a single step. I use them only to alter the counters. Then I actually do what I want to the character during the prestep (most stuff like move them left/right, etc) method or a key handler (one time things, like jumping). That way I don't care how many times the contact gets added or removed for the same set of shapes during that step as long as it averages out to the same. For example, one contact gets removed, but another is added still works out to a count of 1 when I check the status at the next step.

FYI - I see you are using circles instead of polygons. At least in the java version I've noticed my counts get wacky when I mix those in. I haven't tracked it down to my problem or the API yet though (boxes all seem to work fine, maybe it's the way I calculate the normals).


I like the graphics in your screenshots! I wish I had just a small amount of artistic talent 🙂
I do something similar to Edge, which seems to work. My player has a std::vector of structurs like:

Code:
struct ContactInfo
{
    b2Body *Body;
    int Count;
};


When I get an Add, I check to see if the body is already in the vector. If so, I increment its count, otherwise I add it with an initial count of 1. When I get a Remove, I find it in the vector and decrement its count. If its count then becomes 0 or less, I remove it.

I then examine the vector outside of the step to see what sensors the player is currently in contact with.
Paul wrote:
I do something similar to Edge, which seems to work. My player has a std::vector of structurs like:

Code:
struct ContactInfo
{
    b2Body *Body;
    int Count;
};


When I get an Add, I check to see if the body is already in the vector. If so, I increment its count, otherwise I add it with an initial count of 1. When I get a Remove, I find it in the vector and decrement its count. If its count then becomes 0 or less, I remove it.

I then examine the vector outside of the step to see what sensors the player is currently in contact with.


So, do you store contacts and collisions in every character/enemy/object?
I thought the usual way was a general data structure after calling Step.
Actually I'm still thinking how I'm going to do it.
For others looking at this thread, note that there is body.getBodiesInContact() which works very, very well for simple situations where you don't actually need to know about the contact points as such, but rather who is colliding with who.

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