From 34634082ff755a0811bfe21822a155e897459108 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Sun, 14 Jun 2015 19:25:12 -0700 Subject: [PATCH] Fixed bug 3012 - Android accelerometer joystick axis values overflow when values from Android are larger than gravity Magnus Bjerke Vik This causes issues when for instance using the joystick API to make an Android phone rotate an object by rotating the phone. When the absolute value of an axis reported by android is larger than earth gravity, SDL will overflow the Sint16 value used for joystick axes, causing sporadic movements when close to the gravity. Just holding the phone so that e.g. Y points directly upwards will make it unstable, and even more if you just tap the phone gently from below (increasing the acceleration). More detailed: SDLActivity gets the accelerometer values in onSensorChanged and divides each axis by earth gravity. SDL_SYS_JoystickUpdate takes each of the axis values, multiplies them by 32767.0 (largest Sint16), and the casts them to Sint16. From this you can see that any value from Android that exceeds earth gravity will overflow the joystick axis. A fix is to clamp the values so that they won't overflow the Sint16. --- src/joystick/android/SDL_sysjoystick.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/joystick/android/SDL_sysjoystick.c b/src/joystick/android/SDL_sysjoystick.c index 91480b8bb90f0..b564f1850de68 100644 --- a/src/joystick/android/SDL_sysjoystick.c +++ b/src/joystick/android/SDL_sysjoystick.c @@ -518,6 +518,15 @@ SDL_SYS_JoystickUpdate(SDL_Joystick * joystick) if (item->joystick) { if (Android_JNI_GetAccelerometerValues(values)) { for ( i = 0; i < 3; i++ ) { + if (values[i] > 1.0f) + { + values[i] = 1.0f; + } + else if (values[i] < -1.0f) + { + values[i] = -1.0f; + } + value = (Sint16)(values[i] * 32767.0f); SDL_PrivateJoystickAxis(item->joystick, i, value); }