class Particle {
constructor(params) {
this.acceleration = p5Instance.createVector(0, 0);
this.velocity = P5.Vector.random2D();
this.position = p5Instance.createVector(
Math.random() * width,
Math.random() * height
);
this.colRowIndex = [0, 0];
}
queryCurrentAngleViaCurrentColRowIndex(flowfield) {
this.colRowIndex =
flowfield[roundAbs(this.position.x / size)][
roundAbs(this.position.y / size)
];
}
drivenByForce(context) {
let angle = this.colRowIndex[0];
this.acceleration.add(
p5Instance.createVector(
step_length * Math.cos(angle),
step_length * Math.sin(angle)
)
);
this.velocity.add(this.acceleration);
this.position.add(this.velocity);
this.velocity.limit(2);
this.acceleration.setMag(0);
context.fillStyle = `hsla(50%, 50%, 50%, 1)`;
context.fillRect(this.position.x, this.position.y, 2, 2);
}
goBackToField() {
if (this.position.x > width) {
this.position.x = 0;
} else if (this.position.x < -size) {
this.position.x = width - 1;
}
if (this.position.y > height) {
this.position.y = 0;
} else if (this.position.y < -size) {
this.position.y = height - 1;
}
}
}