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: if abs(distance - position.z) < 0.01:
return 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 return
if interpolate: if interpolate:
position = lerp(position, Global.player.position, speed * delta) position = Math.damp(position, Global.player.position, speed * delta)
else: else:
position = Global.player.position position = Global.player.position

View File

@ -23,7 +23,7 @@ func _process(delta):
if !collected_by: if !collected_by:
return 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): func on_body_entered(body: Node3D):
if body is Player: 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): func _process(delta: float):
var time := interpolation_speed * delta var time := interpolation_speed * delta
player.position.x = lerpf(player.position.x, server_position.x, time) player.position.x = Math.dampf(player.position.x, server_position.x, time)
player.position.z = lerpf(player.position.z, server_position.z, 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) owner.controller.direction_changed.connect(on_direction_changed)
func _process(delta): 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): func on_direction_changed(new_direction: Vector3):
direction = new_direction direction = new_direction