2011-11-06 116 views
0

当我们在不同方向转动或旋转手机(例如加速汽车)时,人们如何创建使对象移动的游戏? 他们使用加速度计和陀螺仪的读数,还是使用Android中的OpenGL?Android游戏如何使用传感器?

+0

他们使用传感器读数来控制他们用来绘制的任何东西,不管它是2D还是3D等。 –

回答

4

它们很可能使用加速计,因为它在各种设备上广泛使用。

你可能想看看这个教程:http://blog.androgames.net/85/android-accelerometer-tutorial/

而这里的加速功能由谷歌演示:http://developer.android.com/resources/samples/AccelerometerPlay/src/com/example/android/accelerometerplay/AccelerometerPlayActivity.html

+1

Thanx user1032613 ...它是一个很好的例子...但是woooo它做了很多复杂的调整:) ..会通过并理解逻辑。 – Kozlov

+0

嗨,我试图理解样本中的代码。它涉及到很多数学,还有更多的硬编码使用没有任何评论。是否有任何机构谁了解代码好? 和@ user1032613这就是我正在寻找:) – Kozlov

2

这个例子显示了一个简单的计算为3个方向看重
间距,方向和方位角。

if(SensorManager.getRotationMatrix(R, null, AccelerometerValues_last, MagneticFieldValues_last)) 
{ 
SensorManager.remapCoordinateSystem(R, SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X, remapR); 
SensorManager.getOrientation(remapR, orientationValues); 

Matrix.multiplyMV(orientationVector, 0, remapR, 0, sZVector, 0); 
pitch = (float) (-Math.atan2(orientationVector[1], orientationVector[2]) * RADIANS_TO_DEGREES); 

Matrix.multiplyMV(orientationVector, 0, remapR, 0, sZVector, 0); 
orientation = (float) (-Math.atan2(orientationVector[0], orientationVector[1]) * RADIANS_TO_DEGREES); 

Matrix.invertM(remapR_inv, 0, remapR, 0); 
Matrix.multiplyMV(azimuthVector, 0, remapR_inv, 0, sZVector, 0); 
azimuth = (float) (180 + Math.atan2(azimuthVector[0], azimuthVector[1]) * RADIANS_TO_DEGREES); 
}  

文章How to use Android sensors?中的完整代码。仅供参考。