-
Notifications
You must be signed in to change notification settings - Fork 1
/
orbits.bundle.js
567 lines (504 loc) · 13.3 KB
/
orbits.bundle.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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
'use strict';
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var G = 6.67428e-11;
var AU = 149.6e6 * 1000;
var scalevalue = 50;
var speedvalue = 5;
var SCALE = scalevalue / AU;
var timestep = 24 * 3600 / speedvalue;
var viewx = 0;
var viewy = 0;
var mass = 3e+24;
var massIndex = 24;
var _sling = null;
var rect;
var Scene = function () {
function Scene() {
var size = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : { width: 900, height: 600 };
_classCallCheck(this, Scene);
this.objects = [];
this.tickInterval = undefined;
this.width = size.width;
this.height = size.height;
var canvas = document.getElementById('canvas');
canvas.width = $(window).width();
canvas.height = $(window).height();
this.canvas = canvas;
document.body.appendChild(canvas);
this.context = canvas.getContext('2d');
rect = canvas.getBoundingClientRect();
}
_createClass(Scene, [{
key: 'addObject',
value: function addObject(object) {
this.objects.push(object);
}
}, {
key: 'removeObject',
value: function removeObject(obj) {
this.objects = this.objects.filter(function (item) {
return item !== obj;
});
}
}, {
key: 'resetObject',
value: function resetObject() {
this.objects = [];
}
// returns force exerted on this body by the other body
}, {
key: 'attraction',
value: function attraction(self, other) {
if (self === other) return false;
// compute distance between bodies
var sx = self.options.px;
var sy = self.options.py;
var ox = other.options.px;
var oy = other.options.py;
var dx = ox - sx;
var dy = oy - sy;
var d = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
if (d == 0) return [0, 0];
// compute force
var F = G * self.options.mass * other.options.mass / Math.pow(d, 2);
// compute direction
var o = Math.atan2(dy, dx);
var Fx = Math.cos(o) * F;
var Fy = Math.sin(o) * F;
//console.log(sx, sy, ox, oy, dx, dy, d, F, o, Fx, Fy);
return [Fx, Fy];
}
}, {
key: 'render',
value: function render(paused) {
var _this = this;
this.context.clearRect(0, 0, this.context.canvas.width, this.context.canvas.height);
if (!paused) {
(function () {
var changes = [];
_this.objects.forEach(function (o) {
var total = [0, 0];
_this.objects.forEach(function (o2) {
if (o !== o2) {
var tmp = _this.attraction(o, o2);
total[0] += tmp[0];
total[1] += tmp[1];
}
});
changes.push(total);
});
changes.forEach(function (change, index) {
_this.objects[index].updateV(change[0], change[1]);
});
})();
}
this.objects.forEach(function (o) {
o.draw(_this.context);
});
if (_sling != null) _sling.draw(this.context);
}
}, {
key: 'sling',
value: function sling() {
window.addEventListener('mousedown', function (e) {
_sling = new Slingshot({
px: e.pageX - rect.left,
py: e.pageY - rect.top
});
_sling.followToMouse();
});
window.addEventListener('mouseup', function (e) {
if (_sling.options.px != e.pageX - rect.left) {
_sling.shoot();
}
_sling.unfollow();
_sling = null;
});
document.body.onkeyup = function (e) {};
document.body.onkeyup = function (e) {
console.log(e.keyCode);
if (e.keyCode == 66) {
console.log(mousex, mousey);
var blackhole = new Body({
name: "Blackhole",
mass: 1.989e30 * 5,
vx: 0,
vy: 0,
px: (mousex - scene.canvas.width / 2 - viewx) / SCALE,
py: (mousey - scene.canvas.height / 2 - viewy) / SCALE,
radius: 3,
linewidth: 3,
strokestyle: 'white'
});
scene.addObject(blackhole);
console.log(scene.objects);
} else if (e.keyCode == 32) {
var body = new Body({
name: "White hole",
mass: 1.989e30,
vx: 20000,
vy: 0,
px: 0,
py: -1 * AU,
radius: 5,
linewidth: 1,
strokestyle: '#FF0'
});
scene.addObject(body);
var body2 = new Body({
name: "White hole",
mass: 1.989e30,
vx: -20000,
vy: 0,
px: 0,
py: 1 * AU,
radius: 5,
linewidth: 1,
strokestyle: '#FF0'
});
scene.addObject(body2);
}
};
}
}]);
return Scene;
}();
var Body = function () {
function Body(options) {
_classCallCheck(this, Body);
this.options = options;
}
_createClass(Body, [{
key: 'updateV',
value: function updateV(x, y) {
this.options.vx += x / this.options.mass * timestep;
this.options.vy += y / this.options.mass * timestep;
if (Math.sqrt(Math.pow(this.options.vy, 2) + Math.pow(this.options.vx, 2)) > 299792458) {
console.log("Error exceeded speed of light", Math.sqrt(Math.pow(this.options.vy, 2) + Math.pow(this.options.vx, 2)));
}
this.updateP();
}
}, {
key: 'updateP',
value: function updateP() {
this.options.px += this.options.vx * timestep;
this.options.py += this.options.vy * timestep;
}
}, {
key: 'draw',
value: function draw(context) {
if (context == null) return false;
context.lineWidth = this.options.linewidth;
context.fillStyle = this.options.strokestyle;
context.beginPath();
context.arc(this.options.px * SCALE + scene.canvas.width / 2 + viewx, this.options.py * SCALE + scene.canvas.height / 2 + viewy, this.options.radius, 0, 2 * Math.PI, false);
context.fill();
}
}]);
return Body;
}();
var Slingshot = function () {
function Slingshot(options) {
_classCallCheck(this, Slingshot);
this.options = options;
}
_createClass(Slingshot, [{
key: 'unfollow',
value: function unfollow() {
var _this2 = this;
window.removeEventListener('mousemove', function (e) {
_this2.options.px2 = e.pageX - rect.left;
_this2.options.py2 = e.pageY - rect.top;
});
}
}, {
key: 'followToMouse',
value: function followToMouse() {
var _this3 = this;
window.addEventListener('mousemove', function (e) {
_this3.options.px2 = e.pageX - rect.left;
_this3.options.py2 = e.pageY - rect.top;
});
}
}, {
key: 'shoot',
value: function shoot() {
var size = massIndex / 2 - 9 + Math.random() * 3;
var body = new Body({
name: randomName(Math.ceil(Math.random() * 5) + 3),
mass: mass,
vx: (this.options.px - this.options.px2) * 500,
vy: (this.options.py - this.options.py2) * 500,
px: (this.options.px - scene.canvas.width / 2 - viewx) / SCALE,
py: (this.options.py - scene.canvas.height / 2 - viewy) / SCALE,
radius: Math.ceil(size),
linewidth: 1,
strokestyle: '#' + (Math.floor(Math.random() * 156) + 100).toString(16) + (Math.floor(Math.random() * 156) + 100).toString(16) + (Math.floor(Math.random() * 156) + 100).toString(16)
});
scene.addObject(body);
}
}, {
key: 'draw',
value: function draw(context) {
this.context = context;
if (this.context == null) return false;
this.context.beginPath();
this.context.moveTo(this.options.px, this.options.py);
context.lineTo(this.options.px2, this.options.py2);
this.context.strokeStyle = '#FF4136';
context.stroke();
}
}]);
return Slingshot;
}();
function randomName(length) {
var x = "";
var possible = "abcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < length; i++) {
x += possible.charAt(Math.floor(Math.random() * possible.length));
if (i == 0) x.toUpperCase();
}
return x;
}
var scene = new Scene();
scene.sling();
function setState(id) {
scalevalue = 50;
SCALE = scalevalue / AU;
viewx = 0;
viewy = 0;
scene.resetObject();
if (id === 1) {
var sun = new Body({
name: "Sun",
mass: 1.989e30,
vx: 0,
vy: 0,
px: 0,
py: 0,
radius: 8,
linewidth: 3,
strokestyle: 'yellow'
});
var earth = new Body({
name: "Earth",
mass: 5.972e24,
vx: 0,
vy: 30290,
px: -147.09e6 * 1000,
py: 0,
radius: 3,
linewidth: 1,
strokestyle: '#60a6d4'
});
var moon = new Body({
name: "Moon",
mass: 0.07346e24,
vx: 0,
vy: 31366,
px: -147.4533e6 * 1000,
py: 0,
radius: 1,
linewidth: 1,
strokestyle: '#E2E6E7'
});
var mercury = new Body({
name: "Mercury",
mass: 0.330e24,
vx: 0,
vy: 58980,
px: -46e6 * 1000,
py: 0,
radius: 2,
linewidth: 1,
strokestyle: '#736E52'
});
var jupiter = new Body({
name: "Jupiter",
mass: 1898.19e24,
vx: 0,
vy: 13720,
px: -740.52e6 * 1000,
py: 0,
radius: 10,
linewidth: 1,
strokestyle: '#CEA089'
});
var saturn = new Body({
name: "saturn",
mass: 568.34e24,
vx: 0,
vy: 10180,
px: -1352.55e6 * 1000,
py: 0,
radius: 5,
linewidth: 1,
strokestyle: '#91987B'
});
var uranus = new Body({
name: "uranus",
mass: 86.813e24,
vx: 0,
vy: 7110,
px: -2741.30e6 * 1000,
py: 0,
radius: 5,
linewidth: 1,
strokestyle: '#90979E'
});
var neptune = new Body({
name: "Neptune",
mass: 102.413e24,
vx: 0,
vy: 5500,
px: -4444.45e6 * 1000,
py: 0,
radius: 7,
linewidth: 1,
strokestyle: '#2D30CC'
});
var mars = new Body({
name: "Mars",
mass: 0.64171e24,
vx: 0,
vy: 26500,
px: -206.62e6 * 1000,
py: 0,
radius: 2,
linewidth: 1,
strokestyle: '#BF5B2D'
});
var venus = new Body({
name: "Venus",
mass: 4.87e24,
vx: 0,
vy: 35260,
px: -107.48e6 * 1000,
py: 0,
radius: 2,
linewidth: 1,
strokestyle: '#CFAA60'
});
scene.addObject(sun);
scene.addObject(earth);
scene.addObject(moon);
scene.addObject(mercury);
scene.addObject(mars);
scene.addObject(jupiter);
scene.addObject(saturn);
scene.addObject(uranus);
scene.addObject(neptune);
scene.addObject(venus);
}
scene.render();
}
$(window).bind("resize", function () {
scene.canvas.width = $(window).width();
scene.canvas.height = $(window).height();
});
var mousex = 0;
var mousey = 0;
$(window).mousemove(function (e) {
mousex = e.pageX - rect.left;
mousey = e.pageY - rect.top;
});
$(window).keydown(function (e) {
console.log(e.which);
switch (e.which) {
case 171:
if (scalevalue < 5) {
scalevalue = scalevalue * 2;
} else {
scalevalue += 3;
}
SCALE = scalevalue / AU;
break;
case 173:
if (scalevalue < 5) {
scalevalue = scalevalue / 2;
} else {
scalevalue -= 3;
}
SCALE = scalevalue / AU;
break;
case 37:
viewx += 10;
break;
case 39:
viewx -= 10;
break;
case 38:
viewy += 10;
break;
case 40:
viewy -= 10;
break;
case 82:
setMass(24);
setState(1);
break;
case 80:
running = !running;
break;
case 72:
scene.removeObject();
break;
case 48:
setMass(18);
break;
case 49:
setMass(20);
break;
case 50:
setMass(22);
break;
case 51:
setMass(24);
break;
case 52:
setMass(26);
break;
case 53:
setMass(28);
break;
case 54:
setMass(30);
break;
case 55:
setMass(32);
break;
case 56:
setMass(34);
break;
case 57:
setMass(36);
break;
default:
return;
}
e.preventDefault();
});
function setMass(m) {
massIndex = m;
mass = eval('3e+' + m);
document.querySelector("#mass").innerHTML = '3e+' + m;
}
function changespeed(e) {
speedvalue = e;
timestep = 24 * 3600 / speedvalue;
document.querySelector("#time").innerHTML = Math.round(fps / speedvalue * 10) / 10 + 'earthdays / second';
}
function changefps(e) {
fps = e;
}
var fps = 60;
var running = false;
function draw() {
setTimeout(function () {
requestAnimationFrame(draw);
scene.render(running);
}, 1000 / fps);
}
setState(1, scene);
draw();