0

我有一个可以放大和缩小以及平移的控件。通过点击两个按钮的选择,用户可以分别放大或缩小。我的问题是放大或缩小后的翻译:如果我缩小到大约 0.2F,我必须多次点击平移才能移动我在基数 0 处可能拥有的相同距离。我似乎通过划分缩放解决了这个问题通过它的自平方:z/(z*z) 但这似乎改变了整个矩阵——而且我没有在任何线上用这个来转换矩阵!

这是我的代码:

Matrix mtrx = new Matrix();
mtrx.Translate(pan.X, pan.Y);
mtrx.Scale(zoom, zoom);
e.Graphics.Transform = mtrx;

// To-Do drawing code here

基本注意事项:

  • 按钮单击事件时缩放增加和减少 .12F
  • 正在绘制的控件是继承的 UserControl 类
  • 开发环境为 C# 2010
  • 绘图代码处理两个位置为 {0, 0} 的矩形
  • 当我放大或缩小某种方式时,我只想能够以与基数 0(未放大或缩小)相同的速度平移。〜没有运动滞后的感觉

编辑:我已经将上面的代码更新为我之前处理的改进版本。这会处理缩放后的平移速度,但新问题是缩放发生的位置:好像缩放点也在被平移......

编辑:我将如何在 3D(XNA)中做到这一点:

public class Camera
{
    private float timeLapse = 0.0f;
    private Vector3 position, view, up;

    public Camera(Vector2 windowSize)
    {
        viewMatrix = Matrix.CreateLookAt(this.position, this.view, this.up);
        projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.Pi / 4.0f,
                           (float)windowSize.X / (float)windowSize.Y, 0.005f, 1000.0f);
    }
    //Sets time lapse between frames
    public void SetFrameInterval(GameTime gameTime)
    {
        timeLapse = (float)gameTime.ElapsedGameTime.Milliseconds;
    }

    //Move camera and view position according to gamer's move and strafe events.
    private void zoomHelper(Vector3 direction, float speed)
    {
        speed *= (float)timeLapse; // scale rate of change
        direction *= speed;
        position.Y += direction.Y;
        position.X += direction.X;      // adjust position 
        position.Z += direction.Z;
        view.Y += direction.Y;
        view.X += direction.X;      // adjust view change
        view.Z += direction.Z;
    }
    //Allows the camera to move forward or backward in the direction it is looking.
    public void Zoom(float amount)
    {
        Vector3 look = new Vector3(0.0f, 0.0f, 0.0f);
        Vector3 unitLook;
        // scale rate of change in movement
        const float SCALE = 0.005f;
        amount *= SCALE;
        // update forward direction
        look = view - position;
        // get new camera direction vector 
        unitLook = Vector3.Normalize(look);
        // update camera position and view position
        zoomHelper(unitLook, amount);
        viewMatrix = Matrix.CreateLookAt(position, view, up);
    }

    //Allows the camera to pan on a 2D plane in 3D space.
    public void Pan(Vector2 mouseCoords)
    {
        // The 2D pan translation vector in screen space
        Vector2 scaledMouse = (mouseCoords * 0.005f) * (float)timeLapse;
        // The camera's look vector
        Vector3 look = view - position;
        // The pan coordinate system basis vectors
        Vector3 Right = Vector3.Normalize(Vector3.Cross(look, up));
        Vector3 Up = Vector3.Normalize(Vector3.Cross(Right, look));
        // The 3D pan translation vector in world space
        Vector3 Pan = scaledMouse.X * Right + -scaledMouse.Y * Up;
        // Translate the camera and the target by the pan vector
        position += Pan;
        view += Pan;
        viewMatrix = Matrix.CreateLookAt(position, view, up);
    }
}

像这样使用:

Camera cam; // object
effect.Transform = cam.viewMatrix * cam.projectionMatrix;
// ToDo: Drawing Code Here
4

1 回答 1

0

尝试这个:

Matrix mtrx = new Matrix();  
mtrx.Scale(zoom, zoom);  
mtrx.Translate(pan.X/zoom, pan.Y/zoom, MatrixOrder.Append);   
e.Graphics.Transform = mtrx;  
// To-Do drawing code here
于 2012-10-22T08:28:11.970 回答