开发者

Using iPhone/iPod Touch acceleration to rotate a 3D object

开发者 https://www.devze.com 2023-01-06 17:25 出处:网络
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 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 wa开发者_开发技巧y 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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消