LibGdxOpenGLプロジェクト用に作成した次のJavaクラスがあります。カメラは、画面の上下または側面をレターボックス化してサイズを変更しても、画面のアスペクト比を維持します。ここまでは順調ですね。
この問題は、クリックのマウスの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());
しかし、数字は一致していません。どんな提案でも本当にありがたいです!