2015-07-20 72 views
1

我正在嘗試使用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); 
} 

它返回當前鼠標座標,和

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 Maps的功能)。我現在一直在努力嘗試兩天,我嘗試了很多東西,但我無法弄清楚計算x和y的方程式。

請注意,所有返回並輸入的點都是相對於地圖的位置,特別是地圖頂部的一部分(其頂角點爲(0,0))。值getCameraLeft()和getCameraTop()在getMouseCoordinates()函數返回

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

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

任何幫助,將不勝感激。我希望,我沒有表達自己太複雜。

回答

1

我終於找到了正確的公式:

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

謝謝反正,我敢肯定,最終會有人給我正確答案:)