2
我有我爲LibGdx OpenGL項目編寫的以下Java類。 無論您如何通過letterboxing頂部和底部或側面來調整屏幕大小,相機都會保持屏幕的寬高比。到現在爲止還挺好。如何計算這個班的信箱大小?
當我嘗試獲取鼠標點擊的x,y座標,並且信箱涉及該軸時,問題就出現了。 首先這裏是類:
public class Camera {
private static float viewportWidth;
private static float viewportHeight;
private static float aspectRatio;
private static float barSize;
/**
* Creates an orthographic camera where the "play area" has the given viewport size. The viewport will be scaled to maintain the aspect ratio.
*
* @param virtualWidth the width of the game screen in virtual pixels.
* @param virtualHeight the height of the game screen in virtual pixels.
* @return the new camera.
*
*/
public static OrthographicCamera createCamera(float virtualWidth, float virtualHeight) {
aspectRatio = virtualWidth/virtualHeight;
float physicalWidth = Gdx.graphics.getWidth();
float physicalHeight = Gdx.graphics.getHeight();
if (physicalWidth/physicalHeight >= aspectRatio) {
// Letterbox left and right.
viewportHeight = virtualHeight;
viewportWidth = viewportHeight * physicalWidth/physicalHeight;
barSize = ????;
}
else {
// Letterbox above and below.
viewportWidth = virtualWidth;
viewportHeight = viewportWidth * physicalHeight/physicalWidth;
barSize = ????;
}
OrthographicCamera cam = new OrthographicCamera(viewportWidth , viewportHeight);
cam.position.set(virtualWidth/2, virtualHeight/2, 0);
cam.rotate(180, 1, 0, 0);
cam.update();
Gdx.app.log("BTLog", "barSize:"+barSize);
return cam;
}
public static float getViewportWidth() {
return viewportWidth;
}
public static float getViewportHeight() {
return viewportHeight;
}
}
LibGdx提供我x和當偶數發生y座標,我需要將這些原始座標轉換成我的相機的規模(虛擬高度和寬度)。
當屏幕被拉伸(無上下黑邊的話),這是很容易通過使用獲得的X和Y座標:
xRelative = (int) (x/(float)Gdx.graphics.getWidth() * Camera.getViewportWidth());
yRelative = (int) (y/(float)Gdx.graphics.getHeight() * Camera.getViewportHeight());
問題是,當與信箱開始發揮作用,它拋出關閉該軸的座標。我知道我需要考慮信箱的寬度,但我有一段時間計算如何計算它。
上面我在哪裏「barSize = ????;」我的第一本能是做到這一點: barSize = physicalHeight - viewportHeight; //使用高度例如
一旦我得到了barSize,我確信我可以用它來得到正確的數字(例如使用Y軸):
yRelative = (int) (y/(float)Gdx.graphics.getHeight() * Camera.getViewportHeight() - Camera.getBarSize());
但數字不匹配。任何建議,將非常感激!