XNA 2d相机捏合和缩放

时间:2011-12-31 15:37:54

标签: c# windows-phone-7 xna touch

我正在尝试使用二维相机在我的应用中实现Pinch Zoom。我可以使用以下代码成功放大和缩小:

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;

还可以将相机移动远离边缘,因此相机将始终保持在工作区内。

我一直在尝试实现一种机制,让相机在捏合手势中心放大,而不是在工作区的Vector2.Zero上放大。从另一个关于SO的问题来看,似乎我可以让相机跟随夹点中心(或者至少尝试一下)。

所以我希望我可以利用以下内容:

case GestureType.PinchComplete:
    pinchInProgress = false;
    break;

将一个手势与另一个手势区分开来,使相机朝着手势开始时定义的一个点移动。

我希望这一切都有道理。

无论如何,这里真正的问题是pinchInProgress永远不会被设置为false。 它在GestureType.Pinch块中被正确设置为true,但似乎PinchComplete永远不会被触发。

修改 还尝试在pinchInProgress = false中添加断点; VS中的一行,它永远不会达到这一点。

1 个答案:

答案 0 :(得分:1)

事实证明我忘了启用PinchComplete手势

TouchPanel.EnabledGestures = GestureType.FreeDrag | GestureType.Pinch | GestureType.PinchComplete;

道歉

相关问题