我正在使用Android重力和磁场传感器通过SensorManager.getRotationMatrix和SensorManager.getOrientation计算方向.这给了我方位角,俯仰和方向数.当设备平放在桌子上时,结果看起来很合理.
但是,我在清单中禁用了纵向和横向之间的切换,因此getWindowManager().getDefaultDisplay().getRotation()始终为零.当我将设备旋转90度以使其垂直竖立时,我遇到了麻烦.有时这些数字似乎非常错误,我意识到这与万向节锁相关.但是,其他应用程序似乎没有这个问题.例如,我将我的应用与两个免费传感器测试应用程序(传感器测试仪(Dicotomica)和传感器监测(R软件))进行了比较.当设备平坦时,我的应用程序同意这些应用程序,但当我将设备旋转到垂直位置时,可能会有很大差异.这两个应用程序似乎彼此一致,那么他们如何解决这个问题呢?
我正在创建3D Compass应用程序.
我正在使用getOrientation方法来获取方向(几乎与此处相同的实现).如果我把电话放在桌子上它运作良好,但当电话顶部指向天空(减去图片上的Z轴;球体是地球)时,getOrientation开始给出非常糟糕的结果.它为Z轴提供了几个真实度数的0到180度之间的值.有什么方法可以抑制这种行为吗?我创建了一个描述问题的小视频(抱歉质量不好).提前致谢.

解决方案:旋转模型时,有以下区别:
gl.glRotatef(_angleY, 0f, 1f, 0f); //ROLL
gl.glRotatef(_angleX, 1f, 0f, 0f); //ELEVATION
gl.glRotatef(_angleZ, 0f, 0f, 1f); //AZIMUTH
gl.glRotatef(_angleX, 1f, 0f, 0f); //ELEVATION
gl.glRotatef(_angleY, 0f, 1f, 0f); //ROLL
gl.glRotatef(_angleZ, 0f, 0f, 1f); //AZIMUTH
Run Code Online (Sandbox Code Playgroud) 我已经实现了旋转矢量和方向矢量的监听器,虽然我知道它已经折旧我想测试两者.
我知道旋转矢量是一个融合传感器并推荐但是根据它,NORTH(getOrientation返回的值[0](rotationMatrix,value)geaving bearing 0)与Orientation传感器的NORTH不匹配.我还从playstore的不同应用程序中得出结论,方向传感器值似乎更接近它们.
此外很多次我从Rotation_Vector的方位角值[0]然后getOrientation刚刚射击并保持在-180到180之间振荡
PS"getRotationMatrix(float [] R,float [] I,float [] gravity,float [] geomagnetic)"也给出与旋转向量相同的结果.
public final void onSensorChanged(SensorEvent event)
{
float rotationMatrix[];
switch(event.sensor.getType())
{
.
.
.
case Sensor.TYPE_ROTATION_VECTOR:
rotationMatrix=new float[16];
mSensorManager.getRotationMatrixFromVector(rotationMatrix,event.values);
determineOrientation(rotationMatrix);
break;
case Sensor.TYPE_ORIENTATION:
sensorZValue.setText(""+event.values[0]); //rotation about geographical z axis
sensorXValue.setText(""+event.values[1]); //rotation about geographical x axis
sensorYValue.setText(""+event.values[2]); //rotation about geographical y axis
}//switch case ends
}
private void determineOrientation(float[] rotationMatrix)
{
float[] orientationValues = new float[3];
SensorManager.getOrientation(rotationMatrix, orientationValues);
double azimuth = Math.toDegrees(orientationValues[0]);
double pitch = Math.toDegrees(orientationValues[1]); …Run Code Online (Sandbox Code Playgroud) android magnetometer digital-compass rotational-matrices android-sensors