From 5c3600b29fbc02e92a3ccbd86a9da46efd03bac2 Mon Sep 17 00:00:00 2001 From: Marcel Admiraal Date: Thu, 13 Jan 2022 18:13:50 +0000 Subject: [PATCH] Fix mouse velocity not changing fast enough - Uses all accumulated movements when calculating velocity - Discards old accumulated movements - Sets last mouse velocity to zero when there is no movement --- core/input/input.cpp | 30 ++++++++++++++++++------------ core/input/input.h | 2 +- doc/classes/Input.xml | 4 ++-- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/core/input/input.cpp b/core/input/input.cpp index 26df16792d6..b543e273fe0 100644 --- a/core/input/input.cpp +++ b/core/input/input.cpp @@ -189,32 +189,37 @@ void Input::VelocityTrack::update(const Vector2 &p_delta_p) { float delta_t = tdiff / 1000000.0; last_tick = tick; + if (delta_t > max_ref_frame) { + // First movement in a long time, reset and start again. + velocity = Vector2(); + accum = p_delta_p; + accum_t = 0; + return; + } + accum += p_delta_p; accum_t += delta_t; - if (accum_t > max_ref_frame * 10) { - accum_t = max_ref_frame * 10; + if (accum_t < min_ref_frame) { + // Not enough time has passed to calculate speed precisely. + return; } - while (accum_t >= min_ref_frame) { - float slice_t = min_ref_frame / accum_t; - Vector2 slice = accum * slice_t; - accum = accum - slice; - accum_t -= min_ref_frame; - - velocity = (slice / min_ref_frame).lerp(velocity, min_ref_frame / max_ref_frame); - } + velocity = accum / accum_t; + accum = Vector2(); + accum_t = 0; } void Input::VelocityTrack::reset() { last_tick = OS::get_singleton()->get_ticks_usec(); velocity = Vector2(); + accum = Vector2(); accum_t = 0; } Input::VelocityTrack::VelocityTrack() { min_ref_frame = 0.1; - max_ref_frame = 0.3; + max_ref_frame = 3.0; reset(); } @@ -704,7 +709,8 @@ Point2 Input::get_mouse_position() const { return mouse_pos; } -Point2 Input::get_last_mouse_velocity() const { +Point2 Input::get_last_mouse_velocity() { + mouse_velocity_track.update(Vector2()); return mouse_velocity_track.velocity; } diff --git a/core/input/input.h b/core/input/input.h index 8b1d7d6161a..b661560b37c 100644 --- a/core/input/input.h +++ b/core/input/input.h @@ -274,7 +274,7 @@ public: Vector3 get_gyroscope() const; Point2 get_mouse_position() const; - Vector2 get_last_mouse_velocity() const; + Vector2 get_last_mouse_velocity(); MouseButton get_mouse_button_mask() const; void warp_mouse_position(const Vector2 &p_to); diff --git a/doc/classes/Input.xml b/doc/classes/Input.xml index 45b15331d2e..7129f2b67dd 100644 --- a/doc/classes/Input.xml +++ b/doc/classes/Input.xml @@ -141,10 +141,10 @@ Returns the strength of the joypad vibration: x is the strength of the weak motor, and y is the strength of the strong motor. - + - Returns the mouse velocity for the last time the cursor was moved, and this until the next frame where the mouse moves. This means that even if the mouse is not moving, this function will still return the value of the last motion. + Returns the last mouse velocity. To provide a precise and jitter-free velocity, mouse velocity is only calculated every 0.1s. Therefore, mouse velocity will lag mouse movements.