2016-11-17 37 views
0

我畫精靈uppon機構,用此方法(我的身體裏面的包裝):對人體畫精靈 - 攝像頭查看端口和調整

private void drawBaseSprite(Batch batch){ 
     Sprite baseSprite = this.getBaseSprite(); 

     Vector3 bodyPixelPos = camera.project(new Vector3(body.getPosition().x, body.getPosition().y, 0)); 

     // TODO: 17/11/16 Review this 
     float w = scale * (baseSprite.getTexture().getWidth())/camera.zoom; 
     float h = scale * (baseSprite.getTexture().getHeight())/camera.zoom ; 

     baseSprite.setSize(w,h); 
     baseSprite.setOrigin(w/2, h/2); 
     baseSprite.setPosition(bodyPixelPos.x -w/2, bodyPixelPos.y - h/2); 
     baseSprite.setRotation(body.getAngle() * MathUtils.radiansToDegrees); 


     baseSprite.draw(batch); 
    } 

一切都很好,直到我嘗試調整Windows。嗯,我跟隨此調整大小邏輯(實施屏幕):

@Override 
public void resize(int width, int height) { 
    stage.getViewport().update(width, height, true); 
    stage.camera.setToOrtho(false, Constants.VIEWPORT_HEIGHT *width/(float)height, Constants.VIEWPORT_HEIGHT); 
} 

調整大小前: before resize 調整大小(大寬度)後: after resize (larger width)

我覺得這是荒謬的,因爲這樣的:

 float w = scale * (baseSprite.getTexture().getWidth())/camera.zoom; 
     float h = scale * (baseSprite.getTexture().getHeight())/camera.zoom ; 

不會改變,而圖像是x-scaled。

回答

0

我正確理解你的問題是圖像不能停留在身體上方嗎?

如果是這樣,它沒有的原因是您設置的圖像相對於相機的大小,而不是它應該覆蓋的身體。當相機的變焦被改變時,您調整圖像的大小,但不調整主體,並且它們不同步。

我會建議改變你的邏輯,使得圖像的高度/寬度完全由身體決定,而你的身體根據相機縮放進行縮放。

+0

謝謝回答,但我跟蹤的子畫面尺寸: Gdx.app.log(this.getClass()getSimpleName(), 「W: 」+ W +「 H」。+ H); 並且即使在調整大小之後它們也不會改變,因爲相機的縮放也不會改變(僅查看端口改變)。 – neogineer

0

嗯,這解決了我的問題,但我仍然不知道爲什麼。

private void drawBaseSprite(Batch batch){ 
     Sprite baseSprite = this.getBaseSprite(); 
     batch.setProjectionMatrix(camera.combined); 
     float someScale = 0.1f; 

     float w = scale * (baseSprite.getTexture().getWidth())/camera.zoom *someScale; 
     float h = scale * (baseSprite.getTexture().getHeight())/camera.zoom *someScale; 


     Vector3 bodyPixelPos = camera.project(new Vector3(body.getPosition().x, body.getPosition().y, 0)) 
       .scl(someScale*camera.viewportHeight/(Gdx.graphics.getHeight()/20f)).sub(w/2, h/2, 0); 

     baseSprite.setSize(w,h); 
     baseSprite.setOrigin(w/2, h/2); 
     baseSprite.setPosition(bodyPixelPos.x, bodyPixelPos.y); 
     baseSprite.setRotation(body.getAngle() * MathUtils.radiansToDegrees); 

     baseSprite.draw(batch); 
    }