Armyk
Member
video:
I have recently dabbled into networking and multiplayer and I just can't figure out why is my client side interpolation glitching out. (the video)
The client sends keyboard inputs to the authoritative game server, the server receives and processes the inputs fine, then it sends the "snapshot" (x and y position of the player) back for the client to process it.
I am storing these snapshots in a ds_list along with the time of receiving.
After that, the player starts interpolating between these snapshots. It works well, but it glitches out sometime for some reason that I cannot find.
This is the log file I am creating the hsp jumps high somethimes causing the glitch:
Can you please help me? Thank you.
I have recently dabbled into networking and multiplayer and I just can't figure out why is my client side interpolation glitching out. (the video)
The client sends keyboard inputs to the authoritative game server, the server receives and processes the inputs fine, then it sends the "snapshot" (x and y position of the player) back for the client to process it.
I am storing these snapshots in a ds_list along with the time of receiving.
Code:
//vars
var client_id = buffer_read(server_buffer,buffer_u8);
var px = buffer_read(server_buffer,buffer_u16);
var py = buffer_read(server_buffer,buffer_u16);
var receive_tick = buffer_read(server_buffer,buffer_u16);
//player
var player = connected_players[? client_id];
if (player != undefined) {
var size = ds_list_size(player.movement_snapshots);
if (size > 1) {
var last_snapshot = player.movement_snapshots[| size-1];
if (receive_tick > last_snapshot[3]) {
ds_list_add(player.movement_snapshots,[px,py,current_time,receive_tick]); //[x,y,receive_time,server_tick]
} else {
scrDebugMessage("receive tick ("+string(receive_tick)+") lower than server tick("+string(last_snapshot[3])+")");
}
} else {
ds_list_add(player.movement_snapshots,[px,py,current_time,receive_tick]); //[x,y,receive_time,server_tick]
}
}
Code:
//vars
var size = ds_list_size(movement_snapshots);
var delay = 100;
var render_time = current_time - delay;
//exit if no snapshot
if (size < 1) return;
//just teleport
if (size == 1) {
var p = movement_snapshots[| size-1];
x = p[0];
y = p[1];
return;
}
//cleanup old snapshots from the list
var nearest_future_snapshot = movement_snapshots[| 1];
while (size > 2 and render_time > nearest_future_snapshot[2]) {
ds_list_delete(movement_snapshots,0);
nearest_future_snapshot = movement_snapshots[| 1];
}
//find nearest past snapshot and nearest future one
var nearest_past = movement_snapshots[| 0];
var nearest_future = movement_snapshots[| 1];
//lerp between the old x and the future x
var lerp_time = min((render_time - nearest_past[2]) / (nearest_future[2] - nearest_past[2]),1); //(render_time - past_time) / (total time between future and past snapshot)
x = lerp(nearest_past[0],nearest_future[0],lerp_time);
y = lerp(nearest_past[1],nearest_future[1],lerp_time);
//debugging
hsp = x-last_x;
last_x = x;
if (last_snapshot != nearest_past) show_debug_message("[" + string(render_time) + "]" + " hsp:" + string(hsp) + " size:" + string(size) + " full time:" + string(nearest_future[2] - nearest_past[2]) + " lerp_time:" + string(lerp_time) + " x:" + string(x) + " last_x:" + string(nearest_past[0]) + " dest_x:" + string(nearest_future[0]));
last_snapshot = nearest_past;
Code:
[23918] hsp:3.91 size:5 full time:34 lerp_time:0.50 x:1032.50 last_x:1029 dest_x:1036
[23952] hsp:2.43 size:5 full time:49 lerp_time:0.35 x:1038.43 last_x:1036 dest_x:1043
[23985] hsp:2.35 size:5 full time:34 lerp_time:0.03 x:1043.21 last_x:1043 dest_x:1050
[24034] hsp:7 size:5 full time:16 lerp_time:1 x:1057 last_x:1050 dest_x:1057 THIS ONE IS CAUSING THE GLITCH
[24051] hsp:2.33 size:5 full time:51 lerp_time:0.33 x:1059.33 last_x:1057 dest_x:1064
Last edited by a moderator: