/* * SwarmApplet.java * * (C) 2005, Alex S. */ import java.awt.*; import java.util.Vector; class MovingObject { public double x,y; public double direction; public double speed; public MovingObject(double x,double y,double direction,double speed){ this.x = x; this.y = y; this.direction = direction; this.speed = speed; } public void move(int w,int h){ double sin = Math.sin(direction); double cos = Math.cos(direction); double nx = x + cos * speed; double ny = y + sin * speed; if(nx > w-1){ nx = w-1; cos = -cos; } if(nx < 0){ nx = 0; cos = -cos; } if(ny > h-1){ ny = h-1; sin = -sin; } if(ny < 0){ ny = 0; sin = -sin; } // sin += 0.5; direction = Math.atan2(sin,cos); x = nx; y = ny; } } /** * very basic swarm applet */ public class SwarmApplet extends BufferedApplet { private final static int NUM_OBJS = 200; private final static int GWIDTH = 40; private final static int GHEIGHT = 40; private final static int RADIUS = 5; private final static double EPSILON = 0.001; private int width,height; private int[] queue = new int[GWIDTH * GHEIGHT]; private int[] queuelast = new int[GWIDTH * GHEIGHT]; private int[] next = new int[NUM_OBJS]; private int[] objx = new int[NUM_OBJS]; private int[] objy = new int[NUM_OBJS]; private MovingObject[] objects = new MovingObject[NUM_OBJS]; /** * initialize things */ public void init(){ super.init(); width = bounds().width; height = bounds().height; for(int i=0;i 0){ dx /= len; dy /= len; cos = (1-xcenterpull)*cos + xcenterpull*dx; sin = (1-ycenterpull)*sin + ycenterpull*dy; sin += 0.03; // downward bias. objects[i].direction = Math.atan2(sin,cos); objects[i].speed = len > RADIUS/2 ? RADIUS/2:len; } } */ int bx = objx[i]; int by = objy[i]; int Ux = bx - 1 < 0 ? 0:bx-1; int Uy = by - 1 < 0 ? 0:by-1; int Lx = bx + 1 > GWIDTH-1 ? GWIDTH-1:bx+1; int Ly = by + 1 > GHEIGHT-1 ? GHEIGHT-1:by+1; //System.out.println("current bucket: "+bx+","+by+" RANGE: ("+Ux+","+Uy+")->("+Lx+","+Ly+")"); for(int cy=Uy;cy<=Ly;cy++){ for(int cx=Ux;cx<=Lx;cx++){ //System.out.println("examining: "+cx+", "+cy); int bucket = cy * GWIDTH + cx; for(int j = queue[bucket];j != -1; j = next[j]){ if(i != j){ double ox = objects[j].x; double oy = objects[j].y; double dx = ox - x; double dy = oy - y; double dd = dx * dx + dy * dy; //System.out.print("comparing: "+i+" to "+j+" dd:"+dd+" distsqrt: "+distsquared); if(dd < distsquared){ double dist = Math.sqrt(dd) - EPSILON; double disp = ((diam - dist)/2)/dist; //System.out.print(" dist: "+dist); objects[i].x -= dx * disp; objects[i].y -= dy * disp; objects[j].x += dx * disp; objects[j].y += dy * disp; /* { double d1 = Math.sqrt( (objects[j].x - objects[i].x)* (objects[j].x - objects[i].x) + (objects[j].y - objects[i].y)* (objects[j].y - objects[i].y) ); System.out.println("before: "+dist+" after: "+d1); } */ objects[i].direction = Math.atan2(-dy,-dx); objects[j].direction = Math.atan2(dy,dx); double speed = objects[i].speed; objects[i].speed = objects[j].speed; objects[j].speed = speed; } //System.out.println(""); } } } } } if (true || damage) { g.setColor(Color.white); g.fillRect(0, 0, bounds().width, bounds().height); /* g.setColor(Color.gray); for(int i=0;i