-
Notifications
You must be signed in to change notification settings - Fork 0
/
bundle.js
509 lines (422 loc) · 13.2 KB
/
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
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.loaded = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports, __webpack_require__) {
var engine = function (options, callback) {
var Constants = __webpack_require__(1);
var Vector = __webpack_require__(2);
var movingSphere;
if (Constants.ELASTIC === true) {
movingSphere = __webpack_require__(3);
}
callback(Constants, Vector, movingSphere);
};
// DEMO: A Basic Illustration of How The Engine Can Be Used. Delete the code
//below and provide your own callback to the engine.
var newOptions = {
FRICTION_COEFF: 0,
ELASTIC: true,
WALLS: true,
// CONST_MASS: 50,
CONST_DENSITY: 0.00025,
DIM_X: 750,
DIM_Y: 750
};
engine(newOptions, function (Constants, Vector, movingSphere) {
var canvas = document.getElementById('canvas');
var c = canvas.getContext('2d');
Constants.setConstants(newOptions);
var balls = [];
while (balls.length < 20) {
balls.push(movingSphere.createRandom(balls));
}
var runSimFrame = function () {
c.clearRect(0, 0, canvas.width, canvas.height);
var totalKE = 0;
var mass = arguments[0].CONST_MASS;
balls.forEach(function (ball) {
ball.draw(c);
totalKE += ball.mass * ball.vel.norm();
});
totalKE = totalKE / 2;
document.getElementById('kE').innerHTML = "Total KE of System: " + Math.round(totalKE);
var uncheckedBalls = balls.slice();
for (var i = 0; i < balls.length; i++) {
balls[i].move();
uncheckedBalls.shift();
for (var j = 0; j < uncheckedBalls.length; j++) {
if (balls[i].isCollidedWith(uncheckedBalls[j]) && !balls[i].equals(uncheckedBalls[j])) {
balls[i].handleCollision(uncheckedBalls[j]);
}
}
}
};
var sim = setInterval(runSimFrame.bind(this, Constants), 20);
var restart = function () {
clearInterval(sim);
balls = [];
while (balls.length < 20) {
balls.push(movingSphere.createRandom(balls));
}
var friction = parseFloat(document.getElementById("friction").value);
Constants.FRICTION_COEFF = friction;
sim = setInterval(runSimFrame.bind(this, Constants), 20);
};
document.getElementById("restart").addEventListener("click", restart);
});
// module.exports = engine;
/***/ },
/* 1 */
/***/ function(module, exports) {
var configConstants = {
FRICTION_COEFF: 0,
ELASTIC: true,
GRAVITY: false,
CONST_DENSITY: null,
CONST_MASS: null,
CONST_RADIUS: null,
SPACE_DIM: 2,
WALLS: true,
DIM_X: null,
DIM_Y: null
};
configConstants.setConstants = function (options) {
if (options === undefined) {
return;
}
Object.keys(this).forEach(function (key) {
if (options[key] !== undefined) {
this[key] = options[key];
}
}.bind(this));
};
module.exports = configConstants;
/***/ },
/* 2 */
/***/ function(module, exports) {
var Vector = function (x, y) {
this[0] = x;
this[1] = y;
};
var _trunc = function (num) {
return parseFloat(num.toFixed(4));
};
Vector.prototype.findTheta = function () {
if (this[0] === 0) {
if (this[1] > 0) {
return Math.PI / 2;
}
else if (this[1] === 0) {
return 0;
}
else {
return -Math.PI / 2;
}
}
if (this[1] === 0) {
if (this[0] > 0) {
return 0;
}
else {
return Math.PI;
}
}
if (this[0] > 0) {
return Math.atan(-this[1] / this[0]);
}
else {
return Math.atan(-this[1] / this[0]) + Math.PI;
}
};
Vector.prototype.scalarProd = function (vec) {
return (this[0] * vec[0]) + (this[1] * vec[1]);
};
Vector.prototype.norm = function () {
return Math.pow(this[0], 2) + Math.pow(this[1], 2);
};
Vector.prototype.mag = function () {
return Math.sqrt(this.norm());
};
Vector.prototype.getRelativeAngle = function (vec) {
if (vec.norm() === 0 || this.norm() === 0) {
return null;
}
var denom = (Math.sqrt(this.norm()) * Math.sqrt(vec.norm()));
var innerVal = this.scalarProd(vec) / denom;
if (_trunc(innerVal) === 1) {
if (this.norm() === vec.norm()) {
return 0;
}
return false;
}
return Math.acos(_trunc(innerVal));
};
Vector.prototype.unitize = function () {
if (this.mag() === 0) {
return new Vector(0, 0);
}
var dir = this.findTheta();
var newX = Math.cos(dir);
var newY = Math.sin(dir);
return new Vector(newX, newY);
};
Vector.prototype.willIntersect = function (otherVec, thisPos, otherPos) {
var denom = (this[1] / this[0]) - (otherVec[1] / otherVec[0]);
var x = (otherPos[1] - thisPos[1]) +
(this[1] / this[0]) * (thisPos[0]) +
(otherVec[1] / otherVec[0]) * (otherPos[0]);
if (denom === 0) {
return true;
}
x = x / denom;
if (x >= 0) {
return true;
}
return false;
};
Vector.prototype.rotate = function (angle) {
var newX = this[0] * Math.cos(angle) - this[1] * Math.sin(angle);
var newY = this[0] * Math.sin(angle) + this[1] * Math.cos(angle);
return new Vector(newX, newY);
};
Vector.prototype.reflectOffVector = function (v2, mass1, mass2, collAngle) {
var result = new Vector(0,0);
var angle1 = this.findTheta();
var angle2 = v2.findTheta();
var firstTermThis = (this.mag() *
_trunc(Math.cos(angle1-collAngle)) *
(mass1 - mass2) +
(2 * mass2 * v2.mag() *
_trunc(Math.cos(angle2-collAngle)))) / (mass1 + mass2);
result[0] = firstTermThis * _trunc(Math.cos(collAngle));
result[0] += this.mag() *
_trunc(Math.sin(angle1 - collAngle)) *
_trunc(Math.cos(collAngle + (Math.PI / 2)));
result[1] = firstTermThis * _trunc(Math.sin(collAngle));
result[1] += this.mag() *
_trunc(Math.sin(angle1 - collAngle)) *
_trunc(Math.sin(collAngle + (Math.PI / 2)));
result[1] = -result[1];
return result;
};
module.exports = Vector;
/***/ },
/* 3 */
/***/ function(module, exports, __webpack_require__) {
var Vector = __webpack_require__(2);
var Util = __webpack_require__(4);
var Constants = __webpack_require__(1);
var _trunc = function (num) {
return parseFloat(num.toFixed(4));
};
var _randomParams = function () {
var creationParams = {};
var density = Constants.CONST_DENSITY;
var radius = (Math.random() * 15) + 10;
creationParams.color = _randomColor();
creationParams.pos = _randomPos();
creationParams.vel = new Vector(Math.random() * 7, Math.random() * 7);
creationParams.radius = radius;
creationParams.mass = Math.pow(radius, 3) * (4/3) * Math.PI * density;
return creationParams;
};
var movingSphere = function (settings) {
this.pos = settings.pos;
this.vel = settings.vel;
this.mass = settings.mass || Constants.CONST_MASS;
this.radius = settings.radius;
this.color = settings.color;
};
movingSphere.createRandom = function (otherSpheres) {
var resultSphere;
var creationParams = Util.randomParams();
resultSphere = new movingSphere(creationParams);
var invalid = false;
for (var i = 0; i < otherSpheres.length; i++) {
if (resultSphere.isCollidedWith(otherSpheres[i])) {
invalid = true;
}
}
if (invalid) {
return movingSphere.createRandom(otherSpheres);
}
else {
return resultSphere;
}
};
movingSphere.prototype.draw = function (ctx) {
ctx.fillStyle = this.color;
ctx.beginPath();
ctx.arc(this.pos[0], this.pos[1], this.radius, 0, 2 * Math.PI, false);
ctx.fill();
};
movingSphere.prototype.equals = function (otherObj) {
if (this.pos[0] === otherObj.pos[0] && this.pos[1] === otherObj.pos[1]) {
return true;
}
return false;
};
movingSphere.prototype.move = function () {
var oldVel = Object.assign(Vector, this.vel);
this.pos[0] += this.vel[0];
this.pos[1] += this.vel[1];
if (Constants.WALLS) {
this.handleWallCollision();
}
this.applyFriction();
return oldVel;
};
movingSphere.prototype.undoMove = function (oldVel) {
this.pos[0] -= oldVel[0];
this.pos[1] -= oldVel[1];
};
movingSphere.prototype.detectWallCollisionX = function () {
var Xbound = Constants.DIM_X - this.radius;
var dimXRef = this.pos[0] > Xbound || this.pos[0] < 0 + this.radius;
return dimXRef;
};
movingSphere.prototype.detectWallCollisionY = function () {
var Ybound = Constants.DIM_Y - this.radius;
var dimYRef = this.pos[1] > Ybound || this.pos[1] < 0 + this.radius;
return dimYRef;
};
movingSphere.prototype.handleWallCollision = function () {
var dimXRef = this.detectWallCollisionX();
var dimYRef = this.detectWallCollisionY();
if (dimXRef || dimYRef) {
this.pos[0] -= this.vel[0];
this.pos[1] -= this.vel[1];
this.bounce(dimXRef, dimYRef);
}
};
movingSphere.prototype.applyFriction = function () {
coeffFriction = Constants.FRICTION_COEFF;
this.vel[0] = this.vel[0] * Math.sqrt(1 - coeffFriction);
this.vel[1] = this.vel[1] * Math.sqrt(1 - coeffFriction);
};
movingSphere.prototype.bounce = function (bounceX, bounceY) {
this.vel[0] = bounceX ? -this.vel[0] : this.vel[0];
this.vel[1] = bounceY ? -this.vel[1] : this.vel[1];
};
movingSphere.prototype.getCollisionAngle = function (otherObj) {
return this.vel.getRelativeAngle(otherObj.vel);
};
movingSphere.prototype.isCollidedWith = function (otherObj) {
var distance = new Vector(otherObj.pos[0] - this.pos[0],
otherObj.pos[1] - this.pos[1]);
if (distance.mag() <= (this.radius + otherObj.radius)) {
return true;
}
return false;
};
movingSphere.prototype.isStationary = function () {
if (this.vel.mag() < 0.07) {
return true;
}
return false;
};
movingSphere.prototype.handleStationaryCollision = function (stillObj) {
if (!this.isStationary()) {
stillObj.vel[0] = this.vel[0];
stillObj.vel[1] = this.vel[1];
this.vel[0] = 0;
this.vel[1] = 0;
}
};
movingSphere.prototype.kineticEnergy = function () {
return this.mass * this.vel.norm() / 2;
};
movingSphere.prototype.handleCollision = function (otherObj) {
var formerThisVel = this.vel;
var formerOtherVel = otherObj.vel;
var connVec = new Vector(this.pos[0] - otherObj.pos[0],
this.pos[1] - otherObj.pos[1]);
var resultRotation = connVec.findTheta();
var mass1;
var mass2;
if (Constants.CONST_MASS) {
mass1 = Constants.CONST_MASS;
mass2 = Constants.CONST_MASS;
}
else {
mass1 = this.mass;
mass2 = otherObj.mass;
}
var collAngle = Math.PI;
var thisVel = this.vel.rotate(resultRotation);
var otherVel = otherObj.vel.rotate(resultRotation);
this.vel = thisVel.reflectOffVector(otherVel, mass1, mass2, collAngle);
otherObj.vel = otherVel.reflectOffVector(thisVel, mass2, mass1, collAngle);
this.vel = this.vel.rotate(-resultRotation);
otherObj.vel = otherObj.vel.rotate(-resultRotation);
};
module.exports = movingSphere;
/***/ },
/* 4 */
/***/ function(module, exports, __webpack_require__) {
var Constants = __webpack_require__(1);
var Vector = __webpack_require__(2);
module.exports = {
randomPos: function () {
return [(Constants.DIM_X - 42) * Math.random() + 21,
(Constants.DIM_Y - 42) * Math.random() + 21];
},
randomColor: function () {
return "#FF" + Math.round((Math.random() * 9999));
},
randomParams: function () {
var creationParams = {};
var density = Constants.CONST_DENSITY;
var radius = (Math.random() * 15) + 10;
creationParams.color = this.randomColor();
creationParams.pos = this.randomPos();
creationParams.vel = new Vector(Math.random() * 7, Math.random() * 7);
creationParams.radius = radius;
creationParams.mass = Math.pow(radius, 3) * (4/3) * Math.PI * density;
return creationParams;
}
};
/***/ }
/******/ ]);
//# sourceMappingURL=bundle.js.map