b2ContactListener - Add-Remove-Discrepancy?

Hi there,

I am experiencing problems with my implementation of the b2ContactListener. I am trying to keep track of whether a character touches certain kinds of objects. In order to do that I use a counter that increments with each Add-event and decrements with each Remove. Apparently it is working alright, up to a point where my character hits two things simultaneously.

Is that a known problem? Of course there might be a problem in my code, but haven't found one so far...

Cheers

Toby
I am trying something similar.
Could you please tell me how you set up the add listener? I have no idea on what I should do (add some lines on my world creation, edit the b2ContactListener, etc).

Also, you should paste the code you are using on your counter.
Uchiha wrote:
I am trying something similar.
Could you please tell me how you set up the add listener? I have no idea on what I should do (add some lines on my world creation, edit the b2ContactListener, etc).

Also, you should paste the code you are using on your counter.

That's a lot of code.

What you want to do is basically extend b2ContactListener...
Code:
public class GameContactListener extends b2ContactListener


...and then instantiate that class.
Code:
world = new b2World(worldAABB, grav, false);
contactListener = new GameContactListener();
world.SetContactListener(contactListener);


I don't know what would be the best way to do the counter, but I am dispatching events from the Add-method to the objects involved in the contact, and process them there...

Cheers,
Toby
Try to use array, which will hold references to bodies contacted with your character, instead of counter. Of course, you will need a little function to check is body already in array or not. Length of the array will show how much bodies contacted with your character.

sorry for my bad English
Hi Toby.
Counting Adds and Removes is an fine technique. As far as I know, the events system is working correctly. What version are you using, some previous ones were broken. Are you sure the calls are not balancing at the end of each step; sometimes you can get odd transitory effects.
Thank you for all your replies. The error was in my code, of course.

Here's what I've got so far:

http://playground.eplay.de/box2d/gameengine/demos/001/


Sort of hacky at the moment... 🙂

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