Enable prediction on SP

isolation_bkp/dynres
Dominik Madarász 2021-10-27 00:32:40 +02:00
parent 6f57fb178a
commit 4be5ee9b61
2 changed files with 122 additions and 115 deletions

View File

@ -1,114 +1,120 @@
#include "zpl.h" #include "zpl.h"
#include "prediction.h" #include "prediction.h"
#include "platform.h" #include "platform.h"
#include "world/world.h" #include "world/world.h"
#include "game.h" #include "game.h"
#define PREDICT_SMOOTH_FACTOR_LO 0.10 #define PREDICT_SMOOTH_FACTOR_LO 0.10
#define PREDICT_SMOOTH_FACTOR_HI 0.01 #define PREDICT_SMOOTH_FACTOR_HI 0.01
static inline float map_factor(float x) { static inline float map_factor(float x) {
x = 1.0f - zpl_clamp01(x); x = 1.0f - zpl_clamp01(x);
return 1.0f - x*x*x*x*x*x*x*x; return 1.0f - x*x*x*x*x*x*x*x;
} }
static inline float base_angle(float x) { static inline float base_angle(float x) {
while (x > ZPL_TAU) x -= ZPL_TAU; while (x > ZPL_TAU) x -= ZPL_TAU;
while (x < 0.0f) x += ZPL_TAU; while (x < 0.0f) x += ZPL_TAU;
return x; return x;
} }
static inline float spherical_lerp(float a, float b, float t) { static inline float spherical_lerp(float a, float b, float t) {
a = base_angle(a); a = base_angle(a);
b = base_angle(b); b = base_angle(b);
float d = b - a; float d = b - a;
if (d < -ZPL_PI) { if (d < -ZPL_PI) {
b += ZPL_TAU; b += ZPL_TAU;
} else if (d > ZPL_PI) { } else if (d > ZPL_PI) {
b -= ZPL_TAU; b -= ZPL_TAU;
} }
return base_angle(zpl_lerp(a, b, t)); return base_angle(zpl_lerp(a, b, t));
} }
float smooth_val(float cur, float tgt, uint64_t dt) { float smooth_val(float cur, float tgt, uint64_t dt) {
float factor = zpl_clamp01(map_factor(zpl_unlerp(dt, WORLD_TRACKER_UPDATE_MP_FAST_MS, WORLD_TRACKER_UPDATE_MP_SLOW_MS))); float factor = zpl_clamp01(map_factor(zpl_unlerp(dt, WORLD_TRACKER_UPDATE_MP_FAST_MS, WORLD_TRACKER_UPDATE_MP_SLOW_MS)));
#if 0 #if 0
dt = 200; dt = 200;
factor = map_factor(zpl_unlerp(dt, WORLD_TRACKER_UPDATE_MP_FAST_MS, WORLD_TRACKER_UPDATE_MP_SLOW_MS)); factor = map_factor(zpl_unlerp(dt, WORLD_TRACKER_UPDATE_MP_FAST_MS, WORLD_TRACKER_UPDATE_MP_SLOW_MS));
zpl_printf("lerp factor: %f\n", factor); zpl_printf("lerp factor: %f\n", factor);
zpl_exit(0); zpl_exit(0);
#endif #endif
return zpl_lerp(cur, tgt, zpl_lerp(PREDICT_SMOOTH_FACTOR_LO, PREDICT_SMOOTH_FACTOR_HI, factor)); return zpl_lerp(cur, tgt, zpl_lerp(PREDICT_SMOOTH_FACTOR_LO, PREDICT_SMOOTH_FACTOR_HI, factor));
} }
float smooth_val_spherical(float cur, float tgt, uint64_t dt) { float smooth_val_spherical(float cur, float tgt, uint64_t dt) {
float factor = zpl_clamp01(map_factor(zpl_unlerp(dt, WORLD_TRACKER_UPDATE_MP_FAST_MS, WORLD_TRACKER_UPDATE_MP_SLOW_MS))); float factor = zpl_clamp01(map_factor(zpl_unlerp(dt, WORLD_TRACKER_UPDATE_MP_FAST_MS, WORLD_TRACKER_UPDATE_MP_SLOW_MS)));
return spherical_lerp(cur, tgt, zpl_lerp(PREDICT_SMOOTH_FACTOR_LO, PREDICT_SMOOTH_FACTOR_HI, factor)); return spherical_lerp(cur, tgt, zpl_lerp(PREDICT_SMOOTH_FACTOR_LO, PREDICT_SMOOTH_FACTOR_HI, factor));
} }
void predict_receive_update(entity_view *d, entity_view *data) { void predict_receive_update(entity_view *d, entity_view *data) {
if (d && data->flag & EFLAG_INTERP) { if (d && data->flag & EFLAG_INTERP) {
// NOTE(zaklaus): store target pos but keep x,y unchanged // NOTE(zaklaus): store target pos but keep x,y unchanged
float tx = data->x; float tx = data->x;
float ty = data->y; float ty = data->y;
float theading = data->heading; float theading = data->heading;
data->x = d->x; data->x = d->x;
data->y = d->y; data->y = d->y;
data->heading = d->heading; data->heading = d->heading;
data->tx = tx; data->tx = tx;
data->ty = ty; data->ty = ty;
data->theading = theading; data->theading = theading;
} }
data->tran_effect = d->tran_effect; data->tran_effect = d->tran_effect;
data->tran_time = d->tran_time; data->tran_time = d->tran_time;
} }
#define ENTITY_DO_LERP_SP 1
void lerp_entity_positions(uint64_t key, entity_view *data) {
(void)key; void lerp_entity_positions(uint64_t key, entity_view *data) {
world_view *view = game_world_view_get_active(); (void)key;
world_view *view = game_world_view_get_active();
if (data->flag == EFLAG_INTERP) {
if (game_get_kind() == GAMEKIND_CLIENT) { if (data->flag == EFLAG_INTERP) {
data->x = smooth_val(data->x, data->tx, view->delta_time[data->layer_id]); #if ENTITY_DO_LERP_SP==0
data->y = smooth_val(data->y, data->ty, view->delta_time[data->layer_id]); if (game_get_kind() == GAMEKIND_CLIENT)
data->heading = smooth_val_spherical(data->heading, data->theading, view->delta_time[data->layer_id]); #else
} else { if (1)
(void)view; #endif
data->x = data->tx; {
data->y = data->ty; data->x = smooth_val(data->x, data->tx, view->delta_time[data->layer_id]);
data->heading = data->theading; data->y = smooth_val(data->y, data->ty, view->delta_time[data->layer_id]);
} data->heading = smooth_val_spherical(data->heading, data->theading, view->delta_time[data->layer_id]);
} } else {
} (void)view;
data->x = data->tx;
void do_entity_fadeinout(uint64_t key, entity_view * data) { data->y = data->ty;
(void)key; data->heading = data->theading;
switch (data->tran_effect) { }
case ETRAN_FADEIN: { }
data->tran_time += platform_frametime(); }
if (data->tran_time > 1.0f) { void do_entity_fadeinout(uint64_t key, entity_view * data) {
data->tran_effect = ETRAN_NONE; (void)key;
data->tran_time = 1.0f; switch (data->tran_effect) {
} case ETRAN_FADEIN: {
}break; data->tran_time += platform_frametime();
case ETRAN_FADEOUT: { if (data->tran_time > 1.0f) {
data->tran_time -= platform_frametime(); data->tran_effect = ETRAN_NONE;
data->tran_time = 1.0f;
if (data->tran_time < 0.0f) { }
data->tran_effect = ETRAN_REMOVE; }break;
data->tran_time = 0.0f;
} case ETRAN_FADEOUT: {
}break; data->tran_time -= platform_frametime();
default: break; if (data->tran_time < 0.0f) {
} data->tran_effect = ETRAN_REMOVE;
} data->tran_time = 0.0f;
}
}break;
default: break;
}
}

View File

@ -173,7 +173,8 @@ void VehicleHandling(ecs_iter_t *it) {
{ {
float dx = zpl_sin(car->heading); float dx = zpl_sin(car->heading);
float dy = -zpl_cos(car->heading); float dy = -zpl_cos(car->heading);
debug_push_circle((debug_v2){p[i].x+dx*car->steer*-20, p[i].y+dy*car->steer*-20}, 5.0f, 0x00FFAAFF); float steer_mult = -80.0f;
debug_push_circle((debug_v2){p[i].x+dx*car->steer*steer_mult, p[i].y+dy*car->steer*steer_mult}, 5.0f, 0x00FFAAFF);
} }
} }
} }