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

Improve interpolation smoothing example #10197

Merged
merged 1 commit into from
Nov 5, 2024

Conversation

tetrapod00
Copy link
Contributor

@tetrapod00 tetrapod00 commented Nov 3, 2024

Supersedes #6134.

Improves explanation of interpolation smoothing ("lerp smoothing").
Adds a note about framerate-independent version, with off-site links to full explanations.

I believe keeping the current, framerate-dependent version is good. It is simpler, and is required context appreciate the framerate-independent version. (I would also like to include the truly naive version with no delta: $Sprite2D.position = $Sprite2D.position.lerp(mouse_pos, FOLLOW_SPEED). But the explanation would be too long.)

The explanation of the framerate-independent version belongs in a note. The simple version does actually work in most cases, because physics delta is consistent in normal gameplay, more so than process delta.

Unfortunately, while it's easy enough to explain why just multiplying by delta doesn't work, it's hard to concisely explain why the framerate-independent version works. So I also included links to the a short-ish article deriving the better formula, and to a good but very long video on the problem and how to derive the better formula.

Both GDScript and C# examples have been tested and confirmed to work. I did not deeply test the formulas at varying framerates, though.

Note that the form used in this PR:

value = value.lerp(target, 1 - exp(-rate* delta))

seems different than the form used in the article (after changing the names and lerp() function signature):

value = target.lerp(value, exp(-rate*delta))

It's equivalent if you flip the target and value, and compensate with a 1 - x though:

value = value.lerp(target, 1 - exp(-rate*delta))

Note also that we use exp() in place of exp2(). This is still valid; only the rate value needs to change to compensate for the change in exponential base.

Interpolation can be used to smooth movement, rotation, etc. Here is an example of a circle following the mouse using smoothed motion:
Interpolation can be used to smoothly follow a moving target value, such as a
position or a rotation. Each frame, ``lerp()`` moves the current value towards
the target value by a fixed percentage of the remaining difference between the values.
Copy link
Contributor Author

@tetrapod00 tetrapod00 Nov 3, 2024

Choose a reason for hiding this comment

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

"Percentage" is technically incorrect here, since the value is in the range of [0,1] rather than [0,100]. "Portion" or maybe "fraction" is more technically correct, but have the wrong connotations - in English, "portion" can also mean a fixed amount rather than a value that depends on the total value.

Copy link
Member

@mhilbrunner mhilbrunner Nov 5, 2024

Choose a reason for hiding this comment

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

I think this is fine as is, I agree with the reasoning.

I think there is something getting lost here by removing "Interpolation can be used to smooth movement, rotation, etc". Thats actually telling less experienced users what this whole thing is useful for, which is my biggest quibble with the current state of the page.

Starting the page with "Interpolation is a very basic operation in graphics programming. It's good to become familiar with it in order to expand your horizons as a graphics developer" and "interpolation can be described as A * (1 - t) + B * t" is likely to lose a lot of less mathematically minded users.

I'd actually like to see the above sentence somewhere near the intro, a short what is this actually useful for in my game.

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'd be happy to do a pass on the rest of the page too, but I think that's slightly out of scope of this PR which just aims to improve the section on using lerp() to follow a moving target (or "lerp smoothing").

Copy link
Member

Choose a reason for hiding this comment

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

In that case I'm fine with this as is, too. That example is better for the beginning rather halfway down the page, anyway.

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 made a change to the very first paragraph:

  • bringing the removed sentence to the beginning of the page
  • removing one of the banned words "basic"
  • changing "graphics developer" to "game developer"

To be clear, the rest of this page still needs more work, but it's beyond the scope of this PR.

Comment on lines 150 to 151
the ``weight`` parameter of ``lerp()`` represents a *percentage* of the remaining
difference in values, not an *absolute amount to change*. In ``_physics_process()``,
Copy link
Contributor Author

@tetrapod00 tetrapod00 Nov 3, 2024

Choose a reason for hiding this comment

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

If it's not clear from a single sentence, explaining why weight being a percentage rather than an absolute amount matters feels beyond the scope of the docs. There are two good links to get a deeper explanation, if the reader wants them.

Of course, if the single sentence can be made more clear we should do that!

Copy link
Member

Choose a reason for hiding this comment

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

I think this is fine.

@AThousandShips AThousandShips added enhancement area:manual Issues and PRs related to the Manual/Tutorials section of the documentation topic:animation labels Nov 3, 2024
@aXu-AP
Copy link
Contributor

aXu-AP commented Nov 5, 2024

LGTM, keeps it simple while explaining the topic with enough depth.

tutorials/math/interpolation.rst Outdated Show resolved Hide resolved
Improves explanation of interpolation ("lerp") smoothing.
Adds a note about framerate-independent version, with off-site links to
full explanations.

Co-Authored-By: aXu-AP <[email protected]>
@mhilbrunner mhilbrunner merged commit 7eb4617 into godotengine:master Nov 5, 2024
1 check passed
@mhilbrunner
Copy link
Member

Thank you!

@tetrapod00 tetrapod00 deleted the lerp-smoothing branch November 5, 2024 23:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:manual Issues and PRs related to the Manual/Tutorials section of the documentation enhancement topic:animation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants