Android陀螺仪 - OpenGL LookAt位置

时间:2012-08-06 09:53:07

标签: android opengl-es sensor gyroscope

我希望通过陀螺仪传感器的能力获得我想要“看看”的X,Y,Z位置的确切位置

我能够从陀螺仪传感器获得9值,但我无法弄清楚这个陀螺仪传感器确切位置的公式。

我希望公式或示例得到正确的答案

以下是我想要结果的代码

double standX = 0.0f, standY = 0.0f, standZ = 0.0f;
double lookX = 0.0f, lookY = 0.0f, lookZ = 1.0f;
double headupX = 0.0f, headupY = 1.0f, headupZ = 0.0f;
float[] deltaRotationMatrix = new float[9];
SensorManager.getRotationMatrixFromVector(deltaRotationMatrix, deltaRotationVector);

// code here

// in my onDraw
// I'm trying to make the lookAt position proper
Matrix.setLookAtM(mViewMatrix, 0, 
        eyeX, eyeY, eyeZ, lookX, lookY, lookZ, headupX , headupY, headupZ);

结果示例如下:

example of x=0, y=0, z=-1

example of x=0, y=1, z=0

example of x=1, y=0, z=1

1 个答案:

答案 0 :(得分:1)

我已经完成了我的问题 这是我的解决方案

首先,我需要制作这个3矩阵来得到x,y,z位置的结果

private float[] mRotXMatrix = new float[] {
 1, 0, 0, 0, 
 0, 0, 1, 0,
 0, 1, 0, 0,
 0, 0, 0, 1 };
private float[] mRotYMatrix = new float[] {
 0, 0, 1, 0,
 0, 1, 0, 0,
 1, 0, 0, 0,
 0, 0, 0, 1 };
private float[] mRotZMatrix = new float[] { 
 0, 1, 0, 0,
 1, 0, 0, 0,
 0, 0, 1, 0,
 0, 0, 0, 1 };

然后在onSensorChanged(SensorEvent e)方法中,我将所有这3个矩阵与我的相机视图一起旋转

Matrix.multiplyMM(mViewMatrix, 0, deltaRotationMatrix, 0, mViewMatrix, 0);
Matrix.multiplyMM(mRotXMatrix, 0, deltaRotationMatrix, 0, mRotXMatrix, 0);
Matrix.multiplyMM(mRotYMatrix, 0, deltaRotationMatrix, 0, mRotYMatrix, 0);
Matrix.multiplyMM(mRotZMatrix, 0, deltaRotationMatrix, 0, mRotZMatrix, 0);

要获得我的相机视图的X,Y,Z,只需从这些矩阵中获取

viewX = mRotXMatrix[2];
viewY = -mRotYMatrix[6]; // +/- up to your initial camera view
viewZ = mRotZMatrix[10];