Random dynamic bodies missing collision detection

So I now have a new problem that hopefully someone can help me with.

This doesn't happen for all dynamic bodies but for some rare cases, when I create a player (creates a body), the dynamic body falls because of gravity but hits no platforms/etc. Its collision detection is just gone completely. I've tested it out to make sure that it isn't being called and its not. I've set the body to not allow sleeping, set it to active, made sure it was dynamic, etc etc. It ends up falling and never stopping (the y coordinate in the world keeps increasing, tested).

So does anyone have any idea why a dynamic body could be in the world, have gravity, but not be able to detect collisions? Note: I make bodies through the same exact method and most of the time it works, these are rare occurrences but ones that need to be fixed. Any help would be so appreciated.

Thank you.

Code: Select all

      BodyDef playerDef = this.playerDef();

      Body body = null;
      while (body == null){
         body = world.getWorld().createBody(playerDef); // Adds body to physics world
      }
      _body = body; // Sets global _body to body
      _body.setSleepingAllowed(false);
      _body.setUserData(this);

      PolygonShape dynamicBox = new PolygonShape();
      dynamicBox.setAsBox(15, 15);

      FixtureDef fixtureDef = new FixtureDef();
      fixtureDef.shape = dynamicBox;
      fixtureDef.density = 1.0f;
      fixtureDef.friction = 0.0f;      
      fixtureDef.restitution = 0.0f;

      body.createFixture(fixtureDef);


Here is the code. Thank you.
Are you calling the method from the same thread that calls world.step?
I wish. The method is called when a player joins the server, as of now I have hacked around the situation by providing a counter measure and removing the body then adding a new one in that works.

IF by chance the body was being made during world.step (aka in a diff thread but at the same time/world is locked), could that possibly be why? I thought the body would not be made in that case. In this case, the bodies are actually being made (body count increases) but the body defies all collision detection but keeps gravity.

THANKYOU for the help. Really struggling.
Yes, that's probably the reason why. I'm surprised you haven't run into other problems as well haha. You'll need to cache these actions so your update thread (which is calling world.step) can apply them before or after the world.step call. Same thing with contact listeners.
Ahh.. thats weird.. I've been making sure to wait for the world to be unlocked before calling createBody and I thought that was the only thing that needed to be done. So instead I should cache everything and then make the bodies during the update method? Thanks for the help, I guess I was just a little unclear on the design of box2d.
Well, that's what I'm guessing, since I've never heard of this issue before. The only other reason I could think of that this would happen is if you had a bug with how your set your filters

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