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

Fix initial GetMouseDelta on GLFW #4663

Closed
wants to merge 1 commit into from
Closed

Conversation

ve-nt
Copy link

@ve-nt ve-nt commented Jan 7, 2025

The initial mouse delta is often very large, depending on where the cursor was before the window opened. This is because the first mouse delta is the x/y coordinates your cursor entered the screen from. This can (and often does) cause the free camera to make a sudden jump when its moved for the first time.

The solution to this is to initialize the previousPosition and the currentPosition to the same value on the first mouse input, essentially ignoring the first mouse delta.

@ve-nt
Copy link
Author

ve-nt commented Jan 7, 2025

This fixes #4654

The initial mouse delta is often very large, depending on where the
cursor was before the window opened. This is because the first mouse
delta is the x/y coordinates your cursor entered the screen from. This
can (and often does) cause the free camera to make a sudden jump when
its moved for the first time.

The solution to this is to initialize the previousPosition and the
currentPosition to the same value on the first mouse input, essentially
ignoring the first mouse delta.
@ve-nt
Copy link
Author

ve-nt commented Jan 7, 2025

@asdqwe
Strange, I don't get the same output when I run your test code on my machine:

Mouse start position OUTSIDE the area where window will be created:

WARNING: time:1.05   pos: 409.00 x 225.00   delta: 0.00 x 0.00
WARNING: time:1.07   pos: 409.00 x 223.00   delta: 0.00 x -2.00
WARNING: time:1.09   pos: 408.00 x 222.00   delta: -1.00 x -1.00
WARNING: time:1.10   pos: 407.00 x 220.00   delta: -1.00 x -2.00
WARNING: time:1.12   pos: 407.00 x 219.00   delta: 0.00 x -1.00
WARNING: time:1.14   pos: 407.00 x 219.00   delta: 0.00 x 0.00
WARNING: time:1.15   pos: 407.00 x 219.00   delta: 0.00 x 0.00
WARNING: time:1.17   pos: 407.00 x 219.00   delta: 0.00 x 0.00

Mouse start position INSIDE the area where window will be created:

WARNING: time:0.93   pos: 416.00 x 214.00   delta: 0.00 x 0.00
WARNING: time:0.95   pos: 416.00 x 214.00   delta: 0.00 x 0.00
WARNING: time:0.97   pos: 416.00 x 215.00   delta: 0.00 x 1.00
WARNING: time:0.98   pos: 414.00 x 216.00   delta: -2.00 x 1.00
WARNING: time:1.00   pos: 413.00 x 216.00   delta: -1.00 x 0.00
WARNING: time:1.02   pos: 411.00 x 217.00   delta: -2.00 x 1.00
WARNING: time:1.03   pos: 411.00 x 217.00   delta: 0.00 x 0.00
WARNING: time:1.05   pos: 411.00 x 217.00   delta: 0.00 x 0.00
WARNING: time:1.07   pos: 411.00 x 217.00   delta: 0.00 x 0.00

This is with me positioning my cursor, running your code, and moving my cursor slightly. Then I strip off the excess zero deltas. I'm assuming that's how you're running it as well.

@ve-nt
Copy link
Author

ve-nt commented Jan 7, 2025

Am I supposed to see that delta on the second frame without any mouse input? The first two frames always have a delta of 0.00 x 0.00 for me. I've tried triggering a delta on the second frame by moving my mouse as I start the program, but even then its just coming up zero.

I.E.

WARNING: time:0.12   pos: 400.00 x 225.00   delta: 0.00 x 0.00
WARNING: time:0.13   pos: 409.00 x 225.00   delta: 0.00 x 0.00
WARNING: time:0.15   pos: 395.00 x 222.00   delta: -14.00 x -3.00
WARNING: time:0.17   pos: 386.00 x 222.00   delta: -9.00 x 0.00
WARNING: time:0.18   pos: 380.00 x 223.00   delta: -6.00 x 1.00

If you need any info on platform differences let me know and I'll try and provide any info I can. Currently I'm building your test code by adding it to the examples/others directory. Here's the compile command from make:

gcc -o others/test others/test.c -Wall -std=c99 -D_DEFAULT_SOURCE -Wno-missing-braces -Wunused-result -O2 -D_DEFAULT_SOURCE -I. -I/home/vent/dev/repo/raylib/src -I/home/vent/dev/repo/raylib/src/external  -I/usr/local/include -I/home/vent/dev/repo/raylib/src/external/glfw/include -L. -L/home/vent/dev/repo/raylib/src -L/home/vent/dev/repo/raylib/src -L/usr/local/lib -lraylib -lGL -lm -lpthread -ldl -lrt -lX11 -latomic -DPLATFORM_DESKTOP -DPLATFORM_DESKTOP_GLFW

@ve-nt
Copy link
Author

ve-nt commented Jan 7, 2025

Here's what I'm seeing: https://www.youtube.com/watch?v=rXTMOdLJj6A

@@ -1873,6 +1873,13 @@ static void MouseCursorPosCallback(GLFWwindow *window, double x, double y)
CORE.Input.Mouse.currentPosition.y = (float)y;
CORE.Input.Touch.position[0] = CORE.Input.Mouse.currentPosition;

static bool firstMouseInput = true;
Copy link
Owner

@raysan5 raysan5 Jan 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is one of the most horrible things I've seen in some time. I'm afraid I'm not adding this dirty hack to raylib... just for macOS!!! 😱

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for clarification, does it happen with other platforms?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As said in the other thread, this issue is not limited to macOS. I'm seeing the same issue on Linux.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for clarification, still thinking it should be addressed at GLFW level.

@raysan5
Copy link
Owner

raysan5 commented Jan 7, 2025

If the issue is related to GLFW, it should be addressed by GLFW, not hacked in raylib.

@raysan5 raysan5 closed this Jan 7, 2025
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.

2 participants