用于Android游戏的运动传感器

时间:2014-12-07 07:55:29

标签: android rotation

我想知道哪些传感器用于Android中的竞赛游戏。

事实上,我需要在我的Android手机中测量旋转的确切角度,但我发现我的手机没有像GRAVITY或MAGNETIC那样需要的传感器,它的android API是17.基于我的搜索通过网络,我发现没有这些传感器就无法测量精确的旋转角度。

所以我想知道我的手机游戏是如何运作的???在这样的游戏中,您可以通过旋转屏幕来控制汽车。但是,当这些游戏没有所需的传感器时,这些游戏如何找到它的轮换效果!

这个问题可能看起来有点愚蠢,但如果有人回答,我会很感激。

1 个答案:

答案 0 :(得分:0)

您可以使用Accelerometer检查所有X,Y和Z轴上的设备方向。正如你所说,你不需要依赖GRAVITY和MAGNETIC。请参考下面的代码,这可能会对您有所帮助 -

float[] rotationMatrix = new float[9];  
if(SensorManager.getRotationMatrix(rotationMatrix, null, lastAcceleration, lastMagneticField)){
float[] orientMatrix = new float[3];
float remapMatrix = new float[9];
SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_X, SensorManager.AXIS_Z, remapMatrix);
SensorManager.getOrientation(remapMatrix, orientMatrix);

orientation = orientMat[0]*180/(float)Math.PI;

干杯:)