GamepadsSDL 3.2
SDL_GetGamepadAxis
Reads a stick or trigger position as a 16-bit integer.
Signature
Sint16 SDL_GetGamepadAxis(SDL_Gamepad *gamepad, SDL_GamepadAxis axis);
How it works
Sticks go from -32768 to 32767, with 0 at rest; divide by 32767.0f to get about -1..1. Positive Y is DOWN. Triggers go from 0 (released) to 32767 (fully pressed).
A stick at rest rarely reads exactly 0: worn pads drift a little. Ignore values below a "dead zone" (around 0.15 of full range), or the character creeps on its own.
Parameters
gamepadSDL_Gamepad *
The gamepad.
axisSDL_GamepadAxis
Which axis.
Common values
- SDL_GAMEPAD_AXIS_LEFTX / _LEFTY
- Left stick.
- SDL_GAMEPAD_AXIS_RIGHTX / _RIGHTY
- Right stick.
- SDL_GAMEPAD_AXIS_LEFT_TRIGGER / _RIGHT_TRIGGER
- Triggers, 0..32767.
Return value
The axis value, or 0 on failure.
Example
example.cpp
1float lx = SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_LEFTX) / 32767.0f;
2float ly = SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_LEFTY) / 32767.0f;
3if (std::hypot(lx, ly) < 0.15f) lx = ly = 0.0f; // dead zone
4player.move(lx, ly);