簡體   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