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

[core] Some degrees issues on swipe gestures for web. #3154

Closed
3 tasks done
ghost opened this issue Jul 4, 2023 · 5 comments · Fixed by #3155
Closed
3 tasks done

[core] Some degrees issues on swipe gestures for web. #3154

ghost opened this issue Jul 4, 2023 · 5 comments · Fixed by #3155

Comments

@ghost
Copy link

ghost commented Jul 4, 2023

  • I tested it on latest raylib version from master branch.
  • I checked there is no similar issue already reported.
  • My code has no errors or misuse of raylib.

Issue description

Upon taking a closer look at the swipe gestures handling, I noticed three issues:

  1. It's apparently missing 30° on GESTURE_SWIPE_DOWN at rgestures.h#L317 to complete the 360°;
  2. The GESTURE_SWIPE_RIGHT range is 60° (0+30=30, 360-30=330), but the GESTURE_SWIPE_LEFT range is 90° (180-60=120,180+30=210);
  3. Albeit a rare occurance, the exact 30.0f, 120.0f (that should be 150.0f), 210.0f and 330.0f degrees are not being considered.

I believe these were the cause of some unexpected behavior I randomly experienced while testing the swipe gestures.

Proposed solution

The fix appears to be simple enough:

  1. Change the 300 to 330 at rgestures.h#L317 to complete the 360°;
  2. Change the 120 to 150 at rgestures.h#L315 and rgestures.h#L316 so both sides range is 60°;
  3. Change the > to >= at rgestures.h#L315 and rgestures.h#L317 so those angles get caught if they happen.

Will send a PR with the proposed changes shortly.

Environment

Platform: Linux (x86_64)
Operating System: Linux Mint 21.1
OpenGL version: 3.1 Mesa 22.0.5
GPU: Intel HD Graphics 3000

Platform: Android
Operating System: Android 5.1.1
OpenGL version: OpenGL ES 1.1/2.0
GPU: Mali-400

Issue Screenshot

On Chrome (114.0.5735.106 64-bit) for Linux and on Chrome (95.0.4638.74) for Android:

img

Code Example

Minimal reproduction code to test the issue:

#include "raylib.h"

#if defined(PLATFORM_WEB)
    #include <emscripten/emscripten.h>
#endif

#include <stdio.h> // For snprintf().

int lastSwipe = GESTURE_NONE;
char lastAngleStr[20];

void Update(void) {
    const int gesture = GetGestureDetected();
    if (gesture == GESTURE_SWIPE_UP || gesture == GESTURE_SWIPE_RIGHT || gesture == GESTURE_SWIPE_LEFT || gesture == GESTURE_SWIPE_DOWN) { lastSwipe = gesture; }

    const float lastAngle = GetGestureDragAngle();
    snprintf(lastAngleStr, 20, "%f", lastAngle);

    BeginDrawing();
    ClearBackground(RAYWHITE);
    DrawRectangleLinesEx({0, 0, 400, 400}, 2.0f, BLACK);
    DrawText("Swipe on the screen.", 95, 360, 20, BLACK);
    DrawText("Last swipe:", 25, 20, 20, BLACK);
    DrawRectangle(70, 50, 20, 20, lastSwipe == GESTURE_SWIPE_UP    ? BLUE : LIGHTGRAY);
    DrawRectangle(50, 70, 20, 20, lastSwipe == GESTURE_SWIPE_LEFT  ? BLUE : LIGHTGRAY);
    DrawRectangle(90, 70, 20, 20, lastSwipe == GESTURE_SWIPE_RIGHT ? BLUE : LIGHTGRAY);
    DrawRectangle(70, 90, 20, 20, lastSwipe == GESTURE_SWIPE_DOWN  ? BLUE : LIGHTGRAY);
    DrawText("Swipe angle:", 250, 20, 20, BLACK);
    DrawText(lastAngleStr, 280, 50, 20, RED);
    DrawCircle( 200, 200, 100.0f, WHITE);
    DrawTriangle( {200, 200}, {248, 286}, {285, 251}, LIGHTGRAY);
    DrawLineEx( {50, 200}, {350, 200}, 2.0f, GREEN);
    DrawLineEx( {200, 50}, {200, 350}, 2.0f, GREEN);
    DrawLineEx( {115, 148}, {285, 251}, 2.0f, BLUE);
    DrawLineEx( {113, 249}, {285, 150}, 2.0f, BLUE);
    DrawText("30", 290, 130, 20, BLACK);
    DrawText("150", 80, 130, 20, BLACK);
    DrawText("210", 80, 250, 20, BLACK);
    DrawText("300", 245, 290, 20, BLACK);
    DrawText("330", 290, 250, 20, BLACK);
    EndDrawing();
}

int main(void) {
    InitWindow(400, 400, "Swipe Angle Test");
    #if defined(PLATFORM_WEB)
        emscripten_set_main_loop(Update, 0, 1);
    #else
        SetTargetFPS(60);
        while (!WindowShouldClose()) { Update(); }
    #endif
    CloseWindow();
    return 0;
}
@raysan5
Copy link
Owner

raysan5 commented Jul 5, 2023

@ubkp Is the above code from the screenshot example? I think it could be moved to a raylib example to illustrate usage.

@ghost
Copy link
Author

ghost commented Jul 5, 2023

@raysan5 It is, although it's not very interactive (just printed the protractor to have some quick visual reference for the test). But I can make a better one.

I'm currently debugging GESTURE_DOUBLETAP not getting registered for Touch on Web. I'll include it (and the other gestures) so it's more complete.

@raysan5
Copy link
Owner

raysan5 commented Jul 5, 2023

@ubkp ok, that would be great!

@ghost
Copy link
Author

ghost commented Jul 14, 2023

@raysan5 Sorry it took long, but I've finished the example. Just sent a PR with it.

Follow-up: Almost finishing sorting the GESTURE_DOUBLETAP. I should have the issues and fixes ready soon.

@raysan5
Copy link
Owner

raysan5 commented Jul 14, 2023

@ubkp that's great! thank you very much for the improvement!

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 a pull request may close this issue.

1 participant