我的部分代码通过存储xyz位置,xyz刻度和四元数来存储等效的4x3矩阵。以下代码段:
class tTransform
{
// data
tVector4f m_Position;
tQuaternion m_Rotation;
tVector4f m_Scale;
};
我想将这些对象中的两个相乘,(好像它是一个矩阵乘法),并且我想知道是否有更快/更好的方法来做到比将每个对象转换为矩阵,做到这样倍增,然后再次提取结果位置,旋转和缩放?
答案 0 :(得分:3)
健康警告,因为这是来自内存并且完全未经测试。
您需要定义或替换tQuaternion
和tVector4
s。
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;
}
};
答案 1 :(得分:0)
我回答了托马斯(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
答案 2 :(得分:-1)
有人告诉我这在一般情况下是不可能的。 参见https://gamedev.stackexchange.com/questions/167287/combine-two-translation-rotation-scale-triplets-without-matrices
问题是结构不能代表剪切力,这在组合旋转和非均匀缩放后可能需要。
请纠正我的错误。