Improved network interpolation

This commit is contained in:
Eduard Urbach 2024-02-15 18:06:44 +01:00
parent b439919ad0
commit 2e7b385b46
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
6 changed files with 22 additions and 18 deletions

View File

@ -84,7 +84,6 @@ packet_type = 11
[node name="PlayerMove" type="Node" parent="Client"] [node name="PlayerMove" type="Node" parent="Client"]
script = ExtResource("8_ke1yy") script = ExtResource("8_ke1yy")
delay = 100
packet_type = 12 packet_type = 12
[node name="PlayerJump" type="Node" parent="Client"] [node name="PlayerJump" type="Node" parent="Client"]

View File

@ -9,11 +9,12 @@ func handle_packet(data: PackedByteArray):
var buffer := StreamPeerBuffer.new() var buffer := StreamPeerBuffer.new()
buffer.data_array = data buffer.data_array = data
var player_id_length := buffer.get_size() - 8 var player_id_length := buffer.get_size() - 4 - 4 - 4 - 4
var player_id := buffer.get_string(player_id_length) var player_id := buffer.get_string(player_id_length)
var x := buffer.get_float() var x := buffer.get_float()
# var y := buffer.get_float()
var z := buffer.get_float() var z := buffer.get_float()
var direction_x := buffer.get_float()
var direction_z := buffer.get_float()
var player := Global.players.get_player(player_id) var player := Global.players.get_player(player_id)
@ -24,9 +25,10 @@ func handle_packet(data: PackedByteArray):
if !controller: if !controller:
return return
controller.server_position.x = x controller.server_position.x = x
controller.server_position.z = z controller.server_position.z = z
controller.direction_changed.emit(Vector3(direction_x, 0, direction_z))
func _physics_process(_delta): func _physics_process(_delta):
if Global.player == null: if Global.player == null:
@ -46,6 +48,7 @@ func send_position():
var buffer := StreamPeerBuffer.new() var buffer := StreamPeerBuffer.new()
buffer.put_8(PacketHandler.Packet.PLAYER_MOVE) buffer.put_8(PacketHandler.Packet.PLAYER_MOVE)
buffer.put_float(Global.player.position.x) buffer.put_float(Global.player.position.x)
# buffer.put_float(Global.player.position.y)
buffer.put_float(Global.player.position.z) buffer.put_float(Global.player.position.z)
buffer.put_float(Global.player.movement.direction.x)
buffer.put_float(Global.player.movement.direction.z)
%Client.send(buffer.data_array) %Client.send(buffer.data_array)

View File

@ -4,6 +4,10 @@ extends Character
signal name_changed(new_name: String) signal name_changed(new_name: String)
var id: String var id: String
var movement: MovementComponent
func _enter_tree():
movement = get_node("Movement")
func set_player_name(new_name: String): func set_player_name(new_name: String):
name = new_name name = new_name

View File

@ -1,10 +1,9 @@
class_name ProxyController class_name ProxyController
extends Controller extends Controller
## The character that we're controlling. @export var interpolation_speed: float = 10.0
var player: Player
## The authoritative position on the server. var player: Player
var server_position: Vector3 var server_position: Vector3
func _init(new_player: Player): func _init(new_player: Player):
@ -14,13 +13,7 @@ func _init(new_player: Player):
func _ready(): func _ready():
server_position = player.position server_position = player.position
func _process(_delta): func _process(delta: float):
var move := server_position - player.position var time := interpolation_speed * delta
move.y = 0.0 player.position.x = lerpf(player.position.x, server_position.x, time)
player.position.z = lerpf(player.position.z, server_position.z, time)
if move.length_squared() < 0.02:
direction_changed.emit(Vector3.ZERO)
return
var direction := Vector3(move.x, 0, move.z).normalized()
direction_changed.emit(direction)

View File

@ -21,10 +21,14 @@ func (game *Game) Move(data []byte, address *net.UDPAddr) error {
player.Position.X = math.Float32frombits(binary.LittleEndian.Uint32(data)) player.Position.X = math.Float32frombits(binary.LittleEndian.Uint32(data))
player.Position.Z = math.Float32frombits(binary.LittleEndian.Uint32(data[4:])) player.Position.Z = math.Float32frombits(binary.LittleEndian.Uint32(data[4:]))
player.direction.X = math.Float32frombits(binary.LittleEndian.Uint32(data[8:]))
player.direction.Z = math.Float32frombits(binary.LittleEndian.Uint32(data[12:]))
update := []byte(player.ID) update := []byte(player.ID)
update = AppendFloat(update, player.Position.X) update = AppendFloat(update, player.Position.X)
update = AppendFloat(update, player.Position.Z) update = AppendFloat(update, player.Position.Z)
update = AppendFloat(update, player.direction.X)
update = AppendFloat(update, player.direction.Z)
game.BroadcastOthers(PlayerMove, update, player) game.BroadcastOthers(PlayerMove, update, player)
return nil return nil

View File

@ -12,6 +12,7 @@ type Player struct {
game *Game game *Game
authToken string authToken string
address *net.UDPAddr address *net.UDPAddr
direction Vector3
lastPacket time.Time lastPacket time.Time
} }