How can I make PS4 and Xbox One controllers vibration / rumble / haptic on iOS 13 and Android works properly using SDL2?

5.3k views Asked by At

I use SDL2 and I try to make my controllers rumble on both iOS 13 and Android 10. You can see an extract of my code below:

 joystick = SDL_JoystickOpen(device);
 SDL_Haptic * haptic = SDL_HapticOpenFromJoystick(joystick);
 SDL_HapticRumbleInit(haptic);
 SDL_HapticRumblePlay(haptic, (float)0.5, 2000);
 SDL_HapticClose(haptic);

But for now it only works on Android 10 with PS4 controller, with Xbox One controller, SDL call to SDL_NumHaptics() allways return 0 on iOS 13 and Android 10 and it's the same with PS4 controller on iOS 13...

Does someone had encounter the same issue ? If yes, is there a way to solve or work around it ?

Thank you in advance.

1

There are 1 answers

0
FenomenCoyote On

You have to add SDL_Delay(2000) before yo close the Haptic. If you don't do it, then you are closing it before it could actually do the rumble

     joystick = SDL_JoystickOpen(device);
     SDL_Haptic * haptic = SDL_HapticOpenFromJoystick(joystick);
     SDL_HapticRumbleInit(haptic);
     SDL_HapticRumblePlay(haptic, (float)0.5, 2000);

     SDL_Delay(2000);

     SDL_HapticClose(haptic);

I also would add the corresponding checks, as seen in the wiki of sdl2 :

SDL_Haptic *haptic;

// Open the device
haptic = SDL_HapticOpen( 0 );
if (haptic == NULL)
   return -1;

// Initialize simple rumble
if (SDL_HapticRumbleInit( haptic ) != 0)
   return -1;

// Play effect at 50% strength for 2 seconds
if (SDL_HapticRumblePlay( haptic, 0.5, 2000 ) != 0)
   return -1;
SDL_Delay( 2000 );

// Clean up
SDL_HapticClose( haptic );