From 20eb13f4588245550937611f72fb5e608fd27178 Mon Sep 17 00:00:00 2001 From: Chris Biscardi Date: Tue, 18 Jun 2024 04:38:17 -0700 Subject: [PATCH] Use smooth_nudge in 2d_top_down_camera example (#13907) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Objective [`StableInterpolate`](https://dev-docs.bevyengine.org/bevy/math/trait.StableInterpolate.html) was introduced after this example was written (or more accurately, the two PRs were merged on the same day and developed in parallel). The example used `Vec3::lerp` where I believe it is now preferred to use `smooth_nudge`. ## Solution Its not entirely clear to me whether `StableInterpolate` should be preferred in this scenario, although it seems likely. So I figured a PR to make the change would either result in it being merged or denied with a reason. ## Testing ``` cargo run --example 2d_top_down_camera ``` Co-authored-by: François Mockers --- examples/camera/2d_top_down_camera.rs | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/examples/camera/2d_top_down_camera.rs b/examples/camera/2d_top_down_camera.rs index 7321712988d62..ad565b36f54bd 100644 --- a/examples/camera/2d_top_down_camera.rs +++ b/examples/camera/2d_top_down_camera.rs @@ -17,8 +17,8 @@ use bevy::sprite::{MaterialMesh2dBundle, Mesh2dHandle}; /// Player movement speed factor. const PLAYER_SPEED: f32 = 100.; -/// Camera lerp factor. -const CAM_LERP_FACTOR: f32 = 2.; +/// How quickly should the camera snap to the desired location. +const CAMERA_DECAY_RATE: f32 = 2.; #[derive(Component)] struct Player; @@ -103,14 +103,11 @@ fn update_camera( let Vec3 { x, y, .. } = player.translation; let direction = Vec3::new(x, y, camera.translation.z); - // Applies a smooth effect to camera movement using interpolation between - // the camera position and the player position on the x and y axes. - // Here we use the in-game time, to get the elapsed time (in seconds) - // since the previous update. This avoids jittery movement when tracking - // the player. - camera.translation = camera + // Applies a smooth effect to camera movement using stable interpolation + // between the camera position and the player position on the x and y axes. + camera .translation - .lerp(direction, time.delta_seconds() * CAM_LERP_FACTOR); + .smooth_nudge(&direction, CAMERA_DECAY_RATE, time.delta_seconds()); } /// Update the player position with keyboard inputs.