I want to pinch zoom around a specific coordinate on a tiled 15f x 15f 3D board. I.e. I don't want to zoom around origin. Thus I need to pan the board accordingly.
I am using a PerspectiveCamera
(near=0.1f, far=100f). For a perfect fit of the screen the camera is located at approx.z=13.4
before zooming.
Now what (I think) I want to do is to:
Unproject the screen coordinates (GestureDetector.pinch
method) done once for each pinch zoom:
float icx = (initialPointer1.x + initialPointer2.x) / 2;
float icy = (initialPointer1.y + initialPointer2.y) / 2;
pointToMaintain = new Vector3(icx, icy, 0f);
mCamera.unproject(pointToMaintain);
Now for each zoom cycle (as I adjust the mCamera.z
accordingly and do mCamera.update()
) I project the point back to screen coordinates:
Vector3 pointNewPos = new Vector3(pointToMaintain);
mCamera.project(pointNewPos);
Then calculate the delta and pan accordingly:
int dX = (int) (pointNewPos.x - icx);
int dY = (int) (pointNewPos.y - icy);
pan(...); /* I.e. mCamera.translate(...) */
My problem is that the mCamera.z is initially above pointToMaintain.z and then goes below as the user moves the fingers:
cam.z ptm.z dX dY
0 13.40
1 13.32 13.30 12 134
2 13.24 13.30 12 -188
...
(0) is the original value of mCamera.z before zooming starts. (1) is not not valid? However (2) should be OK.
My questions are:
(1) How can I get a "valid" pointToMaintain
when unprojecting the screen coordinates on the camera. I.e. a point that is not less than cam.z
. (The reason I get the point to 13.30 is (I guess) because near=0.1f. But as seen above this results in (weird?) screen coordinates).
(2) Is this a good strategy for moving the tiles board closer to the coordinates the user pinched zoomed on?