-
Notifications
You must be signed in to change notification settings - Fork 0
/
gravitational_attraction.html
182 lines (148 loc) · 5.79 KB
/
gravitational_attraction.html
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
<!DOCTYPE html>
<html>
<head>
<script language="javascript" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.1.9/p5.js"></script>
</head>
<body>
<script>
let balls = [];
let numBalls = 100;
let attractor;
let vel = 10;
function setup() {
createCanvas(800, 600);
for (let i = 0; i < numBalls; i++) {
balls[i] = new Ball(random(width), random(height), 3, random(-vel, vel), random(-vel, vel));
}
attractor = new Attractor(width / 2, height / 2, 100);
}
function draw() {
background(0);
for (let [i, b] of balls.entries()) {
b.update();
b.paths();
b.show();
attractor.attract(b);
for (let i = 0; i < b.path.length; i++) {
if (b.path.length > 1000) {
b.path.splice(0, 1);
}
noFill();
strokeWeight(2);
stroke(b.color, 50);
point(b.path[i].x, b.path[i].y);
}
attractor.remove(b, i);
if (balls.length >= 1) {
for (let [im, m] of balls.entries()) {
if (dist(b.pos.x, b.pos.y, m.pos.x, m.pos.y) < b.r + m.r && i != im) {
let ax = (b.pos.x + m.pos.x) / 2;
let ay = (b.pos.y + m.pos.y) / 2;
let am = (b.mass + m.mass) * 0.75;
// let avx = (b.vel.x + m.vel.x) / 2;
// let avy = (b.vel.y + m.vel.y) / 2;
let av = b.vel;
av.add(m.vel);
// let avx = 0;
// let avy = 0;
// b.vel.x = 0;
// b.vel.y = 0;
// m.vel.x = 0;
// m.vel.y = 0;
balls.splice(i, 1);
if (i < im) {
balls.splice(im - 1, 1);
} else {
balls.splice(im, 1);
}
balls.push(new Ball(ax, ay, am, av.x, av.y));
break;
}
}
}
}
attractor.show();
}
function mouseClicked() {
balls.push(new Ball(mouseX, mouseY, 10, random(-5, 5), random(-5, 5)));
// balls.push(new Ball(mouseX, mouseY, 10, 2, 2));
}
class Ball {
constructor(x, y, m, vx, vy) {
this.pos = createVector(x, y);
this.vel = createVector(vx, vy);
this.acc = createVector(0, 0);
this.mass = m;
this.r = sqrt(this.mass) * 2;
this.color = color(random(100, 255), random(100, 255), random(100, 255));
this.path = [];
}
applyForce(force) {
let f = p5.Vector.div(force, this.mass);
this.drawArrow('#66ff33', 30, f);
this.acc.add(f);
}
drawArrow(showColor, size, f) {
if (f.x != 0 || f.y != 0) {
push();
stroke(color(showColor));
fill(color(showColor));
translate(this.pos.x, this.pos.y);
line(0, 0, f.x * size, f.y * size);
rotate(f.heading());
translate(f.mag() * size, 0);
let arrowSize = 7;
triangle(0, arrowSize / 2, 0, -arrowSize / 2, arrowSize, 0);
pop();
}
}
update() {
this.vel.add(this.acc);
this.pos.add(this.vel);
this.acc.set(0, 0);
if (this.vel.x !== 0 || this.vel.y !== 0 && this.vel.y !== 1) {
this.drawArrow('#ffff33', 5, this.vel);
}
}
paths() {
this.path.push({
x: this.pos.x,
y: this.pos.y
});
}
show() {
stroke(255);
strokeWeight(1);
fill(this.color, 80);
ellipse(this.pos.x, this.pos.y, this.r * 2);
}
}
class Attractor {
constructor(x, y, m) {
this.pos = createVector(x, y);
this.mass = m;
this.r = sqrt(this.mass) * 2;
}
attract(ball) {
let force = p5.Vector.sub(this.pos, ball.pos);
let distanceSq = constrain(force.magSq(), 100, 10000);
let G = 5;
let strength = G * (this.mass * ball.mass) / distanceSq;
force.setMag(strength);
ball.applyForce(force);
}
remove(ball, id) {
if (dist(this.pos.x, this.pos.y, ball.pos.x, ball.pos.y) < this.r) {
balls.splice(id, 1);
this.mass += ball.mass;
this.r = sqrt(this.mass) * 2;
}
}
show() {
noStroke();
fill(255, 0, 100);
ellipse(this.pos.x, this.pos.y, this.r * 2);
}
}
</script>
</body></html>