From 4ca2dc4e964d477bdbe85421d97038932afb74d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Wed, 27 Oct 2021 11:31:42 +0200 Subject: [PATCH] Ensure our delta time is capped to avoid huge calculation errors --- code/game/src/prediction.c | 4 ++-- code/modules/modules/systems.c | 8 ++++---- code/modules/modules/systems.h | 4 ++++ code/modules/source/system_onfoot.c | 4 ++-- code/modules/source/system_vehicle.c | 10 +++++----- 5 files changed, 17 insertions(+), 13 deletions(-) diff --git a/code/game/src/prediction.c b/code/game/src/prediction.c index eb76962..10e2fa4 100644 --- a/code/game/src/prediction.c +++ b/code/game/src/prediction.c @@ -42,13 +42,13 @@ float smooth_val(float cur, float tgt, uint64_t dt) { zpl_exit(0); #endif - return zpl_lerp(cur, tgt, zpl_lerp(PREDICT_SMOOTH_FACTOR_LO, PREDICT_SMOOTH_FACTOR_HI, factor)*platform_frametime()); + return zpl_lerp(cur, tgt, zpl_clamp01(zpl_lerp(PREDICT_SMOOTH_FACTOR_LO, PREDICT_SMOOTH_FACTOR_HI, factor)*platform_frametime())); } 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))); - return spherical_lerp(cur, tgt, zpl_lerp(PREDICT_SMOOTH_FACTOR_LO, PREDICT_SMOOTH_FACTOR_HI, factor)*platform_frametime()); + return spherical_lerp(cur, tgt, zpl_clamp01(zpl_lerp(PREDICT_SMOOTH_FACTOR_LO, PREDICT_SMOOTH_FACTOR_HI, factor)*platform_frametime())); } void predict_receive_update(entity_view *d, entity_view *data) { diff --git a/code/modules/modules/systems.c b/code/modules/modules/systems.c index 58c12f7..75a8e05 100644 --- a/code/modules/modules/systems.c +++ b/code/modules/modules/systems.c @@ -56,8 +56,8 @@ void IntegratePositions(ecs_iter_t *it) { } #endif - p[i].x += v[i].x * it->delta_time; - p[i].y += v[i].y * it->delta_time; + p[i].x += v[i].x * safe_dt(it); + p[i].y += v[i].y * safe_dt(it); { debug_v2 a = {p[i].x, p[i].y}; @@ -127,8 +127,8 @@ void ApplyWorldDragOnVelocity(ecs_iter_t *it) { world_block_lookup lookup = world_block_from_realpos(p[i].x, p[i].y); float drag = zpl_clamp(blocks_get_drag(lookup.block_id), 0.0f, 1.0f); float friction = blocks_get_friction(lookup.block_id); - v[i].x = zpl_lerp(v[i].x, 0.0f, PHY_WALK_DRAG*drag*friction*it->delta_time); - v[i].y = zpl_lerp(v[i].y, 0.0f, PHY_WALK_DRAG*drag*friction*it->delta_time); + v[i].x = zpl_lerp(v[i].x, 0.0f, PHY_WALK_DRAG*drag*friction*safe_dt(it)); + v[i].y = zpl_lerp(v[i].y, 0.0f, PHY_WALK_DRAG*drag*friction*safe_dt(it)); } } diff --git a/code/modules/modules/systems.h b/code/modules/modules/systems.h index 6e25fcb..1688f92 100644 --- a/code/modules/modules/systems.h +++ b/code/modules/modules/systems.h @@ -1,6 +1,10 @@ #pragma once #include "flecs/flecs.h" +inline float safe_dt(ecs_iter_t *it) { + return zpl_min(it->delta_time, 0.03334f); +} + typedef struct { // NOTE(zaklaus): Public systems are exposed here int32_t _unused; diff --git a/code/modules/source/system_onfoot.c b/code/modules/source/system_onfoot.c index 43bab03..0a97616 100644 --- a/code/modules/source/system_onfoot.c +++ b/code/modules/source/system_onfoot.c @@ -10,7 +10,7 @@ void MovementImpulse(ecs_iter_t *it) { world_block_lookup lookup = world_block_from_realpos(p[i].x, p[i].y); float drag = zpl_clamp(blocks_get_drag(lookup.block_id), 0.0f, 1.0f); double speed = PLR_MOVE_SPEED * (in[i].sprint ? PLR_MOVE_SPEED_MULT : 1.0); - v[i].x += in[i].x*speed*drag*it->delta_time; - v[i].y -= in[i].y*speed*drag*it->delta_time; + v[i].x += in[i].x*speed*drag*safe_dt(it); + v[i].y -= in[i].y*speed*drag*safe_dt(it); } } diff --git a/code/modules/source/system_vehicle.c b/code/modules/source/system_vehicle.c index fc51e3f..0eb0fb7 100644 --- a/code/modules/source/system_vehicle.c +++ b/code/modules/source/system_vehicle.c @@ -105,15 +105,15 @@ void VehicleHandling(ecs_iter_t *it) { if (j == 0) { Input const* in = ecs_get(it->world, pe, Input); - car->force += zpl_lerp(0.0f, in->y * VEHICLE_FORCE, VEHICLE_ACCEL*it->delta_time); + car->force += zpl_lerp(0.0f, in->y * VEHICLE_FORCE, VEHICLE_ACCEL*safe_dt(it)); if (in->sprint) { - car->force = zpl_lerp(car->force, 0.0f, VEHICLE_BRAKE_FORCE*it->delta_time); + car->force = zpl_lerp(car->force, 0.0f, VEHICLE_BRAKE_FORCE*safe_dt(it)); if (zpl_abs(car->force) < 5.5f) car->force = 0.0f; } car->steer *= 0.97f; - car->steer += (in->x * VEHICLE_STEER)*it->delta_time; + car->steer += (in->x * VEHICLE_STEER)*safe_dt(it); car->steer = zpl_clamp(car->steer, -40.0f, 40.0f); } } @@ -135,8 +135,8 @@ void VehicleHandling(ecs_iter_t *it) { fr_x += car->force * drag * zpl_cos(car->heading + zpl_to_radians(car->steer)); fr_y += car->force * drag * zpl_sin(car->heading + zpl_to_radians(car->steer)); - v[i].x += ((fr_x + bk_x) / 2.0f - p[i].x)*it->delta_time*VEHICLE_POWER; - v[i].y += ((fr_y + bk_y) / 2.0f - p[i].y)*it->delta_time*VEHICLE_POWER; + v[i].x += ((fr_x + bk_x) / 2.0f - p[i].x)*safe_dt(it)*VEHICLE_POWER; + v[i].y += ((fr_y + bk_y) / 2.0f - p[i].y)*safe_dt(it)*VEHICLE_POWER; car->heading = zpl_arctan2(fr_y - bk_y, fr_x - bk_x); world_block_lookup lookahead = world_block_from_realpos(p[i].x+PHY_LOOKAHEAD(v[i].x), p[i].y+PHY_LOOKAHEAD(v[i].y));