【问题标题】:WPF, 3d camera won't rotate correctlyWPF,3d 相机不会正确旋转
【发布时间】:2013-09-14 17:45:49
【问题描述】:

编辑

哦,还有一件事,longtitute 效果很好,但是当 lattitute 达到大约 1.57 时,它会翻转......

编辑作品!

哦,只有转一圈才开始增加转速。猜测必须在每次旋转后重置 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;
        }


    }

【问题讨论】:

  • 相机是什么UpDirection
  • 你希望上面的代码如何旋转任何东西?只是相机位置的平移。 Bzw,你应该更新tempMouseYtempMouseX。否则,deltas 会变得很大。
  • UpDirection = new Vector3D(0, 1, 0);但就是这样吗?我假设相机会通过移动它的位置来旋转,并且仍然保持它指向世界的中心。
  • 回复:编辑 - 看起来您忘记重新分配相机位置。也许某处有错误的标志。
  • 尝试在您的lookDirection 中切换objectPositionposition。也许我把它弄反了。

标签: .net wpf perspectivecamera


【解决方案1】:

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

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

double distance;
double latitude;
double longitude;

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

在鼠标移动处理程序中,将latitude 调整为与 Y 增量成比例(您可能希望将其限制为 +/-pi/2,以免超出极点),并与 longitude 成比例X 增量。

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

Point3D position = new Point3D
(
    Math.Cos(longitude) * Math.Cos(latitude) * distance,
    Math.Sin(latitude) * distance,
    Math.Sin(longitude) * Math.Cos(latitude) * distance
);
// This line is just a simplified version of your LookDirection calculation
Vector3D lookDirection = objectPosition - position;

【讨论】:

  • 我很少看到这么漂亮、非常优雅的答案 :) 如果可以的话,我会 +2。
  • 感谢 Kendall 的回答,看起来是正确的,但是当我实现它时(参见上面的编辑),相机只是切换到黑色......我在这里做错了吗?
  • 你能解释一下“换黑”是什么意思吗?
  • 看不到中心的物体,即相机指向其他地方。
  • 查看我对这个问题的评论。我认为你有一个不好的distance
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-23
  • 2016-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多