簡體   English   中英

Java / Android-libGDX / box2d-主體速度較低

[英]Java/Android - libGDX/box2d - body has low velocity

我試圖創建一個簡單的FlappyBird克隆,而不是一只鳥,而是一頂帽子。

到目前為止,代碼很簡單:位於屏幕中心的帽子(Sprite obj。)為position.x等於camera.x(因為照相機移動,帽子也隨之“移動”),帽子的位置由動態物體影響,該物體通過重力和向上的力移動。

問題是:帽子太慢,我什至將世界向量設置為0,500,但它仍然走得很慢+“向上”力甚至更慢,因此程序中對象的總速度非常低,我我們嘗試了許多不同的方法來進行更改(更改PPM,增加力量大小等)

這是代碼:

final float PIXELS_TO_METERS = 100f;
    // The first function to launch, runs only once.
@Override
public void create () {
    camera = new OrthographicCamera(288, 497);
    batch = new SpriteBatch();

    bg = new Texture("bg.png");
    hat = new Sprite(new Texture("hat.png"));
    ground = new Texture("ground.png");

    world = new World(new Vector2(0, -9.18f), true);

    BodyDef bodyDef = new BodyDef();
    PolygonShape shape = new PolygonShape();
    FixtureDef fdef = new FixtureDef();

    bodyDef.type = BodyDef.BodyType.DynamicBody;
    bodyDef.position.set(hat.getX(), hat.getY());
    bhat = world.createBody(bodyDef);

    shape.setAsBox(hat.getWidth()/PIXELS_TO_METERS,
      hat.getHeight()/PIXELS_TO_METERS);
    fdef.shape = shape;
    bhat.createFixture(fdef);

}

@Override
public void render () {
    cameraupdate();

    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    worldupdate();
    worlddraw();

    batch.setProjectionMatrix(camera.combined);
}

public void worldupdate() {
    if(fTouch)  
        world.step(Gdx.graphics.getDeltaTime(), 6, 2);

    if(Gdx.input.justTouched()) {
        if(fTouch == false)
            fTouch = true;
        bhat.applyForceToCenter(new Vector2(0, 30), true);
    }
}

public void worlddraw() {
    batch.begin();
    batch.draw(bg, camera.position.x-144, -268,  bg.getWidth(),
    bg.getHeight()+20);

    batch.draw(hat, camera.position.x, bhat.getPosition().y);
    for (int i = 0; i < 4000; i++) 
        batch.draw(ground, i * ground.getWidth() - 287, -250);
    batch.end();
}

您可能需要將步驟修復為:

public static float TIME_STEP = 1/500;
public float accumulator = 0

public void worldupdate() {
    if(fTouch) {
       this.accumulator += Gdx.graphics.getDeltaTime();
       while (this.accumulator >= TIME_STEP) {
           world.step(TIME_STEP, 6, 2);
           this.accumulator -= TIME_STEP
       }
    }
}

這可能會提高速度。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM