欧拉角转成四元数的公式
2006-07-18 18:20
363 查看
这两天看MDL模型的显示代码,参考《3D数学基础:图形与游戏开发》中的公式,发现欧拉角转成四元数的公式和书中提到的公式不一致,狂晕!~~~
void AngleQuaternion(const vec3_t Angles, vec4_t Quaternion)
float Angle = Angles[0] * 0.5f;
float Sin0 = (float)sin(Angle);
float Cos0 = (float)cos(Angle);
Angle = Angles[1] * 0.5f;
float Sin1 = (float)sin(Angle);
float Cos1 = (float)cos(Angle);
Angle = Angles[2] * 0.5f;
float Sin2 = (float)sin(Angle);
float Cos2 = (float)cos(Angle);
Quaternion[0] = Sin0 * Cos1 * Cos2 - Cos0 * Sin1 * Sin2;
Quaternion[1] = Cos0 * Sin1 * Cos2 + Sin0 * Cos1 * Sin2;
Quaternion[2] = Cos0 * Cos1 * Sin2 - Sin0 * Sin1 * Cos2;
Quaternion[3] = Cos0 * Cos1 * Cos2 + Sin0 * Sin1 * Sin2;
}
书上的公式是这样的:
|cos(h/2) |
| 0 |
h=|sin(-h/2) |
| 0 |
|cos(p/2) |
|-sin(p/2) |
p=| 0 |
| 0 |
|cos(b/2) |
| 0 |
b=| 0 |
|-sin(b/2) |
| cos(h/2)cos(p/2)cos(b/2)+sin(h/2)sin(p/2)sin(b/2)| (w)
|cos(h/2)sin(p/2)cos(b/2)+sin(h/2)cos(p/2)sin(b/2) | (x)
q惯性-物体(h,p,b) = |sin(h/2)cos(p/2)cos(b/2)-cos(h/2)sin(p/2)sin(b/2) | (y)
|cos(h/2)cos(p/2)sin(b/2)-sin(h/2)sin(p/2)cos(b/2) | (z)
上面的代码与公式比较以后,发现有点像这样:
Quaternion[0] = y;
Quaternion[1] = x;
Quaternion[2] = z;
Quaternion[3] = w;
这样明显是不对的,而四元数转矩阵的时候,用的四元数应该是
Quaternion[0] = x;
Quaternion[1] = y;
Quaternion[2] = z;
Quaternion[3] = w;
颠倒顺序乘也都不对
今天想起了前些天摘录的文章《Quaternion Powers》
里面提到的欧拉角转换成四元数的公式:
So if you have three Euler angles (a, b, c), then you can form three independent quaternions
Qx = [ cos(a/2), (sin(a/2), 0, 0)]
Qy = [ cos(b/2), (0, sin(b/2), 0)]
Qz = [ cos(c/2), (0, 0, sin(c/2))]
And the final quaternion is obtained by Qx * Qy * Qz.
算出来的结果跟代码中的是一样的。竟然是两种截然不同的转换公式,狂晕一个先。具体的原因还不知道 :(,搞清楚了再贴上来。
公式如下:
| cos(a/2)*cos(b/2)*cos(c/2)+sin(a/2)*sin(b/2)*sin(c/2) | (w)
| sin(a/2)*cos(b/2)*cos(c/2)-cos(a/2)*sin(b/2)*sin(c/2) | (x)
q=| cos(a/2)*sin(b/2)*cos(c/2)+sin(a/2)*cos(b/2)*sin(c/2) | (y)
| cos(a/2)*cos(b/2)*sin(c/2)-sin(a/2)*sin(b/2)*cos(c/2) | (z)
void AngleQuaternion(const vec3_t Angles, vec4_t Quaternion)
float Angle = Angles[0] * 0.5f;
float Sin0 = (float)sin(Angle);
float Cos0 = (float)cos(Angle);
Angle = Angles[1] * 0.5f;
float Sin1 = (float)sin(Angle);
float Cos1 = (float)cos(Angle);
Angle = Angles[2] * 0.5f;
float Sin2 = (float)sin(Angle);
float Cos2 = (float)cos(Angle);
Quaternion[0] = Sin0 * Cos1 * Cos2 - Cos0 * Sin1 * Sin2;
Quaternion[1] = Cos0 * Sin1 * Cos2 + Sin0 * Cos1 * Sin2;
Quaternion[2] = Cos0 * Cos1 * Sin2 - Sin0 * Sin1 * Cos2;
Quaternion[3] = Cos0 * Cos1 * Cos2 + Sin0 * Sin1 * Sin2;
}
书上的公式是这样的:
|cos(h/2) |
| 0 |
h=|sin(-h/2) |
| 0 |
|cos(p/2) |
|-sin(p/2) |
p=| 0 |
| 0 |
|cos(b/2) |
| 0 |
b=| 0 |
|-sin(b/2) |
| cos(h/2)cos(p/2)cos(b/2)+sin(h/2)sin(p/2)sin(b/2)| (w)
|cos(h/2)sin(p/2)cos(b/2)+sin(h/2)cos(p/2)sin(b/2) | (x)
q惯性-物体(h,p,b) = |sin(h/2)cos(p/2)cos(b/2)-cos(h/2)sin(p/2)sin(b/2) | (y)
|cos(h/2)cos(p/2)sin(b/2)-sin(h/2)sin(p/2)cos(b/2) | (z)
上面的代码与公式比较以后,发现有点像这样:
Quaternion[0] = y;
Quaternion[1] = x;
Quaternion[2] = z;
Quaternion[3] = w;
这样明显是不对的,而四元数转矩阵的时候,用的四元数应该是
Quaternion[0] = x;
Quaternion[1] = y;
Quaternion[2] = z;
Quaternion[3] = w;
颠倒顺序乘也都不对
今天想起了前些天摘录的文章《Quaternion Powers》
里面提到的欧拉角转换成四元数的公式:
Euler to Quaternion
Converting from Euler angles to a quaternion is slightly more tricky, as the order of operations must be correct. Since you can convert the Euler angles to three independent quaternions by setting the arbitrary axis to the coordinate axes, you can then multiply the three quaternions together to obtain the final quaternion.So if you have three Euler angles (a, b, c), then you can form three independent quaternions
Qx = [ cos(a/2), (sin(a/2), 0, 0)]
Qy = [ cos(b/2), (0, sin(b/2), 0)]
Qz = [ cos(c/2), (0, 0, sin(c/2))]
And the final quaternion is obtained by Qx * Qy * Qz.
算出来的结果跟代码中的是一样的。竟然是两种截然不同的转换公式,狂晕一个先。具体的原因还不知道 :(,搞清楚了再贴上来。
公式如下:
| cos(a/2)*cos(b/2)*cos(c/2)+sin(a/2)*sin(b/2)*sin(c/2) | (w)
| sin(a/2)*cos(b/2)*cos(c/2)-cos(a/2)*sin(b/2)*sin(c/2) | (x)
q=| cos(a/2)*sin(b/2)*cos(c/2)+sin(a/2)*cos(b/2)*sin(c/2) | (y)
| cos(a/2)*cos(b/2)*sin(c/2)-sin(a/2)*sin(b/2)*cos(c/2) | (z)
相关文章推荐
- 四元数、欧拉角及方向余弦矩阵的相互转换公式
- 3D数学基础公式------>矩阵,欧拉角,四元数,之间转换<2>
- 四元数、欧拉角及方向余弦矩阵的相互转换公式
- 四元数、欧拉角及方向余弦矩阵的相互转换公式
- 欧拉角转成四元数的公式
- [原]Unity手游之路 四 3d旋转-四元数,欧拉角和变幻矩阵
- 四元数与欧拉角之间的转换
- 笔记——关于欧拉角与四元数
- 旋转矩阵 欧拉角 四元数
- 矩阵,欧拉角,四元数比较
- 矩阵、欧拉角、轴-角对、四元数随笔
- 3D数学之-向量矩阵欧拉角和四元数
- 四元数与欧拉角(yaw roll pitch)转换公式与推导,以及它们unity中的不同
- 三维旋转:旋转矩阵,欧拉角,四元数
- OSG 四元数与欧拉角互转
- 四元数与欧拉角---wii 的坐标系转换
- XNA旋转:矩阵,四元数和欧拉角向量 .
- 旋转矩阵——四元数,欧拉角
- 四元数与欧拉角之间的转换
- Matrix4x4矩阵变换、欧拉角转四元数、角度转弧度