0
votes

In my game I use lot's of Box2D bodies which contains more than one fixtures. And also I use Contact Listener to detect the collision between different bodies. Unfortunately, I've got one terrible problem with it. When bodies collide, Contact Listener detect it more than one times. How to debounce Contact Listener? My log:

...
postSolve A: brows B: bShape
endContact A: brows B: bShape
endContact A: brows B: bShape
endContact A: brows B: bShape
endContact A: brows B: bShape
endContact A: brows B: bShape
endContact A: brows B: bShape

After I assign unique user data for each fixture of my body.

New log:

...
endContact A: f-3 B: f-0
endContact A: f-4 B: f-0
endContact A: f-5 B: f-0
endContact A: f-7 B: f-0
2

2 Answers

1
votes

I do not know how your fixtures are set up, but it is likely that your restitution may be the problem (bounciness). Try increasing / decreasing it. This would decrease the likelihood of your bodies bouncing very slightly apart so quickly.

0
votes

I found the solution for my problem. I added one variable, which contains the state of collision. Code:

public class CollisionController implements ContactListener {

private boolean collided;

...


@Override
public void beginContact(Contact contact) {
    collided = false;
}

@Override
public void endContact(Contact contact) {
    if (!collided && contact.getFixtureA().getUserData() != null && contact.getFixtureB().getUserData() != null) {
        // CODE
        collided = true;
    }
}

...