【问题标题】:Computing Vector from Quaternion works, computing Quaternion from Vector does not从四元数计算向量有效,从向量计算四元数不
【发布时间】:2013-02-03 01:57:39
【问题描述】:

所以我使用四元数在 3D 空间中创建一个由两个点组成的段,并稍后尝试重新计算一个类似的四元数(一个表示穿过空间的相同向量;我知道该段围绕自身的旋转是未定义的) .我正在创建这样的细分:

sf::Vector3<float> Start(0, 0, 0);
sf::Vector3<float> End = Start;

//Create a vector from the start to the end
sf::Vector3<float> Translation = Orientation.MultVect(sf::Vector3<float>(0, 1, 0));

//Add that vector onto the start position
End.x += Translation.x * Length;
End.y += Translation.y * Length;
End.z += Translation.z * Length;

Orientation::MultVect() 如下所示:

sf::Vector3<float> Quaternion::MultVect(sf::Vector3<float> Vector)
{
    //From http://www.idevgames.com/articles/quaternions
    Quaternion VectorQuat = Quaternion();
    VectorQuat.x = Vector.x;
    VectorQuat.y = Vector.y;
    VectorQuat.z = Vector.z;
    VectorQuat.w = 0.0;

    Quaternion Inverse = (*this);
    Inverse.Invert();

    Quaternion Result = Inverse * VectorQuat * (*this);

    sf::Vector3<float> ResultVector;
    ResultVector.x = Result.x;
    ResultVector.y = Result.y;
    ResultVector.z = Result.z;

    return ResultVector;
}

现在这个函数似乎在其他情况下工作得很好,所以我认为问题不在这里,但你永远不知道。我还应该提到,考虑到我提供的四元数 if(我从欧拉角构造,有时与其他四元数相乘),这一点最终会出现在我期望的地方。

对我来说,问题似乎在于从StartEnd 重新计算四元数。为此,我使用了这个函数,它在将场景中的对象定向到其他对象时效果很好(除非有问题的对象沿着完全相同的 Y 轴,在这种情况下,我得到具有 NaN 值的四元数)。这是我的做法:

Quaternion Quaternion::FromLookVector(sf::Vector3<float> FromPoint, sf::Vector3<float> ToPoint)
{
    ///Based on this post:
    ///http://stackoverflow.com/questions/13014973/quaternion-rotate-to
    //Get the normalized vector from origin position to ToPoint
    sf::Vector3<double> VectorTo(ToPoint.x - FromPoint.x,
                                 ToPoint.y - FromPoint.y,
                                 ToPoint.z - FromPoint.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;

    //Define a unit up vector
    sf::Vector3<double> VectorUp(0, -1, 0);

    //The X axis is the cross product of both
    //Get the cross product as the axis of rotation
    sf::Vector3<double> AxisX(VectorTo.y*VectorUp.z - VectorTo.z*VectorUp.y,
                              VectorTo.z*VectorUp.x - VectorTo.x*VectorUp.z,
                              VectorTo.x*VectorUp.y - VectorTo.y*VectorUp.x);
    //Normalize the axis
    //Get the length of VectorTo
    double AxisXLength = sqrt(AxisX.x*AxisX.x +
                              AxisX.y*AxisX.y +
                              AxisX.z*AxisX.z);

    //Normalize VectorTo
    AxisX.x /= AxisXLength;
    AxisX.y /= AxisXLength;
    AxisX.z /= AxisXLength;

    //Get the adjusted Y vector
    //Get the cross product of the other two axes
    sf::Vector3<double> AxisY(VectorTo.y*AxisX.z - VectorTo.z*AxisX.y,
                              VectorTo.z*AxisX.x - VectorTo.x*AxisX.z,
                              VectorTo.x*AxisX.y - VectorTo.y*AxisX.x);
    //Normalize the axis
    //Get the length of VectorTo
    double AxisYLength = sqrt(AxisY.x*AxisY.x +
                              AxisY.y*AxisY.y +
                              AxisY.z*AxisY.z);
    //Normalize VectorTo
    AxisY.x /= AxisYLength;
    AxisY.y /= AxisYLength;
    AxisY.z /= AxisYLength;

    //A matrix representing the Thing's orientation
    GLfloat RotationMatrix[16] = {(float)AxisX.x,
                                  (float)AxisX.y,
                                  (float)AxisX.z,
                                  0,
                                  (float)AxisY.x,
                                  (float)AxisY.y,
                                  (float)AxisY.z,
                                  0,
                                  (float)VectorTo.x,
                                  (float)VectorTo.y,
                                  (float)VectorTo.z,
                                  0,
                                  0,
                                  0,
                                  0,
                                  1};

    Quaternion LookQuat = Quaternion::FromMatrix(RotationMatrix);

    //Reset the quaternion orientation
    return LookQuat;
}

因此,当我计算分段时,我还会检查它们的重构值,如下所示:

sf::Vector3<float> Start(0, 0, 0);
sf::Vector3<float> End = Start;

//Create a vector from the start to the end
sf::Vector3<float> Translation = Orientation.MultVect(sf::Vector3<float>(0, 1, 0));
//Add that vector onto the start position
End.x += Translation.x * Length;
End.y += Translation.y * Length;
End.z += Translation.z * Length;

std::cout << "STATIC END     (";
std::cout << End.x << ",";
std::cout << End.y << ",";
std::cout << End.z << ")\n";

///TEST
Quaternion Reconstructed = Quaternion::FromLookVector(Start, End);
Translation = Reconstructed.MultVect(sf::Vector3<float>(0, 1, 0));
sf::Vector3<float> TestEnd = Start;
TestEnd.x += Translation.x * Length;
TestEnd.y += Translation.y * Length;
TestEnd.z += Translation.z * Length;

std::cout << "RECONSTRUCTED END (";
std::cout << TestEnd.x << ",";
std::cout << TestEnd.y << ",";
std::cout << TestEnd.z << ")\n";

而且两者不匹配。例如,如果静态终点是 (0,14.3998,0.0558498),那么重新计算的点是 (0,8.05585,-6.39976)。不过,两者应该是相同的。旋转的未定义部分不应该改变终点的位置,只有滚动(或 Z 旋转,或任何你想称之为的),因为这是一个片段,所以无关紧要。

请注意,当我最终将它用于除简单线段以外的其他事物时,滚动 很重要,这就是为什么我使用向上向量来确保沿这些线段放置的对象始终尽可能让它们的顶部朝上(如果需要,可以单独确定垂直向上或向下的物体的特殊任意滚动)。另一个目标是创建多个串在一起的片段,每个片段都相对于前一个片段的方向旋转,而不是相对于全局空间旋转。

那么我在这里做错了什么?为什么我不能重新计算与第一个执行相同转换的第二个四元数?

【问题讨论】:

  • 四元数乘法不通勤。四元数是相对变换(不是绝对变换)。最后,您需要进一步缩小代码范围:仅仅因为代码可能是自洽的,并不意味着它就是您想要的。

标签: c++ math 3d rotation quaternions


【解决方案1】:

我不完全确定您是如何计算两个向量之间的“旋转”四元数,但我很确定它非常麻烦。至少,如果我理解正确的话,你有指向某个方向的“看”向量,并且对象从原点 (0,0,0) 沿着那个方向“看”,对吗?

如果是上述情况,应该不会太难。我觉得很奇怪的一件事是你的四元数 - 向量乘法似乎是相反的顺序。我将四元数 * 向量定义为:

quat qt = *this * quat(0, vec.x, vec.y, vec.z) * inverse();
return vec3(qt.x, qt.y, qt.z);

其中 quat 构造函数定义为 quat(w, x, y, z) 并且 inverse() 方法返回一个副本。逆等于共轭,定义为(w, -x, -y, -z)。但是,要做到这一点,你的四元数必须被归一化,只有这样它们才能真正代表一个方向(只有这样,逆才会等于共轭)。然后我将四元数乘法定义如下:

// This describes A * B (not communative!)
w = A.w * B.w - A.x * B.x - A.y * B.y - A.z * B.z;
x = A.w * B.x + A.x * B.w + A.y * B.z - A.z * B.y;
y = A.w * B.y + A.y * B.w + A.z * B.x - A.x * B.z;
z = A.w * B.z + A.z * B.w + A.x * B.y - A.y * B.x;

除此之外,您希望能够从“角轴”构造一个四元数。这意味着它应该采用一个旋转轴,以及一个围绕该轴旋转的角度(以弧度为单位)。我只给你那个算法,因为它在直觉上没有多大意义:

// if axis is already unit length, remove the division
double halfAngle = angle * 0.5f; // In radians
double scale = sin(halfAngle) / axis.magnitude();

w = cos(halfAngle);
x = axis.x * scale;
y = axis.y * scale;
z = axis.z * scale;

所以现在我们只需要计算一个要围绕它旋转的轴,以及我们想要围绕它旋转多少,以弧度为单位。乍一看,这可能看起来很复杂,但这只是了解正在发生的事情的一个例子。你有两个向量,A 和 B。你想计算一个描述从 A 到 B 的旋转的四元数。为了让轴旋转,我们只需要一个与两者垂直的轴,显然这可以通过取交叉产品。如果您使用右手坐标系,则为:

axis = A × B

如果您使用的是左手坐标系,我认为您应该颠倒顺序,但不要相信我的话。现在得到两个向量之间的角度。这可以通过采用点积来非常简单地完成。唯一的问题是您必须对两个向量进行归一化,因此它们的长度为 1,并且不会改变点积的结果。这样,点积将返回角度的余弦,因此我们可以得到实际的角度:

angle = acos(normalize(A) * normalize(B))

当然,乘号代表点积。现在我们只需将轴和角度插入我上面给你的算法中,我们有一个四元数描述从外观向量 A 到外观向量 B 的“旋转”。现在如果向量指向完全相同的方向,那将是不明智的应用算法,因为轴将是(0,0,0)。如果您查看算法,我希望您看到它要么尝试除以零,要么简单地输出全零。因此,每当我应用该算法时,我首先检查轴是否不全为零。

您当前使用的公式对我来说似乎很奇怪且效率低下。我也不明白你为什么要先计算矩阵,从矩阵计算四元数是一项相当昂贵的计算。事实上,我相信从四元数计算相反的矩阵会更快。

无论如何,祝你好运!

【讨论】:

  • 好吧,事实证明您的评论帮助我弄清楚我在代码的其他地方犯了错误。我在上面粘贴的 Quaternion::FromLookVector() 函数旨在获取两点之间的四元数,而不是两个向量,即来自单个向量;但当然,由于四元数只是相对变换,我需要假设一个默认的外观向量,以使四元数表现得像一个绝对变换。我一直在假设错误的默认外观矢量。
  • 我会考虑您的建议来提高性能;先前的调查表明,最好通过在矩阵表示之间进行转换来从头开始使用预定义的向上向量构建一个查看四元数,尽管我更欢迎一些更快的东西。感谢您抽出宝贵时间回复!
  • 个人认为向上向量的做事方式比较过时。四元数通常用于没有固定此类参数的“动态”旋转,因此不会出现意外错误(例如,沿向上向量直视是灾难性的)。您还可以使用纯四元数和叉积以及不使用任何类型的固定向量来生成相机系统的精确副本。您所需要的只是一个预定义的默认外观矢量,即您的起始方向。我建议你在你的程序中将它定义为 const (或类似的),祝你好运!
  • 对于我正在构建的引擎类型的东西,我实际上实现了一个基于四元数的定向系统。我存储每个节点的比例、位置和方向,然后计算它们的最终节点世界矩阵,因此我只需将该矩阵乘以相机矩阵即可进行渲染。您使用更多内存来存储所有内容,但它非常灵活,在某些情况下您甚至可以减少一些计算。如果你愿意,我可以在 pastebin 或类似的东西上抛出一些算法;)
猜你喜欢
  • 2011-10-05
  • 2012-01-26
  • 1970-01-01
  • 2017-07-16
  • 1970-01-01
  • 1970-01-01
  • 2012-06-11
  • 2019-02-24
  • 1970-01-01
相关资源
最近更新 更多