Skip to content

Commit

Permalink
Fixed bug 3012 - Android accelerometer joystick axis values overflow …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
slouken committed Jun 15, 2015
1 parent e3df6d5 commit 3463408
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/joystick/android/SDL_sysjoystick.c
Expand Up @@ -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);
}
Expand Down

0 comments on commit 3463408

Please sign in to comment.