summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorRĂ©mi Verschelde <remi@verschelde.fr>2022-02-03 18:10:30 +0100
committerGitHub <noreply@github.com>2022-02-03 18:10:30 +0100
commitbf0253bab936f053bb5ae56e6fcf67defaf9afbc (patch)
tree5a7d2fb655314787a94a0e6ef1f4c890ac32352e /core
parentffc828ac50a54b177caf99ff44c385b459827335 (diff)
parent5c3600b29fbc02e92a3ccbd86a9da46efd03bac2 (diff)
Merge pull request #56764 from madmiraal/fix-45592-2
Diffstat (limited to 'core')
-rw-r--r--core/input/input.cpp30
-rw-r--r--core/input/input.h2
2 files changed, 19 insertions, 13 deletions
diff --git a/core/input/input.cpp b/core/input/input.cpp
index 7106bd0745..d36d0f4da0 100644
--- a/core/input/input.cpp
+++ b/core/input/input.cpp
@@ -190,32 +190,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();
}
@@ -719,7 +724,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 80f260f30e..ab2cd377f4 100644
--- a/core/input/input.h
+++ b/core/input/input.h
@@ -270,7 +270,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);