【问题标题】:Zooming to Mouse position in LWJGL在 LWJGL 中缩放到鼠标位置
【发布时间】:2015-07-20 22:43:08
【问题描述】:

我正在尝试使用 LWJGL 实现对等距地图的缩放。目前我有功能

public static void setCameraPosition(float x, float y) {

    x *= zoom;
    y *= zoom;

    cameraX = x;
    cameraY = y;

    x -= (Display.getWidth() / 2) * zoom;
    y -= (Display.getHeight() / 2) * zoom;

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    GLU.gluLookAt(x, y, 1f, x, y, 0, 0.0f, 1.0f, 0.0f);
}   

将相机中心设置为一个点 (x, y),

public static Point getMouseCoordinates() {
    float x = Mouse.getX() * getZoom() + getCameraLeft();
    float y = (Display.getHeight() - Mouse.getY()) * getZoom() + getCameraTop();
    return new Point((int) x, (int) y);
}

返回当前鼠标坐标,和

public static void setZoom(int newZoom) {

    if (newZoom >= 4) newZoom = 4;
    else if (newZoom <= 1) newZoom = 1;

    if (zoom == newZoom) return;

    float x = ?; <-----
    float y = ?; <-----

    zoom = newZoom;

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0,  0+Display.getWidth() * zoom , 0+Display.getHeight() * zoom, 0, 1, -1);
    setCameraPosition((int) x, (int) y);
}

应该将缩放设置为 1 到 4 之间的整数值。如您所见,我想在将缩放更改为某个点后设置相机位置 - 并且需要计算该点以便当前鼠标位置不会改变(也就是放大到鼠标位置,例如谷歌地图所做的)。我已经尝试了 2 天了,我尝试了很多东西,但我就是无法弄清楚计算 x 和 y 的方程式。

请注意,所有返回和输入的点都是相对于地图的位置,特别是相对于地图的顶部(其上角点为 (0, 0))。 getMouseCoordinates() 函数中的 getCameraLeft() 和 getCameraTop() 值返回

public static float getCameraLeft() {
    return cameraX - zoom * (Display.getWidth() / 2);
}

public static float getCameraTop() {
    return cameraY - zoom * (Display.getHeight() / 2);
}

.

任何帮助将不胜感激。我希望,我没有把自己表达得太复杂。

【问题讨论】:

    标签: java opengl lwjgl


    【解决方案1】:

    我终于找到了正确的方程式:

            float x = getMouseCoordinates().getX() + (getCameraX() - getMouseCoordinates().getX()) * (float) newZoom / (float) zoom;
            float y = getMouseCoordinates().getY() + (getCameraY() - getMouseCoordinates().getY()) * (float) newZoom / (float) zoom;
    

    无论如何,谢谢你,我相信最终会有人给我正确的答案:)

    【讨论】:

      猜你喜欢
      • 2020-05-28
      • 2013-03-26
      • 2015-04-20
      • 2014-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-18
      • 2017-02-11
      相关资源
      最近更新 更多