2015-06-16 69 views
0

我想創建一個簡單的FlappyBird克隆,而不是一隻鳥 - 那裏有一頂帽子。Java/Android - libGDX/box2d - 身體速度很低

到目前爲止,代碼很簡單:屏幕中央的一個帽子(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(); 
} 

回答

0

你可能要修復的步驟是這樣的:

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 
     } 
    } 
} 

這可能有助於速度。