-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
132 lines (117 loc) · 3.1 KB
/
sketch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
let particles = [];
const numParticles = 500;
const noiseScale = 0.01;
let flowField;
let attractor;
function setup() {
createCanvas(windowWidth, windowHeight);
background(0);
flowField = new FlowField(20);
attractor = new Attractor(width / 2, height / 2, 100); // Create an attractor at the center
for (let i = 0; i < numParticles; i++) {
particles.push(new Particle());
}
}
function draw() {
flowField.update();
particles.forEach(particle => {
attractor.attract(particle); // Apply attraction force from the attractor
particle.follow(flowField);
particle.update();
particle.edges();
particle.show();
});
attractor.show(); // Display the attractor
}
class FlowField {
constructor(r) {
this.resolution = r;
this.cols = floor(width / this.resolution);
this.rows = floor(height / this.resolution);
this.field = new Array(this.cols * this.rows);
this.zoff = 0;
}
update() {
let xoff = 0;
for (let i = 0; i < this.cols; i++) {
let yoff = 0;
for (let j = 0; j < this.rows; j++) {
let index = i + j * this.cols;
let angle = noise(xoff, yoff) * TWO_PI * 2;
let v = p5.Vector.fromAngle(angle);
v.setMag(1);
this.field[index] = v;
yoff += noiseScale;
}
xoff += noiseScale;
}
}
}
class Particle {
constructor() {
this.pos = createVector(random(width), random(height));
this.vel = createVector(0, 0);
this.acc = createVector(0, 0);
this.maxSpeed = 2;
this.prevPos = this.pos.copy();
}
follow(flow) {
let x = floor(this.pos.x / flow.resolution);
let y = floor(this.pos.y / flow.resolution);
let index = x + y * flow.cols;
let force = flow.field[index];
this.applyForce(force);
}
applyForce(force) {
this.acc.add(force);
}
update() {
this.vel.add(this.acc);
this.vel.limit(this.maxSpeed);
this.pos.add(this.vel);
if (this.acc.mag() !== 0 && isFinite(this.acc.mag())) {
this.acc.mult(0); // Only multiply if acc is defined and finite
}
}
edges() {
if (this.pos.x > width) this.pos.x = 0;
if (this.pos.x < 0) this.pos.x = width;
if (this.pos.y > height) this.pos.y = 0;
if (this.pos.y < 0) this.pos.y = height;
this.prevPos = this.pos.copy();
}
show() {
stroke(0, random(150, 200), random(200, 250), 30);
strokeWeight(1);
line(this.prevPos.x, this.prevPos.y, this.pos.x, this.pos.y);
this.updatePrev();
}
updatePrev() {
this.prevPos.x = this.pos.x;
this.prevPos.y = this.pos.y;
}
}
class Attractor {
constructor(x, y, m) {
this.pos = createVector(x, y);
this.mass = m;
this.r = sqrt(this.mass) * 2;
}
attract(mover) {
let force = p5.Vector.sub(this.pos, mover.pos);
let distanceSq = constrain(force.magSq(), 100, 1000);
let G = 5;
let strength = G * (this.mass * mover.mass) / distanceSq;
force.setMag(strength);
mover.applyForce(force);
}
show() {
noStroke();
fill(0, 150, 200,10);
ellipse(this.pos.x, this.pos.y, this.r * 1.2);
}
}
function mouseClicked() {
attractor.pos.x = mouseX;
attractor.pos.y = mouseY;
}