5

我的部分代码通过存储 xyz 位置、xyz 比例和四元数来存储相当于 4x3 矩阵的内容。下面的代码片段:

class tTransform
{

    // data
    tVector4f    m_Position;
    tQuaternion  m_Rotation;
    tVector4f    m_Scale;

};

我想将这些对象中的 2 个相乘,(就好像它是矩阵相乘一样),并且想知道是否有比将每个对象转换为矩阵更快/更好的方法,以这种方式进行相乘,然后提取结果位置,旋转并再次缩小?

4

3 回答 3

3

健康警告,因为这是来自记忆并且完全未经测试。您需要为tQuaternions 和tVector4s 定义或替换运算符。

class tTransform
{

    // data
    tVector4f    m_Position;
    tQuaternion  m_Rotation;
    tVector4f    m_Scale;

public:
    // World = Parent * Local (*this == parent)
    tTransform operator * (const tTransform& localSpace)
    {
        tTransform worldSpace;
        worldSpace.m_Position = m_Position + 
                                m_Rotation * (localSpace.m_Position * m_Scale);
        worldSpace.m_Rotation = m_Rotation * localSpace.m_Rotation;
        worldSpace.m_Scale = m_Scale * (m_Rotation * localSpace.m_Scale);
        return worldSpace;
    }

    // Local = World / Parent (*this = World)
    tTransform operator / (const tTransform& parentSpace)
    {
        tTransform localSpace;
        tQuaternion parentSpaceConjugate = parentSpace.m_Rotation.conjugate(); 
        localSpace.m_Position = (parentSpaceConjugate * 
                                (m_Position - parentSpace.m_Position)) /
                                parentSpace.m_Scale;

        localSpace.m_Rotation = parentSpaceConjugate * m_Rotation;

        localSpace.m_Scale = parentSpaceConjugate *
                             (m_Scale / parentSpace.m_Scale);
        return localSpace;
    }
};
于 2012-01-07T15:00:06.820 回答
1

我回答了 Tomas 的问题,我也会在这里复制,因为它也回答了你的问题。答案几乎是伪代码,因此您应该能够将其应用到您的课程中。您没有指定使用哪个顺序构建矩阵(TRS 或 SRT),所以我假设为 TRS。(你的向量是列)

transform transform::operator * (const transform &other) const
{
    // mat1 = T1 * R1 * S1; mat2 = T2 * R2 * S2
    // mat = mat1 * mat2; mat*v = mat1 * mat2 * v
    // assuming "this" is mat1, and other is mat2
    // alternatively "this" can be considered parent, and other child in a node hierarchy
    transform r;
    // R = R1 * R2
    r.orientation = orientation * other.orientation;
    // Note: I don't know how to implement inverse of quat in your lib
    // S = R2^-1 * (S1 * (R2 * S2))
    r.scale = inverse(other.orientation) * (scale * (other.orientation * other.scale));
    // T = T1 * (R1 * (S1 * T2))
    r.position = position + (orientation * (scale * other.position));
    return r;
}

您可以在此处了解如何通过四元数旋转矢量: https ://gamedev.stackexchange.com/questions/28395/rotating-vector3-by-a-quaternion

于 2020-04-22T07:06:55.253 回答
-1

有人告诉我,这在一般情况下是不可能的。请参阅https://gamedev.stackexchange.com/questions/167287/combine-two-translation-rotation-scale-triplets-without-matrices

问题是结构不能表示剪切,在结合旋转和非均匀缩放后可能需要它。

如果我错了,请纠正我。

于 2019-07-28T18:31:09.210 回答