I am working on an app that involves drawing a line on my android screen based on its orientation and could use some help or pointers.
The line is drawn in the following way: If the phone is held flat then then the line shrinks and becomes a dot and as the phone is tilted and orientatated the line becomes bigger - ie the phone stood up and the line points down and is max magnitude of 9.8 and held flat it is a small dot. Crucialy no matter what angle the phone is held at the arrow allways points down- ie the line of gravity.
Now I figured out how to calculate the yaw pitch and roll angles of the phones but mathematically I am a bit lost on how to derive the vector of this line from this information - any pointers would be most welcome开发者_运维问答.
Thanks
Ok so I figured this out with a lot of help from the Replica Island source and in turn an Nvidia paper.
Once you have the pitch, roll, yaw from the TYPE_ORIENTATION sensor reading:
@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....
}
}
Here is the canonicalOrientationToScreenOrientation function:
// 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];
}
精彩评论