Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.x] Fix mouse speed not changing fast enough #56765

Merged
merged 1 commit into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/os/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class Input : public Object {
virtual void vibrate_handheld(int p_duration_ms = 500) = 0;

virtual Point2 get_mouse_position() const = 0;
virtual Point2 get_last_mouse_speed() const = 0;
virtual Point2 get_last_mouse_speed() = 0;
virtual int get_mouse_button_mask() const = 0;

virtual void warp_mouse_position(const Vector2 &p_to) = 0;
Expand Down
4 changes: 2 additions & 2 deletions doc/classes/Input.xml
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,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_speed" qualifiers="const">
<method name="get_last_mouse_speed">
<return type="Vector2" />
<description>
Returns the mouse speed 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 speed. To provide a precise and jitter-free speed, mouse speed is only calculated every 0.1s. Therefore, mouse speed will lag mouse movements.
</description>
</method>
<method name="get_magnetometer" qualifiers="const">
Expand Down
30 changes: 18 additions & 12 deletions main/input_default.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,32 +42,37 @@ void InputDefault::SpeedTrack::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.
speed = 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;

speed = (slice / min_ref_frame).linear_interpolate(speed, min_ref_frame / max_ref_frame);
}
speed = accum / accum_t;
accum = Vector2();
accum_t = 0;
}

void InputDefault::SpeedTrack::reset() {
last_tick = OS::get_singleton()->get_ticks_usec();
speed = Vector2();
accum = Vector2();
accum_t = 0;
}

InputDefault::SpeedTrack::SpeedTrack() {
min_ref_frame = 0.1;
max_ref_frame = 0.3;
max_ref_frame = 3.0;
reset();
}

Expand Down Expand Up @@ -559,7 +564,8 @@ void InputDefault::set_mouse_position(const Point2 &p_posf) {
Point2 InputDefault::get_mouse_position() const {
return mouse_pos;
}
Point2 InputDefault::get_last_mouse_speed() const {
Point2 InputDefault::get_last_mouse_speed() {
mouse_speed_track.update(Vector2());
return mouse_speed_track.speed;
}

Expand Down
2 changes: 1 addition & 1 deletion main/input_default.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ class InputDefault : public Input {
virtual Vector3 get_gyroscope() const;

virtual Point2 get_mouse_position() const;
virtual Point2 get_last_mouse_speed() const;
virtual Point2 get_last_mouse_speed();
virtual int get_mouse_button_mask() const;

virtual void warp_mouse_position(const Vector2 &p_to);
Expand Down