Improved movement interpolation

This commit is contained in:
Eduard Urbach 2024-02-23 20:13:46 +01:00
parent 7926b7a80e
commit fcee55d9a4
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
10 changed files with 48 additions and 31 deletions

View File

@ -22,7 +22,7 @@ var look_enabled: bool
@export_group("Zoom") @export_group("Zoom")
@export var zoom_speed := 0.5 @export var zoom_speed := 0.5
@export var zoom_interpolation := 10.0 @export var zoom_interpolation := 7.5
@export var zoom_min := 2.5 @export var zoom_min := 2.5
@export var zoom_max := 8.0 @export var zoom_max := 8.0
var distance: float var distance: float

View File

@ -24,7 +24,7 @@ func _process(delta):
if !collected_by: if !collected_by:
return return
global_position = Math.damp(global_position, Global.player.global_position + Vector3.UP, 1.0 * delta) global_position = Math.damp(global_position, Global.player.global_position + Vector3.UP, 0.75 * delta)
func on_body_entered(body: Node3D): func on_body_entered(body: Node3D):
if not body is Player: if not body is Player:

View File

@ -1,16 +1,16 @@
class_name Math class_name Math
static func damp(from: Variant, to: Variant, weight: float, smoothing: float = 0.75): static func damp(from: Variant, to: Variant, weight: float):
return lerp(from, to, 1 - exp(-smoothing * weight)) return lerp(from, to, 1 - exp(-weight))
static func dampf(from: float, to: float, weight: float, smoothing: float = 0.75): static func dampf(from: float, to: float, weight: float):
return lerpf(from, to, 1 - exp(-smoothing * weight)) return lerpf(from, to, 1 - exp(-weight))
static func damp_angle(from: float, to: float, weight: float, smoothing: float = 0.75): static func damp_angle(from: float, to: float, weight: float):
return lerp_angle(from, to, 1 - exp(-smoothing * weight)) return lerp_angle(from, to, 1 - exp(-weight))
static func damp_spherical(from: Vector3, to: Vector3, weight: float, smoothing: float = 0.75): static func damp_spherical(from: Vector3, to: Vector3, weight: float):
return from.slerp(to, 1 - exp(-smoothing * weight)) return from.slerp(to, 1 - exp(-weight))
static func from_to_rotation(from: Vector3, to: Vector3) -> Quaternion: static func from_to_rotation(from: Vector3, to: Vector3) -> Quaternion:
var axis := from.cross(to).normalized() var axis := from.cross(to).normalized()

View File

@ -21,11 +21,10 @@ func handle_packet(data: PackedByteArray):
%Client.log("Add player: %s %s @ %v" % [player_id, player_name, server_position]) %Client.log("Add player: %s %s @ %v" % [player_id, player_name, server_position])
var player := spawn_player(player_id) var player := spawn_player(player_id, server_position)
player.position = server_position
player.set_player_name(player_name) player.set_player_name(player_name)
func spawn_player(id: String) -> Player: func spawn_player(id: String, position: Vector3) -> Player:
var player: Player var player: Player
if Global.players.has(id): if Global.players.has(id):
@ -38,6 +37,7 @@ func spawn_player(id: String) -> Player:
player = player_scene.instantiate() player = player_scene.instantiate()
player.id = id player.id = id
player.position = position
if id == Global.account.id: if id == Global.account.id:
player.controller = PlayerController.new(player) player.controller = PlayerController.new(player)

View File

@ -1,9 +1,15 @@
class_name PlayerMove
extends PacketHandler extends PacketHandler
@export var delay := 50 const updates_per_second := 20.0
var last_sent := Time.get_ticks_msec() var delay: float
var last_sent := Time.get_unix_time_from_system()
var last_sent_position := Vector3.ZERO var last_sent_position := Vector3.ZERO
var last_sent_direction := Vector3.ZERO
func _ready():
delay = 1.0 / updates_per_second
func handle_packet(data: PackedByteArray): func handle_packet(data: PackedByteArray):
var buffer := StreamPeerBuffer.new() var buffer := StreamPeerBuffer.new()
@ -30,21 +36,22 @@ func handle_packet(data: PackedByteArray):
controller.server_position.z = z controller.server_position.z = z
controller.direction_changed.emit(Vector3(direction_x, 0, direction_z)) controller.direction_changed.emit(Vector3(direction_x, 0, direction_z))
func _physics_process(_delta): func _process(_delta):
if !Global.player: if !Global.player:
return return
if Global.player.position == last_sent_position: if Global.player.position == last_sent_position && Global.player.movement.direction == last_sent_direction:
return return
if Time.get_ticks_msec() < last_sent + delay: if Time.get_unix_time_from_system() < last_sent + delay:
return return
send_position() send_movement()
last_sent = Time.get_ticks_msec() last_sent = Time.get_unix_time_from_system()
last_sent_position = Global.player.position last_sent_position = Global.player.position
last_sent_direction = Global.player.movement.direction
func send_position(): func send_movement():
var buffer := StreamPeerBuffer.new() var buffer := StreamPeerBuffer.new()
buffer.put_u8(PacketHandler.Packet.PLAYER_MOVE) buffer.put_u8(PacketHandler.Packet.PLAYER_MOVE)
buffer.put_float(Global.player.position.x) buffer.put_float(Global.player.position.x)

View File

@ -0,0 +1,13 @@
class_name BotController
extends Controller
var turn: float
var turn_speed: float = 2.0
func _process(delta):
if (Time.get_ticks_msec() / 1000) % 2 == 0:
direction_changed.emit(Vector3.ZERO)
else:
turn += turn_speed * delta
var direction := Vector3.RIGHT.rotated(Vector3.UP, turn)
direction_changed.emit(direction)

View File

@ -1,7 +1,7 @@
class_name ProxyController class_name ProxyController
extends Controller extends Controller
@export var interpolation_speed: float = 10.0 @export var interpolation_speed := 5.0
var player: Player var player: Player
var server_position: Vector3 var server_position: Vector3
@ -13,7 +13,7 @@ func _init(new_player: Player):
func _ready(): func _ready():
server_position = player.position server_position = player.position
func _process(delta: float): func _physics_process(delta: float):
var time := interpolation_speed * delta var time := interpolation_speed * delta
player.position.x = Math.dampf(player.position.x, server_position.x, 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) player.position.z = Math.dampf(player.position.z, server_position.z, time)

View File

@ -3,7 +3,7 @@ extends Node
@export var move_speed := 4.5 @export var move_speed := 4.5
@export var jump_velocity := 4.5 @export var jump_velocity := 4.5
@export var deceleration := 0.75 @export var deceleration_speed := 20
var body: Character var body: Character
var direction: Vector3 var direction: Vector3
@ -20,8 +20,8 @@ func _physics_process(delta):
body.velocity.x = direction.x * move_speed body.velocity.x = direction.x * move_speed
body.velocity.z = direction.z * move_speed body.velocity.z = direction.z * move_speed
else: else:
body.velocity.x *= deceleration body.velocity.x = Math.dampf(body.velocity.x, 0, deceleration_speed * delta)
body.velocity.z *= deceleration body.velocity.z = Math.dampf(body.velocity.z, 0, deceleration_speed * delta)
if !body.is_on_floor(): if !body.is_on_floor():
body.velocity.y -= gravity * delta body.velocity.y -= gravity * delta

View File

@ -2,7 +2,7 @@ class_name RotationComponent
extends Node extends Node
@export var root: Node3D @export var root: Node3D
@export var rotation_speed: float = 20.0 @export var rotation_speed: float = 15.0
var direction: Vector3 var direction: Vector3
var angle: float var angle: float

View File

@ -37,10 +37,6 @@ window/stretch/mode="canvas_items"
window/stretch/aspect="expand" window/stretch/aspect="expand"
window/vsync/vsync_mode=0 window/vsync/vsync_mode=0
[editor]
run/main_run_args="--offline"
[editor_plugins] [editor_plugins]
enabled=PackedStringArray("res://addons/terrain_3d/plugin.cfg") enabled=PackedStringArray("res://addons/terrain_3d/plugin.cfg")
@ -167,4 +163,5 @@ skill_4={
[physics] [physics]
common/physics_jitter_fix=0.0
3d/physics_engine="JoltPhysics3D" 3d/physics_engine="JoltPhysics3D"