3D Math / 2D旋转计算:拆分/切割3D模型?

时间:2011-04-05 01:54:49

标签: c# 3d rotation 2d trigonometry

我正在尝试在其Z轴(向上/向下)上旋转3D对象。

public void RotateY(float angle)
{
    foreach (CoordinateVertices cv in this.GetAll<CoordinateVertices>())
    {
        for (int i = 0; i < cv.Coordinates.Length; i++)
        {
            Vector3 old = cv.Coordinates[i];

            float theta = Math.Atan2(old.Y, old.X) + angle;
            float rayon = Math.Sqrt(Math.Pow(old.X, 2) + Math.Pow(old.Y, 2));

            cv.Coordinates[i] = new Vector3(Math.Cos(theta) * rayon, Math.Sin(theta) * rayon, old.Z);
        }
    }
}

三角函数非常简单,看起来效果很好,但出于某种原因,我的3D对象减少了一半。

Comparison

有人知道发生了什么吗?我会在数学StackExchange上发布这个,但它也可能是我的编程问题,三角函数非常简单。

3 个答案:

答案 0 :(得分:1)

编辑:以下是与上述相同的替代方法。我花了几分钟才意识到以下解决方案与最初发布的代码相同。

它应该是这样的:

double Xnew = X * cos(theta) + Y * sin(theta);
double Ynew = Y * cos(theta) - X * sin(theta);

或者在您的代码中:

public void RotateY(float angle)
{
    foreach (CoordinateVertices cv in this.GetAll<CoordinateVertices>())
    {
        for (int i = 0; i < cv.Coordinates.Length; i++)
        {
            Vector3 old = cv.Coordinates[i];
            float xnew = old.X * Math.Cos(angle) + old.Y * Math.Sin(angle);
            float ynew = old.Y * Math.Cos(angle) - old.X * Math.Sin(angle);

            cv.Coordinates[i] = new Vector3(xnew, ynew, old.Z);
        }
    }
}

上面的代码假设您正在围绕原点进行旋转。如果你没有围绕原点旋转,你只需要转换为原点,旋转,然后转换回来。

请点击此处了解详情:http://en.wikipedia.org/wiki/Transformation_matrix#Rotation

答案 1 :(得分:1)

如前所述,您的代码没有任何问题。但是,您可能也有兴趣使用Transform函数(可以同时对整个坐标数组进行操作)。 Vector3.Transform (Vector3[], Matrix)。您可以使用针对任何轴计算给定角度θ的rotation matrix进行旋转。我希望这对于大量积分来说要快得多。 (减少三角计算,可能还有硬件加速)

答案 2 :(得分:0)

实际上,这个错误消失了,无处不在。我继续测试更多的值,他们工作了。我回到了和以前一样的价值,它起作用了。这太荒谬了,它总是发生在我身上。

这是什么名字?臭虫自己消失。

相关问题