2D Toolkit Forum
2D Toolkit => Support => Topic started by: edb on February 12, 2015, 08:01:20 pm
-
Anybody have any code or ideas on how to zoom in to a specific point when using tk2dCamera.zoomFactor to zoom?
When you change the value of zoomFactor, it zooms in/out on the position of the camera.
I need it zoom in on a specific point (for doing a pinch zoom on mobile devices).
Thanks.
-
You'll need to translate the camera while zooming. Basically if you know the offset to the point is P when zoomFactor = 1, you will need to move the camera appropriately, scaled by the difference where the camera would be at the zoomScale vs when its one. Don't have any code handy, sorry.
-
Got it working! If anybody's curious, here's the bit in my code where the zoom happens:
Touch touchZero = Input.GetTouch(0);
Touch touchOne = Input.GetTouch(1);
// A finger was lifted, reset some stuff
if (touchZero.phase == TouchPhase.Ended || touchOne.phase == TouchPhase.Ended)
{
initialPinchCenterHasBeenSet = false;
return;
}
// Find the position in the previous frame of each touch.
Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;
// Find the magnitude of the vector (the distance) between the touches in each frame.
float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
float touchDeltaMag = (touchZero.position - touchOne.position).magnitude;
// Find the difference in the distances between each frame.
float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag;
// Save the initial pinch points
Vector2 pinchCenterScreenPt = (touchZeroPrevPos + touchOnePrevPos) / 2.0f;
if (initialPinchCenterHasBeenSet == false)
{
initialPinchCenterScreenPt = pinchCenterScreenPt;
// Clamp initialPinchCenterScreenPt to be within the playfield
Vector3 lowerRightCornerScreenPt = gameplayManager.mainTk2dCamera.camera.WorldToScreenPoint(lowerRightMostCornerOfGridcels);
Vector3 upperLeftCornerScreenPt = gameplayManager.mainTk2dCamera.camera.WorldToScreenPoint(upperLeftMostCornerOfGridcels);
initialPinchCenterScreenPt.x = Mathf.Clamp(initialPinchCenterScreenPt.x, upperLeftCornerScreenPt.x, lowerRightCornerScreenPt.x);
initialPinchCenterScreenPt.y = Mathf.Clamp(initialPinchCenterScreenPt.y, lowerRightCornerScreenPt.y, upperLeftCornerScreenPt.y);
initialPinchCenterWorldPt = gameplayManager.mainTk2dCamera.camera.ScreenToWorldPoint(initialPinchCenterScreenPt);
initialPinchCenterHasBeenSet = true;
initialCameraScreenPt = gameplayManager.mainTk2dCamera.camera.WorldToScreenPoint(gameplayManager.mainTk2dCamera.transform.position);
}
//#### ZOOM PART 1. ZOOMFACTOR change
float maxZoomFactor = 2.0f;
// Get a zoomFactor by multiplying the pinch distance by a speed
valForPinchZeroToOne = valForPinchZeroToOne - deltaMagnitudeDiff*multitouchPinchZoomSpeed;
valForPinchZeroToOne = Mathf.Clamp(valForPinchZeroToOne, 0, 1);
float valForZoomFactor = Mathf.Lerp(gameplayManager.zoomFactorSave, maxZoomFactor, valForPinchZeroToOne);
gameplayManager.mainTk2dCamera.ZoomFactor = valForZoomFactor;
// CLAMP ZoomFactor
gameplayManager.mainTk2dCamera.ZoomFactor = Mathf.Clamp(gameplayManager.mainTk2dCamera.ZoomFactor, gameplayManager.zoomFactorSave, maxZoomFactor);
gameplayManager.mainTk2dCamera.UpdateCameraMatrix();
//####
//++++ ZOOM PART 2. MOVE the Tk2dCamera's position.
if ( Mathf.Abs(deltaMagnitudeDiff) > 0) // Only do the move when there was a zoom
{
Vector3 originalOffsetScreen = initialPinchCenterScreenPt - initialCameraScreenPt;
Vector3 curOffsetScreen = mainCamera.WorldToScreenPoint(initialPinchCenterWorldPt) - initialCameraScreenPt;
Vector3 difference = curOffsetScreen-originalOffsetScreen;
Vector3 moveTowardPinchScreenPt = new Vector3();
moveTowardPinchScreenPt = initialCameraScreenPt + difference;
gameplayManager.mainTk2dCamera.transform.position = gameplayManager.mainTk2dCamera.ScreenToWorldPoint(moveTowardPinchScreenPt);
gameplayManager.mainTk2dCamera.UpdateCameraMatrix();
}
//++++