Andengine:PhysicsFactory.createCircleBody正在创建一个方形体..为什么?

时间:2015-04-08 20:23:36

标签: android box2d andengine

我用这个来创建一个玩家身体

private void addPlayer(){ 
        // viral -- add player
        final float startX = (CAMERA_WIDTH - this.mPlayerTextureRegion
                .getWidth()) / 2;
        final float startY = CAMERA_HEIGHT + 100;
        this.playerFace = new Sprite(startX, startY, this.mPlayerTextureRegion,
                this.getVertexBufferObjectManager());
        this.playerBody = PhysicsFactory.createCircleBody(this.mPhysicsWorld,
                playerFace, BodyType.DynamicBody, FIXTURE_DEF);
        final PhysicsHandler physicsHandler = new PhysicsHandler(playerFace);
        playerFace.registerUpdateHandler(physicsHandler);
        playerBody.setBullet(true);

        playerFaceHalfWidth = playerFace.getWidth() / 2;
        playerFaceHalfHeight = playerFace.getHeight() / 2;

        this.mScene.attachChild(playerFace);
        playerFace.setY(CAMERA_HEIGHT - playerFaceHalfHeight * 2);
    }

这就是我如何检测与其他精灵的碰撞:

protected void onManagedUpdate(float pSecondsElapsed) {
                if (playerFace.collidesWith(this)) {
                    // safe of touching border - else game over
                    if (!isPlayerInSafeZone()) {
                        Log.w("cd", "Game Over");
                        isGameInProgress = false;
                        mScene.setIgnoreUpdate(true);
                        processLevelEnd();
                    }
                }
            };

现在的问题是,即使我添加了一个圆形体,当其他精灵碰到玩家的角落时,也会与玩家一起检测到碰撞。它是一个透明区域,图像中只有一个圆圈,为其创建了主体。为什么会这样?上面的代码有什么问题?

::根据Lucaz的输入进行更新:

这是我的contactlistener

private ContactListener createContactListener()
{
ContactListener contactListener = new ContactListener()
{
    @Override
    public void beginContact(Contact contact)
    {
        final Fixture x1 = contact.getFixtureA();
        final Fixture x2 = contact.getFixtureB();
        final String x1id = (String) x1.getBody().getUserData();
        final String x2id = (String) x2.getBody().getUserData();
        Log.w("cd", "x1 = " + x1id + " --- x2 = " + x2id);
    }

    @Override
    public void endContact(Contact contact)
    {

    }

    @Override
    public void preSolve(Contact contact, Manifold oldManifold)
    {

    }

    @Override
    public void postSolve(Contact contact, ContactImpulse impulse)
    {

    }
};
return contactListener;
}

这就是我为播放器和其他精灵设置用户数据的方式:

玩家:

    this.playerFace = new Sprite(startX, startY, this.mPlayerTextureRegion,
            this.getVertexBufferObjectManager());
    this.playerBody = PhysicsFactory.createCircleBody(this.mPhysicsWorld,
            playerFace, BodyType.DynamicBody, FIXTURE_DEF);
    playerBody.setBullet(true);
    playerBody.setUserData("player");

其他精灵(球):

    body = PhysicsFactory.createCircleBody(this.mPhysicsWorld, face,
            BodyType.DynamicBody, FIXTURE_DEF);
    body.setLinearVelocity(vector2);
    body.setBullet(true);
    body.setUserData("ball");

这就是我将听众添加到物理世界的方式

this.mPhysicsWorld.setContactListener(createContactListener());

编写监听器以打印碰撞对象的用户数据:

但是当一个精灵与玩家发生碰撞时,我会为玩家获取null,但是另一个精灵的用户数据如下所示...如何解决这个问题?

x1 = null --- x2 = ball

1 个答案:

答案 0 :(得分:0)

因为collidesWith()方法正在检查sprite的冲突,而不是body。因此它会检查两个精灵是否重叠(即使它们是png图像,它们也会被视为不透明的矩形)。要检查实体之间的碰撞,还有另一种方法:

private ContactListener createContactListener()
{
ContactListener contactListener = new ContactListener()
{
    @Override
    public void beginContact(Contact contact)
    {
        final Fixture x1 = contact.getFixtureA();
        final Fixture x2 = contact.getFixtureB();
    }

    @Override
    public void endContact(Contact contact)
    {

    }

    @Override
    public void preSolve(Contact contact, Manifold oldManifold)
    {

    }

    @Override
    public void postSolve(Contact contact, ContactImpulse impulse)
    {

    }
};
return contactListener;
}

请看这个简单的教程:

http://www.matim-dev.com/handling-collisions-between-bodies.html