好的,所以我在 Replica Island 的源代码和 Nvidia 论文的帮助下解决了这个问题。
从 TYPE_ORIENTATION 传感器读取俯仰、滚动、偏航后:
@Override
public void onSensorChanged(SensorEvent event)
{
synchronized (this)
{
m_orientationInput[0] = x;
m_orientationInput[1] = y;
m_orientationInput[2] = z;
canonicalOrientationToScreenOrientation(m_rotationIndex, m_orientationInput, m_orientationOutput);
// Now we have screen space rotations around xyz.
final float horizontalMotion = m_orientationOutput[0] / 90.0f;
final float verticalMotion = m_orientationOutput[1] / 90.0f;
// send details to renderer....
}
}
这是 canonicalOrientationToScreenOrientation 函数:
// From NVIDIA http://developer.download.nvidia.com/tegra/docs/tegra_android_accelerometer_v5f.pdf
private void canonicalOrientationToScreenOrientation(int displayRotation, float[] canVec, float[] screenVec)
{
final int axisSwap[][] =
{
{ 1, -1, 0, 1 }, // ROTATION_0
{-1, -1, 1, 0 }, // ROTATION_90
{-1, 1, 0, 1 }, // ROTATION_180
{ 1, 1, 1, 0 } // ROTATION_270
};
final int[] as = axisSwap[displayRotation];
screenVec[0] = (float)as[0] * canVec[ as[2] ];
screenVec[1] = (float)as[1] * canVec[ as[3] ];
screenVec[2] = canVec[2];
}