-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbsp.js
More file actions
473 lines (371 loc) · 10.9 KB
/
Copy pathbsp.js
File metadata and controls
473 lines (371 loc) · 10.9 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
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
$(document).ready(function() {
var canvas = document.getElementById("canvas")//$("#canvas");
var ctx = canvas.getContext("2d");
var H;
var W;
var TX;
var TY;
//Game Constants
var G = 9.81;
var M = 1000000;
var e = 0.1; // coefficient of restitution
var R = 75;
var outer = R*5;
var orbSize = 25;
var orbRadius = orbSize / 4; //This value is based off the image
//Game score vars
var score = 0;
var bestscore = 0;
var background = new Image();
function resize() {
W = $(window).width() - 4;
H = $(window).height() - 4;
$("#canvas").css("width", W);
$("#canvas").css("height", H);
var ratio = window.devicePixelRatio;
W *= ratio;
H *= ratio;
$("#canvas").attr("width", W);
$("#canvas").attr("height", H);
TX = W / 2;
TY = H / 2;
ctx.imageSmoothingEnabled = false;
ctx.mozImageSmoothingEnabled = false;
ctx.webkitImageSmoothingEnabled = false;
// The rest of this is to save a background gradient as an image, cause drawing gradients is slower than an image
ctx.save();
ctx.translate(TX, TY);
var grd=ctx.createRadialGradient(0,0,R,0,0,1.5*R);
grd.addColorStop(0,'#2F387B');
grd.addColorStop(1,'#000000');
// Fill with gradient
ctx.fillStyle=grd;
ctx.fillRect(-TX,-TY,W,H);
ctx.beginPath();
ctx.arc(0,0,outer,0,2*Math.PI);
ctx.strokeStyle = '#999999';
ctx.stroke();
ctx.restore();
background = new Image();
background.src = canvas.toDataURL("image/png");
}
resize();
$(window).resize(resize);
$(window).onresize = resize;
//Load images
var imgcount = 2;
function checkImgs(){
imgcount--;
if(imgcount <= 0){
run();
}
}
var imgEarth = new Image();
var imgSput = new Image();
imgEarth.onload = checkImgs;
imgSput.onload = checkImgs;
imgEarth.src = "earth.png";
imgSput.src = "sputnik.png";
var deadOrbList = [];
var orbiterList = [];
var alive = 0;
//
// Vector operations
//
function Vector(x, y) {
this.x = x;
this.y = y;
}
Vector.prototype.add = function (v2) {
var v = new Vector(this.x + v2.x,
this.y + v2.y);
return v;
}
Vector.prototype.sub = function(v2) {
var v = new Vector(this.x - v2.x,
this.y - v2.y);
return v;
}
Vector.prototype.mult = function (v2) {
var v = new Vector(this.x * v2.x,
this.y * v2.y);
return v;
}
Vector.prototype.scale = function (s) {
var v = new Vector(this.x * s,
this.y * s);
return v;
}
Vector.prototype.normalize = function () {
var m = mag(this);
var v = new Vector(this.x / m,
this.y / m);
return v;
}
Vector.prototype.angle = function () {
return Math.atan2(this.y,this.x);
}
Vector.prototype.rotate = function (a) {
var v = new Vector(this.x*Math.cos(a) - this.y*Math.sin(a),
this.x*Math.sin(a) + this.y*Math.cos(a));
return v;
}
Vector.prototype.equal = function (v2) {
return this.x == v2.x && this.y == v2.y && this.z == v2.z;
}
function mag(v){
return Math.sqrt((v.x * v.x)+(v.y * v.y));
}
// New vector scaling
function scale(v, s){
alert("Deprecated scale!");
return new Vector(v.x * s, v.y * s);
}
function normToOrigin(v){
return v.scale(-1).normalize();
}
//normal between two positions
function norm(pos1, pos2){
return pos1.sub(pos2);
}
//
// Orbiter stuff
//
function newOrb() {
var orbiter = {
pos : new Vector(0, -R),
vel : new Vector(0,0),
orient : new Vector(0,-1),
t: 0,
force: new Vector(0,0),
started: false,
dead: false,
thrust: false
};
orbiterList.push(orbiter);
return orbiter;
}
// Initial Orbiter
var currOrb = newOrb();
function destroyOrb(orb) {
orb.dead = true;
orb.vel = orb.vel.scale(0.05);
if (orb == currOrb) {
currOrb = newOrb();
}
}
//
// Physics / Math
//
//dist from origin (0,0)
function dist(v) {
return Math.sqrt((v.x * v.x)+(v.y * v.y));
}
//dist between two positions
function dist2(v1, v2){
var x = v1.x - v2.x;
var y = v1.y - v2.y;
return Math.sqrt((x * x) + (y * y));
}
function dot(v1, v2){
return (v1.x * v2.y)+(v2.x * v1.y);
}
function rk4_finalstep(p1, v1, v2, v3, v4, dt){
var v = new Vector(
this.x = p1.x + (v1.x + 2*v2.x + 2*v3.x + v4.x)*(dt/6),
this.y = p1.y + (v1.y + 2*v2.y + 2*v3.y + v4.y)*(dt/6)
);
return v;
}
function rk4(orb, dt) {
// Returns final (position, velocity) array after time dt has passed.
// x: initial position
// v: initial velocity
// a: acceleration function a(x,v,dt) (must be callable)
// dt: timestep
var p1 = orb.pos;
var v1 = orb.vel;
var force = orb.force;
var a1 = getForce(p1, v1, orb).add(force);
var p2 = p1.add(v1.scale(0.5).scale(dt));
var v2 = v1.add(a1.scale(0.5).scale(dt));
var a2 = getForce(p2, v2, orb).add(force);
var p3 = p1.add(v2.scale(0.5).scale(dt));
var v3 = v1.add(a2.scale(0.5).scale(dt));
var a3 = getForce(p3, v3, orb).add(force);
var p4 = p1.add(v3.scale(dt));
var v4 = v1.add(a3.scale(dt));
var a4 = getForce(p4, v4, orb).add(force);
//var pf = p1.add(v1.add(v2.scale(2).add(v3.scale(2)).add(v4)).scale(dt/6));
//var vf = v1.add(a1.add(a2.scale(2).add(a3.scale(2)).add(a4)).scale(dt/6));
var pf = rk4_finalstep(p1, v1, v2, v3, v4, dt);
var vf = rk4_finalstep(v1, a1, a2, a3, a4, dt);
orb.pos = pf;
orb.vel = vf;
}
function getForce(p, v, o){
var d = dist(p);
// The force on the orbiter is G*M/d^2
var f = G*M/(d*d);
var n = normToOrigin(p);
var drag = v.scale(-0.1*Math.max(0,mag(v)-300)); // using magic velocity 300
if(o.dead){
drag = drag.add(v.scale(-0.02));
}
// Add a force in the normal dir with magnitude f
return n.scale(f).add(drag);
}
function clearForce(orb){
orb.force = new Vector(0,0);
}
function appForce(orb, dir, f) {
return;
}
function appThrust(){
var force = 1800;
var coeff = R/dist(currOrb.pos);
if(currOrb.thrust == true){
currOrb.force = currOrb.orient.normalize().scale(force*coeff);
}
}
//
// Drawing
//
function center(f, o) {
ctx.save();
ctx.translate(TX, TY);
f(o);
ctx.restore();
}
function drawOrb(orb){
var w = orbSize;
var h_offset = w / 2.05;
var w_offset = w / -25;
var h = w * imgSput.height / imgSput.width;
ctx.translate(orb.pos.x , orb.pos.y );
ctx.rotate(orb.orient.angle() + Math.PI / 2);
ctx.drawImage(imgSput, - w/2 + w_offset , -h/2 + h_offset, w, h);
ctx.beginPath();
ctx.arc(0, 0, orbRadius, 0, Math.PI*2);
if(orb.dead){
ctx.strokeStyle = "red";
ctx.stroke();
}else if(orb === currOrb){
ctx.fillStyle = "green";
ctx.fill();
}
}
var upv = new Vector(0,-1);
function draw(){
//Draw background atmosphere gradient
ctx.drawImage(background,0,0);
center(function(){
var size = 200;
ctx.drawImage(imgEarth, -R, -R, 2*R, 2*R * imgEarth.height / imgEarth.width);
});
ctx.font = "30px Fixedsys";
ctx.fillStyle = "green";
ctx.fillText("in contact: " + score,10,30);
ctx.fillText("best: " + bestscore, 10, 60);
orbiterList.forEach(function(o) {
var A = Math.atan2(o.pos.y, o.pos.x) + Math.PI/2;
var d = dist(o.pos);
var cutoff = 2*R
//set orientation of orbiter
if (d < cutoff+R) {
o.orient = upv.rotate((A + (Math.PI/2) * ((d-R)/cutoff))).normalize();
} else if(Math.abs(mag(o.vel))>0){
o.orient = upv.rotate((A + (Math.PI/2)));
}
center(drawOrb, o);
});
}
//
// Event loop stuff
//
// oldT is needed to calculate what our timestep (dt) should be
var oldT = Date.now();
var dt = 0;
var DT = 0.01;
var invspeed = 1300;
function step(){
score = 0;
orbiterList.forEach(function(o) {
//collisions with outer and inner limits
if(dist(o.pos) > outer && o.dead !== true){
o.dead = true;
o.vel = o.vel.scale(0.4);
o.force = new Vector(0,0);
if(currOrb === o){currOrb = newOrb();}
}
if( dist(o.pos) < R ){
deadOrbList.push(o);
if(currOrb === o){currOrb = newOrb();}
}
// inter-object collisions
orbiterList.forEach(function(o2) {
if(dist2(o.pos, o2.pos) <= 2*orbRadius && o !== o2){
var n = norm(o.pos, o2.pos).normalize();
var coll = dot(o.vel.sub(o2.vel), n);
//if objects are moving toward each other
if(coll < 0){
var j = coll * (1+e) * (0.5);
o.vel = o.vel.sub(n.scale(j));
o2.vel = o2.vel.add(n.scale(j));
if(!o.dead){
o.dead = true;
o.force = new Vector(0,0);
}
if(!o2.dead){
o2.dead = true;
o2.force = new Vector(0,0);
}
if(currOrb === o || currOrb === o2){
currOrb.started = true;
currOrb = newOrb();
}
}
}
});
if(o.started){
rk4(o, DT);
}
if(!o.dead){
score++;
}
});
if(currOrb.started){ currOrb.t += DT; }
if(currOrb.t > 5){ currOrb = newOrb();}else{
score--;
}
deadOrbList.forEach(function(o) {
//score--;
if (orbiterList.indexOf(o) !== -1) {
orbiterList.splice(orbiterList.indexOf(o), 1);
deadOrbList.splice(deadOrbList.indexOf(o), 1);
}
});
bestscore = Math.max(score, bestscore);
}
$(document).mousedown(function(){
currOrb.thrust = true;
currOrb.started = true;
});
$(document).mouseup(function(){
currOrb.thrust = false;
currOrb.force = new Vector(0,0);
});
function run() {
var now = Date.now();
dt = (now - oldT) / invspeed;
oldT = now;
// This ensures that the simulation runs at the same speed, even if the drawing can't keep up
while(dt > 0 && dt < 3) {
dt -= DT;
appThrust();
step();
}
draw();
requestAnimationFrame(run);
}
});