From a374acc4f0b19b991f56491c94b1ef7f609d44c2 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Fri, 16 Feb 2024 12:51:05 +0100 Subject: [PATCH] Improved lerp interpolation --- client/camera/Camera.gd | 2 +- client/camera/FollowPlayer.gd | 2 +- client/item/soul/Soul.gd | 2 +- client/math/Math.gd | 10 ++++++++++ client/player/controller/ProxyController.gd | 4 ++-- client/player/rotation/RotationComponent.gd | 2 +- 6 files changed, 16 insertions(+), 6 deletions(-) create mode 100644 client/math/Math.gd diff --git a/client/camera/Camera.gd b/client/camera/Camera.gd index ac1e510..950f629 100644 --- a/client/camera/Camera.gd +++ b/client/camera/Camera.gd @@ -44,4 +44,4 @@ func _process(delta): if abs(distance - position.z) < 0.01: return - position.z = lerpf(position.z, distance, zoom_interpolation * delta) + position.z = Math.dampf(position.z, distance, zoom_interpolation * delta) diff --git a/client/camera/FollowPlayer.gd b/client/camera/FollowPlayer.gd index 71c5adc..bcba24d 100644 --- a/client/camera/FollowPlayer.gd +++ b/client/camera/FollowPlayer.gd @@ -8,7 +8,7 @@ func _process(delta): return if interpolate: - position = lerp(position, Global.player.position, speed * delta) + position = Math.damp(position, Global.player.position, speed * delta) else: position = Global.player.position diff --git a/client/item/soul/Soul.gd b/client/item/soul/Soul.gd index 70f67fe..73539bc 100644 --- a/client/item/soul/Soul.gd +++ b/client/item/soul/Soul.gd @@ -23,7 +23,7 @@ func _process(delta): if !collected_by: return - global_position = lerp(global_position, Global.player.global_position + Vector3.UP, 1.0 * delta) + global_position = Math.damp(global_position, Global.player.global_position + Vector3.UP, 1.0 * delta) func on_body_entered(body: Node3D): if body is Player: diff --git a/client/math/Math.gd b/client/math/Math.gd new file mode 100644 index 0000000..fc07277 --- /dev/null +++ b/client/math/Math.gd @@ -0,0 +1,10 @@ +class_name Math + +static func damp(from: Variant, to: Variant, weight: float, smoothing: float = 0.75): + return lerp(from, to, 1 - exp(-smoothing * weight)) + +static func dampf(from: float, to: float, weight: float, smoothing: float = 0.75): + return lerpf(from, to, 1 - exp(-smoothing * weight)) + +static func damp_angle(from: float, to: float, weight: float, smoothing: float = 0.75): + return lerp_angle(from, to, 1 - exp(-smoothing * weight)) \ No newline at end of file diff --git a/client/player/controller/ProxyController.gd b/client/player/controller/ProxyController.gd index 88e2925..27bb31f 100644 --- a/client/player/controller/ProxyController.gd +++ b/client/player/controller/ProxyController.gd @@ -15,5 +15,5 @@ func _ready(): func _process(delta: float): var time := interpolation_speed * delta - player.position.x = lerpf(player.position.x, server_position.x, time) - player.position.z = lerpf(player.position.z, server_position.z, time) \ No newline at end of file + player.position.x = Math.dampf(player.position.x, server_position.x, time) + player.position.z = Math.dampf(player.position.z, server_position.z, time) \ No newline at end of file diff --git a/client/player/rotation/RotationComponent.gd b/client/player/rotation/RotationComponent.gd index c448071..0ae2705 100644 --- a/client/player/rotation/RotationComponent.gd +++ b/client/player/rotation/RotationComponent.gd @@ -12,7 +12,7 @@ func _ready(): owner.controller.direction_changed.connect(on_direction_changed) func _process(delta): - root.rotation.y = lerp_angle(root.rotation.y, angle, rotation_speed * delta) + root.rotation.y = Math.damp_angle(root.rotation.y, angle, rotation_speed * delta) func on_direction_changed(new_direction: Vector3): direction = new_direction