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
Closed
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
13 changes: 7 additions & 6 deletions tutorials/math/interpolation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -118,24 +118,25 @@ Interpolation can be used to smooth movement, rotation, etc. Here is an example

func _physics_process(delta):
var mouse_pos = get_local_mouse_position()

$Sprite2D.position = $Sprite2D.position.lerp(mouse_pos, delta * FOLLOW_SPEED)
Comment on lines -121 to -122
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.

var weight = 1 - exp(-FOLLOW_SPEED * delta)

$Sprite2D.position = $Sprite2D.position.lerp(mouse_pos, weight)

.. code-tab:: csharp

private const float FollowSpeed = 4.0f;

public override void _PhysicsProcess(float delta)
{
Vector2 mousePos = GetLocalMousePosition();

Sprite2D sprite = GetNode<Sprite2D>("Sprite2D");
Vector2 mousePos = GetLocalMousePosition();
float weight = 1f - Mathf.Exp(-FollowSpeed * delta);

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

sprite.Position = sprite.Position.Lerp(mousePos, weight);
}

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.