LibGdx OpenGL 프로젝트에 대해 작성한 다음 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 좌표를 얻기 위해 아주 쉽게 없습니다 다음 letterboxes가 활동하기 시작하면 문제가
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());
그러나 숫자를 어울리지 마. 어떤 제안이라도 정말 고맙겠습니다!