2014-09-02 68 views
0

我遇到了一個問題,讓我的圖像填充整個屏幕而不伸展自己。目前我有一個正交相機設置,但問題是屏幕中間的圖像顯示的原點(0,0)。我該如何做這項工作?這是我的代碼。LibGDX - 我的圖像沒有填充屏幕的寬度和高度

public class GameScreen implements Screen { 
    SlingshotSteve game; 

    public void Menu(SlingshotSteve game){ 
     this.game = game; 
    } 

    OrthographicCamera camera; 

    SpriteBatch batch; 
    TextureRegion backgroundTexture; 
    Texture texture; 

    GameScreen(final SlingshotSteve gam) { 
     this.game = gam; 

    camera = new OrthographicCamera(800, 480); 

    batch = new SpriteBatch(); 

    Texture texture = new Texture(Gdx.files.internal("background.jpg")); 
    backgroundTexture = new TextureRegion(texture, 0, 0, 500, 500); 

} 

public void render(float delta) { 
     Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 

     camera.update(); 
     batch.setProjectionMatrix(camera.combined); 

     batch.begin(); 
     batch.draw(backgroundTexture, 0, 0, SlingshotSteve.WIDTH, SlingshotSteve.HEIGHT); 
     batch.end(); 

} 

@Override 
public void dispose() { 
     batch.dispose(); 
     texture.dispose(); 

} 

回答

0

你的Orthographic相機是如何實現的?這聽起來像你可能對「像素完美」轉換感興趣,它允許屏幕座標直接關聯到世界座標。您可以使用下面的參數來實現這個轉換。

final float lOrthoLeft = 0; 
final float lOrthoRight = pScreenHeight; 
final float lOrthoTop = 0; 
final float lOrthoBottom = pScreenWidth; 
final float lOrthoNear = Float.MIN_VALUE; 
final float lOrthoFar = Float.MAX_VALUE; 

在這一點上,它可能是你的lOrthoLeftlOrthoRightlOrthoTop和你的正投影法lOrthoBottom(-Width/2)(+Width/2)(-Height/2)(+Height/2)(或某些變化)以定位世界起源在屏幕的中心。

但是,這也與libgdx如何呈現在屏幕上有關。 OpenGL ES是libgdx背後的硬件加速圖形API,它使用一個特殊的座標系統,以屏幕中心爲原點。你會發現這些資源有助於理解如何在這些域中工作。

libgdx co-ordinate system

Displaying Graphics with OpenGL ES