/* GalaxiesApplet.java * Adapted from galaxy.c --- spinning galaxies * Galaxy Collision Screen Saver * * Originally done by Uli Siegmund on Amiga * for EGS in Cluster * Port from Cluster/EGS to C/Intuition by Harald Backert * Port to X11 and incorporation into xlockmore by Hubert Feyrer * * * Permission to use, copy, modify, and distribute this software and its * documentation for any purpose and without fee is hereby granted, * provided that the above copyright notice appear in all copies and that * both that copyright notice and this permission notice appear in * supporting documentation. * * This file is provided AS IS with no warranties of any kind. The author * shall have no liability with respect to the infringement of copyrights, * trade secrets or any patents by this file or any part thereof. In no * event will the author be liable for any lost revenue or profits or * other special, indirect and consequential damages. * * Revision History: * 08-Nov-01: Convert to java applet by Chris Stevenson * http://www.users.on.net/zhcchz/java/galaxy/UniverseApplet.html * with z axis information * for star size, perspective view by Richard Loftin * * 14-Nov-00: Port to Win32 by Richard Loftin - now * uses z axis information for star size, perspective view. * 10-May-97: Compatible with xscreensaver * 18-Apr-97: Memory leak fixed by Tom Schmidt * 07-Apr-97: Modified by Dave Mitchell * random star sizes * colors change depending on velocity * 23-Oct-94: Modified by David Bagley * 10-Oct-94: Add colors by Hubert Feyer * 30-Sep-94: Initial port by Hubert Feyer * 09-Mar-94: VMS can generate a random number 0.0 which results in a * division by zero, corrected by Jouk Jansen * */ import java.awt.*; import java.awt.event.*; import java.applet.*; import java.math.*; import java.util.*; class Star { static final double QCONS = 0.001; double pos[]; double vel[]; int size; public Star(Galaxy galaxy) { pos = new double[3]; vel = new double[3]; double w = 2.0 * Math.PI * Math.random(); double sinw = Math.sin(w); double cosw = Math.cos(w); double d = Math.random() * galaxy.size; double h = Math.random() * Math.exp(-2.0 * (d / galaxy.size)) / 5.0 * galaxy.size; if (Math.random() < 0.5) { h = -h; } pos[0] = galaxy.mat[0][0] * d * cosw + galaxy.mat[1][0] * d * sinw + galaxy.mat[2][0] * h + galaxy.pos[0]; pos[1] = galaxy.mat[0][1] * d * cosw + galaxy.mat[1][1] * d * sinw + galaxy.mat[2][1] * h + galaxy.pos[1]; pos[2] = galaxy.mat[0][2] * d * cosw + galaxy.mat[1][2] * d * sinw + galaxy.mat[2][2] * h + galaxy.pos[2]; double v = Math.sqrt(galaxy.mass * QCONS / Math.sqrt(d * d + h * h)); vel[0] = -galaxy.mat[0][0] * v * sinw + galaxy.mat[1][0] * v * cosw + galaxy.vel[0]; vel[1] = -galaxy.mat[0][1] * v * sinw + galaxy.mat[1][1] * v * cosw + galaxy.vel[1]; vel[2] = -galaxy.mat[0][2] * v * sinw + galaxy.mat[1][2] * v * cosw + galaxy.vel[2]; size = (int)(Math.random() * 7.0); } } class Galaxy { public final static int MAXSTARS = 700; public final static double GALAXYRANGESIZE = 0.1; public final static double GALAXYMINSIZE = 0.1; final static double ZOFFSET = 1.5; int mass; int nstars; Star[] stars; double pos[]; double vel[]; double size; double[][] mat; public Galaxy(Universe universe) { pos = new double[3]; vel = new double[3]; nstars = MAXSTARS; vel[0] = Math.random() * 2.0 - 1.0; vel[1] = Math.random() * 2.0 - 1.0; vel[2] = Math.random() * 2.0 - 1.0; pos[0] = -vel[0] * universe.f_deltat * universe.f_hititerations + Math.random() - 0.5; pos[1] = -vel[1] * universe.f_deltat * universe.f_hititerations + Math.random() - 0.5; pos[2] = (-vel[2] * universe.f_deltat * universe.f_hititerations + Math.random() - 0.5) + ZOFFSET; mass = nstars; size = GALAXYRANGESIZE * Math.random() + GALAXYMINSIZE; /* initialize galaxy disk */ double w1 = 2.0 * Math.PI * Math.random(); double w2 = 2.0 * Math.PI * Math.random(); double sinw1 = Math.sin(w1); double sinw2 = Math.sin(w2); double cosw1 = Math.cos(w1); double cosw2 = Math.cos(w2); mat = new double[3][3]; mat[0][0] = cosw2; mat[0][1] = -sinw1 * sinw2; mat[0][2] = cosw1 * sinw2; mat[1][0] = 0.0; mat[1][1] = cosw1; mat[1][2] = sinw1; mat[2][0] = -sinw2; mat[2][1] = -sinw1 * cosw2; mat[2][2] = cosw1 * cosw2; /* create stars */ stars = new Star[nstars]; for (int i=0; i0.0) { int x = (int) ((150.0 * st.pos[0] / st.pos[2]) + xf2); int y = (int) ((150.0 * st.pos[1] / st.pos[2]) + yf2); int z = (int) (2.0 / (st.pos[2] + st.size)) + 1; if(z>10)z=10; g1.fillRect(x, y, z, z); } } for (int k = i + 1; k < ngalaxies; ++k) { Galaxy gtk = universe.galaxies[k]; double d0 = gtk.pos[0] - gt.pos[0]; double d1 = gtk.pos[1] - gt.pos[1]; double d2 = gtk.pos[2] - gt.pos[2]; d = d0 * d0 + d1 * d1 + d2 * d2; d = gt.mass * gt.mass / (d * Math.sqrt(d)) * delta; d0 *= d; d1 *= d; d2 *= d; gt.vel[0] += d0 / gt.mass; gt.vel[1] += d1 / gt.mass; gt.vel[2] += d2 / gt.mass; gtk.vel[0] -= d0 / gtk.mass; gtk.vel[1] -= d1 / gtk.mass; gtk.vel[2] -= d2 / gtk.mass; } gt.pos[0] += gt.vel[0] * f_deltat; gt.pos[1] += gt.vel[1] * f_deltat; gt.pos[2] += gt.vel[2] * f_deltat; } g.drawImage(im, 0, 0, this); step++; if(step>300) { step = 0; universe = new Universe(ngalaxies); } } }