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

Fix smooth follow example in interpolation #6134

Closed
wants to merge 1 commit into from

Conversation

aXu-AP
Copy link
Contributor

@aXu-AP aXu-AP commented Sep 3, 2022

Old method is not really frame-independent. Formula with explanation found from here.

Old method is not really frame-independent. Formula with explanation found from https://www.gamedeveloper.com/programming/improved-lerp-smoothing-
@aXu-AP
Copy link
Contributor Author

aXu-AP commented Sep 3, 2022

Notice: I tested the code only in 3.x build, then changed linear_interpolation to lerp. I think there shouldn't be any other changes between 3.x and 4.0. Also C# code is untested.

@Calinou Calinou added the bug label Sep 3, 2022
@TheYellowArchitect
Copy link
Contributor

TheYellowArchitect commented Oct 5, 2022

Adding link since OP hasn't https://docs.godotengine.org/en/latest/tutorials/math/interpolation.html
I tested current implementation in 4.0 beta2, and then tried yours (so I tested both versions)

The version currently at the docs does interpolate evenly but it jitters. Your version is better as its smooth without jitter. I don't like that the code is slightly more complicated (newbies will use it but won't understand it, compared to current version), but the results are worth it imo.

@skyace65 skyace65 added the area:manual Issues and PRs related to the Manual/Tutorials section of the documentation label Jan 13, 2023
@Vennnot
Copy link
Contributor

Vennnot commented Apr 26, 2023

Who can make a call on wether if we merge this or not?

Comment on lines -121 to -122

$Sprite2D.position = $Sprite2D.position.lerp(mouse_pos, delta * FOLLOW_SPEED)
Copy link
Contributor

@Flarkk Flarkk Sep 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest keeping the current version, but adding a comment right after :

# An alternative version with even smoother results and a little more math : 
# $Sprite2D.position = $Sprite2D.position.lerp(mouse_pos, 1 - exp(-FOLLOW_SPEED * delta))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know. Kind of kills the idea of fixing the incorrect method in the first place.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the original method is not really wrong in itself. It just produces less smooth results, which might not be a problem in the context of a beginners tutorial.


sprite.Position = sprite.Position.Lerp(mousePos, delta * FollowSpeed);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

}

Here is how it looks:

.. image:: img/interpolation_follow.gif

This useful for smoothing camera movement, allies following you (ensuring they stay within a certain range), and many other common game patterns.
This is useful for smoothing camera movement, allies following you (ensuring they stay within a certain range), and many other common game patterns. Notice that making movement framerate-independent is more involved than just multiplying weight by delta value.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The final notice not only doesn't bring much information, but may add confusion. I'd rather not add it.

@Flarkk
Copy link
Contributor

Flarkk commented Sep 12, 2024

I feel this trick's worth mentioning indeed, but it's true it may be a little bit too much involved for beginners.

I suggest keeping the more pedagogical current version, but mentioning the improved one as a comment for advanced users.

@tetrapod00
Copy link
Contributor

I noticed this independently the other day too, and was considering making a PR like this. My take on the solution, with @aXu-AP as co-author, is #10197.

@aXu-AP aXu-AP closed this Nov 7, 2024
@aXu-AP aXu-AP deleted the patch-1 branch November 7, 2024 09:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
archived area:manual Issues and PRs related to the Manual/Tutorials section of the documentation bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants