I’m trying to implement Pinch Zoom in my app using a 2d camera. I am able to zoom in and out successfully using the following code:
case GestureType.Pinch:
offset = new Vector2(0, 0);
oldPosition1 = gesture.Position - gesture.Delta;
oldPosition2 = gesture.Position2 - gesture.Delta2;
newDistance = Vector2.Distance(gesture.Position, gesture.Position2);
oldDistance = Vector2.Distance(oldPosition1, oldPosition2);
scaleFactor = newDistance / oldDistance;
if (pinchInProgress == false)
{
pinchTarget = new Vector2((gesture.Position.X + gesture.Position2.X) / 2, (gesture.Position.Y + gesture.Position2.Y) / 2);
pinchInProgress = true;
}
// Prevents from zooming out further than full screen
if (workSpace.Width * cam.Zoom < SharedGraphicsDeviceManager.Current.GraphicsDevice.Viewport.Width && scaleFactor < 1)
scaleFactor = 1;
if (workSpace.Height * cam.Zoom < SharedGraphicsDeviceManager.Current.GraphicsDevice.Viewport.Height && scaleFactor < 1)
scaleFactor = 1;
cam.Zoom = MathHelper.Clamp(cam.Zoom * scaleFactor, 0.1f, 1.5f);
if (cam.Pos.X - SharedGraphicsDeviceManager.Current.GraphicsDevice.Viewport.Width < -(workSpace.Width * cam.Zoom))
offset.X = -(cam.Pos.X + workSpace.Width * cam.Zoom - SharedGraphicsDeviceManager.Current.GraphicsDevice.Viewport.Width);
if (cam.Pos.Y - SharedGraphicsDeviceManager.Current.GraphicsDevice.Viewport.Height < -(workSpace.Height * cam.Zoom))
offset.Y = -(cam.Pos.Y + workSpace.Height * cam.Zoom - SharedGraphicsDeviceManager.Current.GraphicsDevice.Viewport.Height);
if (cam.Pos.X + offset.X > 0)
offset.X = -(cam.Pos.X);
if (cam.Pos.Y + offset.Y > 0)
offset.Y = -(cam.Pos.Y);
cam.Move(offset);
break;
which also handles moving camera away from the edge so the camera will always stay within the workspace.
I’ve been trying to implement a mechanism for the camera to zoom in at the pinch gesture center and not at Vector2.Zero of the workspace. From another question on SO it seems I can get the camera to follow the pinch center (or at least try).
So I was hoping I could utilise the following:
case GestureType.PinchComplete:
pinchInProgress = false;
break;
to differentiate one gesture from another and make the camera move towards one point defined at the begining of the gesture.
I hope it all makes sense.
Anyway, the real problem here is that pinchInProgress never gets set to false.
It gets set properly to true in GestureType.Pinch block but it seems like PinchComplete never gets triggered.
EDIT:
Also tried adding breakpoint at pinchInProgress = false; line in VS and it never gets to this point.
It turns out I forgot to enable PinchComplete gesture
Apologies