-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsorder.js
More file actions
94 lines (85 loc) · 1.97 KB
/
sorder.js
File metadata and controls
94 lines (85 loc) · 1.97 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
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
const Sorder = (object, property, { frequency, springiness, response }) => {
if (Array.isArray(property)) {
let updaters = property.map(prop => Sorder(object, prop, { frequency, springiness, response }));
return (...args) => {
for (const update of updaters) {
update(...args);
}
};
}
// @t3ssel8r is godsend
// f -> frequency
// zeta -> springiness
// r -> response
let x = object[property];
let xp = object[property];
let y = object[property];
let yd = 0;
let k1 = springiness / (Math.PI * frequency),
k2 = 1 / ((2 * Math.PI * frequency) ** 2),
k3 = response * springiness / (2 * Math.PI * frequency);
const update = (T) => {
const xd = (x - xp) / T;
xp = x;
const k2_stable = Math.max(k2, 1.1 * (T*T/4 + T*k1/2));
y += T*yd
yd += T * (x + k3*xd - y - k1*yd) / k2_stable;
}
Reflect.defineProperty(object, property, {
get() {
return y;
},
set(value) {
x = value;
}
});
return update;
}
const createSpring = ({
initial = 0,
frequency = 2,
springiness = 1,
response = 1,
} = {}) => {
let x = initial; // target
let xp = initial; // last target
let y = initial; // current value
let yd = 0; // current velocity
const k1 = springiness / (Math.PI * frequency);
const k2 = 1 / ((2 * Math.PI * frequency) ** 2);
const k3 = response * springiness / (2 * Math.PI * frequency);
const update = (dt) => {
const xd = (x - xp) / dt;
xp = x;
const k2_stable = Math.max(k2, 1.1 * (dt * dt / 4 + dt * k1 / 2));
y += dt * yd;
yd += dt * (x + k3 * xd - y - k1 * yd) / k2_stable;
};
return {
update,
get value() {
return y;
},
get velocity() {
return yd;
},
set target(v) {
x = v;
},
set velocity(v) {
yd = v;
},
set value(v) {
y = v;
},
snapTo(v) {
x = v;
xp = v;
y = v;
yd = 0;
},
impulse(v) {
yd += v;
}
};
};