-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
109 lines (95 loc) · 1.94 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
const mapWidth = 1050;
const mapHeight = 900;
const celestialBodies = [];
let startButton, stopButton;
let sliders = [];
let running = false;
// Receives data update from index.js component
// Data is object with shape of celestialBody class
const updateBody = (celestialBody, i) => {
const body = celestialBodies[i];
body.pos.x = celestialBody.x;
body.pos.y = celestialBody.y;
body.mass = celestialBody.mass;
body.size = celestialBody.size;
body.vel.x = celestialBody.xVel;
body.vel.y = celestialBody.yVel;
body.color = celestialBody.color;
body.stationary = celestialBody.stationary;
if (!running) {
for (let body of celestialBodies) {
body.resetSimulatedPath();
}
}
};
// Delets a celestialBody from the array
const deleteBody = i => {
celestialBodies.splice(i, 1);
}
// Function to start or stop simulation
const setSim = (status) => {
running = status;
if (!status) {
for (let body of celestialBodies) {
body.resetSimulatedPath();
}
}
};
// p5js setup
function setup() {
const canvas = createCanvas(mapWidth, mapHeight);
canvas.parent('canvas-container');
celestialBodies.push(
new CelestialBody(
width / 2,
height / 2 - 50,
1.989 * Math.pow(10, 26),
25,
0,
0,
'#FFF700',
false
)
); // sun
celestialBodies.push(
new CelestialBody(
200,
height / 2 - 50,
3 * Math.pow(10, 22),
20,
0,
5,
'#F08C00',
false
)
);
celestialBodies.push(
new CelestialBody(
339,
height / 2 - 50,
3 * Math.pow(10, 22),
10,
0,
7,
'#00FFB7',
false
)
);
}
function draw() {
background(20);
//frameRate(30);
//noLoop();
if (running) {
for (let body of celestialBodies) {
body.update();
}
} else {
for (let body of celestialBodies) {
body.simulatePath();
}
}
for (let body of celestialBodies) {
body.show();
}
}