/** * Moon.java -- Describes a moon * * By Bob Jenkins, July 1998 * Permission granted to use, reuse, rewrite this without notifying me. * * This class (Moon) describes a moon and has single-moon routines. * The class Simulate controls all the moons. * * A lot of these class variables really ought to be local variables. * But my experience with Java is that local variables not only aren't * local, they aren't even garbage collected properly. Bleah. */ import java.awt.*; import java.lang.Math; import java.util.*; // An object. It has mass, position, velocity, and other things public final class Moon { Point p; // position Point v; // velocity Point a; // acceleration double m; // mass Color c; // color /* ov,oa,nop,noa are circular queues of length HISTORY. The array positions [i] and [i+history] must be pointers to the same thing for this to work. If you need this as a queue, the elements are in [head] to [head+history-1]. If you just need to do something to all entries, it's fine to do it on [0] to [history-1]. We store old velocities (really differences between consecutive positions) rather than actual old positions so that we can represent the velocity with full precision even when it is very small compared to the position. */ Point ov[]; // old velocities (really ov[0] would be op[0]-op[1]) Point oa[]; // old accelerations Point nov[]; // new old velocities (for changing gears) Point noa[]; // new old accelerations (for changing gears) int head; // array offset of head of queues ov, oa, nov, noa // number of points used by step() static final int POINTS = 9; // number of points in step method static final int PERIOD = POINTS-1;// same error at ov[i] and ov[i+PERIOD] // size of queues static final int history = 2*POINTS + 2; // method to build polynomials to dejitter and rescale static final BInterpolate dejit = new BInterpolate(POINTS); double r; // radius int id; // identifier int screenx; // x coordinate on screen int screeny; // y coordinate on screen int screenr; // radius on screen Point peye; // point as translated by the eye // p(x,y,z) v(x,y,z) mass color size // example, "p(0.0,0.0,0.0) v(0.0,0.0,0.0) 1.0 ff00ff 1.0" public Moon(String str, int id) { StringTokenizer st = new StringTokenizer(str, " pv(,)"); Point p = new Point(0.0, 0.0, 0.0); Point v = new Point(0.0, 0.0, 0.0); p.x = Point.s2d(st.nextToken(), 0.0); p.y = Point.s2d(st.nextToken(), 0.0); p.z = Point.s2d(st.nextToken(), 0.0); v.x = Point.s2d(st.nextToken(), 0.0); v.y = Point.s2d(st.nextToken(), 0.0); v.z = Point.s2d(st.nextToken(), 0.0); double mass = Point.s2d(st.nextToken(), 0.0); Color c = Point.s2c(st.nextToken(), Color.white); double planetsize = Point.s2d(st.nextToken(), 0.0); setMoon(p,v,mass,c,planetsize,id); } public Moon(Point p, // position Point v, // velocity double m, // mass Color c, // color double r, // radius int id) // identifier { setMoon(p,v,m,c,r,id); } void setMoon(Point p, Point v, double m, Color c, double r, int id) { this.ov = new Point[2*history]; this.oa = new Point[2*history]; this.nov = new Point[2*history]; this.noa = new Point[2*history]; for (int i=0; i