Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/121.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从CMRotationMatrix获取俯仰、偏航和横滚_Ios_Opengl Es_Core Motion - Fatal编程技术网

Ios 从CMRotationMatrix获取俯仰、偏航和横滚

Ios 从CMRotationMatrix获取俯仰、偏航和横滚,ios,opengl-es,core-motion,Ios,Opengl Es,Core Motion,我有一个CMRotationMatrix*rot,我想从矩阵中得到俯仰、偏航和滚动。 你知道我该怎么做吗 谢谢 从矩阵中俯仰、偏航、滚动。你知道我该怎么做吗 按哪个顺序?俯仰角、偏航角和横滚角,通常称为欧拉角,并不明确表示旋转。根据执行单个子旋转的顺序,最终会得到完全不同的旋转矩阵 我个人的建议是:根本不要使用欧拉角,它们只会带来(数值)麻烦。使用矩阵(您已经这样做了)或四元数。我自己找到的: CMAttitude *currentAttitude = motionManager.deviceM

我有一个CMRotationMatrix*rot,我想从矩阵中得到俯仰、偏航和滚动。 你知道我该怎么做吗

谢谢

从矩阵中俯仰、偏航、滚动。你知道我该怎么做吗

按哪个顺序?俯仰角、偏航角和横滚角,通常称为欧拉角,并不明确表示旋转。根据执行单个子旋转的顺序,最终会得到完全不同的旋转矩阵

我个人的建议是:根本不要使用欧拉角,它们只会带来(数值)麻烦。使用矩阵(您已经这样做了)或四元数。

我自己找到的:

CMAttitude *currentAttitude = motionManager.deviceMotion.attitude;    

        if (currentAttitude == nil)
        {
            NSLog(@"Could not get device orientation.");
            return;
        }
        else {
            float PI = 3.14159265;
            float yaw = currentAttitude.yaw * 180/PI;
            float pitch = currentAttitude.pitch * 180/PI;
            float roll = currentAttitude.roll * 180/PI;

        }

使用四元数比使用欧拉角更好。。。。横摇、俯仰和偏航值可使用以下公式从四元数推导得出:

roll  = atan2(2*y*w - 2*x*z, 1 - 2*y*y - 2*z*z)
pitch = atan2(2*x*w - 2*y*z, 1 - 2*x*x - 2*z*z)
yaw   =  asin(2*x*y + 2*z*w)
它可以实现为:

CMQuaternion quat = self.motionManager.deviceMotion.attitude.quaternion;
myRoll = radiansToDegrees(atan2(2*(quat.y*quat.w - quat.x*quat.z), 1 - 2*quat.y*quat.y - 2*quat.z*quat.z)) ;
myPitch = radiansToDegrees(atan2(2*(quat.x*quat.w + quat.y*quat.z), 1 - 2*quat.x*quat.x - 2*quat.z*quat.z));
myYaw = radiansToDegrees(asin(2*quat.x*quat.y + 2*quat.w*quat.z));
#define radiansToDegrees(x) (180/M_PI)*x
其中radianstoDegrees是一个预处理器指令,其实现方式如下:

CMQuaternion quat = self.motionManager.deviceMotion.attitude.quaternion;
myRoll = radiansToDegrees(atan2(2*(quat.y*quat.w - quat.x*quat.z), 1 - 2*quat.y*quat.y - 2*quat.z*quat.z)) ;
myPitch = radiansToDegrees(atan2(2*(quat.x*quat.w + quat.y*quat.z), 1 - 2*quat.x*quat.x - 2*quat.z*quat.z));
myYaw = radiansToDegrees(asin(2*quat.x*quat.y + 2*quat.w*quat.z));
#define radiansToDegrees(x) (180/M_PI)*x
这样做是为了将公式给出的弧度值转换为度

有关转换的更多信息,请参见此处:
这里:。

M_PI
将是PI更精确的表示形式,并保存变量;)我很好奇,“使用四元数比使用欧拉角更好”背后的原因是什么?谢谢。它工作得很好。motionManager.deviceMotion.attitude.pitch/roll/yaw确实不稳定。我想知道苹果为什么不修复它们。这是因为欧拉角可能会掉进去,你失去了一个无法成功恢复的自由度。有一些变通方法(例如检测接近万向节锁定状态),但更好、更有效的解决方案是使用其他表示法(四元数或旋转矩阵)。欧拉角仍然易于使用(和理解),并且适用于许多用例。您如何将这些值转换为0:360度范围?我同意这里的其他评论;Apple的横摇/俯仰/偏航值似乎不稳定/易受万向节锁定条件的影响。具体来说,当我大幅改变设备的音高时,滚动值往往变得不稳定,即使我实际上没有“滚动”设备。