class Attractor { Particle p; private float r; private color c; Attractor(Particle _p, float _r, color _c) { p = _p; p.makeFixed(); r = _r; c = _c; } void move(int d) { float s = 5; switch(d) { case 1: p.moveBy(0, -s, 0); break; case 2: p.moveBy(s, 0, 0); break; case 3: p.moveBy(0, s, 0); break; case 4: p.moveBy(-s, 0, 0); break; } if ( x() < r ) p.moveTo(r, y(), 0); else if ( x() > width - r ) p.moveTo(width - r, y(), 0); if ( y() < r ) p.moveTo(x(), r, 0); else if ( y() > height - r ) p.moveTo(x(), height - r, 0); } void collide(Pebble peb) { float ad = dist(x(), y(), peb.x(), peb.y()); float rd = r + peb.radius(); if ( ad < rd ) { float dx = (peb.x() - x())/ad; float dy = (peb.y() - y())/ad; peb.moveTo(x() + dx*rd, y() + dx*rd); peb.setVelocity(dx*20, dy*20); } } float radius() { return r; } float x() { return p.position().x(); } float y() { return p.position().y(); } void render() { fill(c); stroke(0); strokeWeight(2); ellipse(x(), y(), r, r); } }