-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhysics2rand.html
More file actions
279 lines (257 loc) · 9.46 KB
/
Physics2rand.html
File metadata and controls
279 lines (257 loc) · 9.46 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Physics sim</title>
<!-- this is the code that will allow icon to be visible in the title bar of the brower -->
<link rel="icon" type="image/ico" href="WebsiteLogo2.png" />
<!-- you can also say this: <link rel="icon" type="image/png" href="location/image.png" /> -->
<style>
* {
PADDING: 0;
MARGIN: 0;
}
canvas {
background: #eee;
display: block;
margin: 0 auto;
}
</style>
<link rel="stylesheet" href="./bootstrap.min.css">
<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML' type='text/javascript' async ></script>
</head>
<body>
<h1 style="text-align:center">Strange Orbits</h1>
<h4>This is not an accurate solar system, but the gravitational force is accurate. This is my Solar system project, just with random
velocities given to the planets. Every time you refresh the page, they will be given new random velocities. The display showing
the force on Earth is accurate.</h4>
<canvas id="myCanvas" width="1200" height="600"></canvas>
<script>
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
const ImgPlanRad = 3;
var earth = {
Radius: ImgPlanRad,
Mass: 5.973e24,
x: 1.496e11,
y: 0,
vX: 0,
vY: 3.0e4 * Math.random(),
force: 0,
forceX: 0,
forceY: 0
};
var venus = {
Radius: ImgPlanRad,
Mass: 4.867e24,
x: 1.0820893e11,
y: 0,
vX: 0,
vY: 3.5e4 * Math.random()
};
var mars = {
Radius: ImgPlanRad,
Mass: 6.39e23,
x: 2.279e11,
y: 0,
vX: 0,
vY: 2.4e4 * Math.random()
};
var mercury = {
Radius: ImgPlanRad,
Mass: 3.3022e23,
x: 5.7e10,
y: 0,
vX: 0,
vY: 4.7872e4 * Math.random()
};
//Mass measured in km, x in m, and velocity is m/s
var ballRadius = 2;
var x = 0;
var y = 0;
var vX = 0;
var vY = 0;
let day = 0; // for debug
//"Planet" = THE SUN
const PlanetRadius = 10;
const Gravity = 6.67408e-11 /*meters cubed, divided by KgS^-2*/;
const PlanetMass = 1.989e30;
//const BallMass = 5.973e24;
var BallMass = 1;
const TimeInt = 3600 * 6;
//86400 = 1 earth day
const PlanetX = 0;
const PlanetY = 0;
//y and x are ball Positions
//Planet Position is (600, 300)!
function distance(x, y, SunX, SunY) {
return Math.sqrt(Math.pow(SunY - y, 2) + Math.pow(SunX - x, 2));
}
function forceX(ballX, ballY, SunX, SunY) {
let d = distance(ballX, ballY, SunX, SunY);
let f = force(BallMass, PlanetMass, Gravity, x, y, SunX, SunY);
if(PlanetNum == 3){
earth.forceX = f * ((SunX - ballX) / d);
}
return f * ((SunX - ballX) / d);
}
function forceY(ballX, ballY, SunX, SunY) {
let d = distance(ballX, ballY, SunX, SunY);
let f = force(BallMass, PlanetMass, Gravity, x, y, SunX, SunY);
if(PlanetNum == 3){
earth.forceY = f * ((SunY - ballY) / d);
}
return f * ((SunY - ballY) / d);
}
function force(BallMass, PlanetMass, Gravity, x, y, SunX, SunY) {
let d = distance(x, y, SunX, SunY);
if(PlanetNum == 3){
earth.force = (BallMass * PlanetMass * Gravity) / Math.pow(d, 2);
}
return (BallMass * PlanetMass * Gravity) / Math.pow(d, 2);
}
var xPos;
var yPos;
var accelerationX;
var accelerationY;
function gravity() {
accelerationX = forceX(x, y, PlanetX, PlanetY) / BallMass;
accelerationY = forceY(x, y, PlanetX, PlanetY) / BallMass;
if(PlanetNum == 3){
EarthV();
} else if(PlanetNum == 2){
VenusV();
} else if(PlanetNum == 1){
MercuryV();
} else if(PlanetNum == 4){
MarsV();
} else if(PlanetNum == 5){
}
day = day + 1;
}
function EarthV(){
earth.vX = earth.vX + accelerationX * TimeInt;
earth.vY = earth.vY + accelerationY * TimeInt;
}
function VenusV(){
venus.vX = venus.vX + accelerationX * TimeInt;
venus.vY = venus.vY + accelerationY * TimeInt;
}
function MarsV(){
mars.vX = mars.vX + accelerationX * TimeInt;
mars.vY = mars.vY + accelerationY * TimeInt;
}
function MercuryV(){
mercury.vX = mercury.vX + accelerationX * TimeInt;
mercury.vY = mercury.vY + accelerationY * TimeInt;
}
function solarToCanvasX(px){
px = px / 0.8e9;
return px = px + 600;
}
function solarToCanvasY(py){
py = py / 0.8e9;
return py = py + 300;
}
function drawBalls() {
//Earth
xPos = solarToCanvasX(earth.x);
yPos = solarToCanvasY(earth.y);
ctx.beginPath();
ctx.arc(xPos, yPos, earth.Radius, 0, Math.PI * 2);
ctx.fillStyle = "DarkBlue";
ctx.fill();
ctx.closePath();
//Venus
xPos = solarToCanvasX(venus.x);
yPos = solarToCanvasY(venus.y);
ctx.beginPath();
ctx.arc(xPos, yPos, venus.Radius, 0, Math.PI * 2);
ctx.fillStyle = "Coral";
ctx.fill();
ctx.closePath();
//Mars
xPos = solarToCanvasX(mars.x);
yPos = solarToCanvasY(mars.y);
ctx.beginPath();
ctx.arc(xPos, yPos, mars.Radius, 0, Math.PI * 2);
ctx.fillStyle = "Maroon";
ctx.fill();
ctx.closePath();
//Mercury
xPos = solarToCanvasX(mercury.x);
yPos = solarToCanvasY(mercury.y);
ctx.beginPath();
ctx.arc(xPos, yPos, mars.Radius, 0, Math.PI * 2);
ctx.fillStyle = "LightSlateGrey";
ctx.fill();
ctx.closePath();
//Sun
ctx.beginPath();
ctx.arc(600, 300, 10, 0, Math.PI * 2);
ctx.fillStyle = "Yellow";
ctx.fill();
ctx.closePath();
}
var PlanetNum = 0;
function draw() {
ctx.clearRect(0, 0, 1200, 600);
drawBalls();
ctx.textBaseline = "top";
ctx.font = "30px Consolas";
ctx.fillStyle = "SeaGreen";
ctx.fillText("Force on Earth", 0, 500);
ctx.font = "20px Consolas";
ctx.fillText("Total Force: " + earth.force.toPrecision(3) + "N", 0, 530);
//ctx.fillText("Earth Day: " + day, 1000, 530);
ctx.fillText("Force on X: " + earth.forceX.toPrecision(3) + "N", 0, 550);
ctx.fillText("Force on Y: " + earth.forceY.toPrecision(3) + "N", 0, 570);
ctx.font = "10px Consolas";
//ctx.fillText("All calculations are in Newtons", 0, 590);
//Earth's Gravity
PlanetNum = 3;
BallMass = earth.Mass;
x = earth.x;
y = earth.y;
gravity();
//Earth's Position Change
earth.x = earth.x + earth.vX * TimeInt;
earth.y = earth.y + earth.vY * TimeInt;
//Venus's Gravity
PlanetNum = 2;
BallMass = venus.Mass;
x = venus.x;
y = venus.y;
gravity();
//Venus's Position Change
venus.x = venus.x + venus.vX * TimeInt;
venus.y = venus.y + venus.vY * TimeInt;
//Mercury's Gravity
PlanetNum = 1;
BallMass = mercury.Mass;
x = mercury.x;
y = mercury.y;
gravity();
//Mercury's Position Change
mercury.x = mercury.x + mercury.vX * TimeInt;
mercury.y = mercury.y + mercury.vY * TimeInt;
//Mars's Gravity
PlanetNum = 4;
BallMass = mars.Mass;
x = mars.x;
y = mars.y;
gravity();
//Mars's Position Change
mars.x = mars.x + mars.vX * TimeInt;
mars.y = mars.y + mars.vY * TimeInt;
}
changed = true;
var interval = setInterval(draw, 10);
</script>
<h2 style="text-align:center">How it Works</h2>
<h4 style="text-align:center">See the description for the regular <a href="./Physics2.html">Solar System</a>.</h4>
<h4>While this may seem useless, spacecraft uses a method called Gravity Assist to use less energy and move around the universe quicker. Craft
like the Voyager 1 use the gravity of planets as a speed boost, and slingshot off of them!
</h4>
</body>
</html>