// orbiting creatures // wander till someone appears. int screenX = 600; int screenY = 600; int howMany = 80; int targetSize = 40; float Gx = 20.5; float Gy = 20.5; float Kx = 0.1; float Ky = 0.1; float orbitDistance = 80; HashSet bichos = new HashSet(); HashSet targets = new HashSet(); HashSet moving = new HashSet(); Target nulltarget; void setup() { nulltarget = new Target(0,0); for (int i=0; i0?1:-1); fillColor = color(random(150,250),random(150,250),(m*97)%255); } void step() { // choose the nearest target. if (targets.size() == 0) { target = nulltarget; } else { float minDist = 99999999; Iterator it = targets.iterator(); while (it.hasNext()) { Target t = (Target)it.next(); float d = dist(x,y, t.x, t.y); if (d < minDist) { minDist = d; target = t; } } } float dt = millis() - lastTime; lastTime = millis(); float d = dist(x,y,target.x,target.y); float dx = target.x - x; float dy = target.y - y; // si estan muy cerca, no los toco if (d > 10) { // dx/d = cos, dy/d = sin ax = (dx/d) * Gx * target.m / (d*d); ay = (dy/d) * Gy * target.m / (d*d); ax = ax + ( (dx/d) * Kx * sqrt(d) / m ); ay = ay + ( (dy/d) * Ky * sqrt(d) / m ); ax = ax / 100; ay = ay / 100; } vx = vx + ax * dt; vy = vy + ay * dt; x = x + vx*dt; y = y + vy*dt; // los roto un poquito // esto seria bueno que se hiciera de acuerdo al angulo de aproximacion (izq o der) float r = clockwise * (-HALF_PI)/153; x = ( x*cos(r) - y*sin(r) ); y = ( x*sin(r) + y*cos(r) ); // siempre tiro un poco hacia el centro x = x * 0.99; y = y * 0.99; } void draw() { // screen coords float sx = x + screenX/2; float sy = y + screenY/2; noStroke(); fill(fillColor); ellipse(sx,sy,diameter,diameter); } } // ------------------------------------ // ------------------------------------ // ------------------------------------ // ------------------------------------ // ------------------------------------ // --- class Target ------------------- class Target { float x = 0; float y = 0; float m = 1; boolean deleted = false; Target(float posX, float posY) { this.x = posX; this.y = posY; } void draw() { // screen coords float tsx = x + screenX/2; float tsy = y + screenY/2; stroke(200); noFill(); ellipse(tsx,tsy,targetSize,targetSize); } }