Nilorea Library trajectory API graphical demo (Allegro5) Split-screen demonstration of the cubic Hermite spline trajectory API with multi-waypoint paths:
#define ALLEGRO_UNSTABLE 1
#define WIDTH 800
#define HEIGHT 600
#define PANEL_W 400
#define FPS 60.0
#define DT (1.0 / FPS)
#define MAX_TRAIL 300
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <allegro5/allegro.h>
#include <allegro5/allegro_primitives.h>
#include <allegro5/allegro_font.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#define MAX_WP2D 50
typedef struct {
double x, y, z, vx, vy, vz;
#define NUM_WP3D 7
{0.0, 0.0, 0.0, 2.0, 1.0, 0.5},
{3.0, 2.0, 1.0, 1.0, 1.5, -0.5},
{4.0, 4.0, -1.0, -1.0, 1.0, -1.0},
{1.0, 5.0, -2.0, -2.0, 0.0, 0.5},
{-2.0, 3.0, 0.0, -1.0, -1.5, 1.0},
{-1.0, 1.0, 1.5, 1.0, -1.0, 0.0},
{0.0, 0.0, 0.0, 2.0, 1.0, 0.5},
};
}
}
}
}
static void project_3d(
double x,
double y,
double z,
float* sx,
float* sy) {
double scale = 30.0;
double cy = (double)
HEIGHT / 2.0;
double angle =
M_PI / 4.0;
*sx = (float)(cx + x * scale + z * 0.5 * cos(angle) * scale);
*sy = (float)(cy - y * scale - z * 0.5 * sin(angle) * scale);
}
static void draw_arrow(
float x1,
float y1,
float x2,
float y2, ALLEGRO_COLOR color,
float thickness) {
al_draw_line(x1, y1, x2, y2, color, thickness);
float dx = x2 - x1, dy = y2 - y1;
float len = sqrtf(dx * dx + dy * dy);
if (len < 2.0f) return;
float ux = dx / len, uy = dy / len;
float px = -uy * 4.0f, py = ux * 4.0f;
float bx = x2 - ux * 8.0f, by = y2 - uy * 8.0f;
al_draw_filled_triangle(x2, y2, bx + px, by + py,
bx - px, by - py, color);
}
static void draw_trail(
float trail[][2],
int count,
int head, ALLEGRO_COLOR base_color) {
if (count < 2) return;
unsigned char r, g, b;
al_unmap_rgb(base_color, &r, &g, &b);
for (int i = 1; i < count; i++) {
float alpha = (float)i / (float)count;
ALLEGRO_COLOR c = al_map_rgba(
(unsigned char)(r * alpha),
(unsigned char)(g * alpha),
(unsigned char)(b * alpha),
(unsigned char)(alpha * 200));
al_draw_line(trail[i0][0], trail[i0][1],
trail[i1][0], trail[i1][1], c, 2);
}
}
double vx, vy;
if (i == 0) {
} else {
}
}
}
}
return;
}
double dist = sqrt(dx * dx + dy * dy);
if (dist < 10.0) return;
if (dur < 0.3) dur = 0.3;
}
float px = (float)pos[0], py = (float)pos[1];
for (int i = 1; i <= total_steps; i++) {
double t = t0 + (t1 - t0) * (double)i / (double)total_steps;
float nx = (float)pos[0], ny = (float)pos[1];
al_draw_line(px, py, nx, ny,
al_map_rgba(0, 200, 255, 180), 2);
px = nx;
py = ny;
}
double ext = last_seg_dur * 0.5;
if (ext < 0.3) ext = 0.3;
for (int i = 1; i <= 20; i++) {
double t = t1 + ext * (double)i / 20.0;
float nx = (float)pos[0], ny = (float)pos[1];
if (i % 2 == 0)
al_draw_line(px, py, nx, ny,
al_map_rgba(255, 255, 0, 120), 1);
px = nx;
py = ny;
}
}
al_map_rgb(15, 15, 30));
ALLEGRO_COLOR grid = al_map_rgba(35, 35, 55, 255);
for (
int x = 0; x <
PANEL_W; x += 40)
al_draw_line((
float)x, 0, (
float)x,
HEIGHT, grid, 1);
for (
int y = 0; y <
HEIGHT; y += 40)
al_draw_line(0, (
float)y,
PANEL_W, (
float)y, grid, 1);
al_draw_text(font, al_map_rgb(220, 220, 220), 5, 5, 0,
"2D Multi-Waypoint Path");
ALLEGRO_COLOR wc;
if (i == 0)
wc = al_map_rgb(0, 200, 0);
wc = al_map_rgb(200, 0, 0);
else
wc = al_map_rgb(100, 150, 255);
al_draw_filled_circle(wx, wy, 5, wc);
al_draw_circle(wx, wy, 5, al_map_rgb(255, 255, 255), 1);
char num[16];
snprintf(num, sizeof(num), "%d", i);
al_draw_text(font, al_map_rgb(200, 200, 200),
wx + 7, wy - 6, 0, num);
}
al_map_rgb(100, 180, 255));
al_draw_filled_circle(ox, oy, 8,
al_map_rgb(255, 180, 0));
al_draw_circle(ox, oy, 8,
al_map_rgb(255, 255, 255), 2);
float vs = 0.15f;
al_map_rgb(255, 255, 100), 1);
char buf[128];
const char*
mode =
"???";
snprintf(buf, sizeof(buf), "t=%.2f seg=%d/%d [%s]",
al_draw_text(font, al_map_rgb(200, 200, 200), 5, 18, 0, buf);
snprintf(buf, sizeof(buf), "pos=(%.0f,%.0f) vel=(%.0f,%.0f) wp=%d",
al_draw_text(font, al_map_rgb(200, 200, 200), 5, 34, 0, buf);
} else {
? "Click to place waypoints"
: "Click to add more waypoints";
al_draw_text(font, al_map_rgb(150, 150, 150),
ALLEGRO_ALIGN_CENTER, msg);
}
al_map_rgba(0, 200, 255, 180), 2);
al_draw_text(font, al_map_rgb(160, 160, 160),
30,
HEIGHT - 66, 0,
"Interpolation");
al_map_rgba(255, 255, 0, 120), 1);
al_map_rgba(255, 255, 0, 120), 1);
al_draw_text(font, al_map_rgb(160, 160, 160),
30,
HEIGHT - 52, 0,
"Extrapolation");
al_draw_text(font, al_map_rgb(90, 90, 90), 5,
HEIGHT - 15, 0,
"[R] Reset [ESC] Quit");
}
double t = 0.0;
}
}
}
float sx, sy;
}
float ox, oy, ax, ay;
draw_arrow(ox, oy, ax, ay, al_map_rgb(200, 60, 60), 2);
al_draw_text(font, al_map_rgb(200, 60, 60),
ax + 4, ay - 6, 0, "X");
draw_arrow(ox, oy, ax, ay, al_map_rgb(60, 200, 60), 2);
al_draw_text(font, al_map_rgb(60, 200, 60),
ax + 4, ay - 6, 0, "Y");
draw_arrow(ox, oy, ax, ay, al_map_rgb(60, 60, 200), 2);
al_draw_text(font, al_map_rgb(60, 60, 200),
ax + 4, ay - 6, 0, "Z");
}
ALLEGRO_COLOR gc = al_map_rgba(45, 45, 45, 128);
for (int i = -4; i <= 4; i++) {
float x1, y1, x2, y2;
al_draw_line(x1, y1, x2, y2, gc, 1);
al_draw_line(x1, y1, x2, y2, gc, 1);
}
}
float px, py;
for (int i = 1; i <= total_steps; i++) {
double t = t0 + (t1 - t0) * (double)i / (double)total_steps;
float nx, ny;
al_draw_line(px, py, nx, ny,
al_map_rgba(255, 100, 255, 140), 2);
px = nx;
py = ny;
}
}
al_map_rgb(18, 18, 22));
al_draw_text(font, al_map_rgb(220, 220, 220),
PANEL_W + 5, 5, 0,
"3D Multi-Waypoint Loop");
float sx, sy;
? al_map_rgb(255, 80, 80)
: al_map_rgb(100, 100, 200);
al_draw_filled_circle(sx, sy, 4, c);
al_draw_circle(sx, sy, 4, al_map_rgb(200, 200, 200), 1);
char num[16];
snprintf(num, sizeof(num), "%d", i);
al_draw_text(font, al_map_rgb(180, 180, 180),
sx + 6, sy - 6, 0, num);
}
al_map_rgb(200, 120, 255));
float obj_sx, obj_sy;
&obj_sx, &obj_sy);
float gnd_sx, gnd_sy;
&gnd_sx, &gnd_sy);
al_draw_line(gnd_sx, gnd_sy, obj_sx, obj_sy,
al_map_rgba(100, 100, 100, 80), 1);
al_draw_filled_circle(gnd_sx, gnd_sy, 2,
al_map_rgba(100, 100, 100, 60));
al_draw_filled_circle(obj_sx, obj_sy, 7,
al_map_rgb(255, 200, 50));
al_draw_circle(obj_sx, obj_sy, 7,
al_map_rgb(255, 255, 255), 2);
double vs3 = 0.4;
float vel_sx, vel_sy;
&vel_sx, &vel_sy);
al_map_rgb(255, 255, 100), 1);
char buf[128];
snprintf(buf, sizeof(buf), "t=%.2f seg=%d/%d wp=%d",
al_draw_text(font, al_map_rgb(200, 200, 200),
snprintf(buf, sizeof(buf), "pos=(%.1f, %.1f, %.1f)",
al_draw_text(font, al_map_rgb(200, 200, 200),
snprintf(buf, sizeof(buf), "vel=(%.1f, %.1f, %.1f)",
al_draw_text(font, al_map_rgb(200, 200, 200),
}
int main(
int argc,
char* argv[]) {
(void)argc;
if (!al_init()) {
n_abort(
"Could not init Allegro.\n");
}
if (!al_init_primitives_addon()) {
n_abort(
"Unable to initialize primitives addon\n");
}
if (!al_init_font_addon()) {
n_abort(
"Unable to initialize font addon\n");
}
if (!al_install_keyboard()) {
n_abort(
"Unable to initialize keyboard handler\n");
}
if (!al_install_mouse()) {
n_abort(
"Unable to initialize mouse handler\n");
}
al_set_new_display_flags(ALLEGRO_OPENGL | ALLEGRO_WINDOWED);
n_abort(
"Unable to create display\n");
}
"Nilorea Trajectory API - Multi-Waypoint Demo");
ALLEGRO_FONT* font = al_create_builtin_font();
ALLEGRO_TIMER* timer = al_create_timer(1.0 /
FPS);
ALLEGRO_EVENT_QUEUE* event_queue = al_create_event_queue();
al_register_event_source(event_queue,
al_get_display_event_source(
display));
al_register_event_source(event_queue,
al_get_timer_event_source(timer));
al_register_event_source(event_queue,
al_get_keyboard_event_source());
al_register_event_source(event_queue,
al_get_mouse_event_source());
ALLEGRO_BITMAP* scrbuf = al_create_bitmap(
WIDTH,
HEIGHT);
al_start_timer(timer);
int do_draw = 0;
ALLEGRO_EVENT ev;
al_wait_for_event(event_queue, &ev);
if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
} else if (ev.type == ALLEGRO_EVENT_KEY_DOWN) {
if (ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE) {
} else if (ev.keyboard.keycode == ALLEGRO_KEY_R) {
}
} else if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) {
if (ev.mouse.button == 1) {
(float)ev.mouse.y);
}
} else if (ev.type == ALLEGRO_EVENT_DISPLAY_SWITCH_IN ||
ev.type == ALLEGRO_EVENT_DISPLAY_SWITCH_OUT) {
al_flush_event_queue(event_queue);
} else if (ev.type == ALLEGRO_EVENT_TIMER) {
}
do_draw = 1;
}
if (do_draw && al_is_event_queue_empty(event_queue)) {
al_set_target_bitmap(scrbuf);
al_clear_to_color(al_map_rgb(0, 0, 0));
al_map_rgb(80, 80, 80), 2);
al_set_target_bitmap(al_get_backbuffer(
display));
al_draw_bitmap(scrbuf, 0, 0, 0);
al_flip_display();
do_draw = 0;
}
}
al_destroy_bitmap(scrbuf);
al_destroy_font(font);
al_destroy_timer(timer);
al_destroy_event_queue(event_queue);
return 0;
}
ALLEGRO_DISPLAY * display
static void draw_3d_axes(ALLEGRO_FONT *font)
static TRAJECTORY * traj_3d
static void reset_2d(void)
static void rebuild_2d_trajectory(void)
static void trail_push_2d(float x, float y)
static void trail_push_3d(float x, float y)
static double time_3d_end
static void draw_full_path_3d(void)
static void init_physics_3d(PHYSICS *p, double px, double py, double pz, double vx, double vy, double vz)
static void draw_3d_panel(ALLEGRO_FONT *font)
static void draw_full_path_2d(void)
static void handle_click_2d(float mx, float my)
static void init_physics_2d(PHYSICS *p, double px, double py, double vx, double vy)
static void project_3d(double x, double y, double z, float *sx, float *sy)
static void init_3d(void)
static const WAYPOINT3D waypoints_3d[7]
static int trail_3d_count
static int trail_2d_count
static void draw_2d_panel(ALLEGRO_FONT *font)
static const double WP3D_DUR
static float trail_2d[300][2]
static void update_3d_logic(void)
static void draw_ground_grid(void)
static void draw_arrow(float x1, float y1, float x2, float y2, ALLEGRO_COLOR color, float thickness)
static float trail_3d[300][2]
static void draw_trail(float trail[][2], int count, int head, ALLEGRO_COLOR base_color)
static TRAJECTORY * traj_2d
static const double WP2D_SPEED
void n_abort(char const *format,...)
abort program with a text
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_ERR
error conditions
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
#define LOG_NOTICE
normal but significant condition
VECTOR3D speed
vx,vy,vz actual speed
VECTOR3D position
x,y,z actual position
#define VECTOR3D_SET(VECTOR, X, Y, Z)
helper to set a VECTOR3D position
double VECTOR3D[3]
struct of a point
structure of the physics of an object
PHYSICS current
current computed state at current_time
int nb_points
number of waypoints in the points array
double time_val
timestamp of this waypoint
TRAJECTORY_POINT * points
array of waypoints for multi-point paths (NULL if single segment)
int current_segment
index of the currently loaded segment (points[i] -> points[i+1]), -1 if none
int mode
current computation mode: TRAJECTORY_INTERP, TRAJECTORY_EXTRAP, or TRAJECTORY_BEFORE
int trajectory_get_position(TRAJECTORY *traj, double time_val, VECTOR3D out)
Compute position at a given time.
TRAJECTORY * trajectory_new(int nb_components)
Allocate and initialize a new TRAJECTORY.
int trajectory_add_point(TRAJECTORY *traj, const PHYSICS *state, double time_val)
Add a waypoint to the multi-point trajectory path.
#define TRAJECTORY_EXTRAP
trajectory is extrapolating beyond the end state using quadratic motion
void trajectory_delete(TRAJECTORY **traj)
Free a TRAJECTORY and set the pointer to NULL.
#define TRAJECTORY_3D
use 3 components (x,y,z) for trajectory computation
#define TRAJECTORY_BEFORE
trajectory is extrapolating before the start state using quadratic motion
#define TRAJECTORY_2D
use 2 components (x,y) for trajectory computation
#define TRAJECTORY_INTERP
trajectory is interpolating along the cubic Hermite spline between start and end
int trajectory_compute(TRAJECTORY *traj, double time_val)
Compute the full state (position, speed, acceleration, orientation, angular_speed) at a given time us...
int trajectory_clear_points(TRAJECTORY *traj)
Clear all waypoints from the multi-point path.
structure holding all data for trajectory interpolation / extrapolation
Common headers and low-level functions & define.
Trajectory interpolation and dead reckoning for 2D/3D networked simulations.