🐛 fix zoom-to-mouse using getWorldPoint diff instead of manual formula
This commit is contained in:
@@ -54,19 +54,20 @@ export class CameraSystem {
|
|||||||
|
|
||||||
// Scroll wheel zoom — zoom toward mouse pointer position
|
// Scroll wheel zoom — zoom toward mouse pointer position
|
||||||
this.scene.input.on('wheel', (ptr: Phaser.Input.Pointer, _objs: unknown, _dx: number, dy: number) => {
|
this.scene.input.on('wheel', (ptr: Phaser.Input.Pointer, _objs: unknown, _dx: number, dy: number) => {
|
||||||
const oldZoom = cam.zoom
|
const newZoom = Phaser.Math.Clamp(cam.zoom - Math.sign(dy) * ZOOM_STEP, MIN_ZOOM, MAX_ZOOM)
|
||||||
const newZoom = Phaser.Math.Clamp(oldZoom - Math.sign(dy) * ZOOM_STEP, MIN_ZOOM, MAX_ZOOM)
|
if (newZoom === cam.zoom) return
|
||||||
if (newZoom === oldZoom) return
|
|
||||||
|
|
||||||
// World point under mouse before zoom
|
// Sample the world point under the mouse BEFORE the zoom change
|
||||||
const worldX = cam.scrollX + ptr.x / oldZoom
|
const before = cam.getWorldPoint(ptr.x, ptr.y)
|
||||||
const worldY = cam.scrollY + ptr.y / oldZoom
|
|
||||||
|
|
||||||
cam.setZoom(newZoom)
|
cam.setZoom(newZoom)
|
||||||
|
|
||||||
// Adjust scroll so the same world point stays under the mouse
|
// Sample the same screen position AFTER zoom — it now maps to a different world point
|
||||||
cam.scrollX = worldX - ptr.x / newZoom
|
const after = cam.getWorldPoint(ptr.x, ptr.y)
|
||||||
cam.scrollY = worldY - ptr.y / newZoom
|
|
||||||
|
// Shift scroll by the difference so the original world point snaps back under the mouse
|
||||||
|
cam.scrollX -= after.x - before.x
|
||||||
|
cam.scrollY -= after.y - before.y
|
||||||
})
|
})
|
||||||
|
|
||||||
// Middle-click pan: start on button down
|
// Middle-click pan: start on button down
|
||||||
|
|||||||
Reference in New Issue
Block a user