Not sure if this is the proper place for this (JBox2 specific category can't come fast enough
🙂 But I don't remember having this problem with previous versions. My memory is fuzzy on this though. Anyway, the problem is I get duplicate contact notifications for every contact and I sometimes get a Remove without the Add. Here is my proof
😉ADD GROUND[1] info: org.jbox2d.collision.ContactID@2f0d54, player[0], crate[3], normal:0.0,-1.0
ADD GROUND[2] info: org.jbox2d.collision.ContactID@2f0d54, player[0], crate[3], normal:0.0,-1.0
ADD GROUND[3] info: org.jbox2d.collision.ContactID@1d95da8, player[0], crate[3], normal:-0.01586383,-0.9998742
ADD GROUND[4] info: org.jbox2d.collision.ContactID@1d95da8, player[0], crate[3], normal:-0.01586383,-0.9998742
END GROUND[3] info: org.jbox2d.collision.ContactID@1d95da8, player[0], crate[3], normal:-0.01586383,-0.9998742
END GROUND[2] info: org.jbox2d.collision.ContactID@1d95da8, player[0], crate[3], normal:-0.01586383,-0.9998742
END GROUND[1] info: org.jbox2d.collision.ContactID@193f6e2, player[0], crate[3], normal:-0.0031993303,-0.9999949
END GROUND[0] info: org.jbox2d.collision.ContactID@193f6e2, player[0], crate[3], normal:-0.0031993303,-0.9999949
ADD GROUND[1] info: org.jbox2d.collision.ContactID@1358f03, player[0], crate[3], normal:-0.0036194175,-0.99999344
ADD GROUND[2] info: org.jbox2d.collision.ContactID@1358f03, player[0], crate[3], normal:-0.0036194175,-0.99999344
ADD LEFT[1] info: org.jbox2d.collision.ContactID@61736e, player[0], crate[3], normal:-0.99999994,-0.0
ADD LEFT[2] info: org.jbox2d.collision.ContactID@61736e, player[0], crate[3], normal:-0.99999994,-0.0
END LEFT[1] info: org.jbox2d.collision.ContactID@61736e, player[0], crate[3], normal:-0.99999994,-0.0
END LEFT[0] info: org.jbox2d.collision.ContactID@61736e, player[0], crate[3], normal:-0.99999994,-0.0
END LEFT[-1] info: org.jbox2d.collision.ContactID@af72d8, player[0], crate[3], normal:-0.99999994,-0.0
END LEFT[-2] info: org.jbox2d.collision.ContactID@af72d8, player[0], crate[3], normal:-0.99999994,-0.0
ADD GROUND[3] info: org.jbox2d.collision.ContactID@9cfec1, player[0], ground[1], normal:0.0,-1.0
ADD GROUND[4] info: org.jbox2d.collision.ContactID@9cfec1, player[0], ground[1], normal:0.0,-1.0
END GROUND[3] info: org.jbox2d.collision.ContactID@11946c2, player[0], ground[1], normal:0.0,-1.0
END GROUND[2] info: org.jbox2d.collision.ContactID@11946c2, player[0], ground[1], normal:0.0,-1.0
ADD GROUND[3] info: org.jbox2d.collision.ContactID@747fa2, player[0], ground[1], normal:0.0,-1.0
ADD GROUND[4] info: org.jbox2d.collision.ContactID@747fa2, player[0], ground[1], normal:0.0,-1.0The scenario is I have 3 bodies (1 shape each) representing the ground, a crate and the player. The player is dropped on the crate on the edge and slide off to the ground. The ContactListener is on the player shape.
Here is my ContactListener subclass, which delegates a call to a shape specific listener on the player. I've verified this add method is getting called twice (not just the player's listener). I've include just the Add method below for breviety. The others are the same.
Code: Select all
public class BaseContactListener implements ContactListener {
/** Called when a new contact is added. */
@Override
public void add(ContactPoint point) {
Object shape1Data = point.shape1.getUserData();
if (shape1Data instanceof ShapePropsExt) {
ShapePropsExt s = (ShapePropsExt)shape1Data;
if (s.handler != null) s.handler.onStart(point.shape2, point);
}
Object shape2Data = point.shape2.getUserData();
if (shape2Data instanceof ShapePropsExt) {
ShapePropsExt s = (ShapePropsExt)shape2Data;
if (s.handler != null) s.handler.onStart(point.shape1, point);
}
}
...Here is the handler method on the player shape.
Code: Select all
ShapeContactHandler handler = new ShapeContactHandler() {
@Override
public void onStart(Shape other, ContactPoint point) {
float normalAngle = getAngleOfContact(point);
if (normalAngle >= CORNER_SW && normalAngle <= CORNER_SE) {
isOnGround++;
printContactInfo(point, "START GROUND", isOnGround);
} else if (normalAngle < CORNER_NE && normalAngle > CORNER_SE) {
isBlockedRight++;
printContactInfo(point, "START RIGHT", isBlockedRight);
} else if (normalAngle <= CORNER_NW && normalAngle >= CORNER_NE) {
// Contact above player. We don't track this currently.
printContactInfo(point, "START ABOVE", 0);
} else {
isBlockedLeft++;
printContactInfo(point, "START LEFT", isBlockedLeft);
}
}
...Any ideas on where I should start looking?
EDIT: I think i may be able to explain the extra Remove (End) for the left. As the player falls off the crate it tips over. So it looks like the remove contact was initially added as a ground one but shows up as a left on the remove because the contact normal changed in the time between. I assume that is why it is given a different contact id as well (same with the add/remove events above), although if so that makes it a pain in the butt for me to reconcile the add/remove. Maybe I'll give up and go back to using sensors for player's left/right/ground so I don't have to worry about angles changing... sigh. Still not sure why I get duplicate notifications though. I'll keep digging on that.
EDIT2: At least in my case the issue seems to be in PolyContact.evaluate(), although I saw it in a few other Contacts as well. It creates the ContactPoint at the top of the method and then iterates over the Manifold Points using the same cp. For each manifold point it has the possibility of calling the add (,persist, or remove) method on the contact listener, which apparently it is doing twice in all cases. This is definitely beyond my grasp of JBox2D. Is this a feature or a bug?