-
Notifications
You must be signed in to change notification settings - Fork 1
/
sim.js
138 lines (128 loc) · 3.49 KB
/
sim.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
function setup () {
canvas = document.getElementById("my-canvas");
ctx = canvas.getContext("2d");
bodies = [];
particles = [];
bodies.push(new Body([500, 300], 1000));
G = 10;
dt = 0.1;
scrollX = 0;
scrollY = 0;
document.addEventListener("keydown", function (e) {
switch (e.keyCode) {
case 37:
scrollX -= 10;
break;
case 38:
scrollY -= 10;
break;
case 40:
scrollX += 10;
break;
case 39:
scrollY += 10;
break;
}
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, 1000, 1000);
});
document.addEventListener("mousedown", startDrag);
document.addEventListener("mouseup", setAim);
running = true;
loop();
}
function startDrag (e) {
x1 = e.pageX;
y1 = e.pageY;
// console.log('startDrag');
}
function setAim (e) {
var x2 = e.pageX;
var y2 = e.pageY;
var Dx = x1 - x2;
var Dy = y1 - y2;
var particle = new Particle([x1, y1], [-Dx/30, -Dy/30], 100);
particles.push(particle);
running = true;
// document.removeEventListener("mousedown", startDrag);
// document.removeEventListener("mouseup", setAim);
}
class Body {
constructor (Pos, M, colour='black') {
this.x = Pos[0];
this.y = Pos[1];
this.M = M;
this.colour = colour;
this.shape = new Path2D();
// this.shape.moveTo(this.x, this.y);
this.shape.arc(this.x, this.y, Math.sqrt(this.M), 0, 2*Math.PI);
}
render () {
ctx.fillStyle = this.colour;
ctx.fill(this.shape);
}
}
class Particle extends Body {
constructor (Pos, Vel, M, colour='red') {
super(Pos, M, colour);
this.Vx = Vel[0];
this.Vy = Vel[1];
this.Ax = 0;
this.Ay = 0;
this.Ax0 = 0;
this.Ay0 = 0;
this.colour = colour;
}
}
function loop() {
if(running) {
physics();
draw();
}
requestAnimationFrame(loop);
}
function physics() {
for(i = 0; i < particles.length; i++) {
particles[i].Ax0 = particles[i].Ax;
particles[i].Ay0 = particles[i].Ay;
particles[i].Ax = 0;
particles[i].Ay = 0;
for ( j = 0; j < bodies.length; j++) {
var A = calcAcc(particles[i], bodies[j]);
particles[i].Ax += A[0];
particles[i].Ay += A[1];
}
for ( j = 0; j < particles.length; j++) {
if(i == j) continue;
var A = calcAcc(particles[i], particles[j]);
particles[i].Ax += A[0];
particles[i].Ay += A[1];
}
particles[i].x += particles[i].Vx * dt + 0.5 * particles[i].Ax0 * dt**2;
particles[i].Vx += 0.5 * (particles[i].Ax0 + particles[i].Ax) * dt;
particles[i].y += particles[i].Vy * dt + 0.5 * particles[i].Ay0 * dt**2;
particles[i].Vy += 0.5 * (particles[i].Ay0 + particles[i].Ay) * dt;
}
}
function draw() {
for(i = 0; i < bodies.length; i++) {
bodies[i].render();
}
for ( j = 0; j < particles.length; j++) {
ctx.fillStyle = particles[j].colour;
ctx.fillRect(particles[j].x - 1 -scrollX, particles[j].y - 1 - scrollY, 2, 2);
}
}
function calcAcc(particle, body) {
var Dx = particle.x - body.x;
var Dy = particle.y - body.y;
var r = Math.sqrt(Dx**2 + Dy**2);
var theta = Math.atan(Dy/Dx);
var F = G * body.M * particle.M/ r**2;
var a = F / particle.M;
var ax = Math.abs(a * Math.sin(Math.PI/2 - theta));
var ay = Math.abs(a * Math.cos(Math.PI/2 - theta));
if (Dx > 0) ax *= -1;
if (Dy > 0) ay *= -1;
return [ax, ay];
}