16) Reflection of a reflection
Just some happy bouncing particles. Woot.
let gui;
let params = {
particleCount: 1,
particleCountMin: 0,
particleCountMax: 500,
wallCount: 2,
wallCountMin: 0,
wallCountMax: 50,
updatesPerFrame: 10,
updatesPerFrameMin: 1,
updatesPerFrameMax: 50,
drawMode: [
'solid',
'fade',
'clear',
],
thickness: 5,
thicknessMin: 1,
thicknessMax: 10,
rainbow: true,
drawWalls: true,
}
let particles;
let walls;
function setup() {
createCanvas(400, 400);
colorMode(HSL, 100);
gui = createGuiPanel('params');
gui.addObject(params);
gui.setPosition(420, 0);
particles = [];
walls = [];
walls.push({
point1: createVector(0, 0),
point2: createVector(0, height),
});
walls.push({
point1: createVector(0, height),
point2: createVector(width, height),
});
walls.push({
point1: createVector(width, height),
point2: createVector(width, 0),
});
walls.push({
point1: createVector(width, 0),
point2: createVector(0, 0),
});
}
function draw() {
if (params.drawMode == 'clear') {
background(255);
} else if (params.drawMode == 'fade') {
background(255, 255, 255, 16);
}
updateToParams();
doRainbow();
for (let i = 0; i < params.updatesPerFrame; i++) {
moveParticles();
drawParticles();
}
if (params.drawWalls) {
drawWalls();
}
}
function updateToParams() {
while (particles.length < params.particleCount) {
console.log('adding point');
particles.push({
color: [
parseInt(random(100)),
50 + parseInt(random(50)),
50 + parseInt(random(50)),
100
],
position: createVector(
random(width),
random(height)
),
velocity: createVector(
2.0 * random() - 1.0,
2.0 * random() - 1.0
)
});
}
while (particles.length > params.particleCount) {
console.log('removing point');
particles.pop();
}
while (walls.length - 4 < params.wallCount) {
console.log('adding wall');
walls.push({
point1: createVector(
random(width),
random(height)
),
point2: createVector(
random(width),
random(height)
)
});
}
while (walls.length - 4 > params.wallCount) {
console.log('removing wall');
walls.pop();
}
}
function moveParticles() {
for (let particle of particles) {
let newPosition = p5.Vector.add(
particle.position,
particle.velocity
);
// Bounce off walls
for (let wall of walls) {
// Hit a wall
let d1 = pointPointDistance(newPosition, wall.point1);
let d2 = pointPointDistance(newPosition, wall.point2);
let dw = pointPointDistance(wall.point1, wall.point2);
let buffer = 0.1;
if (d1 + d2 >= dw - buffer && d1 + d2 <= dw + buffer) {
newPosition.sub(particle.velocity);
let wallV = p5.Vector.sub(wall.point2, wall.point1);
wallV.rotate(HALF_PI);
particle.velocity.reflect(wallV);
newPosition.add(particle.velocity);
}
}
particle.position.set(newPosition);
}
}
function pointLineDistance(p1, p2, p0) {
return (
((p2.x - p1.x) * (p1.y - p0.y) - (p1.x - p0.x) * (p2.y - p1.y))
/
sqrt((p2.x - p1.x) ** 2 + (p2.y - p1.y) ** 2)
);
}
function pointPointDistance(p1, p2) {
return dist(p1.x, p1.y, p2.x, p2.y);
}
function doRainbow() {
if (params.rainbow) {
for (let particle of particles) {
particle.color[0] = (particle.color[0] + random()) % 100;
}
}
}
function drawParticles() {
for (let particle of particles) {
noStroke();
fill(particle.color);
circle(particle.position.x, particle.position.y, params.thickness);
}
}
function drawWalls() {
for (let wall of walls) {
stroke("black");
strokeWeight(params.thickness);
noFill();
line(wall.point1.x, wall.point1.y, wall.point2.x, wall.point2.y);
}
}
Posts in Genuary 2023:
- Genuary 2023.01: Perfect loop
- Genuary 2023.02: Made in 10 minutes
- Genuary 2023.03: Glitch art
- Genuary 2023.04: Intersections
- Genuary 2023.05: Debug view
- Genuary 2023.06: Steal like an artist
- Genuary 2023.07: Sample a color palette
- Genuary 2023.08: Signed Distance Functions
- Genuary 2023.09: Plants
- Genuary 2023.10: Generative Music
- Genuary 2023.11: Suprematism
- Genuary 2023.12: Tessellation
- Genuary 2023.13: Something you've always wanted to learn
- Genuary 2023.14: Asemic Writing
- Genuary 2023.15: Sine Waves
- Genuary 2023.16: Reflections of a Reflection
- Genuary 2023.17: A grid inside a grid inside a grid
- Genuary 2023.18: Definitely not a grid
- Genuary 2023.19: Black and white
- Genuary 2023.20: Art Deco
- Genuary 2023.21: Persian Carpet
- Genuary 2023.22: Shadows
- Genuary 2023.23: Moiré
- Genuary 2023.24: Textile
- Genuary 2023.25: Yayoi Kusama
- Genuary 2023.26: My kid could have made that
- Genuary 2023.27: In the style of Hilma Af Klint
- Genuary 2023.28: Generative poetry
- Genuary 2023.29: Maximalism
- Genuary 2023.30: Minimalism
- Genuary 2023.31: Break a previous image