【问题标题】:3D object is rotating along all three axes when only X-Y rotation is desired当只需要 X-Y 旋转时,3D 对象沿所有三个轴旋转
【发布时间】:2013-01-19 01:38:56
【问题描述】:

所以我在 OpenGL 场景中为我的 3D 对象实现旋转,到目前为止,基本旋转本身(使用四元数)工作正常。我还主要解决了“观察”问题,这样我现在可以让任何 3D 对象(包括相机)准确无误地面对 3D 空间中的任何点。

然而,当命令一个物体面对一个给定的点时,它会频繁地沿着它的局部Z轴滚动;看这张图片,小船正看着红、绿、蓝三条线的交汇点。您可以看到它看起来不像人们期望的那样,例如,沿其 Y 轴旋转以面向绿线,然后沿其 X 轴向下倾斜以面向三条线的交点。

我不一定想要这个。我宁愿它直接看那个点,但顺时针稍微倾斜一些(从前面看),这样它的翅膀的尖端在相同的 Y 水平。由于面对一个点的整个点是局部 Z 轴穿过该点,因此对象如何围绕其局部 Z 轴旋转并不重要,但产生的旋转始终是倾斜的,尽管倾斜似乎取决于对象的相对位置及其焦点。

无论如何,这是我的 LookAt() 代码,我想对其进行修改,以便更好地控制最终的 Z 旋转。

void Thing::LookAt(sf::Vector3<float> Target)
{
    ///Derived from pseudocode found here:
    ///http://stackoverflow.com/questions/13014973/quaternion-rotate-to
    //Reset the rotation to default
    m_Orientation = Quaternion();

    //Get the normalized vector from the camera position to Target
    sf::Vector3<double> VectorTo(Target.x - m_Position.x,
                                 Target.y - m_Position.y,
                                 Target.z - m_Position.z);
    //Get the length of VectorTo
    double VectorLength = sqrt(VectorTo.x*VectorTo.x +
                               VectorTo.y*VectorTo.y +
                               VectorTo.z*VectorTo.z);
    //Normalize VectorTo
    VectorTo.x /= VectorLength;
    VectorTo.y /= VectorLength;
    VectorTo.z /= VectorLength;

    //Straight-ahead vector
    sf::Vector3<double> LocalVector = m_Orientation.MultVect(sf::Vector3<double>(0, 0, -1));

    //Get the cross product as the axis of rotation
    sf::Vector3<double> Axis(VectorTo.y*LocalVector.z - VectorTo.z*LocalVector.y,
                             VectorTo.z*LocalVector.x - VectorTo.x*LocalVector.z,
                             VectorTo.x*LocalVector.y - VectorTo.y*LocalVector.x);
    //Normalize the axis
    //Get the length of VectorTo
    double AxisLength = sqrt(Axis.x*Axis.x +
                             Axis.y*Axis.y +
                             Axis.z*Axis.z);
    //Normalize VectorTo
    Axis.x /= AxisLength;
    Axis.y /= AxisLength;
    Axis.z /= AxisLength;

    //Get the dot product to find the angle
    double DotProduct = (VectorTo.x*LocalVector.x +
                        VectorTo.y*LocalVector.y +
                        VectorTo.z*LocalVector.z);
    double Angle = acos(DotProduct);

    //Determine whether or not the angle is positive
    //Get the cross product of the axis and the local vector
    sf::Vector3<double> ThirdVect(Axis.y*LocalVector.z - Axis.z*LocalVector.y,
                                  Axis.z*LocalVector.x - Axis.x*LocalVector.z,
                                  Axis.x*LocalVector.y - Axis.y*LocalVector.x);
    //If the dot product of that and the local vector is negative, so is the angle
    if (ThirdVect.x*VectorTo.x + ThirdVect.y*VectorTo.y + ThirdVect.z*VectorTo.z < 0)
    {
        Angle = -Angle;
    }

    //Finally, create a quaternion
    //Quaternion AxisAngle;
    m_Orientation.FromAxisAngle(Angle, Axis.x, Axis.y, Axis.z);
    m_Orientation.RotationMatrix(m_RotationMatrix);
}

我的问题是我不知道如何获得对围绕 Z 轴的最终旋转的控制。请注意,当我从 m_Orientation 四元数中提取 Z 角时,它告诉我上图中的船围绕其 Z 轴旋转了 0 弧度。我尝试手动将 Z 分量设置为 0 并重新规范化四元数,但(显然我认为)这不起作用。

目前,计算船“顶朝上”旋转所需的特定 Z 分量是很好的,即它的两个翼尖在同一 Y 平面上,并且它的 Y 轴与可能,但我想更清楚地了解如何操纵最终的 Z 旋转。有什么想法或资源吗?

【问题讨论】:

    标签: c++ math opengl rotation quaternions


    【解决方案1】:

    如果您围绕任意轴执行单次旋转,很自然会得到这个结果。你想象过这种转变吗?

    但是,有一种更简单的方法可以构建旋转矩阵。您只需要找到轴映射到的向量。

    例如如果模型指向 -z,则 z 轴(正向)映射到 z = VectorToz = -VectorTo。要保留向上方向,您需要对其进行定义。假设up = (0, 1, 0)。然后将 x 轴映射到 x = z x up 的叉积。最后,您需要重新计算上向量以保持与y = x x z 的纯旋转。 (中间的 x 表示叉积算子)。

    计算完这些向量后,您现在可以如下构造变换矩阵(使用归一化向量):

        /  x.x  y.x  z.x  0  \
        |  x.y  y.y  z.y  0  |
    T = |  x.z  y.z  z.z  0  |
        \  0    0    0    1  /
    

    根据您的 API,您可能需要转置此矩阵。

    如果需要四元数,可以使用this one之类的方法。

    【讨论】:

    • 谢谢!这主要是可行的,尽管最终结果是船颠倒和倒退。我发现我需要将向上向量设为“(0, -1, 0)”,并将 X 轴向量设为“-VectorTo”,以便船舶正确定向。这可能意味着我在某处的代码中犯了错误,我相信我会找到它。另一方面,这是否意味着没有简单的方法可以直接使用四元数? IIRC Unity 引擎的四元数可以采用向上向量 (docs.unity3d.com/Documentation/ScriptReference/…),它们是否也会在底层使用矩阵?
    • 听起来你真的需要转置矩阵。但如果你有一个可行的解决方案,那没关系。可能有一种方法可以直接计算四元数。但是,我不能说如何。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-27
    • 1970-01-01
    • 2020-10-04
    相关资源
    最近更新 更多