DEV Community

Sunder Iyer
Sunder Iyer

Posted on

TIL Godot Smooth Scrolling

2021-03-08

Did you know you can copy/paste images directly in dev.to posts!

I'm building a game tool in Godot and I noticed that middle-mouse scrolling my text in the app and editor had choppy performance. However, if I used the scroll bar directly, the scrolling was as smooth as butter. What's going on here?

I dug into my TextEdit node and found this...
Smooth Scrolling option and tooltip
When I turned it off, the middle-mouse scrolling was smooth. What gives?

I did a quick issue search and found this pull request. Fortunately, it put me on the right track.

I reduced the Project's Physics Fps since I don't need a Physics engine running. However, it turns out I incorrectly assumed what Physics means in Godot. Physics goes beyond just rigid body simulation and collision detection. Nodes can use the Physics process to perform other calculations like determining scrolling position. In this case, the TextEdit node used the delta time of the Physics process and since I reduced the Fps, this led to bigger deltas and choppiness.

if (scrolling) {
    if (get_value() != target_scroll) {
        double target = target_scroll - get_value();
        double dist = sqrt(target * target);
        double vel = ((target / dist) * 500) * get_physics_process_delta_time();

        if (Math::abs(vel) >= dist) {
            set_value(target_scroll);
            scrolling = false;
            set_physics_process_internal(false);
        } else {
            set_value(get_value() + vel);
        }
Enter fullscreen mode Exit fullscreen mode

Also see: https://docs.godotengine.org/en/stable/classes/class_node.html#class-node-method-physics-process

Discussion (0)