0

In the game I'm developing, I have bodies (from the box2d extension) flying around. Some of these bodies collide with each other. The problem is that for every single contact the beginContact method in contactListener get called twice (at least).

Both the bodies that are colliding have the maximum elasticity of 1.

Is there a way to make the collision happen exactly once, i.e., have beginContact called only once for every contact?

asherbret
  • 5,439
  • 4
  • 38
  • 58

2 Answers2

0

Turns out I had created two identical bodies behind one another by mistake. Obviously, this caused the beginContact method to be called twice for each collision.

This must be one of the sneakiest bugs I have ever had.

asherbret
  • 5,439
  • 4
  • 38
  • 58
0

I had an issue that matches your question as phrased (except with plain Box2D in C++, though that issue is generic to any Box2D binding), but the cause was different.
Two shapes that should have kept colliding registered an EndContact and a BeginContact very shortly after. The reason was that one of the two shapes had setType called on its body shortly after the collision began.
When setType actually changes the body type, any involved contacts are deleted and recreated. This causes the contact callbacks to be invoked again.
It appears that older Box2D versions did not have this behavior.

Placing a breakpoint on EndContact and inspecting the call stack can be helpful to debug this issue.

asu
  • 1,875
  • 17
  • 27