Hello Guest

Author Topic: Zoom to a point when using zoomFactor  (Read 4376 times)

edb

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 43
    • View Profile
Zoom to a point when using zoomFactor
« 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.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Zoom to a point when using zoomFactor
« Reply #1 on: February 13, 2015, 11:57:19 pm »
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.

edb

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 43
    • View Profile
Re: Zoom to a point when using zoomFactor
« Reply #2 on: February 14, 2015, 07:05:43 pm »
Got it working!  If anybody's curious, here's the bit in my code where the zoom happens:

Code: [Select]
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();
}
//++++