-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolarSystem.html
241 lines (187 loc) · 6.63 KB
/
SolarSystem.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<html>
<style>
.html_orbit {
position: absolute;
border: dotted white 2px;
border-radius: 50%;
}
#html_sun {
width: 50px;
height: 50px;
position: absolute;
background-color: yellow;
border-radius: 50%;
left: 375px;
top: 475px;
}
#html_mercury {
width: 20px;
height: 20px;
position: absolute;
background-color: blue;
border-radius: 50%;
left: 200px;
top: 500px;
}
#html_venus {
width: 20px;
height: 20px;
position: absolute;
background-color: blue;
border-radius: 50%;
left: 200px;
top: 500px;
}
#html_earth {
width: 20px;
height: 20px;
position: absolute;
background-color: blue;
border-radius: 50%;
left: 200px;
top: 500px;
}
#html_mars {
width: 20px;
height: 20px;
position: absolute;
background-color: blue;
border-radius: 50%;
left: 200px;
top: 500px;
}
#html_jupiter {
width: 20px;
height: 20px;
position: absolute;
background-color: blue;
border-radius: 50%;
left: 200px;
top: 500px;
}
</style>
<body style="background-color:black;">
<a>The Smith System</a>
<p>
<button onclick="start()">Start</button>
</p>
<a style="font-size:2em;color:red" id="time"></a> <br />
<a style="font-size:2em;color:red" id="distance"></a> <br />
<a style="font-size:2em;color:red" id="speed"></a>
<div>
<div id="html_sun"></div>
<div id="html_mercury"></div>
<div id="html_venus"></div>
<div id="html_earth"></div>
<div id="html_mars"></div>
<div id="html_jupiter"></div>
<div id="html_mercury_orbit" class="html_orbit"></div>
<div id="html_venus_orbit" class="html_orbit"></div>
<div id="html_earth_orbit" class="html_orbit"></div>
<div id="html_mars_orbit" class="html_orbit"></div>
<div id="html_jupiter_orbit" class="html_orbit"></div>
</div>
<script>
function make_vector(x_in, y_in) {
let vector = {
x: x_in,
y: y_in,
add(v2) {
return make_vector(this.x + v2.x, this.y + v2.y);
},
sub(v2) {
return make_vector(this.x - v2.x, this.y - v2.y);
},
sm(scalar) {
return make_vector(this.x * scalar, this.y * scalar);
},
mag() {
return Math.sqrt(Math.pow(this.x, 2) + Math.pow(this.y, 2));
}
};
return vector;
}
// Gravitational Const.
const G = 2000.0;
let sun = {
mass: 1.0,
r: make_vector(400.0, 500.0),
v: make_vector(0, 0)
};
// AU -> px
let au = 200.0;
function make_planet(planet_name, orbital_r) {
// Orbital Radius
let distance = orbital_r * au;
// Mass (1<=m<=10)
let m = Math.random() * 0.9 + 0.1;
// Initial Position
let r0 = make_vector(sun.r.x + distance, sun.r.y);
// Position relative to sun
let del0 = make_vector(r0.x - sun.r.x, r0.y - sun.r.y)
// Initial velocity of planet
let v0 = make_vector(0.0, Math.sqrt(G * sun.mass / del0.mag()));
// Define planet object
let planet = {
name: planet_name,
orbital_r: distance,
mass: m,
r: r0,
v: v0
};
// Returns planet object
return planet;
}
let p_mercury = make_planet("Mercury", 0.21);
let p_venus = make_planet("Venus", 0.72);
let p_earth = make_planet("Earth", 1.00);
let p_mars = make_planet("Mars", 1.52);
let p_jupiter = make_planet("Jupiter", 5.20);
function start() {
let html_mercury = document.getElementById("html_mercury");
let html_venus = document.getElementById("html_venus");
let html_earth = document.getElementById("html_earth");
let html_mars = document.getElementById("html_mars");
let html_jupiter = document.getElementById("html_jupiter");
let html_mercury_orbit = document.getElementById("html_mercury_orbit");
let html_venus_orbit = document.getElementById("html_venus_orbit");
let html_earth_orbit = document.getElementById("html_earth_orbit");
let html_mars_orbit = document.getElementById("html_mars_orbit");
let html_jupiter_orbit = document.getElementById("html_jupiter_orbit");
// Time
let t = 0;
let dt = 0.1;
// Timer (ms)
let id1 = setInterval(function () {
frame(p_mercury, html_mercury, html_mercury_orbit);
frame(p_venus, html_venus, html_venus_orbit);
frame(p_earth, html_earth, html_earth_orbit);
frame(p_mars, html_mars, html_mars_orbit);
frame(p_jupiter, html_jupiter, html_jupiter_orbit);
}, 40); // 40ms = 25 fps
function frame(planet, html_planet, html_planet_orbit) {
// Draw orbital path
html_planet_orbit.style.width = 2 * planet.orbital_r;
html_planet_orbit.style.height = 2 * planet.orbital_r;
html_planet_orbit.style.left = sun.r.x - planet.orbital_r;
html_planet_orbit.style.top = sun.r.y - planet.orbital_r;
// Increment time
t = t + dt;
document.getElementById("time").innerHTML = "t: " + Math.round(t);
// Position relative to sun
let del = make_vector(planet.r.x - sun.r.x, planet.r.y - sun.r.y);
let r = del.mag();
// Newtonian Gravity
let Fg = -G * (sun.mass * planet.mass) / Math.pow(r, 2);
let a = make_vector((Fg / planet.mass) * (del.x / r), (Fg / planet.mass) * (del.y / r));
// Update velocity
planet.v = planet.v.add(a.sm(dt));
// Update position
planet.r = planet.r.add(planet.v.sm(dt));
html_planet.style.left = Math.round(planet.r.x - 10.0) + 'px';
html_planet.style.top = Math.round(planet.r.y - 10.0) + 'px';
}
}
</script>
</body>
</html>