四元数与欧拉角之间的转换

转自:http://www.cppblog.com/heath/archive/2009/12/13/103127.html

在3D图形学中,最常用的旋转表示方法便是四元数和欧拉角,比起矩阵来具有节省存储空间和方便插值的优点。本文主要归纳了两种表达方式的转换,计算公式采用3D笛卡尔坐标系:

图1 3D Cartesian coordinate System (from wikipedia)

定义分别为绕Z轴、Y轴、X轴的旋转角度,如果用Tait-Bryan angle表示,分别为Yaw、Pitch、Roll。

图2 Tait-Bryan angles (from wikipedia)

一、四元数的定义

通过旋转轴和绕该轴旋转的角度可以构造一个四元数:

其中是绕旋转轴旋转的角度,为旋转轴在x,y,z方向的分量(由此确定了旋转轴)。

二、欧拉角到四元数的转换

三、四元数到欧拉角的转换

arctanarcsin的结果是,这并不能覆盖所有朝向(对于的取值范围已经满足),因此需要用atan2来代替arctan

四、在其他坐标系下使用

在其他坐标系下,需根据坐标轴的定义,调整一下以上公式。如在Direct3D中,笛卡尔坐标系的X轴变为Z轴,Y轴变为X轴,Z轴变为Y轴(无需考虑方向)。

五、示例代码

http://www.cppblog.com/Files/heath/Euler2Quaternion.rar
Demo渲染两个模型,左边使用欧拉角,右边使用四元数,方向键Up、Left、Right旋转模型。

参考文献:
[1] http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles
[2] Ken Shoemake, Animating Rotation with Quaternion Curves, 1985

posted on 2009-12-13 18:44 Heath

void Quaternion::SetEulerAngle(const EulerAngle &ea) { float fCosHRoll = cos(ea.m_fRoll * .5f); float fSinHRoll = sin(ea.m_fRoll * .5f); float fCosHPitch = cos(ea.m_fPitch * .5f); float fSinHPitch = sin(ea.m_fPitch * .5f); float fCosHYaw = cos(ea.m_fYaw * .5f); float fSinHYaw = sin(ea.m_fYaw * .5f); /// Cartesian coordinate System //w = fCosHRoll * fCosHPitch * fCosHYaw + fSinHRoll * fSinHPitch * fSinHYaw; //x = fSinHRoll * fCosHPitch * fCosHYaw - fCosHRoll * fSinHPitch * fSinHYaw; //y = fCosHRoll * fSinHPitch * fCosHYaw + fSinHRoll * fCosHPitch * fSinHYaw; //z = fCosHRoll * fCosHPitch * fSinHYaw - fSinHRoll * fSinHPitch * fCosHYaw; w = fCosHRoll * fCosHPitch * fCosHYaw + fSinHRoll * fSinHPitch * fSinHYaw; x = fCosHRoll * fSinHPitch * fCosHYaw + fSinHRoll * fCosHPitch * fSinHYaw; y = fCosHRoll * fCosHPitch * fSinHYaw - fSinHRoll * fSinHPitch * fCosHYaw; z = fSinHRoll * fCosHPitch * fCosHYaw - fCosHRoll * fSinHPitch * fSinHYaw; } EulerAngle Quaternion::GetEulerAngle() const { EulerAngle ea; /// Cartesian coordinate System //ea.m_fRoll = atan2(2 * (w * x + y * z) , 1 - 2 * (x * x + y * y)); //ea.m_fPitch = asin(2 * (w * y - z * x)); //ea.m_fYaw = atan2(2 * (w * z + x * y) , 1 - 2 * (y * y + z * z)); ea.m_fRoll = atan2(2 * (w * z + x * y) , 1 - 2 * (z * z + x * x)); ea.m_fPitch = asin(CLAMP(2 * (w * x - y * z) , -1.0f , 1.0f)); ea.m_fYaw = atan2(2 * (w * y + z * x) , 1 - 2 * (x * x + y * y)); return ea; }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值