使用 iPhone/iPod Touch 加速旋转 3D 对象

发布于 2024-09-08 19:32:30 字数 648 浏览 2 评论 0原文

我正在尝试使用 iPhone/iPod 加速来直接操作 3D 对象。

为此,我一直在搜索很多东西(欧拉角、四元数等)。

我正在使用 OpenSG,其中我有一个 3D 环境,并且想要操纵某个对象(仅使用加速度计以所有可能的 iPhone/iPod 自由度旋转)。

因此,我试图找出解决这个问题的方法,但它仍然没有达到预期的结果,并且在某些角度出现一些奇怪的旋转。

有人可以告诉我我做错了什么吗?或者,有没有更好的方法来做到这一点而不使用四元数?

acceleration 变量是一个 Vec3f,包含来自 iPhone/iPod 的经过低通滤波器过滤的加速度计值。

acceleration.normalize();
Vec3f reference = OSG::Vec3f(0, 0, 1);
OSG::Vec3f axis = acceleration.cross( reference );
angle = acos( acceleration.dot( reference ) );
OSG::Quaternion quat;
quat.setValueAsAxisRad(axis, angle);

在此代码之后,我使用四元数 quat 更新我的场景节点。

I'm trying to use an iPhone/iPod acceleration to manipulate directly a 3D object.

For that I've been searching lot's of stuff (Euler angles, Quaternions, etc).

I'm using OpenSG, where I have a 3D environment and want to manipulate a certain object (just rotating in all possible iPhone/iPod degrees of freedom using only accelerometer).

So, I tried to figure it out a solution for this problem but it still doesn't have the expected result and get some weird rotations in some angles.

Can someone tell me what I'm doing wrong? Or, is there a better way of doing this without using quaternions?

The acceleration variable is a Vec3f containing the accelerometer values from iPhone/iPod filtered with a low-pass filter.

acceleration.normalize();
Vec3f reference = OSG::Vec3f(0, 0, 1);
OSG::Vec3f axis = acceleration.cross( reference );
angle = acos( acceleration.dot( reference ) );
OSG::Quaternion quat;
quat.setValueAsAxisRad(axis, angle);

After this code, I update my scene node using quaternion quat.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晨敛清荷 2024-09-15 19:32:30

我想做同样的事情,只是尝试了一下,我之前没有玩过加速度计,看起来应该是可能的。

问题是,如果你将 iPhone 放在桌子上,然后慢慢旋转它并观察加速计的输出,它基本上不会改变(重力下降一倍)。如果您在四个边缘中的任何一个上向上/向下倾斜它,您将看到输出变化。

换句话说,您知道您的桌面正在向上/向下或向左/向右倾斜,但您无法判断您正在旋转它。因此,您可以将此倾斜映射到 3D 对象的两次旋转。

您可能可以使用指南针进行水平旋转,但我无法尝试,因为我正在 Unity 游戏引擎中进行原型设计,而且它似乎还不支持指南针。

I wanted to do the exact same thing and just tried it, I hadn't played around with an accelerometer before and it seemed like it should be possible.

The problem is that if you set your iPhone on a table and then slowly spin it around and observe the output of the accelerometer it basically doesn't change (one gravity down). If you tilt it up/down on any of the four edges you will see the output change.

In other words you know that your table top is tilting top/bottom or left/right, but you can't tell that you are spinning it. So you can map this tilt to two rotations of a 3D object.

You could probably use the compass for the horizontal rotation, I couldn't try because I was prototyping in the Unity Game Engine and it doesn't seem to support compass yet.

与往事干杯 2024-09-15 19:32:30

出色的 Brad Larson 在编写 Moleculs 应用程序时发布了关于他最初使用 3D 查看器的体验的精彩描述。

他的旋转方法是通过以下方式实现的:

GLfloat currentModelViewMatrix[16];
glGetFloatv(GL_MODELVIEW_MATRIX, currentModelViewMatrix);   
glRotatef(xRotation, currentModelViewMatrix[1], currentModelViewMatrix[5], currentModelViewMatrix[9]);
glGetFloatv(GL_MODELVIEW_MATRIX, currentModelViewMatrix);
glRotatef(yRotation, currentModelViewMatrix[0], currentModelViewMatrix[4], currentModelViewMatrix[8]);

但是无论这是否有帮助,我都无法推荐这篇博客文章Brad 学到了一两课

编辑补充说,我可能误读了这个问题,但会保留此处的帖子,因为它可能会帮助人们使用类似的关键字进行搜索。

The ever wonderful Brad Larson posted an excellent description of his initial experiences of a 3d viewer while writing his Moleculs app.

His method for rotations was achieved as follows:

GLfloat currentModelViewMatrix[16];
glGetFloatv(GL_MODELVIEW_MATRIX, currentModelViewMatrix);   
glRotatef(xRotation, currentModelViewMatrix[1], currentModelViewMatrix[5], currentModelViewMatrix[9]);
glGetFloatv(GL_MODELVIEW_MATRIX, currentModelViewMatrix);
glRotatef(yRotation, currentModelViewMatrix[0], currentModelViewMatrix[4], currentModelViewMatrix[8]);

but whether or not this is helpful I can't recommend this blog entry enough Brad learns a lesson or two

Editing to add that I may have misread the question, but will keep the post here as it will likely help people searching with similar keywords.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文