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

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

#include "nilorea/n_trajectory.h"
#include <math.h>
+ Include dependency graph for n_trajectory.c:

Go to the source code of this file.

Functions

static double hermite_d2h00 (double s)
 Second derivative of h00: d2h00/ds2 = 12s - 6.
 
static double hermite_d2h01 (double s)
 Second derivative of h01: d2h01/ds2 = -12s + 6.
 
static double hermite_d2h10 (double s)
 Second derivative of h10: d2h10/ds2 = 6s - 4.
 
static double hermite_d2h11 (double s)
 Second derivative of h11: d2h11/ds2 = 6s - 2.
 
static double hermite_dh00 (double s)
 First derivative of h00: dh00/ds = 6s^2 - 6s.
 
static double hermite_dh01 (double s)
 First derivative of h01: dh01/ds = -6s^2 + 6s.
 
static double hermite_dh10 (double s)
 First derivative of h10: dh10/ds = 3s^2 - 4s + 1.
 
static double hermite_dh11 (double s)
 First derivative of h11: dh11/ds = 3s^2 - 2s.
 
static double hermite_h00 (double s)
 Hermite basis function h00: weights the start position.
 
static double hermite_h01 (double s)
 Hermite basis function h01: weights the end position.
 
static double hermite_h10 (double s)
 Hermite basis function h10: weights the start tangent.
 
static double hermite_h11 (double s)
 Hermite basis function h11: weights the end tangent.
 
int trajectory_add_point (TRAJECTORY *traj, const PHYSICS *state, double time_val)
 Add a waypoint to the multi-point trajectory path.
 
int trajectory_clear_points (TRAJECTORY *traj)
 Clear all waypoints from the multi-point path.
 
int trajectory_compute (TRAJECTORY *traj, double time_val)
 Compute the full state (position, speed, acceleration, orientation, angular_speed) at a given time using cubic Hermite interpolation (within the interval) or quadratic extrapolation (outside the interval).
 
static void trajectory_compute_tangents (TRAJECTORY *traj)
 Recompute the Hermite tangent vectors from current start/end states.
 
void trajectory_delete (TRAJECTORY **traj)
 Free a TRAJECTORY and set the pointer to NULL.
 
double trajectory_distance (TRAJECTORY *traj, double time_a, double time_b, int steps)
 Compute the approximate arc length (distance traveled) along the trajectory between two times, using numerical integration with the given number of steps.
 
int trajectory_get_acceleration (TRAJECTORY *traj, double time_val, VECTOR3D out)
 Compute acceleration at a given time.
 
int trajectory_get_orientation (TRAJECTORY *traj, double time_val, VECTOR3D out)
 Compute orientation at a given time.
 
int trajectory_get_position (TRAJECTORY *traj, double time_val, VECTOR3D out)
 Compute position at a given time.
 
int trajectory_get_speed (TRAJECTORY *traj, double time_val, VECTOR3D out)
 Compute velocity at a given time.
 
static void trajectory_load_segment (TRAJECTORY *traj, double time_val)
 Load the correct spline segment for a given time when using multi-point paths.
 
TRAJECTORYtrajectory_new (int nb_components)
 Allocate and initialize a new TRAJECTORY.
 
int trajectory_set_end (TRAJECTORY *traj, const PHYSICS *state, double time_val)
 Set the terminal (end) state and time of the trajectory.
 
int trajectory_set_start (TRAJECTORY *traj, const PHYSICS *state, double time_val)
 Set the initial (start) state and time of the trajectory.
 
int trajectory_update (TRAJECTORY *traj, const PHYSICS *new_end, double time_val)
 Update the trajectory with a new end state (dead reckoning shift).
 

Detailed Description

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

Implements the "Defeating Lag With Cubic Splines" approach from GameDev.net Article 914 by Nicholas Van Caldwell, using cubic Hermite spline interpolation with quadratic extrapolation.

Cubic Hermite spline formula: P(s) = h00(s)*P0 + h10(s)*M0 + h01(s)*P1 + h11(s)*M1

Where s is normalized time in [0,1], P0/P1 are endpoint positions, M0/M1 are tangent vectors (velocity * duration), and: h00(s) = 2s^3 - 3s^2 + 1 (start position weight) h10(s) = s^3 - 2s^2 + s (start tangent weight) h01(s) = -2s^3 + 3s^2 (end position weight) h11(s) = s^3 - s^2 (end tangent weight)

Author
Castagnier Mickael
Version
1.0
Date
28/02/2026

Definition in file n_trajectory.c.

Function Documentation

◆ hermite_d2h00()

static double hermite_d2h00 ( double  s)
static

Second derivative of h00: d2h00/ds2 = 12s - 6.

Parameters
snormalized time in [0,1]
Returns
d2h00(s)

Definition at line 122 of file n_trajectory.c.

Referenced by trajectory_compute(), and trajectory_get_acceleration().

+ Here is the caller graph for this function:

◆ hermite_d2h01()

static double hermite_d2h01 ( double  s)
static

Second derivative of h01: d2h01/ds2 = -12s + 6.

Parameters
snormalized time in [0,1]
Returns
d2h01(s)

Definition at line 140 of file n_trajectory.c.

Referenced by trajectory_compute(), and trajectory_get_acceleration().

+ Here is the caller graph for this function:

◆ hermite_d2h10()

static double hermite_d2h10 ( double  s)
static

Second derivative of h10: d2h10/ds2 = 6s - 4.

Parameters
snormalized time in [0,1]
Returns
d2h10(s)

Definition at line 131 of file n_trajectory.c.

Referenced by trajectory_compute(), and trajectory_get_acceleration().

+ Here is the caller graph for this function:

◆ hermite_d2h11()

static double hermite_d2h11 ( double  s)
static

Second derivative of h11: d2h11/ds2 = 6s - 2.

Parameters
snormalized time in [0,1]
Returns
d2h11(s)

Definition at line 149 of file n_trajectory.c.

Referenced by trajectory_compute(), and trajectory_get_acceleration().

+ Here is the caller graph for this function:

◆ hermite_dh00()

static double hermite_dh00 ( double  s)
static

First derivative of h00: dh00/ds = 6s^2 - 6s.

Parameters
snormalized time in [0,1]
Returns
dh00(s)

Definition at line 86 of file n_trajectory.c.

Referenced by trajectory_compute(), and trajectory_get_speed().

+ Here is the caller graph for this function:

◆ hermite_dh01()

static double hermite_dh01 ( double  s)
static

First derivative of h01: dh01/ds = -6s^2 + 6s.

Parameters
snormalized time in [0,1]
Returns
dh01(s)

Definition at line 104 of file n_trajectory.c.

Referenced by trajectory_compute(), and trajectory_get_speed().

+ Here is the caller graph for this function:

◆ hermite_dh10()

static double hermite_dh10 ( double  s)
static

First derivative of h10: dh10/ds = 3s^2 - 4s + 1.

Parameters
snormalized time in [0,1]
Returns
dh10(s)

Definition at line 95 of file n_trajectory.c.

Referenced by trajectory_compute(), and trajectory_get_speed().

+ Here is the caller graph for this function:

◆ hermite_dh11()

static double hermite_dh11 ( double  s)
static

First derivative of h11: dh11/ds = 3s^2 - 2s.

Parameters
snormalized time in [0,1]
Returns
dh11(s)

Definition at line 113 of file n_trajectory.c.

Referenced by trajectory_compute(), and trajectory_get_speed().

+ Here is the caller graph for this function:

◆ hermite_h00()

static double hermite_h00 ( double  s)
static

Hermite basis function h00: weights the start position.

Parameters
snormalized time in [0,1]
Returns
h00(s) = 2s^3 - 3s^2 + 1

Definition at line 50 of file n_trajectory.c.

Referenced by trajectory_compute(), trajectory_get_orientation(), and trajectory_get_position().

+ Here is the caller graph for this function:

◆ hermite_h01()

static double hermite_h01 ( double  s)
static

Hermite basis function h01: weights the end position.

Parameters
snormalized time in [0,1]
Returns
h01(s) = -2s^3 + 3s^2

Definition at line 68 of file n_trajectory.c.

Referenced by trajectory_compute(), trajectory_get_orientation(), and trajectory_get_position().

+ Here is the caller graph for this function:

◆ hermite_h10()

static double hermite_h10 ( double  s)
static

Hermite basis function h10: weights the start tangent.

Parameters
snormalized time in [0,1]
Returns
h10(s) = s^3 - 2s^2 + s

Definition at line 59 of file n_trajectory.c.

Referenced by trajectory_compute(), trajectory_get_orientation(), and trajectory_get_position().

+ Here is the caller graph for this function:

◆ hermite_h11()

static double hermite_h11 ( double  s)
static

Hermite basis function h11: weights the end tangent.

Parameters
snormalized time in [0,1]
Returns
h11(s) = s^3 - s^2

Definition at line 77 of file n_trajectory.c.

Referenced by trajectory_compute(), trajectory_get_orientation(), and trajectory_get_position().

+ Here is the caller graph for this function:

◆ trajectory_compute_tangents()

static void trajectory_compute_tangents ( TRAJECTORY traj)
static

Recompute the Hermite tangent vectors from current start/end states.

Tangents are: m = velocity * duration (for position) and angular_speed * duration (for orientation)

Parameters
trajThe trajectory to update tangents for

Definition at line 158 of file n_trajectory.c.

References PHYSICS::angular_speed, TRAJECTORY::duration, TRAJECTORY::end, TRAJECTORY::m0, TRAJECTORY::m0_orient, TRAJECTORY::m1, TRAJECTORY::m1_orient, TRAJECTORY::nb_components, PHYSICS::speed, and TRAJECTORY::start.

Referenced by trajectory_load_segment(), trajectory_set_end(), trajectory_set_start(), and trajectory_update().

+ Here is the caller graph for this function:

◆ trajectory_load_segment()

static void trajectory_load_segment ( TRAJECTORY traj,
double  time_val 
)
static

Load the correct spline segment for a given time when using multi-point paths.

Searches forward/backward from current_segment for efficiency with sequential access. Updates traj->start, end, start_time, end_time, duration, and tangents for the found segment.

Parameters
trajThe trajectory (must have nb_points >= 2)
time_valThe time to find the segment for

Definition at line 174 of file n_trajectory.c.

References TRAJECTORY::current_segment, TRAJECTORY::duration, TRAJECTORY::end, TRAJECTORY::end_time, TRAJECTORY::nb_points, TRAJECTORY::points, TRAJECTORY::start, TRAJECTORY::start_time, TRAJECTORY_POINT::state, TRAJECTORY_POINT::time_val, and trajectory_compute_tangents().

Referenced by trajectory_add_point(), trajectory_compute(), trajectory_get_acceleration(), trajectory_get_orientation(), trajectory_get_position(), and trajectory_get_speed().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: