xxxxxxxxxx
44
var particles = [];
var target;
function setup() {
createCanvas(600, 400);
for (var i = 0; i < 10; i++) {
particles.push(new Particle());
}
background(0);
target = createVector(0, 200);
}
function draw() {
for (var i = 0; i < 10; i++) {
particles[i].show();
particles[i].update();
}
target.x = target.x + 1;
}
function Particle() {
this.pos = createVector(random(width), random(height));
this.vel = createVector(1, 0);
this.acc = createVector(-0.01, 0);
this.show = function() {
strokeWeight(4);
stroke(255);
point(this.pos.x, this.pos.y);
}
this.update = function() {
// Direction
//acc = new PVector(mouseX - pos.x, mouseY - pos.y);
this.acc = p5.Vector.sub(target, this.pos);
// Magnitude
this.acc.setMag(1);
this.pos.add(this.vel);
this.vel.add(this.acc);
this.vel.limit(5);
}
}