Use mouse event relative motion to calculate mouse velocity #56754
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently, mouse velocity is updated when
set_mouse_position()
is called. This has two side effects:set_mouse_position()
is called even when the mouse hasn't moved. This results in large mouse velocities when, for example, the mouse is warped.MOUSE_MODE_CAPTURED
.This PR uses
InputEventMouseMotion
's relative motion to update the mouse's velocity instead of calls toset_mouse_position()
. This ensures that mouse velocities are updated when the mouse is captured.Note:
set_mouse_position()
tends to be called to update the velocity before setting aInputEventMouseMotion
'svelocity
property usingget_last_mouse_velocity()
. With this change the velocity (and position) aren't updated until the event is parsed. So callingset_mouse_position()
to update the velocity is no longer meaningful. Therefore, these lines have been removed. Although it would appear that this would make the velocity value used inInputEventMouseMotion
out of date, the reality is that, to ensure the velocity is precise and not jittery, the velocity is only updated every 0.1 seconds. This is far less frequent thanInputEventMouseMotion
events are generated (around every 0.008 seconds). Therefore, callingset_mouse_position()
to update the velocity before setting aInputEventMouseMotion
'svelocity
property, was ineffective anyway.Note2: This change doesn't fix mouse warping from setting the mouse velocity. Mouse warping events should be filtered. However, at least on Linux, this is not happening. See #8414, which is still happening even though the issue has been closed.
Note3: #45592, reports a different issue, which highlighted a different problem: mouse velocities are not properly clearing old residual movements. However, #45592 arose, because of trying to find a workaround to mouse speeds being zero, when using
MOUSE_MODE_CAPTURED
.