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

TRAJECTORY: cubic Hermite spline dead reckoning and movement simulation for 2D/3D

Structures

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

Functions

Name
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)
int trajectory_set_start(TRAJECTORY * traj, const PHYSICS * state, double time_val)
set the initial state and time of the trajectory
int trajectory_set_end(TRAJECTORY * traj, const PHYSICS * state, double time_val)
set the terminal state and time of the trajectory, recomputes Hermite tangents
TRAJECTORY * trajectory_new(int nb_components)
allocate a new trajectory for 2D or 3D use
int trajectory_get_speed(TRAJECTORY * traj, double time_val, VECTOR3D out)
compute only speed 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_orientation(TRAJECTORY * traj, double time_val, VECTOR3D out)
compute only orientation at given time, stores result in out
int trajectory_get_acceleration(TRAJECTORY * traj, double time_val, VECTOR3D out)
compute only acceleration at given time, stores result in out
double trajectory_distance(TRAJECTORY * traj, double time_a, double time_b, int steps)
distance along the trajectory between two times
void trajectory_delete(TRAJECTORY ** traj)
free a trajectory
int trajectory_compute(TRAJECTORY * traj, double time_val)
compute full state (position, speed, acceleration, orientation) at given time
int trajectory_clear_points(TRAJECTORY * traj)
clear all waypoints from the multi-point path
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)

Defines

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

Functions Documentation

function trajectory_update

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)

Parameters:

  • traj The trajectory to update
  • new_end The new physics state received (e.g. from a network packet)
  • time_val The timestamp of the new state

Return: TRUE or FALSE

Update the trajectory with a new end state (dead reckoning shift).

shift: old end becomes new start, set a new end state (for continuous dead reckoning updates)

The previous end state becomes the new start state, and the new state becomes the end. This is the core function for continuous dead reckoning: call it each time a new network update arrives to create a smooth spline from the current endpoint to the new one.

function trajectory_set_start

int trajectory_set_start(
    TRAJECTORY * traj,
    const PHYSICS * state,
    double time_val
)

set the initial state and time of the trajectory

Parameters:

  • traj The trajectory to configure
  • state The physics state at the start point
  • time_val The timestamp of this state

Return: TRUE or FALSE

Set the initial (start) state and time of the trajectory.

set the initial state and time of the trajectory

function trajectory_set_end

int trajectory_set_end(
    TRAJECTORY * traj,
    const PHYSICS * state,
    double time_val
)

set the terminal state and time of the trajectory, recomputes Hermite tangents

Parameters:

  • traj The trajectory to configure
  • state The physics state at the end point
  • time_val The timestamp of this state (must be > start_time)

Return: TRUE or FALSE

Set the terminal (end) state and time of the trajectory.

set the terminal state and time of the trajectory, recomputes Hermite tangents

Recomputes the Hermite tangent vectors.

function trajectory_new

TRAJECTORY * trajectory_new(
    int nb_components
)

allocate a new trajectory for 2D or 3D use

Parameters:

  • nb_components TRAJECTORY_2D (2) or TRAJECTORY_3D (3)

Return: A new TRAJECTORY or NULL on error

Allocate and initialize a new TRAJECTORY.

allocate a new trajectory for 2D or 3D use

function trajectory_get_speed

int trajectory_get_speed(
    TRAJECTORY * traj,
    double time_val,
    VECTOR3D out
)

compute only speed at given time, stores result in out

Parameters:

  • traj The trajectory
  • time_val The time at which to compute
  • out VECTOR3D to store the result

Return: TRUE or FALSE

Compute velocity at a given time.

compute only speed at given time, stores result in out

function trajectory_get_position

int trajectory_get_position(
    TRAJECTORY * traj,
    double time_val,
    VECTOR3D out
)

compute only position at given time, stores result in out

Parameters:

  • traj The trajectory
  • time_val The time at which to compute
  • out VECTOR3D to store the result

Return: TRUE or FALSE

Compute position at a given time.

compute only position at given time, stores result in out

This is a lightweight version of trajectory_compute that only computes position.

function trajectory_get_orientation

int trajectory_get_orientation(
    TRAJECTORY * traj,
    double time_val,
    VECTOR3D out
)

compute only orientation at given time, stores result in out

Parameters:

  • traj The trajectory
  • time_val The time at which to compute
  • out VECTOR3D to store the result

Return: TRUE or FALSE

Compute orientation at a given time.

compute only orientation at given time, stores result in out

function trajectory_get_acceleration

int trajectory_get_acceleration(
    TRAJECTORY * traj,
    double time_val,
    VECTOR3D out
)

compute only acceleration at given time, stores result in out

Parameters:

  • traj The trajectory
  • time_val The time at which to compute
  • out VECTOR3D to store the result

Return: TRUE or FALSE

Compute acceleration at a given time.

compute only acceleration at given time, stores result in out

function trajectory_distance

double trajectory_distance(
    TRAJECTORY * traj,
    double time_a,
    double time_b,
    int steps
)

distance along the trajectory between two times

Parameters:

  • traj The trajectory
  • time_a Start time
  • time_b End time
  • steps Number of integration steps (higher = more precise, 100 is usually enough)

Return: The approximate distance, or -1.0 on error

Compute the approximate arc length (distance traveled) along the trajectory between two times, using numerical integration with the given number of steps.

distance along the trajectory between two times

function trajectory_delete

void trajectory_delete(
    TRAJECTORY ** traj
)

free a trajectory

Parameters:

Free a TRAJECTORY and set the pointer to NULL.

free a trajectory

function trajectory_compute

int trajectory_compute(
    TRAJECTORY * traj,
    double time_val
)

compute full state (position, speed, acceleration, orientation) at given time

Parameters:

  • traj The trajectory
  • time_val The time at which to compute the state

Return: TRUE or FALSE

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).

compute full state (position, speed, acceleration, orientation) at given time

function trajectory_clear_points

int trajectory_clear_points(
    TRAJECTORY * traj
)

clear all waypoints from the multi-point path

Parameters:

  • traj The trajectory

Return: TRUE or FALSE

Clear all waypoints from the multi-point path.

clear all waypoints from the multi-point path

Keeps the allocated memory for reuse. Resets start/end/duration.

function trajectory_add_point

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)

Parameters:

  • traj The trajectory
  • state The physics state at this waypoint
  • time_val The timestamp of this waypoint (must be > previous point's time)

Return: TRUE or FALSE

Add a waypoint to the multi-point trajectory path.

add a waypoint to the multi-point path (times must be strictly increasing)

Waypoints must be added in strictly increasing chronological order. When 2+ waypoints exist, trajectory_compute and trajectory_get_* functions automatically find and interpolate the correct segment.

Macros Documentation

define TRAJECTORY_INTERP

#define TRAJECTORY_INTERP 1

trajectory is interpolating along the cubic Hermite spline between start and end

define TRAJECTORY_EXTRAP

#define TRAJECTORY_EXTRAP 2

trajectory is extrapolating beyond the end state using quadratic motion

define TRAJECTORY_BEFORE

#define TRAJECTORY_BEFORE 3

trajectory is extrapolating before the start state using quadratic motion

define TRAJECTORY_3D

#define TRAJECTORY_3D 3

use 3 components (x,y,z) for trajectory computation

define TRAJECTORY_2D

#define TRAJECTORY_2D 2

use 2 components (x,y) for trajectory computation