Improved lerp interpolation

This commit is contained in:
Eduard Urbach 2024-02-16 12:51:05 +01:00
parent 2a53a731db
commit a374acc4f0
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
6 changed files with 16 additions and 6 deletions

View File

@ -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)

View File

@ -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

View File

@ -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:

10
client/math/Math.gd Normal file
View File

@ -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))

View File

@ -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)
player.position.x = Math.dampf(player.position.x, server_position.x, time)
player.position.z = Math.dampf(player.position.z, server_position.z, time)

View File

@ -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