/** * Attraction.java is ported from attraction xscreensaver module. */ /* xscreensaver, Copyright (c) 1992, 1995, 1996, 1997, 1998 * Jamie Zawinski * * Permission to use, copy, modify, distribute, and sell this software and its * documentation for any purpose is hereby granted without fee, provided that * the above copyright notice appear in all copies and that both that * copyright notice and this permission notice appear in supporting * documentation. No representations are made about the suitability of this * software for any purpose. It is provided "as is" without express or * implied warranty. */ /* Simulation of a pair of quasi-gravitational fields, maybe sorta kinda a little like the strong and weak electromagnetic forces. Derived from a Lispm screensaver by John Pezaris . Mouse control and viscosity added by "Philip Edward Cutone, III" . John sez: The simulation started out as a purely accurate gravitational simulation, but, with constant simulation step size, I quickly realized the field being simulated while grossly gravitational was, in fact, non-conservative. It also had the rather annoying behavior of dealing very badly with colliding orbs. Therefore, I implemented a negative-gravity region (with two thresholds; as I read your code, you only implemented one) to prevent orbs from every coming too close together, and added a viscosity factor if the speed of any orb got too fast. This provides a nice stable system with interesting behavior. I had experimented with a number of fields including the van der Waals force (very interesting orbiting behavior) and 1/r^3 gravity (not as interesting as 1/r^2). An even normal viscosity (rather than the thresholded version to bleed excess energy) is also not interesting. The 1/r^2, -1/r^2, -10/r^2 thresholds proved not only robust but also interesting -- the orbs never collided and the threshold viscosity fixed the non-conservational problem. Philip sez: > An even normal viscosity (rather than the thresholded version to > bleed excess energy) is also not interesting. unless you make about 200 points.... set the viscosity to about .8 and drag the mouse through it. it makes a nice wave which travels through the field. And (always the troublemaker) Joe Keane sez: Despite what John sez, the field being simulated is always conservative. The real problem is that it uses a simple hack, computing acceleration *based only on the starting position*, instead of a real differential equation solver. Thus you'll always have energy coming out of nowhere, although it's most blatant when balls get close together. If it were done right, you wouldn't need viscosity or artificial limits on how close the balls can get. */ public class Attraction extends DoubleBufferedFrame { public static final int ball_mode = 0; public static final int line_mode = 1; public static final int polygon_mode = 2; public static final int tail_mode = 3; public static final int spline_mode = 4; public static final int spline_filled_mode = 5; public static int init_radius = -1; public static int init_vx = 1; public static int init_vy = 1; public static int init_points = 5; public static int init_segments = 1; public static init_threshold = 20; public static int init_delay = 0; public static int init_global_size = 1; public static boolean init_glow = false; public static boolean init_orbit = false; public static double init_viscosity = 1; public static int init_mode = ball_mode; public static int init_colors = 8; public static double init_v_mult = 1.0; class Ball { double x, y; double vx, vy; double dx, dy; double mass; int size; int pixel_index; int hue; public Ball() { } } static Ball[] balls; static int npoints; static threshold; static int delay; static int global_size; static int segments; static boolean glow_p; static boolean orbit_p; static XPoint[] point_stack; static int point_stack_size, point_stack_fp; static Color[] colors; static int ncolors; static int fg_index; // static int color_shift; /*flip mods for mouse interaction*/ //static Bool mouse_p; //int mouse_x, mouse_y, mouse_mass, root_x, root_y; static double viscosity; static int mode; */ //static GC draw_gc, erase_gc; static int MAX_SIZE=16; /* #define min(a,b) ((a)<(b)?(a):(b)) #define max(a,b) ((a)>(b)?(a):(b)) */ public int rand_size() { return((int)Math.min(MAX_SIZE,8+(random() % (MAXSIZE-9)))); } public void init_balls(Graphics g, int width, int height) { int i; // XWindowAttributes xgwa; // XGCValues gcv; int xlim, ylim, midx, midy, r, vx, vy; double th; // Colormap cmap; // char *mode_str; // XGetWindowAttributes (dpy, window, &xgwa); xlim = width; ylim = height; // cmap = xgwa.colormap; midx = xlim/2; midy = ylim/2; r = init_radius; if (r <= 0 || r > Math.min(xlim/2, ylim/2)) { r = min (xlim/2, ylim/2) - 50; } vx = init_vx; vy = init_vy; npoints = init_points; if (npoints < 1) { npoints = 3 + (random () % 5); } balls = new Balls[npoints]; for(int x=0;x0) { try { Thread.currentThread().sleep(delay); } catch(Exception e) { System.out.println("fatal error: "+e); System.exit(-10); } } } } }