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"]
script = ExtResource("8_ke1yy")
delay = 100
packet_type = 12
[node name="PlayerJump" type="Node" parent="Client"]

View File

@ -9,11 +9,12 @@ func handle_packet(data: PackedByteArray):
var buffer := StreamPeerBuffer.new()
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 x := buffer.get_float()
# var y := 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)
@ -27,6 +28,7 @@ func handle_packet(data: PackedByteArray):
controller.server_position.x = x
controller.server_position.z = z
controller.direction_changed.emit(Vector3(direction_x, 0, direction_z))
func _physics_process(_delta):
if Global.player == null:
@ -46,6 +48,7 @@ func send_position():
var buffer := StreamPeerBuffer.new()
buffer.put_8(PacketHandler.Packet.PLAYER_MOVE)
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.movement.direction.x)
buffer.put_float(Global.player.movement.direction.z)
%Client.send(buffer.data_array)

View File

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

View File

@ -1,10 +1,9 @@
class_name ProxyController
extends Controller
## The character that we're controlling.
var player: Player
@export var interpolation_speed: float = 10.0
## The authoritative position on the server.
var player: Player
var server_position: Vector3
func _init(new_player: Player):
@ -14,13 +13,7 @@ func _init(new_player: Player):
func _ready():
server_position = player.position
func _process(_delta):
var move := server_position - player.position
move.y = 0.0
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)
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)

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.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 = AppendFloat(update, player.Position.X)
update = AppendFloat(update, player.Position.Z)
update = AppendFloat(update, player.direction.X)
update = AppendFloat(update, player.direction.Z)
game.BroadcastOthers(PlayerMove, update, player)
return nil

View File

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