Skip to content

Commit

Permalink
Fix initial GetMouseDelta on GLFW
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ve-nt committed Jan 7, 2025
1 parent fc29bc2 commit cada094
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/platforms/rcore_desktop_glfw.c
Original file line number Diff line number Diff line change
Expand Up @@ -1869,10 +1869,18 @@ static void MouseButtonCallback(GLFWwindow *window, int button, int action, int
// GLFW3 Cursor Position Callback, runs on mouse move
static void MouseCursorPosCallback(GLFWwindow *window, double x, double y)
{
static bool firstMouseInput = true;

CORE.Input.Mouse.currentPosition.x = (float)x;
CORE.Input.Mouse.currentPosition.y = (float)y;
CORE.Input.Touch.position[0] = CORE.Input.Mouse.currentPosition;

if (firstMouseInput)
{
CORE.Input.Mouse.previousPosition = CORE.Input.Mouse.currentPosition;
firstMouseInput = false;
}

#if defined(SUPPORT_GESTURES_SYSTEM) && defined(SUPPORT_MOUSE_GESTURES)
// Process mouse events as touches to be able to use mouse-gestures
GestureEvent gestureEvent = { 0 };
Expand Down

0 comments on commit cada094

Please sign in to comment.