我的部分代码通过存储xyz位置、xyz标度和四元数来存储相当于4x3矩阵的内容。下面是代码片段:
class tTransform
{
// data
tVector4f m_Position;
tQuaternion m_Rotation;
tVector4f m_Scale;
};我想将这些对象中的两个相乘(就像是矩阵乘法一样),我想知道是否有比将每个对象转换为矩阵更快/更好的方法,以这种方式相乘,然后提取结果位置,旋转并再次缩放?
发布于 2012-01-07 23:00:07
健康警告,因为这是来自内存,完全未经测试。需要为tQuaternion%s和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;
}
};发布于 2020-04-22 15:06:55
我回答了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
发布于 2019-07-29 02:31:09
我被告知这在一般情况下是不可能的。请参阅https://gamedev.stackexchange.com/questions/167287/combine-two-translation-rotation-scale-triplets-without-matrices
问题是该结构不能表示剪切,在组合旋转和非均匀缩放后可能需要剪切。
如果我说错了,请纠正我。
https://stackoverflow.com/questions/8594102
复制相似问题