xxxxxxxxxx
92
function Particle(p) {
this.pos = p.createVector(random(p.width), random(p.height));
this.vel = p.createVector(0, 0);
this.acc = p.createVector(0, 0);
this.maxspeed = 4;
this.col = p5.Color;
//this.col = (random(250, 325), 100, 100);
//this.col.setAlpha(5);
this.prevPos = this.pos.copy();
this.update = function(color1, color2) {
this.vel.add(this.acc);
this.vel.limit(this.maxspeed);
this.pos.add(this.vel);
this.acc.mult(0);
var xin = this.pos.x / p.width;
var yin = this.pos.y / p.height;
var zin = (xin / 2) + (yin / 2);
this.col = p.lerpColor(color1, color2, zin);
this.col.setAlpha(5);
}
this.gradient = function(particles) {
var perceptionRadius = 2;
var total = 0;
for (let other of particles) {
let d = dist(this.position.x, this.position.y, other.position.x, other.position.y);
if (other != this && d < perceptionRadius) {
total++;
}
}
}
this.follow = function(vectors) {
var x = floor(this.pos.x / scl);
var y = floor(this.pos.y / scl);
var index = x + y * cols;
var force = vectors[index];
this.applyForce(force);
}
this.applyForce = function(force) {
this.acc.add(force);
}
this.show = function() {
// this.col.x = map(this.pos.x, 0, width, 50, 255);
// this.col.z = map(this.pos.y, 0, height, 130, 255);
//stroke(this.col.x, this.col.y, this.col.z, 5);
p.stroke(this.col, 5);
p.strokeWeight(1);
p.line(this.pos.x, this.pos.y, this.prevPos.x, this.prevPos.y);
//point(this.pos.x, this.pos.y);
this.updatePrev();
};
this.updatePrev = function() {
this.prevPos.x = this.pos.x;
this.prevPos.y = this.pos.y;
}
this.edges = function() {
if (this.pos.x > p.width) {
this.pos.x = 0;
this.updatePrev();
}
if (this.pos.x < 0) {
this.pos.x = p.width;
this.updatePrev();
}
if (this.pos.y > p.height) {
this.pos.y = 0;
this.updatePrev();
}
if (this.pos.y < 0) {
this.pos.y = p.height;
this.updatePrev();
}
}
}