6 Module DEAD_RECKONING
gullradriel edited this page 2026-10-29 20:54:40 +01:00

DEAD_RECKONING: latency hiding with extrapolation and convergence blending

Structures

Name
struct DR_VEC3
3D vector used for position, velocity, and acceleration
struct DR_STATE
A snapshot of entity kinematic state at a point in time.
struct DR_ENTITY
Dead reckoned entity with extrapolation and convergence state.

Types

Name
enum DR_BLEND { DR_BLEND_SNAP = 0, DR_BLEND_PVB = 1, DR_BLEND_CUBIC = 2}
Dead reckoning convergence/blending mode.
enum DR_ALGO { DR_ALGO_VEL_ACC = 2, DR_ALGO_VEL = 1, DR_ALGO_STATIC = 0}
Dead reckoning extrapolation algorithm.

Functions

Name
DR_VEC3 dr_vec3_zero(void )
Zero vector.
DR_VEC3 dr_vec3_sub(DR_VEC3 a, DR_VEC3 b)
Vector subtraction: a - b.
DR_VEC3 dr_vec3_scale(DR_VEC3 v, double s)
Scalar multiplication: v * s.
DR_VEC3 dr_vec3_lerp(DR_VEC3 a, DR_VEC3 b, double t)
Linear interpolation: a + (b - a) * t.
double dr_vec3_length_sq(DR_VEC3 v)
Squared length of vector.
double dr_vec3_length(DR_VEC3 v)
Length of vector.
double dr_vec3_distance(DR_VEC3 a, DR_VEC3 b)
Distance between two points.
DR_VEC3 dr_vec3_add(DR_VEC3 a, DR_VEC3 b)
Vector addition: a + b.
DR_VEC3 dr_vec3(double x, double y, double z)
Create a DR_VEC3 from components.
void dr_entity_set_threshold(DR_ENTITY * entity, double threshold)
set the position error threshold
void dr_entity_set_position(DR_ENTITY * entity, const DR_VEC3 * pos, const DR_VEC3 * vel, const DR_VEC3 * acc, double time)
force-set entity position without blending
void dr_entity_set_blend_time(DR_ENTITY * entity, double blend_time)
set the convergence blend duration
void dr_entity_set_blend_mode(DR_ENTITY * entity, DR_BLEND blend_mode)
set the convergence blending mode
void dr_entity_set_algo(DR_ENTITY * entity, DR_ALGO algo)
set the extrapolation algorithm
void dr_entity_receive_state(DR_ENTITY * entity, const DR_VEC3 * pos, const DR_VEC3 * vel, const DR_VEC3 * acc, double time)
receive a new authoritative state update from the network
DR_VEC3 dr_entity_extrapolate(const DR_ENTITY * entity, const DR_STATE * state, double dt)
extrapolate a kinematic state forward by dt seconds
void dr_entity_destroy(DR_ENTITY ** entity_ptr)
destroy a dead reckoning entity and free its memory
DR_ENTITY * dr_entity_create(DR_ALGO algo, DR_BLEND blend_mode, double pos_threshold, double blend_time)
create a new dead reckoning entity
void dr_entity_compute(DR_ENTITY * entity, double time, DR_VEC3 * out_pos)
compute the dead reckoned display position at a given time
bool dr_entity_check_threshold(const DR_ENTITY * entity, const DR_VEC3 * true_pos, const DR_VEC3 * true_vel, const DR_VEC3 * true_acc, double time)
check if the owner's true state has diverged beyond the threshold

Types Documentation

enum DR_BLEND

Enumerator Value Description
DR_BLEND_SNAP 0 Snap instantly to new state (no smoothing).
DR_BLEND_PVB 1 Projective Velocity Blending (recommended).
DR_BLEND_CUBIC 2 Cubic Bezier spline convergence.

Dead reckoning convergence/blending mode.

Determines how the entity transitions from its predicted position to the newly received authoritative position.

enum DR_ALGO

Enumerator Value Description
DR_ALGO_VEL_ACC 2 Velocity + acceleration: P(t) = P0 + V0t + 0.5A0*t^2.
DR_ALGO_VEL 1 Velocity only: P(t) = P0 + V0*t.
DR_ALGO_STATIC 0 No extrapolation; entity stays at last position.

Dead reckoning extrapolation algorithm.

Determines how the entity position is predicted forward in time from the last known kinematic state.

Functions Documentation

function dr_vec3_zero

static inline DR_VEC3 dr_vec3_zero(
    void 
)

Zero vector.

function dr_vec3_sub

static inline DR_VEC3 dr_vec3_sub(
    DR_VEC3 a,
    DR_VEC3 b
)

Vector subtraction: a - b.

function dr_vec3_scale

static inline DR_VEC3 dr_vec3_scale(
    DR_VEC3 v,
    double s
)

Scalar multiplication: v * s.

function dr_vec3_lerp

static inline DR_VEC3 dr_vec3_lerp(
    DR_VEC3 a,
    DR_VEC3 b,
    double t
)

Linear interpolation: a + (b - a) * t.

function dr_vec3_length_sq

static inline double dr_vec3_length_sq(
    DR_VEC3 v
)

Squared length of vector.

function dr_vec3_length

static inline double dr_vec3_length(
    DR_VEC3 v
)

Length of vector.

function dr_vec3_distance

static inline double dr_vec3_distance(
    DR_VEC3 a,
    DR_VEC3 b
)

Distance between two points.

function dr_vec3_add

static inline DR_VEC3 dr_vec3_add(
    DR_VEC3 a,
    DR_VEC3 b
)

Vector addition: a + b.

function dr_vec3

static inline DR_VEC3 dr_vec3(
    double x,
    double y,
    double z
)

Create a DR_VEC3 from components.

function dr_entity_set_threshold

void dr_entity_set_threshold(
    DR_ENTITY * entity,
    double threshold
)

set the position error threshold

Parameters:

  • entity the entity
  • threshold new threshold value (distance units)

Set the position error threshold for triggering network updates.

set the position error threshold

function dr_entity_set_position

void dr_entity_set_position(
    DR_ENTITY * entity,
    const DR_VEC3 * pos,
    const DR_VEC3 * vel,
    const DR_VEC3 * acc,
    double time
)

force-set entity position without blending

Parameters:

  • entity the entity
  • pos position (required)
  • vel velocity (NULL treated as zero)
  • acc acceleration (NULL treated as zero)
  • time timestamp

Force-set entity position without triggering convergence blending.

force-set entity position without blending

Used for teleportation or initial placement.

function dr_entity_set_blend_time

void dr_entity_set_blend_time(
    DR_ENTITY * entity,
    double blend_time
)

set the convergence blend duration

Parameters:

  • entity the entity
  • blend_time new blend time in seconds

Set the convergence blend duration.

set the convergence blend duration

function dr_entity_set_blend_mode

void dr_entity_set_blend_mode(
    DR_ENTITY * entity,
    DR_BLEND blend_mode
)

set the convergence blending mode

Parameters:

  • entity the entity
  • blend_mode new blending mode

Set the convergence blending mode.

set the convergence blending mode

function dr_entity_set_algo

void dr_entity_set_algo(
    DR_ENTITY * entity,
    DR_ALGO algo
)

set the extrapolation algorithm

Parameters:

  • entity the entity
  • algo new algorithm

Set the extrapolation algorithm.

set the extrapolation algorithm

function dr_entity_receive_state

void dr_entity_receive_state(
    DR_ENTITY * entity,
    const DR_VEC3 * pos,
    const DR_VEC3 * vel,
    const DR_VEC3 * acc,
    double time
)

receive a new authoritative state update from the network

Parameters:

  • entity the entity
  • pos new authoritative position (required)
  • vel new authoritative velocity (NULL treated as zero)
  • acc new authoritative acceleration (NULL treated as zero)
  • time timestamp of the update

Receive a new authoritative state update from the network.

receive a new authoritative state update from the network

This is called when a new state packet arrives from the network (or when the owner determines a new update is needed). It records the current predicted position as prev_predicted so the convergence algorithm can smoothly blend from there to the new state's extrapolation.

function dr_entity_extrapolate

DR_VEC3 dr_entity_extrapolate(
    const DR_ENTITY * entity,
    const DR_STATE * state,
    double dt
)

extrapolate a kinematic state forward by dt seconds

Parameters:

  • entity the entity (used for algorithm selection)
  • state the state to extrapolate from
  • dt time delta in seconds (can be negative for backward extrapolation)

Return: the extrapolated position

Extrapolate a kinematic state forward by dt seconds.

extrapolate a kinematic state forward by dt seconds

Uses the entity's configured algorithm (static, velocity, or velocity+acceleration).

function dr_entity_destroy

void dr_entity_destroy(
    DR_ENTITY ** entity_ptr
)

destroy a dead reckoning entity and free its memory

Parameters:

  • entity_ptr pointer to the entity pointer

Destroy a dead reckoning entity and set the pointer to NULL.

destroy a dead reckoning entity and free its memory

function dr_entity_create

DR_ENTITY * dr_entity_create(
    DR_ALGO algo,
    DR_BLEND blend_mode,
    double pos_threshold,
    double blend_time
)

create a new dead reckoning entity

Parameters:

  • algo extrapolation algorithm (DR_ALGO_STATIC, DR_ALGO_VEL, DR_ALGO_VEL_ACC)
  • blend_mode convergence mode (DR_BLEND_SNAP, DR_BLEND_PVB, DR_BLEND_CUBIC)
  • pos_threshold distance threshold for triggering network updates
  • blend_time convergence blend duration in seconds

Return: newly allocated entity, or NULL on failure

Create a new dead reckoning entity.

create a new dead reckoning entity

function dr_entity_compute

void dr_entity_compute(
    DR_ENTITY * entity,
    double time,
    DR_VEC3 * out_pos
)

compute the dead reckoned display position at a given time

Parameters:

  • entity the entity
  • time current time
  • out_pos output: the blended display position

Compute the dead reckoned display position at a given time.

compute the dead reckoned display position at a given time

If the entity is currently blending (after receiving a new state update), this returns a convergence-blended position. Otherwise, it returns the pure extrapolated position from the last known state.

function dr_entity_check_threshold

bool dr_entity_check_threshold(
    const DR_ENTITY * entity,
    const DR_VEC3 * true_pos,
    const DR_VEC3 * true_vel,
    const DR_VEC3 * true_acc,
    double time
)

check if the owner's true state has diverged beyond the threshold

Parameters:

  • entity the entity
  • true_pos the owner's true position
  • true_vel the owner's true velocity (unused, reserved for velocity threshold)
  • true_acc the owner's true acceleration (unused, reserved)
  • time current time

Return: true if threshold exceeded (send update), false if prediction is acceptable

Check whether the owner's true state has diverged from the dead reckoned prediction beyond the configured threshold.

check if the owner's true state has diverged beyond the threshold

The owner entity runs the same extrapolation algorithm as remotes. If the error exceeds the threshold, a new state update should be sent.