Skip to content

Commit

Permalink
Merge pull request #56764 from madmiraal/fix-45592-2
Browse files Browse the repository at this point in the history
  • Loading branch information
akien-mga authored Feb 3, 2022
2 parents ffc828a + 5c3600b commit bf0253b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
30 changes: 18 additions & 12 deletions core/input/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion core/input/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ class Input : public Object {
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);
Expand Down
4 changes: 2 additions & 2 deletions doc/classes/Input.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
</description>
</method>
<method name="get_last_mouse_velocity" qualifiers="const">
<method name="get_last_mouse_velocity">
<return type="Vector2" />
<description>
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.
</description>
</method>
<method name="get_magnetometer" qualifiers="const">
Expand Down

0 comments on commit bf0253b

Please sign in to comment.