-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsatellites.js
More file actions
219 lines (188 loc) · 5.79 KB
/
satellites.js
File metadata and controls
219 lines (188 loc) · 5.79 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
var G = 0.3;
var zoom = 2;
var earth = new Body(new Point(0, 0), new Vector(0, 0), 1200, 28, 'earth.gif');
var moon = new Body(new Point(0.5, -360), new Vector(1, 0), 200, 15, 'moon.gif');
var massiveBodies = [earth, moon];
var satelliteImage = new Image();
satelliteImage.src = 'data:image/svg+xml,' + escape('<svg xmlns="http://www.w3.org/2000/svg" width="4" height="4"><circle fill="#FFF" cx="2" cy="2" r="2"/></svg>');
var satellites = [];
var satellitesDestroyed = 0;
var ctx = document.getElementById('satellites').getContext('2d');
var lastMouseDownPoint = null;
var elCount = document.getElementById('count');
var elCountDestroyed = document.getElementById('count_destroyed');
var bodyImagesRemainingToLoad = 2;
function onBodyImageLoaded()
{
if (--bodyImagesRemainingToLoad == 0)
window.requestAnimationFrame(draw);
}
function Body(pos, vel, mass, radius, urlOrImage)
{
this.pos = pos;
this.vel = vel;
this.mass = mass;
this.radius = radius;
if (typeof(urlOrImage) === "string")
{
function onimgload(event)
{
this.img.width = this.loadingImg.width * zoom;
this.img.height = this.loadingImg.height * zoom;
var ctx = this.img.getContext('2d');
ctx.drawImage(this.loadingImg, 0, 0, this.loadingImg.width * zoom, this.loadingImg.height * zoom);
delete this.loadingImg;
onBodyImageLoaded();
}
this.img = document.createElement("canvas");
this.loadingImg = new Image();
this.loadingImg.addEventListener('load', onimgload.bind(this), false);
this.loadingImg.src = urlOrImage;
}
else
{
this.img = urlOrImage;
}
}
Body.prototype.draw = function Body_draw()
{
var screenPt = this.pos.toScreen();
ctx.drawImage(this.img, screenPt.x - this.img.width / 2, screenPt.y - this.img.height / 2);
}
Body.prototype.move = function Body_move()
{
for (var i = 0; i < massiveBodies.length; i++)
{
var body = massiveBodies[i];
if (body == this)
continue;
this.vel.addInplace(body.acceleration(this.pos));
}
this.pos.addInplace(this.vel);
}
Body.prototype.acceleration = function Body_acceleration(pos)
{
var dir = this.pos.sub(pos);
var mag = (G * this.mass) / dir.magSquared();
return dir.normalize().mult(mag);
}
Body.prototype.checkCollisions = function Body_checkCollisions()
{
for (var i = 0; i < massiveBodies.length; i++)
{
var body = massiveBodies[i];
if (this.pos.sub(body.pos).magSquared() < (body.radius * body.radius))
return true;
}
return false;
}
function Point(x, y)
{
this.x = x;
this.y = y;
}
Point.prototype.add = function Point_add(v)
{
return new Point(this.x + v.dx, this.y + v.dy);
}
Point.prototype.addInplace = function Point_addInplace(v)
{
this.x += v.dx;
this.y += v.dy;
}
Point.prototype.sub = function Point_sub(p)
{
return new Vector(this.x - p.x, this.y - p.y);
}
Point.prototype.toScreen = function Point_toScreen()
{
return new Point(this.x + (ctx.canvas.width / 2), -this.y + (ctx.canvas.height / 2));
}
Point.prototype.fromScreen = function Point_fromScreen()
{
return new Point(this.x - (ctx.canvas.width / 2), -this.y + (ctx.canvas.height / 2));
}
function Vector(dx, dy)
{
this.dx = dx;
this.dy = dy;
}
Vector.prototype.magSquared = function Vector_magSquared()
{
return (this.dx * this.dx) + (this.dy * this.dy);
}
Vector.prototype.mag = function Vector_mag()
{
return Math.sqrt(this.magSquared());
}
Vector.prototype.add = function Vector_add(v)
{
return new Vector(this.dx + v.dx, this.dy + v.dy);
}
Vector.prototype.addInplace = function Vector_addInplace(v)
{
this.dx += v.dx;
this.dy += v.dy;
}
Vector.prototype.mult = function Vector_mult(c)
{
return new Vector(this.dx * c, this.dy * c);
}
Vector.prototype.div = function Vector_div(c)
{
return new Vector(this.dx / c, this.dy / c);
}
Vector.prototype.normalize = function Vector_normalize()
{
return this.div(this.mag());
}
function draw()
{
window.requestAnimationFrame(draw);
try
{
// Move everything
for (var i = 0; i < satellites.length; i++)
satellites[i].move();
moon.move();
// Remove all satellites that collided with the Earth or Moon
// or have been gotten too far away.
var newSatellites = [];
for (var i = 0; i < satellites.length; i++)
{
var sat = satellites[i];
if (!sat.checkCollisions() && sat.pos.sub(earth.pos).magSquared() < 500000)
newSatellites.push(sat);
else
satellitesDestroyed++;
}
satellites = newSatellites;
// Paint background
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
earth.draw();
// Moon's orbit
ctx.beginPath();
ctx.setLineDash([5, 15]);
ctx.arc(ctx.canvas.width / 2, ctx.canvas.height / 2, 360, 0, 2 * Math.PI);
ctx.strokeStyle = '#606060';
ctx.stroke();
moon.draw();
// Draw satellites
for (var i = 0; i < satellites.length; i++)
satellites[i].draw();
elCount.textContent = satellites.length;
elCountDestroyed.textContent = satellitesDestroyed;
} catch (e) {
console.error(e);
}
}
ctx.canvas.addEventListener('mousedown', function satellites_onmousedown(event)
{
lastMouseDownPoint = new Point(event.offsetX, event.offsetY).fromScreen();
}, false);
ctx.canvas.addEventListener('mouseup', function satellites_onmouseup(event)
{
var mouseUpPoint = new Point(event.offsetX, event.offsetY).fromScreen();
satellites.push(new Body(lastMouseDownPoint, mouseUpPoint.sub(lastMouseDownPoint).div(10), 0, 0, satelliteImage));
}, false);