Repository navigation
Expand file tree
/
Copy pathsimulation.js
More file actions
336 lines (287 loc) · 6.64 KB
/
Copy pathsimulation.js
File metadata and controls
336 lines (287 loc) · 6.64 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
const G = 1;
let earth;
let planets;
let stars;
let particles;
let bg;
let bgs;
let spaceImg;
let checkBoxArrow;
let checkBoxAttraction;
let checkBoxTracing;
let checkboxDestroying;
let s2p;
let s2s;
let p2p;
let checkboxS2S;
let checkboxS2P;
let checkboxP2P;
let arrow;
let attraction;
let tracing;
let destroy;
let spawn;
let planetsButton;
let starsButton;
let ecc;
function preload() {
spaceImg = loadImage('assets/space.jpg');
}
function setup() {
createCanvas(windowWidth, windowHeight);
attraction = true;
tracing = false;
arrow = true;
destroy = true;
spawn = true;
bg = "gray";
checkBoxAttraction = document.getElementById("attraction");
checkBoxTracing = document.getElementById("tracing");
checkBoxArrow = document.getElementById("arrow");
checkboxDestroying = document.getElementById("destroy");
checkboxS2S = document.getElementById("s2s");
checkboxS2P = document.getElementById("s2p");
checkboxP2P = document.getElementById("p2p");
s2p = true;
s2s = false;
p2p = false;
planets = [];
stars = [];
particles = [];
planetsButton = document.getElementById("planet");
starsButton = document.getElementById("star");
}
function draw() {
translate(width / 2, height / 2);
background(bg);
scale(scaleAmount);
if (attraction) {
// Stars attract planets
if (s2p) {
for (let planet of planets) {
for (let star of stars) {
star.attract(planet);
}
}
}
// Planets attract each other
if (p2p) {
for(let planet of planets) {
for (let other of planets) {
if (planet !== other) {
planet.attract(other);
}
}
}
}
// Stars attact each other
if(s2s) {
for (let star of stars) {
for (let other of stars) {
if (star !== other) {
star.attract(other);
}
}
}
}
}
// Update and draw each Particle
particles.forEach(particle => {
particle.update();
particle.draw();
if(Date.now() - particle.timer > 3000) {
let indexOfParticle = particles.indexOf(particle);
particles.splice(indexOfParticle, 1);
}
});
// Update and draw each planet
planets.forEach(planet => {
planet.update();
planet.draw();
if (tracing) {
planet.drawTrace();
}
if(arrow) {
planet.drawArrow();
}
});
// Update and draw each star
stars.forEach(star => {
star.update();
star.draw();
if(arrow) {
star.drawArrow();
}
});
if (destroy) {
for(let planet of planets) {
// Planets destroy each other
for (let other of planets) {
if (planet !== other) {
let distance = dist(planet.pos.x, planet.pos.y, other.pos.x, other.pos.y);
if (distance <= planet.r + other.r) {
let rand = Math.floor(Math.random() * 50) + 30;
for (let i = 0; i < rand; i++) {
particles.push(new Particle(planet.pos.x, planet.pos.y, planet.color, other.color));
particles.push(new Particle(other.pos.x, other.pos.y, other.color, planet.color));
}
let indexOfPlanet = planets.indexOf(planet);
planets.splice(indexOfPlanet, 1);
let indexOfOther = planets.indexOf(other);
planets.splice(indexOfOther, 1);
}
}
}
// Planets and Stars destroy each other
for (let star of stars) {
let distance = dist(planet.pos.x, planet.pos.y, star.pos.x, star.pos.y);
if (distance <= planet.r + star.r) {
let rand = Math.floor(Math.random() * 50) + 30;
for (let i = 0; i < rand; i++) {
particles.push(new Particle(planet.pos.x + planet.r, planet.pos.y + planet.r, planet.color, star.color));
}
let index = planets.indexOf(planet);
planets.splice(index, 1);
}
}
}
// Stars destroy each other
for (let star of stars) {
for (let other of stars) {
if (star !== other) {
if (destroy) {
let distance = dist(star.pos.x, star.pos.y, other.pos.x, other.pos.y);
if (distance <= star.r + other.r) {
let rand = Math.floor(Math.random() * 50) + 30;
for (let i = 0; i < rand; i++) {
particles.push(new Particle(star.pos.x, star.pos.y, star.color, other.color));
particles.push(new Particle(other.pos.x, other.pos.y, other.color, star.color));
}
let indexOfStar = stars.indexOf(star);
stars.splice(indexOfStar, 1);
let indexOfOther = stars.indexOf(other);
stars.splice(indexOfOther, 1);
}
}
}
}
}
}
}
function mouseClicked() {
if (spawn) {
let pos = createVector(mouseX - width/2, mouseY - height/2).div(scaleAmount);
let vel = createVector(0, 0);
let radios = document.getElementsByName("everyObject");
for (let i = 0; i < radios.length; i++) {
if (radios[i].checked) {
if (allObjects[i] instanceof Planet) {
planets.push(new Planet(pos.x, pos.y, vel.x, vel.y, allObjects[i].mass, allObjects[i].r, allObjects[i].color));
}
else if (allObjects[i] instanceof Star){
stars.push(new Star(pos.x, pos.y, vel.x, vel.y, allObjects[i].mass, allObjects[i].r, allObjects[i].color));
}
}
}
}
}
function toggleCreateObjectScreen() {
let x = document.getElementById("container");
if(x.style.display === "none") {
x.style.display = "flex";
}
else {
x.style.display = "none";
}
}
function changeBackground() {
bgs = document.getElementsByName("fonts");
for (let i = 0; i < bgs.length; i++) {
if (bgs[i].checked) {
if (bgs[i].value === "space") {
bg = spaceImg;
}
else {
bg = bgs[i].value;
}
}
}
}
function destroyObject() {
if (checkboxDestroying.checked == true) {
destroy = true;
} else {
destroy = false;
}
}
function attract() {
if (checkBoxAttraction.checked == true) {
attraction = false;
} else {
attraction = true;
}
}
function arrowShow() {
if (checkBoxArrow.checked == true) {
arrow = true;
}
else {
arrow = false;
}
}
function trace() {
if (checkBoxTracing.checked == true) {
tracing = true;
} else {
tracing = false;
}
}
function orbiting() {
for (let i = 0; i < planets.length; i++) {
// planets[i].startOrbiting(stars[0]);
planets[i].startEllipticOrbit(stars[0]);
}
}
function s2sAttraction() {
if (checkboxS2S.checked == true) {
s2s = true;
} else {
s2s = false;
}
}
function s2pAttraction() {
if (checkboxS2P.checked == true) {
s2p = true;
} else {
s2p = false;
}
}
function p2pAttraction() {
if (checkboxP2P.checked == true) {
p2p = true;
} else {
p2p = false;
}
}
function randomizingPosition() {
for (let i = 0; i < planets.length; i++) {
planets[i].randomizeDir(stars[0]);
}
}
function disableSpawn() {
spawn = false;
}
function enableSpawn() {
spawn = true;
}
function reset() {
planets = [];
stars = [];
particles = [];
}
function youSure() {
return "Are you sure you want to leave this page?";
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
}