Ensure our delta time is capped to avoid huge calculation errors

isolation_bkp/dynres
Dominik Madarász 2021-10-27 11:31:42 +02:00
parent 7059e881fb
commit 4ca2dc4e96
5 changed files with 17 additions and 13 deletions

View File

@ -42,13 +42,13 @@ float smooth_val(float cur, float tgt, uint64_t dt) {
zpl_exit(0); zpl_exit(0);
#endif #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 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)*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) { void predict_receive_update(entity_view *d, entity_view *data) {

View File

@ -56,8 +56,8 @@ void IntegratePositions(ecs_iter_t *it) {
} }
#endif #endif
p[i].x += v[i].x * it->delta_time; p[i].x += v[i].x * safe_dt(it);
p[i].y += v[i].y * it->delta_time; p[i].y += v[i].y * safe_dt(it);
{ {
debug_v2 a = {p[i].x, p[i].y}; 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); 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 drag = zpl_clamp(blocks_get_drag(lookup.block_id), 0.0f, 1.0f);
float friction = blocks_get_friction(lookup.block_id); 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].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*it->delta_time); v[i].y = zpl_lerp(v[i].y, 0.0f, PHY_WALK_DRAG*drag*friction*safe_dt(it));
} }
} }

View File

@ -1,6 +1,10 @@
#pragma once #pragma once
#include "flecs/flecs.h" #include "flecs/flecs.h"
inline float safe_dt(ecs_iter_t *it) {
return zpl_min(it->delta_time, 0.03334f);
}
typedef struct { typedef struct {
// NOTE(zaklaus): Public systems are exposed here // NOTE(zaklaus): Public systems are exposed here
int32_t _unused; int32_t _unused;

View File

@ -10,7 +10,7 @@ void MovementImpulse(ecs_iter_t *it) {
world_block_lookup lookup = world_block_from_realpos(p[i].x, p[i].y); 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 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); 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].x += in[i].x*speed*drag*safe_dt(it);
v[i].y -= in[i].y*speed*drag*it->delta_time; v[i].y -= in[i].y*speed*drag*safe_dt(it);
} }
} }

View File

@ -105,15 +105,15 @@ void VehicleHandling(ecs_iter_t *it) {
if (j == 0) { if (j == 0) {
Input const* in = ecs_get(it->world, pe, Input); 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) { 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) if (zpl_abs(car->force) < 5.5f)
car->force = 0.0f; car->force = 0.0f;
} }
car->steer *= 0.97f; 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); 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_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)); 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].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)*it->delta_time*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); 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)); world_block_lookup lookahead = world_block_from_realpos(p[i].x+PHY_LOOKAHEAD(v[i].x), p[i].y+PHY_LOOKAHEAD(v[i].y));