
๐ mold.js
class Mold {
constructor() {
this.x = random(width);
this.y = random(height);
this.r = 0.5;
this.heading = random(360);
this.vx = cos(this.heading);
this.vy = sin(this.heading);
this.rotAngle = 45;
this.stop = false;
this.rSensorPos = createVector(0, 0);
this.lSensorPos = createVector(0, 0);
this.fSensorPos = createVector(0, 0);
this.sensorAngle = 35;
this.sensorDist = 10;
}
update() {
if (this.stop) {
this.vx = 0;
this.vy = 0;
} else {
this.vx = cos(this.heading);
this.vy = sin(this.heading);
}
this.x = (this.x + this.vx + width) % width;
this.y = (this.y + this.vy + height) % height;
this.getSensorPos(this.rSensorPos, this.heading + this.sensorAngle);
this.getSensorPos(this.lSensorPos, this.heading - this.sensorAngle);
this.getSensorPos(this.fSensorPos, this.heading);
let index, l, r, f;
index =
4 * (d * floor(this.rSensorPos.y)) * (d * width) +
4 * (d * floor(this.rSensorPos.x));
r = pixels[index];
index =
4 * (d * floor(this.lSensorPos.y)) * (d * width) +
4 * (d * floor(this.lSensorPos.x));
l = pixels[index];
index =
4 * (d * floor(this.fSensorPos.y)) * (d * width) +
4 * (d * floor(this.fSensorPos.x));
f = pixels[index];
if (f > l && f > r) {
this.heading += 0;
} else if (f < l && f < r) {
if (random(1) < 0.5) {
this.heading += this.rotAngle;
} else {
this.heading -= this.rotAngle;
}
} else if (l > r) {
this.heading -= this.rotAngle;
} else if (r > l) {
this.heading += this.rotAngle;
}
}
display() {
noStroke();
fill(255);
ellipse(this.x, this.y, this.r * 2, this.r * 2);
}
getSensorPos(sensor, angle) {
sensor.x = (this.x + this.sensorDist * cos(angle) + width) % width;
sensor.y = (this.y + this.sensorDist * sin(angle) + height) % height;
}
}
๐ script.js
let molds = [];
let num = 4000;
let d;
function setup() {
createCanvas(400, 400);
angleMode(DEGREES);
d = pixelDensity();
for (let i = 0; i < num; i++) {
molds[i] = new Mold();
}
}
function draw() {
background(0, 5);
loadPixels();
for (let i = 0; i < num; i++) {
if (key == "s") {
molds[i].stop = true;
updatePixels();
noLoop();
} else {
molds[i].stop = false;
}
molds[i].update();
molds[i].display();
}
}
1. Mold ๊ฐ์ฒด
- ๊ฐ
Mold๋ ์บ๋ฒ์ค ๋ด ๋๋คํ ์์น์์ ์์ฑ๋ฉ๋๋ค.
heading์ ์ด๋ ๋ฐฉํฅ์ ๋ํ๋ด๋ฉฐ, vx์ vy๋ ํด๋น ๋ฐฉํฅ์ x, y ์ฑ๋ถ์
๋๋ค.
stop ๋ณ์๋ "s" ํค๋ฅผ ๋๋ฅด๋ฉด true๊ฐ ๋์ด ์ด๋์ ๋ฉ์ถฅ๋๋ค.
2. ์ผ์ ์์คํ
Mold๋ ์์ชฝ, ์ผ์ชฝ, ์ค๋ฅธ์ชฝ ์ผ์๋ฅผ ๊ฐ์ง๊ณ ์์ผ๋ฉฐ, ์ผ์๋ ํ์ฌ ๋ฐฉํฅ์ ๊ธฐ์ค์ผ๋ก ยฑ45๋ ์์น์ ์์ต๋๋ค.
- ์ผ์๋ ์ฃผ๋ณ ํฝ์
์ ์์ ๊ฐ์ ์ฝ์ด, ์ฃผ๋ณ ํ๊ฒฝ์ ๋ฐ๋ผ ์ด๋ ๋ฐฉํฅ์ ๊ฒฐ์ ํฉ๋๋ค.
3. ๋ฐฉํฅ ์ ํ ๋ก์ง
- ์์ชฝ ์ผ์ ๊ฐ์ด ๊ฐ์ฅ ํฌ๋ฉด ์ง์งํฉ๋๋ค.
- ์์ชฝ ์ผ์ ๊ฐ์ด ๊ฐ์ฅ ์์ผ๋ฉด ๋๋คํ๊ฒ ์ผ์ชฝ ๋๋ ์ค๋ฅธ์ชฝ์ผ๋ก ํ์ ํฉ๋๋ค.
- ์ผ์ชฝ ์ผ์ ๊ฐ์ด ๋ ํฌ๋ฉด ์ค๋ฅธ์ชฝ์ผ๋ก, ์ค๋ฅธ์ชฝ ์ผ์ ๊ฐ์ด ๋ ํฌ๋ฉด ์ผ์ชฝ์ผ๋ก ํ์ ํฉ๋๋ค.
4. ์บ๋ฒ์ค ๊ฒฝ๊ณ ์ฒ๋ฆฌ
Mold๊ฐ ์บ๋ฒ์ค ๊ฒฝ๊ณ๋ฅผ ๋์ด๊ฐ๋ฉด ๋ฐ๋ํธ์์ ๋ฑ์ฅํ๋๋ก % ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํฉ๋๋ค.