-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHandler.js
More file actions
56 lines (55 loc) · 1.91 KB
/
Copy pathHandler.js
File metadata and controls
56 lines (55 loc) · 1.91 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
// Generated by CoffeeScript 1.4.0
var Handler;
Handler = {
scriptIntervalID: null,
delay: 60,
stop: function() {
if (Handler.scriptIntervalID) {
return clearInterval(Handler.scriptIntervalID);
}
},
start: function() {
return Handler.scriptIntervalID = setInterval(Handler.main, Handler.delay);
},
main: function() {
var bumpedYoba, currentYoba, _i, _len, _ref, _ref1;
canvas.clear();
_ref = Yoba.getAllYobas();
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
currentYoba = _ref[_i];
if (currentYoba.atRightBorder()) {
currentYoba.speed = -Math.abs(currentYoba.speed);
} else if (currentYoba.atLeftBorder()) {
currentYoba.speed = +Math.abs(currentYoba.speed);
}
bumpedYoba = currentYoba.getBumpedYoba();
if (bumpedYoba) {
_ref1 = Handler.getSpeedsAfterBump(currentYoba.mass, currentYoba.speed, bumpedYoba.mass, bumpedYoba.speed), currentYoba.speed = _ref1[0], bumpedYoba.speed = _ref1[1];
currentYoba.startSpeek();
bumpedYoba.startSpeek();
}
currentYoba.continueSpeek();
currentYoba.speed -= Handler.getFrictionalAcceleration(currentYoba.radius, currentYoba.mass, currentYoba.speed);
currentYoba.angle = Handler.getNewAngle(currentYoba.angle, currentYoba.radius, currentYoba.speed);
currentYoba.position += Math.round(currentYoba.speed);
currentYoba.redraw();
}
return Yoba.stopScriptIfAllStopped();
},
getSpeedsAfterBump: function(m1, v1, m2, v2) {
return [((m1 - m2) * v1 + 2 * m2 * v2) / (m1 + m2), (2 * m1 * v1 + (m2 - m1) * v2) / (m1 + m2)];
},
getFrictionalAcceleration: function(R, M, speed) {
var N, a, f;
f = 0.3;
N = 9.8 * M;
a = (N * (f / R)) / R;
return (speed > 0 ? a : -a);
},
getNewAngle: function(a0, R, speed) {
var dFi, fi;
dFi = Math.round(speed) / R;
fi = a0 + dFi;
return 2 * Math.PI + fi % (4 * Math.PI);
}
};