Box2D Java

mh114 wrote:I think that whole if-statement will need to go, as it's for the liquids -- and I don't see the reason to keep the dependency as LiquidTest is not included in the lib anyway..

Sorry again, you should remove that if and it should work again.. 🙂


It's quite all right, it was a problem that needed to be fixed anyways 🙂

Edge wrote:Found it. In World.drawShape, there is code that assumes if userData is non-null that it must be the LiquidTest app. I assume this is mh114's change.


I have it fixed. I didn't remove it, instead I did a pretty dirty solution of just copying over the liquid test user data, and using that to check to see if it's a liquid. It's going to be dirty until we fully integrate liquid into the engine.

Also, I made the liquid drawing look better. Now the lines go along the liquid's velocity 😛
No worries. Thanks for fixing.

Since we're on the topic of debug drawing, what did you think of my suggestion to draw the sensors in yellow?
Good to know that nobody went out of business due to my stupid mistake.. 😉

Another (tiny) improvement that springs to mind would be making all the color constants (and the new LIQUID_INT Integer too) in World.java static final. This would save a bit of memory, although having several World-instances is perhaps not the most common use case.. 😛

In any case, thanks again for your work on JBox2D ewjordan and toucansam, it just keeps getting better and better. 🙂
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.0


The 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?
Edge wrote: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.


This is because manifolds have a maximum of 2 points (unless you changed it in the settings), and it will iterate over those two points and usually produce 2 points per manifold. Also, don't go off of the memory location of the ContactID as it is reused for all the manifold points.

It look like you're either getting duplicates from that, or it's because you report each contact twice:

Code: Select all

         Object shape1Data = point.shape1.getUserData();
         if (shape1Data instanceof ShapePropsExt) {
            ShapePropsExt s = (ShapePropsExt)shape1Data;
            if (s.handler != null) s.handler.onStart(point.shape2, point); <-numba one
         }
         Object shape2Data = point.shape2.getUserData();
         if (shape2Data instanceof ShapePropsExt) {
            ShapePropsExt s = (ShapePropsExt)shape2Data;
            if (s.handler != null) s.handler.onStart(point.shape1, point); <-numba two
         }



Edge wrote: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?


Also, the two contact points might be in the same spot.


I'm really not positive though, and this could probably be answered better in general discussion :/
It look like you're either getting duplicates from that, or it's because you report each contact twice


It is possible for two different handlers to get notified for a single contact with the code I have, but not the same shape handler. To double check that I hadn't mistyped something, I also put a debug at the top of BaseContactListener to verify that it was getting notified twice(and it was).

Thank you for looking into. I'll probably just change designs and revisit once the new Box2D contact handling is ported. Even without the double notification, you can still get a multiple contacts for the same set of shapes (different contact points), so I have to handle that anyway. It was really more the normals changing between the add and remove that I couldn't correlate that causes my problems. Sensors shouldn't have this issue though. They are more boolean in usage.
Hi guys,

I just updated to the latest JBox2D code to keep up with you.
The old version worked fine (I think I checked it out in march) but with the current code I get AssertionErrors all the time:

Code: Select all

java.lang.AssertionError
        at org.jbox2d.collision.Distance.ProcessThree(Distance.java:196)
        at org.jbox2d.collision.Distance.DistanceGeneric(Distance.java:296)
        at org.jbox2d.collision.Distance.DistancePC(Distance.java:491)
        at org.jbox2d.collision.Distance.distance(Distance.java:636)
        at org.jbox2d.collision.TOI.timeOfImpact(TOI.java:85)
        at org.jbox2d.dynamics.World.solveTOI(World.java:849)
        at org.jbox2d.dynamics.World.step(World.java:560)
[...]
java.lang.AssertionError
        at org.jbox2d.collision.Distance.ProcessThree(Distance.java:155)
[...]
java.lang.AssertionError
        at org.jbox2d.collision.Distance.ProcessThree(Distance.java:156)
[...]
java.lang.AssertionError
        at org.jbox2d.collision.Distance.ProcessThree(Distance.java:162)
[...]
(apart from the first line the stacktraces are identical so I left the rest out)

Is it possible that with the latest optimizations JBox2D lost the capability of running multiple worlds in parallel?
It looks like that to me because I'm running dozens of Box2D worlds in parallel but when I reduce it to one world the problem does not seem to occur.

I would be very happy if the old behaviour could be restored because otherwise I'm stuck to the old code for all times (since my application relies on multiple worlds).
I'm not too familiar with the Box2D internals and thus thankful for any hint on this issue.
okay, the static stuff in Distance.java

Code: Select all

   // These are used to avoid allocations on hot paths:
   private static final Vec2 p1s[] = new Vec2[3];
   private static final Vec2 p2s[] = new Vec2[3];
   private static final Vec2 points[] = new Vec2[3];
   private static final Vec2 v = new Vec2();
   private static final Vec2 vNeg = new Vec2();
   private static final Vec2 w = new Vec2();
   private static final Vec2 w1 = new Vec2();
   private static final Vec2 w2 = new Vec2();
   
   static{
      for (int i=0; i<3; ++i) {
         p1s[i] = new Vec2();
         p2s[i] = new Vec2();
         points[i] = new Vec2();
      }
   }

makes it pretty clear that the engine does not work multi-threaded anymore.
Imho JBox2D loses an important feature with this.
On the other hand I do understand your wish to reduce allocations.
Maybe it would be possible to introduce a boolean parameter "multithreaded" which, if set, would make JBox2D use one Distance instance per World object?
Is this the only place where this flag would have to be handled or are there more places like this in the new version?
Boing wrote:okay, the static stuff in Distance.java
makes it pretty clear that the engine does not work multi-threaded anymore.
Imho JBox2D loses an important feature with this.

I'll take a look into this today, we may be able to work around some of these issues. Officially, the engine doesn't support multiple worlds running in parallel, but I can see how it would be useful (once I get a machine with more than two cores, it's something I'll likely want to do, as well), so I'll give it a shot and see if I can get it working right.
thanks, ewjordan.
tell me if I can help.

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