-
Notifications
You must be signed in to change notification settings - Fork 318
/
base.js
87 lines (77 loc) · 2.56 KB
/
base.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
// Console shim
(function () {
var f = function () {};
if (!console) {
console = {
log:f, info:f, warn:f, debug:f, error:f
};
}
}());
var limitNumber = function(num, min, max) {
return Math.min(max, Math.max(num, min));
};
var epsilonEquals = function(a, b) {
return Math.abs(a-b) < 0.00000001;
};
// Polyfill from MDN
var sign = function(x) {
x = +x; // convert to a number
if (x === 0 || isNaN(x)){
return x;
}
return x > 0 ? 1 : -1;
};
if(typeof Math.sign === "undefined") {
Math.sign = sign;
}
var deprecationWarning = function(name) {
console.warn("You are using a deprecated feature scheduled for removal: " + name);
};
var newGuard = function(obj, type) {
if(!(obj instanceof type)) { throw "Incorrect instantiation, got " + typeof obj + " but expected " + type; }
}
var createBoolPassthroughFunction = function(owner, obj, objPropertyName) {
return function(val) {
if(typeof val !== "undefined") {
obj[objPropertyName] = val ? true : false;
obj.trigger("change:" + objPropertyName, obj[objPropertyName]);
return owner;
} else {
return obj[objPropertyName];
}
};
};
distanceNeededToAchieveSpeed = function(currentSpeed, targetSpeed, acceleration) {
// v² = u² + 2a * d
var requiredDistance = (Math.pow(targetSpeed, 2) - Math.pow(currentSpeed, 2)) / (2 * acceleration);
return requiredDistance;
};
accelerationNeededToAchieveChangeDistance = function(currentSpeed, targetSpeed, distance) {
// v² = u² + 2a * d
var requiredAcceleration = 0.5 * ((Math.pow(targetSpeed, 2) - Math.pow(currentSpeed, 2)) / distance);
return requiredAcceleration;
};
// Fake frame requester helper used for testing and fitness simulations
var createFrameRequester = function(timeStep) {
var currentCb = null;
var requester = {};
requester.currentT = 0.0;
requester.register = function(cb) { currentCb = cb; };
requester.trigger = function() { requester.currentT += timeStep; if(currentCb !== null) { currentCb(requester.currentT); } };
return requester;
};
var getCodeObjFromCode = function(code) {
if (code.trim().substr(0,1) == "{" && code.trim().substr(-1,1) == "}") {
code = "(" + code + ")";
}
/* jslint evil:true */
obj = eval(code);
/* jshint evil:false */
if(typeof obj.init !== "function") {
throw "Code must contain an init function";
}
if(typeof obj.update !== "function") {
throw "Code must contain an update function";
}
return obj;
}