1 /* xscreensaver, Copyright (c) 1992 Jamie Zawinski <jwz@mcom.com>
3 * Permission to use, copy, modify, distribute, and sell this software and its
4 * documentation for any purpose is hereby granted without fee, provided that
5 * the above copyright notice appear in all copies and that both that
6 * copyright notice and this permission notice appear in supporting
7 * documentation. No representations are made about the suitability of this
8 * software for any purpose. It is provided "as is" without express or
12 /* Simulation of a pair of quasi-gravitational fields, maybe sorta kinda
13 a little like the strong and weak electromagnetic forces. Derived from
14 a Lispm screensaver by John Pezaris <pz@mit.edu>.
18 The simulation started out as a purely accurate gravitational simulation,
19 but, with constant simulation step size, I quickly realized the field being
20 simulated while grossly gravitational was, in fact, non-conservative. It
21 also had the rather annoying behavior of dealing very badly with colliding
22 orbs. Therefore, I implemented a negative-gravity region (with two
23 thresholds; as I read your code, you only implemented one) to prevent orbs
24 from every coming too close together, and added a viscosity factor if the
25 speed of any orb got too fast. This provides a nice stable system with
28 I had experimented with a number of fields including the van der Waals
29 force (very interesting orbiting behavior) and 1/r^3 gravity (not as
30 interesting as 1/r^2). An even normal viscosity (rather than the
31 thresholded version to bleed excess energy) is also not interesting.
32 The 1/r^2, -1/r^2, -10/r^2 thresholds proved not only robust but also
33 interesting -- the orbs never collided and the threshold viscosity fixed
34 the non-conservational problem.
37 #include "screenhack.h"
42 #define M_PI 3.14159265358979323846
45 #include <math.h> /* for M_PI */
58 static unsigned int default_fg_pixel;
59 static struct ball *balls;
63 static int global_size;
67 static XPoint *point_stack;
68 static int point_stack_size, point_stack_fp, pixel_stack_fp, pixel_stack_size;
69 static unsigned long *pixel_stack;
70 static unsigned int color_shift;
72 static enum object_mode {
73 ball_mode, line_mode, polygon_mode, spline_mode, spline_filled_mode,
77 static enum color_mode {
78 cycle_mode, random_mode
81 static GC draw_gc, erase_gc;
85 #define min(a,b) ((a)<(b)?(a):(b))
86 #define max(a,b) ((a)>(b)?(a):(b))
89 init_balls (dpy, window)
94 XWindowAttributes xgwa;
96 int xlim, ylim, midx, midy, r, vx, vy;
100 XGetWindowAttributes (dpy, window, &xgwa);
103 cmap = xgwa.colormap;
106 r = get_integer_resource ("radius", "Integer");
107 if (r <= 0 || r > min (xlim/2, ylim/2))
108 r = min (xlim/2, ylim/2) - 50;
109 vx = get_integer_resource ("vx", "Integer");
110 vy = get_integer_resource ("vy", "Integer");
111 npoints = get_integer_resource ("points", "Integer");
113 npoints = 3 + (random () % 5);
114 balls = (struct ball *) malloc (npoints * sizeof (struct ball));
115 segments = get_integer_resource ("segments", "Integer");
116 if (segments < 0) segments = 1;
117 threshold = get_integer_resource ("threshold", "Integer");
118 if (threshold < 0) threshold = 0;
119 delay = get_integer_resource ("delay", "Integer");
120 if (delay < 0) delay = 0;
121 global_size = get_integer_resource ("size", "Integer");
122 if (global_size < 0) global_size = 0;
123 glow_p = get_boolean_resource ("glow", "Boolean");
124 orbit_p = get_boolean_resource ("orbit", "Boolean");
125 color_shift = get_integer_resource ("colorShift", "Integer");
126 if (color_shift >= 360) color_shift = 5;
128 mode_str = get_string_resource ("mode", "Mode");
129 if (! mode_str) mode = ball_mode;
130 else if (!strcmp (mode_str, "balls")) mode = ball_mode;
131 else if (!strcmp (mode_str, "lines")) mode = line_mode;
132 else if (!strcmp (mode_str, "polygons")) mode = polygon_mode;
133 else if (!strcmp (mode_str, "tails")) mode = tail_mode;
134 else if (!strcmp (mode_str, "splines")) mode = spline_mode;
135 else if (!strcmp (mode_str, "filled-splines")) mode = spline_filled_mode;
138 "%s: mode must be balls, lines, tails, polygons, splines, or\n\
139 filled-splines, not \"%s\"\n",
144 mode_str = get_string_resource ("colorMode", "ColorMode");
145 if (! mode_str) cmode = cycle_mode;
146 else if (!strcmp (mode_str, "cycle")) cmode = cycle_mode;
147 else if (!strcmp (mode_str, "random")) cmode = random_mode;
149 fprintf (stderr, "%s: colorMode must be cycle or random, not \"%s\"\n",
154 if (mode != ball_mode && mode != tail_mode) glow_p = False;
156 if (mode == polygon_mode && npoints < 3)
159 if (mode != ball_mode)
161 int size = (segments ? segments : 1);
162 point_stack_size = size * (npoints + 1);
163 point_stack = (XPoint *) calloc (point_stack_size, sizeof (XPoint));
166 pixel_stack_size = segments;
168 pixel_stack_size = (360 / color_shift);
169 pixel_stack = (unsigned long *)
170 calloc (pixel_stack_size, sizeof (unsigned int));
174 gcv.line_width = (mode == tail_mode
175 ? (global_size ? global_size : (MAX_SIZE * 2 / 3))
177 gcv.cap_style = (mode == tail_mode ? CapRound : CapButt);
179 gcv.foreground = default_fg_pixel =
180 get_pixel_resource ("foreground", "Foreground", dpy, cmap);
181 draw_gc = XCreateGC (dpy, window, GCForeground|GCLineWidth|GCCapStyle, &gcv);
182 gcv.foreground = get_pixel_resource ("background", "Background", dpy, cmap);
183 erase_gc = XCreateGC (dpy, window, GCForeground|GCLineWidth|GCCapStyle,&gcv);
185 if (!mono_p && mode != ball_mode)
186 for (i = 0; i < pixel_stack_size; i++)
189 color.pixel = default_fg_pixel;
190 XQueryColor (dpy, cmap, &color);
191 if (!XAllocColor (dpy, cmap, &color)) abort ();
192 pixel_stack [i] = color.pixel;
195 #define rand_size() min (MAX_SIZE, 8 + (random () % (MAX_SIZE - 9)))
197 if (orbit_p && !global_size)
198 /* To orbit, all objects must be the same mass, or the math gets
200 global_size = rand_size ();
202 th = frand (M_PI+M_PI);
203 for (i = 0; i < npoints; i++)
205 int new_size = (global_size ? global_size : rand_size ());
208 balls [i].size = new_size;
209 balls [i].mass = (new_size * new_size * 10);
210 balls [i].x = midx + r * cos (i * ((M_PI+M_PI) / npoints) + th);
211 balls [i].y = midy + r * sin (i * ((M_PI+M_PI) / npoints) + th);
214 balls [i].vx = vx ? vx : ((6.0 - (random () % 11)) / 8.0);
215 balls [i].vy = vy ? vy : ((6.0 - (random () % 11)) / 8.0);
217 balls [i].color.pixel = default_fg_pixel;
218 balls [i].color.flags = DoRed | DoGreen | DoBlue;
221 if (i != 0 && (glow_p || mode != ball_mode))
222 balls [i].hue = balls [0].hue;
224 balls [i].hue = random () % 360;
225 hsv_to_rgb (balls [i].hue, 1.0, 1.0,
226 &balls [i].color.red, &balls [i].color.green,
227 &balls [i].color.blue);
228 if (!XAllocColor (dpy, cmap, &balls [i].color))
229 mono_p = True; /* just give up */
237 double v_mult = get_float_resource ("vMult", "Float");
238 if (v_mult == 0.0) v_mult = 1.0;
240 for (i = 1; i < npoints; i++)
242 double _2ipi_n = (2 * i * M_PI / npoints);
243 double x = r * cos (_2ipi_n);
244 double y = r * sin (_2ipi_n);
245 double distx = r - x;
246 double dist2 = (distx * distx) + (y * y);
247 double dist = sqrt (dist2);
248 double a1 = ((balls[i].mass / dist2) *
249 ((dist < threshold) ? -1.0 : 1.0) *
255 fprintf (stderr, "%s: domain error: forces on balls too great\n",
259 v = sqrt (a * r) * v_mult;
260 for (i = 0; i < npoints; i++)
262 double k = ((2 * i * M_PI / npoints) + th);
263 balls [i].vx = -v * sin (k);
264 balls [i].vy = v * cos (k);
268 if (mono_p) glow_p = False;
269 XClearWindow (dpy, window);
273 compute_force (i, dx_ret, dy_ret)
275 float *dx_ret, *dy_ret;
280 for (j = 0; j < npoints; j++)
282 float x_dist, y_dist, dist, dist2;
284 if (i == j) continue;
285 x_dist = balls [j].x - balls [i].x;
286 y_dist = balls [j].y - balls [i].y;
287 dist2 = (x_dist * x_dist) + (y_dist * y_dist);
290 if (dist > 0.1) /* the balls are not overlapping */
292 float new_acc = ((balls[j].mass / dist2) *
293 ((dist < threshold) ? -1.0 : 1.0));
294 float new_acc_dist = new_acc / dist;
295 *dx_ret += new_acc_dist * x_dist;
296 *dy_ret += new_acc_dist * y_dist;
299 { /* the balls are overlapping; move randomly */
300 *dx_ret += (frand (10.0) - 5.0);
301 *dy_ret += (frand (10.0) - 5.0);
307 run_balls (dpy, window)
311 int last_point_stack_fp = point_stack_fp;
312 static int tick = 500, xlim, ylim;
313 static Colormap cmap;
318 XWindowAttributes xgwa;
319 XGetWindowAttributes (dpy, window, &xgwa);
323 cmap = xgwa.colormap;
326 /* compute the force of attraction/repulsion among all balls */
327 for (i = 0; i < npoints; i++)
328 compute_force (i, &balls[i].dx, &balls[i].dy);
330 /* move the balls according to the forces now in effect */
331 for (i = 0; i < npoints; i++)
333 float old_x = balls[i].x;
334 float old_y = balls[i].y;
336 int size = balls[i].size;
337 balls[i].vx += balls[i].dx;
338 balls[i].vy += balls[i].dy;
340 /* don't let them get too fast: impose a terminal velocity
341 (actually, make the medium have friction) */
342 if (balls[i].vx > 10)
347 if (balls[i].vy > 10)
353 balls[i].x += balls[i].vx;
354 balls[i].y += balls[i].vy;
356 /* bounce off the walls */
357 if (balls[i].x >= (xlim - balls[i].size))
359 balls[i].x = (xlim - balls[i].size - 1);
361 balls[i].vx = -balls[i].vx;
363 if (balls[i].y >= (ylim - balls[i].size))
365 balls[i].y = (ylim - balls[i].size - 1);
367 balls[i].vy = -balls[i].vy;
373 balls[i].vx = -balls[i].vx;
379 balls[i].vy = -balls[i].vy;
385 /* make color saturation be related to particle acceleration. */
389 double s, v, fraction;
390 float vx = balls [i].dx;
391 float vy = balls [i].dy;
393 if (vx < 0) vx = -vx;
394 if (vy < 0) vy = -vy;
396 if (fraction > limit) fraction = limit;
398 s = 1 - (fraction / limit);
401 s = (s * 0.75) + 0.25;
403 hsv_to_rgb (balls [i].hue, s, v,
404 &new_color.red, &new_color.green, &new_color.blue);
405 if (XAllocColor (dpy, cmap, &new_color))
407 XFreeColors (dpy, cmap, &balls [i].color.pixel, 1, 0);
408 balls [i].color = new_color;
412 if (mode == ball_mode)
415 XSetForeground (dpy, draw_gc, balls [i].color.pixel);
416 XFillArc (dpy, window, erase_gc, (int) old_x, (int) old_y,
417 size, size, 0, 360*64);
418 XFillArc (dpy, window, draw_gc, (int) new_x, (int) new_y,
419 size, size, 0, 360*64);
421 if (mode != ball_mode)
423 point_stack [point_stack_fp].x = new_x;
424 point_stack [point_stack_fp].y = new_y;
429 /* draw the lines or polygons after computing all points */
430 if (mode != ball_mode)
432 point_stack [point_stack_fp].x = balls [0].x; /* close the polygon */
433 point_stack [point_stack_fp].y = balls [0].y;
435 if (point_stack_fp == point_stack_size)
437 else if (point_stack_fp > point_stack_size) /* better be aligned */
442 color2 = balls [0].color;
446 cycle_hue (&color2, color_shift);
449 color2.red = random () % 65535;
450 color2.green = random () % 65535;
451 color2.blue = random () % 65535;
457 if (!XAllocColor (dpy, cmap, &color2))
459 color2 = balls [0].color;
460 if (!XAllocColor (dpy, cmap, &balls [0].color))
463 pixel_stack [pixel_stack_fp++] = balls [0].color.pixel;
464 if (pixel_stack_fp >= pixel_stack_size)
466 XFreeColors (dpy, cmap, pixel_stack + pixel_stack_fp, 1, 0);
467 balls [0].color = color2;
468 XSetForeground (dpy, draw_gc, balls [0].color.pixel);
478 XDrawLines (dpy, window, erase_gc, point_stack + point_stack_fp,
479 npoints + 1, CoordModeOrigin);
480 XDrawLines (dpy, window, draw_gc, point_stack + last_point_stack_fp,
481 npoints + 1, CoordModeOrigin);
485 XFillPolygon (dpy, window, erase_gc, point_stack + point_stack_fp,
486 npoints + 1, (npoints == 3 ? Convex : Complex),
488 XFillPolygon (dpy, window, draw_gc, point_stack + last_point_stack_fp,
489 npoints + 1, (npoints == 3 ? Convex : Complex),
495 for (i = 0; i < npoints; i++)
497 int index = point_stack_fp + i;
498 int next_index = (index + (npoints + 1)) % point_stack_size;
499 XDrawLine (dpy, window, erase_gc,
500 point_stack [index].x,
501 point_stack [index].y,
502 point_stack [next_index].x,
503 point_stack [next_index].y);
505 index = last_point_stack_fp + i;
506 next_index = (index - (npoints + 1)) % point_stack_size;
507 if (next_index < 0) next_index += point_stack_size;
508 if (point_stack [next_index].x == 0 &&
509 point_stack [next_index].y == 0)
511 XDrawLine (dpy, window, draw_gc,
512 point_stack [index].x,
513 point_stack [index].y,
514 point_stack [next_index].x,
515 point_stack [next_index].y);
520 case spline_filled_mode:
523 static spline *s = 0;
524 if (! s) s = make_spline (npoints);
527 for (i = 0; i < npoints; i++)
529 s->control_x [i] = point_stack [point_stack_fp + i].x;
530 s->control_y [i] = point_stack [point_stack_fp + i].y;
532 compute_closed_spline (s);
533 if (mode == spline_filled_mode)
534 XFillPolygon (dpy, window, erase_gc, s->points, s->n_points,
535 (s->n_points == 3 ? Convex : Complex),
538 XDrawLines (dpy, window, erase_gc, s->points, s->n_points,
541 for (i = 0; i < npoints; i++)
543 s->control_x [i] = point_stack [last_point_stack_fp + i].x;
544 s->control_y [i] = point_stack [last_point_stack_fp + i].y;
546 compute_closed_spline (s);
547 if (mode == spline_filled_mode)
548 XFillPolygon (dpy, window, draw_gc, s->points, s->n_points,
549 (s->n_points == 3 ? Convex : Complex),
552 XDrawLines (dpy, window, draw_gc, s->points, s->n_points,
564 char *progclass = "Attraction";
566 char *defaults [] = {
567 "Attraction.background: black", /* to placate SGI */
568 "Attraction.foreground: white",
581 XrmOptionDescRec options [] = {
582 { "-mode", ".mode", XrmoptionSepArg, 0 },
583 { "-points", ".points", XrmoptionSepArg, 0 },
584 { "-threshold", ".threshold", XrmoptionSepArg, 0 },
585 { "-segments", ".segments", XrmoptionSepArg, 0 },
586 { "-delay", ".delay", XrmoptionSepArg, 0 },
587 { "-size", ".size", XrmoptionSepArg, 0 },
588 { "-color-mode", ".colorMode", XrmoptionSepArg, 0 },
589 { "-color-shift", ".colorShift", XrmoptionSepArg, 0 },
590 { "-radius", ".radius", XrmoptionSepArg, 0 },
591 { "-vx", ".vx", XrmoptionSepArg, 0 },
592 { "-vy", ".vy", XrmoptionSepArg, 0 },
593 { "-vmult", ".vMult", XrmoptionSepArg, 0 },
594 { "-glow", ".glow", XrmoptionNoArg, "true" },
595 { "-noglow", ".glow", XrmoptionNoArg, "false" },
596 { "-orbit", ".orbit", XrmoptionNoArg, "true" }
598 int options_size = (sizeof (options) / sizeof (options[0]));
601 screenhack (dpy, window)
605 init_balls (dpy, window);
608 run_balls (dpy, window);
609 if (delay) usleep (delay);