WPF 3D - 围绕它自己的轴旋转模型

Gro*_*kys 4 math 3d wpf geometry

假设我有一个简单的WPF 3D场景设置,其中一个矩形围绕X轴旋转-45度,如下所示:

<Viewport3D>
    <Viewport3D.Camera>
        <PerspectiveCamera Position="0,0,4"/>
    </Viewport3D.Camera>
    <ModelVisual3D>
        <ModelVisual3D.Content>
            <DirectionalLight Color="White" Direction="-1,-1,-3" />
        </ModelVisual3D.Content>
    </ModelVisual3D>
    <ModelVisual3D>
        <ModelVisual3D.Content>
            <GeometryModel3D>
                <GeometryModel3D.Geometry>
                    <MeshGeometry3D Positions="-1,-1,0  1,-1,0  -1,1,0  1,1,0"
                                    TriangleIndices="0,1,2 1,3,2"/>
                </GeometryModel3D.Geometry>
                <GeometryModel3D.Material>
                    <DiffuseMaterial Brush="Red"/>
                </GeometryModel3D.Material>
            </GeometryModel3D>
        </ModelVisual3D.Content>
        <ModelVisual3D.Transform>
            <Transform3DGroup>
                <RotateTransform3D>
                    <RotateTransform3D.Rotation>
                        <AxisAngleRotation3D Axis="1,0,0" Angle="-45"/>
                    </RotateTransform3D.Rotation>
                </RotateTransform3D>
            </Transform3DGroup>
        </ModelVisual3D.Transform>
    </ModelVisual3D>
</Viewport3D>
Run Code Online (Sandbox Code Playgroud)

这给了我以下内容:

alt text http://www.freeimagehosting.net/uploads/4aa48434a9.png

现在我想围绕模型的Z轴旋转图像45度.如果我像这样放入第二个RotateTransform3D:

                <RotateTransform3D>
                    <RotateTransform3D.Rotation>
                        <AxisAngleRotation3D Axis="0,0,1" Angle="45"/>
                    </RotateTransform3D.Rotation>
                </RotateTransform3D>
Run Code Online (Sandbox Code Playgroud)

它围绕场景的 Z轴旋转.对于这个特定的X旋转我已经找到了我需要的是:

                <RotateTransform3D>
                    <RotateTransform3D.Rotation>
                        <AxisAngleRotation3D Axis="0,1,1" Angle="45"/>
                    </RotateTransform3D.Rotation>
                </RotateTransform3D>
Run Code Online (Sandbox Code Playgroud)

但在这里我的数学失败了.任何人都可以告诉我如何使用任意X(和Y,如果你想)轮换?

Gro*_*kys 11

好的,和一位数学家朋友谈过,他给了我答案:

所以我想如果你绕着矢量(1,0,0)旋转'a'的角度你需要做什么(即围绕x轴旋转,以便在yz平面上变换你的物体).

进一步的旋转

x' - (1,0,0)保持不变!

y' - (0,cosa,sina)

z' - (0,-sina,cosa)

类似的原理将适用于xz平面(0,1,0)的旋转

x' - (-sina,0,cosa)

y' - (0,1,0) - 相同

z' - (sina,o,cosa)

并在xy平面周围(0,0,1)

x' - (-sina,cosa,0)

y' - (cosa,sina,0)

z' - (0,0,1)保持不变

TADA!

更新:我创建了一个函数来计算一个矩阵,该矩阵将在所有3个轴上旋转一个对象.这可以与MatrixTransform3D一起使用.

    Matrix3D CalculateRotationMatrix(double x, double y, double z)
    {
        Matrix3D matrix = new Matrix3D();

        matrix.Rotate(new Quaternion(new Vector3D(1, 0, 0), x));
        matrix.Rotate(new Quaternion(new Vector3D(0, 1, 0) * matrix, y));
        matrix.Rotate(new Quaternion(new Vector3D(0, 0, 1) * matrix, z));

        return matrix;
    }
Run Code Online (Sandbox Code Playgroud)