6 n__trajectory_8h
gullradriel edited this page 2026-10-09 00:55:18 +02:00

include/nilorea/n_trajectory.h

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

Structures

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

Detailed Description

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

Author: Castagnier Mickael

Version: 1.0

Date: 28/02/2026

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

Source code

/*
 * Nilorea Library
 * Copyright (C) 2005-2026 Castagnier Mickael
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 */


#ifndef _N_TRAJECTORY_HEADER
#define _N_TRAJECTORY_HEADER

#ifdef __cplusplus
extern "C" {
#endif


#include "n_3d.h"

#define TRAJECTORY_INTERP 1
#define TRAJECTORY_EXTRAP 2
#define TRAJECTORY_BEFORE 3

#define TRAJECTORY_2D 2
#define TRAJECTORY_3D 3

typedef struct TRAJECTORY_POINT {
    PHYSICS state;
    double time_val;
} TRAJECTORY_POINT;

typedef struct TRAJECTORY {
    PHYSICS start;
    PHYSICS end;
    PHYSICS current;

    double start_time;
    double end_time;
    double duration;
    double current_time;

    VECTOR3D m0;
    VECTOR3D m1;
    VECTOR3D m0_orient;
    VECTOR3D m1_orient;

    TRAJECTORY_POINT* points;
    int nb_points;
    int nb_points_allocated;
    int current_segment;

    int nb_components;
    int mode;
} TRAJECTORY;

TRAJECTORY* trajectory_new(int nb_components);
void trajectory_delete(TRAJECTORY** traj);

int trajectory_set_start(TRAJECTORY* traj, const PHYSICS* state, double time_val);
int trajectory_set_end(TRAJECTORY* traj, const PHYSICS* state, double time_val);
int trajectory_update(TRAJECTORY* traj, const PHYSICS* new_end, double time_val);

int trajectory_compute(TRAJECTORY* traj, double time_val);
int trajectory_get_position(TRAJECTORY* traj, double time_val, VECTOR3D out);
int trajectory_get_speed(TRAJECTORY* traj, double time_val, VECTOR3D out);
int trajectory_get_acceleration(TRAJECTORY* traj, double time_val, VECTOR3D out);
int trajectory_get_orientation(TRAJECTORY* traj, double time_val, VECTOR3D out);

int trajectory_add_point(TRAJECTORY* traj, const PHYSICS* state, double time_val);
int trajectory_clear_points(TRAJECTORY* traj);

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


#ifdef __cplusplus
}
#endif

#endif  // header guard