From 674e8b162800719486418a7e2b7f8624bb6e5714 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Wed, 14 Sep 2022 09:07:59 +0000 Subject: [PATCH] smooth out delta time --- code/game/src/packets/pkt_send_librg_update.c | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/code/game/src/packets/pkt_send_librg_update.c b/code/game/src/packets/pkt_send_librg_update.c index 37773e9..142fe58 100644 --- a/code/game/src/packets/pkt_send_librg_update.c +++ b/code/game/src/packets/pkt_send_librg_update.c @@ -20,6 +20,27 @@ size_t pkt_send_librg_update_encode(void *data, int32_t data_length, uint8_t lay return pkt_pack_msg_size(&pc); } +#define NUM_SAMPLES 128 + +static float smooth_time(float time) { + static float time_samples[NUM_SAMPLES] = {}; + static int32_t curr_index = 0; + + time_samples[curr_index] = time; + if (++curr_index == NUM_SAMPLES) + curr_index = 0; + + float average = 0; + for (int32_t i = NUM_SAMPLES; i--; ) + average += time_samples[i]; + average /= NUM_SAMPLES; + + time = zpl_min(time, average * 2); + return time; +} + +#undef NUM_SAMPLES + int32_t pkt_send_librg_update_handler(pkt_header *header) { cw_unpack_context uc = {0}; pkt_unpack_msg(&uc, header, 2); @@ -42,7 +63,7 @@ int32_t pkt_send_librg_update_handler(pkt_header *header) { if (state < 0) zpl_printf("[ERROR] world read error: %d\n", state); float now = (float)get_cached_time(); - view->delta_time[layer_id] = now - view->last_update[layer_id]; + view->delta_time[layer_id] = smooth_time(now - view->last_update[layer_id]); view->last_update[layer_id] = now; return state;