我试图使用.NET/C#将欧拉角描述的3D旋转转换为矩阵,然后再转回去。我的约定是:
- 左手系(x向右,y向上,z向前)
- 旋转顺序:绕y轴的偏航,绕x轴的俯仰,绕z轴的转弯
- 使用左手规则进行正旋转(拇指指向+无穷大)
我的尝试是:
欧拉角到矩阵(我已经删除了x、y、z平移部分以简化问题)
Matrix3D matrix = new Matrix3D() {
M11 = cosH * cosB - sinH * sinP * sinB,
M12 = - sinB * cosP,
M13 = sinH * cosB + cosH * sinP * sinB,
M21 = cosH * sinB + sinH * sinP * cosB,
M22 = cosB * cosP,
M23 = sinB * sinH - cosH * sinP * cosB,
M31 = - sinH * cosP,
M32 = - sinP,
M33 = cosH * cosP,
};
矩阵转欧拉角
const double RD_TO_DEG = 180 / Math.PI;
double h, p, b; // angles in degrees
// extract pitch
double sinP = -matrix.M23;
if (sinP >= 1) {
p = 90; } // pole
else if (sinP <= -1) {
p = -90; } // pole
else {
p = Math.Asin(sinP) * RD_TO_DEG; }
// extract heading and bank
if (sinP < -0.9999 || sinP > 0.9999) { // account for small angle errors
h = Math.Atan2(-matrix.M31, matrix.M11) * RD_TO_DEG;
b = 0; }
else {
h = Math.Atan2(matrix.M13, matrix.M33) * RD_TO_DEG;
b = Math.Atan2(matrix.M21, matrix.M22) * RD_TO_DEG; }
这一定有问题。如果我取3个角度,将它们转换成矩阵,再将该矩阵转换回角度,结果与最初的值不同。
我浏览了几个网站,使用不同的公式,从euclideanspace.com开始,但现在我完全迷失了,找不到正确的计算方法。希望能得到一些帮助。有数学家在吗?