在 LWJGL 中缩放到鼠标位置
Zooming to Mouse position in LWJGL
我正在尝试使用 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);
}
其中return是当前鼠标坐标,
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 之间的整数值。如您所见,我想在将缩放更改为某个点后设置相机位置 - 需要计算该点这样当前的鼠标位置就不会改变(也就是放大到鼠标位置,例如 Google 地图所做的)。我已经尝试了整整 2 天了,我尝试了很多东西,但我就是想不出计算 x 和 y 的方程式。
请注意,所有 return 编辑和输入的点都是相对于地图的位置,特别是相对于地图的顶部(其顶角点为 (0, 0))。 getMouseCoordinates() 函数中的值 getCameraLeft() 和 getCameraTop() return
public static float getCameraLeft() {
return cameraX - zoom * (Display.getWidth() / 2);
}
和
public static float getCameraTop() {
return cameraY - zoom * (Display.getHeight() / 2);
}
.
如有任何帮助,我们将不胜感激。我希望,我没有表达自己太复杂。
我终于找到了正确的方程式:
float x = getMouseCoordinates().getX() + (getCameraX() - getMouseCoordinates().getX()) * (float) newZoom / (float) zoom;
float y = getMouseCoordinates().getY() + (getCameraY() - getMouseCoordinates().getY()) * (float) newZoom / (float) zoom;
无论如何谢谢你,我相信最终会有人给我正确的答案:)
我正在尝试使用 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);
}
其中return是当前鼠标坐标,
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 之间的整数值。如您所见,我想在将缩放更改为某个点后设置相机位置 - 需要计算该点这样当前的鼠标位置就不会改变(也就是放大到鼠标位置,例如 Google 地图所做的)。我已经尝试了整整 2 天了,我尝试了很多东西,但我就是想不出计算 x 和 y 的方程式。
请注意,所有 return 编辑和输入的点都是相对于地图的位置,特别是相对于地图的顶部(其顶角点为 (0, 0))。 getMouseCoordinates() 函数中的值 getCameraLeft() 和 getCameraTop() return
public static float getCameraLeft() {
return cameraX - zoom * (Display.getWidth() / 2);
}
和
public static float getCameraTop() {
return cameraY - zoom * (Display.getHeight() / 2);
}
.
如有任何帮助,我们将不胜感激。我希望,我没有表达自己太复杂。
我终于找到了正确的方程式:
float x = getMouseCoordinates().getX() + (getCameraX() - getMouseCoordinates().getX()) * (float) newZoom / (float) zoom;
float y = getMouseCoordinates().getY() + (getCameraY() - getMouseCoordinates().getY()) * (float) newZoom / (float) zoom;
无论如何谢谢你,我相信最终会有人给我正确的答案:)