-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
182 lines (152 loc) · 3.63 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
let colours = ["red", "blue", "green", "yellow"];
let population;
let dt = 0.1;
let F = 10;
let B = 0;
let Cwidth = 400;
let Cheight = 400;
let Cdepth = 400;
let maxV = 10;
function setup() {
createCanvas(Cwidth + 10, Cheight + 10, WEBGL);
population = new popu();
population.addParticles(300)
cam = createCamera();
cam.setPosition(400, 600, 600);
cam.lookAt(Cwidth / 2 + 5, Cheight / 2 + 5, Cdepth / 2 + 5);
}
function draw() {
background(220);
specularColor(255, 255, 255);
ambientLight(150);
pointLight(50, 50, 50, 0, 0, 0);
orbitControl();
//population.addParticles(1)
population.run()
population.render();
push();
noFill();
strokeWeight(3);
translate(Cwidth / 2 - 5, Cheight / 2 - 5, Cdepth / 2 - 5)
box(Cwidth + 15, Cheight + 15, Cdepth + 15);
pop();
}
class popu {
constructor() {
this.particles = [];
}
run() {
for (let p = 0; p < this.particles.length; p++) {
this.particles[p].run(this.particles);
}
}
render() {
for (let p = 0; p < this.particles.length; p++) {
this.particles[p].render();
}
}
addParticles(n) {
for (let i = 0; i < n; i++) {
this.particles.push(new particle(random(colours)));
}
}
}
class particle {
constructor(colour) {
this.pos = createVector(random(-200, 200), random(-200, 200), random(-200, 200));
this.vel = createVector(0, 0, 0);
this.colour = colour;
if (colour == "red") {
this.kr = -1;
this.kg = 2;
this.kb = -1;
this.ky = -2;
} else if (colour == "green") {
this.kr = -2;
this.kg = 2;
this.kb = 2;
this.ky = 0;
} else if (colour == "blue") {
this.kr = 1;
this.kg = -1;
this.kb = 3;
this.ky = 1;
} else if (colour == "yellow") {
this.kr = 1;
this.kg = -1;
this.kb = 3;
this.ky = 2;
}
}
getForceTo(that) {
let k;
if (that.colour == "red") {
k = this.kr;
} else if (that.colour == "green") {
k = this.kg;
} else if (that.colour == "blue") {
k = this.kb;
} else if (that.colour == "yellow") {
k = this.ky;
}
let between = p5.Vector.sub(this.pos, that.pos);
let dist = between.mag();
let mag;
if (dist < 10) {
mag = 4000 * sin((dist / 10) * PI);
} else if (dist < 60) {
mag = -k * (dist - 10);
} else if (dist < 100)
mag = k * (dist - 60) - (k * 50);
else {
mag = 0;
}
between.normalize();
return between.mult(mag);
}
run(all) {
let total = createVector(0, 0, 0);
for (let p = 0; p < all.length; p++) {
total.add(this.getForceTo(all[p]));
}
let drag = p5.Vector.mult(this.vel, this.vel.mag() ^ 2)
total.sub(drag.mult(F));
let brown = p5.Vector.random3D()
total.add(brown.mult(B));
this.vel.add(total);
this.vel.limit(maxV);
this.pos.add(this.vel.mult(dt));
if (this.pos.x > Cwidth / 2) {
this.pos.x = Cwidth / 2;
this.vel.x = 0;
}
if (this.pos.x < -Cwidth / 2) {
this.pos.x = -Cwidth / 2;
this.vel.x = 0;
}
if (this.pos.y > Cheight / 2) {
this.pos.y = Cheight / 2;
this.vel.y = 0;
}
if (this.pos.y < -Cheight / 2) {
this.pos.y = -Cheight / 2;
this.vel.y = 0;
}
if (this.pos.z > Cdepth / 2) {
this.pos.z = Cdepth / 2;
this.vel.z = 0;
}
if (this.pos.z < -Cdepth / 2) {
this.pos.z = -Cdepth / 2;
this.vel.z = 0;
}
}
render() {
specularMaterial(this.colour);
strokeWeight(0);
push();
translate(this.pos.x + Cwidth / 2 + 5, this.pos.y + Cheight / 2 + 5, this.pos.z + Cdepth / 2 + 5)
sphere(3);
pop();
}
}