-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
81 lines (73 loc) · 1.75 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
let width = 1000;
let height = 550;
let true_G = 5.9229 * (10 ** -5);
var star;
var c_planet;
var planets = [];
var planet_num;
var star_num;
var distv;
var dist;
var dir = [];
var grav;
function one_star_setup() {
star = new Star();
planets.push(star);
}
function two_star_setup() {
let stardist = 150;
let starvel = 3.3;
star1 = new Star();
star1.pos = [width / 2 - stardist, height / 2];
star1.vel = [0, starvel];
star2 = new Star();
star2.pos = [width / 2 + stardist, height / 2];
star2.vel = [0, -starvel];
planets.push(star1);
planets.push(star2);
}
function setup() {
planet_slider();
grav_slider();
star_slider();
createCanvas(width, height).position(5, 5);
for (var i = 0; i < planet_num; i++) {
planets[i] = new Planet();
}
c_planet = new Control();
planets.push(c_planet);
if (star_num == 1) {
one_star_setup();
} else {
two_star_setup();
}
}
function draw() {
background(10);
gravity_update();
gnum();
pnum();
snum();
}
function gravity_update() {
for (var i = planets.length - 1; i >= 0; i--) {
for (var j = planets.length - 1; j >= 0; j--) {
distv = [planets[j].pos[0] - planets[i].pos[0], planets[j].pos[1] - planets[i].pos[1]];
dist = sqrt((distv[0] ** 2) + (distv[1] ** 2)).toFixed(5);
if (dist > 1) {
dir = [distv[0] / dist, distv[1] / dist];
grav = (true_G * G_mult) * planets[j].mass / (dist ** 2);
planets[i].vel[0] += dir[0] * grav;
planets[i].vel[1] += dir[1] * grav;
}
}
planets[i].move();
planets[i].draw();
if (planets[i].pos[0] < -500 || planets[i].pos[0] > 1500) {
planets.splice(i, 1);
}
//if (planets[i].pos[1] < -500 || planets[i].pos[1] > 1500) {
// planets.splice(i, 1);
//}
}
}