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

Trajectory interpolation and dead reckoning for 2D/3D networked simulations. More...

#include "n_3d.h"
+ Include dependency graph for n_trajectory.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  TRAJECTORY
 structure holding all data for trajectory interpolation / extrapolation More...
 
struct  TRAJECTORY_POINT
 a single waypoint in a multi-point trajectory path More...
 

Macros

#define TRAJECTORY_2D   2
 use 2 components (x,y) for trajectory computation
 
#define TRAJECTORY_3D   3
 use 3 components (x,y,z) for trajectory computation
 
#define TRAJECTORY_BEFORE   3
 trajectory is extrapolating before the start state using quadratic motion
 
#define TRAJECTORY_EXTRAP   2
 trajectory is extrapolating beyond the end state using quadratic motion
 
#define TRAJECTORY_INTERP   1
 trajectory is interpolating along the cubic Hermite spline between start and end
 

Functions

int trajectory_add_point (TRAJECTORY *traj, const PHYSICS *state, double time_val)
 add a waypoint to the multi-point path (times must be strictly increasing)
 
int trajectory_clear_points (TRAJECTORY *traj)
 clear all waypoints from the multi-point path
 
int trajectory_compute (TRAJECTORY *traj, double time_val)
 compute full state (position, speed, acceleration, orientation) at given time
 
void trajectory_delete (TRAJECTORY **traj)
 free a trajectory
 
double trajectory_distance (TRAJECTORY *traj, double time_a, double time_b, int steps)
 distance along the trajectory between two times
 
int trajectory_get_acceleration (TRAJECTORY *traj, double time_val, VECTOR3D out)
 compute only acceleration at given time, stores result in out
 
int trajectory_get_orientation (TRAJECTORY *traj, double time_val, VECTOR3D out)
 compute only orientation at given time, stores result in out
 
int trajectory_get_position (TRAJECTORY *traj, double time_val, VECTOR3D out)
 compute only position at given time, stores result in out
 
int trajectory_get_speed (TRAJECTORY *traj, double time_val, VECTOR3D out)
 compute only speed at given time, stores result in out
 
TRAJECTORYtrajectory_new (int nb_components)
 allocate a new trajectory for 2D or 3D use
 
int trajectory_set_end (TRAJECTORY *traj, const PHYSICS *state, double time_val)
 set the terminal state and time of the trajectory, recomputes Hermite tangents
 
int trajectory_set_start (TRAJECTORY *traj, const PHYSICS *state, double time_val)
 set the initial state and time of the trajectory
 
int trajectory_update (TRAJECTORY *traj, const PHYSICS *new_end, double time_val)
 shift: old end becomes new start, set a new end state (for continuous dead reckoning updates)
 

Detailed Description

Trajectory interpolation and dead reckoning for 2D/3D networked simulations.

Implementation of the "Defeating Lag With Cubic Splines" approach (GameDev.net Article 914 by Nicholas Van Caldwell) combined with cubic Hermite spline interpolation and quadratic extrapolation.

The API provides smooth movement prediction for networked games and local simulations by:

  • Using cubic Hermite spline interpolation between two known states (matching position and velocity at both endpoints)
  • Quadratic extrapolation beyond the known interval using velocity and acceleration
  • Smooth orientation interpolation using the same Hermite approach

Works for both 2D (xy) and 3D (xyz) coordinate systems.

Time units are user-defined: as long as position, speed, acceleration, and time values use consistent units, the math works. For example:

  • position in pixels, speed in pixels/sec, accel in pixels/sec^2, time in sec
  • position in meters, speed in m/usec, accel in m/usec^2, time in usec
Author
Castagnier Mickael
Version
1.0
Date
28/02/2026

Definition in file n_trajectory.h.