我回答了 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