0

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.

4

1 回答 1

1

根据我的观点,我在这方面看到了很多错误。

首先,您正在创建具有 (0,0,0) 之类的固定属性的实体。所以这段代码创建了什么样的主体,我无法理解!

首先使这件事正确并根据要求应用适当的值。

如果您的假设是破坏 body,那么为此编写代码意味着不要使用 active 方法。

您必须在线程中编写销毁代码,以便它独立执行。所以你必须维护销毁体的标志,这样同一个线程就不会调用两次。

编辑:像这样创建物理连接器,

physicsWorld.registerPhysicsConnector(new PhysicsConnector(testSprite, testBody));
于 2013-09-24T03:17:31.310 回答