我如何计算这个类的信箱大小?

发布于 2024-11-02 16:41:30 字数 2407 浏览 1 评论 0原文

我为 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 坐标:

xRelative = (int) (x / (float)Gdx.graphics.getWidth() * Camera.getViewportWidth());
yRelative = (int) (y / (float)Gdx.graphics.getHeight() * Camera.getViewportHeight());

问题是当信箱发挥作用时,它会丢失该轴的坐标。我知道我需要考虑信箱的宽度,但我花了很长时间才弄清楚如何计算它。

上面我有“barSize = ??????;”我的第一直觉是这样做: barSize = 物理高度 - 视口高度; // 例如使用高度

一旦我得到了 barSize,我相当确定我可以使用它来获取正确的数字(例如使用 y 轴):

yRelative = (int) (y / (float)Gdx.graphics.getHeight() * Camera.getViewportHeight() - Camera.getBarSize());

但是数字不匹配。任何建议将非常感激!

I have the following Java class I've written for a LibGdx OpenGL project.
The camera keeps the aspect ratio of the screen no matter how you resize it by letterboxing either the top and bottom, or the sides. So far, so good.

The issue comes when I try to obtain the mouse x, y coordinates of a click, and the letterbox is involved for that axis.
First here is the class:

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 supplies me the x and y coordinates when an even happens, and I need to translate these raw coordinates into the scale of my camera (the virtual height and width).

When the screen is stretched (no letterboxing at all), it's pretty easy to obtain the x and y coordinates by using:

xRelative = (int) (x / (float)Gdx.graphics.getWidth() * Camera.getViewportWidth());
yRelative = (int) (y / (float)Gdx.graphics.getHeight() * Camera.getViewportHeight());

The problem is when the letterboxes come into play, it throws off the coordinate for that axis. I know I need to take into account the width of the letterboxing, but i'm having a hell of a time figuring how to calculate it.

Above where I have "barSize = ????;" my first instinct was to do this:
barSize = physicalHeight - viewportHeight; // to use height for example

Once I get the barSize, i'm fairly sure I can use this to get the right numbers (using the y axis for example):

yRelative = (int) (y / (float)Gdx.graphics.getHeight() * Camera.getViewportHeight() - Camera.getBarSize());

But the numbers don't match up. Any suggestions would be a really appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

硪扪都還晓 2024-11-09 16:41:30
Ray ray = camera.getPickRay(x, y);
System.out.println(ray.origin.x);
System.out.println(ray.origin.y);
Ray ray = camera.getPickRay(x, y);
System.out.println(ray.origin.x);
System.out.println(ray.origin.y);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文