WPF 中的 3D 对象旋转

发布于 2024-10-02 15:30:58 字数 1914 浏览 1 评论 0原文

我在 WPF 中有一个 3D 透明对象。用于创建对象的画笔的不透明度值已设置为 0.25。 我必须使用鼠标移动来围绕其中心点旋转这个 3D 对象。 目前,对象与鼠标移动的方向相反地旋转。 有人知道如何在 3D 空间中旋转透明物体吗?

用于旋转的代码是:

public static void DoMouseMoveEvent(object sender, MouseEventArgs e, Transform3DGroup transform3DGroup, System.Windows.Controls.Viewport3D viewport3D, ref Point mLastPos, Point3D centerPoint)
    {
        var pos = Mouse.GetPosition(viewport3D);


        var actualPos = new Point(pos.X - viewport3D.ActualWidth / 2,
              viewport3D.ActualHeight / 2 - pos.Y);
        double dx = actualPos.X - mLastPos.X;
        double dy = actualPos.Y - mLastPos.Y;

        double mouseAngle = 0;

        if (dx != 0 && dy != 0)
        {
            mouseAngle = Math.Asin(Math.Abs(dy) /
                Math.Sqrt(Math.Pow(dx, 2) + Math.Pow(dy, 2)));
            if (dx < 0 && dy > 0) mouseAngle += Math.PI / 2;
            else if (dx < 0 && dy < 0) mouseAngle += Math.PI;
            else if (dx > 0 && dy < 0) mouseAngle += Math.PI * 1.5;
        }
        else if (dx == 0 && dy != 0)
        {
            mouseAngle = Math.Sign(dy) > 0 ? Math.PI / 2 : Math.PI * 1.5;
        }
        else if (dx != 0 && dy == 0)
        {
            mouseAngle = Math.Sign(dx) > 0 ? 0 : Math.PI;
        }

        double axisAngle = mouseAngle + Math.PI / 2;

        var axis = new Vector3D(Math.Cos(axisAngle) * 4, Math.Sin(axisAngle) * 4, 0);

        //axis.Normalize();

        double rotation = 0.02 * Math.Sqrt(Math.Pow(dx, 2) + Math.Pow(dy, 2));

        var r = new QuaternionRotation3D(new Quaternion(axis, rotation * 180 / Math.PI));

        var rotateTransform = new RotateTransform3D(r, centerPoint);            

        transform3DGroup.Children.Add(rotateTransform);

        mLastPos = actualPos;
    }

错误行为的原因是没有捕获 z 坐标来计算上述代码片段中的旋转吗?

感谢您的帮助....

I have a 3D transparent object in WPF. The opacity value for the brush used to create the object has been set to 0.25.
I have to rotate this 3D object about its central point using mouse movement.
At present, the object rotates against the direction of the mouse movement.
Does anyone have an idea on how to rotate a transparent object in 3D space?

The code used for rotation is:

public static void DoMouseMoveEvent(object sender, MouseEventArgs e, Transform3DGroup transform3DGroup, System.Windows.Controls.Viewport3D viewport3D, ref Point mLastPos, Point3D centerPoint)
    {
        var pos = Mouse.GetPosition(viewport3D);


        var actualPos = new Point(pos.X - viewport3D.ActualWidth / 2,
              viewport3D.ActualHeight / 2 - pos.Y);
        double dx = actualPos.X - mLastPos.X;
        double dy = actualPos.Y - mLastPos.Y;

        double mouseAngle = 0;

        if (dx != 0 && dy != 0)
        {
            mouseAngle = Math.Asin(Math.Abs(dy) /
                Math.Sqrt(Math.Pow(dx, 2) + Math.Pow(dy, 2)));
            if (dx < 0 && dy > 0) mouseAngle += Math.PI / 2;
            else if (dx < 0 && dy < 0) mouseAngle += Math.PI;
            else if (dx > 0 && dy < 0) mouseAngle += Math.PI * 1.5;
        }
        else if (dx == 0 && dy != 0)
        {
            mouseAngle = Math.Sign(dy) > 0 ? Math.PI / 2 : Math.PI * 1.5;
        }
        else if (dx != 0 && dy == 0)
        {
            mouseAngle = Math.Sign(dx) > 0 ? 0 : Math.PI;
        }

        double axisAngle = mouseAngle + Math.PI / 2;

        var axis = new Vector3D(Math.Cos(axisAngle) * 4, Math.Sin(axisAngle) * 4, 0);

        //axis.Normalize();

        double rotation = 0.02 * Math.Sqrt(Math.Pow(dx, 2) + Math.Pow(dy, 2));

        var r = new QuaternionRotation3D(new Quaternion(axis, rotation * 180 / Math.PI));

        var rotateTransform = new RotateTransform3D(r, centerPoint);            

        transform3DGroup.Children.Add(rotateTransform);

        mLastPos = actualPos;
    }

Could the reason of the incorrect behavior be that the z-coordinate is not captured for computing the rotation in the above code snippet?

Thanks for the help....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲欢浪云 2024-10-09 15:30:58

我认为z坐标有问题。因为z坐标用于三维位置,如果不使用该坐标,则很难在三维中旋转图形。如果你计算 z 坐标我想问题就会解决

i think problem in z coordinate. Becouse z coordinate using for position in third dimension,if you dont use this coordinate rotate figure in three dimension very hard. If you calculate z coordinate i think problem will be resolved

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文