41#define ALLEGRO_UNSTABLE 1
58#include <allegro5/allegro.h>
59#include <allegro5/allegro_primitives.h>
60#include <allegro5/allegro_font.h>
63#define M_PI 3.14159265358979323846
92 double x, y, z,
vx, vy, vz;
97 {0.0, 0.0, 0.0, 2.0, 1.0, 0.5},
98 {3.0, 2.0, 1.0, 1.0, 1.5, -0.5},
99 {4.0, 4.0, -1.0, -1.0, 1.0, -1.0},
100 {1.0, 5.0, -2.0, -2.0, 0.0, 0.5},
101 {-2.0, 3.0, 0.0, -1.0, -1.5, 1.0},
102 {-1.0, 1.0, 1.5, 1.0, -1.0, 0.0},
103 {0.0, 0.0, 0.0, 2.0, 1.0, 0.5},
137static void project_3d(
double x,
double y,
double z,
float* sx,
float* sy) {
140 double cy = (double)
HEIGHT / 2.0;
141 double angle =
M_PI / 4.0;
142 *sx = (float)(cx + x * scale + z * 0.5 * cos(angle) * scale);
143 *sy = (float)(cy - y * scale - z * 0.5 * sin(angle) * scale);
148static void draw_arrow(
float x1,
float y1,
float x2,
float y2, ALLEGRO_COLOR color,
float thickness) {
149 al_draw_line(x1, y1, x2, y2, color, thickness);
150 float dx = x2 - x1, dy = y2 - y1;
151 float len = sqrtf(dx * dx + dy * dy);
152 if (len < 2.0f)
return;
153 float ux = dx / len, uy = dy / len;
154 float px = -uy * 4.0f, py = ux * 4.0f;
155 float bx = x2 - ux * 8.0f, by = y2 - uy * 8.0f;
156 al_draw_filled_triangle(x2, y2, bx + px, by + py,
157 bx - px, by - py, color);
160static void draw_trail(
float trail[][2],
int count,
int head, ALLEGRO_COLOR base_color) {
161 if (count < 2)
return;
162 unsigned char r, g, b;
163 al_unmap_rgb(base_color, &r, &g, &b);
164 for (
int i = 1; i < count; i++) {
167 float alpha = (float)i / (
float)count;
168 ALLEGRO_COLOR c = al_map_rgba(
169 (
unsigned char)(r * alpha),
170 (
unsigned char)(g * alpha),
171 (
unsigned char)(b * alpha),
172 (
unsigned char)(alpha * 200));
173 al_draw_line(trail[i0][0], trail[i0][1],
174 trail[i1][0], trail[i1][1], c, 2);
236 double dist = sqrt(dx * dx + dy * dy);
237 if (dist < 10.0)
return;
240 if (dur < 0.3) dur = 0.3;
259 float px = (float)pos[0], py = (
float)pos[1];
261 for (
int i = 1; i <= total_steps; i++) {
262 double t = t0 + (t1 - t0) * (
double)i / (double)total_steps;
264 float nx = (float)pos[0], ny = (
float)pos[1];
265 al_draw_line(px, py, nx, ny,
266 al_map_rgba(0, 200, 255, 180), 2);
273 double ext = last_seg_dur * 0.5;
274 if (ext < 0.3) ext = 0.3;
275 for (
int i = 1; i <= 20; i++) {
276 double t = t1 + ext * (double)i / 20.0;
278 float nx = (float)pos[0], ny = (
float)pos[1];
280 al_draw_line(px, py, nx, ny,
281 al_map_rgba(255, 255, 0, 120), 1);
289 al_map_rgb(15, 15, 30));
291 ALLEGRO_COLOR grid = al_map_rgba(35, 35, 55, 255);
292 for (
int x = 0; x <
PANEL_W; x += 40)
293 al_draw_line((
float)x, 0, (
float)x,
HEIGHT, grid, 1);
294 for (
int y = 0; y <
HEIGHT; y += 40)
295 al_draw_line(0, (
float)y,
PANEL_W, (
float)y, grid, 1);
297 al_draw_text(font, al_map_rgb(220, 220, 220), 5, 5, 0,
298 "2D Multi-Waypoint Path");
308 wc = al_map_rgb(0, 200, 0);
310 wc = al_map_rgb(200, 0, 0);
312 wc = al_map_rgb(100, 150, 255);
313 al_draw_filled_circle(wx, wy, 5, wc);
314 al_draw_circle(wx, wy, 5, al_map_rgb(255, 255, 255), 1);
317 snprintf(num,
sizeof(num),
"%d", i);
318 al_draw_text(font, al_map_rgb(200, 200, 200),
319 wx + 7, wy - 6, 0, num);
325 al_map_rgb(100, 180, 255));
332 al_draw_filled_circle(ox, oy, 8,
333 al_map_rgb(255, 180, 0));
334 al_draw_circle(ox, oy, 8,
335 al_map_rgb(255, 255, 255), 2);
342 al_map_rgb(255, 255, 100), 1);
346 const char*
mode =
"???";
354 snprintf(buf,
sizeof(buf),
"t=%.2f seg=%d/%d [%s]",
357 al_draw_text(font, al_map_rgb(200, 200, 200), 5, 18, 0, buf);
358 snprintf(buf,
sizeof(buf),
"pos=(%.0f,%.0f) vel=(%.0f,%.0f) wp=%d",
364 al_draw_text(font, al_map_rgb(200, 200, 200), 5, 34, 0, buf);
367 ?
"Click to place waypoints"
368 :
"Click to add more waypoints";
369 al_draw_text(font, al_map_rgb(150, 150, 150),
371 ALLEGRO_ALIGN_CENTER, msg);
376 al_map_rgba(0, 200, 255, 180), 2);
377 al_draw_text(font, al_map_rgb(160, 160, 160),
378 30,
HEIGHT - 66, 0,
"Interpolation");
380 al_map_rgba(255, 255, 0, 120), 1);
382 al_map_rgba(255, 255, 0, 120), 1);
383 al_draw_text(font, al_map_rgb(160, 160, 160),
384 30,
HEIGHT - 52, 0,
"Extrapolation");
386 al_draw_text(font, al_map_rgb(90, 90, 90), 5,
HEIGHT - 15, 0,
387 "[R] Reset [ESC] Quit");
400 for (
int i = 0; i <
NUM_WP3D; i++) {
432 float ox, oy, ax, ay;
436 draw_arrow(ox, oy, ax, ay, al_map_rgb(200, 60, 60), 2);
437 al_draw_text(font, al_map_rgb(200, 60, 60),
438 ax + 4, ay - 6, 0,
"X");
441 draw_arrow(ox, oy, ax, ay, al_map_rgb(60, 200, 60), 2);
442 al_draw_text(font, al_map_rgb(60, 200, 60),
443 ax + 4, ay - 6, 0,
"Y");
446 draw_arrow(ox, oy, ax, ay, al_map_rgb(60, 60, 200), 2);
447 al_draw_text(font, al_map_rgb(60, 60, 200),
448 ax + 4, ay - 6, 0,
"Z");
452 ALLEGRO_COLOR gc = al_map_rgba(45, 45, 45, 128);
453 for (
int i = -4; i <= 4; i++) {
454 float x1, y1, x2, y2;
457 al_draw_line(x1, y1, x2, y2, gc, 1);
460 al_draw_line(x1, y1, x2, y2, gc, 1);
476 for (
int i = 1; i <= total_steps; i++) {
477 double t = t0 + (t1 - t0) * (
double)i / (double)total_steps;
481 al_draw_line(px, py, nx, ny,
482 al_map_rgba(255, 100, 255, 140), 2);
490 al_map_rgb(18, 18, 22));
492 al_draw_text(font, al_map_rgb(220, 220, 220),
493 PANEL_W + 5, 5, 0,
"3D Multi-Waypoint Loop");
504 for (
int i = 0; i <
NUM_WP3D; i++) {
510 ? al_map_rgb(255, 80, 80)
511 : al_map_rgb(100, 100, 200);
512 al_draw_filled_circle(sx, sy, 4, c);
513 al_draw_circle(sx, sy, 4, al_map_rgb(200, 200, 200), 1);
516 snprintf(num,
sizeof(num),
"%d", i);
517 al_draw_text(font, al_map_rgb(180, 180, 180),
518 sx + 6, sy - 6, 0, num);
523 al_map_rgb(200, 120, 255));
527 float obj_sx, obj_sy;
534 float gnd_sx, gnd_sy;
538 al_draw_line(gnd_sx, gnd_sy, obj_sx, obj_sy,
539 al_map_rgba(100, 100, 100, 80), 1);
540 al_draw_filled_circle(gnd_sx, gnd_sy, 2,
541 al_map_rgba(100, 100, 100, 60));
544 al_draw_filled_circle(obj_sx, obj_sy, 7,
545 al_map_rgb(255, 200, 50));
546 al_draw_circle(obj_sx, obj_sy, 7,
547 al_map_rgb(255, 255, 255), 2);
551 float vel_sx, vel_sy;
560 al_map_rgb(255, 255, 100), 1);
564 snprintf(buf,
sizeof(buf),
"t=%.2f seg=%d/%d wp=%d",
567 al_draw_text(font, al_map_rgb(200, 200, 200),
569 snprintf(buf,
sizeof(buf),
"pos=(%.1f, %.1f, %.1f)",
573 al_draw_text(font, al_map_rgb(200, 200, 200),
575 snprintf(buf,
sizeof(buf),
"vel=(%.1f, %.1f, %.1f)",
579 al_draw_text(font, al_map_rgb(200, 200, 200),
585int main(
int argc,
char* argv[]) {
592 n_abort(
"Could not init Allegro.\n");
594 if (!al_init_primitives_addon()) {
595 n_abort(
"Unable to initialize primitives addon\n");
597 if (!al_init_font_addon()) {
598 n_abort(
"Unable to initialize font addon\n");
600 if (!al_install_keyboard()) {
601 n_abort(
"Unable to initialize keyboard handler\n");
603 if (!al_install_mouse()) {
604 n_abort(
"Unable to initialize mouse handler\n");
607 al_set_new_display_flags(ALLEGRO_OPENGL | ALLEGRO_WINDOWED);
610 n_abort(
"Unable to create display\n");
613 "Nilorea Trajectory API - Multi-Waypoint Demo");
615 ALLEGRO_FONT* font = al_create_builtin_font();
616 ALLEGRO_TIMER* timer = al_create_timer(1.0 /
FPS);
618 ALLEGRO_EVENT_QUEUE* event_queue = al_create_event_queue();
619 al_register_event_source(event_queue,
620 al_get_display_event_source(
display));
621 al_register_event_source(event_queue,
622 al_get_timer_event_source(timer));
623 al_register_event_source(event_queue,
624 al_get_keyboard_event_source());
625 al_register_event_source(event_queue,
626 al_get_mouse_event_source());
628 ALLEGRO_BITMAP* scrbuf = al_create_bitmap(
WIDTH,
HEIGHT);
633 al_start_timer(timer);
640 al_wait_for_event(event_queue, &ev);
642 if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
644 }
else if (ev.type == ALLEGRO_EVENT_KEY_DOWN) {
645 if (ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE) {
647 }
else if (ev.keyboard.keycode == ALLEGRO_KEY_R) {
650 }
else if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) {
651 if (ev.mouse.button == 1) {
655 }
else if (ev.type == ALLEGRO_EVENT_DISPLAY_SWITCH_IN ||
656 ev.type == ALLEGRO_EVENT_DISPLAY_SWITCH_OUT) {
657 al_clear_keyboard_state(
display);
658 al_flush_event_queue(event_queue);
659 }
else if (ev.type == ALLEGRO_EVENT_TIMER) {
675 if (do_draw && al_is_event_queue_empty(event_queue)) {
676 al_set_target_bitmap(scrbuf);
677 al_clear_to_color(al_map_rgb(0, 0, 0));
683 al_map_rgb(80, 80, 80), 2);
685 al_set_target_bitmap(al_get_backbuffer(
display));
686 al_draw_bitmap(scrbuf, 0, 0, 0);
694 al_destroy_bitmap(scrbuf);
695 al_destroy_font(font);
696 al_destroy_timer(timer);
697 al_destroy_event_queue(event_queue);
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.