Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rcore] Fix #4405 #4420

Merged
merged 3 commits into from
Oct 22, 2024
Merged

Conversation

asdqwe
Copy link
Contributor

@asdqwe asdqwe commented Oct 22, 2024

All credits to @dmg46664 for finding this issue (#4405, ref) and pointing the solution. Great catch!

Summary: At the moment a mininum value of 0.1f is being applied on all gamepad axis at all times on all platforms. Since the deadzone depends on each specific gamepad, it's indeed something that should be exposed to the end-user (e.g.: the player playing the game) be able to adjust to his case at runtime. This PR adds deadzone to CORE.Input.Gamepad and exposes SetGamepadDeadzone() to set that value. It also allows the dev-user to zero it and handle the deadzone himself.

This PR can be tested with:
#include "raylib.h"
int main(void) {
    InitWindow(400, 300, "test");
    SetTargetFPS(60);

    // Control the deadzone here:
    SetGamepadDeadzone(0.001f);

    while (!WindowShouldClose()) {

        // Get axis values:
        float lax = GetGamepadAxisMovement(0, GAMEPAD_AXIS_LEFT_X);
        float lay = GetGamepadAxisMovement(0, GAMEPAD_AXIS_LEFT_Y);
        float rax = GetGamepadAxisMovement(0, GAMEPAD_AXIS_RIGHT_X);
        float ray = GetGamepadAxisMovement(0, GAMEPAD_AXIS_RIGHT_Y);

        BeginDrawing();
        ClearBackground(RAYWHITE);

        DrawCircle(100, 80, 50, LIGHTGRAY);
        DrawCircle(100+(int)(lax*50), 80+(int)(lay*50), 5, RED);
        DrawText(TextFormat("LAX %f", lax), 30, 140, 20, BLACK);
        DrawText(TextFormat("LAY %f", lay), 30, 160, 20, BLACK);

        DrawCircle(300, 80, 50, LIGHTGRAY);
        DrawCircle(300+(int)(rax*50), 80+(int)(ray*50), 5, BLUE);
        DrawText(TextFormat("RAX %f", rax), 230, 140, 20, BLACK);
        DrawText(TextFormat("RAY %f", ray), 230, 160, 20, BLACK);

        EndDrawing();
    }
    CloseWindow();
    return 0;
}
And devs can implement individual deadzones like this:
#include "raylib.h"
int main(void) {
    InitWindow(400, 300, "test");
    SetTargetFPS(60);

    // Set axis deadzones:
    SetGamepadDeadzone(0.0f);
    int dz = 0;
    float laxdz = 0.5f;
    float laydz = 0.5f;
    float raxdz = 0.5f;
    float raydz = 0.5f;

    while (!WindowShouldClose()) {

        // Toggle deadzone: [gamepad button A]
        if (IsGamepadButtonPressed(0, GAMEPAD_BUTTON_RIGHT_FACE_DOWN)) { dz = !dz; }

        // Get axis values:
        float lax = GetGamepadAxisMovement(0, GAMEPAD_AXIS_LEFT_X);
        float lay = GetGamepadAxisMovement(0, GAMEPAD_AXIS_LEFT_Y);
        float rax = GetGamepadAxisMovement(0, GAMEPAD_AXIS_RIGHT_X);
        float ray = GetGamepadAxisMovement(0, GAMEPAD_AXIS_RIGHT_Y);

        // Calculate deadzones:
        if (dz) {
            if (lax > -laxdz && lax < laxdz) { lax = 0.0f; }
            if (lay > -laydz && lay < laydz) { lay = 0.0f; }
            if (rax > -raxdz && rax < raxdz) { rax = 0.0f; }
            if (ray > -raydz && ray < raydz) { ray = 0.0f; }
        }

        BeginDrawing();
        ClearBackground(RAYWHITE);

        DrawCircle(100, 80, 50, LIGHTGRAY);
        if (dz) { DrawEllipse(100, 80, 50*laxdz, 50*laydz, BLACK); }
        DrawCircle(100+(int)(lax*50), 80+(int)(lay*50), 5, RED);
        DrawText(TextFormat("LAX %f", lax), 30, 140, 20, BLACK);
        DrawText(TextFormat("LAY %f", lay), 30, 160, 20, BLACK);

        DrawCircle(300, 80, 50, LIGHTGRAY);
        if (dz) { DrawEllipse(300, 80, 50*raxdz, 50*raydz, BLACK); }
        DrawCircle(300+(int)(rax*50), 80+(int)(ray*50), 5, BLUE);
        DrawText(TextFormat("RAX %f", rax), 230, 140, 20, BLACK);
        DrawText(TextFormat("RAY %f", ray), 230, 160, 20, BLACK);

        DrawText("Toggle deadzone:\n[gamepad button A]", 30, 220, 20, BLACK);
        EndDrawing();
    }
    CloseWindow();
    return 0;
}

Fixes #4405.

@raysan5
Copy link
Owner

raysan5 commented Oct 22, 2024

@asdqwe Is it really required to add a new function to raylib API? I'd prefer to avoid that if it could be managed on user side.

Afaik, it can be checked and scaled at user side, no need for a new internal global and a new function.

@raysan5 raysan5 merged commit 7a4a84a into raysan5:master Oct 22, 2024
14 checks passed
@raysan5
Copy link
Owner

raysan5 commented Oct 22, 2024

@asdqwe thanks for the review!

@asdqwe asdqwe deleted the fix-gamepad-deadzone-runtime branch October 22, 2024 23:05
@raysan5
Copy link
Owner

raysan5 commented Oct 22, 2024

@asdqwe that would be great! thanks! 👍🙂

psxdev pushed a commit to raylib4Consoles/raylib that referenced this pull request Nov 18, 2024
* Fix raysan5#4405 at runtime

* Add parameter validation

* Remove default global deadzone
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Gamepad] Remove default gamepad axis deadzone on MacOS/GLFW
2 participants