Simple code returning 0.000000 for gyroscope sensor data

Here is my code:

#include <SDL3/SDL.h>
#include

int main(int argc, char* argv) {

SDL_Init(SDL_INIT_GAMEPAD);

int gamepadCount = 69;
SDL_GetGamepads(&gamepadCount);
printf("Number of gamepads detected: %d\n", gamepadCount);

SDL_Gamepad* gamepad_id = SDL_OpenGamepad(1);

float data_rate = 21;

data_rate = SDL_GetGamepadSensorDataRate(gamepad_id, SDL_SENSOR_GYRO);

printf("%f", data_rate);

bool sensorState = SDL_SetGamepadSensorEnabled(gamepad_id, SDL_SENSOR_GYRO, true);

printf("\n%s", sensorState ? "true" : "false");

SDL_Delay(1000);  // wait 100 milliseconds

float gyro_data[3] = {21.0};

bool sensorData = SDL_GetGamepadSensorData(gamepad_id, SDL_SENSOR_GYRO, gyro_data, 3);

printf("\n%s", sensorData ? "true" : "false");

printf("\n%f %f %f", gyro_data[0], gyro_data[1], gyro_data[2]);

SDL_Quit();

return 0;

}

And it prints this when compiled:
Number of gamepads detected: 1
250.000000
true
true
0.000000 0.000000 0.000000

That should mean that it can see the gamepad. The gamepad is opened because it returns SDL_GAMEPAD* which is used in other functions. It successfully returns the sensor data rate (its a dualsense so 250 is correct). It returns a success for enabling gyro sensor data. It returns a success for reading gyro sensor data, but I just get 0s clearly, why?

You need to process events or call SDL_UpdateGamepads() to get data from the controller.

1 Like

Thank you very much bro, its working now :ok_hand: