xxxxxxxxxx
let particles = [];
let masonryGrid;
function setup() {
let canvas = createCanvas(windowWidth, windowHeight);
//canvas.parent("sketch-container");
for (let i = 0; i < 5; i++) {
particles.push(new Particle(random(width), random(height)));
}
background(30, 30, 50);
}
function draw() {
for (let particle of particles) {
particle.update();
particle.display();
particle.checkEdges();
particle.checkParticlesCollision(particles);
}
}
class Particle {
constructor(x, y) {
this.pos = createVector(x, y);
this.vel = p5.Vector.random2D().mult(random(1, 13));
this.acc = createVector();
this.size = random(5, 15);
this.color = color(random(100, 255), random(100, 255), random(100, 255));
}
update() {
this.vel.add(this.acc);
this.pos.add(this.vel);
this.acc.mult(0);
}
display() {
noStroke();
fill(this.color);
ellipse(this.pos.x, this.pos.y, this.size);
}
checkEdges() {
if (this.pos.x > width - this.size / 2 || this.pos.x < this.size / 2) {
this.vel.x *= -1;
}
if (this.pos.y > height - this.size / 2 || this.pos.y < this.size / 2) {
this.vel.y *= -1;
}
}
checkParticlesCollision(others) {
for (let other of others) {
if (other !== this) {
let distance = dist(this.pos.x, this.pos.y, other.pos.x, other.pos.y);
if (distance < this.size / 2 + other.size / 2) {
// Collision detected, swap velocities
let temp = this.vel.copy();
this.vel = other.vel.copy();
other.vel = temp;
}
}
}
}
}