-
Notifications
You must be signed in to change notification settings - Fork 0
/
solar system.js
168 lines (143 loc) · 3.59 KB
/
solar system.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
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
elem = document.getElementById("container");
setCanvas(elem);
w=WIDTH;
h=HEIGHT;
fetch_mouse_pos(elem,'mousemove');
G=10**-3.5 ;
class Particle {
constructor(x,y,m,vx,vy,r) {
this.x=x;
this.y=y;
this.m=m;
this.trail=[];
this.r=r||sqrt(m);
var c=random(0,340);
this.c=`hsl(${c},100%,50%)`;
this.c_trail=`hsla(${c},100%,50%,0.5)`;
this.v={
x:vx,
y:vy
};
this.a={
x:0,
y:0
};
}
calculateForce(other) {
let dx=this.x-other.x;
let dy=this.y-other.y;
let F_gravity = G*this.m*other.m;
let F_x=-F_gravity*dx/sqrt(dx*dx+dy*dy);
let F_y=-F_gravity*dy/sqrt(dx*dx+dy*dy);
return {
x:F_x,
y:F_y
};
}
calculateAcceleration(other) {
let F=this.calculateForce(other);
let a_x=F.x/this.m;
let a_y=F.y/this.m;
return {
x:a_x,
y:a_y
};
}
calculateAccelerationAll(particles) {
this.a.x = 0;
this.a.y = 0;
for(let i=0;i<particles.length;i++){
let other=particles[i];
if(other!=this){
let a_other=this.calculateAcceleration(other);
this.a.x+=a_other.x;
this.a.y+=a_other.y;
}
}
}
//fuse on collision with another particle
fuse(other) {
let dx=this.x-other.x;
let dy=this.y-other.y;
let dist=Math.sqrt(dx*dx+dy*dy);
if(dist<this.r+other.r){
// make new particle
let new_m=this.m+other.m;
let new_x=(this.m*this.x+other.m*other.x)/new_m;
let new_y=(this.m*this.y+other.m*other.y)/new_m;
let new_vx=(this.m*this.v.x+other.m*other.v.x)/new_m;
let new_vy=(this.m*this.v.y+other.m*other.v.y)/new_m;
let new_particle=new Particle(new_x,new_y,new_m,new_vx,new_vy);
// remove old particles
particles.splice(particles.indexOf(this),1);
particles.splice(particles.indexOf(other),1);
// add new particle
particles.push(new_particle);
}
}
// fuse all
fuseAll(particles) {
for(let i=0;i<particles.length;i++){
let other=particles[i];
if(other!=this){
this.fuse(other);
}
}
}
update() {
this.trail.push([this.x,this.y]);
if(this.trail.length>100){
this.trail.shift();
}
this.v.x+=this.a.x;
this.v.y+=this.a.y;
this.x+=this.v.x;
this.y+=this.v.y;
}
show() {
new circle(this.x,this.y,this.r,this.c,0.7,this.c,0);
}
show_trail() {
new polygon(this.trail,this.c,0,this.c_trail,1);
}
}
colors=['#17078B','#4F03A1','#AA2595','#C5417D','#DB5C67','#E97158','#F8973F','#FCB530','#F3EE22']
Fabsmax=0;
Fabsmin=0;
t=0;
particles=[];
//sun
sun=new Particle(w/2,h/2,1000,0,0,50);
particles.push(sun);
for(let i=0;i<8;i++){
let x= w/2+ 80+i*30;
let y= h/2;
let m=0.1;
let vx=0;
let vy=6+i*0.5;
let r=10;
let p=new Particle(x,y,m,vx,vy,r);
particles.push(p);
}
function draw() {
clearCanvas();
for(let i=0;i<particles.length;i++){
let p=particles[i];
p.calculateAccelerationAll(particles);
//p.fuseAll(particles);
p.update();
p.show();
p.show_trail();
}
requestAnimationFrame(draw);
}
function Fbetween(x1,y1,x2,y2,m1,m2) {
r=Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
F={
x:((G*m1*m2)/(r*r*r))*(x2-x1),
y:((G*m1*m2)/(r*r*r))*(y2-y1)
}
return F;
}
///////////////////////////////////////////////////////////////////////////////
draw();