简体   繁体   English

3d立方体y轴旋转不能正常工作

[英]3d cube y axis rotation not working properly

i am currently creating a rubiks cube project for my a level NEA and the cube solves but now im trying to implement a 3d model of this cube and i have this code.我目前正在为我的级别 NEA 创建一个 rubiks 多维数据集项目,并且该多维数据集可以解决,但现在我正在尝试实现该多维数据集的 3d model 并且我有此代码。 At the moment the x axis and z axis rotations work correctly but the y axis rotation seems to start of as a cube but as it rotates round becomes more of a trapezium as it rotates 180'.目前 x 轴和 z 轴旋转正常工作,但 y 轴旋转似乎从立方体开始,但随着旋转 180',圆形变得更像梯形。 any help on whats wrong would be greatly appreciated.任何错误的帮助将不胜感激。

Point3D final;
            double x = rotation.x;

            final.x = original.x;
            final.y = original.y * Math.Cos(x) - original.z * Math.Sin(x);
            final.z = original.y * Math.Sin(x) + original.z * Math.Cos(x);
            

            original.x = final.x;
            original.y = final.y;
            original.z = final.z;

            x = rotation.y;

            final.x = original.z * Math.Sin(x) + original.x * Math.Cos(x);
            final.y = original.y;
            final.z = original.y * Math.Cos(x) - original.x * Math.Sin(x);

            original.x = final.x;
            original.y = final.y;
            original.z = final.z;

            x = rotation.z;

            final.x = original.x * Math.Cos(x) - original.y * Math.Sin(x);
            final.y = original.x * Math.Sin(x) + original.y * Math.Cos(x);
            final.z = original.z;

typo.错字。 Change line for y -rotation toy旋转的线更改为

final.z = original.z * Math.Cos(x) - original.x * Math.Sin(x);

You were using original.y instead of original.z , but for a y -rotation the value of y does not play into the rotation.您使用的是original.y而不是original.z ,但是对于y旋转, y的值不会影响旋转。


May I suggest you define the rotations in methods我可以建议你在方法中定义轮换吗

public static class Rotations
{
    public static Point3D RotateAboutX(this Point3D point, double angle)
    {
        return new Point3D(
            point.X,
             Math.Cos(angle) * point.Y- Math.Sin(angle) * point.Z,
             Math.Sin(angle) * point.Y+ Math.Cos(angle) * point.Z);
    }
    public static Point3D RotateAboutY(this Point3D point, double angle)
    {
        return new Point3D(
            Math.Cos(angle) * point.X + Math.Sin(angle) * point.Z,
            point.Y,
            -Math.Sin(angle) * point.X + Math.Cos(angle) * point.Z);
    }
    public static Point3D RotateAboutZ(this Point3D point, double angle)
    {
        return new Point3D(
             Math.Cos(angle) * point.X - Math.Sin(angle) * point.Y,
             Math.Sin(angle) * point.X + Math.Cos(angle) * point.Y,
            point.Z);
    }
}

and then used them as needed.然后根据需要使用它们。 For Example例如

Point3D final = original.RotateAboutX(rotation.x)
                .RotateAboutY(rotation.y)
                .RotateAboutZ(rotation.z);

or the remain true to the original code或对原始代码保持真实

Point3D final = original.RotateAboutX(rotation.x);
original = final;
final = original.RotateAboutY(rotation.y);
original = final;
final = original.RotateAboutZ(rotation.z);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM