I am having with either my Physics World, ContactListener, or just my creation of bodies.
My issue is that I can create a body/sprite. The body and sprite are drawn correctly, and the player cannot walk through them, but collisions do not work with them. I had a method that had this problem, so to test the issue, when the player shot an arrow I created a body like this:
Sprite testSprite = new Sprite(player.getX() + 100, player.getY() + 100,
resourcesManager.wall_region, vbom);
Body testBody = PhysicsFactory.createBoxBody(physicsWorld, testSprite,
BodyType.DynamicBody, PhysicsFactory.createFixtureDef(0, 0, 0));
attachChild(testSprite);
physicsWorld.registerPhysicsConnector(new PhysicsConnector(testSprite, testBody));
testBody.setUserData(new UserData("Tile", testSprite, 100, testBody));
System.out.println(testBody);
These bodies/sprites create fine and I cannot walk through them. But the contact between these test bodies and arrows does not work correctly.
This is the collision in my ContactListener that should happen when an arrow and the tiles collide:
if (((boolean) ((UserData) x1.getBody().getUserData())
.getType().equals("Tile"))
&& ((boolean) ((UserData) x2.getBody()
.getUserData()).getType().equals("arrow"))) {
System.out.println("Tile/Arrow");
engine.runOnUpdateThread(new Runnable() {
@Override
public void run() {
x2.getBody().setActive(false);
final Sprite sprite = (Sprite) ((UserData) x2
.getBody().getUserData()).getSprite();
detachChild(sprite);
final PhysicsConnector physicsConnector = physicsWorld
.getPhysicsConnectorManager()
.findPhysicsConnectorByShape(sprite);
physicsWorld
.unregisterPhysicsConnector(physicsConnector);
}
});
}
But the weird thing is that it takes a while for this collision to register. For example, I could shoot 10 arrows at the testBody and they would just bounce off, but when I shot the 11th the above collision would happen.
Suggestions? I have been dealing with this issue for a long time and it is getting very frustrating, and I would be glad to provide more information if needed. Thanks.