Custom haptic effect


I have tried to create an effect of type SDL_HAPTIC_CUSTOM, but when I call for SDL_HapticNewEffect, I get failure return code and message “Haptic error Unable to create effect”.
The important parts of my code look like:

int device_index = 0;
SDL_Joystick* joystick = SDL_JoystickOpen(device_index);
SDL_Haptic* haptic = SDL_HapticOpen(device_index);

SDL_HapticEffect* effect = new SDL_HapticEffect;
SDL_memset(effect , 0, sizeof(SDL_HapticEffect));
effect->type = SDL_HAPTIC_CUSTOM;
effect->custom.direction.type = SDL_HAPTIC_CARTESIAN;
effect->custom.length = 5000;
effect->custom.period = 500;
const int nof_samples = 1000;
effect->custom.channels = 1;
effect->custom.samples = nof_samples;
effect-> = new Uint16[nof_samples];

for(int i = 0; i < nof_samples; ++i) {
  effect->[i] = Uint16(0); //Or some generated signal

assert(SDL_HapticEffectSupported(haptic, effect));

std::cout << SDL_HapticNewEffect(haptic, effect) << std::endl; //Returns -1
std::cout << SDL_GetError() << std::endl; //prints "Haptic error Unable to create effect"

The last call fails and returns -1. I guess I am setting the parameters of effect->custom to wrong values, but I don’t get any clue from the error message. I have setup joystick correctly, and can run other effects e.g. SDL_HAPTIC_CONSTANT, SDL_HAPTIC_SINE and SDL_HAPTIC_SPRING.

Is there any good example on how to set up the custom effect?


Maybe you get this error because you try to create to many effect.

SDL_HapticNumEffects always reports that DirectInput device on Windows can hold 128 effects, but the real number is way lower, for example Logitech joystick can hold two effects per axis.


I tried your suggestions and printed out SDL_HapticNumAxes and SDL_HapticNumEffects which gave 1 axis respective 128 supported effect slots.

I am currently trying my software with Thrustmaster T300RS Racing Wheel but get the same problems with Logitech G25/G27/G29. OS is Windows 10.

So I tried to create only one effect, the Custom one, but still get the same error:

“Haptic error Unable to create effect”

I traced down the error message, and the call chain looks like:

int SDL_HapticNewEffect(SDL_Haptic * haptic, SDL_HapticEffect * effect);
int SDL_SYS_HapticNewEffect(SDL_Haptic * haptic, struct haptic_effect *effect, SDL_HapticEffect * base);
int SDL_DINPUT_HapticNewEffect(SDL_Haptic * haptic, struct haptic_effect *effect, SDL_HapticEffect * base)
  --> DI_SetError("Unable to create effect", ret);

I am quite sure my parameters to SDL_HapticEffect*, type = SDL_HAPTIC_CUSTOM are incorrect and results in this error, but there are no examples and the documentation is defective when it comes to the custom effect settings.

For example, why is the (Uint16*) member an unsigned value? If it is supposed to be a custom signal (as I have interpretated it), should it not be able to have negative values as well? Compare with SdlHapticPeriodic.magnitude (Sint16) which is a signed value.

Help and hints appreciated!


I didn’t notice last time that you forgot to set the direction:

effect->custom.direction.dir[0] = 1;

The other parameters seem OK. The SDL documentation is based on the Microsoft one which does not say much more:

I agree with you, should be an Sint16. You should file a bug.


I tried to set the direction as suggested, but same error.

m_effect->custom.direction.type = SDL_HAPTIC_CARTESIAN;
m_effect->custom.direction.dir[0] = 1;
m_effect->custom.direction.dir[1] = 0;
m_effect->custom.direction.dir[2] = 0;

I have also tried to use SDL_HAPTIC_POLAR as coordinate system, and varied the length of the effect, but nothing seems to help.

As I have one rotational axis on my steering wheel, I cannot really tell what the direction means in this case. I guess the direction is one dimensional (around the steering wheel axis), but I can have missed somehting there.

I will file a bug, and in the meanwhile any further suggestions are appreciated!