问题描述
我正在开发一个应用程序,该应用程序涉及根据其方向在我的android屏幕上绘制一条线,并且可能会使用一些帮助或指针.
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.
按以下方式绘制线:如果将电话放平,则线会收缩并变成点,并且当电话倾斜和定向时,线会变大-即电话站立并且线指向下方并且最大幅度为9.8并保持平坦,这是一个小点.无论手机始终保持在箭头的哪个角度都始终指向下方,也就是重力线.
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.
谢谢
推荐答案
好,所以我在复制岛资源以及Nvidia论文的很多帮助下找到了答案.
Ok so I figured this out with a lot of help from the Replica Island source and in turn an Nvidia paper.
一旦您从TYPE_ORIENTATION传感器的读数获得了俯仰,横滚和偏航:
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....
}
}
以下是canonicalOrientationToScreenOrientation函数:
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];
}
这篇关于使用方向信息获得方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!