2011-11-06 120 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?中的完整代碼。僅供參考。