1exspline(3) Allegro manual exspline(3)
2
3
4
6 exspline - Constructing smooth movement paths from spline curves. Alle‐
7 gro game programming library.
8
10 #include <allegro.h>
11
12
13 Example exspline
14
16 This program demonstrates the use of spline curves to create smooth
17 paths connecting a number of node points. This can be useful for con‐
18 structing realistic motion and animations.
19
20 The technique is to connect the series of guide points p1..p(n) with
21 spline curves from p1-p2, p2-p3, etc. Each spline must pass though both
22 of its guide points, so they must be used as the first and fourth of
23 the spline control points. The fun bit is coming up with sensible val‐
24 ues for the second and third spline control points, such that the
25 spline segments will have equal gradients where they meet. I came up
26 with the following solution:
27
28 For each guide point p(n), calculate the desired tangent to the curve
29 at that point. I took this to be the vector p(n-1) -> p(n+1), which can
30 easily be calculated with the inverse tangent function, and gives
31 decent looking results. One implication of this is that two dummy guide
32 points are needed at each end of the curve, which are used in the tan‐
33 gent calculations but not connected to the set of splines.
34
35 Having got these tangents, it becomes fairly easy to calculate the
36 spline control points. For a spline between guide points p(a) and p(b),
37 the second control point should lie along the positive tangent from
38 p(a), and the third control point should lie along the negative tangent
39 from p(b). How far they are placed along these tangents controls the
40 shape of the curve: I found that applying a 'curviness' scaling factor
41 to the distance between p(a) and p(b) works well.
42
43 One thing to note about splines is that the generated points are not
44 all equidistant. Instead they tend to bunch up nearer to the ends of
45 the spline, which means you will need to apply some fudges to get an
46 object to move at a constant speed. On the other hand, in situations
47 where the curve has a noticeable change of direction at each guide
48 point, the effect can be quite nice because it makes the object slow
49 down for the curve.
50
51
53 END_OF_MAIN(3), SCREEN_W(3), acquire_screen(3), alert(3), alle‐
54 gro_error(3), allegro_init(3), allegro_message(3), calc_spline(3), cir‐
55 clefill(3), clear_keybuf(3), clear_to_color(3), desktop_palette(3),
56 fixatan2(3), fixcos(3), fixed(3), fixmul(3), fixsin(3), fixsqrt(3),
57 fixtof(3), fixtoi(3), font(3), ftofix(3), install_keyboard(3),
58 install_mouse(3), install_timer(3), itofix(3), key(3), keypressed(3),
59 line(3), makecol(3), mouse_b(3), mouse_x(3), mouse_y(3), pal‐
60 ette_color(3), poll_mouse(3), readkey(3), release_screen(3), screen(3),
61 set_gfx_mode(3), set_palette(3), show_mouse(3), spline(3), textout_cen‐
62 tre_ex(3), textprintf_centre_ex(3), textprintf_ex(3), vsync(3),
63 xor_mode(3)
64
65
66
67Allegro version 4.2.3 exspline(3)