Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_trajectory.c
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
40#define ALLEGRO_UNSTABLE 1
41
42#define WIDTH 800
43#define HEIGHT 600
44#define PANEL_W 400
45#define FPS 60.0
46#define DT (1.0 / FPS)
47#define MAX_TRAIL 300
48
49#include <stdio.h>
50#include <string.h>
51#include <math.h>
52
53#include "nilorea/n_common.h"
54#include "nilorea/n_log.h"
56
57#include <allegro5/allegro.h>
58#include <allegro5/allegro_primitives.h>
59#include <allegro5/allegro_font.h>
60
61#ifndef M_PI
62#define M_PI 3.14159265358979323846
63#endif
64
65/* 2D Demo State */
66static TRAJECTORY* traj_2d = NULL;
67static double time_2d = 0.0;
68static int running_2d = 0;
69
70/* raw waypoint storage for Catmull-Rom velocity computation */
71#define MAX_WP2D 50
73static int wp2d_count = 0;
74static const double WP2D_SPEED = 80.0; /* pixels/sec reference speed */
75
76static float trail_2d[MAX_TRAIL][2];
77static int trail_2d_count = 0;
78static int trail_2d_head = 0;
79
80/* 3D Demo State */
81static TRAJECTORY* traj_3d = NULL;
82static double time_3d = 0.0;
83static double time_3d_end = 0.0;
84
85static float trail_3d[MAX_TRAIL][2];
86static int trail_3d_count = 0;
87static int trail_3d_head = 0;
88
89/* 3D waypoints forming a closed loop (last = first for seamless looping) */
90typedef struct {
91 double x, y, z, vx, vy, vz;
93
94#define NUM_WP3D 7
96 {0.0, 0.0, 0.0, 2.0, 1.0, 0.5},
97 {3.0, 2.0, 1.0, 1.0, 1.5, -0.5},
98 {4.0, 4.0, -1.0, -1.0, 1.0, -1.0},
99 {1.0, 5.0, -2.0, -2.0, 0.0, 0.5},
100 {-2.0, 3.0, 0.0, -1.0, -1.5, 1.0},
101 {-1.0, 1.0, 1.5, 1.0, -1.0, 0.0},
102 {0.0, 0.0, 0.0, 2.0, 1.0, 0.5}, /* loop back to start */
103};
104static const double WP3D_DUR = 4.0; /* seconds per 3D segment */
105
106/* Helpers */
107
108static void init_physics_2d(PHYSICS* p, double px, double py, double vx, double vy) {
109 memset(p, 0, sizeof(PHYSICS));
110 VECTOR3D_SET(p->position, px, py, 0.0);
111 VECTOR3D_SET(p->speed, vx, vy, 0.0);
112}
113
114static void init_physics_3d(PHYSICS* p, double px, double py, double pz, double vx, double vy, double vz) {
115 memset(p, 0, sizeof(PHYSICS));
116 VECTOR3D_SET(p->position, px, py, pz);
117 VECTOR3D_SET(p->speed, vx, vy, vz);
118}
119
120static void trail_push_2d(float x, float y) {
121 trail_2d[trail_2d_head][0] = x;
122 trail_2d[trail_2d_head][1] = y;
125}
126
127static void trail_push_3d(float x, float y) {
128 trail_3d[trail_3d_head][0] = x;
129 trail_3d[trail_3d_head][1] = y;
132}
133
134/* 3D Projection (Cabinet) */
135
136static void project_3d(double x, double y, double z, float* sx, float* sy) {
137 double scale = 30.0;
138 double cx = (double)PANEL_W + (double)PANEL_W / 2.0;
139 double cy = (double)HEIGHT / 2.0;
140 double angle = M_PI / 4.0;
141 *sx = (float)(cx + x * scale + z * 0.5 * cos(angle) * scale);
142 *sy = (float)(cy - y * scale - z * 0.5 * sin(angle) * scale);
143}
144
145/* Drawing Utilities */
146
147static void draw_arrow(float x1, float y1, float x2, float y2, ALLEGRO_COLOR color, float thickness) {
148 al_draw_line(x1, y1, x2, y2, color, thickness);
149 float dx = x2 - x1, dy = y2 - y1;
150 float len = sqrtf(dx * dx + dy * dy);
151 if (len < 2.0f) return;
152 float ux = dx / len, uy = dy / len;
153 float px = -uy * 4.0f, py = ux * 4.0f;
154 float bx = x2 - ux * 8.0f, by = y2 - uy * 8.0f;
155 al_draw_filled_triangle(x2, y2, bx + px, by + py,
156 bx - px, by - py, color);
157}
158
159static void draw_trail(float trail[][2], int count, int head, ALLEGRO_COLOR base_color) {
160 if (count < 2) return;
161 unsigned char r, g, b;
162 al_unmap_rgb(base_color, &r, &g, &b);
163 for (int i = 1; i < count; i++) {
164 int i0 = (head - count + i - 1 + MAX_TRAIL) % MAX_TRAIL;
165 int i1 = (head - count + i + MAX_TRAIL) % MAX_TRAIL;
166 float alpha = (float)i / (float)count;
167 ALLEGRO_COLOR c = al_map_rgba(
168 (unsigned char)(r * alpha),
169 (unsigned char)(g * alpha),
170 (unsigned char)(b * alpha),
171 (unsigned char)(alpha * 200));
172 al_draw_line(trail[i0][0], trail[i0][1],
173 trail[i1][0], trail[i1][1], c, 2);
174 }
175}
176
177/* 2D Demo: Init / Reset / Click / Rebuild / Draw */
178
179static void rebuild_2d_trajectory(void) {
181 if (wp2d_count < 2) return;
182
183 for (int i = 0; i < wp2d_count; i++) {
184 double vx, vy;
185
186 if (i == 0) {
187 /* forward difference */
188 double dt = wp2d_t[1] - wp2d_t[0];
189 vx = (wp2d_x[1] - wp2d_x[0]) / dt;
190 vy = (wp2d_y[1] - wp2d_y[0]) / dt;
191 } else if (i == wp2d_count - 1) {
192 /* backward difference */
193 double dt = wp2d_t[i] - wp2d_t[i - 1];
194 vx = (wp2d_x[i] - wp2d_x[i - 1]) / dt;
195 vy = (wp2d_y[i] - wp2d_y[i - 1]) / dt;
196 } else {
197 /* Catmull-Rom: central difference */
198 double dt = wp2d_t[i + 1] - wp2d_t[i - 1];
199 vx = (wp2d_x[i + 1] - wp2d_x[i - 1]) / dt;
200 vy = (wp2d_y[i + 1] - wp2d_y[i - 1]) / dt;
201 }
202
203 PHYSICS state;
204 init_physics_2d(&state, wp2d_x[i], wp2d_y[i], vx, vy);
206 }
207
208 running_2d = 1;
209}
210
211static void reset_2d(void) {
214 time_2d = 0.0;
215 running_2d = 0;
216 wp2d_count = 0;
217 trail_2d_count = 0;
218 trail_2d_head = 0;
219}
220
221static void handle_click_2d(float mx, float my) {
222 if (mx >= PANEL_W || wp2d_count >= MAX_WP2D) return;
223
224 if (wp2d_count == 0) {
225 wp2d_x[0] = (double)mx;
226 wp2d_y[0] = (double)my;
227 wp2d_t[0] = 0.0;
228 wp2d_count = 1;
229 return;
230 }
231
232 /* compute time for new waypoint based on distance */
233 double dx = (double)mx - wp2d_x[wp2d_count - 1];
234 double dy = (double)my - wp2d_y[wp2d_count - 1];
235 double dist = sqrt(dx * dx + dy * dy);
236 if (dist < 10.0) return;
237
238 double dur = dist / WP2D_SPEED;
239 if (dur < 0.3) dur = 0.3;
240
241 wp2d_x[wp2d_count] = (double)mx;
242 wp2d_y[wp2d_count] = (double)my;
243 wp2d_t[wp2d_count] = wp2d_t[wp2d_count - 1] + dur;
244 wp2d_count++;
245
247}
248
249static void draw_full_path_2d(void) {
250 if (!traj_2d || traj_2d->nb_points < 2) return;
251
252 double t0 = traj_2d->points[0].time_val;
253 double t1 = traj_2d->points[traj_2d->nb_points - 1].time_val;
254 int total_steps = (traj_2d->nb_points - 1) * 30;
255
256 VECTOR3D pos;
258 float px = (float)pos[0], py = (float)pos[1];
259
260 for (int i = 1; i <= total_steps; i++) {
261 double t = t0 + (t1 - t0) * (double)i / (double)total_steps;
263 float nx = (float)pos[0], ny = (float)pos[1];
264 al_draw_line(px, py, nx, ny,
265 al_map_rgba(0, 200, 255, 180), 2);
266 px = nx;
267 py = ny;
268 }
269
270 /* extrapolation tail: base length on last segment, not total path */
271 double last_seg_dur = traj_2d->points[traj_2d->nb_points - 1].time_val - traj_2d->points[traj_2d->nb_points - 2].time_val;
272 double ext = last_seg_dur * 0.5;
273 if (ext < 0.3) ext = 0.3;
274 for (int i = 1; i <= 20; i++) {
275 double t = t1 + ext * (double)i / 20.0;
277 float nx = (float)pos[0], ny = (float)pos[1];
278 if (i % 2 == 0)
279 al_draw_line(px, py, nx, ny,
280 al_map_rgba(255, 255, 0, 120), 1);
281 px = nx;
282 py = ny;
283 }
284}
285
286static void draw_2d_panel(ALLEGRO_FONT* font) {
287 al_draw_filled_rectangle(0, 0, PANEL_W, HEIGHT,
288 al_map_rgb(15, 15, 30));
289
290 ALLEGRO_COLOR grid = al_map_rgba(35, 35, 55, 255);
291 for (int x = 0; x < PANEL_W; x += 40)
292 al_draw_line((float)x, 0, (float)x, HEIGHT, grid, 1);
293 for (int y = 0; y < HEIGHT; y += 40)
294 al_draw_line(0, (float)y, PANEL_W, (float)y, grid, 1);
295
296 al_draw_text(font, al_map_rgb(220, 220, 220), 5, 5, 0,
297 "2D Multi-Waypoint Path");
298
299 /* draw full spline path */
301
302 /* draw all waypoint markers */
303 for (int i = 0; i < wp2d_count; i++) {
304 float wx = (float)wp2d_x[i], wy = (float)wp2d_y[i];
305 ALLEGRO_COLOR wc;
306 if (i == 0)
307 wc = al_map_rgb(0, 200, 0);
308 else if (i == wp2d_count - 1)
309 wc = al_map_rgb(200, 0, 0);
310 else
311 wc = al_map_rgb(100, 150, 255);
312 al_draw_filled_circle(wx, wy, 5, wc);
313 al_draw_circle(wx, wy, 5, al_map_rgb(255, 255, 255), 1);
314
315 char num[16];
316 snprintf(num, sizeof(num), "%d", i);
317 al_draw_text(font, al_map_rgb(200, 200, 200),
318 wx + 7, wy - 6, 0, num);
319 }
320
321 if (running_2d && traj_2d && traj_2d->nb_points >= 2) {
322 /* trail */
324 al_map_rgb(100, 180, 255));
325
326 /* current object */
328 float ox = (float)traj_2d->current.position[0];
329 float oy = (float)traj_2d->current.position[1];
330
331 al_draw_filled_circle(ox, oy, 8,
332 al_map_rgb(255, 180, 0));
333 al_draw_circle(ox, oy, 8,
334 al_map_rgb(255, 255, 255), 2);
335
336 /* velocity arrow */
337 float vs = 0.15f;
338 draw_arrow(ox, oy,
339 ox + (float)traj_2d->current.speed[0] * vs,
340 oy + (float)traj_2d->current.speed[1] * vs,
341 al_map_rgb(255, 255, 100), 1);
342
343 /* info text */
344 char buf[128];
345 const char* mode = "???";
347 mode = "INTERP";
348 else if (traj_2d->mode == TRAJECTORY_EXTRAP)
349 mode = "EXTRAP";
350 else if (traj_2d->mode == TRAJECTORY_BEFORE)
351 mode = "BEFORE";
352
353 snprintf(buf, sizeof(buf), "t=%.2f seg=%d/%d [%s]",
355 traj_2d->nb_points - 1, mode);
356 al_draw_text(font, al_map_rgb(200, 200, 200), 5, 18, 0, buf);
357 snprintf(buf, sizeof(buf), "pos=(%.0f,%.0f) vel=(%.0f,%.0f) wp=%d",
362 wp2d_count);
363 al_draw_text(font, al_map_rgb(200, 200, 200), 5, 34, 0, buf);
364 } else {
365 const char* msg = (wp2d_count == 0)
366 ? "Click to place waypoints"
367 : "Click to add more waypoints";
368 al_draw_text(font, al_map_rgb(150, 150, 150),
369 PANEL_W / 2, 18,
370 ALLEGRO_ALIGN_CENTER, msg);
371 }
372
373 /* legend */
374 al_draw_line(5, HEIGHT - 60, 25, HEIGHT - 60,
375 al_map_rgba(0, 200, 255, 180), 2);
376 al_draw_text(font, al_map_rgb(160, 160, 160),
377 30, HEIGHT - 66, 0, "Interpolation");
378 al_draw_line(5, HEIGHT - 46, 15, HEIGHT - 46,
379 al_map_rgba(255, 255, 0, 120), 1);
380 al_draw_line(18, HEIGHT - 46, 25, HEIGHT - 46,
381 al_map_rgba(255, 255, 0, 120), 1);
382 al_draw_text(font, al_map_rgb(160, 160, 160),
383 30, HEIGHT - 52, 0, "Extrapolation");
384
385 al_draw_text(font, al_map_rgb(90, 90, 90), 5, HEIGHT - 15, 0,
386 "[R] Reset [ESC] Quit");
387}
388
389/* 3D Demo: Init / Update / Draw */
390
391static void init_3d(void) {
393 time_3d = 0.0;
394 trail_3d_count = 0;
395 trail_3d_head = 0;
396
397 /* add all waypoints to the trajectory */
398 double t = 0.0;
399 for (int i = 0; i < NUM_WP3D; i++) {
400 const WAYPOINT3D* w = &waypoints_3d[i];
401 PHYSICS state;
402 init_physics_3d(&state, w->x, w->y, w->z,
403 w->vx, w->vy, w->vz);
404 trajectory_add_point(traj_3d, &state, t);
405 t += WP3D_DUR;
406 }
407 time_3d_end = t - WP3D_DUR; /* time of last waypoint */
408}
409
410static void update_3d_logic(void) {
411 if (!traj_3d || traj_3d->nb_points < 2) return;
412
413 time_3d += DT;
414
415 /* loop when past the last waypoint */
416 if (time_3d >= time_3d_end) {
418 trail_3d_count = 0;
419 trail_3d_head = 0;
420 }
421
423 float sx, sy;
426 traj_3d->current.position[2], &sx, &sy);
427 trail_push_3d(sx, sy);
428}
429
430static void draw_3d_axes(ALLEGRO_FONT* font) {
431 float ox, oy, ax, ay;
432 project_3d(0, 0, 0, &ox, &oy);
433
434 project_3d(2.5, 0, 0, &ax, &ay);
435 draw_arrow(ox, oy, ax, ay, al_map_rgb(200, 60, 60), 2);
436 al_draw_text(font, al_map_rgb(200, 60, 60),
437 ax + 4, ay - 6, 0, "X");
438
439 project_3d(0, 2.5, 0, &ax, &ay);
440 draw_arrow(ox, oy, ax, ay, al_map_rgb(60, 200, 60), 2);
441 al_draw_text(font, al_map_rgb(60, 200, 60),
442 ax + 4, ay - 6, 0, "Y");
443
444 project_3d(0, 0, 2.5, &ax, &ay);
445 draw_arrow(ox, oy, ax, ay, al_map_rgb(60, 60, 200), 2);
446 al_draw_text(font, al_map_rgb(60, 60, 200),
447 ax + 4, ay - 6, 0, "Z");
448}
449
450static void draw_ground_grid(void) {
451 ALLEGRO_COLOR gc = al_map_rgba(45, 45, 45, 128);
452 for (int i = -4; i <= 4; i++) {
453 float x1, y1, x2, y2;
454 project_3d((double)i, 0, -4, &x1, &y1);
455 project_3d((double)i, 0, 4, &x2, &y2);
456 al_draw_line(x1, y1, x2, y2, gc, 1);
457 project_3d(-4, 0, (double)i, &x1, &y1);
458 project_3d(4, 0, (double)i, &x2, &y2);
459 al_draw_line(x1, y1, x2, y2, gc, 1);
460 }
461}
462
463static void draw_full_path_3d(void) {
464 if (!traj_3d || traj_3d->nb_points < 2) return;
465
466 double t0 = traj_3d->points[0].time_val;
467 double t1 = traj_3d->points[traj_3d->nb_points - 1].time_val;
468 int total_steps = (traj_3d->nb_points - 1) * 30;
469
470 VECTOR3D pos;
472 float px, py;
473 project_3d(pos[0], pos[1], pos[2], &px, &py);
474
475 for (int i = 1; i <= total_steps; i++) {
476 double t = t0 + (t1 - t0) * (double)i / (double)total_steps;
478 float nx, ny;
479 project_3d(pos[0], pos[1], pos[2], &nx, &ny);
480 al_draw_line(px, py, nx, ny,
481 al_map_rgba(255, 100, 255, 140), 2);
482 px = nx;
483 py = ny;
484 }
485}
486
487static void draw_3d_panel(ALLEGRO_FONT* font) {
488 al_draw_filled_rectangle(PANEL_W, 0, WIDTH, HEIGHT,
489 al_map_rgb(18, 18, 22));
490
491 al_draw_text(font, al_map_rgb(220, 220, 220),
492 PANEL_W + 5, 5, 0, "3D Multi-Waypoint Loop");
493
495 draw_3d_axes(font);
496
497 if (!traj_3d || traj_3d->nb_points < 2) return;
498
499 /* draw full looping path */
501
502 /* draw waypoint markers */
503 for (int i = 0; i < NUM_WP3D; i++) {
504 float sx, sy;
506 waypoints_3d[i].z, &sx, &sy);
507 ALLEGRO_COLOR c = (i == traj_3d->current_segment ||
508 i == traj_3d->current_segment + 1)
509 ? al_map_rgb(255, 80, 80)
510 : al_map_rgb(100, 100, 200);
511 al_draw_filled_circle(sx, sy, 4, c);
512 al_draw_circle(sx, sy, 4, al_map_rgb(200, 200, 200), 1);
513
514 char num[16];
515 snprintf(num, sizeof(num), "%d", i);
516 al_draw_text(font, al_map_rgb(180, 180, 180),
517 sx + 6, sy - 6, 0, num);
518 }
519
520 /* trail */
522 al_map_rgb(200, 120, 255));
523
524 /* current object */
526 float obj_sx, obj_sy;
530 &obj_sx, &obj_sy);
531
532 /* vertical drop line (depth cue) */
533 float gnd_sx, gnd_sy;
536 &gnd_sx, &gnd_sy);
537 al_draw_line(gnd_sx, gnd_sy, obj_sx, obj_sy,
538 al_map_rgba(100, 100, 100, 80), 1);
539 al_draw_filled_circle(gnd_sx, gnd_sy, 2,
540 al_map_rgba(100, 100, 100, 60));
541
542 /* object */
543 al_draw_filled_circle(obj_sx, obj_sy, 7,
544 al_map_rgb(255, 200, 50));
545 al_draw_circle(obj_sx, obj_sy, 7,
546 al_map_rgb(255, 255, 255), 2);
547
548 /* velocity arrow */
549 double vs3 = 0.4;
550 float vel_sx, vel_sy;
552 traj_3d->current.speed[0] * vs3,
554 traj_3d->current.speed[1] * vs3,
556 traj_3d->current.speed[2] * vs3,
557 &vel_sx, &vel_sy);
558 draw_arrow(obj_sx, obj_sy, vel_sx, vel_sy,
559 al_map_rgb(255, 255, 100), 1);
560
561 /* info text */
562 char buf[128];
563 snprintf(buf, sizeof(buf), "t=%.2f seg=%d/%d wp=%d",
566 al_draw_text(font, al_map_rgb(200, 200, 200),
567 PANEL_W + 5, 18, 0, buf);
568 snprintf(buf, sizeof(buf), "pos=(%.1f, %.1f, %.1f)",
572 al_draw_text(font, al_map_rgb(200, 200, 200),
573 PANEL_W + 5, 34, 0, buf);
574 snprintf(buf, sizeof(buf), "vel=(%.1f, %.1f, %.1f)",
577 traj_3d->current.speed[2]);
578 al_draw_text(font, al_map_rgb(200, 200, 200),
579 PANEL_W + 5, 50, 0, buf);
580}
581
582/* Main */
583
584int main(int argc, char* argv[]) {
585 (void)argc;
587
588 n_log(LOG_NOTICE, "%s is starting ...", argv[0]);
589
590 if (!al_init()) {
591 n_abort("Could not init Allegro.\n");
592 }
593 if (!al_init_primitives_addon()) {
594 n_abort("Unable to initialize primitives addon\n");
595 }
596 if (!al_init_font_addon()) {
597 n_abort("Unable to initialize font addon\n");
598 }
599 if (!al_install_keyboard()) {
600 n_abort("Unable to initialize keyboard handler\n");
601 }
602 if (!al_install_mouse()) {
603 n_abort("Unable to initialize mouse handler\n");
604 }
605
606 al_set_new_display_flags(ALLEGRO_OPENGL | ALLEGRO_WINDOWED);
607 ALLEGRO_DISPLAY* display = al_create_display(WIDTH, HEIGHT);
608 if (!display) {
609 n_abort("Unable to create display\n");
610 }
611 al_set_window_title(display,
612 "Nilorea Trajectory API - Multi-Waypoint Demo");
613
614 ALLEGRO_FONT* font = al_create_builtin_font();
615 ALLEGRO_TIMER* timer = al_create_timer(1.0 / FPS);
616
617 ALLEGRO_EVENT_QUEUE* event_queue = al_create_event_queue();
618 al_register_event_source(event_queue,
619 al_get_display_event_source(display));
620 al_register_event_source(event_queue,
621 al_get_timer_event_source(timer));
622 al_register_event_source(event_queue,
623 al_get_keyboard_event_source());
624 al_register_event_source(event_queue,
625 al_get_mouse_event_source());
626
627 ALLEGRO_BITMAP* scrbuf = al_create_bitmap(WIDTH, HEIGHT);
628
629 reset_2d();
630 init_3d();
631
632 al_start_timer(timer);
633
634 int done = 0;
635 int do_draw = 0;
636
637 while (!done) {
638 ALLEGRO_EVENT ev;
639 al_wait_for_event(event_queue, &ev);
640
641 if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
642 done = 1;
643 } else if (ev.type == ALLEGRO_EVENT_KEY_DOWN) {
644 if (ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE) {
645 done = 1;
646 } else if (ev.keyboard.keycode == ALLEGRO_KEY_R) {
647 reset_2d();
648 }
649 } else if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) {
650 if (ev.mouse.button == 1) {
651 handle_click_2d((float)ev.mouse.x,
652 (float)ev.mouse.y);
653 }
654 } else if (ev.type == ALLEGRO_EVENT_DISPLAY_SWITCH_IN ||
655 ev.type == ALLEGRO_EVENT_DISPLAY_SWITCH_OUT) {
656 al_clear_keyboard_state(display);
657 al_flush_event_queue(event_queue);
658 } else if (ev.type == ALLEGRO_EVENT_TIMER) {
659 /* 2D logic */
660 if (running_2d && traj_2d && traj_2d->nb_points >= 2) {
661 time_2d += DT;
664 (float)traj_2d->current.position[0],
665 (float)traj_2d->current.position[1]);
666 }
667
668 /* 3D logic */
670
671 do_draw = 1;
672 }
673
674 if (do_draw && al_is_event_queue_empty(event_queue)) {
675 al_set_target_bitmap(scrbuf);
676 al_clear_to_color(al_map_rgb(0, 0, 0));
677
678 draw_2d_panel(font);
679 draw_3d_panel(font);
680
681 al_draw_line(PANEL_W, 0, PANEL_W, HEIGHT,
682 al_map_rgb(80, 80, 80), 2);
683
684 al_set_target_bitmap(al_get_backbuffer(display));
685 al_draw_bitmap(scrbuf, 0, 0, 0);
686 al_flip_display();
687 do_draw = 0;
688 }
689 }
690
693 al_destroy_bitmap(scrbuf);
694 al_destroy_font(font);
695 al_destroy_timer(timer);
696 al_destroy_event_queue(event_queue);
697 al_destroy_display(display);
698
699 return 0;
700}
int main(void)
ALLEGRO_DISPLAY * display
Definition ex_fluid.c:53
#define WIDTH
Definition ex_gui.c:36
#define HEIGHT
Definition ex_gui.c:37
#define FPS
static int mode
#define M_PI
bool done
static void draw_3d_axes(ALLEGRO_FONT *font)
static int trail_2d_head
static TRAJECTORY * traj_3d
static void reset_2d(void)
static void rebuild_2d_trajectory(void)
static double wp2d_x[50]
#define NUM_WP3D
static void trail_push_2d(float x, float y)
static void trail_push_3d(float x, float y)
static int running_2d
#define MAX_WP2D
#define PANEL_W
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 double time_3d
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 double time_2d
#define DT
static double wp2d_t[50]
static void init_3d(void)
static const WAYPOINT3D waypoints_3d[7]
static int trail_3d_count
static int trail_2d_count
#define MAX_TRAIL
static double wp2d_y[50]
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 int trail_3d_head
static void draw_ground_grid(void)
static void draw_arrow(float x1, float y1, float x2, float y2, ALLEGRO_COLOR color, float thickness)
static int wp2d_count
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
Definition n_common.c:52
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:88
#define LOG_ERR
error conditions
Definition n_log.h:75
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:120
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:79
VECTOR3D speed
vx,vy,vz actual speed
Definition n_3d.h:75
VECTOR3D position
x,y,z actual position
Definition n_3d.h:73
#define VECTOR3D_SET(VECTOR, X, Y, Z)
helper to set a VECTOR3D position
Definition n_3d.h:61
double VECTOR3D[3]
struct of a point
Definition n_3d.h:58
structure of the physics of an object
Definition n_3d.h:69
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.
Generic log system.
Trajectory interpolation and dead reckoning for 2D/3D networked simulations.