AndEngine / Box2d / Java - 静态和动态体之间的碰撞检测

时间:2016-06-17 09:23:50

标签: box2d andengine collision

如果两个身体类型不同的精灵发生碰撞,我正在检查.collidesWith

使用下面的代码检测不到与动态和静态体的碰撞。我还想在碰撞后删除尸体。

当前代码:

ContactListener cl = new ContactListener() {

            @Override
            public void preSolve(Contact contact, Manifold oldManifold) {
                // TODO Auto-generated method stub

            }

            @Override
            public void postSolve(Contact contact, ContactImpulse impulse) {
                // TODO Auto-generated method stub

            }

            @Override
            public void endContact(Contact contact) {
                final Fixture x1 = contact.getFixtureA();
                final Fixture x2 = contact.getFixtureB();
            }

            @Override
            public void beginContact(Contact contact) {
                final Fixture x1 = contact.getFixtureA();
                final Fixture x2 = contact.getFixtureB();
                if (x1 != null && x1.getBody() != null
                        && x1.getBody().getUserData() != null) {
                    if (x2 != null && x2.getBody() != null
                            && x2.getBody().getUserData() != null) {
                        Log.d("TEST", "x1: "
                                + x1.getBody().getUserData().toString());
                        Log.d("TEST", "x2: "
                                + x2.getBody().getUserData().toString());

                        if (x2.getBody().getUserData().equals("Ball")
                                && x1.getBody().getUserData().equals("Dynamic")) {
                            destroy();
                        } else if (x2.getBody().getUserData().equals("Ball")
                                && x1.getBody().getUserData().equals("Static")) {
//                          ballSP.destroy();
                            // Toast.makeText(
                            // ResourcesManager.getInstance().activity,
                            // "touched static", Toast.LENGTH_LONG).show();
                        } else if (x2.getBody().getUserData().equals("Ball")
                                && x1.getBody().getUserData().equals("Figur")) {
//                          figSP.destroy();
                        }
                    }
                }
            }
        };

这是平台:

public void createPhysics() {
        if (bodyType != null) {
            FixtureDef FIXTURE = PhysicsFactory.createFixtureDef(0.0f, 0.0f,
                    1.0f);
            platformBody = PhysicsFactory.createBoxBody(physicsWorld, this,
                    bodyType, FIXTURE);
            physcConnector = new PhysicsConnector(this, platformBody, true,
                    true);
            physicsWorld.registerPhysicsConnector(physcConnector);
            physicsWorld.registerPhysicsConnector(new PhysicsConnector(this,
                    platformBody, true, true));
            this.setUserData(platformBody);
            platformBody.setLinearVelocity(physicsWorld.getGravity().x, 0);

            if (bodyType == BodyType.StaticBody) {
                platformBody.setUserData("Static");
                platformBody.setAwake(true);
            } else if (bodyType == BodyType.DynamicBody) {
                platformBody.setUserData("Dynamic");
//              platformBody.setAwake(true);
            }
        }

这就是移动球:

FixtureDef FIXTURE = PhysicsFactory.createFixtureDef(0.0f, 0, 1.0f);
        ballBody = PhysicsFactory.createBoxBody(physicsWorld, this,
                BodyType.DynamicBody, FIXTURE);
        physicsConnector = new PhysicsConnector(this, ballBody, true, true);
        physicsWorld.registerPhysicsConnector(physicsConnector);
        Vector2 shoot = new Vector2((6.78125f - 0.625f), (15.4375f - 0.625f));
        // shoot.nor().mul(4);

        scene.attachChild(this);

        // ResourcesManager.getInstance().camera.setChaseEntity(ballSP);

        ballBody.setLinearVelocity(shoot);
        ballBody.setAngularVelocity(0.5f);
        ballBody.setUserData("Ball");
        ballBody.setAwake(true);

如何检测静态和动态体之间的碰撞?碰撞后应该去除体和精灵?

1 个答案:

答案 0 :(得分:1)

确定一些事情:

1.启用球的移动:

PhysicsHandler physicsHandler = new PhysicsHandler(ballBody);
ballBody.registerUpdateHandler(physicsHandler);

<强> 2。我的样本ContactHandler

 @Override
public void beginContact(Contact contact) {
    final Fixture fixtureA = contact.getFixtureA();
    final Body bodyA = fixtureA.getBody();
    final Object userDataA = bodyA.getUserData();

    final Fixture fixtureB = contact.getFixtureB();
    final Body bodyB = fixtureB.getBody();
    final Object userDataB = bodyB.getUserData();

    GameBody givenGameBodyA = (GameBody) userDataA;
    GameBody givenGameBodyB = (GameBody) userDataB;

    LevelGameBodyType bodyTypeA = givenGameBodyA.getBodyType();
    LevelGameBodyType bodyTypeB = givenGameBodyB.getBodyType();

    if(areBodiesCollided(bodyTypeA, bodyTypeB, LevelGameBodyType.PLAYER, LevelGameBodyType.OBSTACLE)){
        Debug.v(TAG, "Player hit an obstacle!");

  }

private boolean areBodiesCollided(LevelGameBodyType givenGameBodyA, LevelGameBodyType givenGameBodyB, LevelGameBodyType wantedGameBodyA, LevelGameBodyType wantedGameBodyB) {
    return (givenGameBodyA.equals(wantedGameBodyA) || givenGameBodyA.equals(wantedGameBodyB))
            && (givenGameBodyB.equals(wantedGameBodyA) || givenGameBodyB.equals(wantedGameBodyB));

}

}

GameBody是一个接口,LevelGameBodyType是一个标识主体的枚举。 因此,在您的身体定义中,使用GameBody和Identifier Enum设置UserData。

如果这没有帮助,请尝试在每个正文的FixtureDef中使用isSensor = true。

FixtureDef FIXTURE_DEF = PhysicsFactory.createFixtureDef(0, 0.0f, 0.0f, true);

第3。删除机构:

当你在当前线程上删除你的身体时,你会遇到时间问题,所以不要这样做!

你真的应该为你的身体创建一个单独的Java类,例如: MovingBall.java等..

在这个课程中你应该有这样一个方法:

    @Override
public void removeFromPhysicsWorld() {
    isWaitingForDestruction = true;
    GameManager.get().getEngine().runOnUpdateThread(new Runnable() {
        @Override
        public void run() {
            body.setActive(false);
            physicsWorld.destroyBody(body);
        }
    });
}

在您的ContactHandlerImpl中,当机构发生碰撞时执行以下操作:

if(body.isWaitingForDestruction()){
            Debug.v(TAG, "Body waiting for destruction is " + body.getObjectId());
            return;
        }
        Debug.v(TAG, "Player collides with collectable! " + body.getObjectId());
        body.removeFromPhysicsWorld();