繁体   English   中英

libgdx和Box2d碰撞优化

[英]libgdx and Box2d collision optimisation

我是Libgdx的新手,对碰撞/运动的最佳方法有些困惑。 这让我很困扰,除非我知道我没有做错方法,否则我似乎无法继续前进。

这是我目前正在使用的课程。

public class Play extends GameState{
    public Play(GameStateManager {
        super(gsm);
        world = new World(new Vector2(0, -9.81f), true);
        b2dr = new Box2DDebugRenderer();

        cl = new MyContactListener();
        world.setContactListener(cl);

        createPlayer();

        b2dCam = new OrthographicCamera();
        b2dCam.setToOrtho(false, Game.V_WIDTH / PPM, Game.V_HEIGHT / PPM);
    }

    public void update(float dt) {

        handleInput();

        player.update(Gdx.graphics.getDeltaTime());

        world.step(dt, 6, 2);
    }

     public void render() {
        Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);

        cam.update();

        tmr.setView(cam);
        tmr.render();

        sb.setProjectionMatrix(cam.combined);
        player.render(sb);

        b2dr.render(world, b2dCam.combined);
    }



    public void handleInput() {
        if(MyInput.isPressed(MyInput.BUTTON1)){
            if(cl.isPlayerOnGround()){
                player.getBody().applyForceToCenter(0, 150, true);
            }
        }



        if(MyInput.isDown(MyInput.BUTTON2)){
                if(cl.isPlayerOnGround())
                 player.setVelocity(new Vector2(1, player.getVelocity().y));
                else if(Math.abs(player.getVelocity().y) < .3)
                    player.getBody().applyLinearImpulse(.15f, 0, player.getPosition().x, player.getPosition().y, true);
        }else{
            if(MyInput.isDown(MyInput.BUTTON3)){
                    if(cl.isPlayerOnGround()) {
                        player.setVelocity(new Vector2(-1, player.getVelocity().y));
                    }else if(Math.abs(player.getVelocity().y) < .3){
                            player.getBody().applyLinearImpulse(-.15f, 0, player.getPosition().x, player.getPosition().y, true);
                    }
            }else{
                if(cl.isPlayerOnGround())
                    player.setVelocity(new Vector2(0, player.getVelocity().y));
            }
        }
    }
}

我的播放器类对此进行了扩展

public class AnimationHandler {
protected Body body;
protected Animation animation;
protected float width;
protected float height;

private float time;



public AnimationHandler(Body body){
    this.body = body;
    animation = new Animation(1/12f);
}

public void setAnimation(TextureRegion[] region, float delay){
    animation = new Animation(delay, region);
    width = region[0].getRegionWidth();
    height = region[0].getRegionHeight();
}

public void render(SpriteBatch sb){
    time += Gdx.graphics.getDeltaTime();

    sb.begin();
    sb.draw(
            animation.getKeyFrame(time, true),
            body.getPosition().x * B2DVars.PPM - width / 2,
            body.getPosition().y * B2DVars.PPM - width / 2
    );
    sb.end();
}

public Body getBody() {
    return body;
}

因此,我主要要看的是第一类(播放)方法handleInput()。 我目前正在使用player.setVelocity()作为运动。 这对我来说是自动为左右行走进行碰撞处理。 我承认,它运作良好。 但是我希望有人对此发表意见,我一直感觉我的做法存在缺陷。 我正在考虑的一个缺陷是,如果我有一款平台游戏,并且想跳上平台,但是当我掉下来时又落在平台上,我不确定这是可能的。因为它本身在进行碰撞而不是手动进行碰撞正在做。 有什么想法吗? 任何输入都会受到感激,我是游戏编程的新手,我想以最好的方式做事。

我想说Box2D非常适合制作平台游戏(根据我的经验知道)。 这里描述了一个很好的起点: http : //www.badlogicgames.com/wordpress/?p=2017

另外,请尝试防止创建不必要的新对象以降低gc压力:player.setVelocity(new Vector2(1,player.getVelocity()。y));

每次都会创建一个新的Vector2,并且应避免使用它,因为它会产生垃圾。 在这种情况下,body.setLinearVelocity(vx,vy); 会更好

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM