【问题标题】:Libgdx - strange behavior of two Actors(hero and coins) after they collideLibgdx - 两个演员(英雄和硬币)碰撞后的奇怪行为
【发布时间】:2015-10-28 11:15:37
【问题描述】:

我目前正在创建一个 2d 无限奔跑游戏。在我的游戏中,我有两个演员,英雄和硬币,他们的身体都设置为动态,但每次英雄击中/碰撞硬币时,英雄都会向左移动(英雄会受到硬币的影响)。

这是我的代码,我不知道我的设置是否有问题,任何帮助,将不胜感激。

public static Body createRunner(World world) {
        BodyDef bodyDef = new BodyDef();

        bodyDef.type = BodyDef.BodyType.DynamicBody;
        bodyDef.position.set(new Vector2(Constants.RUNNER_X, Constants.RUNNER_Y));
        PolygonShape shape = new PolygonShape();
        shape.setAsBox(Constants.RUNNER_WIDTH / 2, Constants.RUNNER_HEIGHT / 2);
        Body body = world.createBody(bodyDef);
        body.setGravityScale(Constants.RUNNER_GRAVITY_SCALE);    
        body.setUserData(new RunnerUserData());  
        FixtureDef fixtureDef = new FixtureDef();
        fixtureDef.shape = shape;
        fixtureDef.density = 0.0f; 
        fixtureDef.friction = 0.0f;
        fixtureDef.restitution = 0.0f; 
        body.createFixture(fixtureDef);
        body.resetMassData();
        body.setUserData(new RunnerUserData(Constants.RUNNER_WIDTH, Constants.RUNNER_HEIGHT));


        shape.dispose();
        return body;
    }

    public static Body createCoins(World world) {
        EnemyType enemyType = RandomUtils.getRandomEnemyType();
        BodyDef bodyDef = new BodyDef();
        //bodyDef used to set the enemy to make it not be affected when the runner hit
        bodyDef.type = BodyDef.BodyType.DynamicBody;
        bodyDef.position.set(new Vector2(enemyType.getX(), enemyType.getY()));
        PolygonShape shape = new PolygonShape();
        shape.setAsBox(enemyType.getWidth() / 2, enemyType.getHeight() / 2);
        Body body = world.createBody(bodyDef);
        FixtureDef fixtureDef = new FixtureDef();
        fixtureDef.shape = shape;
        fixtureDef.density = -100.0f; 
        fixtureDef.friction = 0.0f;
        fixtureDef.restitution = -50.0f; 
        body.createFixture(fixtureDef);
        body.resetMassData();
        EnemyUserData userData = new EnemyUserData(enemyType.getWidth(), enemyType.getHeight(), enemyType.getRegions());
        body.setUserData(userData);
        shape.dispose();
        return body;
    }

谢谢

【问题讨论】:

    标签: java android libgdx game-physics


    【解决方案1】:

    我会通过 world.setContactFilterContactFilter 设置为您的世界实例

    每当您的灯具发生碰撞时,这都会给您一个回调。如果你从这个回调中返回 false,它应该会阻止 Box2D 解决你的碰撞,这很可能是推动你的英雄离开的原因。

    ContactListener listener = new ContactListneer(){
        public boolean shouldCollide(Fixture fixtureA,
                      Fixture fixtureB){
            Body bodyA = fixtureA.getBody();
            Body bodyB = fixtureB.getBody();
            /* !!! You'll need to implement this next part !!!*/
            //figure out which body is hero, which body is coin
            //if this is a collision between a hero and coin....
            {
                world.destroyBody(coinBody);
                return false;
            }
            //else
                return true;
        }
    };
    world.setContactFilter(contactFilter);
    

    【讨论】:

    • 先生,您能指导我吗?我在课堂上实现了 ContactListener 并确定了哪种体型应该发生碰撞,但是硬币一直在推动英雄,这是我的代码
    • @Override public void beginContact(Contact contact) { Body a = contact.getFixtureA().getBody();身体 b = contact.getFixtureB().getBody(); if ((BodyUtils.bodyIsRunner(a) && BodyUtils.bodyIsCoins(b)) ) { // runner.hit();硬币.remove(); world.destroyBody(b); } else if ((BodyUtils.bodyIsRunner(a) && BodyUtils.bodyIsGround(b)) || (BodyUtils.bodyIsGround(a) && BodyUtils.bodyIsRunner(b))) { runner.landed(); } }
    • 对不起,我是菜鸟,但我真的不知道应该把world.setContactfilter()放在哪里
    猜你喜欢
    • 2016-07-13
    • 1970-01-01
    • 2013-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-31
    • 1970-01-01
    相关资源
    最近更新 更多