-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathstdlib.js
102 lines (85 loc) · 2.21 KB
/
stdlib.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
/**
* stdlib.js - A standard library of functions for the entities. This file
* is implicitely included at the top of every imported entity JS file.
*/
var createBinaryController = function(min, max, deadband) {
return {
min: min,
max: max,
deadband2: deadband*deadband,
target: 0.0,
setTarget: function(tgt) { this.target = tgt; },
update: function(dt, val) {
var e = val - this.target;
if (e*e < this.deadband2) return 0.0;
if (val < this.target) return this.max;
return this.min;
}
};
};
var createPIDController = function(kp, ki, kd) {
return {
kp: kp,
ki: ki,
kd: kd,
target: 0.0,
setTarget: function(tgt) { this.target = tgt; },
integral: 0.0,
last_val: 0.0,
update: function(dt, val) {
var e = this.target - val;
// Proportional term
var prop = this.kp * e;
// Integral term
this.integral += e;
var integral = this.integral * this.ki;
// Derivative term
var de_dt = (val - this.last_val) / dt;
var derivative = -de_dt * this.kd;
this.last_val = val;
return prop + integral + derivative;
}
};
};
var vec3 = function(v) {
this.x = v.x;
this.y = v.y;
this.z = v.z;
};
vec3.prototype.cross = function(other) {
return new vec3({
x: this.y*other.z - this.z*other.y,
y: this.z*other.x - this.x*other.z,
z: this.x*other.y - this.y*other.x
});
};
vec3.prototype.dot = function(other) {
return this.x*other.x + this.y*other.y + this.z*other.z;
};
vec3.prototype.scale = function(s) {
return new vec3({
x: this.x*s,
y: this.y*s,
z: this.z*s
});
};
vec3.prototype.length = function() {
return Math.sqrt(this.x*this.x + this.y*this.y + this.z*this.z);
};
vec3.prototype.normalize = function() {
if (this.length() == 0.0) return new vec3({x: 0.0, y: 0.0, z: 1.0});
return this.scale(1.0/this.length());
};
vec3.prototype.add = function(other) {
return new vec3({
x: this.x + other.x,
y: this.y + other.y,
z: this.z + other.z
});
};
vec3.prototype.rotateAroundAxis = function(axis, amt) {
var v1 = this.scale(Math.cos(amt));
var v2 = axis.cross(this).scale(Math.sin(amt));
var v3 = axis.scale(this.dot(axis)).scale(1.0-Math.cos(amt));
return v1.add(v2).add(v3);
};