XYZ坐标的3D旋转 - 错误导致某些角度?

时间:2017-10-09 02:02:39

标签: vb.net math 3d rotation

请耐心等待我是编程的新手,我正在努力学习如何在0,0,0左右旋转3D点(XYZ)我会尝试改进我的代码以允许旋转任意点(XYZ)。

我从VB开始,在此处和网络上进行大量搜索后,我无法找到解决问题的方法。我差不多40岁,正在努力学习数学和编程,所以请耐心等待,因为我需要时间来消化所有数学方面的问题。

基本上,我试图编写一个算法来旋转3D点,但是,虽然看起来我的代码有些角度,但是其他一些我只是得到奇怪的时髦数字在某些方面可能是正确的,但是我找不到代码中的缺陷。我一直在研究这个问题并尝试了多种方法,但我只是无法发现错误。

这是我的小应用程序的用户界面。原始坐标输入到表单的顶部,在表单的底部我显示了旋转的坐标。

请注意,在下图中,Y10.0坐标绕Z轴旋转90度的简单旋转返回正确的X值(-10),但Y显示一个时髦的数字(6.1230 ...)..但是如果我将Z周围的旋转角度改为45,结果似乎是正确的......

我不知道自己做错了什么来得到这个奇怪的Y.由于这个错误,我完全不相信这个算法的结果,但我现在处于一个盲点... < / p>

Conceptual UI

这是计算按钮的代码:

 Private Sub BtnCompute_Click(sender As Object, e As EventArgs) Handles BtnCompute.Click

    'Capture the values from the text boxes and parse then to doubles
    ValidateAllFieldsWithDoubleValues()

    'Rotate the coordinates
    RotateXYZCoordinates(dblOriginalCoordX, dblOriginalCoordY, dblOriginalCoordZ, dblCurrentRotationAroundX, dblCurrentRotationAroundY, dblCurrentRotationAroundZ)

    'Update the text boxes for the rotated coordinates for XYZ
    txtResultX.Text = dblResultX.ToString
    txtResultY.Text = dblResultY.ToString
    txtResultZ.Text = dblResultZ.ToString

End Sub

这是计算旋转的函数的代码:

Private Function RotateXYZCoordinates(ByVal XCoord As Double, ByVal YCoord As Double, ByVal ZCoord As Double, ByVal Pitch As Double, ByVal Roll As Double, ByVal Yaw As Double)

    'X Rotation
    Dim RadPitch As Double = 0
    Dim CosPitch As Double = 0
    Dim SinPitch As Double = 0
    Dim XRotatedAroundX As Double = 0
    Dim YRotatedAroundX As Double = 0
    Dim ZRotatedAroundX As Double = 0
    RadPitch = Pitch * Math.PI / 180
    CosPitch = Math.Cos(RadPitch)
    SinPitch = Math.Sin(RadPitch)

    XRotatedAroundX = XCoord
    YRotatedAroundX = YCoord * CosPitch - ZCoord * SinPitch
    ZRotatedAroundX = YCoord * SinPitch + ZCoord * CosPitch

    'Y Rotation
    Dim RadRoll As Double = 0
    Dim CosRoll As Double = 0
    Dim SinRoll As Double = 0
    Dim XRotatedAroundY As Double = 0
    Dim YRotatedAroundY As Double = 0
    Dim ZRotatedAroundY As Double = 0
    RadRoll = Roll * Math.PI / 180
    CosRoll = Math.Cos(RadRoll)
    SinRoll = Math.Sin(RadRoll)

    XRotatedAroundY = ZRotatedAroundX * CosRoll - XRotatedAroundX * SinRoll
    YRotatedAroundY = YRotatedAroundX
    ZRotatedAroundY = ZRotatedAroundX * SinRoll + XRotatedAroundX * CosRoll

    'Z Rotation
    Dim RadYaw As Double = 0
    Dim CosYaw As Double = 0
    Dim SinYaw As Double = 0
    Dim XRotatedAroundZ As Double = 0
    Dim YRotatedAroundZ As Double = 0
    Dim ZRotatedAroundZ As Double = 0
    RadYaw = Yaw * Math.PI / 180
    CosYaw = Math.Cos(RadYaw)
    SinYaw = Math.Sin(RadYaw)

    XRotatedAroundZ = XRotatedAroundY * CosYaw - YRotatedAroundY * SinYaw
    YRotatedAroundZ = XRotatedAroundY * SinYaw + YRotatedAroundY * CosYaw
    ZRotatedAroundZ = ZRotatedAroundY

    'Final result

    dblResultX = XRotatedAroundZ
    dblResultY = YRotatedAroundZ
    dblResultZ = ZRotatedAroundZ

    Return Nothing

End Function

我知道这不是一个优雅的代码,但它是我现在可以编码的代码......如果有人可以看看这个并指出我的错误来源,我会感激...我已经在我发布之前观看视频并在这个网站上进行了广泛的搜索......但是现在看来有些事情对我来说还是非常先进的...我不是很懒,如果有人指着某些东西,我愿意学习我现在可以消化......

如果有人可以分享关于如何使这个旋转功能支持围绕0,0,0以外的点旋转的提示,我会感激。

谢谢,

丹尼尔

1 个答案:

答案 0 :(得分:1)

答案是正确的。由于双精度数学和90度旋转,因此精度受到限制。答案实际上是6.12303176911189E-16或.000000000000000612303176911189。将数字四舍五入为小数点的实际值。这也是为什么在浮点数学中1 + 1不等于2而是1.9999999999999999999999999999999999的原因。