-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.js
63 lines (47 loc) · 1.51 KB
/
update.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
// GENERIC UPDATE LOGIC
// The "nominal interval" is the one that all of our time-based units are
// calibrated to e.g. a velocity unit is "pixels per nominal interval"
//
var NOMINAL_UPDATE_INTERVAL = 16.666;
// Dt means "delta time" and is in units of the timer-system (i.e. milliseconds)
//
var g_prevUpdateDt = null;
// Du means "delta u", where u represents time in multiples of our nominal interval
//
var g_prevUpdateDu = null;
// Track odds and evens for diagnostic / illustrative purposes
//
var g_isUpdateOdd = false;
function update(dt) {
// Get out if skipping (e.g. due to pause-mode)
//
if (shouldSkipUpdate()) return;
// Remember this for later
//
var original_dt = dt;
// Warn about very large dt values -- they may lead to error
//
if (dt > 200) {
console.log("Big dt =", dt, ": CLAMPING TO NOMINAL");
dt = NOMINAL_UPDATE_INTERVAL;
}
// If using variable time, divide the actual delta by the "nominal" rate,
// giving us a conveniently scaled "du" to work with.
//
var du = (dt / NOMINAL_UPDATE_INTERVAL);
updateSimulation(du);
g_prevUpdateDt = original_dt;
g_prevUpdateDu = du;
g_isUpdateOdd = !g_isUpdateOdd;
}
// Togglable Pause Mode
//
var KEY_PAUSE = 'P'.charCodeAt(0);
var KEY_STEP = 'O'.charCodeAt(0);
var g_isUpdatePaused = false;
function shouldSkipUpdate() {
if (eatKey(KEY_PAUSE)) {
g_isUpdatePaused = !g_isUpdatePaused;
}
return g_isUpdatePaused && !eatKey(KEY_STEP);
}