Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_dead_reckoning.h File Reference

Dead Reckoning API for latency hiding in networked games. More...

#include <stdbool.h>
#include <math.h>
+ Include dependency graph for n_dead_reckoning.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

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

Enumerations

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

Functions

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
 
void dr_entity_compute (DR_ENTITY *entity, double time, DR_VEC3 *out_pos)
 compute the dead reckoned display position at a given time
 
DR_ENTITYdr_entity_create (DR_ALGO algo, DR_BLEND blend_mode, double pos_threshold, double blend_time)
 create a new dead reckoning entity
 
void dr_entity_destroy (DR_ENTITY **entity_ptr)
 destroy a dead reckoning entity and free its memory
 
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_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
 
void dr_entity_set_algo (DR_ENTITY *entity, DR_ALGO algo)
 set the extrapolation algorithm
 
void dr_entity_set_blend_mode (DR_ENTITY *entity, DR_BLEND blend_mode)
 set the convergence blending mode
 
void dr_entity_set_blend_time (DR_ENTITY *entity, double blend_time)
 set the convergence blend duration
 
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_threshold (DR_ENTITY *entity, double threshold)
 set the position error threshold
 
static DR_VEC3 dr_vec3 (double x, double y, double z)
 Create a DR_VEC3 from components.
 
static DR_VEC3 dr_vec3_add (DR_VEC3 a, DR_VEC3 b)
 Vector addition: a + b.
 
static double dr_vec3_distance (DR_VEC3 a, DR_VEC3 b)
 Distance between two points.
 
static double dr_vec3_length (DR_VEC3 v)
 Length of vector.
 
static double dr_vec3_length_sq (DR_VEC3 v)
 Squared length of vector.
 
static DR_VEC3 dr_vec3_lerp (DR_VEC3 a, DR_VEC3 b, double t)
 Linear interpolation: a + (b - a) * t.
 
static DR_VEC3 dr_vec3_scale (DR_VEC3 v, double s)
 Scalar multiplication: v * s.
 
static DR_VEC3 dr_vec3_sub (DR_VEC3 a, DR_VEC3 b)
 Vector subtraction: a - b.
 
static DR_VEC3 dr_vec3_zero (void)
 Zero vector.
 

Detailed Description

Dead Reckoning API for latency hiding in networked games.

Implements dead reckoning (DR) as described in:

  • "Dead Reckoning: Latency Hiding for Networked Games" (Game Developer)
  • "Believable Dead Reckoning for Networked Games" by Curtiss Murphy (Game Engine Gems 2, 2011)

Dead reckoning is a technique where every participant in a networked game simulates all entities using agreed-upon extrapolation algorithms. When the owner's true state diverges from the extrapolated state beyond a configurable threshold, a new state update is sent. On receipt, the remote side uses a convergence algorithm to smoothly blend from the predicted position to the newly received authoritative state.

Can be used together with n_trajectory for cubic Hermite spline interpolation between known states, while dead reckoning provides the network-level threshold checking and convergence blending.

Supports three extrapolation models:

  • DR_ALGO_STATIC: entity stays at last known position
  • DR_ALGO_VEL: P(t) = P0 + V0 * t
  • DR_ALGO_VEL_ACC: P(t) = P0 + V0 * t + 0.5 * A0 * t^2

Supports three convergence/blending modes:

  • DR_BLEND_SNAP: immediately snap to new state (no smoothing)
  • DR_BLEND_PVB: Projective Velocity Blending (recommended)
  • DR_BLEND_CUBIC: Cubic Bezier spline convergence

Usage:

// Create entity with default settings (VEL_ACC extrapolation, PVB blending)
// When an authoritative state update arrives from the network:
DR_VEC3 pos = {10.0, 5.0, 0.0};
DR_VEC3 vel = {2.0, 1.0, 0.0};
DR_VEC3 acc = {0.0, 0.0, 0.0};
dr_entity_receive_state(ent, &pos, &vel, &acc, current_time);
// Each frame, compute the blended display position:
DR_VEC3 display_pos;
dr_entity_compute(ent, current_time, &display_pos);
// For the owning player: check if a network update should be sent:
DR_VEC3 true_pos = { ... };
DR_VEC3 true_vel = { ... };
DR_VEC3 true_acc = { ... };
if (dr_entity_check_threshold(ent, &true_pos, &true_vel, &true_acc, current_time)) {
// send state update to other players
dr_entity_receive_state(ent, &true_pos, &true_vel, &true_acc, current_time);
}
void dr_entity_destroy(DR_ENTITY **entity_ptr)
Destroy a dead reckoning entity and set the pointer to NULL.
void dr_entity_compute(DR_ENTITY *entity, double time, DR_VEC3 *out_pos)
Compute the dead reckoned display position at a given time.
DR_ENTITY * dr_entity_create(DR_ALGO algo, DR_BLEND blend_mode, double pos_threshold, double blend_time)
Create a new dead reckoning entity.
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 whether the owner's true state has diverged from the dead reckoned prediction beyond the config...
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_BLEND_PVB
Projective Velocity Blending (recommended)
@ DR_ALGO_VEL_ACC
Velocity + acceleration: P(t) = P0 + V0*t + 0.5*A0*t^2.
Dead reckoned entity with extrapolation and convergence state.
3D vector used for position, velocity, and acceleration
Author
Castagnier Mickael
Version
1.0
Date
01/03/2026

Definition in file n_dead_reckoning.h.