WPF,3D相机将无法正确旋转

时间:2013-09-14 17:45:50

标签: .net wpf perspectivecamera

修改

哦,还有一件事,longtitute很有用,但当大约达到1.57时,它会翻转......

enter image description here

编辑工作!

哦,只有旋转速度在转一圈时才开始增加。猜测必须在每次轮换后重置tempMouse。

    private void mouseWheel(object sender, MouseWheelEventArgs  e)
    {
        if (e.Delta < 0) { distance = distance - 0.5; } else { distance = distance + 0.5; }

        Point3D position = new Point3D
            (
                Math.Cos(longitude) * Math.Cos(latitude) * distance,
                Math.Sin(latitude) * distance,
                Math.Sin(longitude) * Math.Cos(latitude) * distance
            );

       _perspectiveCamera.Position = position;
    }


    private void mouseMove(object sender, MouseEventArgs e)
    {

        if ( e.LeftButton == MouseButtonState.Pressed )
        {
            if (isLeftDown == false)
            {
                isLeftDown = true;
                tempMouseX = this.PointToScreen(Mouse.GetPosition(this)).X;
                tempMouseY = this.PointToScreen(Mouse.GetPosition(this)).Y;
            }
            else
            {
                double currentDelataX = tempMouseX - this.PointToScreen(Mouse.GetPosition(this)).X;
                double currentDelataY = tempMouseY - this.PointToScreen(Mouse.GetPosition(this)).Y;

                longitude = longitude - currentDelataX / 5000;
                latitude = latitude - currentDelataY / 5000;



                Point3D objectPosition = new Point3D(0, 0, 0);

                Point3D position = new Point3D
                    (
                        Math.Cos(longitude) * Math.Cos(latitude) * distance,
                        Math.Sin(latitude) * distance,
                        Math.Sin(longitude) * Math.Cos(latitude) * distance
                    );

                _perspectiveCamera.Position = position;

                Vector3D lookDirection = objectPosition - position;
                _perspectiveCamera.LookDirection = lookDirection;


            }

        }

        /////////////////////////////////////////

        if (e.LeftButton == MouseButtonState.Released)
        {
            isLeftDown = false;
        }


    }

1 个答案:

答案 0 :(得分:5)

我认为您想要将相机移动到球体上,而不是像现在这样在平面上移动。

不是将相机的位置存储在欧几里德坐标中,而是将其存储在球坐标中。

double distance;
double latitude;
double longitude;

在鼠标滚轮处理程序中,只需增加/减少distance

在鼠标移动处理程序中,调整与{delta}成比例的latitude(您可能希望将其限制为+/- pi / 2,这样就不会超过极点),{{1与X delta成比例。

要计算您的位置,请使用以下内容:

longitude