xxxxxxxxxx
Particle[] particles = new Particle[50];
void setup() {
size(640,640);
for (int i =0; i<particles.length; i++) {
particles[i] = new Particle(i+2);
}
}
void draw() {
background(255);
for (int i =0; i<particles.length; i++) {
particles[i].motion();
particles[i].drawParticles();
particles[i].constraints();
}
}
class Particle {
PVector location;
PVector velocity;
PVector acceleration;
float topSpeed;
Particle(int topSpeed_) {
location = new PVector(random(width), random(height));
velocity = new PVector(random(-5,5),random(-5,5));
topSpeed = topSpeed_;
acceleration = new PVector();
}
void motion() {
PVector mouse = new PVector(mouseX, mouseY);
if (mousePressed) {
PVector dir = PVector.sub(mouse,location);
dir.normalize();
dir.mult(.5);
acceleration = dir;
}
velocity.add(acceleration);
velocity.limit(topSpeed/3);
location.add(velocity);
}
void drawParticles() {
noStroke();
fill(125 + sin(radians(frameCount))*125, 125 + cos(radians(frameCount))*125, 125 + sin(radians(-frameCount))*125);
ellipse(location.x, location.y, 16, 16);
}
void constraints() {
if ((location.x>width) || (location.x <0)) {
velocity.x = velocity.x * -1;
}
if ((location.y>height) || (location.y <0)) {
velocity.y = velocity.y * -1;
}
}
}