iPhone旋转四元数绝对坐标? [英] iPhone rotation quaternion absolute coordinates?

查看:43
本文介绍了iPhone旋转四元数绝对坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一部带陀螺仪的 iPhone.

I have an iPhone with gyroscope.

假设我有一个手机旋转 Q 的四元数.

Let's say I have a quaternion of the phone rotation Q.

我想在屏幕上显示相对于世界绝对坐标的点.因此,随着我手机的每次旋转,这些点将在真实的 3d 空间(某种增强现实)中静止".假设它是形成一个矩形的 4 个点.

I want to show points on screen relative of the world absolute coordinates. So with every rotation of my phone this points will be "still" in the real 3d space (sort of augmented reality). Let's say it is 4 points forming a rectangle.

因此,我在相对于手机屏幕的 3d 空间中创建了 4 个点,并将 Q 的变换应用于每个点.

So I have created 4 points in 3d space relative to my phone screen and apply the transformation of Q to each of it.

我认为它应该很简单,但我的点不是相对于世界坐标而是相对于我不理解的坐标进行了转换,可能与手机轴有关?.你能帮我解决这个问题吗?我需要在屏幕上创建新视图,该视图将从绝对 3d 空间中的虚拟点投影到旋转的相机.

I thought it should be very simple but my points get transformed not relative to the world coordinates but to the something coordinates I don't understand, may be phone axis related?. Could you please help me with this? I need to create new view on the screen which will be projection from the virtual points in absolute 3d space to the rotated camera.

只要我不沿正常"轴(垂直于屏幕)旋转手机,我的旋转结果似乎就正确.但是在那个方向上旋转会导致完全错误的点平移.

My rotation results seems right as long as I am not rotating the phone along its 'normal' axis (perpendicular to screen). But rotation on that direction results in completely wrong points translation.

包含伪代码.

motionManager.StartDeviceMotionUpdates

motionManager.StartDeviceMotionUpdates

        Quaternion Q;//quaternion read from CMAttitude above, relative frame: XArbitraryZVertical
        var Qi=Q.Conjugate;

        var vX = new Vector3d (-1, 0, 2);
        var vY = new Vector3d (1, 0, 2);
        var vZ = new Vector3d (1, 0, -2);
        var vW = new Vector3d (-1, 0, -2);

        var vXn=Vector3d.Transform(vX,Qi);
        var vYn=Vector3d.Transform(vX,Qi);
        var vZn=Vector3d.Transform(vX,Qi);
        var vWn=Vector3d.Transform(vW,Qi);

        var convertPixels = 50;

        vXn = vXn * convertPixels;
        vYn = vYn * convertPixels;
        vZn = vZn * convertPixels;
        vWn = vWn * convertPixels;

        //screen projection

        X.Frame = new RectangleF (new PointF ((float)(videoArea.Width / 2 + vXn.X), (float)(videoArea.Height / 2 + vXn.Z)), new SizeF (10, 10));
        Y.Frame = new RectangleF (new PointF ((float)(videoArea.Width / 2 + vYn.X), (float)(videoArea.Height / 2 + vYn.Z)), new SizeF (10, 10));
        Z.Frame = new RectangleF (new PointF ((float)(videoArea.Width / 2 + vZn.X), (float)(videoArea.Height / 2 + vZn.Z)), new SizeF (10, 10));
        W.Frame = new RectangleF (new PointF ((float)(videoArea.Width / 2 + vWn.X), (float)(videoArea.Height / 2 + vWn.Z)), new SizeF (10, 10));

推荐答案

最后我用 OpenGL 矩阵完成了.我使用手机给出的旋转矩阵和生成的投影矩阵的乘法.关键功能是将旋转坐标从 iPhone 转换为 OpenGL(右手到左手).我用 -x 和 -y 获得了新的旋转轴,从这个新的轴和角度创建了新的旋转矩阵.这个矩阵给了我正确的结果.

Finally I done it with OpenGL matrices. I use multiplication of the rotation matrix given from phone and the generated projection matrix. The key feature was transforming the coordinates of rotation from the iPhone to OpenGL (right handed to left-handed). I got the new axis of rotation with -x and -y, created the new rotation matrix from this new axis and angle. And that matrix gives me the right results.

这篇关于iPhone旋转四元数绝对坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆