Reputation: 1005
I'm working on a video game where the tilt is the primary control method for the players. But I would like the players to be able to play the game even lying on a couch. Currently the game works best if the device is flat, and still works if you tilt a little bit because I calculate the starting point of the accelerometer. But this gives unexpected results.
Is there a way in Android to calculate the device's rotation (degrees) from a specific starting point? Is it even possible at all? Was anyone able to accomplish this? I know that SpeedX were able to master rotation, but I need it for tilting.
Thank you
Upvotes: 2
Views: 1601
Reputation: 1153
You can get the device rotation degrees through
OrientationEventListener.onOrientationChanged()
which ranges from 0 to 359 degree, though you'll have to calculate the difference between the starting point and the changes in rotational degrees yourself.
void onOrientationChanged (int orientation) {
//orientation is an argument which represents rotation in degrees
}
also you can enable and disable this listener by calling enable()
and disable()
methods of the listener. Here are the deveoper reference docs from google.
this is a link on to how use orientation listener.
public class SimpleOrientationActivity extends Activity {
OrientationEventListener mOrientationListener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mOrientationListener = new OrientationEventListener(this,
SensorManager.SENSOR_DELAY_NORMAL) {
@Override
public void onOrientationChanged(int orientation) {
Log.v(DEBUG_TAG, "Orientation changed to " + orientation);
}
};
if (mOrientationListener.canDetectOrientation() == true) {
Log.v(DEBUG_TAG, "Can detect orientation");
mOrientationListener.enable();
} else {
Log.v(DEBUG_TAG, "Cannot detect orientation");
mOrientationListener.disable();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
mOrientationListener.disable();
}
}
There are other rate values appropriate for game use and other purposes. The default rate, SENSOR_DELAY_NORMAL, is most appropriate for simple orientation changes. Other values, such as SENSOR_DELAY_UI and SENSOR_DELAY_GAME might work for you.
This is another useful link that implements the same code and provides good explanation.
Upvotes: 1
Reputation: 2834
You can get constant callbacks about angular (rotational) velocity. It is up to you to convert that to an angular position. [1]
[1] http://developer.android.com/guide/topics/sensors/sensors_motion.html#sensors-motion-gyro
Upvotes: 2