ArrayList ghosts = new ArrayList(); Puck p = new Puck(); void setup(){ size(500,500); Ghost target = null; Ghost needsTarget = null; for(int i = 0; i < 20; i++){ Ghost g = new Ghost(); if(target == null) needsTarget = g; else g.setTarget(target); target = g; ghosts.add(g); } needsTarget.setTarget(target); } void draw(){ background(0,100,0); p.move(); p.draw(); for(Ghost g : ghosts){ g.move(); g.draw(); } } class Puck{ float sz = 30; float x,y; float xs, ys; Puck(){ x = mouseX; y = mouseY; } void move(){ xs = mouseX - x; ys = mouseY - y; x = mouseX; y = mouseY; } void draw(){ fill(random(200,250),128); ellipse(x,y,sz,sz); } } class Ghost{ float x = 250,y = 250; float xs, ys; float sz = 20; float spd; Ghost target; Ghost(){ x = random(500); y = random(500); sz = random(10,40); spd = 1/sz; } void setTarget(Ghost g){ target = g; } void move(){ float tx = target.x; float ty = target.y; float mul = 1; if(mousePressed){ tx = mouseX; ty = mouseY; mul = 3; } if(x < target.x) xs += spd*mul; if(x > target.x) xs -= spd*mul; if(y < target.y) ys += spd*mul; if(y > target.y) ys -= spd*mul; xs *= .99; ys *= .99; if(dist(x,y,p.x,p.y) < (p.sz + sz) / 2){ float a = atan2(y-p.y,x-p.x); x = p.x + cos(a) * (p.sz + sz) / 2 ; y = p.y + sin(a) * (p.sz + sz) /2; xs *= -1 ;//+ (p.xs/10); ys *= -1 ;//+ (p.ys/10); } if(x < 0) { x = 0; xs = abs(xs)*.9;} if(y < 0) { y = 0; ys = abs(ys)*.9;} if(x > 500) { x = 500; xs = -abs(xs)*.9;} if(y > 500) { y = 500; ys = -abs(ys)*.9;} x += xs; y += ys; } void draw(){ noStroke(); pushMatrix(); translate(x,y); rotate(xs/5); fill(255,128); arc(0,0,sz,sz,PI,2*PI); rect(-sz/2,0,sz,sz); fill(0,128); ellipse(-sz/4,0,sz/5,sz/5); ellipse(sz/4,0,sz/5,sz/5); ellipse(0,sz/4,sz/5,sz/5); popMatrix(); } }